extensions/net.sf.basedb.varsearch/trunk/src/net/sf/basedb/varsearch/dao/Datafiletype.java

Code
Comments
Other
Rev Date Author Line
6113 02 Feb 21 nicklas 1 package net.sf.basedb.varsearch.dao;
6113 02 Feb 21 nicklas 2
6113 02 Feb 21 nicklas 3 import java.lang.reflect.Field;
6113 02 Feb 21 nicklas 4 import java.util.ArrayList;
6113 02 Feb 21 nicklas 5 import java.util.Collections;
6113 02 Feb 21 nicklas 6 import java.util.List;
6113 02 Feb 21 nicklas 7
6113 02 Feb 21 nicklas 8 import net.sf.basedb.core.DataFileType;
6113 02 Feb 21 nicklas 9 import net.sf.basedb.core.DbControl;
6113 02 Feb 21 nicklas 10 import net.sf.basedb.core.File;
6113 02 Feb 21 nicklas 11 import net.sf.basedb.core.FileSetMember;
6113 02 Feb 21 nicklas 12 import net.sf.basedb.core.FileStoreEnabled;
6113 02 Feb 21 nicklas 13 import net.sf.basedb.core.Include;
6113 02 Feb 21 nicklas 14 import net.sf.basedb.core.InvalidDataException;
6113 02 Feb 21 nicklas 15 import net.sf.basedb.core.Item;
6113 02 Feb 21 nicklas 16 import net.sf.basedb.core.ItemNotFoundException;
6113 02 Feb 21 nicklas 17 import net.sf.basedb.core.ItemQuery;
6113 02 Feb 21 nicklas 18 import net.sf.basedb.core.Type;
6113 02 Feb 21 nicklas 19 import net.sf.basedb.core.query.Expressions;
6113 02 Feb 21 nicklas 20 import net.sf.basedb.core.query.Hql;
6113 02 Feb 21 nicklas 21 import net.sf.basedb.core.query.Restrictions;
6113 02 Feb 21 nicklas 22
6113 02 Feb 21 nicklas 23 /**
6113 02 Feb 21 nicklas 24   Used to define file types for data files.
6113 02 Feb 21 nicklas 25   
6113 02 Feb 21 nicklas 26   @author nicklas
6113 02 Feb 21 nicklas 27 */
6113 02 Feb 21 nicklas 28 public class Datafiletype 
6113 02 Feb 21 nicklas 29 {
6113 02 Feb 21 nicklas 30
6113 02 Feb 21 nicklas 31   /**
6113 02 Feb 21 nicklas 32     Variant call file.
6113 02 Feb 21 nicklas 33   */
6113 02 Feb 21 nicklas 34   public static final Datafiletype VCF = 
6113 02 Feb 21 nicklas 35     new Datafiletype("VCF", "net.sf.basedb.reggie.vcf", 
6113 02 Feb 21 nicklas 36       Item.RAWBIOASSAY);
6113 02 Feb 21 nicklas 37
6113 02 Feb 21 nicklas 38   /**
6543 18 Jan 22 nicklas 39     Variant call file for array designs.
6543 18 Jan 22 nicklas 40     @since 1.5
6543 18 Jan 22 nicklas 41   */
6543 18 Jan 22 nicklas 42   public static final Datafiletype VCF_DESIGN = 
6543 18 Jan 22 nicklas 43     new Datafiletype("VCF (design)", "net.sf.basedb.reggie.vcf-design", 
6543 18 Jan 22 nicklas 44       Item.ARRAYDESIGN);
6543 18 Jan 22 nicklas 45
6543 18 Jan 22 nicklas 46   
6543 18 Jan 22 nicklas 47   /**
6113 02 Feb 21 nicklas 48     Get the file type by name of the static constant defined in this class.
6113 02 Feb 21 nicklas 49     
6113 02 Feb 21 nicklas 50     @param cName The name of the static constant
6113 02 Feb 21 nicklas 51     @return A bioplate type object or null if not found
6113 02 Feb 21 nicklas 52    */
6113 02 Feb 21 nicklas 53   public static Datafiletype getByCName(String cName)
6113 02 Feb 21 nicklas 54   {
6113 02 Feb 21 nicklas 55     if (cName == null) return null;
6113 02 Feb 21 nicklas 56     Datafiletype fs = null;
6113 02 Feb 21 nicklas 57     try
6113 02 Feb 21 nicklas 58     {
6113 02 Feb 21 nicklas 59       Field f = Datafiletype.class.getDeclaredField(cName);
6113 02 Feb 21 nicklas 60       fs = (Datafiletype)f.get(null);
6113 02 Feb 21 nicklas 61     }
6113 02 Feb 21 nicklas 62     catch (NoSuchFieldException ex)
6113 02 Feb 21 nicklas 63     {}
6113 02 Feb 21 nicklas 64     catch (IllegalAccessException ex)
6113 02 Feb 21 nicklas 65     {}
6113 02 Feb 21 nicklas 66     catch (ClassCastException ex)
6113 02 Feb 21 nicklas 67     {}
6113 02 Feb 21 nicklas 68     return fs;
6113 02 Feb 21 nicklas 69   }
6113 02 Feb 21 nicklas 70   
6113 02 Feb 21 nicklas 71   private final String name;
6113 02 Feb 21 nicklas 72   private final String externalId;
6113 02 Feb 21 nicklas 73   private final Item itemType;
6113 02 Feb 21 nicklas 74   private int id;
6113 02 Feb 21 nicklas 75   
6113 02 Feb 21 nicklas 76   /**
6113 02 Feb 21 nicklas 77     Create a new definition
6113 02 Feb 21 nicklas 78   */
6113 02 Feb 21 nicklas 79   private Datafiletype(String name, String externalId, Item itemType)
6113 02 Feb 21 nicklas 80   {
6113 02 Feb 21 nicklas 81     this.name = name;
6113 02 Feb 21 nicklas 82     this.externalId = externalId;
6113 02 Feb 21 nicklas 83     this.itemType = itemType;
6113 02 Feb 21 nicklas 84   }
6113 02 Feb 21 nicklas 85     
6113 02 Feb 21 nicklas 86   /**
6113 02 Feb 21 nicklas 87     Get the name of the file type.
6113 02 Feb 21 nicklas 88   */
6113 02 Feb 21 nicklas 89   public String getName()
6113 02 Feb 21 nicklas 90   {
6113 02 Feb 21 nicklas 91     return name;
6113 02 Feb 21 nicklas 92   }
6113 02 Feb 21 nicklas 93
6113 02 Feb 21 nicklas 94   
6113 02 Feb 21 nicklas 95   /**
6113 02 Feb 21 nicklas 96     Get the external id.
6113 02 Feb 21 nicklas 97   */
6113 02 Feb 21 nicklas 98   public String getExternalId()
6113 02 Feb 21 nicklas 99   {
6113 02 Feb 21 nicklas 100     return externalId;
6113 02 Feb 21 nicklas 101   }
6113 02 Feb 21 nicklas 102   
6113 02 Feb 21 nicklas 103   public Item getItemType()
6113 02 Feb 21 nicklas 104   {
6113 02 Feb 21 nicklas 105     return itemType;
6113 02 Feb 21 nicklas 106   }
6113 02 Feb 21 nicklas 107   
6113 02 Feb 21 nicklas 108
6113 02 Feb 21 nicklas 109   /**
6113 02 Feb 21 nicklas 110     Load the file type as a BASE item
6113 02 Feb 21 nicklas 111     @param dc The DbControl to use for database access
6113 02 Feb 21 nicklas 112     @return An FileServer object
6113 02 Feb 21 nicklas 113   */
6113 02 Feb 21 nicklas 114   public DataFileType load(DbControl dc)
6113 02 Feb 21 nicklas 115   {
6113 02 Feb 21 nicklas 116     DataFileType type = null;
6113 02 Feb 21 nicklas 117     if (id == 0)
6113 02 Feb 21 nicklas 118     {
6113 02 Feb 21 nicklas 119       List<DataFileType> result = list(dc);
6113 02 Feb 21 nicklas 120       if (result.size() == 0)
6113 02 Feb 21 nicklas 121       {
6113 02 Feb 21 nicklas 122         throw new ItemNotFoundException("DataFileType["+getName()+"]");
6113 02 Feb 21 nicklas 123       }
6113 02 Feb 21 nicklas 124       else if (result.size() > 1)
6113 02 Feb 21 nicklas 125       {
6113 02 Feb 21 nicklas 126         throw new InvalidDataException("Found > 1 DataFileType["+getName()+"]");
6113 02 Feb 21 nicklas 127       }
6113 02 Feb 21 nicklas 128       type = result.get(0);
6113 02 Feb 21 nicklas 129       id = type.getId();
6113 02 Feb 21 nicklas 130     }
6113 02 Feb 21 nicklas 131     else
6113 02 Feb 21 nicklas 132     {
6113 02 Feb 21 nicklas 133       type = DataFileType.getById(dc, id);
6113 02 Feb 21 nicklas 134     }
6113 02 Feb 21 nicklas 135     return type;
6113 02 Feb 21 nicklas 136   }
6113 02 Feb 21 nicklas 137   
6113 02 Feb 21 nicklas 138   /**
6113 02 Feb 21 nicklas 139     Load the file type as a BASE item. Same as {@link #load(DbControl)} but return null
6113 02 Feb 21 nicklas 140     if no BASE item is found instead of throwing an exception.
6113 02 Feb 21 nicklas 141     @param dc The DbControl to use for database access
6113 02 Feb 21 nicklas 142     @return A DataFileType object
6113 02 Feb 21 nicklas 143   */
6113 02 Feb 21 nicklas 144   public DataFileType get(DbControl dc)
6113 02 Feb 21 nicklas 145   {
6113 02 Feb 21 nicklas 146     DataFileType type = null;
6113 02 Feb 21 nicklas 147     if (id == 0)
6113 02 Feb 21 nicklas 148     {
6113 02 Feb 21 nicklas 149       List<DataFileType> result = list(dc);
6113 02 Feb 21 nicklas 150       if (result.size() == 1)
6113 02 Feb 21 nicklas 151       {
6113 02 Feb 21 nicklas 152         type = result.get(0);
6113 02 Feb 21 nicklas 153         id = type.getId();
6113 02 Feb 21 nicklas 154       }
6113 02 Feb 21 nicklas 155     }
6113 02 Feb 21 nicklas 156     else
6113 02 Feb 21 nicklas 157     {
6113 02 Feb 21 nicklas 158       type = DataFileType.getById(dc, id);
6113 02 Feb 21 nicklas 159     }
6113 02 Feb 21 nicklas 160     return type;
6113 02 Feb 21 nicklas 161   }
6113 02 Feb 21 nicklas 162
6113 02 Feb 21 nicklas 163   
6113 02 Feb 21 nicklas 164   /**
6113 02 Feb 21 nicklas 165     List all file server registered in BASE with an name matching this 
6113 02 Feb 21 nicklas 166     definition. Normally, only a single file server should be returned.
6113 02 Feb 21 nicklas 167   */
6113 02 Feb 21 nicklas 168   public List<DataFileType> list(DbControl dc)
6113 02 Feb 21 nicklas 169   {
6113 02 Feb 21 nicklas 170     ItemQuery<DataFileType> query = DataFileType.getQuery();
6113 02 Feb 21 nicklas 171     query.restrict(
6113 02 Feb 21 nicklas 172       Restrictions.eq(
6113 02 Feb 21 nicklas 173         Hql.property("externalId"), 
6113 02 Feb 21 nicklas 174         Expressions.parameter("name", getExternalId(), Type.STRING)
6113 02 Feb 21 nicklas 175       ));
6113 02 Feb 21 nicklas 176     query.include(Include.ALL);
6113 02 Feb 21 nicklas 177     return query.list(dc);
6113 02 Feb 21 nicklas 178   }
6113 02 Feb 21 nicklas 179   
6113 02 Feb 21 nicklas 180   /**
6113 02 Feb 21 nicklas 181     Get the file of this file type that is attached to the given item.
6113 02 Feb 21 nicklas 182   */
6113 02 Feb 21 nicklas 183   public File getFile(DbControl dc, FileStoreEnabled item)
6113 02 Feb 21 nicklas 184   {
6113 02 Feb 21 nicklas 185     if (!item.hasFileSet()) return null;
6113 02 Feb 21 nicklas 186     FileSetMember member = item.getFileSet().getMember(get(dc));
6113 02 Feb 21 nicklas 187     return member == null ? null : member.getFile();
6113 02 Feb 21 nicklas 188   }
6113 02 Feb 21 nicklas 189   
6113 02 Feb 21 nicklas 190   /**
6113 02 Feb 21 nicklas 191     Get all files of this type that are attached to the given
6113 02 Feb 21 nicklas 192     item.
6113 02 Feb 21 nicklas 193   */
6113 02 Feb 21 nicklas 194   public List<File> getAllFiles(DbControl dc, FileStoreEnabled item)
6113 02 Feb 21 nicklas 195   {
6113 02 Feb 21 nicklas 196     if (!item.hasFileSet()) return Collections.emptyList();
6113 02 Feb 21 nicklas 197     ItemQuery<FileSetMember> query = item.getFileSet().getMembers(get(dc));
6113 02 Feb 21 nicklas 198     List<File> files = new ArrayList<>();
6113 02 Feb 21 nicklas 199     for (FileSetMember member : query.list(dc))
6113 02 Feb 21 nicklas 200     {
6113 02 Feb 21 nicklas 201       files.add(member.getFile());
6113 02 Feb 21 nicklas 202     }
6113 02 Feb 21 nicklas 203     return files;
6113 02 Feb 21 nicklas 204   }
6113 02 Feb 21 nicklas 205
6113 02 Feb 21 nicklas 206 }