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

Code
Comments
Other
Rev Date Author Line
5651 08 Jun 11 nicklas 1 /**
5651 08 Jun 11 nicklas 2   $Id$
5651 08 Jun 11 nicklas 3
5651 08 Jun 11 nicklas 4   Copyright (C) 2011 Nicklas Nordborg
5651 08 Jun 11 nicklas 5
5651 08 Jun 11 nicklas 6   This file is part of BASE - BioArray Software Environment.
5651 08 Jun 11 nicklas 7   Available at http://base.thep.lu.se/
5651 08 Jun 11 nicklas 8
5651 08 Jun 11 nicklas 9   BASE is free software; you can redistribute it and/or
5651 08 Jun 11 nicklas 10   modify it under the terms of the GNU General Public License
5651 08 Jun 11 nicklas 11   as published by the Free Software Foundation; either version 3
5651 08 Jun 11 nicklas 12   of the License, or (at your option) any later version.
5651 08 Jun 11 nicklas 13
5651 08 Jun 11 nicklas 14   BASE is distributed in the hope that it will be useful,
5651 08 Jun 11 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5651 08 Jun 11 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5651 08 Jun 11 nicklas 17   GNU General Public License for more details.
5651 08 Jun 11 nicklas 18
5651 08 Jun 11 nicklas 19   You should have received a copy of the GNU General Public License
5651 08 Jun 11 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
5651 08 Jun 11 nicklas 21 */
5651 08 Jun 11 nicklas 22 package net.sf.basedb.util.overview.validator;
5651 08 Jun 11 nicklas 23
5651 08 Jun 11 nicklas 24 import net.sf.basedb.core.BasicItem;
5651 08 Jun 11 nicklas 25 import net.sf.basedb.core.DbControl;
5651 08 Jun 11 nicklas 26 import net.sf.basedb.core.Item;
5651 08 Jun 11 nicklas 27 import net.sf.basedb.core.ItemSubtype;
5651 08 Jun 11 nicklas 28 import net.sf.basedb.core.PermissionDeniedException;
5651 08 Jun 11 nicklas 29 import net.sf.basedb.core.Subtypable;
5651 08 Jun 11 nicklas 30 import net.sf.basedb.core.Tag;
5651 08 Jun 11 nicklas 31 import net.sf.basedb.util.overview.Fix;
5651 08 Jun 11 nicklas 32 import net.sf.basedb.util.overview.OverviewContext;
5651 08 Jun 11 nicklas 33 import net.sf.basedb.util.overview.Validator;
5651 08 Jun 11 nicklas 34 import net.sf.basedb.util.overview.Node;
5651 08 Jun 11 nicklas 35
5651 08 Jun 11 nicklas 36 /**
5651 08 Jun 11 nicklas 37   Validator implementation for tags. Validation rules:
5651 08 Jun 11 nicklas 38   <ul>
5651 08 Jun 11 nicklas 39   <li>Access denied: {@link Validator#DENIED_TAG}
5651 08 Jun 11 nicklas 40   <li>Incorrect tag type: {@link Validator#INCORRECT_TAGTYPE} 
5651 08 Jun 11 nicklas 41   </ul>
5651 08 Jun 11 nicklas 42
5651 08 Jun 11 nicklas 43   @author Nicklas
5651 08 Jun 11 nicklas 44   @since 3.0
5651 08 Jun 11 nicklas 45   @base.modified $Date$
5651 08 Jun 11 nicklas 46 */
5651 08 Jun 11 nicklas 47 public class TagValidator
5651 08 Jun 11 nicklas 48   extends NameableNodeValidator<Tag>
5651 08 Jun 11 nicklas 49 {
5651 08 Jun 11 nicklas 50   
5651 08 Jun 11 nicklas 51   public TagValidator()
5651 08 Jun 11 nicklas 52   {
5651 08 Jun 11 nicklas 53     super(null, Validator.DENIED_TAG);
5651 08 Jun 11 nicklas 54   }
5651 08 Jun 11 nicklas 55
5651 08 Jun 11 nicklas 56   
5651 08 Jun 11 nicklas 57   /* 
5651 08 Jun 11 nicklas 58     From BasicValidator class
5651 08 Jun 11 nicklas 59     -------------------------
5651 08 Jun 11 nicklas 60   */
5651 08 Jun 11 nicklas 61   /**
5651 08 Jun 11 nicklas 62     If the parent item has a subtype that is related to a TAG subtype, report
5651 08 Jun 11 nicklas 63     the missing item as a {@link Validator#MISSING_TAG} failure. Otherwise,
5651 08 Jun 11 nicklas 64     ignore the missing tag.
5651 08 Jun 11 nicklas 65     @return Always false
5651 08 Jun 11 nicklas 66   */
5651 08 Jun 11 nicklas 67   @Override
5651 08 Jun 11 nicklas 68   public boolean preMissingItem(DbControl dc, OverviewContext context, Node parentNode)
5651 08 Jun 11 nicklas 69   {
5807 14 Oct 11 nicklas 70     BasicItem parentItem = parentNode.getItem(dc);
5651 08 Jun 11 nicklas 71     ItemSubtype expectedSubtype = getExpectedTagSubtype(dc, parentItem);
5651 08 Jun 11 nicklas 72     if (expectedSubtype != null)
5651 08 Jun 11 nicklas 73     {
5651 08 Jun 11 nicklas 74       context.createFailure(Validator.MISSING_TAG, parentNode, 
5651 08 Jun 11 nicklas 75           "Missing " + expectedSubtype.getName().toLowerCase(), getMissingItemFix(dc, parentNode));
5651 08 Jun 11 nicklas 76     }
5651 08 Jun 11 nicklas 77     return false;
5651 08 Jun 11 nicklas 78   }
5651 08 Jun 11 nicklas 79
5651 08 Jun 11 nicklas 80   /**
5807 14 Oct 11 nicklas 81     Checks if the tag is of the correct subtype.
5651 08 Jun 11 nicklas 82   */
5651 08 Jun 11 nicklas 83   @Override
5651 08 Jun 11 nicklas 84   public void postValidate(DbControl dc, OverviewContext context, Node node, Node parentNode)
5651 08 Jun 11 nicklas 85   {
5651 08 Jun 11 nicklas 86     super.postValidate(dc, context, node, parentNode);
5807 14 Oct 11 nicklas 87     Tag tag = (Tag)node.getItem(dc);
5651 08 Jun 11 nicklas 88     
5807 14 Oct 11 nicklas 89     BasicItem parentItem = parentNode.getItem(dc);
5651 08 Jun 11 nicklas 90     ItemSubtype expectedSubtype = getExpectedTagSubtype(dc, parentItem);
5651 08 Jun 11 nicklas 91     try
5651 08 Jun 11 nicklas 92     {
5651 08 Jun 11 nicklas 93       ItemSubtype subtype = tag.getItemSubtype();
5651 08 Jun 11 nicklas 94       if (expectedSubtype != null && !expectedSubtype.equals(subtype))
5651 08 Jun 11 nicklas 95       {
5807 14 Oct 11 nicklas 96         ItemSubtype mySubtype = ((Subtypable)parentItem).getItemSubtype();
5807 14 Oct 11 nicklas 97         Fix subtypeFix = subtype == null ? null : 
5807 14 Oct 11 nicklas 98           new Fix("Change related tag subtype of '" + mySubtype.getName() + "' to '" + subtype.getName() + "'", mySubtype);
5807 14 Oct 11 nicklas 99
5651 08 Jun 11 nicklas 100         context.createFailure(Validator.INCORRECT_TAGTYPE, parentNode, 
5651 08 Jun 11 nicklas 101           "Expected a tag with subtype: " + expectedSubtype.getName(),
5651 08 Jun 11 nicklas 102           new Fix("Change tag of '" + parentNode.getTitle() + "'", parentItem),
5807 14 Oct 11 nicklas 103           new Fix("Change tag type of '" + tag.getName() + "' to '" + expectedSubtype.getName() + "'", tag),
5807 14 Oct 11 nicklas 104           subtypeFix
5651 08 Jun 11 nicklas 105         );
5651 08 Jun 11 nicklas 106       }
5651 08 Jun 11 nicklas 107     }
5651 08 Jun 11 nicklas 108     catch (PermissionDeniedException ex)
5651 08 Jun 11 nicklas 109     {}
5651 08 Jun 11 nicklas 110   }
5651 08 Jun 11 nicklas 111
5651 08 Jun 11 nicklas 112   /**
5651 08 Jun 11 nicklas 113     @return Suggested fix is to add a tag to the parent item
5651 08 Jun 11 nicklas 114   */
5651 08 Jun 11 nicklas 115   @Override
5651 08 Jun 11 nicklas 116   protected Fix getMissingItemFix(DbControl dc, Node parentNode)
5651 08 Jun 11 nicklas 117   {
5651 08 Jun 11 nicklas 118     BasicItem parentItem = parentNode.getItem();    
5651 08 Jun 11 nicklas 119     return new Fix("Add tag to '" + parentNode.getTitle() + "'", parentItem);
5651 08 Jun 11 nicklas 120   }
5651 08 Jun 11 nicklas 121   // --------------------------------
5651 08 Jun 11 nicklas 122   
5651 08 Jun 11 nicklas 123   /**
5651 08 Jun 11 nicklas 124     Get the subtype of the associated tag that we expect for the given
5651 08 Jun 11 nicklas 125     parent item.
5651 08 Jun 11 nicklas 126   */
5651 08 Jun 11 nicklas 127   protected ItemSubtype getExpectedTagSubtype(DbControl dc, BasicItem parentItem)
5651 08 Jun 11 nicklas 128   {
5651 08 Jun 11 nicklas 129     ItemSubtype expectedSubtype = null;
5651 08 Jun 11 nicklas 130     try
5651 08 Jun 11 nicklas 131     {
5651 08 Jun 11 nicklas 132       if (parentItem instanceof Subtypable)
5651 08 Jun 11 nicklas 133       {
5651 08 Jun 11 nicklas 134         expectedSubtype = ItemSubtype.getRelatedSubtype(dc, 
5651 08 Jun 11 nicklas 135             (Subtypable)parentItem, Item.TAG, 0);
5651 08 Jun 11 nicklas 136       }
5651 08 Jun 11 nicklas 137     }
5651 08 Jun 11 nicklas 138     catch (PermissionDeniedException ex)
5651 08 Jun 11 nicklas 139     {}
5651 08 Jun 11 nicklas 140     return expectedSubtype;
5651 08 Jun 11 nicklas 141   }
5651 08 Jun 11 nicklas 142   
5651 08 Jun 11 nicklas 143 }