src/core/net/sf/basedb/util/overview/validator/BasicItemNodeValidatorFactory.java

Code
Comments
Other
Rev Date Author Line
4740 05 Feb 09 nicklas 1 /**
4740 05 Feb 09 nicklas 2   $Id$
4740 05 Feb 09 nicklas 3
4740 05 Feb 09 nicklas 4   Copyright (C) 2008 Nicklas Nordborg
4740 05 Feb 09 nicklas 5
4740 05 Feb 09 nicklas 6   This file is part of BASE - BioArray Software Environment.
4740 05 Feb 09 nicklas 7   Available at http://base.thep.lu.se/
4740 05 Feb 09 nicklas 8
4740 05 Feb 09 nicklas 9   BASE is free software; you can redistribute it and/or
4740 05 Feb 09 nicklas 10   modify it under the terms of the GNU General Public License
4740 05 Feb 09 nicklas 11   as published by the Free Software Foundation; either version 3
4740 05 Feb 09 nicklas 12   of the License, or (at your option) any later version.
4740 05 Feb 09 nicklas 13
4740 05 Feb 09 nicklas 14   BASE is distributed in the hope that it will be useful,
4740 05 Feb 09 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
4740 05 Feb 09 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4740 05 Feb 09 nicklas 17   GNU General Public License for more details.
4740 05 Feb 09 nicklas 18
4740 05 Feb 09 nicklas 19   You should have received a copy of the GNU General Public License
4740 05 Feb 09 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
4740 05 Feb 09 nicklas 21 */
4740 05 Feb 09 nicklas 22 package net.sf.basedb.util.overview.validator;
4740 05 Feb 09 nicklas 23
7513 02 Nov 18 nicklas 24 import java.lang.reflect.InvocationTargetException;
4740 05 Feb 09 nicklas 25 import java.util.HashMap;
4740 05 Feb 09 nicklas 26 import java.util.Map;
4740 05 Feb 09 nicklas 27
4740 05 Feb 09 nicklas 28 import net.sf.basedb.core.BaseException;
4740 05 Feb 09 nicklas 29 import net.sf.basedb.core.BasicItem;
4740 05 Feb 09 nicklas 30 import net.sf.basedb.core.BioSource;
4740 05 Feb 09 nicklas 31 import net.sf.basedb.core.Experiment;
4740 05 Feb 09 nicklas 32 import net.sf.basedb.core.Item;
4740 05 Feb 09 nicklas 33 import net.sf.basedb.core.ItemNotFoundException;
4740 05 Feb 09 nicklas 34 import net.sf.basedb.util.ClassUtil;
4740 05 Feb 09 nicklas 35
4740 05 Feb 09 nicklas 36 /**
4740 05 Feb 09 nicklas 37   Node validator factory implementation for {@link BasicItem} node validators.
4740 05 Feb 09 nicklas 38   The key used in {@link #createNodeValidator(Object)} should normally be 
4740 05 Feb 09 nicklas 39   an {@link Item} object.
4740 05 Feb 09 nicklas 40
4740 05 Feb 09 nicklas 41   @author Nicklas
4740 05 Feb 09 nicklas 42   @version 2.10
4740 05 Feb 09 nicklas 43   @base.modified $Date$
4740 05 Feb 09 nicklas 44 */
4740 05 Feb 09 nicklas 45 public class BasicItemNodeValidatorFactory
4740 05 Feb 09 nicklas 46   implements NodeValidatorFactory<BasicItem, Object>
4740 05 Feb 09 nicklas 47 {
4740 05 Feb 09 nicklas 48   
6959 01 Oct 15 nicklas 49   private final Map<Object, Class<?>> validators;
4740 05 Feb 09 nicklas 50   private boolean useNullValidator;
4864 30 Mar 09 nicklas 51   private boolean disabled;
4740 05 Feb 09 nicklas 52   
4740 05 Feb 09 nicklas 53   /**
4740 05 Feb 09 nicklas 54     Creates a new factory.
4740 05 Feb 09 nicklas 55   */
4740 05 Feb 09 nicklas 56   public BasicItemNodeValidatorFactory()
4740 05 Feb 09 nicklas 57   {
6959 01 Oct 15 nicklas 58     validators = new HashMap<Object, Class<?>>();
4740 05 Feb 09 nicklas 59     this.useNullValidator = true;
4864 30 Mar 09 nicklas 60     this.disabled = false;
4740 05 Feb 09 nicklas 61     registerDefaultNodeValidators();
4740 05 Feb 09 nicklas 62   }
4740 05 Feb 09 nicklas 63   
4740 05 Feb 09 nicklas 64   /*
4740 05 Feb 09 nicklas 65     From the NodeValidatorFactory interface
4740 05 Feb 09 nicklas 66     ---------------------------------------
4740 05 Feb 09 nicklas 67   */
4864 30 Mar 09 nicklas 68   
4864 30 Mar 09 nicklas 69   
4864 30 Mar 09 nicklas 70   @Override
4864 30 Mar 09 nicklas 71   public boolean isDisabled() 
4864 30 Mar 09 nicklas 72   {
4864 30 Mar 09 nicklas 73     return disabled;
4864 30 Mar 09 nicklas 74   }
4864 30 Mar 09 nicklas 75
4864 30 Mar 09 nicklas 76   @Override
4864 30 Mar 09 nicklas 77   public void setDisabled(boolean disabled) 
4864 30 Mar 09 nicklas 78   {
4864 30 Mar 09 nicklas 79     this.disabled = disabled;
4864 30 Mar 09 nicklas 80   }
4864 30 Mar 09 nicklas 81
4740 05 Feb 09 nicklas 82   /**
4740 05 Feb 09 nicklas 83     Create a new node validator that can validate nodes with items of 
4740 05 Feb 09 nicklas 84     the specified item type. If no validator has been registered for the 
4740 05 Feb 09 nicklas 85     given item type, a {@link NullNodeValidator} is returned or an exception 
4740 05 Feb 09 nicklas 86     is thrown depending on the {@link #useNullValidatorIfNotFound()} option.
4740 05 Feb 09 nicklas 87     
4740 05 Feb 09 nicklas 88     @param key An {@link Item} object
4740 05 Feb 09 nicklas 89     @throws ItemNotFoundException If no node loader is found and the 
4740 05 Feb 09 nicklas 90       {@link #useNullValidatorIfNotFound()} setting is false
4740 05 Feb 09 nicklas 91     @return A node validator object
4740 05 Feb 09 nicklas 92   */
6959 01 Oct 15 nicklas 93   @SuppressWarnings("unchecked")
4740 05 Feb 09 nicklas 94   @Override
4740 05 Feb 09 nicklas 95   public NodeValidator<? extends BasicItem> createNodeValidator(Object key)
4740 05 Feb 09 nicklas 96   {
4740 05 Feb 09 nicklas 97     NodeValidator<? extends BasicItem> validator = null;
6959 01 Oct 15 nicklas 98     Class<?> validatorClass = 
4864 30 Mar 09 nicklas 99       disabled ? null : validators.get(key);
4740 05 Feb 09 nicklas 100     if (validatorClass == null)
4740 05 Feb 09 nicklas 101     {
4864 30 Mar 09 nicklas 102       if (useNullValidator || disabled)
4740 05 Feb 09 nicklas 103       {
5500 18 Nov 10 nicklas 104         validator = new NullNodeValidator<BasicItem>(true, false);
4740 05 Feb 09 nicklas 105       }
4740 05 Feb 09 nicklas 106       else
4740 05 Feb 09 nicklas 107       {
4740 05 Feb 09 nicklas 108         throw new ItemNotFoundException("Node validator for key: " + key);
4740 05 Feb 09 nicklas 109       }
4740 05 Feb 09 nicklas 110     }
4740 05 Feb 09 nicklas 111     else
4740 05 Feb 09 nicklas 112     {
4740 05 Feb 09 nicklas 113       try
4740 05 Feb 09 nicklas 114       {
7513 02 Nov 18 nicklas 115         validator = (NodeValidator<BasicItem>)validatorClass.getDeclaredConstructor().newInstance();
4740 05 Feb 09 nicklas 116       }
7513 02 Nov 18 nicklas 117       catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException ex)
4740 05 Feb 09 nicklas 118       {
4740 05 Feb 09 nicklas 119         throw new BaseException("Can't create node validator: " + validatorClass, ex);
4740 05 Feb 09 nicklas 120       }
4740 05 Feb 09 nicklas 121     }
6875 20 Apr 15 nicklas 122     validator = new ExtensionNodeValidator<>(validator, key);
4740 05 Feb 09 nicklas 123     return validator;
4740 05 Feb 09 nicklas 124   }
4740 05 Feb 09 nicklas 125   // --------------------------------------
4740 05 Feb 09 nicklas 126   
4740 05 Feb 09 nicklas 127   /**
4740 05 Feb 09 nicklas 128     Get the null validator setting.
4740 05 Feb 09 nicklas 129     @see #setUsNullValidatorIfNotFound(boolean)
4740 05 Feb 09 nicklas 130   */
4740 05 Feb 09 nicklas 131   public boolean useNullValidatorIfNotFound()
4740 05 Feb 09 nicklas 132   {
4740 05 Feb 09 nicklas 133     return useNullValidator;
4740 05 Feb 09 nicklas 134   }
4740 05 Feb 09 nicklas 135   
4740 05 Feb 09 nicklas 136   /**
4740 05 Feb 09 nicklas 137     Set a flag that determines if a {@link NullNodeValidator} should be used
4740 05 Feb 09 nicklas 138     or if an {@link ItemNotFoundException} should be thrown
4740 05 Feb 09 nicklas 139     if no registered node validator is found for a specific key.
4740 05 Feb 09 nicklas 140     @param useNullValidator TRUE to use an {@link NullNodeValidator}, FALSE to throw
4740 05 Feb 09 nicklas 141       an exception
4740 05 Feb 09 nicklas 142    */
4740 05 Feb 09 nicklas 143   public void setUsNullValidatorIfNotFound(boolean useNullValidator)
4740 05 Feb 09 nicklas 144   {
4740 05 Feb 09 nicklas 145     this.useNullValidator = useNullValidator;
4740 05 Feb 09 nicklas 146   }
4740 05 Feb 09 nicklas 147
4740 05 Feb 09 nicklas 148   
4740 05 Feb 09 nicklas 149   /**
4740 05 Feb 09 nicklas 150     Register a node validator implementation. The implementation must have a
4740 05 Feb 09 nicklas 151     public no-argument constructor. The given class is checked with
7461 14 Mar 18 nicklas 152     {@link ClassUtil#checkAndLoadClass(ClassLoader, String, boolean, Class, Class...)}
4740 05 Feb 09 nicklas 153     
4740 05 Feb 09 nicklas 154     @param key The item type the node validator handles
4740 05 Feb 09 nicklas 155     @param validatorClass The class name of a class that implements {@link NodeValidator},
4740 05 Feb 09 nicklas 156       or null to unregister an existig validator
4740 05 Feb 09 nicklas 157     @throws NoSuchMethodException If the given class has no public no-argument
4740 05 Feb 09 nicklas 158       constructor
4740 05 Feb 09 nicklas 159     @throws ClassCastException If the given class doesn't implement the
4740 05 Feb 09 nicklas 160       {@link NodeValidator} interface
4740 05 Feb 09 nicklas 161   */
4740 05 Feb 09 nicklas 162   public void registerNodeValidator(Item key, Class<? extends NodeValidator<? extends BasicItem>> validatorClass)
4740 05 Feb 09 nicklas 163     throws NoSuchMethodException, ClassCastException
4740 05 Feb 09 nicklas 164   {
4740 05 Feb 09 nicklas 165     if (validatorClass != null) ClassUtil.checkClass(validatorClass, true, NodeValidator.class);
4740 05 Feb 09 nicklas 166     registerCheckedNodeValidator(key, validatorClass);
4740 05 Feb 09 nicklas 167   }
4740 05 Feb 09 nicklas 168   
4740 05 Feb 09 nicklas 169   /**
4740 05 Feb 09 nicklas 170     Register a node loader implementation that you are sure fulfills the
4740 05 Feb 09 nicklas 171     requirements. Eg. it must have a public no-argument constructor and
4740 05 Feb 09 nicklas 172     implement the {@link NodeValidator} interface.
4740 05 Feb 09 nicklas 173     @see #registerNodeValidator(Item, Class)
4740 05 Feb 09 nicklas 174   */
4740 05 Feb 09 nicklas 175   protected void registerCheckedNodeValidator(Object key, 
6959 01 Oct 15 nicklas 176       Class<?> validatorClass)
4740 05 Feb 09 nicklas 177   {
4740 05 Feb 09 nicklas 178     if (validatorClass == null)
4740 05 Feb 09 nicklas 179     {
4740 05 Feb 09 nicklas 180       validators.remove(key);
4740 05 Feb 09 nicklas 181     }
4740 05 Feb 09 nicklas 182     else
4740 05 Feb 09 nicklas 183     {
4740 05 Feb 09 nicklas 184       validators.put(key, validatorClass);
4740 05 Feb 09 nicklas 185     }
4740 05 Feb 09 nicklas 186   }
4740 05 Feb 09 nicklas 187   
4740 05 Feb 09 nicklas 188
4740 05 Feb 09 nicklas 189   /**
4740 05 Feb 09 nicklas 190     Registers default item node validators from {@link BioSource}
4740 05 Feb 09 nicklas 191     to {@link Experiment}, etc.
4740 05 Feb 09 nicklas 192   */
4740 05 Feb 09 nicklas 193   protected void registerDefaultNodeValidators()
4740 05 Feb 09 nicklas 194   {
4740 05 Feb 09 nicklas 195     // Main validators
4740 05 Feb 09 nicklas 196     registerCheckedNodeValidator(Item.BIOSOURCE, BioSourceValidator.class);
4740 05 Feb 09 nicklas 197     registerCheckedNodeValidator(Item.SAMPLE, SampleValidator.class);
4740 05 Feb 09 nicklas 198     registerCheckedNodeValidator(Item.EXTRACT, ExtractValidator.class);
4740 05 Feb 09 nicklas 199     registerCheckedNodeValidator(Item.ARRAYDESIGN, ArrayDesignValidator.class);
4740 05 Feb 09 nicklas 200     registerCheckedNodeValidator(Item.ARRAYBATCH, ArrayBatchValidator.class);
4740 05 Feb 09 nicklas 201     registerCheckedNodeValidator(Item.ARRAYSLIDE, ArraySlideValidator.class);
5651 08 Jun 11 nicklas 202     registerCheckedNodeValidator(Item.PHYSICALBIOASSAY, PhysicalBioAssayValidator.class);
5807 14 Oct 11 nicklas 203     registerCheckedNodeValidator(Item.DERIVEDBIOASSAY, DerivedBioAssayValidator.class);
4740 05 Feb 09 nicklas 204     registerCheckedNodeValidator(Item.RAWBIOASSAY, RawBioAssayValidator.class);
6959 01 Oct 15 nicklas 205     registerCheckedNodeValidator(Item.ROOTRAWBIOASSAY, RawBioAssayValidator.class);
4740 05 Feb 09 nicklas 206     registerCheckedNodeValidator(Item.EXPERIMENT, ExperimentValidator.class);
6042 03 Apr 12 nicklas 207     registerCheckedNodeValidator(Item.BIOPLATE, BioPlateValidator.class);
5689 11 Aug 11 nicklas 208     
5689 11 Aug 11 nicklas 209     // Property node validators
4740 05 Feb 09 nicklas 210     registerCheckedNodeValidator(Item.PROTOCOL, ProtocolValidator.class);
5651 08 Jun 11 nicklas 211     registerCheckedNodeValidator(Item.TAG, TagValidator.class);
4740 05 Feb 09 nicklas 212     registerCheckedNodeValidator("PROTOCOL.PARAMETER", ProtocolParameterValidator.class);
4764 16 Feb 09 nicklas 213     registerCheckedNodeValidator("EXPERIMENTAL.FACTOR", ExperimentalFactorValidator.class);
4740 05 Feb 09 nicklas 214     registerCheckedNodeValidator(Item.SOFTWARE, SoftwareValidator.class);
4740 05 Feb 09 nicklas 215     registerCheckedNodeValidator(Item.HARDWARE, HardwareValidator.class);
7005 09 Nov 15 nicklas 216     registerCheckedNodeValidator(Item.KIT, KitValidator.class);
4740 05 Feb 09 nicklas 217     registerCheckedNodeValidator(Item.ANNOTATION, AnnotationValidator.class);
4740 05 Feb 09 nicklas 218     registerCheckedNodeValidator(Item.PLATFORM, PlatformValidator.class);
4740 05 Feb 09 nicklas 219     registerCheckedNodeValidator(Item.FILESETMEMBER, DataFileValidator.class);
5500 18 Nov 10 nicklas 220     registerCheckedNodeValidator(Item.ANYTOANY, AnyToAnyValidator.class);
7861 21 Oct 20 nicklas 221     registerCheckedNodeValidator(Item.FILE, FileValidator.class);
4740 05 Feb 09 nicklas 222   }
4740 05 Feb 09 nicklas 223
4740 05 Feb 09 nicklas 224 }