]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
54e9db8f7419e793ea33b8f3d37f9a83aac784ac
[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     public final static String COLLECTIONSPACE_CORE_CREATED_BY = "createdBy";
69     public final static String COLLECTIONSPACE_CORE_UPDATED_BY = "updatedBy";
70
71     /*
72      * We're using the "name" field of Nuxeo's DocumentModel to store
73      * the CSID.
74      */
75     public String getCsid(DocumentModel docModel) {
76         return NuxeoUtils.getCsid(docModel);
77     }
78
79     public String getUri(DocumentModel docModel) {
80         return getServiceContextPath()+getCsid(docModel);
81     }
82     
83     
84     /**
85      * getRepositorySession returns Nuxeo Repository Session
86      * @return
87      */
88     public RepositoryInstance getRepositorySession() {
89         return repositorySession;
90     }
91
92     /**
93      * setRepositorySession sets repository session
94      * @param repoSession
95      */
96     public void setRepositorySession(RepositoryInstance repoSession) {
97         this.repositorySession = repoSession;
98     }
99
100     @Override
101     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
102         // TODO for sub-docs - check to see if the current service context is a multipart input, 
103         // OR a docfragment, and call a variant to fill the DocModel.
104         fillAllParts(wrapDoc, Action.CREATE);
105         handleCoreValues(wrapDoc, Action.CREATE);
106     }
107     
108     // TODO for sub-docs - Add completeCreate in which we look for set-aside doc fragments 
109     // and create the subitems. We will create service contexts with the doc fragments
110     // and then call 
111
112
113     @Override
114     public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
115         // TODO for sub-docs - check to see if the current service context is a multipart input, 
116         // OR a docfragment, and call a variant to fill the DocModel.
117         fillAllParts(wrapDoc, Action.UPDATE);
118         handleCoreValues(wrapDoc, Action.UPDATE);
119     }
120
121     @Override
122     public void handleGet(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
123         extractAllParts(wrapDoc);
124     }
125
126     @Override
127     public void handleGetAll(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
128         Profiler profiler = new Profiler(this, 2);
129         profiler.start();
130         setCommonPartList(extractCommonPartList(wrapDoc));
131         profiler.stop();
132     }
133
134     @Override
135     public abstract void completeUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
136
137     @Override
138     public abstract void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
139
140     @Override
141     public abstract T extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
142
143     @Override
144     public abstract void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception;
145
146     @Override
147     public abstract void fillCommonPart(T obj, DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
148
149     @Override
150     public abstract TL extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception;
151
152     @Override
153     public abstract T getCommonPart();
154
155     @Override
156     public abstract void setCommonPart(T obj);
157
158     @Override
159     public abstract TL getCommonPartList();
160
161     @Override
162     public abstract void setCommonPartList(TL obj);
163     
164     @Override
165     public DocumentFilter createDocumentFilter() {
166         DocumentFilter filter = new NuxeoDocumentFilter(this.getServiceContext());
167         return filter;
168     }
169     
170     /**
171      * Gets the authority refs.
172      *
173      * @param docWrapper the doc wrapper
174      * @param authRefFields the auth ref fields
175      * @return the authority refs
176      * @throws PropertyException the property exception
177      */
178     abstract public AuthorityRefList getAuthorityRefs(
179             DocumentWrapper<DocumentModel> docWrapper,
180                 List<String> authRefFields) throws PropertyException;    
181
182     private void handleCoreValues(DocumentWrapper<DocumentModel> docWrapper, 
183                 Action action)  throws ClientException {
184         DocumentModel documentModel = docWrapper.getWrappedObject();
185         String now = GregorianCalendarDateTimeUtils.timestampUTC();
186         ServiceContext ctx = getServiceContext();
187         String userId = ctx.getUserId();
188         if(action==Action.CREATE) {
189             //
190             // Add the tenant ID value to the new entity
191             //
192                 String tenantId = ctx.getTenantId();
193             documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
194                     COLLECTIONSPACE_CORE_TENANTID, tenantId);
195             //
196             // Add the uri value to the new entity
197             //
198             documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
199                     COLLECTIONSPACE_CORE_URI, getUri(documentModel));
200                 //
201                 // Add the CSID to the DublinCore title so we can see the CSID in the default
202                 // Nuxeo webapp.
203                 //
204                 try {
205                 documentModel.setProperty("dublincore", "title",
206                         documentModel.getName());
207                 } catch (Exception x) {
208                         if (logger.isWarnEnabled() == true) {
209                                 logger.warn("Could not set the Dublin Core 'title' field on document CSID:" +
210                                                 documentModel.getName());
211                         }
212                 }
213             documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
214                     COLLECTIONSPACE_CORE_CREATED_AT, now);
215             documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
216                     COLLECTIONSPACE_CORE_CREATED_BY, userId);
217         }
218         if(action==Action.CREATE || action==Action.UPDATE) {
219             documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
220                     COLLECTIONSPACE_CORE_UPDATED_AT, now);
221             documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
222                     COLLECTIONSPACE_CORE_UPDATED_BY, userId);
223         }
224     }
225
226 }