src/core/net/sf/basedb/util/overview/validator/ProtocolParameterValidator.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
4740 05 Feb 09 nicklas 24 import java.util.List;
4740 05 Feb 09 nicklas 25
4740 05 Feb 09 nicklas 26 import net.sf.basedb.core.Annotatable;
4740 05 Feb 09 nicklas 27 import net.sf.basedb.core.Annotation;
4740 05 Feb 09 nicklas 28 import net.sf.basedb.core.AnnotationSet;
4740 05 Feb 09 nicklas 29 import net.sf.basedb.core.AnnotationType;
4740 05 Feb 09 nicklas 30 import net.sf.basedb.core.BasicItem;
4740 05 Feb 09 nicklas 31 import net.sf.basedb.core.DbControl;
4740 05 Feb 09 nicklas 32 import net.sf.basedb.core.InvalidDataException;
4740 05 Feb 09 nicklas 33 import net.sf.basedb.util.overview.Fix;
4740 05 Feb 09 nicklas 34 import net.sf.basedb.util.overview.OverviewContext;
4740 05 Feb 09 nicklas 35 import net.sf.basedb.util.overview.Validator;
4740 05 Feb 09 nicklas 36 import net.sf.basedb.util.overview.Node;
4740 05 Feb 09 nicklas 37
4740 05 Feb 09 nicklas 38 /**
4740 05 Feb 09 nicklas 39   Validator implementation for protocol parameters. The node containing
4740 05 Feb 09 nicklas 40   the parameter is expected to live in a folder-type node that
4740 05 Feb 09 nicklas 41   has a protocol as it's parent. The protocol node must have a 
4740 05 Feb 09 nicklas 42   parent node that contains an {@link Annotatable} item since this
4740 05 Feb 09 nicklas 43   is were the  parameter values are stored. Validation rules:
4740 05 Feb 09 nicklas 44   <ul>
4740 05 Feb 09 nicklas 45   <li>No value for parameter: {@link Validator#MISSING_PARAMETER}
4740 05 Feb 09 nicklas 46   <li>Invalid parameter value: {@link Validator#ANNOTATION_INVALID_VALUE}
4740 05 Feb 09 nicklas 47   </ul>
4740 05 Feb 09 nicklas 48   
4740 05 Feb 09 nicklas 49   @author Nicklas
4740 05 Feb 09 nicklas 50   @version 2.10
4740 05 Feb 09 nicklas 51   @base.modified $Date$
4740 05 Feb 09 nicklas 52 */
4740 05 Feb 09 nicklas 53 public class ProtocolParameterValidator
4764 16 Feb 09 nicklas 54   extends NameableNodeValidator<AnnotationType>
4740 05 Feb 09 nicklas 55 {
4740 05 Feb 09 nicklas 56   
4749 11 Feb 09 nicklas 57   private Node lastProtocolNode;
4740 05 Feb 09 nicklas 58   private Node parentItemNode;
4740 05 Feb 09 nicklas 59   private BasicItem parentItem;
4740 05 Feb 09 nicklas 60   private AnnotationSet annotationSet;
4740 05 Feb 09 nicklas 61   
4740 05 Feb 09 nicklas 62   public ProtocolParameterValidator()
4740 05 Feb 09 nicklas 63   {
4740 05 Feb 09 nicklas 64     super(null, null);
4740 05 Feb 09 nicklas 65   }
4740 05 Feb 09 nicklas 66
4740 05 Feb 09 nicklas 67   
4740 05 Feb 09 nicklas 68   /* 
4740 05 Feb 09 nicklas 69     From BasicNodeValidator class
4740 05 Feb 09 nicklas 70     -----------------------------
4740 05 Feb 09 nicklas 71   */
4740 05 Feb 09 nicklas 72   @Override
4749 11 Feb 09 nicklas 73   public void postValidate(DbControl dc, OverviewContext context, Node node, Node protocolNode)
4740 05 Feb 09 nicklas 74   {
4749 11 Feb 09 nicklas 75     super.postValidate(dc, context, node, protocolNode);
4749 11 Feb 09 nicklas 76     getParentInformation(dc, protocolNode);
4740 05 Feb 09 nicklas 77     
4740 05 Feb 09 nicklas 78     // Safety measure if a parent item is not found -- shouldn't happen
4740 05 Feb 09 nicklas 79     if (parentItem == null) return;
4740 05 Feb 09 nicklas 80
5807 14 Oct 11 nicklas 81     AnnotationType parameter = (AnnotationType)node.getItem(dc);
4740 05 Feb 09 nicklas 82     // Do we have a value for the protocol parameter?
6921 26 May 15 nicklas 83     if (annotationSet == null || !annotationSet.hasAnnotation(parameter, Annotation.Source.PRIMARY))
4740 05 Feb 09 nicklas 84     {
4740 05 Feb 09 nicklas 85       // NO :(
4740 05 Feb 09 nicklas 86       context.createFailure(Validator.MISSING_PARAMETER, parentItemNode, 
4740 05 Feb 09 nicklas 87         "Missing value for parameter: " + parameter.getName(), 
4740 05 Feb 09 nicklas 88         new Fix("Add value for: " + parameter.getName(), parentItem, parameter, false));
4740 05 Feb 09 nicklas 89     }
4740 05 Feb 09 nicklas 90     else
4740 05 Feb 09 nicklas 91     {
4740 05 Feb 09 nicklas 92       // YES, check that the value(s) are valid
4740 05 Feb 09 nicklas 93       Annotation a = annotationSet.getAnnotation(parameter);
4740 05 Feb 09 nicklas 94       try
4740 05 Feb 09 nicklas 95       {
4740 05 Feb 09 nicklas 96         List<?> values = a.getValues();
4740 05 Feb 09 nicklas 97         if (values != null)
4740 05 Feb 09 nicklas 98         {
4740 05 Feb 09 nicklas 99           for (Object value : values)
4740 05 Feb 09 nicklas 100           {
4740 05 Feb 09 nicklas 101             parameter.validateAnnotationValue(value);
4740 05 Feb 09 nicklas 102           }
4740 05 Feb 09 nicklas 103         }
4740 05 Feb 09 nicklas 104       }
4740 05 Feb 09 nicklas 105       catch (InvalidDataException ex)
4740 05 Feb 09 nicklas 106       {
4740 05 Feb 09 nicklas 107         context.createFailure(Validator.ANNOTATION_INVALID_VALUE, parentItemNode, ex.getMessage(),
6939 21 Aug 15 nicklas 108           new Fix("Change parameter value", parentItem, a),
4740 05 Feb 09 nicklas 109           new Fix("Change parameter restrictions", parameter)
4740 05 Feb 09 nicklas 110         );
4740 05 Feb 09 nicklas 111       }
4740 05 Feb 09 nicklas 112     }
4740 05 Feb 09 nicklas 113   }
4740 05 Feb 09 nicklas 114   
4740 05 Feb 09 nicklas 115   /**
4740 05 Feb 09 nicklas 116     @return Always "null"
4740 05 Feb 09 nicklas 117   */
4740 05 Feb 09 nicklas 118   @Override
5651 08 Jun 11 nicklas 119   protected Fix getMissingItemFix(DbControl dc, Node parentNode)
4740 05 Feb 09 nicklas 120   {
4740 05 Feb 09 nicklas 121     return null;
4740 05 Feb 09 nicklas 122   }
4740 05 Feb 09 nicklas 123   // ----------------------------
4740 05 Feb 09 nicklas 124   
4740 05 Feb 09 nicklas 125   /**
4749 11 Feb 09 nicklas 126     Load parent item information from the current protocol node.
6898 12 May 15 nicklas 127     We expect: protocolNode -&gt; annotatableNode
4740 05 Feb 09 nicklas 128   */
4749 11 Feb 09 nicklas 129   private void getParentInformation(DbControl dc, Node protocolNode)
4740 05 Feb 09 nicklas 130   {
4740 05 Feb 09 nicklas 131     // No need to reload if we are still in the same folder
4749 11 Feb 09 nicklas 132     if (lastProtocolNode == protocolNode) return;
4740 05 Feb 09 nicklas 133     
4740 05 Feb 09 nicklas 134     // Reset existing
4749 11 Feb 09 nicklas 135     lastProtocolNode = protocolNode;
4740 05 Feb 09 nicklas 136     parentItemNode = null;
4740 05 Feb 09 nicklas 137     parentItem = null;
4740 05 Feb 09 nicklas 138     annotationSet = null;
4740 05 Feb 09 nicklas 139     
4740 05 Feb 09 nicklas 140     // Get parent item of the protocol
4740 05 Feb 09 nicklas 141     parentItemNode = protocolNode.getParent();
4740 05 Feb 09 nicklas 142     if (parentItemNode == null) return;
4740 05 Feb 09 nicklas 143     parentItem = parentItemNode.getItem(dc);
4740 05 Feb 09 nicklas 144     if (!(parentItem instanceof Annotatable)) return;
4740 05 Feb 09 nicklas 145     
4740 05 Feb 09 nicklas 146     Annotatable annotatable = (Annotatable)parentItem;
4740 05 Feb 09 nicklas 147     if (annotatable.isAnnotated()) annotationSet = annotatable.getAnnotationSet();
4740 05 Feb 09 nicklas 148   }
4740 05 Feb 09 nicklas 149
4740 05 Feb 09 nicklas 150 }