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.client.PoxPayloadIn;
32 import org.collectionspace.services.client.PoxPayloadOut;
33 import org.collectionspace.services.common.api.RefName;
34 import org.collectionspace.services.common.context.ServiceContext;
35 import org.collectionspace.services.common.query.QueryContext;
36 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.Specifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * AbstractDocumentHandler
44 * $LastChangedRevision: $
51 public abstract class AbstractDocumentHandlerImpl<T, TL, WT, WTL>
52 implements DocumentHandler<T, TL, WT, WTL> {
55 private final Logger logger = LoggerFactory.getLogger(AbstractDocumentHandlerImpl.class);
57 /** The properties. */
58 private Map<String, Object> properties = new HashMap<String, Object>();
60 /** The doc filter. */
61 private DocumentFilter docFilter = null;
63 /** The service context. */
64 private ServiceContext serviceContext;
67 * Instantiates a new abstract document handler impl.
69 public AbstractDocumentHandlerImpl() {
73 abstract protected String getRefnameDisplayName(DocumentWrapper<WT> docWrapper);
76 * Should return a reference name for the wrapper object
78 abstract protected RefName.RefNameInterface getRefName(DocumentWrapper<WT> docWrapper, String tenantName, String serviceName);
81 * @see org.collectionspace.services.common.document.DocumentHandler#getServiceContext()
84 public ServiceContext getServiceContext() {
85 return serviceContext;
89 * @see org.collectionspace.services.common.document.DocumentHandler#setServiceContext(org.collectionspace.services.common.context.ServiceContext)
92 public void setServiceContext(ServiceContext ctx) {
97 * @return the properties
100 public Map<String, Object> getProperties() {
105 * @param properties the properties to set
108 public void setProperties(Map<String, Object> properties) {
109 this.properties = properties;
112 // public void initializeDocumentFilter(ServiceContext ctx) {
113 // DocumentFilter docFilter = this.createDocumentFilter(ctx);
114 // this.setDocumentFilter(docFilter);
117 * @see org.collectionspace.services.common.document.DocumentHandler#createDocumentFilter()
120 public abstract DocumentFilter createDocumentFilter();
123 * @return the DocumentFilter
126 public DocumentFilter getDocumentFilter() {
131 * @param properties the DocumentFilter to set
134 public void setDocumentFilter(DocumentFilter docFilter) {
135 this.docFilter = docFilter;
139 * @see org.collectionspace.services.common.document.DocumentHandler#prepare(org.collectionspace.services.common.document.DocumentHandler.Action)
142 final public void prepare(Action action) throws Exception {
172 logger.error("Should never get to this code path. If you did, there is a bug in the code.");
177 logger.error("Should never get to this code path. If you did, there is a bug in the code.");
184 * @see org.collectionspace.services.common.document.DocumentHandler#prepareCreate()
187 public void prepareCreate() throws Exception {
191 * @see org.collectionspace.services.common.document.DocumentHandler#prepareUpdate()
194 public void prepareUpdate() throws Exception {
198 * @see org.collectionspace.services.common.document.DocumentHandler#prepareGet()
201 public void prepareGet() throws Exception {
205 * @see org.collectionspace.services.common.document.DocumentHandler#prepareGetAll()
208 public void prepareGetAll() throws Exception {
212 * @see org.collectionspace.services.common.document.DocumentHandler#prepareDelete()
215 public void prepareDelete() throws Exception {
219 * @see org.collectionspace.services.common.document.DocumentHandler#prepareDelete()
222 public void prepareSync() throws Exception {
223 // Do nothing. Subclasses can override if they want/need to.
227 * @see org.collectionspace.services.common.document.DocumentHandler#handle(org.collectionspace.services.common.document.DocumentHandler.Action, org.collectionspace.services.common.document.DocumentWrapper)
230 final public boolean handle(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
231 boolean result = true;
235 handleCreate((DocumentWrapper<WT>) wrapDoc);
239 handleUpdate((DocumentWrapper<WT>) wrapDoc);
243 handleGet((DocumentWrapper<WT>) wrapDoc);
247 handleGetAll((DocumentWrapper<WTL>) wrapDoc);
251 result = handleDelete((DocumentWrapper<WT>) wrapDoc);
255 result = handleSync((DocumentWrapper<Object>) wrapDoc);
259 logger.error("Should never get to this code path. If you did, there is a bug in the code.");
264 logger.error("Should never get to this code path. If you did, there is a bug in the code.");
273 * @see org.collectionspace.services.common.document.DocumentHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
276 public abstract void handleCreate(DocumentWrapper<WT> wrapDoc) throws Exception;
279 * @see org.collectionspace.services.common.document.DocumentHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
282 public abstract void handleUpdate(DocumentWrapper<WT> wrapDoc) throws Exception;
285 * @see org.collectionspace.services.common.document.DocumentHandler#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
288 public abstract void handleGet(DocumentWrapper<WT> wrapDoc) throws Exception;
291 * @see org.collectionspace.services.common.document.DocumentHandler#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
294 public abstract void handleGetAll(DocumentWrapper<WTL> wrapDoc) throws Exception;
297 * @see org.collectionspace.services.common.document.DocumentHandler#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
300 public boolean handleDelete(DocumentWrapper<WT> wrapDoc) throws Exception {
305 * @see org.collectionspace.services.common.document.DocumentHandler#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
308 public boolean handleSync(DocumentWrapper<Object> wrapDoc) throws Exception {
309 // Do nothing. Subclasses can override if they want/need to.
315 * @see org.collectionspace.services.common.document.DocumentHandler#complete(org.collectionspace.services.common.document.DocumentHandler.Action, org.collectionspace.services.common.document.DocumentWrapper)
318 final public void complete(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
321 completeCreate((DocumentWrapper<WT>) wrapDoc);
325 completeUpdate((DocumentWrapper<WT>) wrapDoc);
329 completeGet((DocumentWrapper<WT>) wrapDoc);
333 completeGetAll((DocumentWrapper<WTL>) wrapDoc);
337 completeDelete((DocumentWrapper<WT>) wrapDoc);
341 completeSync((DocumentWrapper<Object>) wrapDoc);
345 logger.error("Should never get to this code path. If you did, there is a bug in the code.");
350 logger.error("Should never get to this code path. If you did, there is a bug in the code.");
357 * @see org.collectionspace.services.common.document.DocumentHandler#completeCreate(org.collectionspace.services.common.document.DocumentWrapper)
360 public void completeCreate(DocumentWrapper<WT> wrapDoc) throws Exception {
364 * @see org.collectionspace.services.common.document.DocumentHandler#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
367 public void completeUpdate(DocumentWrapper<WT> wrapDoc) throws Exception {
368 //no specific action needed
372 * @see org.collectionspace.services.common.document.DocumentHandler#completeGet(org.collectionspace.services.common.document.DocumentWrapper)
375 public void completeGet(DocumentWrapper<WT> wrapDoc) throws Exception {
379 * @see org.collectionspace.services.common.document.DocumentHandler#completeGetAll(org.collectionspace.services.common.document.DocumentWrapper)
382 public void completeGetAll(DocumentWrapper<WTL> wrapDoc) throws Exception {
386 * @see org.collectionspace.services.common.document.DocumentHandler#completeDelete(org.collectionspace.services.common.document.DocumentWrapper)
389 public void completeDelete(DocumentWrapper<WT> wrapDoc) throws Exception {
393 * @see org.collectionspace.services.common.document.DocumentHandler#completeDelete(org.collectionspace.services.common.document.DocumentWrapper)
396 public void completeSync(DocumentWrapper<Object> wrapDoc) throws Exception {
400 * @see org.collectionspace.services.common.document.DocumentHandler#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
403 public abstract T extractCommonPart(DocumentWrapper<WT> wrapDoc)
407 * @see org.collectionspace.services.common.document.DocumentHandler#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
410 public abstract void fillCommonPart(T obj, DocumentWrapper<WT> wrapDoc)
414 * @see org.collectionspace.services.common.document.DocumentHandler#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
417 public abstract TL extractCommonPartList(DocumentWrapper<WTL> wrapDoc)
421 * @see org.collectionspace.services.common.document.DocumentHandler#fillCommonPartList(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
424 final public void fillCommonPartList(TL obj, DocumentWrapper<WTL> wrapDoc) throws Exception {
425 throw new UnsupportedOperationException("bulk create/update not yet supported");
429 * @see org.collectionspace.services.common.document.DocumentHandler#getCommonPart()
432 public abstract T getCommonPart();
435 * @see org.collectionspace.services.common.document.DocumentHandler#setCommonPart(java.lang.Object)
438 public abstract void setCommonPart(T obj);
441 * @see org.collectionspace.services.common.document.DocumentHandler#getCommonPartList()
444 public abstract TL getCommonPartList();
447 * @see org.collectionspace.services.common.document.DocumentHandler#setCommonPartList(java.lang.Object)
450 public abstract void setCommonPartList(TL obj);
453 * @see org.collectionspace.services.common.document.DocumentHandler#getQProperty(java.lang.String)
456 public abstract String getQProperty(String prop) throws DocumentException;
459 * Strip Nuxeo's schema name from the start of the field / element name.
461 * @see org.collectionspace.services.common.document.DocumentHandler#getUnQProperty(java.lang.String)
464 public String getUnQProperty(String qProp) {
465 StringTokenizer tkz = new StringTokenizer(qProp, ":");
466 if (tkz.countTokens() != 2) {
467 String msg = "Property must be in the form xxx:yyy, "
468 + "e.g. collectionobjects_common:objectNumber";
470 throw new IllegalArgumentException(msg);
472 tkz.nextToken(); //skip
473 return tkz.nextToken();
479 * @throws DocumentException
482 public String getDocumentsToIndexQuery(String indexId, String csid) throws DocumentException, Exception {
487 * @see org.collectionspace.services.common.document.DocumentHandler#getServiceContextPath()
490 public String getServiceContextPath() {
491 return "/" + getServiceContext().getServiceName().toLowerCase() + "/";
497 * @param action the action
498 * @throws Exception the exception
500 private void validate(Action action) throws Exception {
501 List<ValidatorHandler> valHandlers = serviceContext.getValidatorHandlers();
502 for (ValidatorHandler handler : valHandlers) {
503 handler.validate(action, serviceContext);
508 * Creates the CMIS query from the service context. Each document handler is responsible for returning a valid CMIS query using the
509 * information in the current service context -which includes things like the query parameters, etc.
510 * @throws DocumentException
513 public String getCMISQuery(QueryContext queryContext) throws DocumentException {
515 // By default, return nothing. Child classes can override if they want.
521 public boolean isCMISQuery() {
526 public boolean isJDBCQuery() {
531 public Map<String,String> getJDBCQueryParams() {
532 return new HashMap<>();