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