View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.wfc.actionmapping;
20  
21  import org.apache.logging.log4j.LogManager;
22  import org.jdom2.Element;
23  import org.mycore.datamodel.classifications2.MCRCategLinkReference;
24  import org.mycore.datamodel.classifications2.MCRCategLinkService;
25  import org.mycore.datamodel.classifications2.MCRCategLinkServiceFactory;
26  import org.mycore.datamodel.classifications2.MCRCategoryID;
27  import org.mycore.parsers.bool.MCRCondition;
28  
29  /**
30   * @author Thomas Scheffler (yagee)
31   *
32   */
33  public class MCRCategoryCondition implements MCRCondition<MCRWorkflowData> {
34  
35      private MCRCategoryID mcrCategoryID;
36  
37      private boolean not;
38  
39      private String fieldName;
40  
41      private static MCRCategLinkService LINK_SERVICE = MCRCategLinkServiceFactory.getInstance();
42  
43      public MCRCategoryCondition(String fieldName, MCRCategoryID mcrCategoryID, boolean not) {
44          this.fieldName = fieldName;
45          this.mcrCategoryID = mcrCategoryID;
46          this.not = not;
47  
48      }
49  
50      /* (non-Javadoc)
51       * @see org.mycore.parsers.bool.MCRCondition#evaluate(java.lang.Object)
52       */
53      @Override
54      public boolean evaluate(MCRWorkflowData workflowData) {
55          MCRCategLinkReference reference = workflowData.getCategoryReference();
56          if (reference == null) {
57              LogManager.getLogger(getClass())
58                  .error("Cannot evaluate '{}', if MCRWorkflowData does not contain an object reference",
59                      toString());
60              return false;
61          }
62          return LINK_SERVICE.isInCategory(reference, mcrCategoryID) ^ not;
63      }
64  
65      @Override
66      public String toString() {
67          return fieldName + (not ? " != " : " = ") + mcrCategoryID.getID() + " ";
68      }
69  
70      /* (non-Javadoc)
71       * @see org.mycore.parsers.bool.MCRCondition#toXML()
72       */
73      @Override
74      public Element toXML() {
75          Element cond = new Element("condition");
76          cond.setAttribute("field", fieldName);
77          cond.setAttribute("operator", (not ? "!=" : "="));
78          cond.setAttribute("value", mcrCategoryID.getID());
79          return cond;
80      }
81  
82  }