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

Code
Comments
Other
Rev Date Author Line
984 21 Jul 05 enell 1 /*
984 21 Jul 05 enell 2   $Id$
984 21 Jul 05 enell 3
3675 16 Aug 07 jari 4   Copyright (C) 2005 Johan Enell, Nicklas Nordborg
4889 06 Apr 09 nicklas 5   Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg
984 21 Jul 05 enell 6
2304 22 May 06 jari 7   This file is part of BASE - BioArray Software Environment.
2304 22 May 06 jari 8   Available at http://base.thep.lu.se/
984 21 Jul 05 enell 9
984 21 Jul 05 enell 10   BASE is free software; you can redistribute it and/or
984 21 Jul 05 enell 11   modify it under the terms of the GNU General Public License
4479 05 Sep 08 jari 12   as published by the Free Software Foundation; either version 3
984 21 Jul 05 enell 13   of the License, or (at your option) any later version.
984 21 Jul 05 enell 14
984 21 Jul 05 enell 15   BASE is distributed in the hope that it will be useful,
984 21 Jul 05 enell 16   but WITHOUT ANY WARRANTY; without even the implied warranty of
984 21 Jul 05 enell 17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
984 21 Jul 05 enell 18   GNU General Public License for more details.
984 21 Jul 05 enell 19
984 21 Jul 05 enell 20   You should have received a copy of the GNU General Public License
4517 11 Sep 08 jari 21   along with BASE. If not, see <http://www.gnu.org/licenses/>.
984 21 Jul 05 enell 22 */
982 21 Jul 05 enell 23 package net.sf.basedb.core;
982 21 Jul 05 enell 24
982 21 Jul 05 enell 25 import net.sf.basedb.core.data.ArraySlideData;
5642 26 May 11 nicklas 26 import net.sf.basedb.core.data.PhysicalBioAssayData;
7381 22 May 17 nicklas 27 import net.sf.basedb.core.hibernate.TypeWrapper;
1147 30 Aug 05 nicklas 28
4696 09 Dec 08 nicklas 29 import java.util.Date;
1147 30 Aug 05 nicklas 30 import java.util.Set;
1147 30 Aug 05 nicklas 31 import java.util.HashSet;
1147 30 Aug 05 nicklas 32
984 21 Jul 05 enell 33 /**
1147 30 Aug 05 nicklas 34   This class represents a physical microarray slide.
984 21 Jul 05 enell 35   
984 21 Jul 05 enell 36    @author Nicklas, Enell
984 21 Jul 05 enell 37    @version 2.0
984 21 Jul 05 enell 38 */
1136 25 Aug 05 nicklas 39 public class ArraySlide 
6874 17 Apr 15 nicklas 40   extends AnnotatedItem
4696 09 Dec 08 nicklas 41   implements Registered
982 21 Jul 05 enell 42 {
982 21 Jul 05 enell 43   /**
984 21 Jul 05 enell 44     The type of item represented by this class.
984 21 Jul 05 enell 45     
984 21 Jul 05 enell 46     @see Item#ARRAYSLIDE
984 21 Jul 05 enell 47     @see #getType()
984 21 Jul 05 enell 48   */
982 21 Jul 05 enell 49   public static final Item TYPE = Item.ARRAYSLIDE;
982 21 Jul 05 enell 50
982 21 Jul 05 enell 51   /**
982 21 Jul 05 enell 52     The maximum length of the barcode that can be stored in the
982 21 Jul 05 enell 53     database. Check the length against this value before calling the
982 21 Jul 05 enell 54     {@link #setBarcode(String) setBarcode} method to avoid exceptions.
982 21 Jul 05 enell 55   */
982 21 Jul 05 enell 56   public static final int MAX_BARCODE_LENGTH = ArraySlideData.MAX_BARCODE_LENGTH;
982 21 Jul 05 enell 57   
982 21 Jul 05 enell 58   /**
982 21 Jul 05 enell 59     Create a new <code>ArraySlide</code> item.
982 21 Jul 05 enell 60
982 21 Jul 05 enell 61     @param dc The <code>DbControl</code> which will be used for
982 21 Jul 05 enell 62       permission checking and database access
982 21 Jul 05 enell 63     @param arrayBatch The <code>ArrayBatch</code> this slide belongs to
982 21 Jul 05 enell 64     @return The new <code>ArraySlide</code> item
982 21 Jul 05 enell 65     @throws PermissionDeniedException If the logged in user doesn't
1136 25 Aug 05 nicklas 66       have use permission for the array batch
982 21 Jul 05 enell 67     @throws BaseException If there is another error
982 21 Jul 05 enell 68   */
982 21 Jul 05 enell 69   public static ArraySlide getNew(DbControl dc, ArrayBatch arrayBatch)
982 21 Jul 05 enell 70     throws PermissionDeniedException, BaseException
982 21 Jul 05 enell 71   {
982 21 Jul 05 enell 72     ArraySlide as = dc.newItem(ArraySlide.class);
982 21 Jul 05 enell 73     as.setName("New array slide");
982 21 Jul 05 enell 74     as.setArrayBatch(arrayBatch);
4696 09 Dec 08 nicklas 75     as.getData().setEntryDate(new Date());
982 21 Jul 05 enell 76     return as;
982 21 Jul 05 enell 77   }
982 21 Jul 05 enell 78
982 21 Jul 05 enell 79   /**
982 21 Jul 05 enell 80     Get a <code>ArraySlide</code> object when you know the ID.
982 21 Jul 05 enell 81
982 21 Jul 05 enell 82     @param dc The <code>DbControl</code> which will be used for
982 21 Jul 05 enell 83       permission checking and database access.
982 21 Jul 05 enell 84     @param id The ID of the item to load
982 21 Jul 05 enell 85     @return The <code>ArraySlide</code> item
982 21 Jul 05 enell 86     @throws ItemNotFoundException If an item with the specified ID is
982 21 Jul 05 enell 87       not found
982 21 Jul 05 enell 88     @throws PermissionDeniedException If the logged in user doesn't
1136 25 Aug 05 nicklas 89       have read permission to the item
982 21 Jul 05 enell 90     @throws BaseException If there is another error
982 21 Jul 05 enell 91   */
982 21 Jul 05 enell 92   public static ArraySlide getById(DbControl dc, int id)
982 21 Jul 05 enell 93     throws ItemNotFoundException, PermissionDeniedException, BaseException
982 21 Jul 05 enell 94   {
982 21 Jul 05 enell 95     ArraySlide as = dc.loadItem(ArraySlide.class, id);
982 21 Jul 05 enell 96     if (as == null) throw new ItemNotFoundException("ArraySlide[id=" + id + "]");
982 21 Jul 05 enell 97     return as;
982 21 Jul 05 enell 98   }
982 21 Jul 05 enell 99   
982 21 Jul 05 enell 100   /**
4220 14 Apr 08 nicklas 101     Checks if a slide with the given barcode already exists in the database. The
4220 14 Apr 08 nicklas 102     check is done on ALL slides, even on those not normally accessible by the
4220 14 Apr 08 nicklas 103     logged in user.
4220 14 Apr 08 nicklas 104     
4220 14 Apr 08 nicklas 105     @param dc The DbControl to use for database access
4220 14 Apr 08 nicklas 106     @param barcode The barcode to check
4220 14 Apr 08 nicklas 107     @return TRUE if the barcode exists, FALSE if not or if the argument is NULL
4220 14 Apr 08 nicklas 108     @since 2.7
4220 14 Apr 08 nicklas 109   */
4220 14 Apr 08 nicklas 110   public static boolean barcodeExists(DbControl dc, String barcode)
4220 14 Apr 08 nicklas 111   {
4220 14 Apr 08 nicklas 112     if (barcode == null) return false;
4220 14 Apr 08 nicklas 113     barcode = barcode.trim();
4220 14 Apr 08 nicklas 114     org.hibernate.Session session = dc.getHibernateSession();
7381 22 May 17 nicklas 115     org.hibernate.query.Query<Long> query = HibernateUtil.getPredefinedQuery(session, 
7381 22 May 17 nicklas 116       "GET_ARRAYSLIDES_WITH_BARCODE", Long.class, "count(*)");
4220 14 Apr 08 nicklas 117     /*
4220 14 Apr 08 nicklas 118       SELECT count(*)
4220 14 Apr 08 nicklas 119       FROM ArraySlideData ars
4220 14 Apr 08 nicklas 120       WHERE ars.barcode = :barcode
4220 14 Apr 08 nicklas 121     */
7381 22 May 17 nicklas 122     query.setParameter("barcode", barcode, TypeWrapper.H_STRING);
7381 22 May 17 nicklas 123     return HibernateUtil.loadData(query) > 0;
4220 14 Apr 08 nicklas 124   }
4220 14 Apr 08 nicklas 125   
4220 14 Apr 08 nicklas 126   /**
1418 07 Oct 05 nicklas 127     Get a {@link ItemQuery} object configured
1418 07 Oct 05 nicklas 128     to retrieve array slides.
984 21 Jul 05 enell 129     
1418 07 Oct 05 nicklas 130     @return An {@link ItemQuery} object
982 21 Jul 05 enell 131   */
1418 07 Oct 05 nicklas 132   public static ItemQuery<ArraySlide> getQuery() 
982 21 Jul 05 enell 133   {
1418 07 Oct 05 nicklas 134     return new ItemQuery<ArraySlide>(ArraySlide.class);
982 21 Jul 05 enell 135   }
1418 07 Oct 05 nicklas 136
982 21 Jul 05 enell 137   ArraySlide(ArraySlideData data)
982 21 Jul 05 enell 138   {
982 21 Jul 05 enell 139     super(data);
982 21 Jul 05 enell 140   }
6874 17 Apr 15 nicklas 141   
6874 17 Apr 15 nicklas 142   @Override
6874 17 Apr 15 nicklas 143   ArraySlideData getData()
6874 17 Apr 15 nicklas 144   {
6874 17 Apr 15 nicklas 145     return (ArraySlideData)getBasicData();
6874 17 Apr 15 nicklas 146   }
982 21 Jul 05 enell 147
984 21 Jul 05 enell 148   /*
984 21 Jul 05 enell 149     From the Identifiable interface
984 21 Jul 05 enell 150     -------------------------------------------
984 21 Jul 05 enell 151    */
6127 14 Sep 12 nicklas 152   @Override
984 21 Jul 05 enell 153   public Item getType()
982 21 Jul 05 enell 154   {
984 21 Jul 05 enell 155     return TYPE;
982 21 Jul 05 enell 156   }
984 21 Jul 05 enell 157   // -------------------------------------------
1147 30 Aug 05 nicklas 158   /*
1147 30 Aug 05 nicklas 159     From the Annotatable interface
1147 30 Aug 05 nicklas 160     -------------------------------------------
1147 30 Aug 05 nicklas 161   */
1147 30 Aug 05 nicklas 162   /**
1147 30 Aug 05 nicklas 163     Get the array batch.
1147 30 Aug 05 nicklas 164   */
6127 14 Sep 12 nicklas 165   @Override
1147 30 Aug 05 nicklas 166   public Set<Annotatable> getAnnotatableParents()
1147 30 Aug 05 nicklas 167     throws BaseException
1147 30 Aug 05 nicklas 168   {
1147 30 Aug 05 nicklas 169     Set<Annotatable> annotatable = new HashSet<Annotatable>();
7002 05 Nov 15 nicklas 170     ArraySlideData data = getData();
7002 05 Nov 15 nicklas 171     
7002 05 Nov 15 nicklas 172     addAnnotatableParents(getDbControl(), annotatable, 
7002 05 Nov 15 nicklas 173       data.getArrayBatch()
7002 05 Nov 15 nicklas 174     );
1147 30 Aug 05 nicklas 175     return annotatable;
1147 30 Aug 05 nicklas 176   }
1147 30 Aug 05 nicklas 177   // -------------------------------------------
4696 09 Dec 08 nicklas 178   /*
4696 09 Dec 08 nicklas 179     From the Registered interface
4696 09 Dec 08 nicklas 180     -------------------------------------------
4696 09 Dec 08 nicklas 181   */
6127 14 Sep 12 nicklas 182   @Override
4696 09 Dec 08 nicklas 183   public Date getEntryDate()
4696 09 Dec 08 nicklas 184   {
4696 09 Dec 08 nicklas 185     return DateUtil.copy(getData().getEntryDate());
4696 09 Dec 08 nicklas 186   }
7308 13 Mar 17 nicklas 187   @Override
7308 13 Mar 17 nicklas 188   public void setEntryDate(Date entryDate)
7308 13 Mar 17 nicklas 189   {
7308 13 Mar 17 nicklas 190     if (isInDatabase()) 
7308 13 Mar 17 nicklas 191     {
7308 13 Mar 17 nicklas 192       throw new PermissionDeniedException("Not allowed to change entryDate of " + this);
7308 13 Mar 17 nicklas 193     }
7308 13 Mar 17 nicklas 194     getData().setEntryDate(DateUtil.setDateOrToday(entryDate));
7308 13 Mar 17 nicklas 195   }
4696 09 Dec 08 nicklas 196   // -------------------------------------------
982 21 Jul 05 enell 197
984 21 Jul 05 enell 198   /*
984 21 Jul 05 enell 199     From the BasicItem class
984 21 Jul 05 enell 200     -------------------------------------------
984 21 Jul 05 enell 201   */
984 21 Jul 05 enell 202   @Override
1102 22 Aug 05 nicklas 203   public boolean isUsed() 
1102 22 Aug 05 nicklas 204     throws BaseException
982 21 Jul 05 enell 205   {
5642 26 May 11 nicklas 206     return getData().getPhysicalBioAssay() != null || super.isUsed();
982 21 Jul 05 enell 207   }
2898 13 Nov 06 nicklas 208   /**
5642 26 May 11 nicklas 209     Get the bioassay that was used on this plate.
2898 13 Nov 06 nicklas 210     @since 2.2
2898 13 Nov 06 nicklas 211   */
2898 13 Nov 06 nicklas 212   @Override
2898 13 Nov 06 nicklas 213   public Set<ItemProxy> getUsingItems()
2898 13 Nov 06 nicklas 214     throws BaseException
2898 13 Nov 06 nicklas 215   {
2898 13 Nov 06 nicklas 216     Set<ItemProxy> using = super.getUsingItems();
5642 26 May 11 nicklas 217     PhysicalBioAssay item = getPhysicalBioAssay();
2898 13 Nov 06 nicklas 218     if (item != null) using.add(new ItemProxy(item.getId(), item.getType()));
2898 13 Nov 06 nicklas 219     return using;
2898 13 Nov 06 nicklas 220   }
984 21 Jul 05 enell 221   // -------------------------------------------
982 21 Jul 05 enell 222
982 21 Jul 05 enell 223   /**
982 21 Jul 05 enell 224     Get the index of this slide in the batch.
982 21 Jul 05 enell 225   
982 21 Jul 05 enell 226     @return The index of the slide
982 21 Jul 05 enell 227   */
982 21 Jul 05 enell 228   public int getBatchIndex()
982 21 Jul 05 enell 229   {
982 21 Jul 05 enell 230     return getData().getBatchIndex();
982 21 Jul 05 enell 231   }
982 21 Jul 05 enell 232
982 21 Jul 05 enell 233   /**
982 21 Jul 05 enell 234     Set the index of this slide in the batch.
984 21 Jul 05 enell 235     
982 21 Jul 05 enell 236     @param batchIndex The index of the slide in the batch
982 21 Jul 05 enell 237     @throws PermissionDeniedException This exception is thrown if the
1102 22 Aug 05 nicklas 238       logged in user doesn't have write
982 21 Jul 05 enell 239       permission.
982 21 Jul 05 enell 240   */
1102 22 Aug 05 nicklas 241   public void setBatchIndex(int batchIndex) 
1102 22 Aug 05 nicklas 242     throws PermissionDeniedException
982 21 Jul 05 enell 243   {
982 21 Jul 05 enell 244     checkPermission(Permission.WRITE);
982 21 Jul 05 enell 245     getData().setBatchIndex(batchIndex);
982 21 Jul 05 enell 246   }
982 21 Jul 05 enell 247   
982 21 Jul 05 enell 248   /**
982 21 Jul 05 enell 249     Get the barcode of the slide.
1136 25 Aug 05 nicklas 250     @return A string with the barcode sequence
982 21 Jul 05 enell 251   */
982 21 Jul 05 enell 252   public String getBarcode()
982 21 Jul 05 enell 253   {
982 21 Jul 05 enell 254     return getData().getBarcode();
982 21 Jul 05 enell 255   }
982 21 Jul 05 enell 256
982 21 Jul 05 enell 257   /**
982 21 Jul 05 enell 258     Set the barcode for the slide. The value
982 21 Jul 05 enell 259     can be null and must not be longer than the value specified by
1102 22 Aug 05 nicklas 260     the {@link #MAX_BARCODE_LENGTH} constant.
984 21 Jul 05 enell 261     
982 21 Jul 05 enell 262     @param barcode The new barcode for the slide
1136 25 Aug 05 nicklas 263     @throws PermissionDeniedException If the logged in user doesn't 
1136 25 Aug 05 nicklas 264       have write permission
982 21 Jul 05 enell 265     @throws InvalidDataException This exception is thrown if the barcode
1102 22 Aug 05 nicklas 266       is longer than {@link #MAX_BARCODE_LENGTH}
982 21 Jul 05 enell 267   */
1102 22 Aug 05 nicklas 268   public void setBarcode(String barcode) 
1102 22 Aug 05 nicklas 269     throws PermissionDeniedException, InvalidDataException
982 21 Jul 05 enell 270   {
982 21 Jul 05 enell 271     checkPermission(Permission.WRITE);
1102 22 Aug 05 nicklas 272     getData().setBarcode(StringUtil.setNullableString(barcode, "Barcode", MAX_BARCODE_LENGTH));
982 21 Jul 05 enell 273   }
982 21 Jul 05 enell 274   
982 21 Jul 05 enell 275   /**
982 21 Jul 05 enell 276     Check if the slide is flagged as destroyed or not. A
982 21 Jul 05 enell 277     destroyed slide may not be connected to a hybridization.
982 21 Jul 05 enell 278
982 21 Jul 05 enell 279     @return TRUE if the slide is destroyed, FALSE otherwise
982 21 Jul 05 enell 280   */
982 21 Jul 05 enell 281   public boolean isDestroyed()
982 21 Jul 05 enell 282   {
982 21 Jul 05 enell 283     return getData().isDestroyed();
982 21 Jul 05 enell 284   }
982 21 Jul 05 enell 285
982 21 Jul 05 enell 286   /**
982 21 Jul 05 enell 287     Set if the slide should be flagged as destroyed. A
982 21 Jul 05 enell 288     destroyed slide may not be connected to a hybridization.
984 21 Jul 05 enell 289     
982 21 Jul 05 enell 290     @param destroyed true if the slide should be flagged as destroyed,
982 21 Jul 05 enell 291       false otherwise
1136 25 Aug 05 nicklas 292     @throws PermissionDeniedException If the logged in user doesn't 
1136 25 Aug 05 nicklas 293       have write permission
982 21 Jul 05 enell 294   */
1102 22 Aug 05 nicklas 295   public void setDestroyed(boolean destroyed) 
1102 22 Aug 05 nicklas 296     throws PermissionDeniedException
982 21 Jul 05 enell 297   {
982 21 Jul 05 enell 298     checkPermission(Permission.WRITE);
982 21 Jul 05 enell 299     getData().setDestroyed(destroyed);
982 21 Jul 05 enell 300   }
982 21 Jul 05 enell 301   
982 21 Jul 05 enell 302   /**
982 21 Jul 05 enell 303     Get the {@link ArrayBatch} item to which this slide belongs.
984 21 Jul 05 enell 304     
982 21 Jul 05 enell 305     @return The <code>ArrayBatch</code> item
1136 25 Aug 05 nicklas 306     @throws PermissionDeniedException If the logged in user doesn't have read 
1136 25 Aug 05 nicklas 307       permission for the array batch
982 21 Jul 05 enell 308     @throws BaseException If there is another error
982 21 Jul 05 enell 309   */
982 21 Jul 05 enell 310   public ArrayBatch getArrayBatch()
982 21 Jul 05 enell 311     throws PermissionDeniedException, BaseException
982 21 Jul 05 enell 312   {
982 21 Jul 05 enell 313     return getDbControl().getItem(ArrayBatch.class, getData().getArrayBatch());
982 21 Jul 05 enell 314   }
982 21 Jul 05 enell 315   
982 21 Jul 05 enell 316   /**
984 21 Jul 05 enell 317     Set the {@link ArrayBatch} this arrayslide belongs to.
984 21 Jul 05 enell 318     
984 21 Jul 05 enell 319     @param arrayBatch the arrayBatch to be set. Cannot be null.
1102 22 Aug 05 nicklas 320     @throws InvalidDataException If the arrayBatch is null
1136 25 Aug 05 nicklas 321     @throws PermissionDeniedException If the logged in user doesn't have 
1136 25 Aug 05 nicklas 322       use permission for the batch
984 21 Jul 05 enell 323   */
984 21 Jul 05 enell 324   private void setArrayBatch(ArrayBatch arrayBatch) 
984 21 Jul 05 enell 325     throws InvalidDataException, PermissionDeniedException
982 21 Jul 05 enell 326   {
1102 22 Aug 05 nicklas 327     if (arrayBatch == null) throw new InvalidUseOfNullException("arrayBatch");
982 21 Jul 05 enell 328     arrayBatch.checkPermission(Permission.USE);
982 21 Jul 05 enell 329     getData().setArrayBatch(arrayBatch.getData());
982 21 Jul 05 enell 330   }
984 21 Jul 05 enell 331   
984 21 Jul 05 enell 332   /**
5642 26 May 11 nicklas 333     Get the {@link PhysicalBioAssay} item which was used on this slide. To set, 
5642 26 May 11 nicklas 334     use the {@link PhysicalBioAssay#setArraySlide(ArraySlide)} method.
984 21 Jul 05 enell 335
5642 26 May 11 nicklas 336     @return The <code>PhysicalBioAssay</code> item, or null
984 21 Jul 05 enell 337     @throws PermissionDeniedException This exception is thrown if
1102 22 Aug 05 nicklas 338       the logged in user doesn't have read permission to the item
984 21 Jul 05 enell 339     @throws BaseException If there is another error
5642 26 May 11 nicklas 340     @since 3.0
984 21 Jul 05 enell 341   */
5642 26 May 11 nicklas 342   public PhysicalBioAssay getPhysicalBioAssay()
984 21 Jul 05 enell 343     throws PermissionDeniedException, BaseException
984 21 Jul 05 enell 344   {
5642 26 May 11 nicklas 345     return getDbControl().getItem(PhysicalBioAssay.class, getData().getPhysicalBioAssay());
984 21 Jul 05 enell 346   }
4585 15 Oct 08 nicklas 347   
4585 15 Oct 08 nicklas 348   /**
5642 26 May 11 nicklas 349     Checks if this array slide is used by a different bioassay
4585 15 Oct 08 nicklas 350     than the given one.
5642 26 May 11 nicklas 351     @param pba The bioassay to check, or null to check if the slide is used
5642 26 May 11 nicklas 352       by any bioassay
5642 26 May 11 nicklas 353     @return TRUE if the slide is used by a different bioassay, or
5642 26 May 11 nicklas 354       FALSE if it is used by the given bioassay, or not used at all
4585 15 Oct 08 nicklas 355     @since 2.8.3
4585 15 Oct 08 nicklas 356   */
5642 26 May 11 nicklas 357   public boolean isUsedByOther(PhysicalBioAssay pba)
4585 15 Oct 08 nicklas 358   {
5642 26 May 11 nicklas 359     PhysicalBioAssayData usedBy = getData().getPhysicalBioAssay();
4585 15 Oct 08 nicklas 360     if (usedBy == null) return false;
5642 26 May 11 nicklas 361     if (pba == null) return true;
5642 26 May 11 nicklas 362     return !usedBy.equals(pba.getData());
4585 15 Oct 08 nicklas 363   }
4585 15 Oct 08 nicklas 364   
982 21 Jul 05 enell 365 }