2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
24 package org.collectionspace.services.common.document;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.StringTokenizer;
31 import org.collectionspace.services.common.api.RefName;
32 import org.collectionspace.services.common.context.ServiceContext;
33 import org.collectionspace.services.common.query.QueryContext;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * AbstractDocumentHandler
41 * $LastChangedRevision: $
48 public abstract class AbstractDocumentHandlerImpl<T, TL, WT, WTL>
49 implements DocumentHandler<T, TL, WT, WTL> {
52 private final Logger logger = LoggerFactory.getLogger(AbstractDocumentHandlerImpl.class);
54 /** The properties. */
55 private Map<String, Object> properties = new HashMap<String, Object>();
57 /** The doc filter. */
58 private DocumentFilter docFilter = null;
60 /** The service context. */
61 private ServiceContext serviceContext;
64 * Instantiates a new abstract document handler impl.
66 public AbstractDocumentHandlerImpl() {
70 abstract protected String getRefnameDisplayName(DocumentWrapper<WT> docWrapper);
73 * Should return a reference name for the wrapper object
75 abstract protected RefName.RefNameInterface getRefName(DocumentWrapper<WT> docWrapper, String tenantName, String serviceName);
78 * @see org.collectionspace.services.common.document.DocumentHandler#getServiceContext()
81 public ServiceContext getServiceContext() {
82 return serviceContext;
86 * @see org.collectionspace.services.common.document.DocumentHandler#setServiceContext(org.collectionspace.services.common.context.ServiceContext)
89 public void setServiceContext(ServiceContext ctx) {
94 * @return the properties
97 public Map<String, Object> getProperties() {
102 * @param properties the properties to set
105 public void setProperties(Map<String, Object> properties) {
106 this.properties = properties;
109 // public void initializeDocumentFilter(ServiceContext ctx) {
110 // DocumentFilter docFilter = this.createDocumentFilter(ctx);
111 // this.setDocumentFilter(docFilter);
114 * @see org.collectionspace.services.common.document.DocumentHandler#createDocumentFilter()
117 public abstract DocumentFilter createDocumentFilter();
120 * @return the DocumentFilter
123 public DocumentFilter getDocumentFilter() {
128 * @param properties the DocumentFilter to set
131 public void setDocumentFilter(DocumentFilter docFilter) {
132 this.docFilter = docFilter;
136 * @see org.collectionspace.services.common.document.DocumentHandler#prepare(org.collectionspace.services.common.document.DocumentHandler.Action)
139 final public void prepare(Action action) throws Exception {
169 logger.error("Should never get to this code path. If you did, there is a bug in the code.");
174 logger.error("Should never get to this code path. If you did, there is a bug in the code.");
181 * @see org.collectionspace.services.common.document.DocumentHandler#prepareCreate()
184 public void prepareCreate() throws Exception {
188 * @see org.collectionspace.services.common.document.DocumentHandler#prepareUpdate()
191 public void prepareUpdate() throws Exception {
195 * @see org.collectionspace.services.common.document.DocumentHandler#prepareGet()
198 public void prepareGet() throws Exception {
202 * @see org.collectionspace.services.common.document.DocumentHandler#prepareGetAll()
205 public void prepareGetAll() throws Exception {
209 * @see org.collectionspace.services.common.document.DocumentHandler#prepareDelete()
212 public void prepareDelete() throws Exception {
216 * @see org.collectionspace.services.common.document.DocumentHandler#prepareDelete()
219 public void prepareSync() throws Exception {
220 // Do nothing. Subclasses can override if they want/need to.
224 * @see org.collectionspace.services.common.document.DocumentHandler#handle(org.collectionspace.services.common.document.DocumentHandler.Action, org.collectionspace.services.common.document.DocumentWrapper)
227 final public boolean handle(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
228 boolean result = true;
232 handleCreate((DocumentWrapper<WT>) wrapDoc);
236 handleUpdate((DocumentWrapper<WT>) wrapDoc);
240 handleGet((DocumentWrapper<WT>) wrapDoc);
244 handleGetAll((DocumentWrapper<WTL>) wrapDoc);
248 result = handleDelete((DocumentWrapper<WT>) wrapDoc);
252 result = handleSync((DocumentWrapper<Object>) wrapDoc);
256 logger.error("Should never get to this code path. If you did, there is a bug in the code.");
261 logger.error("Should never get to this code path. If you did, there is a bug in the code.");
270 * @see org.collectionspace.services.common.document.DocumentHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
273 public abstract void handleCreate(DocumentWrapper<WT> wrapDoc) throws Exception;
276 * @see org.collectionspace.services.common.document.DocumentHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
279 public abstract void handleUpdate(DocumentWrapper<WT> wrapDoc) throws Exception;
282 * @see org.collectionspace.services.common.document.DocumentHandler#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
285 public abstract void handleGet(DocumentWrapper<WT> wrapDoc) throws Exception;
288 * @see org.collectionspace.services.common.document.DocumentHandler#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
291 public abstract void handleGetAll(DocumentWrapper<WTL> wrapDoc) throws Exception;
294 * @see org.collectionspace.services.common.document.DocumentHandler#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
297 public boolean handleDelete(DocumentWrapper<WT> wrapDoc) throws Exception {
302 * @see org.collectionspace.services.common.document.DocumentHandler#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
305 public boolean handleSync(DocumentWrapper<Object> wrapDoc) throws Exception {
306 // Do nothing. Subclasses can override if they want/need to.
312 * @see org.collectionspace.services.common.document.DocumentHandler#complete(org.collectionspace.services.common.document.DocumentHandler.Action, org.collectionspace.services.common.document.DocumentWrapper)
315 final public void complete(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
318 completeCreate((DocumentWrapper<WT>) wrapDoc);
322 completeUpdate((DocumentWrapper<WT>) wrapDoc);
326 completeGet((DocumentWrapper<WT>) wrapDoc);
330 completeGetAll((DocumentWrapper<WTL>) wrapDoc);
334 completeDelete((DocumentWrapper<WT>) wrapDoc);
338 completeSync((DocumentWrapper<Object>) wrapDoc);
342 logger.error("Should never get to this code path. If you did, there is a bug in the code.");
347 logger.error("Should never get to this code path. If you did, there is a bug in the code.");
354 * @see org.collectionspace.services.common.document.DocumentHandler#completeCreate(org.collectionspace.services.common.document.DocumentWrapper)
357 public void completeCreate(DocumentWrapper<WT> wrapDoc) throws Exception {
361 * @see org.collectionspace.services.common.document.DocumentHandler#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
364 public void completeUpdate(DocumentWrapper<WT> wrapDoc) throws Exception {
365 //no specific action needed
369 * @see org.collectionspace.services.common.document.DocumentHandler#completeGet(org.collectionspace.services.common.document.DocumentWrapper)
372 public void completeGet(DocumentWrapper<WT> wrapDoc) throws Exception {
376 * @see org.collectionspace.services.common.document.DocumentHandler#completeGetAll(org.collectionspace.services.common.document.DocumentWrapper)
379 public void completeGetAll(DocumentWrapper<WTL> wrapDoc) throws Exception {
383 * @see org.collectionspace.services.common.document.DocumentHandler#completeDelete(org.collectionspace.services.common.document.DocumentWrapper)
386 public void completeDelete(DocumentWrapper<WT> wrapDoc) throws Exception {
390 * @see org.collectionspace.services.common.document.DocumentHandler#completeDelete(org.collectionspace.services.common.document.DocumentWrapper)
393 public void completeSync(DocumentWrapper<Object> wrapDoc) throws Exception {
397 * @see org.collectionspace.services.common.document.DocumentHandler#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
400 public abstract T extractCommonPart(DocumentWrapper<WT> wrapDoc)
404 * @see org.collectionspace.services.common.document.DocumentHandler#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
407 public abstract void fillCommonPart(T obj, DocumentWrapper<WT> wrapDoc)
411 * @see org.collectionspace.services.common.document.DocumentHandler#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
414 public abstract TL extractCommonPartList(DocumentWrapper<WTL> wrapDoc)
418 * @see org.collectionspace.services.common.document.DocumentHandler#fillCommonPartList(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
421 final public void fillCommonPartList(TL obj, DocumentWrapper<WTL> wrapDoc) throws Exception {
422 throw new UnsupportedOperationException("bulk create/update not yet supported");
426 * @see org.collectionspace.services.common.document.DocumentHandler#getCommonPart()
429 public abstract T getCommonPart();
432 * @see org.collectionspace.services.common.document.DocumentHandler#setCommonPart(java.lang.Object)
435 public abstract void setCommonPart(T obj);
438 * @see org.collectionspace.services.common.document.DocumentHandler#getCommonPartList()
441 public abstract TL getCommonPartList();
444 * @see org.collectionspace.services.common.document.DocumentHandler#setCommonPartList(java.lang.Object)
447 public abstract void setCommonPartList(TL obj);
450 * @see org.collectionspace.services.common.document.DocumentHandler#getQProperty(java.lang.String)
453 public abstract String getQProperty(String prop) throws DocumentException;
456 * Strip Nuxeo's schema name from the start of the field / element name.
458 * @see org.collectionspace.services.common.document.DocumentHandler#getUnQProperty(java.lang.String)
461 public String getUnQProperty(String qProp) {
462 StringTokenizer tkz = new StringTokenizer(qProp, ":");
463 if (tkz.countTokens() != 2) {
464 String msg = "Property must be in the form xxx:yyy, "
465 + "e.g. collectionobjects_common:objectNumber";
467 throw new IllegalArgumentException(msg);
469 tkz.nextToken(); //skip
470 return tkz.nextToken();
476 * @throws DocumentException
479 public String getDocumentsToIndexQuery(String indexId, String csid) throws DocumentException, Exception {
484 * @see org.collectionspace.services.common.document.DocumentHandler#getServiceContextPath()
487 public String getServiceContextPath() {
488 return "/" + getServiceContext().getServiceName().toLowerCase() + "/";
494 * @param action the action
495 * @throws Exception the exception
497 private void validate(Action action) throws Exception {
498 List<ValidatorHandler> valHandlers = serviceContext.getValidatorHandlers();
499 for (ValidatorHandler handler : valHandlers) {
500 handler.validate(action, serviceContext);
505 * Creates the CMIS query from the service context. Each document handler is responsible for returning a valid CMIS query using the
506 * information in the current service context -which includes things like the query parameters, etc.
507 * @throws DocumentException
510 public String getCMISQuery(QueryContext queryContext) throws DocumentException {
512 // By default, return nothing. Child classes can override if they want.
518 public boolean isCMISQuery() {
523 public boolean isJDBCQuery() {
528 public Map<String,String> getJDBCQueryParams() {
529 return new HashMap<>();