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 AbstractDocumentHandlerImpl<T, TL, WT, WTL>
43 implements DocumentHandler<T, TL, WT, WTL> {
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;
50 public AbstractDocumentHandlerImpl() {
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;
79 // public void initializeDocumentFilter(ServiceContext ctx) {
80 // DocumentFilter docFilter = this.createDocumentFilter(ctx);
81 // this.setDocumentFilter(docFilter);
85 public abstract DocumentFilter createDocumentFilter();
88 * @return the DocumentFilter
91 public DocumentFilter getDocumentFilter() {
96 * @param properties the DocumentFilter to set
99 public void setDocumentFilter(DocumentFilter docFilter) {
100 this.docFilter = docFilter;
104 final public void prepare(Action action) throws Exception {
128 public void prepareCreate() throws Exception {
132 public void prepareUpdate() throws Exception {
136 public void prepareGet() throws Exception {
140 public void prepareGetAll() throws Exception {
144 final public void handle(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
147 handleCreate((DocumentWrapper<WT>) wrapDoc);
151 handleUpdate((DocumentWrapper<WT>) wrapDoc);
155 handleGet((DocumentWrapper<WT>) wrapDoc);
159 handleGetAll((DocumentWrapper<WTL>) wrapDoc);
166 public abstract void handleCreate(DocumentWrapper<WT> wrapDoc) throws Exception;
169 public abstract void handleUpdate(DocumentWrapper<WT> wrapDoc) throws Exception;
172 public abstract void handleGet(DocumentWrapper<WT> wrapDoc) throws Exception;
175 public abstract void handleGetAll(DocumentWrapper<WTL> wrapDoc) throws Exception;
178 final public void complete(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
181 completeCreate((DocumentWrapper<WT>) wrapDoc);
185 completeUpdate((DocumentWrapper<WT>) wrapDoc);
189 completeGet((DocumentWrapper<WT>) wrapDoc);
193 completeGetAll((DocumentWrapper<WTL>) wrapDoc);
199 * completeCreate is called by the client to indicate completion of the create call.
204 public void completeCreate(DocumentWrapper<WT> wrapDoc) throws Exception {
208 public void completeUpdate(DocumentWrapper<WT> wrapDoc) throws Exception {
209 //no specific action needed
213 public void completeGet(DocumentWrapper<WT> wrapDoc) throws Exception {
217 public void completeGetAll(DocumentWrapper<WTL> wrapDoc) throws Exception {
221 public abstract T extractCommonPart(DocumentWrapper<WT> wrapDoc)
225 public abstract void fillCommonPart(T obj, DocumentWrapper<WT> wrapDoc)
229 public abstract TL extractCommonPartList(DocumentWrapper<WTL> wrapDoc)
233 final public void fillCommonPartList(TL obj, DocumentWrapper<WTL> wrapDoc) throws Exception {
234 throw new UnsupportedOperationException("bulk create/update not yet supported");
238 public abstract T getCommonPart();
241 public abstract void setCommonPart(T obj);
244 public abstract TL getCommonPartList();
247 public abstract void setCommonPartList(TL obj);
250 public abstract String getQProperty(String prop);
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";
259 throw new IllegalArgumentException(msg);
261 tkz.nextToken(); //skip
262 return tkz.nextToken();
266 public String getServiceContextPath() {
267 return "/" + getServiceContext().getServiceName().toLowerCase() + "/";
270 private void validate(Action action) throws Exception {
271 List<ValidatorHandler> valHandlers = serviceContext.getValidatorHandlers();
272 for (ValidatorHandler handler : valHandlers) {
273 handler.validate(action, serviceContext);