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