extensions/net.sf.basedb.meludi/trunk/src/net/sf/basedb/meludi/dao/StartList.java

Code
Comments
Other
Rev Date Author Line
3431 29 Jun 15 olle 1 package net.sf.basedb.meludi.dao;
3431 29 Jun 15 olle 2
3431 29 Jun 15 olle 3 import java.util.ArrayList;
3431 29 Jun 15 olle 4 import java.util.List;
3431 29 Jun 15 olle 5
3431 29 Jun 15 olle 6 import net.sf.basedb.core.DbControl;
3433 29 Jun 15 olle 7 import net.sf.basedb.core.Extract;
3433 29 Jun 15 olle 8 import net.sf.basedb.core.Include;
3431 29 Jun 15 olle 9 import net.sf.basedb.core.ItemList;
3431 29 Jun 15 olle 10 import net.sf.basedb.core.ItemQuery;
3431 29 Jun 15 olle 11 import net.sf.basedb.core.Type;
3431 29 Jun 15 olle 12 import net.sf.basedb.core.query.Expressions;
3431 29 Jun 15 olle 13 import net.sf.basedb.core.query.Hql;
3431 29 Jun 15 olle 14 import net.sf.basedb.core.query.Orders;
3431 29 Jun 15 olle 15 import net.sf.basedb.core.query.Restrictions;
3431 29 Jun 15 olle 16 import net.sf.basedb.core.snapshot.SnapshotManager;
3431 29 Jun 15 olle 17 import net.sf.basedb.meludi.Meludi;
3431 29 Jun 15 olle 18
3431 29 Jun 15 olle 19 /**
3431 29 Jun 15 olle 20   Used to define start item lists.
3431 29 Jun 15 olle 21   
3431 29 Jun 15 olle 22   @author olle
3431 29 Jun 15 olle 23   @since 1.3
3431 29 Jun 15 olle 24 */
3431 29 Jun 15 olle 25 public class StartList 
3431 29 Jun 15 olle 26   extends MeludiItem<ItemList>
3431 29 Jun 15 olle 27 {
3501 21 Sep 15 olle 28   public final static String ITEM_SPECIMEN = "Specimen";
3501 21 Sep 15 olle 29   public final static String ITEM_DNA = "DNA";
3501 21 Sep 15 olle 30   public final static String ITEM_RNA = "RNA";
3440 17 Jul 15 olle 31
3440 17 Jul 15 olle 32   /*
3431 29 Jun 15 olle 33   private final String name;
3431 29 Jun 15 olle 34   private final String externalId;
3431 29 Jun 15 olle 35   private final String description;
3431 29 Jun 15 olle 36   private int id;
3431 29 Jun 15 olle 37 */
3431 29 Jun 15 olle 38   
3431 29 Jun 15 olle 39   /**
3431 29 Jun 15 olle 40     Create a new definition
3431 29 Jun 15 olle 41     @param name The name of the list
3431 29 Jun 15 olle 42     @param externalId The external id for the item list
3431 29 Jun 15 olle 43     @param description The description for the item list
3431 29 Jun 15 olle 44   */
3431 29 Jun 15 olle 45 /*
3431 29 Jun 15 olle 46   private StartList(String name, String externalId, String description)
3431 29 Jun 15 olle 47   {
3431 29 Jun 15 olle 48     this.name = name;
3431 29 Jun 15 olle 49     this.externalId = externalId;
3431 29 Jun 15 olle 50     this.description = description;
3431 29 Jun 15 olle 51   }
3431 29 Jun 15 olle 52 */
3431 29 Jun 15 olle 53     
3431 29 Jun 15 olle 54   /**
3431 29 Jun 15 olle 55     Get the name of the item list.
3431 29 Jun 15 olle 56   */
3431 29 Jun 15 olle 57 /*
3431 29 Jun 15 olle 58   public String getName()
3431 29 Jun 15 olle 59   {
3431 29 Jun 15 olle 60     return name;
3431 29 Jun 15 olle 61   }
3431 29 Jun 15 olle 62 */
3431 29 Jun 15 olle 63
3431 29 Jun 15 olle 64   /**
3431 29 Jun 15 olle 65     Get the list description.
3431 29 Jun 15 olle 66   */
3431 29 Jun 15 olle 67 /*
3431 29 Jun 15 olle 68   public String getDescription()
3431 29 Jun 15 olle 69   {
3431 29 Jun 15 olle 70     return description;
3431 29 Jun 15 olle 71   }
3431 29 Jun 15 olle 72 */
3431 29 Jun 15 olle 73
3431 29 Jun 15 olle 74   /**
3431 29 Jun 15 olle 75     Get the external id of the external list.
3431 29 Jun 15 olle 76   */
3431 29 Jun 15 olle 77 /*
3431 29 Jun 15 olle 78   public String getExternalId()
3431 29 Jun 15 olle 79   {
3431 29 Jun 15 olle 80     return externalId;
3431 29 Jun 15 olle 81   }
3431 29 Jun 15 olle 82 */
3431 29 Jun 15 olle 83
3431 29 Jun 15 olle 84   /**
3431 29 Jun 15 olle 85     Load the subtype as a BASE item.
3431 29 Jun 15 olle 86     @param dc The DbControl to use for database access
3431 29 Jun 15 olle 87     @return An ItemSubtype object
3431 29 Jun 15 olle 88   */
3431 29 Jun 15 olle 89 /*
3431 29 Jun 15 olle 90   public ItemList load(DbControl dc)
3431 29 Jun 15 olle 91   {
3431 29 Jun 15 olle 92     ItemList list = null;
3431 29 Jun 15 olle 93     if (id == 0)
3431 29 Jun 15 olle 94     {
3431 29 Jun 15 olle 95       List<ItemList> result = list(dc);
3431 29 Jun 15 olle 96       if (result.size() == 0)
3431 29 Jun 15 olle 97       {
3431 29 Jun 15 olle 98         throw new ItemNotFoundException("ItemList["+getName()+"]");
3431 29 Jun 15 olle 99       }
3431 29 Jun 15 olle 100       else if (result.size() > 1)
3431 29 Jun 15 olle 101       {
3431 29 Jun 15 olle 102         throw new InvalidDataException("Found > 1 ItemList["+getName()+"]");
3431 29 Jun 15 olle 103       }
3431 29 Jun 15 olle 104       list = result.get(0);
3431 29 Jun 15 olle 105       id = list.getId();
3431 29 Jun 15 olle 106     }
3431 29 Jun 15 olle 107     else
3431 29 Jun 15 olle 108     {
3431 29 Jun 15 olle 109       list = ItemList.getById(dc, id);
3431 29 Jun 15 olle 110     }
3431 29 Jun 15 olle 111     return list;
3431 29 Jun 15 olle 112   }
3431 29 Jun 15 olle 113 */
3431 29 Jun 15 olle 114
3431 29 Jun 15 olle 115   /**
3431 29 Jun 15 olle 116     List all item lists registered in BASE with an external id matching this list
3431 29 Jun 15 olle 117     definition. Normally, only a single item list type should be returned.
3431 29 Jun 15 olle 118   */
3431 29 Jun 15 olle 119 /*
3431 29 Jun 15 olle 120   public List<ItemList> list(DbControl dc)
3431 29 Jun 15 olle 121   {
3431 29 Jun 15 olle 122     ItemQuery<ItemList> query = ItemList.getQuery();
3431 29 Jun 15 olle 123     query.restrict(
3431 29 Jun 15 olle 124       Restrictions.eq(
3431 29 Jun 15 olle 125         Hql.property("externalId"), 
3431 29 Jun 15 olle 126         Expressions.parameter("externalId", getExternalId(), Type.STRING)
3431 29 Jun 15 olle 127     ));
3431 29 Jun 15 olle 128     query.include(Include.ALL);
3431 29 Jun 15 olle 129     return query.list(dc);
3431 29 Jun 15 olle 130   }
3431 29 Jun 15 olle 131 */
3431 29 Jun 15 olle 132
3431 29 Jun 15 olle 133   public StartList(ItemList item) {
3431 29 Jun 15 olle 134     super(item);
3431 29 Jun 15 olle 135     // TODO Auto-generated constructor stub
3431 29 Jun 15 olle 136   }
3431 29 Jun 15 olle 137
3431 29 Jun 15 olle 138   /**
3431 29 Jun 15 olle 139     Find all item lists which have names starting with an optional prefix.
3431 29 Jun 15 olle 140
3431 29 Jun 15 olle 141     @param dc The DbControl to use.
3431 29 Jun 15 olle 142     @param prefix String Optional prefix in list name
3431 29 Jun 15 olle 143     @param orderDesc Boolean Flag indicating if list should be sorted in descending order (`false` or `null` gives ascending order).
3431 29 Jun 15 olle 144     @return List<ItemList> The found start item lists.
3431 29 Jun 15 olle 145   */
3431 29 Jun 15 olle 146   public static List<ItemList> findByPrefix(DbControl dc, 
3431 29 Jun 15 olle 147     String prefix, Boolean orderDesc)
3431 29 Jun 15 olle 148   {
3431 29 Jun 15 olle 149     ItemQuery<ItemList> query = ItemList.getQuery();
3431 29 Jun 15 olle 150     query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3431 29 Jun 15 olle 151     if (prefix != null && !prefix.equals(""))
3431 29 Jun 15 olle 152     {
3431 29 Jun 15 olle 153       query.restrict(Restrictions.like(Hql.property("name"), Expressions.parameter("name", prefix + "%", Type.STRING)));
3431 29 Jun 15 olle 154     }
3431 29 Jun 15 olle 155     if (orderDesc != null && orderDesc)
3431 29 Jun 15 olle 156     {
3431 29 Jun 15 olle 157       query.order(Orders.desc(Hql.property("name")));
3431 29 Jun 15 olle 158     }
3431 29 Jun 15 olle 159     else
3431 29 Jun 15 olle 160     {
3431 29 Jun 15 olle 161       query.order(Orders.asc(Hql.property("name")));
3431 29 Jun 15 olle 162     }
3431 29 Jun 15 olle 163
3431 29 Jun 15 olle 164     List<ItemList> startItemLists = query.list(dc);
3431 29 Jun 15 olle 165     return startItemLists;
3431 29 Jun 15 olle 166   }
3431 29 Jun 15 olle 167
3431 29 Jun 15 olle 168   /**
3431 29 Jun 15 olle 169     Find all active item lists which have names starting with an optional prefix.
3431 29 Jun 15 olle 170     Active item lists are lists where Boolean annotation
3431 29 Jun 15 olle 171     SAMPLE_PREP_LIST_IS_PROCESSED has value `false` or `null`.
3431 29 Jun 15 olle 172
3431 29 Jun 15 olle 173     @param dc The DbControl to use.
3431 29 Jun 15 olle 174     @param prefix String Optional prefix in list name
3431 29 Jun 15 olle 175     @param orderDesc Boolean Flag indicating if list should be sorted in descending order (`false` or `null` gives ascending order).
3431 29 Jun 15 olle 176     @return List<ItemList> The found start item lists.
3431 29 Jun 15 olle 177   */
3431 29 Jun 15 olle 178   public static List<ItemList> findActiveByPrefix(DbControl dc, 
3431 29 Jun 15 olle 179     SnapshotManager manager, String prefix, Boolean orderDesc)
3431 29 Jun 15 olle 180   {
3431 29 Jun 15 olle 181     ItemQuery<ItemList> query = ItemList.getQuery();
3431 29 Jun 15 olle 182     query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3431 29 Jun 15 olle 183     if (prefix != null && !prefix.equals(""))
3431 29 Jun 15 olle 184     {
3431 29 Jun 15 olle 185       query.restrict(Restrictions.like(Hql.property("name"), Expressions.parameter("name", prefix + "%", Type.STRING)));
3431 29 Jun 15 olle 186     }
3431 29 Jun 15 olle 187     if (orderDesc != null && orderDesc)
3431 29 Jun 15 olle 188     {
3431 29 Jun 15 olle 189       query.order(Orders.desc(Hql.property("name")));
3431 29 Jun 15 olle 190     }
3431 29 Jun 15 olle 191     else
3431 29 Jun 15 olle 192     {
3431 29 Jun 15 olle 193       query.order(Orders.asc(Hql.property("name")));
3431 29 Jun 15 olle 194     }
3431 29 Jun 15 olle 195
3431 29 Jun 15 olle 196     List<ItemList> activeStartItemLists = new ArrayList<ItemList>();
3431 29 Jun 15 olle 197     List<ItemList> startItemLists = query.list(dc);
3431 29 Jun 15 olle 198
3431 29 Jun 15 olle 199     for (ItemList l : startItemLists)
3431 29 Jun 15 olle 200     {
3431 29 Jun 15 olle 201       Boolean listIsActive = (Boolean) Annotationtype.SAMPLE_PREP_LIST_IS_PROCESSED.getAnnotationValue(dc, manager, l);
3431 29 Jun 15 olle 202       if (listIsActive == null || listIsActive == true)
3431 29 Jun 15 olle 203       {
3431 29 Jun 15 olle 204         activeStartItemLists.add(l);
3431 29 Jun 15 olle 205       }
3431 29 Jun 15 olle 206     }
3431 29 Jun 15 olle 207     return activeStartItemLists;
3431 29 Jun 15 olle 208   }
3431 29 Jun 15 olle 209
3433 29 Jun 15 olle 210   /**
3433 29 Jun 15 olle 211    * Fetches a list of extract source item names from this start item list
3433 29 Jun 15 olle 212    * with Extract type members. A DNA/RNA input extract is stored as
3433 29 Jun 15 olle 213    * member, while For a specimen, the DNA child extract is stored.
3433 29 Jun 15 olle 214    * MeLuDI project items are listed first, then extra material items,
3433 29 Jun 15 olle 215    * each alphabetically sorted in each group.
3433 29 Jun 15 olle 216    * 
3433 29 Jun 15 olle 217    * @param dc DbControl The DbControl to use when connecting to the database.
3433 29 Jun 15 olle 218    * @return List<String> A list of extract source item names related to the extract member items in the item list.
3433 29 Jun 15 olle 219    */
3433 29 Jun 15 olle 220   public List<String> fetchExtractSourceNameListFromItemList(DbControl dc)
3433 29 Jun 15 olle 221   {
3433 29 Jun 15 olle 222     boolean meludiItemsFirst = true;
3433 29 Jun 15 olle 223     List<String> itemNameList = fetchExtractSourceNameListFromItemList(dc, meludiItemsFirst);
3433 29 Jun 15 olle 224     return itemNameList;
3433 29 Jun 15 olle 225   }
3433 29 Jun 15 olle 226
3433 29 Jun 15 olle 227   /**
3433 29 Jun 15 olle 228    * Fetches a list of extract source item names from this start item list
3433 29 Jun 15 olle 229    * with Extract type members. A DNA/RNA input extract is stored as
3433 29 Jun 15 olle 230    * member, while For a specimen, the DNA child extract is stored.
3433 29 Jun 15 olle 231    * The returned list is either alphabetically sorted, with mixed
3433 29 Jun 15 olle 232    * MeLuDI and extra material items, or first lists MeLuDI items,
3433 29 Jun 15 olle 233    * then extra material items, each alphabetically sorted in each group.
3433 29 Jun 15 olle 234    * 
3433 29 Jun 15 olle 235    * @param dc DbControl The DbControl to use when connecting to the database.
3433 29 Jun 15 olle 236    * @param meludiItemsFirst boolean Boolean flag indicating if MeLuDI items should be listed before extra material items.
3433 29 Jun 15 olle 237    * @return List<String> A list of extract source item names related to the extract member items in the item list.
3433 29 Jun 15 olle 238    */
3433 29 Jun 15 olle 239   @SuppressWarnings("unchecked")
3433 29 Jun 15 olle 240   public List<String> fetchExtractSourceNameListFromItemList(DbControl dc, boolean meludiItemsFirst)
3433 29 Jun 15 olle 241   {
3433 29 Jun 15 olle 242     ItemList startList = this.getItem();
4149 03 Oct 16 olle 243     int activeProjectId = dc.getSessionControl().getActiveProjectId();
3433 29 Jun 15 olle 244     // Construct a query to get all member extract items in the item list, sorted alphabetically after name
3433 29 Jun 15 olle 245     ItemQuery<Extract> query = (ItemQuery<Extract>)startList.getMembers();
3433 29 Jun 15 olle 246     query.include(Include.ALL);
3433 29 Jun 15 olle 247     query.order(Orders.asc(Hql.property("name")));
3433 29 Jun 15 olle 248     List<Extract> extractList = query.list(dc);
3433 29 Jun 15 olle 249     // Get an alphabetically sorted list of extract source item names related to the extract member items
3433 29 Jun 15 olle 250     List<String> tmpNameList = new ArrayList<String>();
3433 29 Jun 15 olle 251     if (extractList != null)
3433 29 Jun 15 olle 252     {
3433 29 Jun 15 olle 253       for (Extract e: extractList)
3433 29 Jun 15 olle 254       {
3433 29 Jun 15 olle 255         if (e != null)
3433 29 Jun 15 olle 256         {
3433 29 Jun 15 olle 257           String extractName = e.getName();
3433 29 Jun 15 olle 258           if (Meludi.itemIsExtractSourceItem(extractName))
3433 29 Jun 15 olle 259           {
3433 29 Jun 15 olle 260             tmpNameList.add(extractName);
3433 29 Jun 15 olle 261           }
3433 29 Jun 15 olle 262           else
3433 29 Jun 15 olle 263           {
3433 29 Jun 15 olle 264             // Remove last suffix starting with dot "."
3433 29 Jun 15 olle 265             int lastDotIndex = extractName.lastIndexOf(".");
3433 29 Jun 15 olle 266             if (lastDotIndex >= 0)
3433 29 Jun 15 olle 267             {
3433 29 Jun 15 olle 268               String specimenName = extractName.substring(0, lastDotIndex);
3433 29 Jun 15 olle 269               if (specimenName != null && !specimenName.equals(""))
3433 29 Jun 15 olle 270               {
3433 29 Jun 15 olle 271                 tmpNameList.add(specimenName);
3433 29 Jun 15 olle 272               }
3433 29 Jun 15 olle 273             }
3433 29 Jun 15 olle 274           }
3433 29 Jun 15 olle 275         }  
3433 29 Jun 15 olle 276       }
3433 29 Jun 15 olle 277     }
3433 29 Jun 15 olle 278     // Get final list of extract source item names related to the extract member items
3433 29 Jun 15 olle 279     List<String> itemNameList = new ArrayList<String>();
3433 29 Jun 15 olle 280     if (meludiItemsFirst)
3433 29 Jun 15 olle 281     {
3433 29 Jun 15 olle 282       // Get separate lists of MeLuDI and extra material extract source item names
3433 29 Jun 15 olle 283       List<String> meludiItemNameList = new ArrayList<String>();
3433 29 Jun 15 olle 284       List<String> extraItemNameList = new ArrayList<String>();
3433 29 Jun 15 olle 285       for (String itemName: tmpNameList)
3433 29 Jun 15 olle 286       {
4149 03 Oct 16 olle 287         if (Meludi.itemIsMeludiItem(itemName, activeProjectId))
3433 29 Jun 15 olle 288         {
3433 29 Jun 15 olle 289           meludiItemNameList.add(itemName);
3433 29 Jun 15 olle 290         }
3433 29 Jun 15 olle 291         else
3433 29 Jun 15 olle 292         {
3433 29 Jun 15 olle 293           extraItemNameList.add(itemName);
3433 29 Jun 15 olle 294         }
3433 29 Jun 15 olle 295       }
3433 29 Jun 15 olle 296       // Get a list of extract source item names, MeLuDI items first, then extra material items, each alphabetically sorted in each group
3433 29 Jun 15 olle 297       for (String itemName: meludiItemNameList)
3433 29 Jun 15 olle 298       {
3433 29 Jun 15 olle 299         itemNameList.add(itemName);
3433 29 Jun 15 olle 300       }
3433 29 Jun 15 olle 301       for (String itemName: extraItemNameList)
3433 29 Jun 15 olle 302       {
3433 29 Jun 15 olle 303         itemNameList.add(itemName);
3433 29 Jun 15 olle 304       }
3433 29 Jun 15 olle 305     }
3433 29 Jun 15 olle 306     else
3433 29 Jun 15 olle 307     {
3433 29 Jun 15 olle 308       itemNameList = tmpNameList;
3433 29 Jun 15 olle 309     }
3433 29 Jun 15 olle 310     return itemNameList;
3433 29 Jun 15 olle 311   }
3440 17 Jul 15 olle 312
3440 17 Jul 15 olle 313   /**
3440 17 Jul 15 olle 314    * Fetches a list of item names for one item type from this start item list
3440 17 Jul 15 olle 315    * with Extract type members. A DNA/RNA input extract is stored as
3440 17 Jul 15 olle 316    * member, while For a specimen, the DNA child extract is stored.
3440 17 Jul 15 olle 317    * MeLuDI project items are listed first, then extra material items,
3440 17 Jul 15 olle 318    * each alphabetically sorted in each group.
3440 17 Jul 15 olle 319    * 
3440 17 Jul 15 olle 320    * @param dc DbControl The DbControl to use when connecting to the database.
3440 17 Jul 15 olle 321    * @param itemType String A string defining the type of item to return (Specimen/DNA/RNA).
3440 17 Jul 15 olle 322    * @return List<String> A list of extract source item names related to the extract member items in the item list.
3440 17 Jul 15 olle 323    */
3440 17 Jul 15 olle 324   public List<String> fetchItemNameListFromItemList(DbControl dc, String itemType)
3440 17 Jul 15 olle 325   {
3440 17 Jul 15 olle 326     boolean meludiItemsFirst = true;
3440 17 Jul 15 olle 327     List<String> itemNameList = fetchItemNameListFromItemList(dc, itemType, meludiItemsFirst);
3440 17 Jul 15 olle 328     return itemNameList;
3440 17 Jul 15 olle 329   }
3440 17 Jul 15 olle 330
3440 17 Jul 15 olle 331   /**
3440 17 Jul 15 olle 332    * Fetches a list of item names for one item type from this start item list
3440 17 Jul 15 olle 333    * with Extract type members. A DNA/RNA input extract is stored as
3440 17 Jul 15 olle 334    * member, while For a specimen, the DNA child extract is stored.
3440 17 Jul 15 olle 335    * The returned list is either alphabetically sorted, with mixed
3440 17 Jul 15 olle 336    * MeLuDI and extra material items, or first lists MeLuDI items,
3440 17 Jul 15 olle 337    * then extra material items, each alphabetically sorted in each group.
3440 17 Jul 15 olle 338    * 
3440 17 Jul 15 olle 339    * @param dc DbControl The DbControl to use when connecting to the database.
3440 17 Jul 15 olle 340    * @param itemType String A string defining the type of item to return (Specimen/DNA/RNA).
3440 17 Jul 15 olle 341    * @param meludiItemsFirst boolean Boolean flag indicating if MeLuDI items should be listed before extra material items.
3440 17 Jul 15 olle 342    * @return List<String> A list of extract source item names related to the extract member items in the item list.
3440 17 Jul 15 olle 343    */
3440 17 Jul 15 olle 344   @SuppressWarnings("unchecked")
3440 17 Jul 15 olle 345   public List<String> fetchItemNameListFromItemList(DbControl dc, String itemType, boolean meludiItemsFirst)
3440 17 Jul 15 olle 346   {
3440 17 Jul 15 olle 347     ItemList startList = this.getItem();
3440 17 Jul 15 olle 348     if (itemType == null)
3440 17 Jul 15 olle 349     {
3440 17 Jul 15 olle 350       itemType = ITEM_DNA;
3440 17 Jul 15 olle 351     }
4149 03 Oct 16 olle 352     int activeProjectId = dc.getSessionControl().getActiveProjectId();
3440 17 Jul 15 olle 353     // Construct a query to get all member extract items in the item list, sorted alphabetically after name
3440 17 Jul 15 olle 354     ItemQuery<Extract> query = (ItemQuery<Extract>)startList.getMembers();
3440 17 Jul 15 olle 355     query.include(Include.ALL);
3440 17 Jul 15 olle 356     query.order(Orders.asc(Hql.property("name")));
3440 17 Jul 15 olle 357     List<Extract> extractList = query.list(dc);
3440 17 Jul 15 olle 358     // Get an alphabetically sorted list of extract source item names related to the extract member items
3440 17 Jul 15 olle 359     List<String> tmpNameList = new ArrayList<String>();
3440 17 Jul 15 olle 360     if (extractList != null)
3440 17 Jul 15 olle 361     {
3440 17 Jul 15 olle 362       for (Extract e: extractList)
3440 17 Jul 15 olle 363       {
3440 17 Jul 15 olle 364         if (e != null)
3440 17 Jul 15 olle 365         {
3440 17 Jul 15 olle 366           String extractName = e.getName();
3440 17 Jul 15 olle 367           if (Meludi.itemIsExtractSourceItem(extractName))
3440 17 Jul 15 olle 368           {
3440 17 Jul 15 olle 369             if (itemType.equals(ITEM_DNA) && Meludi.itemIsDna(extractName))
3440 17 Jul 15 olle 370             {
3440 17 Jul 15 olle 371               tmpNameList.add(extractName);
3440 17 Jul 15 olle 372             }
3440 17 Jul 15 olle 373             else if (itemType.equals(ITEM_RNA) && Meludi.itemIsRna(extractName))
3440 17 Jul 15 olle 374             {
3440 17 Jul 15 olle 375               tmpNameList.add(extractName);
3440 17 Jul 15 olle 376             }
3440 17 Jul 15 olle 377           }
3440 17 Jul 15 olle 378           else
3440 17 Jul 15 olle 379           {
3440 17 Jul 15 olle 380             // Remove last suffix starting with dot "."
3440 17 Jul 15 olle 381             int lastDotIndex = extractName.lastIndexOf(".");
3440 17 Jul 15 olle 382             if (lastDotIndex >= 0)
3440 17 Jul 15 olle 383             {
3440 17 Jul 15 olle 384               String specimenName = extractName.substring(0, lastDotIndex);
3440 17 Jul 15 olle 385               if (specimenName != null && !specimenName.equals(""))
3440 17 Jul 15 olle 386               {
3440 17 Jul 15 olle 387                 if (itemType.equals(ITEM_SPECIMEN))
3440 17 Jul 15 olle 388                 {
3440 17 Jul 15 olle 389                   tmpNameList.add(specimenName);
3440 17 Jul 15 olle 390                 }
3440 17 Jul 15 olle 391                 else if (itemType.equals(ITEM_DNA))
3440 17 Jul 15 olle 392                 {
3440 17 Jul 15 olle 393                   tmpNameList.add(specimenName + ".d");
3440 17 Jul 15 olle 394                 }
3440 17 Jul 15 olle 395                 else if (itemType.equals(ITEM_RNA))
3440 17 Jul 15 olle 396                 {
3440 17 Jul 15 olle 397                   tmpNameList.add(specimenName + ".r");
3440 17 Jul 15 olle 398                 }
3440 17 Jul 15 olle 399               }
3440 17 Jul 15 olle 400             }
3440 17 Jul 15 olle 401           }
3440 17 Jul 15 olle 402         }  
3440 17 Jul 15 olle 403       }
3440 17 Jul 15 olle 404     }
3440 17 Jul 15 olle 405     // Get final list of extract source item names related to the extract member items
3440 17 Jul 15 olle 406     List<String> itemNameList = new ArrayList<String>();
3440 17 Jul 15 olle 407     if (meludiItemsFirst)
3440 17 Jul 15 olle 408     {
3440 17 Jul 15 olle 409       // Get separate lists of MeLuDI and extra material item names
3440 17 Jul 15 olle 410       List<String> meludiItemNameList = new ArrayList<String>();
3440 17 Jul 15 olle 411       List<String> extraItemNameList = new ArrayList<String>();
3440 17 Jul 15 olle 412       for (String itemName: tmpNameList)
3440 17 Jul 15 olle 413       {
4149 03 Oct 16 olle 414         if (Meludi.itemIsMeludiItem(itemName, activeProjectId))
3440 17 Jul 15 olle 415         {
3440 17 Jul 15 olle 416           meludiItemNameList.add(itemName);
3440 17 Jul 15 olle 417         }
3440 17 Jul 15 olle 418         else
3440 17 Jul 15 olle 419         {
3440 17 Jul 15 olle 420           extraItemNameList.add(itemName);
3440 17 Jul 15 olle 421         }
3440 17 Jul 15 olle 422       }
3440 17 Jul 15 olle 423       // Get a list of item names, MeLuDI items first, then extra material items, each alphabetically sorted in each group
3440 17 Jul 15 olle 424       for (String itemName: meludiItemNameList)
3440 17 Jul 15 olle 425       {
3440 17 Jul 15 olle 426         itemNameList.add(itemName);
3440 17 Jul 15 olle 427       }
3440 17 Jul 15 olle 428       for (String itemName: extraItemNameList)
3440 17 Jul 15 olle 429       {
3440 17 Jul 15 olle 430         itemNameList.add(itemName);
3440 17 Jul 15 olle 431       }
3440 17 Jul 15 olle 432     }
3440 17 Jul 15 olle 433     else
3440 17 Jul 15 olle 434     {
3440 17 Jul 15 olle 435       itemNameList = tmpNameList;
3440 17 Jul 15 olle 436     }
3440 17 Jul 15 olle 437     return itemNameList;
3440 17 Jul 15 olle 438   }
3501 21 Sep 15 olle 439
3501 21 Sep 15 olle 440   /**
3501 21 Sep 15 olle 441    * Fetches a list of extracts for one item type from this start item list
3501 21 Sep 15 olle 442    * with Extract type members. A DNA/RNA input extract is stored as
3501 21 Sep 15 olle 443    * member, while For a specimen, the DNA child extract is stored.
3501 21 Sep 15 olle 444    * MeLuDI project items are listed first, then extra material items,
3501 21 Sep 15 olle 445    * each alphabetically sorted after their name in each group.
3501 21 Sep 15 olle 446    * 
3501 21 Sep 15 olle 447    * @param dc DbControl The DbControl to use when connecting to the database.
3501 21 Sep 15 olle 448    * @param itemType String A string defining the type of item to return (Specimen/DNA/RNA).
3501 21 Sep 15 olle 449    * @return List<Extract> A list of extract member items in the item list.
3501 21 Sep 15 olle 450    */
3501 21 Sep 15 olle 451   public List<Extract> fetchExtractListFromItemList(DbControl dc, String itemType)
3501 21 Sep 15 olle 452   {
3501 21 Sep 15 olle 453     boolean meludiItemsFirst = true;
3501 21 Sep 15 olle 454     List<Extract> extractList = fetchExtractListFromItemList(dc, itemType, meludiItemsFirst);
3501 21 Sep 15 olle 455     return extractList;
3501 21 Sep 15 olle 456   }
3501 21 Sep 15 olle 457
3501 21 Sep 15 olle 458   /**
3501 21 Sep 15 olle 459    * Fetches a list of extracts for one item type from this start item list
3501 21 Sep 15 olle 460    * with Extract type members. A DNA/RNA input extract is stored as
3501 21 Sep 15 olle 461    * member, while For a specimen, the DNA child extract is stored.
3501 21 Sep 15 olle 462    * The returned list is either alphabetically sorted after item names,
3501 21 Sep 15 olle 463    * with mixed MeLuDI and extra material items, or first lists MeLuDI items,
3501 21 Sep 15 olle 464    * then extra material items, each alphabetically sorted after their name
3501 21 Sep 15 olle 465    * in each group.
3501 21 Sep 15 olle 466    * 
3501 21 Sep 15 olle 467    * @param dc DbControl The DbControl to use when connecting to the database.
3501 21 Sep 15 olle 468    * @param itemType String A string defining the type of item to return (Specimen/DNA/RNA).
3501 21 Sep 15 olle 469    * @param meludiItemsFirst boolean Boolean flag indicating if MeLuDI items should be listed before extra material items.
3501 21 Sep 15 olle 470    * @return List<Extract> A list of extract member items in the item list.
3501 21 Sep 15 olle 471    */
3501 21 Sep 15 olle 472   @SuppressWarnings("unchecked")
3501 21 Sep 15 olle 473   public List<Extract> fetchExtractListFromItemList(DbControl dc, String itemType, boolean meludiItemsFirst)
3501 21 Sep 15 olle 474   {
3501 21 Sep 15 olle 475     ItemList startList = this.getItem();
3501 21 Sep 15 olle 476     if (itemType == null)
3501 21 Sep 15 olle 477     {
3501 21 Sep 15 olle 478       itemType = ITEM_DNA;
3501 21 Sep 15 olle 479     }
4149 03 Oct 16 olle 480     int activeProjectId = dc.getSessionControl().getActiveProjectId();
3501 21 Sep 15 olle 481     // Construct a query to get all member extract items in the item list, sorted alphabetically after name
3501 21 Sep 15 olle 482     ItemQuery<Extract> query = (ItemQuery<Extract>)startList.getMembers();
3501 21 Sep 15 olle 483     query.include(Include.ALL);
3501 21 Sep 15 olle 484     query.order(Orders.asc(Hql.property("name")));
3501 21 Sep 15 olle 485     List<Extract> rawExtractList = query.list(dc);
3501 21 Sep 15 olle 486     // Get an alphabetically sorted list of extract member items of the desired type
3501 21 Sep 15 olle 487     List<Extract> tmpExtractList = new ArrayList<Extract>();
3501 21 Sep 15 olle 488     if (rawExtractList != null)
3501 21 Sep 15 olle 489     {
3501 21 Sep 15 olle 490       for (Extract e: rawExtractList)
3501 21 Sep 15 olle 491       {
3501 21 Sep 15 olle 492         if (e != null)
3501 21 Sep 15 olle 493         {
3501 21 Sep 15 olle 494           String extractName = e.getName();
3501 21 Sep 15 olle 495           if (itemType.equals(ITEM_DNA) && Meludi.itemIsDna(extractName))
3501 21 Sep 15 olle 496           {
3501 21 Sep 15 olle 497             tmpExtractList.add(e);
3501 21 Sep 15 olle 498           }
3501 21 Sep 15 olle 499           else if (itemType.equals(ITEM_RNA) && Meludi.itemIsRna(extractName))
3501 21 Sep 15 olle 500           {
3501 21 Sep 15 olle 501             tmpExtractList.add(e);
3501 21 Sep 15 olle 502           }
3501 21 Sep 15 olle 503         }  
3501 21 Sep 15 olle 504       }
3501 21 Sep 15 olle 505     }
3501 21 Sep 15 olle 506     // Get final list of extract member items of the desired type
3501 21 Sep 15 olle 507     List<Extract> extractList = new ArrayList<Extract>();
3501 21 Sep 15 olle 508     if (meludiItemsFirst)
3501 21 Sep 15 olle 509     {
3501 21 Sep 15 olle 510       // Get separate lists of MeLuDI and extra material item names
3501 21 Sep 15 olle 511       List<Extract> meludiItemExtractList = new ArrayList<Extract>();
3501 21 Sep 15 olle 512       List<Extract> extraItemExtractList = new ArrayList<Extract>();
3501 21 Sep 15 olle 513       for (Extract e: tmpExtractList)
3501 21 Sep 15 olle 514       {
3501 21 Sep 15 olle 515         String extractName = e.getName();
4149 03 Oct 16 olle 516         if (Meludi.itemIsMeludiItem(extractName, activeProjectId))
3501 21 Sep 15 olle 517         {
3501 21 Sep 15 olle 518           meludiItemExtractList.add(e);
3501 21 Sep 15 olle 519         }
3501 21 Sep 15 olle 520         else
3501 21 Sep 15 olle 521         {
3501 21 Sep 15 olle 522           extraItemExtractList.add(e);
3501 21 Sep 15 olle 523         }
3501 21 Sep 15 olle 524       }
3501 21 Sep 15 olle 525       // Get a list of extract member items, MeLuDI items first, then extra material items, each alphabetically sorted in each group
3501 21 Sep 15 olle 526       for (Extract e: meludiItemExtractList)
3501 21 Sep 15 olle 527       {
3501 21 Sep 15 olle 528         extractList.add(e);
3501 21 Sep 15 olle 529       }
3501 21 Sep 15 olle 530       for (Extract e: extraItemExtractList)
3501 21 Sep 15 olle 531       {
3501 21 Sep 15 olle 532         extractList.add(e);
3501 21 Sep 15 olle 533       }
3501 21 Sep 15 olle 534     }
3501 21 Sep 15 olle 535     else
3501 21 Sep 15 olle 536     {
3501 21 Sep 15 olle 537       extractList = tmpExtractList;
3501 21 Sep 15 olle 538     }
3501 21 Sep 15 olle 539     return extractList;
3501 21 Sep 15 olle 540   }
3431 29 Jun 15 olle 541 }