]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9738000c8f29d8240c1b99b63ac4164f1203c3fc
[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.nuxeo.client.java;
25
26 import org.collectionspace.services.common.repository.DocumentHandler;
27 import org.collectionspace.services.common.repository.DocumentWrapper;
28 import org.collectionspace.services.common.repository.DocumentException;
29 import java.util.HashMap;
30 import java.util.Map;
31 import org.collectionspace.services.nuxeo.client.*;
32 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
33 import org.dom4j.Document;
34 import org.nuxeo.ecm.core.api.DocumentModel;
35 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * DocumentModelHandler is a base abstract Nuxeo document handler
41  * using Nuxeo Java Remote APIs for CollectionSpace services
42  *
43  * $LastChangedRevision: $
44  * $LastChangedDate: $
45  */
46 public abstract class DocumentModelHandler<T, TL>
47         implements DocumentHandler<T, TL> {
48
49     private final Logger logger = LoggerFactory.getLogger(DocumentModelHandler.class);
50     private Map<String, Object> properties = new HashMap<String, Object>();
51     private RepositoryInstance repositorySession;
52
53     @Override
54     public abstract void prepare(Action action) throws Exception;
55
56     @Override
57     public void handle(Action action, DocumentWrapper wrapDoc) throws Exception {
58         switch(action){
59             case CREATE:
60                 handleCreate(wrapDoc);
61                 break;
62             case UPDATE:
63                 handleUpdate(wrapDoc);
64                 break;
65             case GET:
66                 handleGet(wrapDoc);
67                 break;
68             case GET_ALL:
69                 handleGetAll(wrapDoc);
70                 break;
71         }
72     }
73
74     @Override
75     public void handleCreate(DocumentWrapper wrapDoc) throws Exception {
76         if(getCommonObject() == null){
77             String msg = "Error creating document: Missing input data";
78             logger.error(msg);
79             throw new IllegalStateException(msg);
80         }
81         //FIXME set other parts as well
82         fillCommonObject(getCommonObject(), wrapDoc);
83     }
84
85     @Override
86     public void handleUpdate(DocumentWrapper wrapDoc) throws Exception {
87         if(getCommonObject() == null){
88             String msg = "Error updating document: Missing input data";
89             logger.error(msg);
90             throw new IllegalStateException(msg);
91         }
92         //FIXME set other parts as well
93         fillCommonObject(getCommonObject(), wrapDoc);
94     }
95
96     @Override
97     public void handleGet(DocumentWrapper wrapDoc) throws Exception {
98         setCommonObject(extractCommonObject(wrapDoc));
99
100         //FIXME retrive other parts as well
101     }
102
103     @Override
104     public void handleGetAll(DocumentWrapper wrapDoc) throws Exception {
105         setCommonObjectList(extractCommonObjectList(wrapDoc));
106     }
107
108     /**
109      * getRepositorySession returns Nuxeo Repository Session
110      * @return
111      */
112     public RepositoryInstance getRepositorySession() {
113         return repositorySession;
114     }
115
116     /**
117      * setRepositorySession sets repository session
118      * @param repoSession
119      */
120     public void setRepositorySession(RepositoryInstance repoSession) {
121         this.repositorySession = repoSession;
122     }
123
124     @Override
125     public abstract T extractCommonObject(DocumentWrapper wrapDoc) throws Exception;
126
127     @Override
128     public abstract void fillCommonObject(T obj, DocumentWrapper wrapDoc) throws Exception;
129
130     @Override
131     public abstract TL extractCommonObjectList(DocumentWrapper wrapDoc) throws Exception;
132
133     @Override
134     public abstract void fillCommonObjectList(TL obj, DocumentWrapper wrapDoc) throws Exception;
135
136     @Override
137     public abstract T getCommonObject();
138
139     @Override
140     public abstract void setCommonObject(T obj);
141
142     @Override
143     public abstract TL getCommonObjectList();
144
145     @Override
146     public abstract void setCommonObjectList(TL obj);
147
148     @Override
149     public Document getDocument(DocumentWrapper wrapDoc) throws DocumentException {
150         DocumentModel docModel = (DocumentModel) wrapDoc.getWrappedObject();
151         return NuxeoUtils.getDocument(getRepositorySession(), docModel);
152     }
153
154     /**
155      * @return the properties
156      */
157     @Override
158     public Map<String, Object> getProperties() {
159         return properties;
160     }
161
162     /**
163      * @param properties the properties to set
164      */
165     @Override
166     public void setProperties(Map<String, Object> properties) {
167         this.properties = properties;
168     }
169 }