Ignore:
Timestamp:
Nov 14, 2011, 2:07:58 PM (10 years ago)
Author:
Martin Svensson
Message:

Merging trunk with branch 2.0

Location:
extensions/net.sf.basedb.reggie/trunk
Files:
9 edited
1 copied

Legend:

Unmodified
Added
Removed
  • extensions/net.sf.basedb.reggie/trunk

  • extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/Reggie.java

    r1461 r1463  
    88import net.sf.basedb.core.AnnotationSet;
    99import net.sf.basedb.core.AnnotationType;
     10import net.sf.basedb.core.AnnotationTypeCategory;
    1011import net.sf.basedb.core.DbControl;
    1112import net.sf.basedb.core.Group;
     
    1516import net.sf.basedb.core.ItemNotFoundException;
    1617import net.sf.basedb.core.ItemQuery;
     18import net.sf.basedb.core.ItemSubtype;
    1719import net.sf.basedb.core.Type;
    1820import net.sf.basedb.core.query.Expressions;
    1921import net.sf.basedb.core.query.Hql;
    2022import net.sf.basedb.core.query.Restrictions;
     23import net.sf.basedb.reggie.dao.Subtype;
    2124
    2225/**
     
    3033    The current version of this package.
    3134  */
    32   public static final String VERSION = "1.6";
     35  public static final String VERSION = "2.0-dev";
    3336
    3437  /**
     
    178181  public static final String ANNOTATION_PARTITION_DATE = "PartitionDate";
    179182 
     183 
     184  /**
     185    List all subtypes for the given item type that has the given name.
     186    @since 2.0
     187  */
     188  public static List<ItemSubtype> listSubtypes(DbControl dc, Subtype subtype)
     189  {
     190    ItemQuery<ItemSubtype> query = ItemSubtype.getQuery(subtype.getMainType());
     191    query.restrict(
     192      Restrictions.eq(
     193        Hql.property("name"),
     194        Expressions.parameter("name", subtype.getName(), Type.STRING)
     195      ));
     196    query.include(Include.ALL);
     197    return query.list(dc);
     198  }
     199 
     200  /**
     201    Find a single item subtype with the given name. If exactly one is found it
     202    is returned, otherwise null is returned or an exception is thrown
     203    depending on the exceptionIfNotFound parameter.
     204  */
     205  public static ItemSubtype findSubtype(DbControl dc, Subtype subtype, boolean exceptionIfNotFound)
     206  {
     207    List<ItemSubtype> result = listSubtypes(dc, subtype);
     208    ItemSubtype s = null;
     209    if (result.size() == 0)
     210    {
     211      if (exceptionIfNotFound) throw new ItemNotFoundException("ItemSubtype["+subtype.getName()+"]");
     212    }
     213    else if (result.size() > 1)
     214    {
     215      if (exceptionIfNotFound) throw new InvalidDataException("Found > 1 ItemSubtype["+subtype.getName()+"]");
     216    }
     217    else
     218    {
     219      s = result.get(0);
     220    }
     221    return s;
     222  }
     223 
    180224  /**
    181225    List all groups with the given name.
     
    214258  }
    215259
     260  /**
     261    List all annotation types with the given name.
     262    @since 2.0
     263  */
     264  public static List<AnnotationTypeCategory> listAnnotationTypeCategories(DbControl dc, String name)
     265  {
     266    ItemQuery<AnnotationTypeCategory> query = AnnotationTypeCategory.getQuery();
     267    query.restrict(Restrictions.eq(Hql.property("name"), Expressions.parameter("name", name, Type.STRING)));
     268    query.include(Include.ALL);
     269    return query.list(dc);
     270  }
     271 
    216272  /**
    217273    List all annotation types with the given name.
  • extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/dao/Case.java

    r1366 r1463  
    6666      merged = true;
    6767      caseQuery = Sample.getQuery();
    68       caseQuery.join(Hql.innerJoin("sourceEvents", "src"));
    69       caseQuery.join(Hql.innerJoin("src", "event", "evt"));
     68      caseQuery.join(Hql.innerJoin("childCreationEvents", "cce"));
     69      caseQuery.join(Hql.innerJoin("cce", "event", "evt"));
    7070      caseQuery.join(Hql.innerJoin("evt", "bioMaterial", "bm")); // 'bm' should now refernce a specimen tube
    7171      caseQuery.restrict(Restrictions.rlike(Hql.property("bm", "name"), Expressions.parameter("name", "^" + name + "\\.[0-9]+$", Type.STRING)));
    7272      caseQuery.setDistinct(true);
    7373     
    74       System.out.println("cq:" + caseQuery);
    7574      cases = caseQuery.list(dc);
    7675     
  • extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/dao/Patient.java

    r1333 r1463  
    1111import net.sf.basedb.core.Item;
    1212import net.sf.basedb.core.ItemQuery;
     13import net.sf.basedb.core.Sample;
    1314import net.sf.basedb.core.query.Annotations;
    1415import net.sf.basedb.reggie.Reggie;
     
    6162  {
    6263    Patient patient = null;
    63     BioSource b = theCase.getSample().getBioSource();
     64    Sample s = theCase.getSample();
     65    BioSource b = s.getParentType() == Item.BIOSOURCE ? (BioSource)s.getParent() : null;
    6466    if (b != null)
    6567    {
     
    100102    loadAnnotations(dc, "gender", Reggie.ANNOTATION_GENDER, null);
    101103    loadAnnotations(dc, "dateOfBirth", Reggie.ANNOTATION_DATE_OF_BIRTH, new DateToStringConverter(new SimpleDateFormat("yyyy-MM-dd")));
    102     /*
    103     // Date-of-birth need special handling for formatting
    104     AnnotationType dateOfBirthType = Reggie.findAnnotationType(dc, Item.BIOSOURCE, Reggie.ANNOTATION_DATE_OF_BIRTH, true);
    105     SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    106     String dateOfBirth = df.format((Date)Reggie.getAnnotationValue(getBioSource(), dateOfBirthType));
    107     setAnnotation("dateOfBirth", dateOfBirth);
    108     */
    109 
    110104  }
    111105
  • extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/servlet/ConsentFormServlet.java

    r1368 r1463  
    2626import net.sf.basedb.reggie.dao.Case;
    2727import net.sf.basedb.reggie.dao.SpecimenTube;
     28import net.sf.basedb.reggie.dao.Subtype;
    2829import net.sf.basedb.util.Values;
    2930import net.sf.basedb.util.error.ThrowableUtil;
     
    162163          String caseName = (String)jsonCase.get("name");
    163164          theCase = Sample.getNew(dc);
     165          theCase.setItemSubtype(Subtype.CASE.load(dc));
    164166          theCase.setName(caseName);
    165167          if ("Not asked".equals(consent))
  • extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/servlet/InstallServlet.java

    r1426 r1463  
    22
    33import java.io.IOException;
    4 import java.util.ArrayList;
    54import java.util.Arrays;
    65import java.util.Collections;
     
    1817
    1918import net.sf.basedb.core.AnnotationType;
     19import net.sf.basedb.core.AnnotationTypeCategory;
    2020import net.sf.basedb.core.Application;
    2121import net.sf.basedb.core.DbControl;
     
    2323import net.sf.basedb.core.Item;
    2424import net.sf.basedb.core.ItemKey;
     25import net.sf.basedb.core.ItemSubtype;
    2526import net.sf.basedb.core.MultiPermissions;
    2627import net.sf.basedb.core.Permission;
     
    3132import net.sf.basedb.core.Type;
    3233import net.sf.basedb.reggie.Reggie;
     34import net.sf.basedb.reggie.dao.Subtype;
    3335import net.sf.basedb.util.EqualsHelper;
    3436import net.sf.basedb.util.error.ThrowableUtil;
     
    9294          sharedToActiveProject.set(Project.getById(dc, projectId), Permission.USE);
    9395        }
     96       
     97        Map<String, Item> subtypeItems = new HashMap<String, Item>();
     98       
     99        // Subtype checks
     100        jsonChecks.add(checkSubtype(dc, Subtype.PATIENT, createIfMissing));
     101        jsonChecks.add(checkSubtype(dc, Subtype.CASE, createIfMissing, Subtype.PATIENT));
     102        jsonChecks.add(checkSubtype(dc, Subtype.SPECIMEN,createIfMissing, Subtype.CASE));
     103        jsonChecks.add(checkSubtype(dc, Subtype.HISTOLOGY, createIfMissing, Subtype.SPECIMEN));
     104        jsonChecks.add(checkSubtype(dc, Subtype.LYSATE, createIfMissing, Subtype.SPECIMEN));
    94105       
    95106        // Annotation type checks
     
    147158            null, effectiveOptions, createIfMissing));
    148159       
     160        // Annotation type categories
     161        jsonChecks.add(checkAnnotationTypeCategory(dc, Subtype.PATIENT, createIfMissing,
     162            Reggie.ANNOTATION_ALL_FIRST_NAMES, Reggie.ANNOTATION_DATE_OF_BIRTH,
     163            Reggie.ANNOTATION_FAMILY_NAME, Reggie.ANNOTATION_GENDER, Reggie.ANNOTATION_PERSONAL_NUMBER));
     164        jsonChecks.add(checkAnnotationTypeCategory(dc, Subtype.CASE, createIfMissing,
     165            Reggie.ANNOTATION_CONSENT, Reggie.ANNOTATION_LATERALITY,
     166            Reggie.ANNOTATION_PAD_CASE, Reggie.ANNOTATION_REASON_IF_NO_SPECIMEN));
     167        jsonChecks.add(checkAnnotationTypeCategory(dc, Subtype.SPECIMEN, createIfMissing,
     168            Reggie.ANNOTATION_ARRIVAL_DATE, Reggie.ANNOTATION_BIOPSY_TYPE,
     169            Reggie.ANNOTATION_LATERALITY, Reggie.ANNOTATION_NOF_DELIVERED_TUBES,
     170            Reggie.ANNOTATION_NOF_PIECES, Reggie.ANNOTATION_OPERATOR_DELIVERY_COMMENT,
     171            Reggie.ANNOTATION_OPERATOR_PARTITION_COMMENT, Reggie.ANNOTATION_OTHER_PATH_NOTE,
     172            Reggie.ANNOTATION_PAD, Reggie.ANNOTATION_RNALATER_DATETIME,
     173            Reggie.ANNOTATION_SAMPLING_DATETIME, Reggie.ANNOTATION_SPECIMEN_TYPE));
     174        jsonChecks.add(checkAnnotationTypeCategory(dc, Subtype.LYSATE, createIfMissing,
     175            Reggie.ANNOTATION_PARTITION_DATE));
     176       
    149177        json.put("checks", jsonChecks);
    150178      }
     179     
    151180    }
    152181    catch (Throwable t)
     
    430459  }
    431460
    432  
     461  /**
     462    Create a subtype with the given options. The subtype is created in
     463    a separate transaction.
     464    @since 2.0
     465  */
     466  public ItemSubtype createSubtype(SessionControl sc, Subtype def, Subtype... relatedTo)
     467  {
     468    ItemSubtype subtype = null;
     469    DbControl dc = sc.newDbControl();
     470    try
     471    {
     472      subtype = ItemSubtype.getNew(dc, def.getMainType());
     473      subtype.setName(def.getName());
     474     
     475      if (relatedTo != null)
     476      {
     477        for (Subtype related : relatedTo)
     478        {
     479          subtype.setRelatedSubtype(related.load(dc));
     480        }
     481      }
     482     
     483      dc.saveItem(subtype);
     484      dc.commit();
     485    }
     486    finally
     487    {
     488      if (dc != null) dc.close();
     489    }
     490    return subtype;
     491  }
     492 
     493  /**
     494    Check for an existing item subtype with the given options.
     495    A JSONObject is returned with the result. The following
     496    keys are used:
     497    <ul>
     498    <li>itemType: ITEMSUBTYPE
     499    <li>name: The name of the subtype
     500    <li>id: The id of the subtype if it exists
     501    <li>mainType: The main item type that the subtype applies to
     502    <li>status: ok, error, or missing
     503    <li>message: A descriptive message in case of an error
     504    </ul>
     505    @since 2.0
     506  */
     507  @SuppressWarnings("unchecked")
     508  public JSONObject checkSubtype(DbControl dc, Subtype subtype,
     509    boolean createIfMissing, Subtype... relatedTo)
     510  {
     511 
     512    JSONObject json = new JSONObject();
     513    JSONArray jsonMessages = new JSONArray();
     514    json.put("itemType", Item.ITEMSUBTYPE.name());
     515    json.put("name", subtype.getName());
     516    json.put("mainType", subtype.getMainType().name());
     517     
     518    List<ItemSubtype> result = Reggie.listSubtypes(dc, subtype);
     519    if (result.size() == 0)
     520    {
     521      if (createIfMissing)
     522      {
     523        ItemSubtype s = createSubtype(dc.getSessionControl(), subtype, relatedTo);
     524        json.put("id", s.getId());
     525        json.put("status", "ok");
     526        jsonMessages.add("Created");
     527      }
     528      else
     529      {
     530        json.put("status", "missing");
     531        jsonMessages.add("Not found");
     532      }
     533    }
     534    else if (result.size() > 1)
     535    {
     536      json.put("status", "error");
     537      jsonMessages.add("Found > 1 subtype");
     538    }
     539    else
     540    {
     541      ItemSubtype s = result.get(0);
     542      json.put("id", s.getId());
     543      json.put("status", "ok"); // For now -- more checks below
     544     
     545      if (relatedTo != null)
     546      {
     547        for (Subtype related : relatedTo)
     548        {
     549          ItemSubtype r = s.getRelatedSubtype(related.getMainType());
     550          if (r == null)
     551          {
     552            json.put("status", "error");
     553            jsonMessages.add("Should be related to '" + related.getName() + "'");
     554          }
     555          else if (!r.getName().equals(related.getName()))
     556          {
     557            json.put("status", "error");
     558            jsonMessages.add("Should be related to: '" +
     559                related.getName() + "' not '" + r.getName() + "'");
     560          }
     561        }
     562      }
     563     
     564    }
     565    if (jsonMessages.size() == 0) jsonMessages.add("Ok");
     566    json.put("messages", jsonMessages);
     567    return json;
     568
     569  }
     570 
     571  /**
     572    Create an annotation type category with the given options. The category
     573    is created in a separate transaction.
     574    @since 2.0
     575  */
     576  public AnnotationTypeCategory createAnnotationTypeCategory(SessionControl sc, Subtype subtype, String... annotationTypes)
     577  {
     578    AnnotationTypeCategory cat = null;
     579    DbControl dc = sc.newDbControl();
     580    try
     581    {
     582      cat = AnnotationTypeCategory.getNew(dc);
     583      cat.setName(subtype.getName());
     584     
     585      if (annotationTypes != null)
     586      {
     587        for (String annotationType : annotationTypes)
     588        {
     589          AnnotationType at = Reggie.findAnnotationType(dc, subtype.getMainType(), annotationType, false);
     590          if (at != null)
     591          {
     592            at.addCategory(cat);
     593          }
     594        }
     595      }
     596     
     597      dc.saveItem(cat);
     598      dc.commit();
     599    }
     600    finally
     601    {
     602      if (dc != null) dc.close();
     603    }
     604    return cat;
     605  }
     606
     607  /**
     608    We need to do this in a separate transaction.
     609    @since 2.0
     610  */
     611  public void addAnnotionTypeToCategory(SessionControl sc, AnnotationType annotationType, AnnotationTypeCategory category)
     612  {
     613    DbControl dc = sc.newDbControl();
     614    try
     615    {
     616      annotationType = AnnotationType.getById(dc, annotationType.getId());
     617      category = AnnotationTypeCategory.getById(dc, category.getId());
     618      annotationType.addCategory(category);
     619      dc.commit();
     620    }
     621    finally
     622    {
     623      if (dc != null) dc.close();
     624    }
     625  }
     626 
     627  /**
     628    Check for an existing annotation type category.
     629    A JSONObject is returned with the result. The following
     630    keys are used:
     631    <ul>
     632    <li>itemType: ANNOTATIONTYPECATEGORY
     633    <li>name: The name of the subtype
     634    <li>id: The id of the subtype if it exists
     635    <li>mainType: The main item type that the subtype applies to
     636    <li>status: ok, error, or missing
     637    <li>message: A descriptive message in case of an error
     638    </ul>
     639    @since 2.0
     640  */
     641  @SuppressWarnings("unchecked")
     642  public JSONObject checkAnnotationTypeCategory(DbControl dc, Subtype subtype,
     643    boolean createIfMissing, String... annotationTypes)
     644  {
     645 
     646    JSONObject json = new JSONObject();
     647    JSONArray jsonMessages = new JSONArray();
     648    json.put("itemType", Item.ANNOTATIONTYPECATEGORY.name());
     649    json.put("name", subtype.getName());
     650     
     651    List<AnnotationTypeCategory> result = Reggie.listAnnotationTypeCategories(dc, subtype.getName());
     652    if (result.size() == 0)
     653    {
     654      if (createIfMissing)
     655      {
     656        AnnotationTypeCategory cat = createAnnotationTypeCategory(dc.getSessionControl(),
     657            subtype, annotationTypes);
     658        json.put("id", cat.getId());
     659        json.put("status", "ok");
     660        jsonMessages.add("Created");
     661      }
     662      else
     663      {
     664        json.put("status", "missing");
     665        jsonMessages.add("Not found");
     666      }
     667    }
     668    else if (result.size() > 1)
     669    {
     670      json.put("status", "error");
     671      jsonMessages.add("Found > 1 annotation type category");
     672    }
     673    else
     674    {
     675      AnnotationTypeCategory cat = result.get(0);
     676      json.put("id", cat.getId());
     677      json.put("status", "ok"); // For now -- more checks below
     678     
     679      if (annotationTypes != null)
     680      {
     681        for (String annotationType : annotationTypes)
     682        {
     683          AnnotationType at = Reggie.findAnnotationType(dc, subtype.getMainType(), annotationType, false);
     684          if (at != null && !at.isMember(cat))
     685          {
     686            if (createIfMissing)
     687            {
     688              addAnnotionTypeToCategory(dc.getSessionControl(), at, cat);
     689              if (jsonMessages.size() == 0) jsonMessages.add("Fixed");
     690            }
     691            else
     692            {
     693              json.put("status", "incomplete");
     694              jsonMessages.add("Not linked with: " + annotationType);
     695            }
     696          }
     697        }
     698      }
     699    }
     700    if (jsonMessages.size() == 0) jsonMessages.add("Ok");
     701    json.put("messages", jsonMessages);
     702    return json;
     703 
     704  }
     705
     706
    433707  /**
    434708    Store options for enumerated annotation types.
  • extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/servlet/PartitionRegistrationServlet.java

    r1457 r1463  
    3030import net.sf.basedb.reggie.converter.StringToDateConverter;
    3131import net.sf.basedb.reggie.dao.SpecimenTube;
     32import net.sf.basedb.reggie.dao.Subtype;
    3233import net.sf.basedb.util.Values;
    3334import net.sf.basedb.util.error.ThrowableUtil;
     
    197198              {
    198199                Extract lysExtract = specimen.newExtract(apWeight);
     200                lysExtract.setItemSubtype(Subtype.LYSATE.load(dc));
    199201                lysExtract.setName(specimen.getName()+".l");
    200202                dc.saveItem(lysExtract);
     
    239241              {
    240242                hisSample = Sample.getNew(dc);
     243                hisSample.setItemSubtype(Subtype.HISTOLOGY.load(dc));
    241244                hisSample.setName(specimen.getName() + ".his");
    242245                dc.saveItem(hisSample);
    243246               
    244                 hisSample.setPooled(true);
    245247                hisSample.setOriginalQuantity(hisWeight);
    246248                BioMaterialEvent creationEvent = hisSample.getCreationEvent();
    247                 creationEvent.addSource(specimen, hisWeight);
     249                creationEvent.addSource(hisSample).setUsedQuantity(hisWeight);
    248250               
    249251                Reggie.setAnnotationValue(hisSample, partitionDateType, partitionDate);
  • extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/servlet/PersonalRegistrationServlet.java

    r1359 r1463  
    3636import net.sf.basedb.reggie.dao.Patient;
    3737import net.sf.basedb.reggie.dao.SpecimenTube;
     38import net.sf.basedb.reggie.dao.Subtype;
    3839import net.sf.basedb.util.MD5;
    3940import net.sf.basedb.util.Values;
     
    276277          // Create new patient
    277278          patient = BioSource.getNew(dc);
     279          patient.setItemSubtype(Subtype.PATIENT.load(dc));
    278280          patient.setName((String)jsonPat.get("name"));
    279281          String pnr = (String)jsonPat.get("personalNumber");
     
    329331          // Register a new case
    330332          theCase = Sample.getNew(dc);
     333          theCase.setItemSubtype(Subtype.CASE.load(dc));
    331334          theCase.setName(originalCaseName);
    332335          if (laterality != null)
     
    334337            Reggie.setAnnotationValue(theCase, lateralityType, laterality);
    335338          }
    336           theCase.setBioSource(patient);
     339          theCase.getCreationEvent().setSource(patient);
    337340          dc.saveItem(theCase);
    338341          jsonMessages.add("Case '" + originalCaseName + "' created successfully.");
     
    359362            JSONObject jsonSpec = (JSONObject)jsonSpecimen.get(i);
    360363            Sample specimen = Sample.getById(dc, ((Number)jsonSpec.get("id")).intValue());
    361             // Set specimen to 'pooled' and link with case
    362             specimen.setPooled(true);
    363             specimen.getCreationEvent().addSource(theCase, null);
     364            // Link the specimen with the case
     365            specimen.getCreationEvent().setSource(theCase);
    364366           
    365367            // Update the specimen annotations
  • extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/servlet/SpecimenTubeServlet.java

    r1457 r1463  
    2626import net.sf.basedb.core.query.Restrictions;
    2727import net.sf.basedb.reggie.Reggie;
    28 import net.sf.basedb.reggie.converter.DateToStringConverter;
    2928import net.sf.basedb.reggie.converter.StringToDateConverter;
    3029import net.sf.basedb.reggie.dao.Case;
    3130import net.sf.basedb.reggie.dao.SpecimenTube;
     31import net.sf.basedb.reggie.dao.Subtype;
    3232import net.sf.basedb.util.Values;
    3333import net.sf.basedb.util.error.ThrowableUtil;
     
    266266            JSONObject jsonSpec = (JSONObject)jsonSpecimen.get(i);
    267267            Sample specimen = Sample.getNew(dc);
     268            specimen.setItemSubtype(Subtype.SPECIMEN.load(dc));
    268269            specimen.setName((String)jsonSpec.get("name"));
    269270            dc.saveItem(specimen);
Note: See TracChangeset for help on using the changeset viewer.