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.mods.access.facts.condition;
20  
21  import java.util.List;
22  import java.util.Optional;
23  
24  import org.jdom2.Element;
25  import org.mycore.access.facts.MCRFactsHolder;
26  import org.mycore.access.facts.condition.fact.MCRStringCondition;
27  import org.mycore.access.facts.fact.MCRObjectIDFact;
28  import org.mycore.access.facts.fact.MCRStringFact;
29  import org.mycore.datamodel.metadata.MCRObject;
30  import org.mycore.mods.MCRMODSWrapper;
31  
32  /**
33   * condition for fact-based access system,
34   * that checks if a certain mods:genre is set.
35   * 
36   * TODO Could probably be replaced with a more generic XPathCondition.
37   * 
38   * @author Robert Stephan
39   *
40   */
41  public class MCRMODSGenreCondition extends MCRStringCondition {
42  
43      private static final String XPATH_COLLECTION = "mods:genre[contains(@valueURI,'/mir_genres#')]";
44  
45      private String idFact = "objid";
46  
47      @Override
48      public void parse(Element xml) {
49          super.parse(xml);
50          this.idFact = Optional.ofNullable(xml.getAttributeValue("idfact")).orElse("objid");
51      }
52  
53      @Override
54      public Optional<MCRStringFact> computeFact(MCRFactsHolder facts) {
55  
56          Optional<MCRObjectIDFact> idc = facts.require(idFact);
57          if (idc.isPresent()) {
58              Optional<MCRObject> optMCRObject = idc.get().getObject();
59              if (optMCRObject.isPresent()) {
60                  MCRMODSWrapper wrapper = new MCRMODSWrapper(optMCRObject.get());
61                  List<Element> e = wrapper.getElements(XPATH_COLLECTION);
62                  if ((e != null) && !(e.isEmpty())) {
63                      String value = e.get(0).getAttributeValue("valueURI").split("#")[1];
64                      if (value.equals(getTerm())) {
65                          MCRStringFact fact = new MCRStringFact(getFactName(), getTerm());
66                          fact.setValue(value);
67                          facts.add(fact);
68                          return Optional.of(fact);
69                      }
70                  }
71              }
72          }
73          return Optional.empty();
74      }
75  }