]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
03a6d3d442b6cd5000bbae824ffba19be3aec5e7
[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.repository;
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>
42         implements DocumentHandler<T, TL> {
43
44     private final Logger logger = LoggerFactory.getLogger(AbstractDocumentHandler.class);
45     private Map<String, Object> properties = new HashMap<String, Object>();
46     private ServiceContext serviceContext;
47
48     public AbstractDocumentHandler() {
49     }
50
51     @Override
52     public ServiceContext getServiceContext() {
53         return serviceContext;
54     }
55
56     @Override
57     public void setServiceContext(ServiceContext ctx) {
58         serviceContext = ctx;
59     }
60
61     /**
62      * @return the properties
63      */
64     @Override
65     public Map<String, Object> getProperties() {
66         return properties;
67     }
68
69     /**
70      * @param properties the properties to set
71      */
72     @Override
73     public void setProperties(Map<String, Object> properties) {
74         this.properties = properties;
75     }
76
77     @Override
78     public void prepare(Action action) throws Exception {
79         //no specific action needed
80     }
81
82     @Override
83     public void handle(Action action, DocumentWrapper wrapDoc) throws Exception {
84         switch(action){
85             case CREATE:
86                 handleCreate(wrapDoc);
87                 break;
88
89             case UPDATE:
90                 handleUpdate(wrapDoc);
91                 break;
92
93             case GET:
94                 handleGet(wrapDoc);
95                 break;
96
97             case GET_ALL:
98                 handleGetAll(wrapDoc);
99                 break;
100
101         }
102     }
103
104     @Override
105     public abstract void handleCreate(DocumentWrapper wrapDoc) throws Exception;
106
107     @Override
108     public abstract void handleUpdate(DocumentWrapper wrapDoc) throws Exception;
109
110     @Override
111     public abstract void handleGet(DocumentWrapper wrapDoc) throws Exception;
112
113     @Override
114     public abstract void handleGetAll(DocumentWrapper wrapDoc) throws Exception;
115
116     @Override
117     public void complete(Action action, DocumentWrapper wrapDoc) throws Exception {
118         switch(action){
119             //TODO: add more actions if needed
120             case UPDATE:
121                 completeUpdate(wrapDoc);
122                 break;
123         }
124     }
125
126     @Override
127     public void completeUpdate(DocumentWrapper wrapDoc) throws Exception {
128         //no specific action needed
129     }
130
131     @Override
132     public abstract void extractAllParts(DocumentWrapper wrapDoc)
133             throws Exception;
134
135     @Override
136     public abstract void fillAllParts(DocumentWrapper wrapDoc)
137             throws Exception;
138
139     @Override
140     public abstract T extractCommonPart(DocumentWrapper wrapDoc)
141             throws Exception;
142
143     @Override
144     public abstract void fillCommonPart(T obj, DocumentWrapper wrapDoc)
145             throws Exception;
146
147     @Override
148     public abstract TL extractCommonPartList(DocumentWrapper wrapDoc)
149             throws Exception;
150
151     @Override
152     final public void fillCommonPartList(TL obj, DocumentWrapper wrapDoc) throws Exception {
153         throw new UnsupportedOperationException("bulk create/update not yet supported");
154     }
155
156     @Override
157     public abstract T getCommonPart();
158
159     @Override
160     public abstract void setCommonPart(T obj);
161
162     @Override
163     public abstract TL getCommonPartList();
164
165     @Override
166     public abstract void setCommonPartList(TL obj);
167
168     @Override
169     public abstract String getQProperty(String prop);
170
171     @Override
172     public String getUnQProperty(String qProp) {
173         StringTokenizer tkz = new StringTokenizer(qProp, ":");
174         if(tkz.countTokens() != 2){
175             String msg = "Property must be in the form xxx:yyy, " +
176                     "e.g. collectionobjects_common:objectNumber";
177             logger.error(msg);
178             throw new IllegalArgumentException(msg);
179         }
180         tkz.nextToken(); //skip
181         return tkz.nextToken();
182     }
183
184     @Override
185     public String getServiceContextPath() {
186         return "/" + getServiceContext().getServiceName().toLowerCase() + "/";
187     }
188 }