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.access.mcrimpl;
20  
21  import java.util.Arrays;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.LinkedList;
26  
27  import org.apache.logging.log4j.LogManager;
28  import org.apache.logging.log4j.Logger;
29  import org.mycore.backend.jpa.access.MCRJPAAccessStore;
30  import org.mycore.common.config.MCRConfiguration2;
31  import org.mycore.datamodel.common.MCRXMLMetadataManager;
32  
33  /**
34   * The purpose of this interface is to make the choice of the persistence layer
35   * configurable. Any concrete database-class which stores MyCoRe Access control
36   * must implement this interface. Which database actually will be used can then
37   * be configured by reading the value <code>MCR.Persistence.Access.Store.Class</code>
38   * from mycore.properties.access
39   * 
40   * @author Arne Seifert
41   * @version $Revision$ $Date$
42   */
43  public abstract class MCRAccessStore {
44      private static final Logger LOGGER = LogManager.getLogger(MCRAccessStore.class);
45  
46      protected static final String SQL_DATEFORMAT = "yyyy-MM-dd HH:mm:ss";
47  
48      protected static final String ACCESS_POOLS = MCRConfiguration2.getString("MCR.AccessPools")
49          .orElse("read,write,delete");
50  
51      private static MCRAccessStore implementation;
52  
53      public abstract String getRuleID(String objID, String acPool);
54  
55      public abstract void createAccessDefinition(MCRRuleMapping accessdata);
56  
57      public abstract void deleteAccessDefinition(MCRRuleMapping accessdata);
58  
59      public abstract void updateAccessDefinition(MCRRuleMapping accessdata);
60  
61      public abstract MCRRuleMapping getAccessDefinition(String pool, String objid);
62  
63      public abstract Collection<String> getMappedObjectId(String pool); // ArrayList with ObjID's as String
64  
65      public abstract Collection<String> getPoolsForObject(String objid); // ArrayList with pools as String
66  
67      public abstract Collection<String> getDatabasePools();
68  
69      public abstract boolean existsRule(String objid, String pool);
70  
71      public abstract boolean isRuleInUse(String ruleid);
72  
73      /**
74       * 
75       * @return a collection of all String IDs an access rule is assigned to
76       */
77      public abstract Collection<String> getDistinctStringIDs();
78  
79      public static MCRAccessStore getInstance() {
80          try {
81              if (implementation == null) {
82                  implementation = MCRConfiguration2
83                      .getSingleInstanceOf("MCR.Persistence.Access.Store.Class", MCRJPAAccessStore.class).get();
84              }
85          } catch (Exception e) {
86              LOGGER.error(e);
87          }
88  
89          return implementation;
90      }
91  
92      public static Collection<String> getPools() {
93          String[] pool = ACCESS_POOLS.split(",");
94          return Arrays.asList(pool);
95      }
96  
97      /**
98       * alle Elemente eines Datentypes aufbereiten
99       * @param type document type
100      * 
101      * @return List of MCRAccessDefinition
102      * @see MCRAccessDefinition
103      */
104     public Collection<MCRAccessDefinition> getDefinition(String type) {
105         try {
106             HashMap<String, Collection<String>> sqlDefinition = new HashMap<>();
107             Collection<String> pools = MCRAccessStore.getInstance().getDatabasePools();
108             //merge pools
109             pools.removeAll(getPools());
110             pools.addAll(getPools());
111 
112             for (String pool : pools) {
113                 sqlDefinition.put(pool, MCRAccessStore.getInstance().getMappedObjectId(pool));
114             }
115 
116             Collection<MCRAccessDefinition> ret = new LinkedList<>();
117             Collection<String> elements;
118             MCRAccessDefinition def = null;
119 
120             if (MCRConfiguration2.getOrThrow("MCR.Metadata.Type." + type, Boolean::parseBoolean)) {
121                 elements = MCRXMLMetadataManager.instance().listIDsOfType(type);
122             } else {
123                 return Collections.emptySet();
124             }
125 
126             for (String element : elements) {
127                 def = new MCRAccessDefinition();
128                 def.setObjID(element);
129                 for (String pool : pools) {
130                     Collection<String> l = sqlDefinition.get(pool);
131                     if (l.contains(element)) {
132                         def.addPool(pool, "X");
133                     } else {
134                         def.addPool(pool, " ");
135                     }
136                 }
137                 ret.add(def);
138             }
139             return ret;
140         } catch (Exception e) {
141             LOGGER.error("definition loading failed: ", e);
142             return null;
143         }
144     }
145 
146     public Collection<MCRAccessDefinition> getRules(String objid) {
147         try {
148             Collection<String> pools = MCRAccessStore.getInstance().getDatabasePools();
149             //merge pools
150             pools.removeAll(getPools());
151             pools.addAll(getPools());
152 
153             Collection<MCRAccessDefinition> ret = new LinkedList<>();
154             //List elements = new LinkedList();
155             MCRAccessDefinition def = new MCRAccessDefinition();
156             def.setObjID(objid);
157             for (String pool : pools) {
158                 String rule = getRuleID(objid, pool);
159                 if (rule != null) {
160                     def.addPool(pool, rule);
161                 } else {
162                     def.addPool(pool, " ");
163                 }
164             }
165             ret.add(def);
166             return ret;
167         } catch (Exception e) {
168             LOGGER.error("definition loading failed: ");
169             return null;
170         }
171     }
172 
173 }