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 |
Utility class for collecting statistics about YellowLabel specimen. |
3770 |
26 Feb 16 |
nicklas |
This class is designed to collect and internally store information |
3770 |
26 Feb 16 |
nicklas |
about yellow label specimen that may take some time to calculate. |
3770 |
26 Feb 16 |
nicklas |
19 |
|
3770 |
26 Feb 16 |
nicklas |
Code should normally not create new instances of this class. |
3770 |
26 Feb 16 |
nicklas |
The {@link CounterService} instance is maintaining an instance |
3770 |
26 Feb 16 |
nicklas |
which is updated at regular intervals. See {@link CounterService#getYellowLabelStatistics()} |
3770 |
26 Feb 16 |
nicklas |
23 |
|
3770 |
26 Feb 16 |
nicklas |
@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 |
Creates a new instance. Do not use from regular code. |
3770 |
26 Feb 16 |
nicklas |
Use {@link CounterService#getYellowLabelStatistics()} |
3770 |
26 Feb 16 |
nicklas |
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 |
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 |
Update the statistics about yellow label specimen. |
3770 |
26 Feb 16 |
nicklas |
Do not use from regular code. The {@link CounterService} |
3770 |
26 Feb 16 |
nicklas |
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 |
// 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 |
// Inner join will create implicit restriction so that only |
3770 |
26 Feb 16 |
nicklas |
// 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 |
Is the item with the given name a descendant to a yellow-label |
3770 |
26 Feb 16 |
nicklas |
specimen? The check is implemented by comparing first 9 |
3770 |
26 Feb 16 |
nicklas |
characters of the item name with the names of known yellow-label |
3770 |
26 Feb 16 |
nicklas |
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 |
} |