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

Code
Comments
Other
Rev Date Author Line
3532 05 Oct 15 olle 1 package net.sf.basedb.meludi.dao;
3532 05 Oct 15 olle 2
3532 05 Oct 15 olle 3 import java.util.ArrayList;
3532 05 Oct 15 olle 4 import java.util.Collection;
3532 05 Oct 15 olle 5 import java.util.List;
3532 05 Oct 15 olle 6
3532 05 Oct 15 olle 7 import org.json.simple.JSONObject;
3532 05 Oct 15 olle 8
3532 05 Oct 15 olle 9 import net.sf.basedb.core.DbControl;
3532 05 Oct 15 olle 10 import net.sf.basedb.core.Include;
3532 05 Oct 15 olle 11 import net.sf.basedb.core.InvalidDataException;
3532 05 Oct 15 olle 12 import net.sf.basedb.core.ItemQuery;
3532 05 Oct 15 olle 13 import net.sf.basedb.core.Tag;
3532 05 Oct 15 olle 14 import net.sf.basedb.core.Type;
3532 05 Oct 15 olle 15 import net.sf.basedb.core.query.Expressions;
3532 05 Oct 15 olle 16 import net.sf.basedb.core.query.Hql;
3532 05 Oct 15 olle 17 import net.sf.basedb.core.query.Orders;
3532 05 Oct 15 olle 18 import net.sf.basedb.core.query.Restrictions;
3532 05 Oct 15 olle 19 import net.sf.basedb.meludi.JsonUtil;
3532 05 Oct 15 olle 20 import net.sf.basedb.meludi.Meludi;
3532 05 Oct 15 olle 21
3532 05 Oct 15 olle 22 /**
3532 05 Oct 15 olle 23   Class for loading information that is related to barcode tags.
3532 05 Oct 15 olle 24   
3532 05 Oct 15 olle 25   @author olle
3532 05 Oct 15 olle 26   @since 1.3.4
3532 05 Oct 15 olle 27 */
3532 05 Oct 15 olle 28 public class Barcode
3532 05 Oct 15 olle 29   extends MeludiItem<Tag>
3532 05 Oct 15 olle 30 {
3532 05 Oct 15 olle 31
3532 05 Oct 15 olle 32
3532 05 Oct 15 olle 33   /**
3532 05 Oct 15 olle 34     Find a barcode item with the given name.
3532 05 Oct 15 olle 35     @return A barcode item, or null if not found.
3532 05 Oct 15 olle 36   */
3532 05 Oct 15 olle 37   public static Barcode findByName(DbControl dc, String name)
3532 05 Oct 15 olle 38   {
3532 05 Oct 15 olle 39     Barcode barcode = null;
3532 05 Oct 15 olle 40
3532 05 Oct 15 olle 41     ItemQuery<Tag> barcodeQuery = Tag.getQuery();
3532 05 Oct 15 olle 42     Subtype.BARCODE.addFilter(dc, barcodeQuery);
3532 05 Oct 15 olle 43     barcodeQuery.restrict(Restrictions.like(Hql.property("name"), Expressions.string(name)));
3532 05 Oct 15 olle 44     barcodeQuery.order(Orders.desc(Hql.property("name")));        
3532 05 Oct 15 olle 45     barcodeQuery.include(Include.ALL);
3532 05 Oct 15 olle 46   
3532 05 Oct 15 olle 47     List<Tag> tmp = barcodeQuery.list(dc);
3532 05 Oct 15 olle 48     if (tmp.size() > 1)
3532 05 Oct 15 olle 49     {
3532 05 Oct 15 olle 50       String barcodeIds = "";
3532 05 Oct 15 olle 51       for (int i = 0; i < tmp.size(); i++)
3532 05 Oct 15 olle 52       {
3532 05 Oct 15 olle 53         Barcode tmpBarcode = new Barcode(tmp.get(i));
3532 05 Oct 15 olle 54         int id = tmpBarcode.getItem().getId();
3532 05 Oct 15 olle 55         if (!barcodeIds.equals(""))
3532 05 Oct 15 olle 56         {
3532 05 Oct 15 olle 57           barcodeIds += ", ";
3532 05 Oct 15 olle 58         }
3532 05 Oct 15 olle 59         barcodeIds += id;
3532 05 Oct 15 olle 60       }
3532 05 Oct 15 olle 61       throw new InvalidDataException(
3532 05 Oct 15 olle 62           "More than one barcode item with the name " + name + " was found. " +
3532 05 Oct 15 olle 63           " The ID values are " + barcodeIds + ". " +
3532 05 Oct 15 olle 64           "This wizard can't be used until that is corrected.");
3532 05 Oct 15 olle 65     }
3532 05 Oct 15 olle 66     if (tmp.size() == 1)
3532 05 Oct 15 olle 67     {
3532 05 Oct 15 olle 68       barcode = new Barcode(tmp.get(0));
3532 05 Oct 15 olle 69     }
3532 05 Oct 15 olle 70     return barcode;
3532 05 Oct 15 olle 71   }
3532 05 Oct 15 olle 72
3532 05 Oct 15 olle 73   /**
3532 05 Oct 15 olle 74     Get a barcode tag when the id is known.
3532 05 Oct 15 olle 75   */
3532 05 Oct 15 olle 76   public static Barcode getById(DbControl dc, int id)
3532 05 Oct 15 olle 77   {
3532 05 Oct 15 olle 78     return new Barcode(Tag.getById(dc, id));
3532 05 Oct 15 olle 79   }
3532 05 Oct 15 olle 80   
3532 05 Oct 15 olle 81   public static Barcode get(Tag tag)
3532 05 Oct 15 olle 82   {
3532 05 Oct 15 olle 83     return new Barcode(tag);
3532 05 Oct 15 olle 84   }
3532 05 Oct 15 olle 85   
3532 05 Oct 15 olle 86   public static List<Barcode> toList(Collection<Tag> tags)
3532 05 Oct 15 olle 87   {
3532 05 Oct 15 olle 88     List<Barcode> barcode = new ArrayList<Barcode>(tags.size());
3532 05 Oct 15 olle 89     for (Tag t : tags)
3532 05 Oct 15 olle 90     {
3532 05 Oct 15 olle 91       barcode.add(new Barcode(t));
3532 05 Oct 15 olle 92     }
3532 05 Oct 15 olle 93     return barcode;
3532 05 Oct 15 olle 94   }
3532 05 Oct 15 olle 95   
3532 05 Oct 15 olle 96   public static List<Barcode> toBarcode(Collection<Tag> tags)
3532 05 Oct 15 olle 97   {
3532 05 Oct 15 olle 98     List<Barcode> barcode = new ArrayList<Barcode>(tags.size());
3532 05 Oct 15 olle 99     for (Tag t : tags)
3532 05 Oct 15 olle 100     {
3532 05 Oct 15 olle 101       barcode.add(new Barcode(t));
3532 05 Oct 15 olle 102     }
3532 05 Oct 15 olle 103     return barcode;
3532 05 Oct 15 olle 104   }
3532 05 Oct 15 olle 105   
3532 05 Oct 15 olle 106   private Barcode(Tag tag)
3532 05 Oct 15 olle 107   {
3532 05 Oct 15 olle 108     super(tag);
3532 05 Oct 15 olle 109   }  
3532 05 Oct 15 olle 110   
3532 05 Oct 15 olle 111   
3532 05 Oct 15 olle 112   /**
3532 05 Oct 15 olle 113     Get the real tag that represents this barcode in BASE.
3532 05 Oct 15 olle 114   */
3532 05 Oct 15 olle 115   public Tag getTag()
3532 05 Oct 15 olle 116   {
3532 05 Oct 15 olle 117     return getItem();
3532 05 Oct 15 olle 118   }
3532 05 Oct 15 olle 119
3532 05 Oct 15 olle 120   @SuppressWarnings("unchecked")
3532 05 Oct 15 olle 121   @Override
3532 05 Oct 15 olle 122   protected void initJSON(JSONObject json) 
3532 05 Oct 15 olle 123   {
3532 05 Oct 15 olle 124     super.initJSON(json);
3532 05 Oct 15 olle 125   }
3532 05 Oct 15 olle 126 }