1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
35
36
37
38
39
40
41
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);
64
65 public abstract Collection<String> getPoolsForObject(String objid);
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
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
99
100
101
102
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
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
150 pools.removeAll(getPools());
151 pools.addAll(getPools());
152
153 Collection<MCRAccessDefinition> ret = new LinkedList<>();
154
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 }