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