Changeset 7120


Ignore:
Timestamp:
Apr 15, 2016, 9:56:30 AM (7 years ago)
Author:
Nicklas Nordborg
Message:

References #2001: The annotation importer should only replace existing values when the new values are different

It turned out to be easier to make this change in the core instead of in the plug-in. The Annotation.setValue() and Annotation.setValues() method has been deprecated and replaced with Annotation.setValueIfDifferent() and Annotation.setValuesIfDifferent() that only update the values if they are not the same as the current values. Both methods return a boolean with TRUE indicating that a change was made. The old methods simply calls the new methods except that they don't return any value.

Location:
trunk/src
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/clients/web/net/sf/basedb/clients/web/Base.java

    r6958 r7120  
    15101510            oValues[j] = formatter.parseString((String)jsonValues.get(j));
    15111511          }
    1512           a.setValues(Arrays.asList(oValues), unit);
     1512          a.setValuesIfDifferent(Arrays.asList(oValues), unit);
    15131513        }
    15141514      }
  • trunk/src/core/net/sf/basedb/core/Annotation.java

    r7109 r7120  
    113113      data.setSource(Source.CLONED.ordinal());
    114114      data.setValues(Type.fromValue(inheritedFrom.getAnnotationType().getValueType()).newParameterValueData());
    115       data.getValues().replaceValues(inheritedFrom.getValues().getValues());
     115      data.getValues().replaceValuesIfDifferent(inheritedFrom.getValues().getValues());
    116116      data.setUnit(inheritedFrom.getUnit());
    117117      data.setLastUpdate(inheritedFrom.getLastUpdate());
     
    526526  }
    527527
     528  @Deprecated
    528529  public void setValue(Object value)
    529530    throws PermissionDeniedException, InvalidDataException, BaseException
    530531  {
    531     setValue(value, null);
     532    setValueIfDifferent(value, null);
    532533  }
    533534 
     
    544545    @throws BaseException If there is another error
    545546    @since 2.9
    546   */
     547    @deprecated In 3.8, use {@link #setValueIfDifferent(Object, Unit)} instead
     548  */
     549  @Deprecated
    547550  public void setValue(Object value, Unit unit)
     551    throws PermissionDeniedException, InvalidDataException, BaseException
     552  {
     553    setValueIfDifferent(value, unit);
     554  }
     555
     556  /**
     557    Set the value of the annotation, replacing any previous values if they
     558    are different. No change is made if the new value is the same
     559    as the existing value.
     560   
     561    @param value The new value
     562    @param unit The unit of the value, or null to use the default unit of
     563      the annotation type
     564    @throws PermissionDeniedException If the logged in user doesn't have
     565      write permission for the annotation or read permission for the annotation
     566      type
     567    @throws InvalidDataException If the value isn't a valid value for the
     568      annotation type, see {@link AnnotationType#validateAnnotationValue(Object)}
     569    @throws BaseException If there is another error
     570    @since 3.8
     571  */
     572  public boolean setValueIfDifferent(Object value, Unit unit)
    548573    throws PermissionDeniedException, InvalidDataException, BaseException
    549574  {
     
    570595      getData().setUnit(unit == null ? annotationType.getData().getDefaultUnit() : unit.getData());
    571596    }
    572     getData().getValues().setSingleValue(Values.getDataValue(value));
    573     getData().setLastUpdate(new Date());
    574     getAnnotationSet().setSnapshotInvalid();
    575   }
    576 
     597   
     598    boolean different = getData().getValues().setSingleValueIfDifferent(Values.getDataValue(value));
     599    if (different)
     600    {
     601      getData().setLastUpdate(new Date());
     602      getAnnotationSet().setSnapshotInvalid();
     603    }
     604    return different;
     605  }
     606
     607 
     608  @Deprecated
    577609  public void setValues(List<?> values)
    578610    throws PermissionDeniedException, InvalidDataException, BaseException
    579611  {
    580     setValues(values, null);
    581   }
    582  
    583   /**
    584     Set the values of the annotation, replacing any previous values.
     612    setValuesIfDifferent(values, null);
     613  }
     614 
     615  /**
     616    Set the values of the annotation, replacing any previous values. NOTE!
     617    As of BASE 3.8 the values are only replaced if the new values are
     618    different from the existing values.
     619   
     620    @deprecated In 3.8, use {@link #setValuesIfDifferent(List, Unit)} instead
     621  */
     622  @Deprecated
     623  public void setValues(List<?> values, Unit unit)
     624    throws PermissionDeniedException, InvalidDataException, BaseException
     625  {
     626    setValuesIfDifferent(values, unit);
     627  }
     628
     629  /**
     630    Set the values of the annotation, replacing any previous values if they
     631    are different. No change is made if the new list of values is the same
     632    as the existing values.
     633   
    585634    @param values A list containing the new values
    586635    @param unit The unit of the value, or null to use the default unit of
     
    594643      {@link AnnotationType#validateAnnotationValue(Object)}
    595644    @throws BaseException If there is another error
    596   */
    597   public void setValues(List<?> values, Unit unit)
     645    @since 3.8
     646  */
     647  public boolean setValuesIfDifferent(List<?> values, Unit unit)
    598648    throws PermissionDeniedException, InvalidDataException, BaseException
    599649  {
     
    634684      getData().setUnit(unit == null ? annotationType.getData().getDefaultUnit() : unit.getData());
    635685    }
    636     getData().getValues().replaceValues(Values.getDataValues(convertedValues));
    637     getData().setLastUpdate(new Date());
    638     getAnnotationSet().setSnapshotInvalid();
    639   }
    640 
     686   
     687    boolean different = getData().getValues().replaceValuesIfDifferent(Values.getDataValues(convertedValues));
     688    if (different)
     689    {
     690      getData().setLastUpdate(new Date());
     691      getAnnotationSet().setSnapshotInvalid();
     692    };
     693    return different;
     694  }
     695
     696 
    641697  /**
    642698    If this is a cloned annotation, resync the cloned values. This method
     
    657713    }
    658714   
    659     data.getValues().replaceValues(inherited.getValues().getValues());
    660     data.setLastUpdate(inherited.getLastUpdate());
    661     data.setUnit(inherited.getUnit());
    662    
    663     getAnnotationSet().setSnapshotInvalid();
     715    if (data.getValues().replaceValuesIfDifferent(inherited.getValues().getValues()))
     716    {
     717      data.setLastUpdate(inherited.getLastUpdate());
     718      data.setUnit(inherited.getUnit());
     719      getAnnotationSet().setSnapshotInvalid();
     720    }
    664721  }
    665722 
  • trunk/src/core/net/sf/basedb/core/AnnotationSet.java

    r7052 r7120  
    723723        if (a.getId() != 0)
    724724        {
    725           a.getValues().replaceValues(null); // Ensure values are initialized in case logging is needed
     725          a.getValues().replaceValuesIfDifferent(null); // Ensure values are initialized in case logging is needed
    726726          Annotation ann = dc.getItem(Annotation.class, a);
    727727          dc.deleteItem(ann);
     
    12561256     
    12571257      // Copy the values
    1258       current.getValues().replaceValues(from.getValues().getValues());
    1259       current.setUnit(from.getUnit());
    1260       current.setLastUpdate(new Date());
    1261       setSnapshotInvalid();
     1258      if (current.getValues().replaceValuesIfDifferent(from.getValues().getValues()))
     1259      {
     1260        current.setUnit(from.getUnit());
     1261        current.setLastUpdate(new Date());
     1262        setSnapshotInvalid();
     1263      }
    12621264    }
    12631265   
  • trunk/src/core/net/sf/basedb/core/AnnotationType.java

    r7103 r7120  
    11691169      getData().setEnumerationValues(pv);
    11701170    }
    1171     pv.replaceValues(Values.getDataValues(values));
     1171    pv.replaceValuesIfDifferent(Values.getDataValues(values));
    11721172  }
    11731173 
  • trunk/src/core/net/sf/basedb/core/Install.java

    r7107 r7120  
    21242124        {
    21252125          ParameterValueData<?> enumValues = type.newParameterValueData();
    2126           enumValues.replaceValues(Arrays.asList(enumeration));
     2126          enumValues.replaceValuesIfDifferent(Arrays.asList(enumeration));
    21272127          at.setEnumeration(true);
    21282128          at.setEnumerationValues(enumValues);
  • trunk/src/core/net/sf/basedb/core/Job.java

    r7119 r7120  
    16101610        toParameter.setLabel(fromParameter.getLabel());
    16111611        toParameter.setDescription(fromParameter.getDescription());
    1612         toParameter.replaceValues(fromParameter.getValues());
     1612        toParameter.replaceValuesIfDifferent(fromParameter.getValues());
    16131613        to.put(toName, toParameter);
    16141614      }
     
    16571657      parameterValue.setDescription(description);
    16581658      parameterValue.setMasked(parameterType.isMasked());
    1659       parameterValue.replaceValues(Values.getDataValues(values));
     1659      parameterValue.replaceValuesIfDifferent(Values.getDataValues(values));
    16601660      old = getData().getParameters().put(name, parameterValue);
    16611661    }
  • trunk/src/core/net/sf/basedb/core/PluginConfiguration.java

    r6881 r7120  
    566566      parameterValue.setLabel(label);
    567567      parameterValue.setDescription(description);
    568       parameterValue.replaceValues(Values.getDataValues(values));
     568      parameterValue.replaceValuesIfDifferent(Values.getDataValues(values));
    569569      getData().getConfigurationValues().put(param, parameterValue);
    570570    }
     
    611611          toParameter.setLabel(fromParameter.getLabel());
    612612          toParameter.setDescription(fromParameter.getDescription());
    613           toParameter.replaceValues(fromParameter.getValues());
     613          toParameter.replaceValuesIfDifferent(fromParameter.getValues());
    614614          to.put(toName, toParameter);
    615615        }
  • trunk/src/core/net/sf/basedb/core/data/DateParameterValueData.java

    r7096 r7120  
    2828
    2929import net.sf.basedb.core.DateUtil;
     30import net.sf.basedb.util.EqualsHelper;
    3031
    3132/**
     
    8485    Overrides the parent method since we want to get rid of the
    8586    time part of the date.
     87    @since 3.8
    8688  */
    8789  @Override
    88   public void replaceValues(List<?> values)
     90  public boolean replaceValuesIfDifferent(List<?> values)
    8991  {
    9092    List<Date> current = getValues();
     93    if (EqualsHelper.invariantEquals(values, current)) return false;
     94   
    9195    storeCurrentValuesAsOld(current);
    9296    current.clear();
     
    98102      }
    99103    }
     104    return true;
    100105  }
    101106 
     
    103108    Overrides the parent method since we want to get rid of the
    104109    time part of the date.
     110    @since 3.8
    105111  */
    106112  @Override
    107   public void setSingleValue(Object value)
     113  public boolean setSingleValueIfDifferent(Object value)
    108114  {
    109115    List<Date> current = getValues();
     116    if (current != null && current.size() == 1 && EqualsHelper.equals(current.get(0), value))
     117    {
     118      return false;
     119    }
     120
    110121    storeCurrentValuesAsOld(current);
    111122    current.clear();
    112123    current.add(DateUtil.truncate((Date)value));
     124    return true;
    113125  }
    114126 
  • trunk/src/core/net/sf/basedb/core/data/ParameterValueData.java

    r7096 r7120  
    2929import net.sf.basedb.core.AnnotationSet;
    3030import net.sf.basedb.core.Type;
     31import net.sf.basedb.util.EqualsHelper;
    3132
    3233/**
     
    5152  ParameterValueData(T... values)
    5253  {
    53     replaceValues(Arrays.asList(values));
     54    replaceValuesIfDifferent(Arrays.asList(values));
    5455  }
    5556
     
    150151    the last update timestamp: {@link AnnotationData#setLastUpdate(java.util.Date)}.
    151152    and to invalidate the annotation set snapshot: {@link AnnotationSet#setSnapshotInvalid()}
     153    @return TRUE if the values were replaced, FALSE if not
     154    @since 3.8
    152155  */
    153156  @SuppressWarnings("unchecked")
    154   public void replaceValues(List<?> values)
     157  public boolean replaceValuesIfDifferent(List<?> values)
    155158  {
    156159    List<T> current = getValues();
     160    if (EqualsHelper.invariantEquals(values, current)) return false;
     161   
    157162    storeCurrentValuesAsOld(current);
    158163    current.clear();
    159164    if (values != null) current.addAll((List<T>) values);
     165    return true;
    160166  }
    161167 
     
    168174    the last update timestamp: {@link AnnotationData#setLastUpdate(java.util.Date)}.
    169175    and to invalidate the annotation set snapshot: {@link AnnotationSet#setSnapshotInvalid()}
     176    @return TRUE if the values were replaced, FALSE if not
     177    @since 3.8
    170178  */
    171179  @SuppressWarnings("unchecked")
    172   public void setSingleValue(Object value)
     180  public boolean setSingleValueIfDifferent(Object value)
    173181  {
    174182    List<T> current = getValues();
     183    if (current != null && current.size() == 1 && EqualsHelper.equals(current.get(0), value))
     184    {
     185      return false;
     186    }
     187   
    175188    storeCurrentValuesAsOld(current);
    176189    current.clear();
    177190    current.add((T) value);
     191    return true;
    178192  }
    179193 
  • trunk/src/core/net/sf/basedb/util/EqualsHelper.java

    r6125 r7120  
    2121*/
    2222package net.sf.basedb.util;
     23
     24import java.util.List;
    2325
    2426/**
     
    110112  }
    111113 
     114  /**
     115    Checks if two lists contains equal elements disregarding
     116    their positions in the array. Eg. [1, 2] is equal to [2, 1]
     117    @param l1 The first list
     118    @param l2 The second list
     119    @return TRUE if both list contains the same elements
     120    @since 3.8
     121  */
     122  public static boolean invariantEquals(List<?> l1, List<?> l2)
     123  {
     124    if (l1 == l2) return true;
     125    if (l1 == null || l2 == null) return false;
     126    if (l1.size() != l2.size()) return false;
     127   
     128    boolean[] matched = new boolean[l2.size()];
     129    for (Object o1 : l1)
     130    {
     131      boolean found = false;
     132      for (int j = 0; j < l2.size(); ++j)
     133      {
     134        if (!matched[j] && equals(o1, l2.get(j)))
     135        {
     136          matched[j] = true;
     137          found = true;
     138          break;
     139        }
     140      }
     141      if (!found) return false;
     142    }
     143    return true;
     144  }
     145
     146 
    112147}
  • trunk/src/plugins/core/net/sf/basedb/plugins/AnnotationFlatFileImporter.java

    r6921 r7120  
    2929import java.util.Collection;
    3030import java.util.Collections;
     31import java.util.Date;
    3132import java.util.EnumSet;
    3233import java.util.HashMap;
     
    719720  protected void beginData()
    720721  {
     722    System.out.println(new Date()+": begin");
    721723    this.dc = sc.newDbControl();
    722724    this.unitCache = new UnitCache(dc);
     
    767769  {
    768770
     771    if (data.dataLineNo() % 100 == 0)
     772    {
     773      System.out.println(new Date()+": line " + data.dataLineNo());
     774    }
     775   
    769776    // Find the item(s) to annotate. Mapper gives us name OR external ID
    770777    String nameOrId = itemMapper.getValue(data);
     
    920927  protected void end(boolean success)
    921928  {
     929    System.out.println(new Date()+": end start");
    922930    try
    923931    {
     
    930938        {
    931939          current++;
     940
    932941          if (progress != null)
    933942          {
     
    945954            ThreadSignalHandler.checkInterrupted();
    946955          }
    947         }
    948        
     956         
     957          if (current % 100 == 0)
     958          {
     959            System.out.println(new Date()+": " + current + "; " + numItems + " items; " + numAnnotations + " annotations");
     960          }
     961
     962        }
     963        System.out.println(new Date()+": commit start");
    949964        dc.commit();
     965        System.out.println(new Date()+": commit end");
    950966        if (progress != null) progress.display(100, "Done");
    951967      }
     
    13911407              if (as == null) as = item.getAnnotationSet();
    13921408              Annotation a = as.getAnnotation(at);
     1409             
    13931410              List<Object> theValues = new ArrayList<Object>(newValues.size());
    13941411              // Make sure all annotation values are converted to the same unit
     
    14071424                theValues.add(theValue);
    14081425              }
    1409               a.setValues(theValues, unit);
    1410               numSet += size;
    1411               if (hasAnnotation && !merge) numReplaced += size;
     1426              if (a.setValuesIfDifferent(theValues, unit))
     1427              {
     1428                numSet += size;
     1429                if (hasAnnotation && !merge) numReplaced += size;
     1430              }
    14121431            }
    14131432            else
  • trunk/src/test/TestAnnotation.java

    r7107 r7120  
    156156        l.addAll(Arrays.asList(value));
    157157        Unit unit = unitId != 0 ? Unit.getById(dc, unitId) : null;
    158         a.setValues(l, unit);
     158        a.setValuesIfDifferent(l, unit);
    159159      }
    160160      dc.commit();
     
    364364      l.addAll(Arrays.asList(value));
    365365      Unit unit = unitId != 0 ? Unit.getById(dc, unitId) : null;
    366       a.setValues(l, unit);
     366      a.setValuesIfDifferent(l, unit);
    367367      dc.commit();
    368368      dc = TestUtil.getDbControl();
  • trunk/src/test/TestBfsExporterImporter.java

    r6875 r7120  
    513513      {
    514514        Object value = values[i];
    515         ba.getAnnotationSet().getAnnotation(at).setValue(value);
     515        ba.getAnnotationSet().getAnnotation(at).setValueIfDifferent(value, null);
    516516        ++i;
    517517        if (i >= values.length) i = 0;
  • trunk/src/test/net/sf/basedb/test/roles/Util.java

    r5983 r7120  
    231231    AnnotationSet as = item.getAnnotationSet();
    232232    Annotation a = as.getAnnotation(at);
    233     a.setValue(value, unit);
     233    a.setValueIfDifferent(value, unit);
    234234  }
    235235 
Note: See TracChangeset for help on using the changeset viewer.