extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/dao/NoSpecimen.java

Code
Comments
Other
Rev Date Author Line
1608 20 Apr 12 nicklas 1 package net.sf.basedb.reggie.dao;
1608 20 Apr 12 nicklas 2
1608 20 Apr 12 nicklas 3 import java.util.ArrayList;
4964 03 Sep 18 nicklas 4 import java.util.Collection;
1608 20 Apr 12 nicklas 5 import java.util.List;
1608 20 Apr 12 nicklas 6
4964 03 Sep 18 nicklas 7 import net.sf.basedb.core.AnnotationRestriction;
4964 03 Sep 18 nicklas 8 import net.sf.basedb.core.AnnotationSimpleRestriction;
4964 03 Sep 18 nicklas 9 import net.sf.basedb.core.AnnotationType;
1608 20 Apr 12 nicklas 10 import net.sf.basedb.core.DbControl;
1608 20 Apr 12 nicklas 11 import net.sf.basedb.core.Include;
4075 05 Sep 16 nicklas 12 import net.sf.basedb.core.InvalidDataException;
1608 20 Apr 12 nicklas 13 import net.sf.basedb.core.ItemQuery;
4964 03 Sep 18 nicklas 14 import net.sf.basedb.core.Operator;
1608 20 Apr 12 nicklas 15 import net.sf.basedb.core.Sample;
1608 20 Apr 12 nicklas 16 import net.sf.basedb.core.Type;
6576 09 Feb 22 nicklas 17 import net.sf.basedb.core.query.Annotations;
1608 20 Apr 12 nicklas 18 import net.sf.basedb.core.query.Expressions;
1608 20 Apr 12 nicklas 19 import net.sf.basedb.core.query.Hql;
1608 20 Apr 12 nicklas 20 import net.sf.basedb.core.query.Orders;
1608 20 Apr 12 nicklas 21 import net.sf.basedb.core.query.Restrictions;
2917 11 Nov 14 nicklas 22 import net.sf.basedb.reggie.Reggie;
1608 20 Apr 12 nicklas 23
1608 20 Apr 12 nicklas 24 /**
1608 20 Apr 12 nicklas 25   Class for loading information that is related to 'NoSpecimen' virtual samples.
1608 20 Apr 12 nicklas 26   
1608 20 Apr 12 nicklas 27   @author nicklas
1608 20 Apr 12 nicklas 28   @since 2.5
1608 20 Apr 12 nicklas 29 */
1608 20 Apr 12 nicklas 30 public class NoSpecimen 
1608 20 Apr 12 nicklas 31   extends ReggieItem<Sample>
1608 20 Apr 12 nicklas 32 {
1608 20 Apr 12 nicklas 33
1608 20 Apr 12 nicklas 34   /**
1608 20 Apr 12 nicklas 35     Find all 'NoSpecimen' items associated with a given case. This method can either
1623 26 Apr 12 nicklas 36     load all (including merged) items, or only items that
1608 20 Apr 12 nicklas 37     are really part of the original case.
1608 20 Apr 12 nicklas 38   */
1623 26 Apr 12 nicklas 39   public static List<NoSpecimen> findByCase(DbControl dc, Case theCase, String originalCaseName, boolean useBiopsyTypeAnnotation)
1608 20 Apr 12 nicklas 40   {
1608 20 Apr 12 nicklas 41     ItemQuery<Sample> noSpecimenQuery = theCase.getSample().getChildSamples();
1608 20 Apr 12 nicklas 42     Subtype.NO_SPECIMEN.addFilter(dc, noSpecimenQuery);
1623 26 Apr 12 nicklas 43     
1623 26 Apr 12 nicklas 44     if (originalCaseName != null)
1608 20 Apr 12 nicklas 45     {
2840 20 Oct 14 nicklas 46       String suffix = null;
2840 20 Oct 14 nicklas 47       if (originalCaseName.length() > 7) 
2840 20 Oct 14 nicklas 48       {
2840 20 Oct 14 nicklas 49         suffix = originalCaseName.substring(7, 8);
2840 20 Oct 14 nicklas 50         originalCaseName = originalCaseName.substring(0, 7);
2840 20 Oct 14 nicklas 51       }
1623 26 Apr 12 nicklas 52       noSpecimenQuery.restrict(Restrictions.like(Hql.property("name"), Expressions.parameter("name", originalCaseName + ".%", Type.STRING)));
1623 26 Apr 12 nicklas 53       if (useBiopsyTypeAnnotation)
1623 26 Apr 12 nicklas 54       {
2840 20 Oct 14 nicklas 55         SpecimenTube.addBiopsyTypeFilter(dc, noSpecimenQuery, suffix);
1623 26 Apr 12 nicklas 56       }
1608 20 Apr 12 nicklas 57     }
1608 20 Apr 12 nicklas 58     noSpecimenQuery.order(Orders.asc(Hql.property("name")));
1608 20 Apr 12 nicklas 59     noSpecimenQuery.include(Include.ALL);
1623 26 Apr 12 nicklas 60     List<NoSpecimen> list = new ArrayList<NoSpecimen>();
1608 20 Apr 12 nicklas 61     for (Sample s : noSpecimenQuery.list(dc))
1608 20 Apr 12 nicklas 62     {
1623 26 Apr 12 nicklas 63       list.add(new NoSpecimen(s));
1608 20 Apr 12 nicklas 64     }
1623 26 Apr 12 nicklas 65     return list;
1608 20 Apr 12 nicklas 66   }
1623 26 Apr 12 nicklas 67   
1623 26 Apr 12 nicklas 68   /**
1623 26 Apr 12 nicklas 69     Find all 'NoSpecimen' items that are linked by name to a given case name. The items start with the 
1623 26 Apr 12 nicklas 70     same name as the case and then have a suffix with a dot plus a running number. Eg. xxx.1, xxx.2, etc.
1623 26 Apr 12 nicklas 71     <p>
1623 26 Apr 12 nicklas 72     If useBiopsyTypeAnnotation is set the lookup will use the 'BiopsyType' annotation
2840 20 Oct 14 nicklas 73     when searching for samples. If the case name has a suffix 'C' or 'D' only samples annotated 
2840 20 Oct 14 nicklas 74     with BiopsyType=SpecimenCoreBiopsy/SpecimenCoreBiopsy2nd are considered. If no suffix is specified 
2840 20 Oct 14 nicklas 75     SpecimenCoreBiopsy* samples are ignored.
1623 26 Apr 12 nicklas 76   */
1623 26 Apr 12 nicklas 77   public static List<NoSpecimen> findByCaseName(DbControl dc, String name, boolean useBiopsyTypeAnnotation)
1623 26 Apr 12 nicklas 78   {
2840 20 Oct 14 nicklas 79     String suffix = null;
2840 20 Oct 14 nicklas 80     if (name.length() > 7) 
2840 20 Oct 14 nicklas 81     {
2840 20 Oct 14 nicklas 82       suffix = name.substring(7, 8);
2840 20 Oct 14 nicklas 83       name = name.substring(0, 7);
2840 20 Oct 14 nicklas 84     }
1608 20 Apr 12 nicklas 85     
1623 26 Apr 12 nicklas 86     ItemQuery<Sample> specimenQuery = Sample.getQuery();
1623 26 Apr 12 nicklas 87     Subtype.NO_SPECIMEN.addFilter(dc, specimenQuery);
1623 26 Apr 12 nicklas 88     specimenQuery.restrict(Restrictions.like(Hql.property("name"), Expressions.parameter("name", name + ".%", Type.STRING)));
1623 26 Apr 12 nicklas 89     specimenQuery.order(Orders.asc(Hql.property("name")));
2917 11 Nov 14 nicklas 90     specimenQuery.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
1623 26 Apr 12 nicklas 91     
1623 26 Apr 12 nicklas 92     if (useBiopsyTypeAnnotation)
1623 26 Apr 12 nicklas 93     {
2840 20 Oct 14 nicklas 94       SpecimenTube.addBiopsyTypeFilter(dc, specimenQuery, suffix);
1623 26 Apr 12 nicklas 95     }
1623 26 Apr 12 nicklas 96     
1623 26 Apr 12 nicklas 97     List<NoSpecimen> items = new ArrayList<NoSpecimen>();
1623 26 Apr 12 nicklas 98     for (Sample s : specimenQuery.list(dc))
1623 26 Apr 12 nicklas 99     {    
1623 26 Apr 12 nicklas 100       items.add(new NoSpecimen(s));
1623 26 Apr 12 nicklas 101     }
1623 26 Apr 12 nicklas 102     return items;
1623 26 Apr 12 nicklas 103   }
6576 09 Feb 22 nicklas 104   
6576 09 Feb 22 nicklas 105   /**
6576 09 Feb 22 nicklas 106     Find all 'NoSpecimen' items that are linked by name to a given case name but
6576 09 Feb 22 nicklas 107     where a specimen exists at an external lab. This is the same as the 'findByCaseName'
6576 09 Feb 22 nicklas 108     method with additional requirement that "ExternalSpecimenExists=Yes".
6576 09 Feb 22 nicklas 109     @since 4.36
6576 09 Feb 22 nicklas 110   */
6576 09 Feb 22 nicklas 111   public static List<NoSpecimen> findWhereExternalSpecimenExists(DbControl dc, String name)
6576 09 Feb 22 nicklas 112   {
6576 09 Feb 22 nicklas 113     String suffix = null;
6576 09 Feb 22 nicklas 114     if (name.length() > 7) 
6576 09 Feb 22 nicklas 115     {
6576 09 Feb 22 nicklas 116       suffix = name.substring(7, 8);
6576 09 Feb 22 nicklas 117       name = name.substring(0, 7);
6576 09 Feb 22 nicklas 118     }
6576 09 Feb 22 nicklas 119     
6576 09 Feb 22 nicklas 120     ItemQuery<Sample> specimenQuery = Sample.getQuery();
6576 09 Feb 22 nicklas 121     Subtype.NO_SPECIMEN.addFilter(dc, specimenQuery);
6576 09 Feb 22 nicklas 122     specimenQuery.join(Annotations.innerJoin(Annotationtype.EXTERNAL_SPECIMEN_EXISTS.get(dc), "es"));
6576 09 Feb 22 nicklas 123     specimenQuery.restrict(Restrictions.eq(Hql.alias("es"), Expressions.string("Yes")));
6576 09 Feb 22 nicklas 124     specimenQuery.restrict(Restrictions.like(Hql.property("name"), Expressions.parameter("name", name + ".%", Type.STRING)));
6576 09 Feb 22 nicklas 125     specimenQuery.order(Orders.asc(Hql.property("name")));
6576 09 Feb 22 nicklas 126     specimenQuery.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
6576 09 Feb 22 nicklas 127     
6576 09 Feb 22 nicklas 128     SpecimenTube.addBiopsyTypeFilter(dc, specimenQuery, suffix);
6576 09 Feb 22 nicklas 129     
6576 09 Feb 22 nicklas 130     List<NoSpecimen> items = new ArrayList<NoSpecimen>();
6576 09 Feb 22 nicklas 131     for (Sample s : specimenQuery.list(dc))
6576 09 Feb 22 nicklas 132     {    
6576 09 Feb 22 nicklas 133       items.add(new NoSpecimen(s));
6576 09 Feb 22 nicklas 134     }
6576 09 Feb 22 nicklas 135     return items;
1623 26 Apr 12 nicklas 136
6576 09 Feb 22 nicklas 137   }
6576 09 Feb 22 nicklas 138
4075 05 Sep 16 nicklas 139   /**
4075 05 Sep 16 nicklas 140      Find a 'NoSpecimen' item with the given external ID.
4075 05 Sep 16 nicklas 141      @return A 'NoSpecimen' item, or null if not found
4075 05 Sep 16 nicklas 142      @since 4.7
4075 05 Sep 16 nicklas 143   */
4075 05 Sep 16 nicklas 144   public static NoSpecimen findByExternalId(DbControl dc, String externalId)
4075 05 Sep 16 nicklas 145   {
4075 05 Sep 16 nicklas 146     NoSpecimen item = null;
1623 26 Apr 12 nicklas 147     
4075 05 Sep 16 nicklas 148     ItemQuery<Sample> query = Sample.getQuery();
4075 05 Sep 16 nicklas 149     Subtype.NO_SPECIMEN.addFilter(dc, query);
4075 05 Sep 16 nicklas 150     query.restrict(Restrictions.eq(Hql.property("externalId"), Expressions.string(externalId)));
4075 05 Sep 16 nicklas 151     query.order(Orders.desc(Hql.property("name")));
4075 05 Sep 16 nicklas 152     query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
4075 05 Sep 16 nicklas 153     
4075 05 Sep 16 nicklas 154     List<Sample> items = query.list(dc);
4075 05 Sep 16 nicklas 155     if (items.size() > 1)
4075 05 Sep 16 nicklas 156     {
4075 05 Sep 16 nicklas 157       throw new InvalidDataException(
4075 05 Sep 16 nicklas 158           "More than one 'NoSpecimen' item with the external id '" + externalId + "' was found. " +
4075 05 Sep 16 nicklas 159           "This wizard can't be used until that is corrected.");
4075 05 Sep 16 nicklas 160     }
4075 05 Sep 16 nicklas 161     if (items.size() == 1)
4075 05 Sep 16 nicklas 162     {
4075 05 Sep 16 nicklas 163       item = new NoSpecimen(items.get(0));
4075 05 Sep 16 nicklas 164     }
4075 05 Sep 16 nicklas 165     return item;
4075 05 Sep 16 nicklas 166   }
4075 05 Sep 16 nicklas 167
4105 15 Sep 16 nicklas 168   /**
4964 03 Sep 18 nicklas 169     Find a specimen tubes by PAD. Not that multiple tubes
4964 03 Sep 18 nicklas 170     may be registered with the same PAD. Note that if the logged
4964 03 Sep 18 nicklas 171     in user doesn't have permission to access PAD information
4964 03 Sep 18 nicklas 172     this method will throw a PermissionDeniedException.
4964 03 Sep 18 nicklas 173     
4964 03 Sep 18 nicklas 174     @param dc DbControl The DbControl to use.
4964 03 Sep 18 nicklas 175     @param pad The PAD to look for
4964 03 Sep 18 nicklas 176     @since 4.19.1
4964 03 Sep 18 nicklas 177   */
4964 03 Sep 18 nicklas 178   public static List<NoSpecimen> findByPAD(DbControl dc, String pad)
4964 03 Sep 18 nicklas 179   {
4964 03 Sep 18 nicklas 180     AnnotationType padType = Annotationtype.PAD.load(dc);
4964 03 Sep 18 nicklas 181   
4964 03 Sep 18 nicklas 182     ItemQuery<Sample> tubeQuery = Sample.getQuery();
6864 04 Nov 22 nicklas 183     Subtype.NO_SPECIMEN.addFilter(dc, tubeQuery);
4964 03 Sep 18 nicklas 184     tubeQuery.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
4964 03 Sep 18 nicklas 185     tubeQuery.restrict(new AnnotationSimpleRestriction(null, padType, Operator.EQ, pad, new AnnotationRestriction.Options()));
4964 03 Sep 18 nicklas 186     List<Sample> tubes = tubeQuery.list(dc);
4964 03 Sep 18 nicklas 187     
4964 03 Sep 18 nicklas 188     return toList(tubes);
4964 03 Sep 18 nicklas 189   }
4964 03 Sep 18 nicklas 190
4964 03 Sep 18 nicklas 191   /**
4105 15 Sep 16 nicklas 192     Get a NoSpecimen item when the id is known.
4105 15 Sep 16 nicklas 193     @since 4.7
4105 15 Sep 16 nicklas 194   */
4105 15 Sep 16 nicklas 195   public static NoSpecimen getById(DbControl dc, int id)
4105 15 Sep 16 nicklas 196   {
4105 15 Sep 16 nicklas 197     return new NoSpecimen(Sample.getById(dc, id));
4105 15 Sep 16 nicklas 198   }
4105 15 Sep 16 nicklas 199
4964 03 Sep 18 nicklas 200   /**
4964 03 Sep 18 nicklas 201     @since 4.19.1
4964 03 Sep 18 nicklas 202   */
4964 03 Sep 18 nicklas 203   public static List<NoSpecimen> toList(Collection<Sample> samples)
4964 03 Sep 18 nicklas 204   {
4964 03 Sep 18 nicklas 205     List<NoSpecimen> tubes = new ArrayList<NoSpecimen>(samples.size());
4964 03 Sep 18 nicklas 206     for (Sample s : samples)
4964 03 Sep 18 nicklas 207     {
4964 03 Sep 18 nicklas 208       tubes.add(new NoSpecimen(s));
4964 03 Sep 18 nicklas 209     }
4964 03 Sep 18 nicklas 210     return tubes;
4964 03 Sep 18 nicklas 211   }
4964 03 Sep 18 nicklas 212
4964 03 Sep 18 nicklas 213   
1608 20 Apr 12 nicklas 214   private NoSpecimen(Sample sample)
1608 20 Apr 12 nicklas 215   {
1608 20 Apr 12 nicklas 216     super(sample);
1608 20 Apr 12 nicklas 217   }  
1608 20 Apr 12 nicklas 218   
1608 20 Apr 12 nicklas 219   
1608 20 Apr 12 nicklas 220   /**
1608 20 Apr 12 nicklas 221     Get the real sample that represents this speciment tube in BASE.
1608 20 Apr 12 nicklas 222   */
1608 20 Apr 12 nicklas 223   public Sample getSample()
1608 20 Apr 12 nicklas 224   {
1608 20 Apr 12 nicklas 225     return getItem();
1608 20 Apr 12 nicklas 226   }
1608 20 Apr 12 nicklas 227
5136 22 Nov 18 nicklas 228   /**
5136 22 Nov 18 nicklas 229     Get the parent case.
5136 22 Nov 18 nicklas 230     @since 4.21
5136 22 Nov 18 nicklas 231   */
5136 22 Nov 18 nicklas 232   public Case getCase()
5136 22 Nov 18 nicklas 233   {
5136 22 Nov 18 nicklas 234     Sample noSpecimen = getItem();
5136 22 Nov 18 nicklas 235     Sample theCase = (Sample)noSpecimen.getParent();
5136 22 Nov 18 nicklas 236     return Case.get(theCase);
5136 22 Nov 18 nicklas 237   }
1608 20 Apr 12 nicklas 238
1608 20 Apr 12 nicklas 239 }