Changeset 5176


Ignore:
Timestamp:
Oct 30, 2009, 11:25:44 AM (12 years ago)
Author:
Nicklas Nordborg
Message:

References #1397: Function to inherit annotations for all experimental factors in the experimental factors table

The core part is implemented. It now uses the cached snapshots to lookup annotations so it should be a lot faster than before, though I have no measurements on this.

It remains to change the gui so that multiple experimental factors can be selected and inherited in one go.

Location:
trunk
Files:
2 edited

Legend:

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

    r5122 r5176  
    2323package net.sf.basedb.core;
    2424
     25import java.util.Collection;
    2526import java.util.Collections;
    2627import java.util.Date;
     28import java.util.HashSet;
    2729import java.util.LinkedList;
    2830import java.util.List;
     
    3537import net.sf.basedb.core.query.Restrictions;
    3638import net.sf.basedb.core.query.Hql;
     39import net.sf.basedb.core.signal.ThreadSignalHandler;
    3740import net.sf.basedb.core.snapshot.AnnotationSetSnapshot;
     41import net.sf.basedb.core.snapshot.AnnotationSnapshot;
     42import net.sf.basedb.core.snapshot.AnnotationTypeFilter;
    3843import net.sf.basedb.core.snapshot.SnapshotManager;
    3944
     
    752757      found annotation is inherited, otherwise only the found annotation is inherited
    753758    @since 2.9
     759    @deprecated In 2.15, use {@link #autoInherit(DbControl, Collection, SnapshotManager, boolean)}
     760      instead since it has better performance
    754761  */
    755762  public int autoInherit(DbControl dc, final AnnotationType annotationType, boolean preferAnnotationSet)
    756763  {
    757     if (findAnnotations(dc, annotationType, null).size() > 0) return 0;
     764    return autoInherit(dc, Collections.singletonList(annotationType), new SnapshotManager(), preferAnnotationSet);
     765  }
     766 
     767  /**
     768    Automatically inherit all annotations of one or more specified annotation
     769    types from parent items that are currntly missing from this annotation
     770    set
     771    <p>
     772    This method will only consider parent items that the logged in user
     773    has access to with USE permission.
     774    <p>
     775    This method may result in inheriting multiple annotations (from different
     776    parent items) of the specified annotation type.
     777   
     778    @param dc The DbControl to use for database access
     779    @param annotationTypes The annotation type to look for
     780    @param manager A snapshot manager that is used to look up annotations
     781    @param preferAnnotationSet If TRUE, the entire annotation set containing the
     782      found annotation is inherited, otherwise only the found annotation is inherited
     783    @since 2.15
     784  */
     785  public int autoInherit(DbControl dc, Collection<AnnotationType> annotationTypes,
     786    SnapshotManager manager, boolean preferAnnotationSet)
     787  {
     788    if (annotationTypes == null || annotationTypes.isEmpty()) return 0;
     789    // Find the annotation types that are currently missing from this annotation set
     790    Set<AnnotationType> missing = new HashSet<AnnotationType>();
     791    AnnotationSetSnapshot snapshot = manager.getSnapshot(dc, getId());
     792    AnnotationTypeFilter atFilter = new AnnotationTypeFilter();
     793    for (AnnotationType at : annotationTypes)
     794    {
     795      atFilter.setAnnotationType(at);
     796      if (manager.findAnnotations(dc, snapshot, atFilter, true).size() == 0)
     797      {
     798        missing.add(at);
     799      }
     800    }
     801    if (missing.isEmpty()) return 0;
     802    ThreadSignalHandler.checkInterrupted();
     803
     804    // Find all annotated parent items for
     805    // which the logged in user has USE permission
    758806    int numInherited = 0;
    759807    Annotatable item = getItem(dc);
    760    
    761     // Filter that only returns items that are annotated with the specified
    762     // annotation type and for which the logged in user has USE permission
    763     Filter<Annotatable> filter = new Filter<Annotatable>()
     808    Filter<Annotatable> parentFilter = new Filter<Annotatable>()
    764809    {
    765810      @Override
    766811      public boolean evaluate(Annotatable a)
    767812      {
    768         return a.isAnnotated() &&
    769           a.hasPermission(Permission.USE) &&
    770           a.getAnnotationSet().hasAnnotation(annotationType);
     813        return a.isAnnotated() && a.hasPermission(Permission.USE);
    771814      }
    772815    };
    773    
    774     // This will find all parent items that we should inherit from
    775     Set<Annotatable> allParents = AnnotationUtil.getAllAnnotatableParentItems(dc, item, filter);
     816    Set<Annotatable> allParents = AnnotationUtil.getAllAnnotatableParentItems(dc, item, parentFilter);
     817
     818    // For each parent -- inherit any annotation that match the given annotation types
    776819    for (Annotatable parent : allParents)
    777820    {
     821      ThreadSignalHandler.checkInterrupted();
    778822      AnnotationSet pas = parent.getAnnotationSet();
    779       numInherited++;
    780       if (preferAnnotationSet)
     823      snapshot = manager.getSnapshot(dc, pas.getId());
     824     
     825      for (AnnotationType at : missing)
    781826      {
    782         inheritAnnotationSet(pas);
     827        atFilter.setAnnotationType(at);
     828        List<AnnotationSnapshot> tmp = manager.findAnnotations(dc, snapshot, atFilter, false);
     829        if (tmp.size() > 0)
     830        {
     831          numInherited++;
     832          if (preferAnnotationSet)
     833          {
     834            inheritAnnotationSet(pas);
     835            break;  // No need to check more annotation types since the complete set is inherited
     836          }
     837          else
     838          {
     839            inheritAnnotation(tmp.get(0).getAnnotation(dc));
     840          }
     841        }
    783842      }
    784       else
    785       {
    786         inheritAnnotation(pas.getAnnotation(annotationType));
    787       }
    788843    }
    789844    return numInherited;
    790845  }
     846
    791847 
    792848  /**
  • trunk/www/views/experiments/index.jsp

    r5060 r5176  
    4444  import="net.sf.basedb.core.PermissionDeniedException"
    4545  import="net.sf.basedb.core.ItemAlreadyExistsException"
     46  import="net.sf.basedb.core.snapshot.SnapshotManager"
    4647  import="net.sf.basedb.util.RemovableUtil"
    4748  import="net.sf.basedb.util.ShareableUtil"
     
    352353    int numInherited = 0;
    353354    int numRawBioAssays = 0;
     355    SnapshotManager manager = new SnapshotManager();
     356    List<AnnotationType> annotationTypes = Collections.singletonList(at);
    354357    for (RawBioAssay rba : query.list(dc))
    355358    {
    356359      numRawBioAssays++;
    357       numInherited += rba.getAnnotationSet().autoInherit(dc, at, false);
     360      numInherited += rba.getAnnotationSet().autoInherit(dc, annotationTypes, manager, false);
    358361    }
    359362    dc.commit();
Note: See TracChangeset for help on using the changeset viewer.