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 @SuppressWarnings({"unchecked", "rawtypes"})
49 public abstract class AbstractDocumentHandlerImpl<T, TL, WT, WTL>
50 implements DocumentHandler<T, TL, WT, WTL> {
53 private final Logger logger = LoggerFactory.getLogger(AbstractDocumentHandlerImpl.class);
55 /** The properties. */
56 private Map<String, Object> properties = new HashMap<String, Object>();
58 /** The doc filter. */
59 private DocumentFilter docFilter = null;
61 /** The service context. */
62 private ServiceContext serviceContext;
65 * Instantiates a new abstract document handler impl.
67 public AbstractDocumentHandlerImpl() {
71 abstract protected String getRefnameDisplayName(DocumentWrapper<WT> docWrapper);
74 * Should return a reference name for the wrapper object
76 abstract protected RefName.RefNameInterface getRefName(DocumentWrapper<WT> docWrapper, String tenantName, String serviceName);
79 * @see org.collectionspace.services.common.document.DocumentHandler#getServiceContext()
82 public ServiceContext getServiceContext() {
83 return serviceContext;
87 * @see org.collectionspace.services.common.document.DocumentHandler#setServiceContext(org.collectionspace.services.common.context.ServiceContext)
90 public void setServiceContext(ServiceContext ctx) {
95 * @return the properties
98 public Map<String, Object> getProperties() {
103 * @param properties the properties to set
106 public void setProperties(Map<String, Object> properties) {
107 this.properties = properties;
110 // public void initializeDocumentFilter(ServiceContext ctx) {
111 // DocumentFilter docFilter = this.createDocumentFilter(ctx);
112 // this.setDocumentFilter(docFilter);
115 * @see org.collectionspace.services.common.document.DocumentHandler#createDocumentFilter()
118 public abstract DocumentFilter createDocumentFilter();
121 * @return the DocumentFilter
124 public DocumentFilter getDocumentFilter() {
129 * @param properties the DocumentFilter to set
132 public void setDocumentFilter(DocumentFilter docFilter) {
133 this.docFilter = docFilter;
137 * @see org.collectionspace.services.common.document.DocumentHandler#prepare(org.collectionspace.services.common.document.DocumentHandler.Action)
140 final public void prepare(Action action) throws Exception {
170 logger.error("Should never get to this code path. If you did, there is a bug in the code.");
175 logger.error("Should never get to this code path. If you did, there is a bug in the code.");
182 * @see org.collectionspace.services.common.document.DocumentHandler#prepareCreate()
185 public void prepareCreate() throws Exception {
189 * @see org.collectionspace.services.common.document.DocumentHandler#prepareUpdate()
192 public void prepareUpdate() throws Exception {
196 * @see org.collectionspace.services.common.document.DocumentHandler#prepareGet()
199 public void prepareGet() throws Exception {
203 * @see org.collectionspace.services.common.document.DocumentHandler#prepareGetAll()
206 public void prepareGetAll() throws Exception {
210 * @see org.collectionspace.services.common.document.DocumentHandler#prepareDelete()
213 public void prepareDelete() throws Exception {
217 * @see org.collectionspace.services.common.document.DocumentHandler#prepareDelete()
220 public void prepareSync() throws Exception {
221 // Do nothing. Subclasses can override if they want/need to.
225 * @see org.collectionspace.services.common.document.DocumentHandler#handle(org.collectionspace.services.common.document.DocumentHandler.Action, org.collectionspace.services.common.document.DocumentWrapper)
228 final public boolean handle(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
229 boolean result = true;
233 handleCreate((DocumentWrapper<WT>) wrapDoc);
237 handleUpdate((DocumentWrapper<WT>) wrapDoc);
241 handleGet((DocumentWrapper<WT>) wrapDoc);
245 handleGetAll((DocumentWrapper<WTL>) wrapDoc);
249 result = handleDelete((DocumentWrapper<WT>) wrapDoc);
253 result = handleSync((DocumentWrapper<Object>) wrapDoc);
257 logger.error("Should never get to this code path. If you did, there is a bug in the code.");
262 logger.error("Should never get to this code path. If you did, there is a bug in the code.");
271 public void sanitize(DocumentWrapper<WT> wrapDoc) {
273 // By default, do nothing. Sub-classes can override if they want to.
279 * @see org.collectionspace.services.common.document.DocumentHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
282 public abstract void handleCreate(DocumentWrapper<WT> wrapDoc) throws Exception;
285 * @see org.collectionspace.services.common.document.DocumentHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
288 public abstract void handleUpdate(DocumentWrapper<WT> wrapDoc) throws Exception;
291 * @see org.collectionspace.services.common.document.DocumentHandler#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
294 public abstract void handleGet(DocumentWrapper<WT> wrapDoc) throws Exception;
297 * @see org.collectionspace.services.common.document.DocumentHandler#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
300 public abstract void handleGetAll(DocumentWrapper<WTL> wrapDoc) throws Exception;
303 * @see org.collectionspace.services.common.document.DocumentHandler#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
306 public boolean handleDelete(DocumentWrapper<WT> wrapDoc) throws Exception {
311 * @see org.collectionspace.services.common.document.DocumentHandler#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
314 public boolean handleSync(DocumentWrapper<Object> wrapDoc) throws Exception {
315 // Do nothing. Subclasses can override if they want/need to.
321 * @see org.collectionspace.services.common.document.DocumentHandler#complete(org.collectionspace.services.common.document.DocumentHandler.Action, org.collectionspace.services.common.document.DocumentWrapper)
324 final public void complete(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
327 completeCreate((DocumentWrapper<WT>) wrapDoc);
331 completeUpdate((DocumentWrapper<WT>) wrapDoc);
335 completeGet((DocumentWrapper<WT>) wrapDoc);
339 completeGetAll((DocumentWrapper<WTL>) wrapDoc);
343 completeDelete((DocumentWrapper<WT>) wrapDoc);
347 completeSync((DocumentWrapper<Object>) wrapDoc);
351 logger.error("Should never get to this code path. If you did, there is a bug in the code.");
356 logger.error("Should never get to this code path. If you did, there is a bug in the code.");
363 * @see org.collectionspace.services.common.document.DocumentHandler#completeCreate(org.collectionspace.services.common.document.DocumentWrapper)
366 public void completeCreate(DocumentWrapper<WT> wrapDoc) throws Exception {
370 * @see org.collectionspace.services.common.document.DocumentHandler#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
373 public void completeUpdate(DocumentWrapper<WT> wrapDoc) throws Exception {
374 //no specific action needed
378 * @see org.collectionspace.services.common.document.DocumentHandler#completeGet(org.collectionspace.services.common.document.DocumentWrapper)
381 public void completeGet(DocumentWrapper<WT> wrapDoc) throws Exception {
385 * @see org.collectionspace.services.common.document.DocumentHandler#completeGetAll(org.collectionspace.services.common.document.DocumentWrapper)
388 public void completeGetAll(DocumentWrapper<WTL> wrapDoc) throws Exception {
392 * @see org.collectionspace.services.common.document.DocumentHandler#completeDelete(org.collectionspace.services.common.document.DocumentWrapper)
395 public void completeDelete(DocumentWrapper<WT> wrapDoc) throws Exception {
399 * @see org.collectionspace.services.common.document.DocumentHandler#completeDelete(org.collectionspace.services.common.document.DocumentWrapper)
402 public void completeSync(DocumentWrapper<Object> wrapDoc) throws Exception {
406 * @see org.collectionspace.services.common.document.DocumentHandler#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
409 public abstract T extractCommonPart(DocumentWrapper<WT> wrapDoc)
413 * @see org.collectionspace.services.common.document.DocumentHandler#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
416 public abstract void fillCommonPart(T obj, DocumentWrapper<WT> wrapDoc)
420 * @see org.collectionspace.services.common.document.DocumentHandler#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
423 public abstract TL extractCommonPartList(DocumentWrapper<WTL> wrapDoc)
427 * @see org.collectionspace.services.common.document.DocumentHandler#fillCommonPartList(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
430 final public void fillCommonPartList(TL obj, DocumentWrapper<WTL> wrapDoc) throws Exception {
431 throw new UnsupportedOperationException("bulk create/update not yet supported");
435 * @see org.collectionspace.services.common.document.DocumentHandler#getCommonPart()
438 public abstract T getCommonPart();
441 * @see org.collectionspace.services.common.document.DocumentHandler#setCommonPart(java.lang.Object)
444 public abstract void setCommonPart(T obj);
447 * @see org.collectionspace.services.common.document.DocumentHandler#getCommonPartList()
450 public abstract TL getCommonPartList();
453 * @see org.collectionspace.services.common.document.DocumentHandler#setCommonPartList(java.lang.Object)
456 public abstract void setCommonPartList(TL obj);
459 * @see org.collectionspace.services.common.document.DocumentHandler#getQProperty(java.lang.String)
462 public abstract String getQProperty(String prop) throws DocumentException;
465 * Strip Nuxeo's schema name from the start of the field / element name.
467 * @see org.collectionspace.services.common.document.DocumentHandler#getUnQProperty(java.lang.String)
470 public String getUnQProperty(String qProp) {
471 StringTokenizer tkz = new StringTokenizer(qProp, ":");
472 if (tkz.countTokens() != 2) {
473 String msg = "Property must be in the form xxx:yyy, "
474 + "e.g. collectionobjects_common:objectNumber";
476 throw new IllegalArgumentException(msg);
478 tkz.nextToken(); //skip
479 return tkz.nextToken();
485 * @throws DocumentException
488 public String getDocumentsToIndexQuery(String indexId, String csid) throws DocumentException, Exception {
493 * @see org.collectionspace.services.common.document.DocumentHandler#getServiceContextPath()
496 public String getServiceContextPath() {
497 return "/" + getServiceContext().getServiceName().toLowerCase() + "/";
503 * @param action the action
504 * @throws Exception the exception
506 private void validate(Action action) throws Exception {
507 List<ValidatorHandler> valHandlers = serviceContext.getValidatorHandlers();
508 for (ValidatorHandler handler : valHandlers) {
509 handler.validate(action, serviceContext);
514 * Creates the CMIS query from the service context. Each document handler is responsible for returning a valid CMIS query using the
515 * information in the current service context -which includes things like the query parameters, etc.
516 * @throws DocumentException
519 public String getCMISQuery(QueryContext queryContext) throws DocumentException {
521 // By default, return nothing. Child classes can override if they want.
527 public boolean isCMISQuery() {
532 public boolean isJDBCQuery() {
537 public Map<String,String> getJDBCQueryParams() {
538 return new HashMap<>();