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