]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
91c7a2f0c290a42f1211630b79e460c34ae30d42
[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 import org.collectionspace.services.common.repository.RepositoryClient;
41 import org.collectionspace.services.common.repository.RepositoryClientFactory;
42
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;
50
51 /**
52  * DocumentModelHandler is a base abstract Nuxeo document handler
53  * using Nuxeo Java Remote APIs for CollectionSpace services
54  *
55  * $LastChangedRevision: $
56  * $LastChangedDate: $
57  */
58 public abstract class DocumentModelHandler<T, TL>
59         extends AbstractMultipartDocumentHandlerImpl<T, TL, DocumentModel, DocumentModelList> {
60
61     private final Logger logger = LoggerFactory.getLogger(DocumentModelHandler.class);
62     private RepositoryInstance repositorySession;
63     //key=schema, value=documentpart
64
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";
72
73     /*
74      * We're using the "name" field of Nuxeo's DocumentModel to store
75      * the CSID.
76      */
77     public String getCsid(DocumentModel docModel) {
78         return NuxeoUtils.getCsid(docModel);
79     }
80
81     public String getUri(DocumentModel docModel) {
82         return getServiceContextPath()+getCsid(docModel);
83     }
84     
85     
86     public RepositoryClient getRepositoryClient(ServiceContext ctx) {
87         RepositoryClient repositoryClient = RepositoryClientFactory.getInstance().getClient(ctx.getRepositoryClientName());
88         return repositoryClient;
89     }
90
91     /**
92      * getRepositorySession returns Nuxeo Repository Session
93      * @return
94      */
95     public RepositoryInstance getRepositorySession() {
96         return repositorySession;
97     }
98
99     /**
100      * setRepositorySession sets repository session
101      * @param repoSession
102      */
103     public void setRepositorySession(RepositoryInstance repoSession) {
104         this.repositorySession = repoSession;
105     }
106
107     @Override
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);
113     }
114     
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
117     // and then call 
118
119
120     @Override
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);
126     }
127
128     @Override
129     public void handleGet(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
130         extractAllParts(wrapDoc);
131     }
132
133     @Override
134     public void handleGetAll(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
135         Profiler profiler = new Profiler(this, 2);
136         profiler.start();
137         setCommonPartList(extractCommonPartList(wrapDoc));
138         profiler.stop();
139     }
140
141     @Override
142     public abstract void completeUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
143
144     @Override
145     public abstract void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
146
147     @Override
148     public abstract T extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
149
150     @Override
151     public abstract void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception;
152
153     @Override
154     public abstract void fillCommonPart(T obj, DocumentWrapper<DocumentModel> wrapDoc) throws Exception;
155
156     @Override
157     public abstract TL extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception;
158
159     @Override
160     public abstract T getCommonPart();
161
162     @Override
163     public abstract void setCommonPart(T obj);
164
165     @Override
166     public abstract TL getCommonPartList();
167
168     @Override
169     public abstract void setCommonPartList(TL obj);
170     
171     @Override
172     public DocumentFilter createDocumentFilter() {
173         DocumentFilter filter = new NuxeoDocumentFilter(this.getServiceContext());
174         return filter;
175     }
176     
177     /**
178      * Gets the authority refs.
179      *
180      * @param docWrapper the doc wrapper
181      * @param authRefFields the auth ref fields
182      * @return the authority refs
183      * @throws PropertyException the property exception
184      */
185     abstract public AuthorityRefList getAuthorityRefs(
186             DocumentWrapper<DocumentModel> docWrapper,
187                 List<String> authRefFields) throws PropertyException;    
188
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) {
196             //
197             // Add the tenant ID value to the new entity
198             //
199                 String tenantId = ctx.getTenantId();
200             documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
201                     COLLECTIONSPACE_CORE_TENANTID, tenantId);
202             //
203             // Add the uri value to the new entity
204             //
205             documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
206                     COLLECTIONSPACE_CORE_URI, getUri(documentModel));
207                 //
208                 // Add the CSID to the DublinCore title so we can see the CSID in the default
209                 // Nuxeo webapp.
210                 //
211                 try {
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());
218                         }
219                 }
220             documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
221                     COLLECTIONSPACE_CORE_CREATED_AT, now);
222             documentModel.setProperty(COLLECTIONSPACE_CORE_SCHEMA,
223                     COLLECTIONSPACE_CORE_CREATED_BY, userId);
224         }
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);
230         }
231     }
232
233 }