]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
bb9690a1b28e4eba0b8125b7168c5a9cbedaeccb
[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     /**
79      * @return the DocumentFilter
80      */
81     @Override
82     public DocumentFilter getDocumentFilter() {
83         return docFilter;
84     }
85
86     /**
87      * @param properties the DocumentFilter to set
88      */
89     @Override
90     public void setDocumentFilter(DocumentFilter docFilter) {
91         this.docFilter = docFilter;
92     }
93
94     @Override
95     public void prepare(Action action) throws Exception {
96         //no specific action needed
97     }
98
99     @Override
100     public void handle(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
101         switch(action){
102             case CREATE:
103                 handleCreate((DocumentWrapper<WT>)wrapDoc);
104                 break;
105
106             case UPDATE:
107                 handleUpdate((DocumentWrapper<WT>)wrapDoc);
108                 break;
109
110             case GET:
111                 handleGet((DocumentWrapper<WT>)wrapDoc);
112                 break;
113
114             case GET_ALL:
115                 handleGetAll((DocumentWrapper<WTL>)wrapDoc);
116                 break;
117
118         }
119     }
120
121     @Override
122     public abstract void handleCreate(DocumentWrapper<WT> wrapDoc) throws Exception;
123
124     @Override
125     public abstract void handleUpdate(DocumentWrapper<WT> wrapDoc) throws Exception;
126
127     @Override
128     public abstract void handleGet(DocumentWrapper<WT> wrapDoc) throws Exception;
129
130     @Override
131     public abstract void handleGetAll(DocumentWrapper<WTL> wrapDoc) throws Exception;
132
133     @Override
134     public void complete(Action action, DocumentWrapper<?> wrapDoc) throws Exception {
135         switch(action){
136             //TODO: add more actions if needed
137             case UPDATE:
138                 completeUpdate((DocumentWrapper<WT>)wrapDoc);
139                 break;
140         }
141     }
142
143     @Override
144     public void completeUpdate(DocumentWrapper<WT> wrapDoc) throws Exception {
145         //no specific action needed
146     }
147
148     @Override
149     public abstract void extractAllParts(DocumentWrapper<WT> wrapDoc)
150             throws Exception;
151
152     @Override
153     public abstract void fillAllParts(DocumentWrapper<WT> wrapDoc)
154             throws Exception;
155
156     @Override
157     public abstract T extractCommonPart(DocumentWrapper<WT> wrapDoc)
158             throws Exception;
159
160     @Override
161     public abstract void fillCommonPart(T obj, DocumentWrapper<WT> wrapDoc)
162             throws Exception;
163
164     @Override
165     public abstract TL extractCommonPartList(DocumentWrapper<WTL> wrapDoc)
166             throws Exception;
167
168     @Override
169     final public void fillCommonPartList(TL obj, DocumentWrapper<WTL> wrapDoc) throws Exception {
170         throw new UnsupportedOperationException("bulk create/update not yet supported");
171     }
172
173     @Override
174     public abstract T getCommonPart();
175
176     @Override
177     public abstract void setCommonPart(T obj);
178
179     @Override
180     public abstract TL getCommonPartList();
181
182     @Override
183     public abstract void setCommonPartList(TL obj);
184
185     @Override
186     public abstract String getQProperty(String prop);
187
188     @Override
189     public String getUnQProperty(String qProp) {
190         StringTokenizer tkz = new StringTokenizer(qProp, ":");
191         if(tkz.countTokens() != 2){
192             String msg = "Property must be in the form xxx:yyy, " +
193                     "e.g. collectionobjects_common:objectNumber";
194             logger.error(msg);
195             throw new IllegalArgumentException(msg);
196         }
197         tkz.nextToken(); //skip
198         return tkz.nextToken();
199     }
200
201     @Override
202     public String getServiceContextPath() {
203         return "/" + getServiceContext().getServiceName().toLowerCase() + "/";
204     }
205 }