]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9e890cc9ddc1f6ecb8a6c9a0970ba3ee1a7be598
[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 import java.util.Map;
28
29 import org.collectionspace.services.client.PoxPayloadIn;
30 import org.collectionspace.services.client.PoxPayloadOut;
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.common.document.DocumentHandler.Action;
38 import org.collectionspace.services.nuxeo.client.*;
39 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
40 import org.collectionspace.services.common.profile.Profiler;
41 import org.collectionspace.services.common.repository.RepositoryClient;
42 import org.collectionspace.services.common.repository.RepositoryClientFactory;
43 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.AuthRefConfigInfo;
44
45 import org.nuxeo.ecm.core.api.ClientException;
46 import org.nuxeo.ecm.core.api.DocumentModel;
47 import org.nuxeo.ecm.core.api.DocumentModelList;
48 import org.nuxeo.ecm.core.api.model.PropertyException;
49 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 /**
54  * DocumentModelHandler is a base abstract Nuxeo document handler
55  * using Nuxeo Java Remote APIs for CollectionSpace services
56  *
57  * $LastChangedRevision: $
58  * $LastChangedDate: $
59  */
60 public abstract class DocumentModelHandler<T, TL>
61         extends AbstractMultipartDocumentHandlerImpl<T, TL, DocumentModel, DocumentModelList> {
62
63     private final Logger logger = LoggerFactory.getLogger(DocumentModelHandler.class);
64     private RepositoryInstance repositorySession;
65     //key=schema, value=documentpart
66
67     public final static String COLLECTIONSPACE_CORE_SCHEMA = "collectionspace_core";
68     public final static String COLLECTIONSPACE_CORE_TENANTID = "tenantId";
69     public final static String COLLECTIONSPACE_CORE_URI = "uri";
70     public final static String COLLECTIONSPACE_CORE_CREATED_AT = "createdAt";
71     public final static String COLLECTIONSPACE_CORE_UPDATED_AT = "updatedAt";
72     public final static String COLLECTIONSPACE_CORE_CREATED_BY = "createdBy";
73     public final static String COLLECTIONSPACE_CORE_UPDATED_BY = "updatedBy";
74     public final static String COLLECTIONSPACE_CORE_CURRENTLIFECYCLESTATE = "currentLifeCycleState";
75
76     /*
77      * We're using the "name" field of Nuxeo's DocumentModel to store
78      * the CSID.
79      */
80     public String getCsid(DocumentModel docModel) {
81         return NuxeoUtils.getCsid(docModel);
82     }
83
84     public String getUri(DocumentModel docModel) {
85         return getServiceContextPath()+getCsid(docModel);
86     }
87         
88     public RepositoryClient<PoxPayloadIn, PoxPayloadOut> getRepositoryClient(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) {
89         RepositoryClient<PoxPayloadIn, PoxPayloadOut> repositoryClient = RepositoryClientFactory.getInstance().getClient(ctx.getRepositoryClientName());
90         return repositoryClient;
91     }
92
93     /**
94      * getRepositorySession returns Nuxeo Repository Session
95      * @return
96      */
97     public RepositoryInstance getRepositorySession() {
98         
99         return repositorySession;
100     }
101
102     /**
103      * setRepositorySession sets repository session
104      * @param repoSession
105      */
106     public void setRepositorySession(RepositoryInstance repoSession) {
107         this.repositorySession = repoSession;
108     }
109
110     @Override
111     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
112         // TODO for sub-docs - check to see if the current service context is a multipart input, 
113         // OR a docfragment, and call a variant to fill the DocModel.
114         fillAllParts(wrapDoc, Action.CREATE);
115         handleCoreValues(wrapDoc, Action.CREATE);
116     }
117     
118     // TODO for sub-docs - Add completeCreate in which we look for set-aside doc fragments 
119     // and create the subitems. We will create service contexts with the doc fragments
120     // and then call 
121
122
123     @Override
124     public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
125         // TODO for sub-docs - check to see if the current service context is a multipart input, 
126         // OR a docfragment, and call a variant to fill the DocModel.
127         fillAllParts(wrapDoc, Action.UPDATE);
128         handleCoreValues(wrapDoc, Action.UPDATE);
129     }
130
131     @Override
132     public void handleGet(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
133         extractAllParts(wrapDoc);
134     }
135
136     @Override
137     public void handleGetAll(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
138         Profiler profiler = new Profiler(this, 2);
139         profiler.start();
140         setCommonPartList(extractCommonPartList(wrapDoc));
141         profiler.stop();
142     }
143
144     @Override
145     public abstract void completeUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
146
147     @Override
148     public abstract void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
149
150     @Override
151     public abstract T extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
152
153     @Override
154     public abstract void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception;
155
156     @Override
157     public abstract void fillCommonPart(T obj, DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
158
159     @Override
160     public abstract TL extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception;
161
162     @Override
163     public abstract T getCommonPart();
164
165     @Override
166     public abstract void setCommonPart(T obj);
167
168     @Override
169     public abstract TL getCommonPartList();
170
171     @Override
172     public abstract void setCommonPartList(TL obj);
173     
174     @Override
175     public DocumentFilter createDocumentFilter() {
176         DocumentFilter filter = new NuxeoDocumentFilter(this.getServiceContext());
177         return filter;
178     }
179     
180     /**
181      * Gets the authority refs.
182      *
183      * @param docWrapper the doc wrapper
184      * @param authRefFields the auth ref fields
185      * @return the authority refs
186      * @throws PropertyException the property exception
187      */
188     abstract public AuthorityRefList getAuthorityRefs(String csid,
189                 List<AuthRefConfigInfo> authRefsInfo) throws PropertyException;    
190
191     private void handleCoreValues(DocumentWrapper<DocumentModel> docWrapper, 
192                 Action action)  throws ClientException {
193         DocumentModel documentModel = docWrapper.getWrappedObject();
194         String now = GregorianCalendarDateTimeUtils.timestampUTC();
195         ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = getServiceContext();
196         String userId = ctx.getUserId();
197         if(action==Action.CREATE) {
198             //
199             // Add the tenant ID value to the new entity
200             //
201                 String tenantId = ctx.getTenantId();
202             documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
203                     COLLECTIONSPACE_CORE_TENANTID, tenantId);
204             //
205             // Add the uri value to the new entity
206             //
207             documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
208                     COLLECTIONSPACE_CORE_URI, getUri(documentModel));
209                 //
210                 // Add the CSID to the DublinCore title so we can see the CSID in the default
211                 // Nuxeo webapp.
212                 //
213                 try {
214                 documentModel.setProperty("dublincore", "title",
215                         documentModel.getName());
216                 } catch (Exception x) {
217                         if (logger.isWarnEnabled() == true) {
218                                 logger.warn("Could not set the Dublin Core 'title' field on document CSID:" +
219                                                 documentModel.getName());
220                         }
221                 }
222             documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
223                     COLLECTIONSPACE_CORE_CREATED_AT, now);
224             documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
225                     COLLECTIONSPACE_CORE_CREATED_BY, userId);
226         }
227         if(action==Action.CREATE || action==Action.UPDATE) {
228             documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
229                     COLLECTIONSPACE_CORE_UPDATED_AT, now);
230             documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
231                     COLLECTIONSPACE_CORE_UPDATED_BY, userId);
232         }
233     }
234
235 }