]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9582978bd7215ec902434005b707649f13838e1c
[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  */
42 public abstract class AbstractDocumentHandlerImpl<T, TL, WT, WTL>
43         implements DocumentHandler<T, TL, WT, WTL> {
44
45     private final Logger logger = LoggerFactory.getLogger(AbstractDocumentHandlerImpl.class);
46     private Map<String, Object> properties = new HashMap<String, Object>();
47     private DocumentFilter docFilter = null;
48     private ServiceContext serviceContext;
49
50     public AbstractDocumentHandlerImpl() {
51     }
52
53     @Override
54     public ServiceContext getServiceContext() {
55         return serviceContext;
56     }
57
58     @Override
59     public void setServiceContext(ServiceContext ctx) {
60         serviceContext = ctx;
61     }
62
63     /**
64      * @return the properties
65      */
66     @Override
67     public Map<String, Object> getProperties() {
68         return properties;
69     }
70
71     /**
72      * @param properties the properties to set
73      */
74     @Override
75     public void setProperties(Map<String, Object> properties) {
76         this.properties = properties;
77     }
78
79 //    public void initializeDocumentFilter(ServiceContext ctx) {
80 //      DocumentFilter docFilter = this.createDocumentFilter(ctx);
81 //      this.setDocumentFilter(docFilter);
82 //    }
83     @Override
84     public abstract DocumentFilter createDocumentFilter();
85
86     /**
87      * @return the DocumentFilter
88      */
89     @Override
90     public DocumentFilter getDocumentFilter() {
91         return docFilter;
92     }
93
94     /**
95      * @param properties the DocumentFilter to set
96      */
97     @Override
98     public void setDocumentFilter(DocumentFilter docFilter) {
99         this.docFilter = docFilter;
100     }
101
102     @Override
103     final public void prepare(Action action) throws Exception {
104         switch (action) {
105             case CREATE:
106                 validate(action);
107                 prepareCreate();
108                 break;
109
110             case UPDATE:
111                 validate(action);
112                 prepareUpdate();
113                 break;
114
115             case GET:
116                 prepareGet();
117                 break;
118
119             case GET_ALL:
120                 prepareGetAll();
121                 break;
122
123             case DELETE:
124                 prepareDelete();
125                 break;
126
127         }
128     }
129
130     @Override
131     public void prepareCreate() throws Exception {
132     }
133
134     @Override
135     public void prepareUpdate() throws Exception {
136     }
137
138     @Override
139     public void prepareGet() throws Exception {
140     }
141
142     @Override
143     public void prepareGetAll() throws Exception {
144     }
145
146     @Override
147     public void prepareDelete() throws Exception {
148     }
149
150     @Override
151     final public void handle(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
152         switch (action) {
153             case CREATE:
154                 handleCreate((DocumentWrapper<WT>) wrapDoc);
155                 break;
156
157             case UPDATE:
158                 handleUpdate((DocumentWrapper<WT>) wrapDoc);
159                 break;
160
161             case GET:
162                 handleGet((DocumentWrapper<WT>) wrapDoc);
163                 break;
164
165             case GET_ALL:
166                 handleGetAll((DocumentWrapper<WTL>) wrapDoc);
167                 break;
168
169             case DELETE:
170                 handleDelete((DocumentWrapper<WT>) wrapDoc);
171                 break;
172
173         }
174     }
175
176     @Override
177     public abstract void handleCreate(DocumentWrapper<WT> wrapDoc) throws Exception;
178
179     @Override
180     public abstract void handleUpdate(DocumentWrapper<WT> wrapDoc) throws Exception;
181
182     @Override
183     public abstract void handleGet(DocumentWrapper<WT> wrapDoc) throws Exception;
184
185     @Override
186     public abstract void handleGetAll(DocumentWrapper<WTL> wrapDoc) throws Exception;
187
188     @Override
189     public void handleDelete(DocumentWrapper<WT> wrapDoc) throws Exception {
190         
191     }
192
193     @Override
194     final public void complete(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
195         switch (action) {
196             case CREATE:
197                 completeCreate((DocumentWrapper<WT>) wrapDoc);
198                 break;
199
200             case UPDATE:
201                 completeUpdate((DocumentWrapper<WT>) wrapDoc);
202                 break;
203
204             case GET:
205                 completeGet((DocumentWrapper<WT>) wrapDoc);
206                 break;
207
208             case GET_ALL:
209                 completeGetAll((DocumentWrapper<WTL>) wrapDoc);
210                 break;
211
212             case DELETE:
213                 completeDelete((DocumentWrapper<WT>) wrapDoc);
214                 break;
215         }
216     }
217
218     @Override
219     public void completeCreate(DocumentWrapper<WT> wrapDoc) throws Exception {
220     }
221
222     @Override
223     public void completeUpdate(DocumentWrapper<WT> wrapDoc) throws Exception {
224         //no specific action needed
225     }
226
227     @Override
228     public void completeGet(DocumentWrapper<WT> wrapDoc) throws Exception {
229     }
230
231     @Override
232     public void completeGetAll(DocumentWrapper<WTL> wrapDoc) throws Exception {
233     }
234
235     @Override
236     public void completeDelete(DocumentWrapper<WT> wrapDoc) throws Exception {
237     }
238
239     @Override
240     public abstract T extractCommonPart(DocumentWrapper<WT> wrapDoc)
241             throws Exception;
242
243     @Override
244     public abstract void fillCommonPart(T obj, DocumentWrapper<WT> wrapDoc)
245             throws Exception;
246
247     @Override
248     public abstract TL extractCommonPartList(DocumentWrapper<WTL> wrapDoc)
249             throws Exception;
250
251     @Override
252     final public void fillCommonPartList(TL obj, DocumentWrapper<WTL> wrapDoc) throws Exception {
253         throw new UnsupportedOperationException("bulk create/update not yet supported");
254     }
255
256     @Override
257     public abstract T getCommonPart();
258
259     @Override
260     public abstract void setCommonPart(T obj);
261
262     @Override
263     public abstract TL getCommonPartList();
264
265     @Override
266     public abstract void setCommonPartList(TL obj);
267
268     @Override
269     public abstract String getQProperty(String prop);
270
271     @Override
272     public String getUnQProperty(String qProp) {
273         StringTokenizer tkz = new StringTokenizer(qProp, ":");
274         if (tkz.countTokens() != 2) {
275             String msg = "Property must be in the form xxx:yyy, "
276                     + "e.g. collectionobjects_common:objectNumber";
277             logger.error(msg);
278             throw new IllegalArgumentException(msg);
279         }
280         tkz.nextToken(); //skip
281         return tkz.nextToken();
282     }
283
284     @Override
285     public String getServiceContextPath() {
286         return "/" + getServiceContext().getServiceName().toLowerCase() + "/";
287     }
288
289     private void validate(Action action) throws Exception {
290         List<ValidatorHandler> valHandlers = serviceContext.getValidatorHandlers();
291         for (ValidatorHandler handler : valHandlers) {
292             handler.validate(action, serviceContext);
293         }
294     }
295 }