Changeset 7122


Ignore:
Timestamp:
Apr 19, 2016, 1:00:41 PM (7 years ago)
Author:
Nicklas Nordborg
Message:

References #2000: Batch API for annotation handling

Check that annotation values are valid as specified by the annotation type.

Added support for units.

File:
1 edited

Legend:

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

    r7121 r7122  
    3939import net.sf.basedb.core.snapshot.SnapshotManager;
    4040import net.sf.basedb.util.EqualsHelper;
     41import net.sf.basedb.util.units.UnitConverter;
    4142
    4243/**
     
    334335     
    335336      CurrentAnnotationInfo info = new CurrentAnnotationInfo(at);
    336 //      info.id = at.getId();
    337 //      info.valueType = at.getValueType();
    338337      currentInfo.put(at.getId(), info);
    339338    }
     
    511510      else
    512511      {
     512        // Check that values are allowed by annotation type
     513        // and convert to the default unit if it is needed
     514        UnitConverter converter = null;
     515        if (unit != null && info.defaultUnit != null && !unit.equals(info.defaultUnit))
     516        {
     517          // Two different unit, convert the values to the default unit
     518          converter = info.defaultUnit.getUnitConverter(unit);
     519        }
     520       
     521        List<Object> convertedValues = new ArrayList<Object>(values.size());
     522        for (Object value : values)
     523        {
     524          if (value instanceof Number)
     525          {
     526            Number numericValue = (Number)value;
     527            if (converter != null)
     528            {
     529              numericValue = converter.convertToSpecificUnit(numericValue.doubleValue());
     530            }
     531            value = info.valueType.convertNumber(numericValue);
     532          }
     533          annotationType.validateAnnotationValue(value);
     534          convertedValues.add(value);
     535        }
     536       
    513537        if (infoIsForCurrentItem)
    514538        {
    515539          // Update the current annotation if the value(s) have changed
    516           if (!EqualsHelper.invariantEquals(values, info.values))
     540          if (!EqualsHelper.invariantEquals(convertedValues, info.values))
    517541          {
    518542            // Update
     
    523547           
    524548            // Insert into **Values
    525             for (Object v : values)
     549            for (Object v : convertedValues)
    526550            {
    527551              insertValues[info.valueType.ordinal()].addToBatch(info.valueId, v);
     
    529553           
    530554            // Update last_update, unit, version in Annotations
    531             updateAnnotations.addToBatch(info.version+1, info.unitId, batchDate, info.annotationId, info.version);
     555            updateAnnotations.addToBatch(info.version+1, unit != null ? unit.getId() : info.unitId, batchDate, info.annotationId, info.version);
    532556          }
    533557        }
     
    541565         
    542566          // Insert into **Values
    543           for (Object v : values)
     567          for (Object v : convertedValues)
    544568          {
    545569            insertValues[info.valueType.ordinal()].addToBatch(valueId, v);
     
    579603  }
    580604 
     605  /**
     606    A flag indicating what type of change that was made to an
     607    annotation as a result of calling
     608    {@link AnnotationBatcher#setValues(AnnotationType, List, Unit)}
     609  */
    581610  public static enum Change
    582611  {
     612    /**
     613      The annotation was not changed.
     614    */
    583615    NO_CHANGE,
     616   
     617    /**
     618      A new annotation was created.
     619    */
    584620    ADDED,
     621   
     622    /**
     623      An existing annotation was updated.
     624    */
    585625    UPDATED,
     626   
     627    /**
     628      An existing annotation was deleted.
     629    */
    586630    DELETED;
    587631  }
    588  
    589   /**
    590     Holds information about an annotation type.
    591   */
    592   /*
    593   static class AnnotationTypeInfo
    594   {
    595     int id;
    596     Type valueType;
    597   }
    598   */
    599632 
    600633  /**
     
    614647    final int annotationTypeId;
    615648    final Type valueType;
     649    final Unit defaultUnit;
    616650   
    617651    // Updated in setCurrentItem
     
    625659    final List<Object> values;
    626660   
    627     public CurrentAnnotationInfo(AnnotationType at)
     661    CurrentAnnotationInfo(AnnotationType at)
    628662    {
    629663      this.annotationTypeId = at.getId();
    630664      this.valueType = at.getValueType();
     665      this.defaultUnit = at.getDefaultUnit();
    631666      this.values = new ArrayList<Object>();
    632667    }
Note: See TracChangeset for help on using the changeset viewer.