]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
64b4a7fe9ae7a5d625009f3bbe4564da6b9e8cd0
[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.common.document;
25
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import java.util.StringTokenizer;
31 import org.collectionspace.services.common.context.ServiceContext;
32
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * AbstractDocumentHandler
38  *
39  * $LastChangedRevision: $
40  * $LastChangedDate: $
41  * @param <T> 
42  * @param <TL> 
43  * @param <WT> 
44  * @param <WTL> 
45  */
46 public abstract class AbstractDocumentHandlerImpl<T, TL, WT, WTL>
47         implements DocumentHandler<T, TL, WT, WTL> {
48
49     /** The logger. */
50     private final Logger logger = LoggerFactory.getLogger(AbstractDocumentHandlerImpl.class);
51     
52     /** The properties. */
53     private Map<String, Object> properties = new HashMap<String, Object>();
54     
55     /** The doc filter. */
56     private DocumentFilter docFilter = null;
57     
58     /** The service context. */
59     private ServiceContext serviceContext;
60
61     /**
62      * Instantiates a new abstract document handler impl.
63      */
64     public AbstractDocumentHandlerImpl() {
65         // Empty constructor
66     }
67
68     /* (non-Javadoc)
69      * @see org.collectionspace.services.common.document.DocumentHandler#getServiceContext()
70      */
71     @Override
72     public ServiceContext getServiceContext() {
73         return serviceContext;
74     }
75
76     /* (non-Javadoc)
77      * @see org.collectionspace.services.common.document.DocumentHandler#setServiceContext(org.collectionspace.services.common.context.ServiceContext)
78      */
79     @Override
80     public void setServiceContext(ServiceContext ctx) {
81         serviceContext = ctx;
82     }
83
84     /**
85      * @return the properties
86      */
87     @Override
88     public Map<String, Object> getProperties() {
89         return properties;
90     }
91
92     /**
93      * @param properties the properties to set
94      */
95     @Override
96     public void setProperties(Map<String, Object> properties) {
97         this.properties = properties;
98     }
99
100 //    public void initializeDocumentFilter(ServiceContext ctx) {
101 //      DocumentFilter docFilter = this.createDocumentFilter(ctx);
102 //      this.setDocumentFilter(docFilter);
103 //    }
104     /* (non-Javadoc)
105  * @see org.collectionspace.services.common.document.DocumentHandler#createDocumentFilter()
106  */
107 @Override
108     public abstract DocumentFilter createDocumentFilter();
109
110     /**
111      * @return the DocumentFilter
112      */
113     @Override
114     public DocumentFilter getDocumentFilter() {
115         return docFilter;
116     }
117
118     /**
119      * @param properties the DocumentFilter to set
120      */
121     @Override
122     public void setDocumentFilter(DocumentFilter docFilter) {
123         this.docFilter = docFilter;
124     }
125
126     /* (non-Javadoc)
127      * @see org.collectionspace.services.common.document.DocumentHandler#prepare(org.collectionspace.services.common.document.DocumentHandler.Action)
128      */
129     @Override
130     final public void prepare(Action action) throws Exception {
131         switch (action) {
132             case CREATE:
133                 validate(action);
134                 prepareCreate();
135                 break;
136
137             case UPDATE:
138                 validate(action);
139                 prepareUpdate();
140                 break;
141
142             case GET:
143                 prepareGet();
144                 break;
145
146             case GET_ALL:
147                 prepareGetAll();
148                 break;
149
150             case DELETE:
151                 validate(action);
152                 prepareDelete();
153                 break;
154
155         }
156     }
157
158     /* (non-Javadoc)
159      * @see org.collectionspace.services.common.document.DocumentHandler#prepareCreate()
160      */
161     @Override
162     public void prepareCreate() throws Exception {
163     }
164
165     /* (non-Javadoc)
166      * @see org.collectionspace.services.common.document.DocumentHandler#prepareUpdate()
167      */
168     @Override
169     public void prepareUpdate() throws Exception {
170     }
171
172     /* (non-Javadoc)
173      * @see org.collectionspace.services.common.document.DocumentHandler#prepareGet()
174      */
175     @Override
176     public void prepareGet() throws Exception {
177     }
178
179     /* (non-Javadoc)
180      * @see org.collectionspace.services.common.document.DocumentHandler#prepareGetAll()
181      */
182     @Override
183     public void prepareGetAll() throws Exception {
184     }
185
186     /* (non-Javadoc)
187      * @see org.collectionspace.services.common.document.DocumentHandler#prepareDelete()
188      */
189     @Override
190     public void prepareDelete() throws Exception {
191     }
192
193     /* (non-Javadoc)
194      * @see org.collectionspace.services.common.document.DocumentHandler#handle(org.collectionspace.services.common.document.DocumentHandler.Action, org.collectionspace.services.common.document.DocumentWrapper)
195      */
196     @Override
197     final public void handle(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
198         switch (action) {
199             case CREATE:
200                 handleCreate((DocumentWrapper<WT>) wrapDoc);
201                 break;
202
203             case UPDATE:
204                 handleUpdate((DocumentWrapper<WT>) wrapDoc);
205                 break;
206
207             case GET:
208                 handleGet((DocumentWrapper<WT>) wrapDoc);
209                 break;
210
211             case GET_ALL:
212                 handleGetAll((DocumentWrapper<WTL>) wrapDoc);
213                 break;
214
215             case DELETE:
216                 handleDelete((DocumentWrapper<WT>) wrapDoc);
217                 break;
218
219         }
220     }
221
222     /* (non-Javadoc)
223      * @see org.collectionspace.services.common.document.DocumentHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
224      */
225     @Override
226     public abstract void handleCreate(DocumentWrapper<WT> wrapDoc) throws Exception;
227
228     /* (non-Javadoc)
229      * @see org.collectionspace.services.common.document.DocumentHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
230      */
231     @Override
232     public abstract void handleUpdate(DocumentWrapper<WT> wrapDoc) throws Exception;
233
234     /* (non-Javadoc)
235      * @see org.collectionspace.services.common.document.DocumentHandler#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
236      */
237     @Override
238     public abstract void handleGet(DocumentWrapper<WT> wrapDoc) throws Exception;
239
240     /* (non-Javadoc)
241      * @see org.collectionspace.services.common.document.DocumentHandler#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
242      */
243     @Override
244     public abstract void handleGetAll(DocumentWrapper<WTL> wrapDoc) throws Exception;
245
246     /* (non-Javadoc)
247      * @see org.collectionspace.services.common.document.DocumentHandler#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
248      */
249     @Override
250     public void handleDelete(DocumentWrapper<WT> wrapDoc) throws Exception {
251         
252     }
253
254     /* (non-Javadoc)
255      * @see org.collectionspace.services.common.document.DocumentHandler#complete(org.collectionspace.services.common.document.DocumentHandler.Action, org.collectionspace.services.common.document.DocumentWrapper)
256      */
257     @Override
258     final public void complete(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
259         switch (action) {
260             case CREATE:
261                 completeCreate((DocumentWrapper<WT>) wrapDoc);
262                 break;
263
264             case UPDATE:
265                 completeUpdate((DocumentWrapper<WT>) wrapDoc);
266                 break;
267
268             case GET:
269                 completeGet((DocumentWrapper<WT>) wrapDoc);
270                 break;
271
272             case GET_ALL:
273                 completeGetAll((DocumentWrapper<WTL>) wrapDoc);
274                 break;
275
276             case DELETE:
277                 completeDelete((DocumentWrapper<WT>) wrapDoc);
278                 break;
279         }
280     }
281
282     /* (non-Javadoc)
283      * @see org.collectionspace.services.common.document.DocumentHandler#completeCreate(org.collectionspace.services.common.document.DocumentWrapper)
284      */
285     @Override
286     public void completeCreate(DocumentWrapper<WT> wrapDoc) throws Exception {
287     }
288
289     /* (non-Javadoc)
290      * @see org.collectionspace.services.common.document.DocumentHandler#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
291      */
292     @Override
293     public void completeUpdate(DocumentWrapper<WT> wrapDoc) throws Exception {
294         //no specific action needed
295     }
296
297     /* (non-Javadoc)
298      * @see org.collectionspace.services.common.document.DocumentHandler#completeGet(org.collectionspace.services.common.document.DocumentWrapper)
299      */
300     @Override
301     public void completeGet(DocumentWrapper<WT> wrapDoc) throws Exception {
302     }
303
304     /* (non-Javadoc)
305      * @see org.collectionspace.services.common.document.DocumentHandler#completeGetAll(org.collectionspace.services.common.document.DocumentWrapper)
306      */
307     @Override
308     public void completeGetAll(DocumentWrapper<WTL> wrapDoc) throws Exception {
309     }
310
311     /* (non-Javadoc)
312      * @see org.collectionspace.services.common.document.DocumentHandler#completeDelete(org.collectionspace.services.common.document.DocumentWrapper)
313      */
314     @Override
315     public void completeDelete(DocumentWrapper<WT> wrapDoc) throws Exception {
316     }
317
318     /* (non-Javadoc)
319      * @see org.collectionspace.services.common.document.DocumentHandler#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
320      */
321     @Override
322     public abstract T extractCommonPart(DocumentWrapper<WT> wrapDoc)
323             throws Exception;
324
325     /* (non-Javadoc)
326      * @see org.collectionspace.services.common.document.DocumentHandler#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
327      */
328     @Override
329     public abstract void fillCommonPart(T obj, DocumentWrapper<WT> wrapDoc)
330             throws Exception;
331
332     /* (non-Javadoc)
333      * @see org.collectionspace.services.common.document.DocumentHandler#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
334      */
335     @Override
336     public abstract TL extractCommonPartList(DocumentWrapper<WTL> wrapDoc)
337             throws Exception;
338
339     /* (non-Javadoc)
340      * @see org.collectionspace.services.common.document.DocumentHandler#fillCommonPartList(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
341      */
342     @Override
343     final public void fillCommonPartList(TL obj, DocumentWrapper<WTL> wrapDoc) throws Exception {
344         throw new UnsupportedOperationException("bulk create/update not yet supported");
345     }
346
347     /* (non-Javadoc)
348      * @see org.collectionspace.services.common.document.DocumentHandler#getCommonPart()
349      */
350     @Override
351     public abstract T getCommonPart();
352
353     /* (non-Javadoc)
354      * @see org.collectionspace.services.common.document.DocumentHandler#setCommonPart(java.lang.Object)
355      */
356     @Override
357     public abstract void setCommonPart(T obj);
358
359     /* (non-Javadoc)
360      * @see org.collectionspace.services.common.document.DocumentHandler#getCommonPartList()
361      */
362     @Override
363     public abstract TL getCommonPartList();
364
365     /* (non-Javadoc)
366      * @see org.collectionspace.services.common.document.DocumentHandler#setCommonPartList(java.lang.Object)
367      */
368     @Override
369     public abstract void setCommonPartList(TL obj);
370
371     /* (non-Javadoc)
372      * @see org.collectionspace.services.common.document.DocumentHandler#getQProperty(java.lang.String)
373      */
374     @Override
375     public abstract String getQProperty(String prop) throws DocumentException;
376
377     /* 
378      * Strip Nuxeo's schema name from the start of the field / element name.
379      * (non-Javadoc)
380      * @see org.collectionspace.services.common.document.DocumentHandler#getUnQProperty(java.lang.String)
381      */
382     @Override
383     public String getUnQProperty(String qProp) {
384         StringTokenizer tkz = new StringTokenizer(qProp, ":");
385         if (tkz.countTokens() != 2) {
386             String msg = "Property must be in the form xxx:yyy, "
387                     + "e.g. collectionobjects_common:objectNumber";
388             logger.error(msg);
389             throw new IllegalArgumentException(msg);
390         }
391         tkz.nextToken(); //skip
392         return tkz.nextToken();
393     }
394
395     /* (non-Javadoc)
396      * @see org.collectionspace.services.common.document.DocumentHandler#getServiceContextPath()
397      */
398     @Override
399     public String getServiceContextPath() {
400         return "/" + getServiceContext().getServiceName().toLowerCase() + "/";
401     }
402
403     /**
404      * Validate.
405      *
406      * @param action the action
407      * @throws Exception the exception
408      */
409     private void validate(Action action) throws Exception {
410         List<ValidatorHandler> valHandlers = serviceContext.getValidatorHandlers();
411         for (ValidatorHandler handler : valHandlers) {
412             handler.validate(action, serviceContext);
413         }
414     }
415     
416     /**
417      * Creates the CMIS query from the service context.  Each document handler is responsible for returning a valid CMIS query using the
418      * information in the current service context -which includes things like the query parameters, etc.
419      */
420     public String getCMISQuery() {
421         //
422         // By default, return nothing.  Child classes can override if they want.
423         //
424         return null;
425     }
426     
427     public boolean isCMISQuery() {
428         return false;
429     }
430 }