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.datamodel.common;
20
21 import java.io.IOException;
22 import java.util.Collection;
23 import java.util.Date;
24 import java.util.List;
25 import java.util.concurrent.TimeUnit;
26
27 import org.jdom2.Document;
28 import org.jdom2.JDOMException;
29 import org.mycore.common.MCRCache;
30 import org.mycore.common.MCRPersistenceException;
31 import org.mycore.common.config.MCRConfiguration2;
32 import org.mycore.common.config.MCRConfigurationBase;
33 import org.mycore.common.content.MCRByteContent;
34 import org.mycore.common.content.MCRContent;
35 import org.mycore.common.content.MCRJDOMContent;
36 import org.mycore.datamodel.metadata.MCRObjectID;
37 import org.xml.sax.SAXException;
38
39 /**
40 * Provides an adapter to communicate with the configured {@link MCRXMLMetadataManagerAdapter} implementation.
41 *
42 * @author Christoph Neidahl (OPNA2608)
43 */
44 public class MCRXMLMetadataManager {
45
46 /**
47 * Our own singleton.
48 */
49 private static MCRXMLMetadataManager SINGLETON;
50
51 /**
52 * The implementation's singleton.
53 */
54 private static MCRXMLMetadataManagerAdapter IMPLEMENTATION;
55
56 /**
57 * Reads the MCR.Metadata.Manager.Class to instantiate and return the configured xml metadata manager.
58 * If MCR.Metadata.Manager.Class is not set, an instance of {@link MCRDefaultXMLMetadataManager} is returned.
59 *
60 * @return an instance of the configured xml metadata manager if any is set, or MCRDefaultXMLMetadataManager
61 */
62 public static synchronized MCRXMLMetadataManager instance() {
63 if (SINGLETON == null) {
64 SINGLETON = new MCRXMLMetadataManager();
65 }
66 if (IMPLEMENTATION == null) {
67 IMPLEMENTATION = MCRConfiguration2
68 .getSingleInstanceOf("MCR.Metadata.Manager", MCRDefaultXMLMetadataManager.class).get();
69 }
70 return SINGLETON;
71 }
72
73 /*
74 * Delegations to IMPLEMENTATION
75 */
76
77 /**
78 * Delegation, see linked method for relevant documentation.
79 *
80 * @see MCRXMLMetadataManagerAdapter#reload()
81 */
82 public void reload() {
83 IMPLEMENTATION.reload();
84 }
85
86 /**
87 * Delegation, see linked method for relevant documentation.
88 *
89 * @param base
90 * @see MCRXMLMetadataManagerAdapter#verifyStore(String)
91 */
92 public void verifyStore(String base) {
93 IMPLEMENTATION.verifyStore(base);
94 }
95
96 /**
97 * Delegation, see linked method for relevant documentation.
98 *
99 * @param mcrid
100 * @param xml
101 * @param lastModified
102 * @throws MCRPersistenceException
103 * @see MCRXMLMetadataManagerAdapter#create(MCRObjectID, MCRContent, Date)
104 */
105 public void create(MCRObjectID mcrid, MCRContent xml, Date lastModified)
106 throws MCRPersistenceException {
107 IMPLEMENTATION.create(mcrid, xml, lastModified);
108 }
109
110 /**
111 * Delegation, see linked method for relevant documentation.
112 *
113 * @param mcrid
114 * @throws MCRPersistenceException
115 * @see MCRXMLMetadataManagerAdapter#delete(MCRObjectID)
116 */
117 public void delete(MCRObjectID mcrid) throws MCRPersistenceException {
118 IMPLEMENTATION.delete(mcrid);
119 }
120
121 /**
122 * Delegation, see linked method for relevant documentation.
123 *
124 * @param mcrid
125 * @param xml
126 * @param lastModified
127 * @see MCRXMLMetadataManagerAdapter#update(MCRObjectID, MCRContent, Date)
128 */
129 public void update(MCRObjectID mcrid, MCRContent xml, Date lastModified)
130 throws MCRPersistenceException {
131 IMPLEMENTATION.update(mcrid, xml, lastModified);
132 }
133
134 /**
135 * Delegation, see linked method for relevant documentation.
136 *
137 * @param mcrid
138 * @return
139 * @throws IOException
140 * @see MCRXMLMetadataManagerAdapter#retrieveContent(MCRObjectID)
141 */
142 public MCRContent retrieveContent(MCRObjectID mcrid) throws IOException {
143 return IMPLEMENTATION.retrieveContent(mcrid);
144 }
145
146 /**
147 * Delegation, see linked method for relevant documentation.
148 *
149 * @param mcrid
150 * @param revision
151 * @return
152 * @throws IOException
153 * @see MCRXMLMetadataManagerAdapter#retrieveContent(MCRObjectID, String)
154 */
155 public MCRContent retrieveContent(MCRObjectID mcrid, String revision) throws IOException {
156 return IMPLEMENTATION.retrieveContent(mcrid, revision);
157 }
158
159 /**
160 * Delegation, see linked method for relevant documentation.
161 *
162 * @param id
163 * @return
164 * @see MCRXMLMetadataManagerAdapter#listRevisions(MCRObjectID)
165 */
166 public List<? extends MCRAbstractMetadataVersion<?>> listRevisions(MCRObjectID id) throws IOException {
167 return IMPLEMENTATION.listRevisions(id);
168 }
169
170 /**
171 * Delegation, see linked method for relevant documentation.
172 *
173 * @param project
174 * @param type
175 * @exception MCRPersistenceException
176 * @return
177 * @see MCRXMLMetadataManagerAdapter#getHighestStoredID(String, String)
178 */
179 public int getHighestStoredID(String project, String type) {
180 return IMPLEMENTATION.getHighestStoredID(project, type);
181 }
182
183 /**
184 * Delegation, see linked method for relevant documentation.
185 *
186 * @param mcrid
187 * @return
188 * @throws MCRPersistenceException
189 * @see MCRXMLMetadataManagerAdapter#exists(MCRObjectID)
190 */
191 public boolean exists(MCRObjectID mcrid) throws MCRPersistenceException {
192 return IMPLEMENTATION.exists(mcrid);
193 }
194
195 /**
196 * Delegation, see linked method for relevant documentation.
197 *
198 * @param base
199 * @return
200 * @see MCRXMLMetadataManagerAdapter#listIDsForBase(String)
201 */
202 public List<String> listIDsForBase(String base) {
203 return IMPLEMENTATION.listIDsForBase(base);
204 }
205
206 /**
207 * Delegation, see linked method for relevant documentation.
208 *
209 * @param type
210 * @return
211 * @see MCRXMLMetadataManagerAdapter#listIDsOfType(String)
212 */
213 public List<String> listIDsOfType(String type) {
214 return IMPLEMENTATION.listIDsOfType(type);
215 }
216
217 /**
218 * Delegation, see linked method for relevant documentation.
219 *
220 * @return
221 * @see MCRXMLMetadataManagerAdapter#listIDs()
222 */
223 public List<String> listIDs() {
224 return IMPLEMENTATION.listIDs();
225 }
226
227 /**
228 * Delegation, see linked method for relevant documentation.
229 *
230 * @return
231 * @see MCRXMLMetadataManagerAdapter#getObjectTypes()
232 */
233 public Collection<String> getObjectTypes() {
234 return IMPLEMENTATION.getObjectTypes();
235 }
236
237 /**
238 * Delegation, see linked method for relevant documentation.
239 *
240 * @return
241 * @see MCRXMLMetadataManagerAdapter#getObjectBaseIds()
242 */
243 public Collection<String> getObjectBaseIds() {
244 return IMPLEMENTATION.getObjectBaseIds();
245 }
246
247 /**
248 * Delegation, see linked method for relevant documentation.
249 *
250 * @param ids
251 * @throws IOException
252 * @see MCRXMLMetadataManagerAdapter#retrieveObjectDates(List)
253 */
254 public List<MCRObjectIDDate> retrieveObjectDates(List<String> ids) throws IOException {
255 return IMPLEMENTATION.retrieveObjectDates(ids);
256 }
257
258 /**
259 * Delegation, see linked method for relevant documentation.
260 *
261 * @param id
262 * @return
263 * @throws IOException
264 * @see MCRXMLMetadataManagerAdapter#getLastModified(MCRObjectID)
265 */
266 public long getLastModified(MCRObjectID id) throws IOException {
267 return IMPLEMENTATION.getLastModified(id);
268 }
269
270 /**
271 * Delegation, see linked method for relevant documentation.
272 *
273 * @param id
274 * @param expire
275 * @param unit
276 * @return
277 * @see MCRXMLMetadataManagerAdapter#getLastModifiedHandle(MCRObjectID, long, TimeUnit)
278 */
279 public MCRCache.ModifiedHandle getLastModifiedHandle(MCRObjectID id, long expire, TimeUnit unit) {
280 return IMPLEMENTATION.getLastModifiedHandle(id, expire, unit);
281 }
282
283 /*
284 * Redirections/wrappers to delegations
285 */
286
287 /**
288 * Stores metadata of a new MCRObject in the persistent store.
289 *
290 * @param mcrid the MCRObjectID
291 * @param xml the xml metadata of the MCRObject
292 * @param lastModified the date of last modification to set
293 * @throws MCRPersistenceException the object couldn't be created due persistence problems
294 */
295 public void create(MCRObjectID mcrid, Document xml, Date lastModified)
296 throws MCRPersistenceException {
297 create(mcrid, new MCRJDOMContent(xml), lastModified);
298 }
299
300 /**
301 * Stores metadata of a new MCRObject in the persistent store.
302 *
303 * @param mcrid the MCRObjectID
304 * @param xml the xml metadata of the MCRObject
305 * @param lastModified the date of last modification to set
306 * @throws MCRPersistenceException the object couldn't be created due persistence problems
307 */
308 public void create(MCRObjectID mcrid, byte[] xml, Date lastModified) throws MCRPersistenceException {
309 create(mcrid, new MCRByteContent(xml, lastModified.getTime()), lastModified);
310 }
311
312 public void delete(String mcrid) throws MCRPersistenceException {
313 delete(MCRObjectID.getInstance(mcrid));
314 }
315
316 /**
317 * Updates metadata of existing MCRObject in the persistent store.
318 *
319 * @param mcrid the MCRObjectID
320 * @param xml the xml metadata of the MCRObject
321 * @param lastModified the date of last modification to set
322 * @throws MCRPersistenceException the object couldn't be updated due persistence problems
323 */
324 public void update(MCRObjectID mcrid, Document xml, Date lastModified)
325 throws MCRPersistenceException {
326 update(mcrid, new MCRJDOMContent(xml), lastModified);
327 }
328
329 /**
330 * Creates or updates metadata of a MCRObject in the persistent store.
331 *
332 * @param mcrid the MCRObjectID
333 * @param xml the xml metadata of the MCRObject
334 * @param lastModified the date of last modification to set
335 * @throws MCRPersistenceException the object couldn't be created or updated due persistence problems
336 */
337 public void createOrUpdate(MCRObjectID mcrid, Document xml, Date lastModified)
338 throws MCRPersistenceException {
339 if (exists(mcrid)) {
340 update(mcrid, xml, lastModified);
341 } else {
342 create(mcrid, xml, lastModified);
343 }
344 }
345
346 /**
347 * Updates metadata of existing MCRObject in the persistent store.
348 *
349 * @param mcrid the MCRObjectID
350 * @param xml the xml metadata of the MCRObject
351 * @param lastModified the date of last modification to set
352 * @throws MCRPersistenceException the object couldn't be updated due persistence problems
353 */
354 public void update(MCRObjectID mcrid, byte[] xml, Date lastModified) throws MCRPersistenceException {
355 update(mcrid, new MCRByteContent(xml, lastModified.getTime()), lastModified);
356 }
357
358 /**
359 * Retrieves stored metadata xml as JDOM document
360 *
361 * @param mcrid the MCRObjectID
362 * @return null if metadata is not present
363 */
364 public Document retrieveXML(MCRObjectID mcrid) throws IOException, JDOMException, SAXException {
365 MCRContent metadata = retrieveContent(mcrid);
366 return metadata == null ? null : metadata.asXML();
367 }
368
369 /**
370 * Retrieves stored metadata xml as byte[] BLOB.
371 *
372 * @param mcrid the MCRObjectID
373 * @return null if metadata is not present
374 */
375 public byte[] retrieveBLOB(MCRObjectID mcrid) throws IOException {
376 MCRContent metadata = retrieveContent(mcrid);
377 return metadata == null ? null : metadata.asByteArray();
378 }
379
380 /**
381 * Lists all objects with their last modification dates.
382 *
383 * @return List of {@link MCRObjectIDDate}
384 * @throws IOException
385 */
386 public List<MCRObjectIDDate> listObjectDates() throws IOException {
387 return retrieveObjectDates(this.listIDs());
388 }
389
390 /**
391 * Lists all objects of the specified <code>type</code> and their last modified date.
392 *
393 * @param type type of object
394 * @throws IOException
395 */
396 public List<MCRObjectIDDate> listObjectDates(String type) throws IOException {
397 return retrieveObjectDates(this.listIDsOfType(type));
398 }
399
400 /**
401 * Returns the time the store's content was last modified
402 *
403 * @return Last modification date of the MyCoRe system
404 */
405 public long getLastModified() {
406 return MCRConfigurationBase.getSystemLastModified();
407 }
408 }