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