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.nuxeo.client.java;
26 import java.util.List;
28 import org.collectionspace.services.client.PoxPayloadIn;
29 import org.collectionspace.services.client.PoxPayloadOut;
30 import org.collectionspace.services.common.authorityref.AuthorityRefList;
31 import org.collectionspace.services.common.context.ServiceContext;
32 import org.collectionspace.services.common.datetime.GregorianCalendarDateTimeUtils;
33 import org.collectionspace.services.common.document.AbstractMultipartDocumentHandlerImpl;
34 import org.collectionspace.services.common.document.DocumentFilter;
35 import org.collectionspace.services.common.document.DocumentWrapper;
36 import org.collectionspace.services.common.document.DocumentHandler.Action;
37 import org.collectionspace.services.nuxeo.client.*;
38 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
39 import org.collectionspace.services.common.profile.Profiler;
41 import org.nuxeo.ecm.core.api.ClientException;
42 import org.nuxeo.ecm.core.api.DocumentModel;
43 import org.nuxeo.ecm.core.api.DocumentModelList;
44 import org.nuxeo.ecm.core.api.model.PropertyException;
45 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * DocumentModelHandler is a base abstract Nuxeo document handler
51 * using Nuxeo Java Remote APIs for CollectionSpace services
53 * $LastChangedRevision: $
56 public abstract class DocumentModelHandler<T, TL>
57 extends AbstractMultipartDocumentHandlerImpl<T, TL, DocumentModel, DocumentModelList> {
59 private final Logger logger = LoggerFactory.getLogger(DocumentModelHandler.class);
60 private RepositoryInstance repositorySession;
61 //key=schema, value=documentpart
63 public final static String COLLECTIONSPACE_CORE_SCHEMA = "collectionspace_core";
64 public final static String COLLECTIONSPACE_CORE_TENANTID = "tenantId";
65 public final static String COLLECTIONSPACE_CORE_URI = "uri";
66 public final static String COLLECTIONSPACE_CORE_CREATED_AT = "createdAt";
67 public final static String COLLECTIONSPACE_CORE_UPDATED_AT = "updatedAt";
70 * We're using the "name" field of Nuxeo's DocumentModel to store
73 public String getCsid(DocumentModel docModel) {
74 return NuxeoUtils.getCsid(docModel);
77 public String getUri(DocumentModel docModel) {
78 return getServiceContextPath()+getCsid(docModel);
83 * getRepositorySession returns Nuxeo Repository Session
86 public RepositoryInstance getRepositorySession() {
87 return repositorySession;
91 * setRepositorySession sets repository session
94 public void setRepositorySession(RepositoryInstance repoSession) {
95 this.repositorySession = repoSession;
99 public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
100 // TODO for sub-docs - check to see if the current service context is a multipart input,
101 // OR a docfragment, and call a variant to fill the DocModel.
102 fillAllParts(wrapDoc, Action.CREATE);
103 handleCoreValues(wrapDoc, Action.CREATE);
106 // TODO for sub-docs - Add completeCreate in which we look for set-aside doc fragments
107 // and create the subitems. We will create service contexts with the doc fragments
112 public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
113 // TODO for sub-docs - check to see if the current service context is a multipart input,
114 // OR a docfragment, and call a variant to fill the DocModel.
115 fillAllParts(wrapDoc, Action.UPDATE);
116 handleCoreValues(wrapDoc, Action.UPDATE);
120 public void handleGet(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
121 extractAllParts(wrapDoc);
125 public void handleGetAll(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
126 Profiler profiler = new Profiler(this, 2);
128 setCommonPartList(extractCommonPartList(wrapDoc));
133 public abstract void completeUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
136 public abstract void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
139 public abstract T extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
142 public abstract void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception;
145 public abstract void fillCommonPart(T obj, DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
148 public abstract TL extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception;
151 public abstract T getCommonPart();
154 public abstract void setCommonPart(T obj);
157 public abstract TL getCommonPartList();
160 public abstract void setCommonPartList(TL obj);
163 public DocumentFilter createDocumentFilter() {
164 DocumentFilter filter = new NuxeoDocumentFilter(this.getServiceContext());
169 * Gets the authority refs.
171 * @param docWrapper the doc wrapper
172 * @param authRefFields the auth ref fields
173 * @return the authority refs
174 * @throws PropertyException the property exception
176 abstract public AuthorityRefList getAuthorityRefs(
177 DocumentWrapper<DocumentModel> docWrapper,
178 List<String> authRefFields) throws PropertyException;
180 private void handleCoreValues(DocumentWrapper<DocumentModel> docWrapper,
181 Action action) throws ClientException {
182 DocumentModel documentModel = docWrapper.getWrappedObject();
183 String now = GregorianCalendarDateTimeUtils.timestampUTC();
184 if(action==Action.CREATE) {
185 String tenantId = getServiceContext().getTenantId();
187 // Add the tenant ID value to the new entity
189 documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
190 COLLECTIONSPACE_CORE_TENANTID,
191 getServiceContext().getTenantId());
193 // Add the uri value to the new entity
195 documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
196 COLLECTIONSPACE_CORE_URI, getUri(documentModel));
198 // Add the CSID to the DublinCore title so we can see the CSID in the default
202 documentModel.setProperty("dublincore", "title",
203 documentModel.getName());
204 } catch (Exception x) {
205 if (logger.isWarnEnabled() == true) {
206 logger.warn("Could not set the Dublin Core 'title' field on document CSID:" +
207 documentModel.getName());
210 documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
211 COLLECTIONSPACE_CORE_CREATED_AT, now);
213 if(action==Action.CREATE || action==Action.UPDATE) {
214 documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
215 COLLECTIONSPACE_CORE_UPDATED_AT, now);