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