]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
3f518cfbd3c2259045e0d3ea08842043c108713f
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.nuxeo.client.java;
25
26 import java.util.List;
27
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
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;
48
49 /**
50  * DocumentModelHandler is a base abstract Nuxeo document handler
51  * using Nuxeo Java Remote APIs for CollectionSpace services
52  *
53  * $LastChangedRevision: $
54  * $LastChangedDate: $
55  */
56 public abstract class DocumentModelHandler<T, TL>
57         extends AbstractMultipartDocumentHandlerImpl<T, TL, DocumentModel, DocumentModelList> {
58
59     private final Logger logger = LoggerFactory.getLogger(DocumentModelHandler.class);
60     private RepositoryInstance repositorySession;
61     //key=schema, value=documentpart
62
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";
68
69     /*
70      * We're using the "name" field of Nuxeo's DocumentModel to store
71      * the CSID.
72      */
73     public String getCsid(DocumentModel docModel) {
74         return NuxeoUtils.getCsid(docModel);
75     }
76
77     public String getUri(DocumentModel docModel) {
78         return getServiceContextPath()+getCsid(docModel);
79     }
80     
81     
82     /**
83      * getRepositorySession returns Nuxeo Repository Session
84      * @return
85      */
86     public RepositoryInstance getRepositorySession() {
87         return repositorySession;
88     }
89
90     /**
91      * setRepositorySession sets repository session
92      * @param repoSession
93      */
94     public void setRepositorySession(RepositoryInstance repoSession) {
95         this.repositorySession = repoSession;
96     }
97
98     @Override
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);
104     }
105     
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
108     // and then call 
109
110
111     @Override
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);
117     }
118
119     @Override
120     public void handleGet(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
121         extractAllParts(wrapDoc);
122     }
123
124     @Override
125     public void handleGetAll(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
126         Profiler profiler = new Profiler(this, 2);
127         profiler.start();
128         setCommonPartList(extractCommonPartList(wrapDoc));
129         profiler.stop();
130     }
131
132     @Override
133     public abstract void completeUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
134
135     @Override
136     public abstract void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
137
138     @Override
139     public abstract T extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
140
141     @Override
142     public abstract void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception;
143
144     @Override
145     public abstract void fillCommonPart(T obj, DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
146
147     @Override
148     public abstract TL extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception;
149
150     @Override
151     public abstract T getCommonPart();
152
153     @Override
154     public abstract void setCommonPart(T obj);
155
156     @Override
157     public abstract TL getCommonPartList();
158
159     @Override
160     public abstract void setCommonPartList(TL obj);
161     
162     @Override
163     public DocumentFilter createDocumentFilter() {
164         DocumentFilter filter = new NuxeoDocumentFilter(this.getServiceContext());
165         return filter;
166     }
167     
168     /**
169      * Gets the authority refs.
170      *
171      * @param docWrapper the doc wrapper
172      * @param authRefFields the auth ref fields
173      * @return the authority refs
174      * @throws PropertyException the property exception
175      */
176     abstract public AuthorityRefList getAuthorityRefs(
177             DocumentWrapper<DocumentModel> docWrapper,
178                 List<String> authRefFields) throws PropertyException;    
179
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();
186             //
187             // Add the tenant ID value to the new entity
188             //
189             documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
190                     COLLECTIONSPACE_CORE_TENANTID,
191                     getServiceContext().getTenantId());
192             //
193             // Add the uri value to the new entity
194             //
195             documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
196                     COLLECTIONSPACE_CORE_URI, getUri(documentModel));
197                 //
198                 // Add the CSID to the DublinCore title so we can see the CSID in the default
199                 // Nuxeo webapp.
200                 //
201                 try {
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());
208                         }
209                 }
210             documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
211                     COLLECTIONSPACE_CORE_CREATED_AT, now);
212         }
213         if(action==Action.CREATE || action==Action.UPDATE) {
214             documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
215                     COLLECTIONSPACE_CORE_UPDATED_AT, now);
216         }
217     }
218
219 }