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