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;
29 import java.util.StringTokenizer;
30 import org.collectionspace.services.common.context.ServiceContext;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * AbstractDocumentHandler
38 * $LastChangedRevision: $
41 public abstract class AbstractDocumentHandler<T, TL, WT, WTL>
42 implements DocumentHandler<T, TL, WT, WTL> {
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;
49 public AbstractDocumentHandler() {
53 public ServiceContext getServiceContext() {
54 return serviceContext;
58 public void setServiceContext(ServiceContext ctx) {
63 * @return the properties
66 public Map<String, Object> getProperties() {
71 * @param properties the properties to set
74 public void setProperties(Map<String, Object> properties) {
75 this.properties = properties;
79 public abstract DocumentFilter createDocumentFilter();
82 * @return the DocumentFilter
85 public DocumentFilter getDocumentFilter() {
90 * @param properties the DocumentFilter to set
93 public void setDocumentFilter(DocumentFilter docFilter) {
94 this.docFilter = docFilter;
98 final public void prepare(Action action) throws Exception {
120 public void prepareCreate() throws Exception {
124 public void prepareUpdate() throws Exception {
128 public void prepareGet() throws Exception {
132 public void prepareGetAll() throws Exception {
136 final public void handle(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
139 handleCreate((DocumentWrapper<WT>) wrapDoc);
143 handleUpdate((DocumentWrapper<WT>) wrapDoc);
147 handleGet((DocumentWrapper<WT>) wrapDoc);
151 handleGetAll((DocumentWrapper<WTL>) wrapDoc);
158 public abstract void handleCreate(DocumentWrapper<WT> wrapDoc) throws Exception;
161 public abstract void handleUpdate(DocumentWrapper<WT> wrapDoc) throws Exception;
164 public abstract void handleGet(DocumentWrapper<WT> wrapDoc) throws Exception;
167 public abstract void handleGetAll(DocumentWrapper<WTL> wrapDoc) throws Exception;
170 final public void complete(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
173 completeCreate((DocumentWrapper<WT>) wrapDoc);
177 completeUpdate((DocumentWrapper<WT>) wrapDoc);
181 completeGet((DocumentWrapper<WT>) wrapDoc);
185 completeGetAll((DocumentWrapper<WTL>) wrapDoc);
191 * completeCreate is called by the client to indicate completion of the create call.
196 public void completeCreate(DocumentWrapper<WT> wrapDoc) throws Exception {
200 public void completeUpdate(DocumentWrapper<WT> wrapDoc) throws Exception {
201 //no specific action needed
205 public void completeGet(DocumentWrapper<WT> wrapDoc) throws Exception {
209 public void completeGetAll(DocumentWrapper<WTL> wrapDoc) throws Exception {
213 public abstract T extractCommonPart(DocumentWrapper<WT> wrapDoc)
217 public abstract void fillCommonPart(T obj, DocumentWrapper<WT> wrapDoc)
221 public abstract TL extractCommonPartList(DocumentWrapper<WTL> wrapDoc)
225 final public void fillCommonPartList(TL obj, DocumentWrapper<WTL> wrapDoc) throws Exception {
226 throw new UnsupportedOperationException("bulk create/update not yet supported");
230 public abstract T getCommonPart();
233 public abstract void setCommonPart(T obj);
236 public abstract TL getCommonPartList();
239 public abstract void setCommonPartList(TL obj);
242 public abstract String getQProperty(String prop);
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";
251 throw new IllegalArgumentException(msg);
253 tkz.nextToken(); //skip
254 return tkz.nextToken();
258 public String getServiceContextPath() {
259 return "/" + getServiceContext().getServiceName().toLowerCase() + "/";