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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.common.document;
26 import java.util.HashMap;
27 import java.util.List;
30 import java.util.StringTokenizer;
31 import org.collectionspace.services.common.context.ServiceContext;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * AbstractDocumentHandler
39 * $LastChangedRevision: $
42 public abstract class AbstractDocumentHandler<T, TL, WT, WTL>
43 implements DocumentHandler<T, TL, WT, WTL> {
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;
50 public AbstractDocumentHandler() {
54 public ServiceContext getServiceContext() {
55 return serviceContext;
59 public void setServiceContext(ServiceContext ctx) {
64 * @return the properties
67 public Map<String, Object> getProperties() {
72 * @param properties the properties to set
75 public void setProperties(Map<String, Object> properties) {
76 this.properties = properties;
80 public abstract DocumentFilter createDocumentFilter();
83 * @return the DocumentFilter
86 public DocumentFilter getDocumentFilter() {
91 * @param properties the DocumentFilter to set
94 public void setDocumentFilter(DocumentFilter docFilter) {
95 this.docFilter = docFilter;
99 final public void prepare(Action action) throws Exception {
123 public void prepareCreate() throws Exception {
127 public void prepareUpdate() throws Exception {
131 public void prepareGet() throws Exception {
135 public void prepareGetAll() throws Exception {
139 final public void handle(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
142 handleCreate((DocumentWrapper<WT>) wrapDoc);
146 handleUpdate((DocumentWrapper<WT>) wrapDoc);
150 handleGet((DocumentWrapper<WT>) wrapDoc);
154 handleGetAll((DocumentWrapper<WTL>) wrapDoc);
161 public abstract void handleCreate(DocumentWrapper<WT> wrapDoc) throws Exception;
164 public abstract void handleUpdate(DocumentWrapper<WT> wrapDoc) throws Exception;
167 public abstract void handleGet(DocumentWrapper<WT> wrapDoc) throws Exception;
170 public abstract void handleGetAll(DocumentWrapper<WTL> wrapDoc) throws Exception;
173 final public void complete(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
176 completeCreate((DocumentWrapper<WT>) wrapDoc);
180 completeUpdate((DocumentWrapper<WT>) wrapDoc);
184 completeGet((DocumentWrapper<WT>) wrapDoc);
188 completeGetAll((DocumentWrapper<WTL>) wrapDoc);
194 * completeCreate is called by the client to indicate completion of the create call.
199 public void completeCreate(DocumentWrapper<WT> wrapDoc) throws Exception {
203 public void completeUpdate(DocumentWrapper<WT> wrapDoc) throws Exception {
204 //no specific action needed
208 public void completeGet(DocumentWrapper<WT> wrapDoc) throws Exception {
212 public void completeGetAll(DocumentWrapper<WTL> wrapDoc) throws Exception {
216 public abstract T extractCommonPart(DocumentWrapper<WT> wrapDoc)
220 public abstract void fillCommonPart(T obj, DocumentWrapper<WT> wrapDoc)
224 public abstract TL extractCommonPartList(DocumentWrapper<WTL> wrapDoc)
228 final public void fillCommonPartList(TL obj, DocumentWrapper<WTL> wrapDoc) throws Exception {
229 throw new UnsupportedOperationException("bulk create/update not yet supported");
233 public abstract T getCommonPart();
236 public abstract void setCommonPart(T obj);
239 public abstract TL getCommonPartList();
242 public abstract void setCommonPartList(TL obj);
245 public abstract String getQProperty(String prop);
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";
254 throw new IllegalArgumentException(msg);
256 tkz.nextToken(); //skip
257 return tkz.nextToken();
261 public String getServiceContextPath() {
262 return "/" + getServiceContext().getServiceName().toLowerCase() + "/";
265 private void validate(Action action) throws Exception {
266 List<ValidatorHandler> valHandlers = serviceContext.getValidatorHandlers();
267 for (ValidatorHandler handler : valHandlers) {
268 handler.validate(action, serviceContext);