]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
55f2cafb30d8acfc0a12bcf5fa36b04ab21671a2
[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.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * AbstractDocumentHandler
41  *
42  * $LastChangedRevision: $
43  * $LastChangedDate: $
44  * @param <T> 
45  * @param <TL> 
46  * @param <WT> 
47  * @param <WTL> 
48  */
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 WORKFLOW:
166                                 logger.error("Should never get to this code path.  If you did, there is a bug in the code.");
167                                 Thread.dumpStack();
168                                 break;
169                                 
170                         default:
171                                 logger.error("Should never get to this code path.  If you did, there is a bug in the code.");
172                                 Thread.dumpStack();
173                                 break;
174         }
175     }
176
177     /* (non-Javadoc)
178      * @see org.collectionspace.services.common.document.DocumentHandler#prepareCreate()
179      */
180     @Override
181     public void prepareCreate() throws Exception {
182     }
183
184     /* (non-Javadoc)
185      * @see org.collectionspace.services.common.document.DocumentHandler#prepareUpdate()
186      */
187     @Override
188     public void prepareUpdate() throws Exception {
189     }
190
191     /* (non-Javadoc)
192      * @see org.collectionspace.services.common.document.DocumentHandler#prepareGet()
193      */
194     @Override
195     public void prepareGet() throws Exception {
196     }
197
198     /* (non-Javadoc)
199      * @see org.collectionspace.services.common.document.DocumentHandler#prepareGetAll()
200      */
201     @Override
202     public void prepareGetAll() throws Exception {
203     }
204
205     /* (non-Javadoc)
206      * @see org.collectionspace.services.common.document.DocumentHandler#prepareDelete()
207      */
208     @Override
209     public void prepareDelete() throws Exception {
210     }
211
212     /* (non-Javadoc)
213      * @see org.collectionspace.services.common.document.DocumentHandler#handle(org.collectionspace.services.common.document.DocumentHandler.Action, org.collectionspace.services.common.document.DocumentWrapper)
214      */
215     @Override
216     final public void handle(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
217         switch (action) {
218             case CREATE:
219                 handleCreate((DocumentWrapper<WT>) wrapDoc);
220                 break;
221
222             case UPDATE:
223                 handleUpdate((DocumentWrapper<WT>) wrapDoc);
224                 break;
225
226             case GET:
227                 handleGet((DocumentWrapper<WT>) wrapDoc);
228                 break;
229
230             case GET_ALL:
231                 handleGetAll((DocumentWrapper<WTL>) wrapDoc);
232                 break;
233
234             case DELETE:
235                 handleDelete((DocumentWrapper<WT>) wrapDoc);
236                 break;
237                 
238                         case WORKFLOW:
239                                 logger.error("Should never get to this code path.  If you did, there is a bug in the code.");
240                                 Thread.dumpStack();
241                                 break;
242                                 
243                         default:
244                                 logger.error("Should never get to this code path.  If you did, there is a bug in the code.");
245                                 Thread.dumpStack();
246                                 break;
247         }
248     }
249
250     /* (non-Javadoc)
251      * @see org.collectionspace.services.common.document.DocumentHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
252      */
253     @Override
254     public abstract void handleCreate(DocumentWrapper<WT> wrapDoc) throws Exception;
255
256     /* (non-Javadoc)
257      * @see org.collectionspace.services.common.document.DocumentHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
258      */
259     @Override
260     public abstract void handleUpdate(DocumentWrapper<WT> wrapDoc) throws Exception;
261
262     /* (non-Javadoc)
263      * @see org.collectionspace.services.common.document.DocumentHandler#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
264      */
265     @Override
266     public abstract void handleGet(DocumentWrapper<WT> wrapDoc) throws Exception;
267
268     /* (non-Javadoc)
269      * @see org.collectionspace.services.common.document.DocumentHandler#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
270      */
271     @Override
272     public abstract void handleGetAll(DocumentWrapper<WTL> wrapDoc) throws Exception;
273
274     /* (non-Javadoc)
275      * @see org.collectionspace.services.common.document.DocumentHandler#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
276      */
277     @Override
278     public void handleDelete(DocumentWrapper<WT> wrapDoc) throws Exception {
279         
280     }
281
282     /* (non-Javadoc)
283      * @see org.collectionspace.services.common.document.DocumentHandler#complete(org.collectionspace.services.common.document.DocumentHandler.Action, org.collectionspace.services.common.document.DocumentWrapper)
284      */
285     @Override
286     final public void complete(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
287         switch (action) {
288             case CREATE:
289                 completeCreate((DocumentWrapper<WT>) wrapDoc);
290                 break;
291
292             case UPDATE:
293                 completeUpdate((DocumentWrapper<WT>) wrapDoc);
294                 break;
295
296             case GET:
297                 completeGet((DocumentWrapper<WT>) wrapDoc);
298                 break;
299
300             case GET_ALL:
301                 completeGetAll((DocumentWrapper<WTL>) wrapDoc);
302                 break;
303
304             case DELETE:
305                 completeDelete((DocumentWrapper<WT>) wrapDoc);
306                 break;
307                 
308                         case WORKFLOW:
309                                 logger.error("Should never get to this code path.  If you did, there is a bug in the code.");
310                                 Thread.dumpStack();
311                                 break;
312                                 
313                         default:
314                                 logger.error("Should never get to this code path.  If you did, there is a bug in the code.");
315                                 Thread.dumpStack();
316                                 break;                
317         }
318     }
319
320     /* (non-Javadoc)
321      * @see org.collectionspace.services.common.document.DocumentHandler#completeCreate(org.collectionspace.services.common.document.DocumentWrapper)
322      */
323     @Override
324     public void completeCreate(DocumentWrapper<WT> wrapDoc) throws Exception {
325     }
326
327     /* (non-Javadoc)
328      * @see org.collectionspace.services.common.document.DocumentHandler#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
329      */
330     @Override
331     public void completeUpdate(DocumentWrapper<WT> wrapDoc) throws Exception {
332         //no specific action needed
333     }
334
335     /* (non-Javadoc)
336      * @see org.collectionspace.services.common.document.DocumentHandler#completeGet(org.collectionspace.services.common.document.DocumentWrapper)
337      */
338     @Override
339     public void completeGet(DocumentWrapper<WT> wrapDoc) throws Exception {
340     }
341
342     /* (non-Javadoc)
343      * @see org.collectionspace.services.common.document.DocumentHandler#completeGetAll(org.collectionspace.services.common.document.DocumentWrapper)
344      */
345     @Override
346     public void completeGetAll(DocumentWrapper<WTL> wrapDoc) throws Exception {
347     }
348
349     /* (non-Javadoc)
350      * @see org.collectionspace.services.common.document.DocumentHandler#completeDelete(org.collectionspace.services.common.document.DocumentWrapper)
351      */
352     @Override
353     public void completeDelete(DocumentWrapper<WT> wrapDoc) throws Exception {
354     }
355
356     /* (non-Javadoc)
357      * @see org.collectionspace.services.common.document.DocumentHandler#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
358      */
359     @Override
360     public abstract T extractCommonPart(DocumentWrapper<WT> wrapDoc)
361             throws Exception;
362
363     /* (non-Javadoc)
364      * @see org.collectionspace.services.common.document.DocumentHandler#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
365      */
366     @Override
367     public abstract void fillCommonPart(T obj, DocumentWrapper<WT> wrapDoc)
368             throws Exception;
369
370     /* (non-Javadoc)
371      * @see org.collectionspace.services.common.document.DocumentHandler#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
372      */
373     @Override
374     public abstract TL extractCommonPartList(DocumentWrapper<WTL> wrapDoc)
375             throws Exception;
376
377     /* (non-Javadoc)
378      * @see org.collectionspace.services.common.document.DocumentHandler#fillCommonPartList(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
379      */
380     @Override
381     final public void fillCommonPartList(TL obj, DocumentWrapper<WTL> wrapDoc) throws Exception {
382         throw new UnsupportedOperationException("bulk create/update not yet supported");
383     }
384
385     /* (non-Javadoc)
386      * @see org.collectionspace.services.common.document.DocumentHandler#getCommonPart()
387      */
388     @Override
389     public abstract T getCommonPart();
390
391     /* (non-Javadoc)
392      * @see org.collectionspace.services.common.document.DocumentHandler#setCommonPart(java.lang.Object)
393      */
394     @Override
395     public abstract void setCommonPart(T obj);
396
397     /* (non-Javadoc)
398      * @see org.collectionspace.services.common.document.DocumentHandler#getCommonPartList()
399      */
400     @Override
401     public abstract TL getCommonPartList();
402
403     /* (non-Javadoc)
404      * @see org.collectionspace.services.common.document.DocumentHandler#setCommonPartList(java.lang.Object)
405      */
406     @Override
407     public abstract void setCommonPartList(TL obj);
408
409     /* (non-Javadoc)
410      * @see org.collectionspace.services.common.document.DocumentHandler#getQProperty(java.lang.String)
411      */
412     @Override
413     public abstract String getQProperty(String prop) throws DocumentException;
414
415     /* 
416      * Strip Nuxeo's schema name from the start of the field / element name.
417      * (non-Javadoc)
418      * @see org.collectionspace.services.common.document.DocumentHandler#getUnQProperty(java.lang.String)
419      */
420     @Override
421     public String getUnQProperty(String qProp) {
422         StringTokenizer tkz = new StringTokenizer(qProp, ":");
423         if (tkz.countTokens() != 2) {
424             String msg = "Property must be in the form xxx:yyy, "
425                     + "e.g. collectionobjects_common:objectNumber";
426             logger.error(msg);
427             throw new IllegalArgumentException(msg);
428         }
429         tkz.nextToken(); //skip
430         return tkz.nextToken();
431     }
432     
433     /**
434      * Should return
435      * @throws Exception 
436      * @throws DocumentException 
437      */
438     @Override
439     public String getDocumentsToIndexQuery(String indexId, String csid) throws DocumentException, Exception {
440         return null;
441     }
442
443     /* (non-Javadoc)
444      * @see org.collectionspace.services.common.document.DocumentHandler#getServiceContextPath()
445      */
446     @Override
447     public String getServiceContextPath() {
448         return "/" + getServiceContext().getServiceName().toLowerCase() + "/";
449     }
450
451     /**
452      * Validate.
453      *
454      * @param action the action
455      * @throws Exception the exception
456      */
457     private void validate(Action action) throws Exception {
458         List<ValidatorHandler> valHandlers = serviceContext.getValidatorHandlers();
459         for (ValidatorHandler handler : valHandlers) {
460             handler.validate(action, serviceContext);
461         }
462     }
463     
464     /**
465      * Creates the CMIS query from the service context.  Each document handler is responsible for returning a valid CMIS query using the
466      * information in the current service context -which includes things like the query parameters, etc.
467      * @throws DocumentException 
468      */
469     @Override
470     public String getCMISQuery(QueryContext queryContext) throws DocumentException {
471         //
472         // By default, return nothing.  Child classes can override if they want.
473         //
474         return null;
475     }
476     
477     @Override
478     public boolean isCMISQuery() {
479         return false;
480     }
481     
482     @Override
483     public boolean isJDBCQuery() {
484         return false;
485     }
486     
487     @Override
488     public Map<String,String> getJDBCQueryParams() {
489         return new HashMap<>();
490     }
491     
492 }