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

Code
Comments
Other
Rev Date Author Line
1811 28 Jan 13 nicklas 1 package net.sf.basedb.reggie.dao;
1811 28 Jan 13 nicklas 2
1811 28 Jan 13 nicklas 3 import java.lang.reflect.Field;
1811 28 Jan 13 nicklas 4 import java.util.List;
1811 28 Jan 13 nicklas 5
1811 28 Jan 13 nicklas 6
1811 28 Jan 13 nicklas 7 import net.sf.basedb.core.BioPlateEventType;
1811 28 Jan 13 nicklas 8 import net.sf.basedb.core.DbControl;
1811 28 Jan 13 nicklas 9 import net.sf.basedb.core.Include;
1811 28 Jan 13 nicklas 10 import net.sf.basedb.core.InvalidDataException;
1811 28 Jan 13 nicklas 11 import net.sf.basedb.core.ItemNotFoundException;
1811 28 Jan 13 nicklas 12 import net.sf.basedb.core.ItemQuery;
1811 28 Jan 13 nicklas 13 import net.sf.basedb.core.Type;
1811 28 Jan 13 nicklas 14 import net.sf.basedb.core.query.Expressions;
1811 28 Jan 13 nicklas 15 import net.sf.basedb.core.query.Hql;
1811 28 Jan 13 nicklas 16 import net.sf.basedb.core.query.Restrictions;
1811 28 Jan 13 nicklas 17
1811 28 Jan 13 nicklas 18 /**
1811 28 Jan 13 nicklas 19   Used to define a bioplate event type items.
1811 28 Jan 13 nicklas 20   
1811 28 Jan 13 nicklas 21   @author nicklas
1867 19 Feb 13 nicklas 22   @since 2.12
1811 28 Jan 13 nicklas 23 */
1811 28 Jan 13 nicklas 24 public class BioplateEventtype 
1811 28 Jan 13 nicklas 25 {
5426 14 May 19 nicklas 26
1811 28 Jan 13 nicklas 27   
1811 28 Jan 13 nicklas 28   /**
1811 28 Jan 13 nicklas 29     Get the annotation type by name of the static constant defined in this class.
1811 28 Jan 13 nicklas 30     
3571 30 Oct 15 nicklas 31     @param cName The name of the static constant
1811 28 Jan 13 nicklas 32     @return An annotationtype object or null if not found
1811 28 Jan 13 nicklas 33   */
1811 28 Jan 13 nicklas 34   public static BioplateEventtype getByCName(String cName)
1811 28 Jan 13 nicklas 35   {
1811 28 Jan 13 nicklas 36     if (cName == null) return null;
1811 28 Jan 13 nicklas 37     BioplateEventtype type = null;
1811 28 Jan 13 nicklas 38     try
1811 28 Jan 13 nicklas 39     {
1811 28 Jan 13 nicklas 40       Field f = BioplateEventtype.class.getDeclaredField(cName);
1811 28 Jan 13 nicklas 41       type = (BioplateEventtype)f.get(null);
1811 28 Jan 13 nicklas 42     }
1811 28 Jan 13 nicklas 43     catch (NoSuchFieldException ex)
1811 28 Jan 13 nicklas 44     {}
1811 28 Jan 13 nicklas 45     catch (IllegalAccessException ex)
1811 28 Jan 13 nicklas 46     {}
1811 28 Jan 13 nicklas 47     catch (ClassCastException ex)
1811 28 Jan 13 nicklas 48     {}
1811 28 Jan 13 nicklas 49     return type;
1811 28 Jan 13 nicklas 50   }
1811 28 Jan 13 nicklas 51   
1811 28 Jan 13 nicklas 52   private final String name;
1811 28 Jan 13 nicklas 53   private final String systemId;
1811 28 Jan 13 nicklas 54   private final String description;
1811 28 Jan 13 nicklas 55   private int id;
1811 28 Jan 13 nicklas 56
1811 28 Jan 13 nicklas 57
1811 28 Jan 13 nicklas 58   /**
1811 28 Jan 13 nicklas 59     Create a new definition of a bioplate event type.
1811 28 Jan 13 nicklas 60     
1811 28 Jan 13 nicklas 61     @param name The name of the event type
1811 28 Jan 13 nicklas 62     @param systemId The system id of the event type
1811 28 Jan 13 nicklas 63   */
1811 28 Jan 13 nicklas 64   private BioplateEventtype(String name, String systemId, String description)
1811 28 Jan 13 nicklas 65   {
1811 28 Jan 13 nicklas 66     this.name = name;
1811 28 Jan 13 nicklas 67     this.systemId = systemId;
1811 28 Jan 13 nicklas 68     this.description = description;
1811 28 Jan 13 nicklas 69   }
1811 28 Jan 13 nicklas 70   
1811 28 Jan 13 nicklas 71   /**
1811 28 Jan 13 nicklas 72     Get the name of the event type.
1811 28 Jan 13 nicklas 73   */
1811 28 Jan 13 nicklas 74   public String getName()
1811 28 Jan 13 nicklas 75   {
1811 28 Jan 13 nicklas 76     return name;
1811 28 Jan 13 nicklas 77   }
1811 28 Jan 13 nicklas 78   
1811 28 Jan 13 nicklas 79   public String getSystemId()
1811 28 Jan 13 nicklas 80   {
1811 28 Jan 13 nicklas 81     return systemId;
1811 28 Jan 13 nicklas 82   }
1811 28 Jan 13 nicklas 83   
1811 28 Jan 13 nicklas 84   public String getDescription()
1811 28 Jan 13 nicklas 85   {
1811 28 Jan 13 nicklas 86     return description;
1811 28 Jan 13 nicklas 87   }
1811 28 Jan 13 nicklas 88   
1811 28 Jan 13 nicklas 89   /**
1811 28 Jan 13 nicklas 90     Load the underlying bioplate event type. If it doesn't exists and exception is thrown.
1811 28 Jan 13 nicklas 91     
1811 28 Jan 13 nicklas 92     @param dc The DbControl to use for database access
1811 28 Jan 13 nicklas 93     @return A BioPlateEventType object
1811 28 Jan 13 nicklas 94   */
1811 28 Jan 13 nicklas 95   public BioPlateEventType load(DbControl dc)
1811 28 Jan 13 nicklas 96   {
1811 28 Jan 13 nicklas 97     BioPlateEventType type = null;
1811 28 Jan 13 nicklas 98     if (id == 0)
1811 28 Jan 13 nicklas 99     {
1811 28 Jan 13 nicklas 100       List<BioPlateEventType> result = list(dc);
1811 28 Jan 13 nicklas 101       if (result.size() == 0)
1811 28 Jan 13 nicklas 102       {
5426 14 May 19 nicklas 103         throw new ItemNotFoundException("BioPlateEventType["+(systemId==null?name:systemId)+"]");
1811 28 Jan 13 nicklas 104       }
1811 28 Jan 13 nicklas 105       else if (result.size() > 1)
1811 28 Jan 13 nicklas 106       {
5426 14 May 19 nicklas 107         throw new InvalidDataException("Found > 1 BioPlateEventType["+(systemId==null?name:systemId)+"]");
1811 28 Jan 13 nicklas 108       }
1811 28 Jan 13 nicklas 109       type = result.get(0);
1811 28 Jan 13 nicklas 110       id = type.getId();
1811 28 Jan 13 nicklas 111     }
1811 28 Jan 13 nicklas 112     else
1811 28 Jan 13 nicklas 113     {
1811 28 Jan 13 nicklas 114       type = BioPlateEventType.getById(dc, id);
1811 28 Jan 13 nicklas 115     }
1811 28 Jan 13 nicklas 116     return type;
1811 28 Jan 13 nicklas 117   }
1811 28 Jan 13 nicklas 118     
1811 28 Jan 13 nicklas 119   /**
1811 28 Jan 13 nicklas 120     List all bioplate event types registered in BASE with a name
1811 28 Jan 13 nicklas 121     matching this definition. Normally, only a single event type
1811 28 Jan 13 nicklas 122     type should be returned.
1811 28 Jan 13 nicklas 123   */
1811 28 Jan 13 nicklas 124   public List<BioPlateEventType> list(DbControl dc)
1811 28 Jan 13 nicklas 125   {
1811 28 Jan 13 nicklas 126     ItemQuery<BioPlateEventType> query = BioPlateEventType.getQuery();
5426 14 May 19 nicklas 127     if (systemId != null)
5426 14 May 19 nicklas 128     {
5426 14 May 19 nicklas 129       query.restrict(Restrictions.eq(Hql.property("systemId"), Expressions.parameter("systemId", systemId, Type.STRING)));
5426 14 May 19 nicklas 130     }
5426 14 May 19 nicklas 131     else
5426 14 May 19 nicklas 132     {
5426 14 May 19 nicklas 133       query.restrict(Restrictions.eq(Hql.property("name"), Expressions.parameter("name", name, Type.STRING)));
5426 14 May 19 nicklas 134     }
1811 28 Jan 13 nicklas 135     query.include(Include.ALL);
1811 28 Jan 13 nicklas 136     return query.list(dc);
1811 28 Jan 13 nicklas 137   }
1811 28 Jan 13 nicklas 138
1811 28 Jan 13 nicklas 139
1811 28 Jan 13 nicklas 140 }