Changeset 6539


Ignore:
Timestamp:
Sep 26, 2014, 12:44:24 PM (8 years ago)
Author:
Nicklas Nordborg
Message:

References #1843: Use snapshot manager to load annotations on list pages

Updated the snapshots so that units are also saved in them.

The samples list page has been updated to use the snapshots. The AnnotationLoaderUtil was created to make it easy to use the snapshots in a list context.

Location:
trunk
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/Type.java

    r6127 r6539  
    3636import net.sf.basedb.core.hibernate.TypeWrapper;
    3737
     38import java.io.IOException;
     39import java.io.ObjectInputStream;
     40import java.io.ObjectOutputStream;
     41import java.io.Serializable;
    3842import java.text.NumberFormat;
    3943import java.text.ParsePosition;
     
    7983      return Integer.valueOf(num.intValue());
    8084    }
     85
     86    @Override
     87    public void writeValue(Serializable value, ObjectOutputStream out)
     88      throws IOException
     89    {
     90      out.writeInt((Integer)value);
     91    }
     92   
     93    @Override
     94    public Serializable readValue(ObjectInputStream in)
     95      throws IOException
     96    {
     97      return in.readInt();
     98    }
    8199  },
    82100
     
    107125    {
    108126      return Long.valueOf(num.longValue());
     127    }
     128
     129    @Override
     130    public void writeValue(Serializable value, ObjectOutputStream out)
     131      throws IOException
     132    {
     133      out.writeLong((Long)value);
     134    }
     135   
     136    @Override
     137    public Serializable readValue(ObjectInputStream in)
     138      throws IOException
     139    {
     140      return in.readLong();
    109141    }
    110142  },
     
    139171      return Float.valueOf(num.floatValue());
    140172    }
     173   
     174    @Override
     175    public void writeValue(Serializable value, ObjectOutputStream out)
     176      throws IOException
     177    {
     178      out.writeFloat((Float)value);
     179    }
     180   
     181    @Override
     182    public Serializable readValue(ObjectInputStream in)
     183      throws IOException
     184    {
     185      return in.readFloat();
     186    }
    141187  },
    142188
     
    170216      return Double.valueOf(num.doubleValue());
    171217    }
     218   
     219    @Override
     220    public void writeValue(Serializable value, ObjectOutputStream out)
     221      throws IOException
     222    {
     223      out.writeDouble((Double)value);
     224    }
     225   
     226    @Override
     227    public Serializable readValue(ObjectInputStream in)
     228      throws IOException
     229    {
     230      return in.readDouble();
     231    }
    172232  },
    173233
     
    194254      return value;
    195255    }
     256
     257    @Override
     258    public void writeValue(Serializable value, ObjectOutputStream out)
     259      throws IOException
     260    {
     261      out.writeObject((String)value);
     262    }
     263   
     264    @Override
     265    public Serializable readValue(ObjectInputStream in)
     266      throws IOException, ClassNotFoundException
     267    {
     268      return (String)in.readObject();
     269    }
    196270  },
    197271
     
    217291    {
    218292      return value;
     293    }
     294   
     295    @Override
     296    public void writeValue(Serializable value, ObjectOutputStream out)
     297      throws IOException
     298    {
     299      out.writeObject((String)value);
     300    }
     301   
     302    @Override
     303    public Serializable readValue(ObjectInputStream in)
     304      throws IOException, ClassNotFoundException
     305    {
     306      return (String)in.readObject();
    219307    }
    220308  },
     
    250338      return net.sf.basedb.util.Values.getBoolean(value);
    251339    }
     340   
     341    @Override
     342    public void writeValue(Serializable value, ObjectOutputStream out)
     343      throws IOException
     344    {
     345      out.writeBoolean((Boolean)value);
     346    }
     347   
     348    @Override
     349    public Serializable readValue(ObjectInputStream in)
     350      throws IOException
     351    {
     352      return (Boolean)in.readBoolean();
     353    }
    252354  },
    253355
     
    272374    {
    273375      return value == null ? 0 : 4;
     376    }
     377   
     378    @Override
     379    public void writeValue(Serializable value, ObjectOutputStream out)
     380      throws IOException
     381    {
     382      out.writeLong(((Date)value).getTime());
     383    }
     384   
     385    @Override
     386    public Serializable readValue(ObjectInputStream in)
     387      throws IOException
     388    {
     389      return new Date(in.readLong());
    274390    }
    275391  },
     
    296412    {
    297413      return value == null ? 0 : 8;
     414    }
     415   
     416    @Override
     417    public void writeValue(Serializable value, ObjectOutputStream out)
     418      throws IOException
     419    {
     420      out.writeLong(((Date)value).getTime());
     421    }
     422   
     423    @Override
     424    public Serializable readValue(ObjectInputStream in)
     425      throws IOException
     426    {
     427      return new Date(in.readLong());
    298428    }
    299429  };
     
    494624  */
    495625  public abstract int sizeOf(Object value);
     626 
     627  /**
     628    Write a value of this type to the output stream. Eg. depending
     629    on which Type is ised one of ObjectOutputStream.write*(value)
     630    methods should be used.
     631    @param value A value which must be of the correct type (not checked)
     632    @param out Stream to write to
     633    @since 3.4
     634  */
     635  public abstract void writeValue(Serializable value, ObjectOutputStream out)
     636    throws IOException;
     637 
     638  /**
     639    Read a value of this type to the input stream. Eg. depending
     640    on which Type is ised one of ObjectInputStream.read*(value)
     641    methods should be used.
     642    @param in Stream to read from
     643    @since 3.4
     644  */
     645  public abstract Serializable readValue(ObjectInputStream in)
     646    throws IOException, ClassNotFoundException;
    496647 
    497648  /**
  • trunk/src/core/net/sf/basedb/core/Unit.java

    r6127 r6539  
    3333import net.sf.basedb.util.formatter.Formatter;
    3434import net.sf.basedb.util.formatter.PrefixSuffixFormatter;
     35import net.sf.basedb.util.units.LinearUnitConverter;
     36import net.sf.basedb.util.units.UnitConverter;
    3537
    3638/**
     
    618620 
    619621  /**
     622    Create a unit converter for converting from the
     623    reference unit (other) to this unit.
     624    @param other The reference unit
     625    @return A unit converter
     626    @since 3.4
     627  */
     628  public UnitConverter getUnitConverter(Unit other)
     629  {
     630    return new LinearUnitConverter(
     631      getReferenceFactor(), getReferenceOffset(),
     632      other.getReferenceFactor(), other.getReferenceOffset());
     633  }
     634 
     635  /**
    620636    Find the unit with a given symbol that has the same
    621637    quantity as this unit.
  • trunk/src/core/net/sf/basedb/core/snapshot/AnnotationSetSnapshot.java

    r5740 r6539  
    4343  <li>The item the annotation set is linked to
    4444  <li>The primary annotations (including their values)
     45  <li>Unit the values should be displayed in (values are
     46    always stored in the unit of the annotation type)
    4547  <li>Inherited annotations (not including the values)
    4648  <li>Inherited annotation sets (not including the values)
     
    6971{
    7072
    71   private static final long serialVersionUID = -6903210659055245440L;
    72  
     73  private static final long serialVersionUID = -7092844726985995667L;
     74
    7375  /**
    7476    The version number for the file format in use when serializing
     
    7779    are not used and that new files are generated instead.
    7880  */
    79   public static final int FILE_VERSION = 2;
     81  public static final int FILE_VERSION = Integer.valueOf(3).intValue();
    8082 
    8183  private transient long created;
  • trunk/src/core/net/sf/basedb/core/snapshot/AnnotationSnapshot.java

    r6065 r6539  
    3636import net.sf.basedb.core.PermissionDeniedException;
    3737import net.sf.basedb.core.Type;
     38import net.sf.basedb.core.Unit;
    3839import net.sf.basedb.core.data.AnnotationData;
    3940import net.sf.basedb.core.data.AnnotationSetData;
     
    6162{
    6263
    63   private static final long serialVersionUID = -4919156087324273194L;
    64 
     64  private static final long serialVersionUID = 5826828832517594630L;
     65 
    6566  private transient boolean inherited;
    6667  private transient int annotationId;
    6768  private transient int annotationSetId;
    6869  private transient int annotationTypeId;
     70  private transient int unitId;
     71  private transient Type valueType;
    6972  private transient List<? extends Serializable> values;
    7073  private transient int itemId;
     
    8992    this.annotationSetId = primary.getAnnotationSet().getId();
    9093    this.annotationTypeId = primary.getAnnotationType().getId();
     94    this.unitId = primary.getUnit() == null ? 0 : primary.getUnit().getId();
     95    this.valueType = Type.fromValue(primary.getAnnotationType().getValueType());
    9196    this.values = new ArrayList<Serializable>((List<Serializable>)primary.getValues().getValues());
    9297  }
     
    128133    this.annotationTypeId = primary.annotationTypeId;
    129134    this.annotationSetId = primary.annotationSetId;
     135    this.unitId = primary.unitId;
     136    this.valueType = primary.valueType;
    130137    this.values = primary.values;
    131138    this.itemId = primary.itemId;
     
    245252
    246253  /**
     254    Get the id of the unit that the annotation values should
     255    use when displayed.
     256    @return The unit id, or 0 if this annotatation doesn't use units
     257    @since 3.4
     258  */
     259  public int getUnitId()
     260  {
     261    return unitId;
     262  }
     263 
     264  /**
     265    Get the unit.
     266    @return A Unit object or null if this annotation doesn't use units
     267    @since 3.4
     268  */
     269  public Unit getUnit(DbControl dc)
     270  {
     271    return unitId == 0 ? null : Unit.getById(dc, unitId);
     272  }
     273
     274  /**
    247275    Get the annotation values. NOTE! The values are only available
    248276    for primary annotations and for inherited annotations returned by
     
    259287    Get the annotations values, optionally converted to some specific unit
    260288    and type of values.
     289   
     290    NOTE! To create a unit converter that converts that values to the unit
     291    specified by {@link #getUnit(DbControl)}:
     292   
     293    this.getUnit(dc).getUnitConverter(this.getAnnotationType(dc).getDefaultUnit())
     294   
    261295    @param converter A converter for converting (numerical) values,
    262296      or null to not convert the values
     
    330364    out.writeInt(annotationSetId);
    331365    out.writeInt(annotationTypeId);
     366    out.writeInt(unitId);
     367    out.writeInt(valueType.getValue());
    332368    out.writeInt(values == null ? 0 : values.size());
    333369    if (values != null)
     
    335371      for (Serializable tmp : values)
    336372      {
    337         out.writeObject(tmp);
     373        valueType.writeValue(tmp, out);
    338374      }
    339375    }
     
    352388    annotationSetId = in.readInt();
    353389    annotationTypeId = in.readInt();
     390    unitId = in.readInt();
     391    valueType = Type.fromValue(in.readInt());
    354392    int numValues = in.readInt();
    355393    if (numValues > 0)
     
    358396      for (int i = 0; i < numValues; ++i)
    359397      {
    360         values.add((Serializable)in.readObject());
     398        values.add(valueType.readValue(in));
    361399      }
    362400      this.values = values;
  • trunk/www/biomaterials/samples/list_samples.jsp

    r6497 r6539  
    5656  import="net.sf.basedb.core.plugin.GuiContext"
    5757  import="net.sf.basedb.core.plugin.Plugin"
     58  import="net.sf.basedb.core.snapshot.AnnotationLoaderUtil"
     59  import="net.sf.basedb.core.snapshot.AnnotationTypeFilter"
     60  import="net.sf.basedb.core.snapshot.AnnotationSnapshot"
     61  import="net.sf.basedb.core.snapshot.AnnotationSetSnapshot"
     62  import="net.sf.basedb.core.snapshot.SnapshotManager"
    5863  import="net.sf.basedb.core.Type"
    5964  import="net.sf.basedb.util.Enumeration"
     
    7883  import="java.util.Map"
    7984  import="java.util.Date"
     85  import="java.util.ArrayList"
    8086%>
    8187<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
     
    97103final DbControl dc = sc.newDbControl();
    98104ItemResultIterator<Sample> samples = null;
    99 ItemResultList<AnnotationType> annotationTypes = null;
     105List<AnnotationLoaderUtil> annotationLoaders = new ArrayList<AnnotationLoaderUtil>();
     106long time = 0;
    100107try
    101108{
     
    128135  Unit microGram = UnitUtil.getUnit(dc, Quantity.MASS, "µg");
    129136  Map<Plugin.MainType, Integer> pluginCount = PluginDefinition.countPlugins(dc, guiContext);
    130   annotationTypes = annotationTypeQuery.list(dc);
     137  SnapshotManager manager = new SnapshotManager();
     138  for (AnnotationType at : annotationTypeQuery.list(dc))
     139  {
     140    annotationLoaders.add(new AnnotationLoaderUtil(dc, manager, at));
     141  }
    131142  try
    132143  {
     
    398409      />
    399410      <%
    400       for (AnnotationType at : annotationTypes)
     411      for (AnnotationLoaderUtil loader : annotationLoaders)
    401412      {
     413        AnnotationType at = loader.getAnnotationType();
    402414        Enumeration<String, String> annotationEnum = null;
    403415        Formatter formatter = FormatterFactory.getTypeFormatter(sc, at.getValueType());
     
    589601          if (samples != null)
    590602          {           
     603            time = -System.currentTimeMillis();
    591604            while (samples.hasNext())
    592605            {
     
    835848                <tbl:cell column="description"><%=HTML.encodeTags(item.getDescription())%></tbl:cell>               
    836849                <%
    837                 AnnotationSet as = item.isAnnotated() ? item.getAnnotationSet() : null;
    838                 if (as != null)
     850                if (item.isAnnotated())
    839851                {
    840                   for (AnnotationType at : annotationTypes)
     852                  AnnotationSetSnapshot snapshot = manager.getSnapshot(dc, item.getAnnotationSet().getId());
     853                  for (AnnotationLoaderUtil loader : annotationLoaders)
    841854                  {
    842                     if (as.hasAnnotation(at))
     855                    if (loader.find(snapshot))
    843856                    {
    844                       Annotation a = as.getAnnotation(at);
    845                       String suffix = a.getUnitSymbol(null);
    846                       if (suffix != null) suffix = "&nbsp;" + suffix;
    847857                      %>
    848858                      <tbl:cell
    849                         column="<%="at"+at.getId()%>"
     859                        column="<%="at"+loader.getId()%>"
    850860                        ><tbl:cellvalue
    851                           list="<%=a.getValues(null)%>"
    852                           suffix="<%=suffix%>"
     861                          list="<%=loader.getValues()%>"
     862                          suffix="<%=loader.getUnitSymbol()%>"
    853863                      /></tbl:cell>
    854864                      <%
     
    883893              }
    884894            }
     895            time += System.currentTimeMillis();
     896            %>
     897              <tbl:panel clazz="messagepanel">
     898                <%=time %> ms
     899              </tbl:panel>
     900            <%
    885901            if (numListed == 0)
    886902            {
Note: See TracChangeset for help on using the changeset viewer.