src/core/net/sf/basedb/util/annotations/InheritSpecification.java

Code
Comments
Other
Rev Date Author Line
6694 26 Jan 15 nicklas 1 /**
6694 26 Jan 15 nicklas 2   $Id $
6694 26 Jan 15 nicklas 3
6694 26 Jan 15 nicklas 4   Copyright (C) 2015 Nicklas Nordborg
6694 26 Jan 15 nicklas 5
6694 26 Jan 15 nicklas 6   This file is part of BASE - BioArray Software Environment.
6694 26 Jan 15 nicklas 7   Available at http://base.thep.lu.se/
6694 26 Jan 15 nicklas 8
6694 26 Jan 15 nicklas 9   BASE is free software; you can redistribute it and/or
6694 26 Jan 15 nicklas 10   modify it under the terms of the GNU General Public License
6694 26 Jan 15 nicklas 11   as published by the Free Software Foundation; either version 3
6694 26 Jan 15 nicklas 12   of the License, or (at your option) any later version.
6694 26 Jan 15 nicklas 13
6694 26 Jan 15 nicklas 14   BASE is distributed in the hope that it will be useful,
6694 26 Jan 15 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
6694 26 Jan 15 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6694 26 Jan 15 nicklas 17   GNU General Public License for more details.
6694 26 Jan 15 nicklas 18
6694 26 Jan 15 nicklas 19   You should have received a copy of the GNU General Public License
6694 26 Jan 15 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
6694 26 Jan 15 nicklas 21 */
6694 26 Jan 15 nicklas 22 package net.sf.basedb.util.annotations;
6694 26 Jan 15 nicklas 23
6694 26 Jan 15 nicklas 24 import net.sf.basedb.core.Annotatable;
6694 26 Jan 15 nicklas 25 import net.sf.basedb.core.AnnotationType;
6694 26 Jan 15 nicklas 26 import net.sf.basedb.core.BioPlate;
6694 26 Jan 15 nicklas 27 import net.sf.basedb.core.BioPlateType;
6694 26 Jan 15 nicklas 28 import net.sf.basedb.core.ItemSubtype;
6694 26 Jan 15 nicklas 29 import net.sf.basedb.core.Subtypable;
6694 26 Jan 15 nicklas 30 import net.sf.basedb.core.snapshot.AnnotationSnapshot;
6694 26 Jan 15 nicklas 31 import net.sf.basedb.util.filter.Filter;
6694 26 Jan 15 nicklas 32
6694 26 Jan 15 nicklas 33 /**
6694 26 Jan 15 nicklas 34   Class for holding rules for how annotations should be inherited from
6694 26 Jan 15 nicklas 35   parent items. If {@link #getRemove()} is set, it override all other
6694 26 Jan 15 nicklas 36   settings and all inherited annotation for the specified annotation type
6694 26 Jan 15 nicklas 37   are removed.
6694 26 Jan 15 nicklas 38   
6694 26 Jan 15 nicklas 39   @author nicklas
6694 26 Jan 15 nicklas 40   @since 3.5
6694 26 Jan 15 nicklas 41 */
6694 26 Jan 15 nicklas 42 public class InheritSpecification 
6694 26 Jan 15 nicklas 43   implements Filter<AnnotationSnapshot>
6694 26 Jan 15 nicklas 44 {
6694 26 Jan 15 nicklas 45
6694 26 Jan 15 nicklas 46   private final int annotationTypeId;
6694 26 Jan 15 nicklas 47   private final AnnotationType annotationType;
6694 26 Jan 15 nicklas 48
6694 26 Jan 15 nicklas 49   private boolean remove;
6925 03 Jun 15 nicklas 50   private boolean clone;
6926 03 Jun 15 nicklas 51   private boolean resync;
6694 26 Jan 15 nicklas 52   private ItemSubtype subtype;
6694 26 Jan 15 nicklas 53   private boolean noDuplicates = true;
6694 26 Jan 15 nicklas 54   private boolean replaceExisting = false;
6694 26 Jan 15 nicklas 55   
6694 26 Jan 15 nicklas 56   /**
6694 26 Jan 15 nicklas 57     Creates a new specification for the given annotation type.
6694 26 Jan 15 nicklas 58   */
6694 26 Jan 15 nicklas 59   public InheritSpecification(AnnotationType annotationType)
6694 26 Jan 15 nicklas 60   {
6694 26 Jan 15 nicklas 61     this.annotationTypeId = annotationType.getId();
6694 26 Jan 15 nicklas 62     this.annotationType = annotationType;
6694 26 Jan 15 nicklas 63   }
6694 26 Jan 15 nicklas 64   
6694 26 Jan 15 nicklas 65   public AnnotationType getAnnotationType()
6694 26 Jan 15 nicklas 66   {
6694 26 Jan 15 nicklas 67     return annotationType;
6694 26 Jan 15 nicklas 68   }
6694 26 Jan 15 nicklas 69   
6694 26 Jan 15 nicklas 70   /**
6694 26 Jan 15 nicklas 71     Set a flag to remove all inherited annotations of the specified annotation type.
6694 26 Jan 15 nicklas 72     Note that if this setting is enabled, it override all other settings.
6694 26 Jan 15 nicklas 73   */
6694 26 Jan 15 nicklas 74   public void setRemove(boolean remove)
6694 26 Jan 15 nicklas 75   {
6694 26 Jan 15 nicklas 76     this.remove = remove;
6694 26 Jan 15 nicklas 77   }
6694 26 Jan 15 nicklas 78   
6694 26 Jan 15 nicklas 79   public boolean getRemove()
6694 26 Jan 15 nicklas 80   {
6694 26 Jan 15 nicklas 81     return remove;
6694 26 Jan 15 nicklas 82   }
6694 26 Jan 15 nicklas 83
6694 26 Jan 15 nicklas 84   /**
6925 03 Jun 15 nicklas 85     Set a flag to inherit annotations by cloning the original values. Use this
6925 03 Jun 15 nicklas 86     if you do not want changes to the original annotation to be reflected in
6925 03 Jun 15 nicklas 87     the inherited (=cloned) annotations.
6925 03 Jun 15 nicklas 88     @since 3.6
6925 03 Jun 15 nicklas 89   */
6925 03 Jun 15 nicklas 90   public void setClone(boolean clone)
6925 03 Jun 15 nicklas 91   {
6925 03 Jun 15 nicklas 92     this.clone = clone;
6925 03 Jun 15 nicklas 93   }
6925 03 Jun 15 nicklas 94   
6925 03 Jun 15 nicklas 95   public boolean getClone()
6925 03 Jun 15 nicklas 96   {
6925 03 Jun 15 nicklas 97     return clone;
6925 03 Jun 15 nicklas 98   }
6925 03 Jun 15 nicklas 99   
6926 03 Jun 15 nicklas 100   public void setResyncCloned(boolean resync)
6926 03 Jun 15 nicklas 101   {
6926 03 Jun 15 nicklas 102     this.resync = resync;
6926 03 Jun 15 nicklas 103   }
6926 03 Jun 15 nicklas 104   
6926 03 Jun 15 nicklas 105   public boolean getResync()
6926 03 Jun 15 nicklas 106   {
6926 03 Jun 15 nicklas 107     return resync;
6926 03 Jun 15 nicklas 108   }
6926 03 Jun 15 nicklas 109   
6925 03 Jun 15 nicklas 110   /**
6694 26 Jan 15 nicklas 111     Set the subtype a parent item must have if annotations should be 
6694 26 Jan 15 nicklas 112     inherited from it. Set to null to inherit from any parent.
6694 26 Jan 15 nicklas 113   */
6694 26 Jan 15 nicklas 114   public void setItemSubtype(ItemSubtype subtype)
6694 26 Jan 15 nicklas 115   {
6694 26 Jan 15 nicklas 116     this.subtype = subtype;
6694 26 Jan 15 nicklas 117   }
6694 26 Jan 15 nicklas 118   
6694 26 Jan 15 nicklas 119   public ItemSubtype getItemSubtype()
6694 26 Jan 15 nicklas 120   {
6694 26 Jan 15 nicklas 121     return subtype;
6694 26 Jan 15 nicklas 122   }
6694 26 Jan 15 nicklas 123   
6694 26 Jan 15 nicklas 124   /**
6694 26 Jan 15 nicklas 125     If this flag is set (default=true), only one annotation of the specified 
6694 26 Jan 15 nicklas 126     annotation type can be inherited.
6694 26 Jan 15 nicklas 127   */
6694 26 Jan 15 nicklas 128   public void setNoDuplicates(boolean noDuplicates)
6694 26 Jan 15 nicklas 129   {
6694 26 Jan 15 nicklas 130     this.noDuplicates = noDuplicates;
6694 26 Jan 15 nicklas 131   }
6694 26 Jan 15 nicklas 132   
6694 26 Jan 15 nicklas 133   public boolean getNoDuplicates()
6694 26 Jan 15 nicklas 134   {
6694 26 Jan 15 nicklas 135     return noDuplicates;
6694 26 Jan 15 nicklas 136   }
6694 26 Jan 15 nicklas 137   
6694 26 Jan 15 nicklas 138   /**
6694 26 Jan 15 nicklas 139     If this flag is set (default=false), existing inherited annotations
6694 26 Jan 15 nicklas 140     of the specified annotation type are removed before inheriting new 
6694 26 Jan 15 nicklas 141     ones. Note that existing annotations are only replaced if a new annotation
6694 26 Jan 15 nicklas 142     is found.
6694 26 Jan 15 nicklas 143   */
6694 26 Jan 15 nicklas 144   public void setReplaceExisting(boolean replaceExisting)
6694 26 Jan 15 nicklas 145   {
6694 26 Jan 15 nicklas 146     this.replaceExisting = replaceExisting;
6694 26 Jan 15 nicklas 147   }
6694 26 Jan 15 nicklas 148   
6694 26 Jan 15 nicklas 149   public boolean getReplaceExisting()
6694 26 Jan 15 nicklas 150   {
6694 26 Jan 15 nicklas 151     return replaceExisting;
6694 26 Jan 15 nicklas 152   }
6694 26 Jan 15 nicklas 153
6694 26 Jan 15 nicklas 154   /**
6694 26 Jan 15 nicklas 155     Checks if the parent item has a subtype matching the subtype (if any)
6694 26 Jan 15 nicklas 156     in this specification. If the parent item is a {@link BioPlate} we
6880 21 Apr 15 nicklas 157     check the subtype of bioplate type ({@link BioPlateType#getItemSubtype()})
6694 26 Jan 15 nicklas 158     @return TRUE if the subtype matches or if this specification doesn't set any subtype
6694 26 Jan 15 nicklas 159   */
6694 26 Jan 15 nicklas 160   boolean isAcceptableParent(Annotatable parent)
6694 26 Jan 15 nicklas 161   {
6694 26 Jan 15 nicklas 162     if (subtype == null) return true;
6694 26 Jan 15 nicklas 163     ItemSubtype subtypeToCheck = null;
6694 26 Jan 15 nicklas 164     if (parent instanceof Subtypable)
6694 26 Jan 15 nicklas 165     {
6694 26 Jan 15 nicklas 166       subtypeToCheck = ((Subtypable)parent).getItemSubtype();
6694 26 Jan 15 nicklas 167     }
6694 26 Jan 15 nicklas 168     else if (parent instanceof BioPlate)
6694 26 Jan 15 nicklas 169     {
6694 26 Jan 15 nicklas 170       BioPlateType plateType = ((BioPlate)parent).getBioPlateType();
6694 26 Jan 15 nicklas 171       subtypeToCheck = plateType != null ? plateType.getItemSubtype() : null;
6694 26 Jan 15 nicklas 172     }
6694 26 Jan 15 nicklas 173     return subtype.equals(subtypeToCheck);
6694 26 Jan 15 nicklas 174   }
6694 26 Jan 15 nicklas 175   
6694 26 Jan 15 nicklas 176   
6694 26 Jan 15 nicklas 177   @Override
6694 26 Jan 15 nicklas 178   public boolean evaluate(AnnotationSnapshot snapshot) 
6694 26 Jan 15 nicklas 179   {
6694 26 Jan 15 nicklas 180     return snapshot.getAnnotationTypeId() == annotationTypeId;
6694 26 Jan 15 nicklas 181   }
6694 26 Jan 15 nicklas 182
6694 26 Jan 15 nicklas 183
6694 26 Jan 15 nicklas 184 }