src/core/net/sf/basedb/util/fileset/ValidationRenderer.java

Code
Comments
Other
Rev Date Author Line
5623 06 May 11 nicklas 1 /**
5623 06 May 11 nicklas 2   $Id$
5623 06 May 11 nicklas 3
5623 06 May 11 nicklas 4   Copyright (C) 2011 Nicklas Nordborg
5623 06 May 11 nicklas 5
5623 06 May 11 nicklas 6   This file is part of BASE - BioArray Software Environment.
5623 06 May 11 nicklas 7   Available at http://base.thep.lu.se/
5623 06 May 11 nicklas 8
5623 06 May 11 nicklas 9   BASE is free software; you can redistribute it and/or
5623 06 May 11 nicklas 10   modify it under the terms of the GNU General Public License
5623 06 May 11 nicklas 11   as published by the Free Software Foundation; either version 3
5623 06 May 11 nicklas 12   of the License, or (at your option) any later version.
5623 06 May 11 nicklas 13
5623 06 May 11 nicklas 14   BASE is distributed in the hope that it will be useful,
5623 06 May 11 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5623 06 May 11 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5623 06 May 11 nicklas 17   GNU General Public License for more details.
5623 06 May 11 nicklas 18
5623 06 May 11 nicklas 19   You should have received a copy of the GNU General Public License
5623 06 May 11 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
5623 06 May 11 nicklas 21 */
5623 06 May 11 nicklas 22 package net.sf.basedb.util.fileset;
5623 06 May 11 nicklas 23
5623 06 May 11 nicklas 24 import java.util.ArrayList;
5623 06 May 11 nicklas 25 import java.util.List;
5623 06 May 11 nicklas 26
5623 06 May 11 nicklas 27 import net.sf.basedb.core.InvalidRelationException;
5623 06 May 11 nicklas 28 import net.sf.basedb.core.PermissionDeniedException;
5623 06 May 11 nicklas 29 import net.sf.basedb.core.signal.ThreadSignalHandler;
8144 21 Apr 23 nicklas 30 import net.sf.basedb.util.extensions.AbstractRenderer;
8144 21 Apr 23 nicklas 31 import net.sf.basedb.util.extensions.Extension;
5623 06 May 11 nicklas 32
5623 06 May 11 nicklas 33 /**
5623 06 May 11 nicklas 34   Renderer implementation for validating the members of a file set.
5623 06 May 11 nicklas 35   
5623 06 May 11 nicklas 36   @author Nicklas
5623 06 May 11 nicklas 37   @since 3.0
5623 06 May 11 nicklas 38   @base.modified $Date$
5623 06 May 11 nicklas 39 */
5623 06 May 11 nicklas 40 public class ValidationRenderer
8144 21 Apr 23 nicklas 41   extends AbstractRenderer<ValidationAction>
5623 06 May 11 nicklas 42 {
5623 06 May 11 nicklas 43
5623 06 May 11 nicklas 44   private final List<ValidatingFileSetMember> members;
5623 06 May 11 nicklas 45   private final List<Throwable> errors;
5623 06 May 11 nicklas 46   
5623 06 May 11 nicklas 47   private boolean isValid;
5623 06 May 11 nicklas 48   private String message;
5623 06 May 11 nicklas 49   
5623 06 May 11 nicklas 50   /**
5623 06 May 11 nicklas 51     Create a new renderer instance.
5623 06 May 11 nicklas 52   
5623 06 May 11 nicklas 53     @param members A list with the members of the file set
5623 06 May 11 nicklas 54     @param errors A list for collecting validation errors
5623 06 May 11 nicklas 55   */
5623 06 May 11 nicklas 56   public ValidationRenderer(List<ValidatingFileSetMember> members, List<Throwable> errors)
5623 06 May 11 nicklas 57   {
5623 06 May 11 nicklas 58     this.members = members;
5623 06 May 11 nicklas 59     this.errors = errors;
5623 06 May 11 nicklas 60   }
5623 06 May 11 nicklas 61
5623 06 May 11 nicklas 62   /*
5623 06 May 11 nicklas 63     From the Renderer interface
5623 06 May 11 nicklas 64     ---------------------------
5623 06 May 11 nicklas 65   */
5623 06 May 11 nicklas 66   @Override
8144 21 Apr 23 nicklas 67   public void render(ValidationAction action, Extension<? extends ValidationAction> ext)
5623 06 May 11 nicklas 68   {
5623 06 May 11 nicklas 69     ThreadSignalHandler.checkInterrupted();
5623 06 May 11 nicklas 70     
5623 06 May 11 nicklas 71     // Check if the action accepts any file(s)
5623 06 May 11 nicklas 72     List<ValidatingFileSetMember> accepted = new ArrayList<ValidatingFileSetMember>();
5623 06 May 11 nicklas 73     for (ValidatingFileSetMember member : members)
5623 06 May 11 nicklas 74     {
5623 06 May 11 nicklas 75       Accept accept = member.isAcceptedByValidator(action);
5623 06 May 11 nicklas 76       if (accept == Accept.VALIDATE_IMMEDIATELY)
5623 06 May 11 nicklas 77       {
5623 06 May 11 nicklas 78         // Validate the file immediately
5623 06 May 11 nicklas 79         validate(action);
5623 06 May 11 nicklas 80         member.setResult(isValid, message);
5623 06 May 11 nicklas 81       }
5623 06 May 11 nicklas 82       else if (accept == Accept.VALIDATE_LATER)
5623 06 May 11 nicklas 83       {
5623 06 May 11 nicklas 84         // File will be validated in batch later
5623 06 May 11 nicklas 85         accepted.add(member);
5623 06 May 11 nicklas 86       }
5623 06 May 11 nicklas 87     }
5623 06 May 11 nicklas 88     
5623 06 May 11 nicklas 89     // Are there any files waiting for validation?
5623 06 May 11 nicklas 90     if (accepted.size() == 0) return;
5623 06 May 11 nicklas 91
5623 06 May 11 nicklas 92     // Validate the remaining files
5623 06 May 11 nicklas 93     validate(action);
5623 06 May 11 nicklas 94     for (ValidatingFileSetMember member : accepted)
5623 06 May 11 nicklas 95     {
5623 06 May 11 nicklas 96       member.setResult(isValid, message);
5623 06 May 11 nicklas 97     }
5623 06 May 11 nicklas 98   }
5623 06 May 11 nicklas 99   // -----------------------------------
5623 06 May 11 nicklas 100   
5623 06 May 11 nicklas 101   private void validate(ValidationAction action)
5623 06 May 11 nicklas 102   {
5623 06 May 11 nicklas 103     try
5623 06 May 11 nicklas 104     {
5623 06 May 11 nicklas 105       action.validateAndExtractMetadata();
5623 06 May 11 nicklas 106       isValid = true;
5623 06 May 11 nicklas 107       message = null;
5623 06 May 11 nicklas 108     }
5623 06 May 11 nicklas 109     catch (Throwable t)
5623 06 May 11 nicklas 110     {
5623 06 May 11 nicklas 111       boolean invalidRelation = t instanceof InvalidRelationException;
5623 06 May 11 nicklas 112       boolean ignore = t instanceof PermissionDeniedException;
5623 06 May 11 nicklas 113       isValid = invalidRelation;
5623 06 May 11 nicklas 114       message = t.getMessage();
5623 06 May 11 nicklas 115       if (!ignore)
5623 06 May 11 nicklas 116       {
5623 06 May 11 nicklas 117         errors.add(t);
5623 06 May 11 nicklas 118         if (!invalidRelation) action.resetMetadata();
5623 06 May 11 nicklas 119       }
5623 06 May 11 nicklas 120     }
5623 06 May 11 nicklas 121   }
5623 06 May 11 nicklas 122   
5623 06 May 11 nicklas 123 }