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;
40 import org.collectionspace.services.common.repository.RepositoryClient;
41 import org.collectionspace.services.common.repository.RepositoryClientFactory;
43 import org.nuxeo.ecm.core.api.ClientException;
44 import org.nuxeo.ecm.core.api.DocumentModel;
45 import org.nuxeo.ecm.core.api.DocumentModelList;
46 import org.nuxeo.ecm.core.api.model.PropertyException;
47 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * DocumentModelHandler is a base abstract Nuxeo document handler
53 * using Nuxeo Java Remote APIs for CollectionSpace services
55 * $LastChangedRevision: $
58 public abstract class DocumentModelHandler<T, TL>
59 extends AbstractMultipartDocumentHandlerImpl<T, TL, DocumentModel, DocumentModelList> {
61 private final Logger logger = LoggerFactory.getLogger(DocumentModelHandler.class);
62 private RepositoryInstance repositorySession;
63 //key=schema, value=documentpart
65 public final static String COLLECTIONSPACE_CORE_SCHEMA = "collectionspace_core";
66 public final static String COLLECTIONSPACE_CORE_TENANTID = "tenantId";
67 public final static String COLLECTIONSPACE_CORE_URI = "uri";
68 public final static String COLLECTIONSPACE_CORE_CREATED_AT = "createdAt";
69 public final static String COLLECTIONSPACE_CORE_UPDATED_AT = "updatedAt";
70 public final static String COLLECTIONSPACE_CORE_CREATED_BY = "createdBy";
71 public final static String COLLECTIONSPACE_CORE_UPDATED_BY = "updatedBy";
74 * We're using the "name" field of Nuxeo's DocumentModel to store
77 public String getCsid(DocumentModel docModel) {
78 return NuxeoUtils.getCsid(docModel);
81 public String getUri(DocumentModel docModel) {
82 return getServiceContextPath()+getCsid(docModel);
86 public RepositoryClient getRepositoryClient(ServiceContext ctx) {
87 RepositoryClient repositoryClient = RepositoryClientFactory.getInstance().getClient(ctx.getRepositoryClientName());
88 return repositoryClient;
92 * getRepositorySession returns Nuxeo Repository Session
95 public RepositoryInstance getRepositorySession() {
96 return repositorySession;
100 * setRepositorySession sets repository session
103 public void setRepositorySession(RepositoryInstance repoSession) {
104 this.repositorySession = repoSession;
108 public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
109 // TODO for sub-docs - check to see if the current service context is a multipart input,
110 // OR a docfragment, and call a variant to fill the DocModel.
111 fillAllParts(wrapDoc, Action.CREATE);
112 handleCoreValues(wrapDoc, Action.CREATE);
115 // TODO for sub-docs - Add completeCreate in which we look for set-aside doc fragments
116 // and create the subitems. We will create service contexts with the doc fragments
121 public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
122 // TODO for sub-docs - check to see if the current service context is a multipart input,
123 // OR a docfragment, and call a variant to fill the DocModel.
124 fillAllParts(wrapDoc, Action.UPDATE);
125 handleCoreValues(wrapDoc, Action.UPDATE);
129 public void handleGet(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
130 extractAllParts(wrapDoc);
134 public void handleGetAll(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
135 Profiler profiler = new Profiler(this, 2);
137 setCommonPartList(extractCommonPartList(wrapDoc));
142 public abstract void completeUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
145 public abstract void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
148 public abstract T extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
151 public abstract void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception;
154 public abstract void fillCommonPart(T obj, DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
157 public abstract TL extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception;
160 public abstract T getCommonPart();
163 public abstract void setCommonPart(T obj);
166 public abstract TL getCommonPartList();
169 public abstract void setCommonPartList(TL obj);
172 public DocumentFilter createDocumentFilter() {
173 DocumentFilter filter = new NuxeoDocumentFilter(this.getServiceContext());
178 * Gets the authority refs.
180 * @param docWrapper the doc wrapper
181 * @param authRefFields the auth ref fields
182 * @return the authority refs
183 * @throws PropertyException the property exception
185 abstract public AuthorityRefList getAuthorityRefs(
186 DocumentWrapper<DocumentModel> docWrapper,
187 List<String> authRefFields) throws PropertyException;
189 private void handleCoreValues(DocumentWrapper<DocumentModel> docWrapper,
190 Action action) throws ClientException {
191 DocumentModel documentModel = docWrapper.getWrappedObject();
192 String now = GregorianCalendarDateTimeUtils.timestampUTC();
193 ServiceContext ctx = getServiceContext();
194 String userId = ctx.getUserId();
195 if(action==Action.CREATE) {
197 // Add the tenant ID value to the new entity
199 String tenantId = ctx.getTenantId();
200 documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
201 COLLECTIONSPACE_CORE_TENANTID, tenantId);
203 // Add the uri value to the new entity
205 documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
206 COLLECTIONSPACE_CORE_URI, getUri(documentModel));
208 // Add the CSID to the DublinCore title so we can see the CSID in the default
212 documentModel.setProperty("dublincore", "title",
213 documentModel.getName());
214 } catch (Exception x) {
215 if (logger.isWarnEnabled() == true) {
216 logger.warn("Could not set the Dublin Core 'title' field on document CSID:" +
217 documentModel.getName());
220 documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
221 COLLECTIONSPACE_CORE_CREATED_AT, now);
222 documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
223 COLLECTIONSPACE_CORE_CREATED_BY, userId);
225 if(action==Action.CREATE || action==Action.UPDATE) {
226 documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
227 COLLECTIONSPACE_CORE_UPDATED_AT, now);
228 documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
229 COLLECTIONSPACE_CORE_UPDATED_BY, userId);