src/core/net/sf/basedb/core/Tag.java

Code
Comments
Other
Rev Date Author Line
5632 17 May 11 nicklas 1 /*
5632 17 May 11 nicklas 2   $Id$
5632 17 May 11 nicklas 3
5632 17 May 11 nicklas 4   Copyright (C) 2011 Nicklas Nordborg
5632 17 May 11 nicklas 5
5632 17 May 11 nicklas 6   This file is part of BASE - BioArray Software Environment.
5632 17 May 11 nicklas 7   Available at http://base.thep.lu.se/
5632 17 May 11 nicklas 8
5632 17 May 11 nicklas 9   BASE is free software; you can redistribute it and/or
5632 17 May 11 nicklas 10   modify it under the terms of the GNU General Public License
5632 17 May 11 nicklas 11   as published by the Free Software Foundation; either version 3
5632 17 May 11 nicklas 12   of the License, or (at your option) any later version.
5632 17 May 11 nicklas 13
5632 17 May 11 nicklas 14   BASE is distributed in the hope that it will be useful,
5632 17 May 11 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5632 17 May 11 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5632 17 May 11 nicklas 17   GNU General Public License for more details.
5632 17 May 11 nicklas 18
5632 17 May 11 nicklas 19   You should have received a copy of the GNU General Public License
5632 17 May 11 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
5632 17 May 11 nicklas 21 */
5632 17 May 11 nicklas 22 package net.sf.basedb.core;
5632 17 May 11 nicklas 23
5632 17 May 11 nicklas 24 import java.util.Date;
5632 17 May 11 nicklas 25 import java.util.Set;
5632 17 May 11 nicklas 26
5632 17 May 11 nicklas 27 import net.sf.basedb.core.data.TagData;
7381 22 May 17 nicklas 28 import net.sf.basedb.core.hibernate.TypeWrapper;
5632 17 May 11 nicklas 29
5632 17 May 11 nicklas 30 /**
5632 17 May 11 nicklas 31   This class represents a tag. Tags are used on extracts so that they
5632 17 May 11 nicklas 32   can be tracked on physical bioassays.
5632 17 May 11 nicklas 33
5632 17 May 11 nicklas 34   @author Nicklas
5632 17 May 11 nicklas 35   @since 3.0
5632 17 May 11 nicklas 36   @base.modified $Date$
5632 17 May 11 nicklas 37 */
5632 17 May 11 nicklas 38 public class Tag
6874 17 Apr 15 nicklas 39   extends AnnotatedItem
5632 17 May 11 nicklas 40   implements Registered, Subtypable
5632 17 May 11 nicklas 41 {
5632 17 May 11 nicklas 42   /**
5632 17 May 11 nicklas 43     The type of item represented by this class.
5632 17 May 11 nicklas 44     @see Item#TAG
5632 17 May 11 nicklas 45     @see #getType()
5632 17 May 11 nicklas 46   */
5632 17 May 11 nicklas 47   public static final Item TYPE = Item.TAG;
5632 17 May 11 nicklas 48   
5632 17 May 11 nicklas 49   /**
5632 17 May 11 nicklas 50     The ID for the LABEL tag type.
5632 17 May 11 nicklas 51   */
5632 17 May 11 nicklas 52   public static final String LABEL = "net.sf.basedb.core.TagType.LABEL";
5632 17 May 11 nicklas 53
5632 17 May 11 nicklas 54   /**
5632 17 May 11 nicklas 55     The ID for the BARCODE tag type.
5632 17 May 11 nicklas 56   */
5632 17 May 11 nicklas 57   public static final String BARCODE = "net.sf.basedb.core.TagType.BARCODE";
5632 17 May 11 nicklas 58
5632 17 May 11 nicklas 59
5632 17 May 11 nicklas 60   /**
5632 17 May 11 nicklas 61     Create a new <code>Tag</code> item.
5632 17 May 11 nicklas 62
5632 17 May 11 nicklas 63     @param dc The <code>DbControl</code> which will be used for
5632 17 May 11 nicklas 64       permission checking and database access
5632 17 May 11 nicklas 65     @return The new <code>Label</code> item
5632 17 May 11 nicklas 66     @throws BaseException If there is an error
5632 17 May 11 nicklas 67   */
5632 17 May 11 nicklas 68   public static Tag getNew(DbControl dc)
5632 17 May 11 nicklas 69     throws BaseException
5632 17 May 11 nicklas 70   {
5632 17 May 11 nicklas 71     Tag t = dc.newItem(Tag.class);
5632 17 May 11 nicklas 72     t.setName("New tag");
5632 17 May 11 nicklas 73     t.getData().setEntryDate(new Date());
5632 17 May 11 nicklas 74     return t;
5632 17 May 11 nicklas 75   }
5632 17 May 11 nicklas 76
5632 17 May 11 nicklas 77   /**
5632 17 May 11 nicklas 78     Get a <code>Tag</code> item when you know the id.
5632 17 May 11 nicklas 79
5632 17 May 11 nicklas 80     @param dc The <code>DbControl</code> which will be used for
5632 17 May 11 nicklas 81       permission checking and database access.
5632 17 May 11 nicklas 82     @param id The id of the item to load
5632 17 May 11 nicklas 83     @return The <code>Tag</code> item
5632 17 May 11 nicklas 84     @throws ItemNotFoundException If an item with the specified 
5632 17 May 11 nicklas 85       id is not found
5632 17 May 11 nicklas 86     @throws PermissionDeniedException If the logged in user doesn't 
5632 17 May 11 nicklas 87       have {@link Permission#READ} permission to the item
5632 17 May 11 nicklas 88     @throws BaseException If there is another error
5632 17 May 11 nicklas 89   */
5632 17 May 11 nicklas 90   public static Tag getById(DbControl dc, int id)
5632 17 May 11 nicklas 91     throws ItemNotFoundException, PermissionDeniedException, BaseException
5632 17 May 11 nicklas 92   {
5632 17 May 11 nicklas 93     Tag t = dc.loadItem(Tag.class, id);
5632 17 May 11 nicklas 94     if (t == null) throw new ItemNotFoundException("Tag[id="+id+"]");
5632 17 May 11 nicklas 95     return t;
5632 17 May 11 nicklas 96   }
5632 17 May 11 nicklas 97
5632 17 May 11 nicklas 98   /**
5632 17 May 11 nicklas 99     Get a query that returns tags.
5632 17 May 11 nicklas 100     @return An {@link ItemQuery} object
5632 17 May 11 nicklas 101   */
5632 17 May 11 nicklas 102   public static ItemQuery<Tag> getQuery()
5632 17 May 11 nicklas 103   {
5632 17 May 11 nicklas 104     return new ItemQuery<Tag>(Tag.class);
5632 17 May 11 nicklas 105   }
5632 17 May 11 nicklas 106
5632 17 May 11 nicklas 107   Tag(TagData data)
5632 17 May 11 nicklas 108   {
5632 17 May 11 nicklas 109     super(data);
5632 17 May 11 nicklas 110   }
5632 17 May 11 nicklas 111
6874 17 Apr 15 nicklas 112   @Override
6874 17 Apr 15 nicklas 113   TagData getData()
6874 17 Apr 15 nicklas 114   {
6874 17 Apr 15 nicklas 115     return (TagData)getBasicData();
6874 17 Apr 15 nicklas 116   }
5632 17 May 11 nicklas 117   /*
5632 17 May 11 nicklas 118     From the Identifiable interface
5632 17 May 11 nicklas 119     -------------------------------------------
5632 17 May 11 nicklas 120   */
6127 14 Sep 12 nicklas 121   @Override
5632 17 May 11 nicklas 122   public Item getType()
5632 17 May 11 nicklas 123   {
5632 17 May 11 nicklas 124     return TYPE;
5632 17 May 11 nicklas 125   }
5632 17 May 11 nicklas 126   // -------------------------------------------
5632 17 May 11 nicklas 127   /*
5632 17 May 11 nicklas 128     From the Registered interface
5632 17 May 11 nicklas 129     -------------------------------------------
5632 17 May 11 nicklas 130   */
6127 14 Sep 12 nicklas 131   @Override
5632 17 May 11 nicklas 132   public Date getEntryDate()
5632 17 May 11 nicklas 133   {
5632 17 May 11 nicklas 134     return DateUtil.copy(getData().getEntryDate());
5632 17 May 11 nicklas 135   }
7308 13 Mar 17 nicklas 136   @Override
7308 13 Mar 17 nicklas 137   public void setEntryDate(Date entryDate)
7308 13 Mar 17 nicklas 138   {
7308 13 Mar 17 nicklas 139     if (isInDatabase()) 
7308 13 Mar 17 nicklas 140     {
7308 13 Mar 17 nicklas 141       throw new PermissionDeniedException("Not allowed to change entryDate of " + this);
7308 13 Mar 17 nicklas 142     }
7308 13 Mar 17 nicklas 143     getData().setEntryDate(DateUtil.setDateOrToday(entryDate));
7308 13 Mar 17 nicklas 144   }
5632 17 May 11 nicklas 145   // -------------------------------------------
5632 17 May 11 nicklas 146   /*
6335 21 Oct 13 olle 147     From the Annotatable interface
6335 21 Oct 13 olle 148     -------------------------------------------
6335 21 Oct 13 olle 149   */
6335 21 Oct 13 olle 150   /**
6335 21 Oct 13 olle 151     @return Always null
6335 21 Oct 13 olle 152     @since 3.3
6335 21 Oct 13 olle 153   */
6335 21 Oct 13 olle 154   @Override
6335 21 Oct 13 olle 155   public Set<Annotatable> getAnnotatableParents()
6335 21 Oct 13 olle 156   {
6335 21 Oct 13 olle 157     return null;
6335 21 Oct 13 olle 158   }
6335 21 Oct 13 olle 159   // -------------------------------------------
6335 21 Oct 13 olle 160   /*
5632 17 May 11 nicklas 161     From the Subtypable interface
5632 17 May 11 nicklas 162     -----------------------------
5632 17 May 11 nicklas 163   */
5632 17 May 11 nicklas 164   @Override
5632 17 May 11 nicklas 165   public ItemSubtype getItemSubtype()
5632 17 May 11 nicklas 166   {
5632 17 May 11 nicklas 167     return getDbControl().getItem(ItemSubtype.class, getData().getItemSubtype());
5632 17 May 11 nicklas 168   }
5632 17 May 11 nicklas 169   @Override
5632 17 May 11 nicklas 170   public void setItemSubtype(ItemSubtype subtype)
5632 17 May 11 nicklas 171   {
5632 17 May 11 nicklas 172     checkPermission(Permission.WRITE);
5632 17 May 11 nicklas 173     if (subtype != null)
5632 17 May 11 nicklas 174     {
5632 17 May 11 nicklas 175       subtype.setOnItem(this);
5632 17 May 11 nicklas 176     }
5632 17 May 11 nicklas 177     else
5632 17 May 11 nicklas 178     {
5632 17 May 11 nicklas 179       getData().setItemSubtype(null);
5632 17 May 11 nicklas 180     }
5632 17 May 11 nicklas 181   }
5632 17 May 11 nicklas 182   // -------------------------------------------
5632 17 May 11 nicklas 183
5632 17 May 11 nicklas 184   /*
5632 17 May 11 nicklas 185     From the BasicItem class
5632 17 May 11 nicklas 186     -------------------------------------------
5632 17 May 11 nicklas 187   */
5632 17 May 11 nicklas 188   /**
5632 17 May 11 nicklas 189     Check if:
5632 17 May 11 nicklas 190     <ul>
5632 17 May 11 nicklas 191     <li>Some {@link Extract}:s are marked with this tag
5632 17 May 11 nicklas 192     </ul>
5632 17 May 11 nicklas 193   */
6127 14 Sep 12 nicklas 194   @Override
5632 17 May 11 nicklas 195   public boolean isUsed()
5632 17 May 11 nicklas 196     throws BaseException
5632 17 May 11 nicklas 197   {
5632 17 May 11 nicklas 198     org.hibernate.Session session = getDbControl().getHibernateSession();
7381 22 May 17 nicklas 199     org.hibernate.query.Query<Long> query = HibernateUtil.getPredefinedQuery(session, 
7381 22 May 17 nicklas 200       "GET_EXTRACTS_FOR_TAG", Long.class, "count(*)");
5632 17 May 11 nicklas 201     /*
5632 17 May 11 nicklas 202       SELECT {1}
5632 17 May 11 nicklas 203       FROM ExtractData ext
5632 17 May 11 nicklas 204       WHERE ext.tag = :tag
5632 17 May 11 nicklas 205     */
7381 22 May 17 nicklas 206     query.setParameter("tag", getId(), TypeWrapper.H_INTEGER);
7381 22 May 17 nicklas 207     boolean used = HibernateUtil.loadData(query) > 0;
5632 17 May 11 nicklas 208     return used || super.isUsed();
5632 17 May 11 nicklas 209   }
5632 17 May 11 nicklas 210   /**
5632 17 May 11 nicklas 211     Get all:
5632 17 May 11 nicklas 212     <ul>
5632 17 May 11 nicklas 213     <li>{@link Extract}:s marked with this tag
6881 21 Apr 15 nicklas 214     </ul>
5632 17 May 11 nicklas 215     @since 2.2
5632 17 May 11 nicklas 216   */
5632 17 May 11 nicklas 217   @Override
5632 17 May 11 nicklas 218   public Set<ItemProxy> getUsingItems()
5632 17 May 11 nicklas 219   {
5632 17 May 11 nicklas 220     Set<ItemProxy> using = super.getUsingItems();
5632 17 May 11 nicklas 221     org.hibernate.Session session = getDbControl().getHibernateSession();
5632 17 May 11 nicklas 222     
5632 17 May 11 nicklas 223     // Labeled extracts
7381 22 May 17 nicklas 224     org.hibernate.query.Query<Integer> query = HibernateUtil.getPredefinedQuery(session, 
7381 22 May 17 nicklas 225         "GET_EXTRACTS_FOR_TAG", Integer.class, "ext.id");
5632 17 May 11 nicklas 226     /*
5632 17 May 11 nicklas 227       SELECT {1}
5632 17 May 11 nicklas 228       FROM ExtractData ext
5632 17 May 11 nicklas 229       WHERE ext.tag = :tag
5632 17 May 11 nicklas 230     */
7381 22 May 17 nicklas 231     query.setParameter("tag", getId(), TypeWrapper.H_INTEGER);
5632 17 May 11 nicklas 232     addUsingItems(using, Item.EXTRACT, query);
5632 17 May 11 nicklas 233     return using;
5632 17 May 11 nicklas 234   }
5632 17 May 11 nicklas 235   // -------------------------------------------
5632 17 May 11 nicklas 236
5641 25 May 11 nicklas 237
5632 17 May 11 nicklas 238 }