Changeset 1427


Ignore:
Timestamp:
Oct 28, 2011, 2:59:21 PM (12 years ago)
Author:
Nicklas Nordborg
Message:

References #335: Use item subtypes - step 1

The installation wizard now check and create the following subtypes:

  • Patient
  • Case
  • Specimen
  • Histology
  • Lysate
Location:
extensions/net.sf.basedb.reggie/branches/2.0-dev
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • extensions/net.sf.basedb.reggie/branches/2.0-dev/resources/install.jsp

    r1342 r1427  
    8585    }
    8686    lastItemType = check.itemType;
     87    var name = check.name;
     88    if (check.mainType) name += ' <span class="itemsubtype">[' + check.mainType + ']</span>';
    8789    if (check.id)
    8890    {
    8991      html += '<td><div class="link" onclick="itemOnClick(event, \''+check.itemType+'\','+check.id+')"';
    90       html += ' title="View this item (use CTRL, ALT or SHIFT to edit)">'+check.name+'</div></td>';
     92      html += ' title="View this item (use CTRL, ALT or SHIFT to edit)">'+name+'</div></td>';
    9193    }
    9294    else
    9395    {
    94       html += '<td><i>' + check.name + '</i></td>';
     96      html += '<td><i>' + name + '</i></td>';
    9597    }
    9698    html += '<td><img src="../../images/'+icon+'"></td><td>';
  • extensions/net.sf.basedb.reggie/branches/2.0-dev/src/net/sf/basedb/reggie/Reggie.java

    r1424 r1427  
    1515import net.sf.basedb.core.ItemNotFoundException;
    1616import net.sf.basedb.core.ItemQuery;
     17import net.sf.basedb.core.ItemSubtype;
    1718import net.sf.basedb.core.Type;
    1819import net.sf.basedb.core.query.Expressions;
    1920import net.sf.basedb.core.query.Hql;
    2021import net.sf.basedb.core.query.Restrictions;
     22import net.sf.basedb.reggie.dao.Subtype;
    2123
    2224/**
     
    177179   */
    178180  public static final String ANNOTATION_PARTITION_DATE = "PartitionDate";
     181 
     182 
     183  /**
     184    List all subtypes for the given item type that has the given name.
     185    @since 2.0
     186  */
     187  public static List<ItemSubtype> listSubtypes(DbControl dc, Subtype subtype)
     188  {
     189    ItemQuery<ItemSubtype> query = ItemSubtype.getQuery(subtype.getMainType());
     190    query.restrict(
     191      Restrictions.eq(
     192        Hql.property("name"),
     193        Expressions.parameter("name", subtype.getName(), Type.STRING)
     194      ));
     195    query.include(Include.ALL);
     196    return query.list(dc);
     197  }
     198 
     199  /**
     200    Find a single item subtype with the given name. If exactly one is found it
     201    is returned, otherwise null is returned or an exception is thrown
     202    depending on the exceptionIfNotFound parameter.
     203  */
     204  public static ItemSubtype findSubtype(DbControl dc, Subtype subtype, boolean exceptionIfNotFound)
     205  {
     206    List<ItemSubtype> result = listSubtypes(dc, subtype);
     207    ItemSubtype s = null;
     208    if (result.size() == 0)
     209    {
     210      if (exceptionIfNotFound) throw new ItemNotFoundException("ItemSubtype["+subtype.getName()+"]");
     211    }
     212    else if (result.size() > 1)
     213    {
     214      if (exceptionIfNotFound) throw new InvalidDataException("Found > 1 ItemSubtype["+subtype.getName()+"]");
     215    }
     216    else
     217    {
     218      s = result.get(0);
     219    }
     220    return s;
     221  }
    179222 
    180223  /**
  • extensions/net.sf.basedb.reggie/branches/2.0-dev/src/net/sf/basedb/reggie/servlet/InstallServlet.java

    r1409 r1427  
    22
    33import java.io.IOException;
    4 import java.util.ArrayList;
    54import java.util.Arrays;
    65import java.util.Collections;
     
    2322import net.sf.basedb.core.Item;
    2423import net.sf.basedb.core.ItemKey;
     24import net.sf.basedb.core.ItemSubtype;
    2525import net.sf.basedb.core.MultiPermissions;
    2626import net.sf.basedb.core.Permission;
     
    3131import net.sf.basedb.core.Type;
    3232import net.sf.basedb.reggie.Reggie;
     33import net.sf.basedb.reggie.dao.Subtype;
    3334import net.sf.basedb.util.EqualsHelper;
    3435import net.sf.basedb.util.error.ThrowableUtil;
     
    9293          sharedToActiveProject.set(Project.getById(dc, projectId), Permission.USE);
    9394        }
     95       
     96        Map<String, Item> subtypeItems = new HashMap<String, Item>();
     97       
     98        // Subtype checks
     99        jsonChecks.add(checkSubtype(dc, Subtype.PATIENT, createIfMissing));
     100        jsonChecks.add(checkSubtype(dc, Subtype.CASE, createIfMissing, Subtype.PATIENT));
     101        jsonChecks.add(checkSubtype(dc, Subtype.SPECIMEN,createIfMissing, Subtype.CASE));
     102        jsonChecks.add(checkSubtype(dc, Subtype.HISTOLOGY, createIfMissing, Subtype.SPECIMEN));
     103        jsonChecks.add(checkSubtype(dc, Subtype.LYSATE, createIfMissing, Subtype.SPECIMEN));
    94104       
    95105        // Annotation type checks
     
    146156        jsonChecks.add(checkAnnotationType(dc, Reggie.ANNOTATION_PARTITION_DATE, new Item[]{Item.SAMPLE, Item.EXTRACT}, Type.DATE, 1,
    147157            null, effectiveOptions, createIfMissing));
     158       
    148159       
    149160        json.put("checks", jsonChecks);
     
    430441  }
    431442
    432  
     443  /**
     444    Create a subtype with the given options. The subtype is created in
     445    a separate transaction.
     446    @since 2.0
     447  */
     448  public ItemSubtype createSubtype(SessionControl sc, Subtype def, Subtype... relatedTo)
     449  {
     450    ItemSubtype subtype = null;
     451    DbControl dc = sc.newDbControl();
     452    try
     453    {
     454      subtype = ItemSubtype.getNew(dc, def.getMainType());
     455      subtype.setName(def.getName());
     456     
     457      if (relatedTo != null)
     458      {
     459        for (Subtype related : relatedTo)
     460        {
     461          subtype.setRelatedSubtype(related.load(dc));
     462        }
     463      }
     464     
     465      dc.saveItem(subtype);
     466      dc.commit();
     467    }
     468    finally
     469    {
     470      if (dc != null) dc.close();
     471    }
     472    return subtype;
     473  }
     474 
     475  /**
     476    Check for an existing item subtype with the given options.
     477    A JSONObject is returned with the result. The following
     478    keys are used:
     479    <ul>
     480    <li>itemType: ITEMSUBTYPE
     481    <li>name: The name of the subtype
     482    <li>id: The id of the subtype if it exists
     483    <li>mainType: The main item type that the subtype applies to
     484    <li>status: ok, error, or missing
     485    <li>message: A descriptive message in case of an error
     486    </ul>
     487    @since 2.0
     488  */
     489  @SuppressWarnings("unchecked")
     490  public JSONObject checkSubtype(DbControl dc, Subtype subtype,
     491    boolean createIfMissing, Subtype... relatedTo)
     492  {
     493 
     494    JSONObject json = new JSONObject();
     495    JSONArray jsonMessages = new JSONArray();
     496    json.put("itemType", Item.ITEMSUBTYPE.name());
     497    json.put("name", subtype.getName());
     498    json.put("mainType", subtype.getMainType().name());
     499     
     500    List<ItemSubtype> result = Reggie.listSubtypes(dc, subtype);
     501    if (result.size() == 0)
     502    {
     503      if (createIfMissing)
     504      {
     505        ItemSubtype s = createSubtype(dc.getSessionControl(), subtype, relatedTo);
     506        json.put("id", s.getId());
     507        json.put("status", "ok");
     508        jsonMessages.add("Created");
     509      }
     510      else
     511      {
     512        json.put("status", "missing");
     513        jsonMessages.add("Not found");
     514      }
     515    }
     516    else if (result.size() > 1)
     517    {
     518      json.put("status", "error");
     519      jsonMessages.add("Found > 1 subtype");
     520    }
     521    else
     522    {
     523      ItemSubtype s = result.get(0);
     524      json.put("id", s.getId());
     525      json.put("status", "ok"); // For now -- more checks below
     526     
     527      if (relatedTo != null)
     528      {
     529        for (Subtype related : relatedTo)
     530        {
     531          ItemSubtype r = s.getRelatedSubtype(related.getMainType());
     532          if (r == null)
     533          {
     534            json.put("status", "error");
     535            jsonMessages.add("Should be related to '" + related.getName() + "'");
     536          }
     537          else if (!r.getName().equals(related.getName()))
     538          {
     539            json.put("status", "error");
     540            jsonMessages.add("Should be related to: '" +
     541                related.getName() + "' not '" + r.getName() + "'");
     542          }
     543        }
     544      }
     545     
     546    }
     547    if (jsonMessages.size() == 0) jsonMessages.add("Ok");
     548    json.put("messages", jsonMessages);
     549    return json;
     550
     551  }
     552 
     553
    433554  /**
    434555    Store options for enumerated annotation types.
Note: See TracChangeset for help on using the changeset viewer.