]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
56368f4f6a9d6c465c5e05f535e71d10c8b70545
[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 void handle(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
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                 handleDelete((DocumentWrapper<WT>) wrapDoc);
250                 break;
251                 
252             case SYNC:
253                 handleSync((DocumentWrapper<Specifier>) 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
268     /* (non-Javadoc)
269      * @see org.collectionspace.services.common.document.DocumentHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
270      */
271     @Override
272     public abstract void handleCreate(DocumentWrapper<WT> wrapDoc) throws Exception;
273
274     /* (non-Javadoc)
275      * @see org.collectionspace.services.common.document.DocumentHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
276      */
277     @Override
278     public abstract void handleUpdate(DocumentWrapper<WT> wrapDoc) throws Exception;
279
280     /* (non-Javadoc)
281      * @see org.collectionspace.services.common.document.DocumentHandler#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
282      */
283     @Override
284     public abstract void handleGet(DocumentWrapper<WT> wrapDoc) throws Exception;
285
286     /* (non-Javadoc)
287      * @see org.collectionspace.services.common.document.DocumentHandler#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
288      */
289     @Override
290     public abstract void handleGetAll(DocumentWrapper<WTL> wrapDoc) throws Exception;
291
292     /* (non-Javadoc)
293      * @see org.collectionspace.services.common.document.DocumentHandler#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
294      */
295     @Override
296     public void handleDelete(DocumentWrapper<WT> wrapDoc) throws Exception {
297         // Do nothing. Subclasses can override if they want/need to.
298     }
299     
300     /* (non-Javadoc)
301      * @see org.collectionspace.services.common.document.DocumentHandler#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
302      */
303     @Override
304     public void handleSync(DocumentWrapper<Specifier> wrapDoc) throws Exception {
305         // Do nothing. Subclasses can override if they want/need to.
306     }
307     
308
309     /* (non-Javadoc)
310      * @see org.collectionspace.services.common.document.DocumentHandler#complete(org.collectionspace.services.common.document.DocumentHandler.Action, org.collectionspace.services.common.document.DocumentWrapper)
311      */
312     @Override
313     final public void complete(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
314         switch (action) {
315             case CREATE:
316                 completeCreate((DocumentWrapper<WT>) wrapDoc);
317                 break;
318
319             case UPDATE:
320                 completeUpdate((DocumentWrapper<WT>) wrapDoc);
321                 break;
322
323             case GET:
324                 completeGet((DocumentWrapper<WT>) wrapDoc);
325                 break;
326
327             case GET_ALL:
328                 completeGetAll((DocumentWrapper<WTL>) wrapDoc);
329                 break;
330
331             case DELETE:
332                 completeDelete((DocumentWrapper<WT>) wrapDoc);
333                 break;
334                 
335             case SYNC:
336                 completeSync((DocumentWrapper<Specifier>) wrapDoc);
337                 break;
338                 
339                         case WORKFLOW:
340                                 logger.error("Should never get to this code path.  If you did, there is a bug in the code.");
341                                 Thread.dumpStack();
342                                 break;
343                                 
344                         default:
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     }
350
351     /* (non-Javadoc)
352      * @see org.collectionspace.services.common.document.DocumentHandler#completeCreate(org.collectionspace.services.common.document.DocumentWrapper)
353      */
354     @Override
355     public void completeCreate(DocumentWrapper<WT> wrapDoc) throws Exception {
356     }
357
358     /* (non-Javadoc)
359      * @see org.collectionspace.services.common.document.DocumentHandler#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
360      */
361     @Override
362     public void completeUpdate(DocumentWrapper<WT> wrapDoc) throws Exception {
363         //no specific action needed
364     }
365
366     /* (non-Javadoc)
367      * @see org.collectionspace.services.common.document.DocumentHandler#completeGet(org.collectionspace.services.common.document.DocumentWrapper)
368      */
369     @Override
370     public void completeGet(DocumentWrapper<WT> wrapDoc) throws Exception {
371     }
372
373     /* (non-Javadoc)
374      * @see org.collectionspace.services.common.document.DocumentHandler#completeGetAll(org.collectionspace.services.common.document.DocumentWrapper)
375      */
376     @Override
377     public void completeGetAll(DocumentWrapper<WTL> wrapDoc) throws Exception {
378     }
379
380     /* (non-Javadoc)
381      * @see org.collectionspace.services.common.document.DocumentHandler#completeDelete(org.collectionspace.services.common.document.DocumentWrapper)
382      */
383     @Override
384     public void completeDelete(DocumentWrapper<WT> wrapDoc) throws Exception {
385     }
386     
387     /* (non-Javadoc)
388      * @see org.collectionspace.services.common.document.DocumentHandler#completeDelete(org.collectionspace.services.common.document.DocumentWrapper)
389      */
390     @Override
391     public void completeSync(DocumentWrapper<Specifier> wrapDoc) throws Exception {
392     }    
393
394     /* (non-Javadoc)
395      * @see org.collectionspace.services.common.document.DocumentHandler#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
396      */
397     @Override
398     public abstract T extractCommonPart(DocumentWrapper<WT> wrapDoc)
399             throws Exception;
400
401     /* (non-Javadoc)
402      * @see org.collectionspace.services.common.document.DocumentHandler#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
403      */
404     @Override
405     public abstract void fillCommonPart(T obj, DocumentWrapper<WT> wrapDoc)
406             throws Exception;
407
408     /* (non-Javadoc)
409      * @see org.collectionspace.services.common.document.DocumentHandler#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
410      */
411     @Override
412     public abstract TL extractCommonPartList(DocumentWrapper<WTL> wrapDoc)
413             throws Exception;
414
415     /* (non-Javadoc)
416      * @see org.collectionspace.services.common.document.DocumentHandler#fillCommonPartList(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
417      */
418     @Override
419     final public void fillCommonPartList(TL obj, DocumentWrapper<WTL> wrapDoc) throws Exception {
420         throw new UnsupportedOperationException("bulk create/update not yet supported");
421     }
422
423     /* (non-Javadoc)
424      * @see org.collectionspace.services.common.document.DocumentHandler#getCommonPart()
425      */
426     @Override
427     public abstract T getCommonPart();
428
429     /* (non-Javadoc)
430      * @see org.collectionspace.services.common.document.DocumentHandler#setCommonPart(java.lang.Object)
431      */
432     @Override
433     public abstract void setCommonPart(T obj);
434
435     /* (non-Javadoc)
436      * @see org.collectionspace.services.common.document.DocumentHandler#getCommonPartList()
437      */
438     @Override
439     public abstract TL getCommonPartList();
440
441     /* (non-Javadoc)
442      * @see org.collectionspace.services.common.document.DocumentHandler#setCommonPartList(java.lang.Object)
443      */
444     @Override
445     public abstract void setCommonPartList(TL obj);
446
447     /* (non-Javadoc)
448      * @see org.collectionspace.services.common.document.DocumentHandler#getQProperty(java.lang.String)
449      */
450     @Override
451     public abstract String getQProperty(String prop) throws DocumentException;
452
453     /* 
454      * Strip Nuxeo's schema name from the start of the field / element name.
455      * (non-Javadoc)
456      * @see org.collectionspace.services.common.document.DocumentHandler#getUnQProperty(java.lang.String)
457      */
458     @Override
459     public String getUnQProperty(String qProp) {
460         StringTokenizer tkz = new StringTokenizer(qProp, ":");
461         if (tkz.countTokens() != 2) {
462             String msg = "Property must be in the form xxx:yyy, "
463                     + "e.g. collectionobjects_common:objectNumber";
464             logger.error(msg);
465             throw new IllegalArgumentException(msg);
466         }
467         tkz.nextToken(); //skip
468         return tkz.nextToken();
469     }
470     
471     /**
472      * Should return
473      * @throws Exception 
474      * @throws DocumentException 
475      */
476     @Override
477     public String getDocumentsToIndexQuery(String indexId, String csid) throws DocumentException, Exception {
478         return null;
479     }
480
481     /* (non-Javadoc)
482      * @see org.collectionspace.services.common.document.DocumentHandler#getServiceContextPath()
483      */
484     @Override
485     public String getServiceContextPath() {
486         return "/" + getServiceContext().getServiceName().toLowerCase() + "/";
487     }
488
489     /**
490      * Validate.
491      *
492      * @param action the action
493      * @throws Exception the exception
494      */
495     private void validate(Action action) throws Exception {
496         List<ValidatorHandler> valHandlers = serviceContext.getValidatorHandlers();
497         for (ValidatorHandler handler : valHandlers) {
498             handler.validate(action, serviceContext);
499         }
500     }
501     
502     /**
503      * Creates the CMIS query from the service context.  Each document handler is responsible for returning a valid CMIS query using the
504      * information in the current service context -which includes things like the query parameters, etc.
505      * @throws DocumentException 
506      */
507     @Override
508     public String getCMISQuery(QueryContext queryContext) throws DocumentException {
509         //
510         // By default, return nothing.  Child classes can override if they want.
511         //
512         return null;
513     }
514     
515     @Override
516     public boolean isCMISQuery() {
517         return false;
518     }
519     
520     @Override
521     public boolean isJDBCQuery() {
522         return false;
523     }
524     
525     @Override
526     public Map<String,String> getJDBCQueryParams() {
527         return new HashMap<>();
528     }
529     
530 }