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