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