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