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

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