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.api.FileTools;
31 import org.collectionspace.services.common.authorityref.AuthorityRefList;
32 import org.collectionspace.services.common.context.ServiceContext;
33 import org.collectionspace.services.common.datetime.GregorianCalendarDateTimeUtils;
34 import org.collectionspace.services.common.document.AbstractMultipartDocumentHandlerImpl;
35 import org.collectionspace.services.common.document.DocumentFilter;
36 import org.collectionspace.services.common.document.DocumentWrapper;
37 import org.collectionspace.services.lifecycle.Lifecycle;
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;
42 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.AuthRefConfigInfo;
44 import org.nuxeo.ecm.core.api.ClientException;
45 import org.nuxeo.ecm.core.api.DocumentModel;
46 import org.nuxeo.ecm.core.api.DocumentModelList;
47 import org.nuxeo.ecm.core.api.model.PropertyException;
48 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 * DocumentModelHandler is a base abstract Nuxeo document handler
54 * using Nuxeo Java Remote APIs for CollectionSpace services
56 * $LastChangedRevision: $
59 public abstract class DocumentModelHandler<T, TL>
60 extends AbstractMultipartDocumentHandlerImpl<T, TL, DocumentModel, DocumentModelList> {
62 private final Logger logger = LoggerFactory.getLogger(DocumentModelHandler.class);
63 private RepositoryInstance repositorySession;
64 //key=schema, value=documentpart
66 public final static String COLLECTIONSPACE_CORE_SCHEMA = "collectionspace_core";
67 public final static String COLLECTIONSPACE_CORE_TENANTID = "tenantId";
68 public final static String COLLECTIONSPACE_CORE_URI = "uri";
69 public final static String COLLECTIONSPACE_CORE_CREATED_AT = "createdAt";
70 public final static String COLLECTIONSPACE_CORE_UPDATED_AT = "updatedAt";
71 public final static String COLLECTIONSPACE_CORE_CREATED_BY = "createdBy";
72 public final static String COLLECTIONSPACE_CORE_UPDATED_BY = "updatedBy";
74 public Lifecycle getLifecycle() {
75 Lifecycle result = null;
78 result = (Lifecycle)FileTools.getJaxbObjectFromFile(Lifecycle.class, "default-lifecycle.xml");
79 } catch (Exception e) {
80 // TODO Auto-generated catch block
88 * We're using the "name" field of Nuxeo's DocumentModel to store
91 public String getCsid(DocumentModel docModel) {
92 return NuxeoUtils.getCsid(docModel);
95 public String getUri(DocumentModel docModel) {
96 return getServiceContextPath()+getCsid(docModel);
99 public RepositoryClient<PoxPayloadIn, PoxPayloadOut> getRepositoryClient(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) {
100 RepositoryClient<PoxPayloadIn, PoxPayloadOut> repositoryClient = RepositoryClientFactory.getInstance().getClient(ctx.getRepositoryClientName());
101 return repositoryClient;
105 * getRepositorySession returns Nuxeo Repository Session
108 public RepositoryInstance getRepositorySession() {
110 return repositorySession;
114 * setRepositorySession sets repository session
117 public void setRepositorySession(RepositoryInstance repoSession) {
118 this.repositorySession = repoSession;
122 public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
123 // TODO for sub-docs - check to see if the current service context is a multipart input,
124 // OR a docfragment, and call a variant to fill the DocModel.
125 fillAllParts(wrapDoc, Action.CREATE);
126 handleCoreValues(wrapDoc, Action.CREATE);
129 // TODO for sub-docs - Add completeCreate in which we look for set-aside doc fragments
130 // and create the subitems. We will create service contexts with the doc fragments
135 public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
136 // TODO for sub-docs - check to see if the current service context is a multipart input,
137 // OR a docfragment, and call a variant to fill the DocModel.
138 fillAllParts(wrapDoc, Action.UPDATE);
139 handleCoreValues(wrapDoc, Action.UPDATE);
143 public void handleGet(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
144 extractAllParts(wrapDoc);
148 public void handleGetAll(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
149 Profiler profiler = new Profiler(this, 2);
151 setCommonPartList(extractCommonPartList(wrapDoc));
156 public abstract void completeUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
159 public abstract void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
162 public abstract T extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
165 public abstract void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception;
168 public abstract void fillCommonPart(T obj, DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
171 public abstract TL extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception;
174 public abstract T getCommonPart();
177 public abstract void setCommonPart(T obj);
180 public abstract TL getCommonPartList();
183 public abstract void setCommonPartList(TL obj);
186 public DocumentFilter createDocumentFilter() {
187 DocumentFilter filter = new NuxeoDocumentFilter(this.getServiceContext());
192 * Gets the authority refs.
194 * @param docWrapper the doc wrapper
195 * @param authRefFields the auth ref fields
196 * @return the authority refs
197 * @throws PropertyException the property exception
199 abstract public AuthorityRefList getAuthorityRefs(String csid,
200 List<AuthRefConfigInfo> authRefsInfo) throws PropertyException;
202 private void handleCoreValues(DocumentWrapper<DocumentModel> docWrapper,
203 Action action) throws ClientException {
204 DocumentModel documentModel = docWrapper.getWrappedObject();
205 String now = GregorianCalendarDateTimeUtils.timestampUTC();
206 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = getServiceContext();
207 String userId = ctx.getUserId();
208 if(action==Action.CREATE) {
210 // Add the tenant ID value to the new entity
212 String tenantId = ctx.getTenantId();
213 documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
214 COLLECTIONSPACE_CORE_TENANTID, tenantId);
216 // Add the uri value to the new entity
218 documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
219 COLLECTIONSPACE_CORE_URI, getUri(documentModel));
221 // Add the CSID to the DublinCore title so we can see the CSID in the default
225 documentModel.setProperty("dublincore", "title",
226 documentModel.getName());
227 } catch (Exception x) {
228 if (logger.isWarnEnabled() == true) {
229 logger.warn("Could not set the Dublin Core 'title' field on document CSID:" +
230 documentModel.getName());
233 documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
234 COLLECTIONSPACE_CORE_CREATED_AT, now);
235 documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
236 COLLECTIONSPACE_CORE_CREATED_BY, userId);
238 if(action==Action.CREATE || action==Action.UPDATE) {
239 documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
240 COLLECTIONSPACE_CORE_UPDATED_AT, now);
241 documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
242 COLLECTIONSPACE_CORE_UPDATED_BY, userId);