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