extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/plugins/cmd/ProtocolValidator.java

Code
Comments
Other
Rev Date Author Line
6222 23 Apr 21 nicklas 1 package net.sf.basedb.reggie.plugins.cmd;
6222 23 Apr 21 nicklas 2
6222 23 Apr 21 nicklas 3 import java.util.List;
6222 23 Apr 21 nicklas 4
6222 23 Apr 21 nicklas 5 import net.sf.basedb.core.DbControl;
6222 23 Apr 21 nicklas 6 import net.sf.basedb.core.ItemQuery;
6222 23 Apr 21 nicklas 7 import net.sf.basedb.core.Protocol;
6222 23 Apr 21 nicklas 8 import net.sf.basedb.core.query.Annotations;
6222 23 Apr 21 nicklas 9 import net.sf.basedb.core.query.Expressions;
6222 23 Apr 21 nicklas 10 import net.sf.basedb.core.query.Hql;
6222 23 Apr 21 nicklas 11 import net.sf.basedb.core.query.Restrictions;
6222 23 Apr 21 nicklas 12 import net.sf.basedb.reggie.Reggie;
6222 23 Apr 21 nicklas 13 import net.sf.basedb.reggie.dao.Annotationtype;
6222 23 Apr 21 nicklas 14 import net.sf.basedb.reggie.dao.Subtype;
6222 23 Apr 21 nicklas 15
6222 23 Apr 21 nicklas 16 /**
6222 23 Apr 21 nicklas 17   Find a protocol that is referenced by the "ExternalRef" annotation.
6222 23 Apr 21 nicklas 18   @since 4.32
6222 23 Apr 21 nicklas 19 */
6222 23 Apr 21 nicklas 20 public class ProtocolValidator
6222 23 Apr 21 nicklas 21   implements ValueValidator<String, Protocol>
6222 23 Apr 21 nicklas 22 {
6222 23 Apr 21 nicklas 23
6222 23 Apr 21 nicklas 24   /**
6222 23 Apr 21 nicklas 25     Sample handling (eg. lysate) protocols.
6222 23 Apr 21 nicklas 26   */
6222 23 Apr 21 nicklas 27   static final ProtocolValidator SAMPLE_HANDLING_PROTOCOL = new ProtocolValidator(Subtype.SAMPLE_HANDLING_PROTOCOL);
6222 23 Apr 21 nicklas 28
6222 23 Apr 21 nicklas 29   /**
6222 23 Apr 21 nicklas 30     Extraction protocols.
6222 23 Apr 21 nicklas 31   */
6222 23 Apr 21 nicklas 32   static final ProtocolValidator EXTRACTION_PROTOCOL = new ProtocolValidator(Subtype.EXTRACTION_PROTOCOL);
6222 23 Apr 21 nicklas 33
6222 23 Apr 21 nicklas 34   /**
6222 23 Apr 21 nicklas 35     Library protocols.
6222 23 Apr 21 nicklas 36   */
6222 23 Apr 21 nicklas 37   static final ProtocolValidator LIB_PROTOCOL = new ProtocolValidator(Subtype.LIBRARY_PROTOCOL);
6222 23 Apr 21 nicklas 38   
6222 23 Apr 21 nicklas 39   private final Subtype protocolType;
6222 23 Apr 21 nicklas 40   
6222 23 Apr 21 nicklas 41   ProtocolValidator(Subtype protocolType)
6222 23 Apr 21 nicklas 42   {
6222 23 Apr 21 nicklas 43     this.protocolType = protocolType;
6222 23 Apr 21 nicklas 44   }
6222 23 Apr 21 nicklas 45   
6222 23 Apr 21 nicklas 46   @Override
6222 23 Apr 21 nicklas 47   public Class<String> getExpectedClass() 
6222 23 Apr 21 nicklas 48   {
6222 23 Apr 21 nicklas 49     return String.class;
6222 23 Apr 21 nicklas 50   }
6222 23 Apr 21 nicklas 51
6222 23 Apr 21 nicklas 52   @Override
6222 23 Apr 21 nicklas 53   public Protocol isValid(DbControl dc, String externalRef, JsonSection section, String entryKey) 
6222 23 Apr 21 nicklas 54   {
6510 03 Dec 21 nicklas 55     externalRef = PatternValidator.CMD_PROTOCOL.isValid(dc, externalRef, section, entryKey);
6338 18 Jun 21 nicklas 56     if (externalRef == null) return null;
6222 23 Apr 21 nicklas 57
6222 23 Apr 21 nicklas 58     ItemQuery<Protocol> query = Protocol.getQuery();
6222 23 Apr 21 nicklas 59     query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
6222 23 Apr 21 nicklas 60     if (protocolType != null)
6222 23 Apr 21 nicklas 61     {
6222 23 Apr 21 nicklas 62       protocolType.addFilter(dc, query);
6222 23 Apr 21 nicklas 63     }
6222 23 Apr 21 nicklas 64     query.join(Annotations.innerJoin(Annotationtype.EXTERNAL_REF.get(dc), "ref"));
6222 23 Apr 21 nicklas 65     query.restrict(Restrictions.eq(Hql.alias("ref"), Expressions.string(externalRef)));
6222 23 Apr 21 nicklas 66     List<Protocol> list = query.list(dc);
6222 23 Apr 21 nicklas 67     if (list.size() != 1)
6222 23 Apr 21 nicklas 68     {
6222 23 Apr 21 nicklas 69       String typeMsg = protocolType != null ? "; type="+protocolType.getName() : "";
6222 23 Apr 21 nicklas 70       if (list.size() > 1)
6222 23 Apr 21 nicklas 71       {
6222 23 Apr 21 nicklas 72         section.addErrorMessage("Found "+list.size()+" protocols: "+entryKey+"="+externalRef+typeMsg);
6222 23 Apr 21 nicklas 73       }
6222 23 Apr 21 nicklas 74       else
6222 23 Apr 21 nicklas 75       {
6222 23 Apr 21 nicklas 76         section.addErrorMessage("Protocol not found: "+entryKey+"="+externalRef+typeMsg);
6222 23 Apr 21 nicklas 77       }
6222 23 Apr 21 nicklas 78       return null;
6222 23 Apr 21 nicklas 79     }
6338 18 Jun 21 nicklas 80     return list.get(0);
6222 23 Apr 21 nicklas 81   }
6222 23 Apr 21 nicklas 82 }
6222 23 Apr 21 nicklas 83