001 /*
002 *
003 * $Revision$ $Date$
004 *
005 * This file is part of *** M y C o R e ***
006 * See http://www.mycore.de/ for details.
007 *
008 * This program is free software; you can use it, redistribute it
009 * and / or modify it under the terms of the GNU General Public License
010 * (GPL) as published by the Free Software Foundation; either version 2
011 * of the License or (at your option) any later version.
012 *
013 * This program is distributed in the hope that it will be useful, but
014 * WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program, in a file called gpl.txt or license.txt.
020 * If not, write to the Free Software Foundation Inc.,
021 * 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA
022 */
023
024 package org.mycore.access.mcrimpl;
025
026 import java.util.Arrays;
027 import java.util.Collection;
028 import java.util.Collections;
029 import java.util.HashMap;
030 import java.util.LinkedList;
031
032 import org.apache.log4j.Logger;
033 import org.mycore.common.MCRConfiguration;
034 import org.mycore.datamodel.common.MCRXMLTableManager;
035
036 /**
037 * The purpose of this interface is to make the choice of the persistence layer
038 * configurable. Any concrete database-class which stores MyCoRe Access control
039 * must implement this interface. Which database actually will be used can then
040 * be configured by reading the value <code>MCR.Persistence.Access.Store.Class</code>
041 * from mycore.properties.access
042 *
043 * @author Arne Seifert
044 * @version $Revision$ $Date$
045 */
046 public abstract class MCRAccessStore {
047 public abstract void createTables();
048
049 public abstract String getRuleID(String objID, String ACPool);
050
051 public abstract void createAccessDefinition(MCRRuleMapping accessdata);
052
053 public abstract void deleteAccessDefinition(MCRRuleMapping accessdata);
054
055 public abstract void updateAccessDefinition(MCRRuleMapping accessdata);
056
057 public abstract MCRRuleMapping getAccessDefinition(String pool, String objid);
058
059 public abstract Collection<String> getMappedObjectId(String pool); // ArrayList with ObjID's as String
060
061 public abstract Collection<String> getPoolsForObject(String objid); // ArrayList with pools as String
062
063 public abstract Collection<String> getDatabasePools();
064
065 public abstract boolean existsRule(String objid, String pool);
066
067 /**
068 *
069 * @return a collection of all String IDs an access rule is assigned to
070 */
071 public abstract Collection<String> getDistinctStringIDs();
072
073 public static Logger logger = Logger.getLogger(MCRAccessStore.class.getName());
074
075 final protected static String sqlDateformat = "yyyy-MM-dd HH:mm:ss";
076
077 final protected static String SQLAccessCtrlRule = MCRConfiguration.instance().getString("MCR.Persistence.Access.Store.Table.Rule", "MCRACCESSRULE");
078
079 final protected static String SQLAccessCtrlMapping = MCRConfiguration.instance().getString("MCR.Persistence.Access.Store.Table.Map", "MCRACCESS");
080
081 final protected static String AccessPools = MCRConfiguration.instance().getString("MCR.AccessPools", "read,write,delete");
082
083 static private MCRAccessStore implementation;
084
085 public static MCRAccessStore getInstance() {
086 try {
087 if (implementation == null) {
088 implementation = (MCRAccessStore) MCRConfiguration.instance().getSingleInstanceOf("MCR.Persistence.Access.Store.Class",
089 "org.mycore.backend.hibernate.MCRHIBAccessStore");
090 }
091 } catch (Exception e) {
092 logger.error(e);
093 }
094
095 return implementation;
096 }
097
098 public static Collection<String> getPools() {
099 String[] pool = AccessPools.split(",");
100 return Arrays.asList(pool);
101 }
102
103 /**
104 * alle Elemente eines Datentypes aufbereiten
105 * @param type document type
106 *
107 * @return List of MCRAccessDefinition
108 * @see MCRAccessDefinition
109 */
110 public Collection<MCRAccessDefinition> getDefinition(String type) {
111 try {
112 HashMap<String, Collection<String>> sqlDefinition = new HashMap<String, Collection<String>>();
113 Collection<String> pools = MCRAccessStore.getInstance().getDatabasePools();
114 //merge pools
115 pools.removeAll(getPools());
116 pools.addAll(getPools());
117
118 for (String pool : pools) {
119 sqlDefinition.put(pool, MCRAccessStore.getInstance().getMappedObjectId(pool));
120 }
121
122 Collection<MCRAccessDefinition> ret = new LinkedList<MCRAccessDefinition>();
123 Collection<String> elements;
124 MCRAccessDefinition def = null;
125
126 if (MCRConfiguration.instance().getBoolean("MCR.Metadata.Type." + type)) {
127 elements = MCRXMLTableManager.instance().retrieveAllIDs(type);
128 } else
129 return Collections.emptySet();
130
131 for (String element : elements) {
132 def = new MCRAccessDefinition();
133 def.setObjID(element);
134 for (String pool : pools) {
135 Collection<String> l = sqlDefinition.get(pool);
136 if (l.contains(element)) {
137 def.addPool(pool, "X");
138 } else {
139 def.addPool(pool, " ");
140 }
141 }
142 ret.add(def);
143 }
144 return ret;
145 } catch (Exception e) {
146 logger.error("definition loading failed: ", e);
147 return null;
148 }
149 }
150
151 public Collection<MCRAccessDefinition> getRules(String objid) {
152 try {
153 Collection<String> pools = MCRAccessStore.getInstance().getDatabasePools();
154 //merge pools
155 pools.removeAll(getPools());
156 pools.addAll(getPools());
157
158 Collection<MCRAccessDefinition> ret = new LinkedList<MCRAccessDefinition>();
159 //List elements = new LinkedList();
160 MCRAccessDefinition def = new MCRAccessDefinition();
161 def.setObjID(objid);
162 for (String pool : pools) {
163 String rule = getRuleID(objid, pool);
164 if (rule != null) {
165 def.addPool(pool, rule);
166 } else {
167 def.addPool(pool, " ");
168 }
169 }
170 ret.add(def);
171 return ret;
172 } catch (Exception e) {
173 logger.error("definition loading failed: ");
174 return null;
175 }
176 }
177
178 }