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.Optional;
22  
23  import org.jdom2.Element;
24  import org.mycore.access.facts.MCRFactsHolder;
25  import org.mycore.access.facts.condition.fact.MCRStringCondition;
26  import org.mycore.access.facts.fact.MCRObjectIDFact;
27  import org.mycore.access.facts.fact.MCRStringFact;
28  import org.mycore.datamodel.metadata.MCRObject;
29  import org.mycore.mods.MCRMODSEmbargoUtils;
30  
31  /**
32   * condition for fact-based access system, that checks 
33   * if an embargo exists for a given MyCoRe object 
34   * 
35   * @author Robert Stephan
36   *
37   */
38  public class MCRMODSEmbargoCondition extends MCRStringCondition {
39  
40      private String idFact = "objid";
41  
42      @Override
43      public void parse(Element xml) {
44          super.parse(xml);
45          this.idFact = Optional.ofNullable(xml.getAttributeValue("idfact")).orElse("objid");
46      }
47  
48      @Override
49      public Optional<MCRStringFact> computeFact(MCRFactsHolder facts) {
50          Optional<MCRObjectIDFact> idc = facts.require(idFact);
51          if (idc.isPresent()) {
52              Optional<MCRObject> optMCRObject = idc.get().getObject();
53              if (optMCRObject.isPresent()) {
54                  String embargo = MCRMODSEmbargoUtils.getEmbargo(optMCRObject.get());
55  
56                  //only positive facts are added to the facts holder
57                  if (embargo != null) {
58                      MCRStringFact fact = new MCRStringFact(getFactName(), getTerm());
59                      fact.setValue(embargo);
60                      facts.add(fact);
61                      return Optional.of(fact);
62                  }
63              }
64          }
65          return Optional.empty();
66      }
67  }