extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/counter/YellowLabelStatistics.java

Code
Comments
Other
Rev Date Author Line
3770 26 Feb 16 nicklas 1 package net.sf.basedb.reggie.counter;
3770 26 Feb 16 nicklas 2
3770 26 Feb 16 nicklas 3 import java.util.HashSet;
3770 26 Feb 16 nicklas 4 import java.util.Iterator;
3770 26 Feb 16 nicklas 5 import java.util.Set;
3770 26 Feb 16 nicklas 6
3770 26 Feb 16 nicklas 7 import net.sf.basedb.core.DbControl;
3770 26 Feb 16 nicklas 8 import net.sf.basedb.core.ItemQuery;
3770 26 Feb 16 nicklas 9 import net.sf.basedb.core.Sample;
3770 26 Feb 16 nicklas 10 import net.sf.basedb.core.query.Annotations;
3770 26 Feb 16 nicklas 11 import net.sf.basedb.reggie.Reggie;
3770 26 Feb 16 nicklas 12 import net.sf.basedb.reggie.dao.Annotationtype;
3770 26 Feb 16 nicklas 13 import net.sf.basedb.reggie.dao.Subtype;
3770 26 Feb 16 nicklas 14
3770 26 Feb 16 nicklas 15 /**
3770 26 Feb 16 nicklas 16   Utility class for collecting statistics about YellowLabel specimen.
3770 26 Feb 16 nicklas 17   This class is designed to collect and internally store information
3770 26 Feb 16 nicklas 18   about yellow label specimen that may take some time to calculate.
3770 26 Feb 16 nicklas 19   
3770 26 Feb 16 nicklas 20   Code should normally not create new instances of this class.
3770 26 Feb 16 nicklas 21   The {@link CounterService} instance is maintaining an instance
3770 26 Feb 16 nicklas 22   which is updated at regular intervals. See {@link CounterService#getYellowLabelStatistics()}
3770 26 Feb 16 nicklas 23   
3770 26 Feb 16 nicklas 24   @since 4.2
3770 26 Feb 16 nicklas 25 */
3770 26 Feb 16 nicklas 26 public class YellowLabelStatistics
3770 26 Feb 16 nicklas 27 {
3770 26 Feb 16 nicklas 28
3770 26 Feb 16 nicklas 29   private long lastUpdated;
3770 26 Feb 16 nicklas 30   private Set<String> yellowLabelSpecimenNames;
3770 26 Feb 16 nicklas 31   
3770 26 Feb 16 nicklas 32   /**
3770 26 Feb 16 nicklas 33     Creates a new instance. Do not use from regular code.
3770 26 Feb 16 nicklas 34     Use {@link CounterService#getYellowLabelStatistics()} 
3770 26 Feb 16 nicklas 35     instead.
3770 26 Feb 16 nicklas 36   */
3770 26 Feb 16 nicklas 37   YellowLabelStatistics()
3770 26 Feb 16 nicklas 38   {
3770 26 Feb 16 nicklas 39     this.yellowLabelSpecimenNames = new HashSet<String>();
3770 26 Feb 16 nicklas 40   }
3770 26 Feb 16 nicklas 41   
3770 26 Feb 16 nicklas 42   /**
3770 26 Feb 16 nicklas 43     Get the time the statistics was last updated.
3770 26 Feb 16 nicklas 44   */
3770 26 Feb 16 nicklas 45   public long getLastUpdated()
3770 26 Feb 16 nicklas 46   {
3770 26 Feb 16 nicklas 47     return lastUpdated;
3770 26 Feb 16 nicklas 48   }
3770 26 Feb 16 nicklas 49   
3770 26 Feb 16 nicklas 50   /**
3770 26 Feb 16 nicklas 51     Update the statistics about yellow label specimen. 
3770 26 Feb 16 nicklas 52     Do not use from regular code. The {@link CounterService}
3770 26 Feb 16 nicklas 53     is calling this code at regular intervals.
3770 26 Feb 16 nicklas 54   */
3770 26 Feb 16 nicklas 55   synchronized void updateStatistics(DbControl dc)
3770 26 Feb 16 nicklas 56   {
3770 26 Feb 16 nicklas 57     // Load all yellow-label specimen
3770 26 Feb 16 nicklas 58     ItemQuery<Sample> query = Sample.getQuery();
3770 26 Feb 16 nicklas 59     Subtype.SPECIMEN.addFilter(dc, query);
3770 26 Feb 16 nicklas 60     query.include(Reggie.INCLUDE_IN_CURRENT_PROJECT);
3770 26 Feb 16 nicklas 61     // Inner join will create implicit restriction so that only 
3770 26 Feb 16 nicklas 62     // specimen that has a YellowLabel annotation are returned
3770 26 Feb 16 nicklas 63     query.join(Annotations.innerJoin(Annotationtype.YELLOW_LABEL.get(dc), "yl"));
3770 26 Feb 16 nicklas 64     
3770 26 Feb 16 nicklas 65     Set<String> yellowTmp = new HashSet<String>();
3770 26 Feb 16 nicklas 66     Iterator<Sample> it = query.iterate(dc);
3770 26 Feb 16 nicklas 67     while (it.hasNext())
3770 26 Feb 16 nicklas 68     {
3770 26 Feb 16 nicklas 69       yellowTmp.add(it.next().getName());
3770 26 Feb 16 nicklas 70     }
3770 26 Feb 16 nicklas 71     
3770 26 Feb 16 nicklas 72     lastUpdated = System.currentTimeMillis();
3770 26 Feb 16 nicklas 73     yellowLabelSpecimenNames = yellowTmp;
3770 26 Feb 16 nicklas 74   }
3770 26 Feb 16 nicklas 75   
3770 26 Feb 16 nicklas 76   /**
3770 26 Feb 16 nicklas 77     Is the item with the given name a descendant to a yellow-label
3770 26 Feb 16 nicklas 78     specimen? The check is implemented by comparing first 9
3770 26 Feb 16 nicklas 79     characters of the item name with the names of known yellow-label
3770 26 Feb 16 nicklas 80     specimen.
3770 26 Feb 16 nicklas 81   */
3770 26 Feb 16 nicklas 82   public boolean isYellowLabelDescendant(String itemName)
3770 26 Feb 16 nicklas 83   {
3770 26 Feb 16 nicklas 84     String specimenName = itemName.substring(0, Math.min(9, itemName.length()));
3770 26 Feb 16 nicklas 85     return yellowLabelSpecimenNames.contains(specimenName);
3770 26 Feb 16 nicklas 86   }
3770 26 Feb 16 nicklas 87   
3770 26 Feb 16 nicklas 88 }