]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
720022cfe6a616e283b5fad3cf537bcea7a7c631
[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.intake.nuxeo;
25
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.collectionspace.services.IntakeJAXBSchema;
31 import org.collectionspace.services.common.repository.DocumentWrapper;
32 import org.collectionspace.services.intake.Intake;
33 import org.collectionspace.services.intake.IntakeList;
34 import org.collectionspace.services.intake.IntakeList.IntakeListItem;
35 import org.collectionspace.services.nuxeo.client.rest.RepresentationHandler;
36 import org.dom4j.Document;
37 import org.dom4j.Element;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * IntakeDocumentModelHandler
43  *
44  * $LastChangedRevision: $
45  * $LastChangedDate: $
46  */
47 public class IntakeRepresenationHandler
48         extends RepresentationHandler<Intake, IntakeList> {
49
50     private final Logger logger = LoggerFactory.getLogger(IntakeRepresenationHandler.class);
51     /**
52      * intakeObj is used to stash JAXB object to use when handle is called
53      * for Action.CREATE, Action.UPDATE or Action.GET
54      */
55     private Intake intake;
56     /**
57      * intakeListObject is stashed when handle is called
58      * for ACTION.GET_ALL
59      */
60     private IntakeList intakeList;
61
62     @Override
63     public void prepare(Action action) throws Exception {
64         switch(action){
65             case CREATE:
66             case UPDATE:
67                 prepare();
68         }
69     }
70
71     private void prepare() {
72         Map<String, String> queryParams = getQueryParams();
73         Intake intakeObject = getCommonObject();
74         if(intakeObject.getCurrentOwner() != null){
75             queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" +
76                     IntakeJAXBSchema.CURRENT_OWNER, intakeObject.getCurrentOwner());
77         }
78
79         if(intakeObject.getDepositor() != null){
80             queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" +
81                     IntakeJAXBSchema.DEPOSITOR, intakeObject.getDepositor());
82         }
83
84         if(intakeObject.getDepositorsRequirements() != null){
85             queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" +
86                     IntakeJAXBSchema.DEPOSITORS_REQUIREMENTS, intakeObject.getDepositorsRequirements());
87         }
88
89         if(intakeObject.getEntryDate() != null){
90             queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" +
91                     IntakeJAXBSchema.ENTRY_DATE, intakeObject.getEntryDate());
92         }
93
94         if(intakeObject.getEntryMethod() != null){
95             queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" +
96                     IntakeJAXBSchema.ENTRY_METHOD, intakeObject.getEntryMethod());
97         }
98
99         if(intakeObject.getEntryNote() != null){
100             queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" +
101                     IntakeJAXBSchema.ENTRY_NOTE, intakeObject.getEntryNote());
102         }
103
104         if(intakeObject.getEntryNumber() != null){
105             queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" +
106                     IntakeJAXBSchema.ENTRY_NUMBER, intakeObject.getEntryNumber());
107         }
108
109         if(intakeObject.getEntryReason() != null){
110             queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" +
111                     IntakeJAXBSchema.ENTRY_REASON, intakeObject.getEntryReason());
112         }
113
114         if(intakeObject.getPackingNote() != null){
115             queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" +
116                     IntakeJAXBSchema.PACKING_NOTE, intakeObject.getPackingNote());
117         }
118
119         if(intakeObject.getReturnDate() != null){
120             queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" +
121                     IntakeJAXBSchema.RETURN_DATE, intakeObject.getReturnDate());
122         }
123     }
124
125     @Override
126     public Intake extractCommonObject(DocumentWrapper wrapDoc)
127             throws Exception {
128         Document document = (Document) wrapDoc.getWrappedObject();
129         Intake intakeObj = new Intake();
130
131         //FIXME property get should be dynamically set using schema inspection
132         //so it does not require hard coding
133         Element root = document.getRootElement();
134
135         // TODO: recognize schema thru namespace uri
136         // Namespace ns = new Namespace("intakeObj",
137         // "http://collectionspace.org/intakeObj");
138
139         Iterator<Element> siter = root.elementIterator("schema");
140         while(siter.hasNext()){
141
142             Element schemaElement = siter.next();
143             if(logger.isDebugEnabled()){
144                 logger.debug("getCommonObject() populating Common Object");
145             }
146             // TODO: recognize schema thru namespace uri
147             if(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME.equals(schemaElement.attribute("name").getValue())){
148                 Element ele = schemaElement.element(IntakeJAXBSchema.CURRENT_OWNER);
149                 if(ele != null){
150                     intakeObj.setCurrentOwner((String) ele.getData());
151                 }
152                 ele = schemaElement.element(IntakeJAXBSchema.DEPOSITOR);
153                 if(ele != null){
154                     intakeObj.setDepositor((String) ele.getData());
155                 }
156                 ele = schemaElement.element(IntakeJAXBSchema.DEPOSITORS_REQUIREMENTS);
157                 if(ele != null){
158                     intakeObj.setDepositorsRequirements((String) ele.getData());
159                 }
160                 ele = schemaElement.element(IntakeJAXBSchema.ENTRY_DATE);
161                 if(ele != null){
162                     intakeObj.setEntryDate((String) ele.getData());
163                 }
164                 ele = schemaElement.element(IntakeJAXBSchema.ENTRY_METHOD);
165                 if(ele != null){
166                     intakeObj.setEntryMethod((String) ele.getData());
167                 }
168                 ele = schemaElement.element(IntakeJAXBSchema.ENTRY_NOTE);
169                 if(ele != null){
170                     intakeObj.setEntryNote((String) ele.getData());
171                 }
172                 ele = schemaElement.element(IntakeJAXBSchema.ENTRY_NUMBER);
173                 if(ele != null){
174                     intakeObj.setEntryNumber((String) ele.getData());
175                 }
176                 ele = schemaElement.element(IntakeJAXBSchema.ENTRY_REASON);
177                 if(ele != null){
178                     intakeObj.setEntryReason((String) ele.getData());
179                 }
180                 ele = schemaElement.element(IntakeJAXBSchema.PACKING_NOTE);
181                 if(ele != null){
182                     intakeObj.setPackingNote((String) ele.getData());
183                 }
184                 ele = schemaElement.element(IntakeJAXBSchema.RETURN_DATE);
185                 if(ele != null){
186                     intakeObj.setReturnDate((String) ele.getData());
187                 }
188             }
189         }
190         return intakeObj;
191     }
192
193     @Override
194     public void fillCommonObject(Intake co, DocumentWrapper wrapDoc)
195             throws Exception {
196         //Nuxeo REST takes create/update through queryParams, nothing to do here
197     }
198
199     @Override
200     public IntakeList extractCommonObjectList(DocumentWrapper wrapDoc) throws Exception {
201         Document document = (Document) wrapDoc.getWrappedObject();
202         if(logger.isDebugEnabled()){
203             logger.debug(document.asXML());
204         }
205         IntakeList intakeListObject = new IntakeList();
206         List<IntakeList.IntakeListItem> list = intakeListObject.getIntakeListItem();
207         Element root = document.getRootElement();
208         for(Iterator i = root.elementIterator(); i.hasNext();){
209
210             Element element = (Element) i.next();
211             if(logger.isDebugEnabled()){
212                 logger.debug(element.asXML());
213             }
214             // set the intakeObj list item entity elements
215             IntakeListItem ilistItem = new IntakeListItem();
216             ilistItem.setEntryNumber(element.attributeValue("entryNumber"));
217             String id = element.attributeValue("id");
218             ilistItem.setCsid(id);
219             ilistItem.setUri("/intakes/" + id);
220             list.add(ilistItem);
221         }
222         return intakeListObject;
223     }
224
225     @Override
226     public void fillCommonObjectList(IntakeList obj, DocumentWrapper wrapDoc)
227             throws Exception {
228         throw new UnsupportedOperationException();
229     }
230
231     @Override
232     public Intake getCommonObject() {
233         return intake;
234     }
235
236     @Override
237     public void setCommonObject(Intake obj) {
238         this.intake = obj;
239     }
240
241     @Override
242     public IntakeList getCommonObjectList() {
243         return intakeList;
244     }
245
246     @Override
247     public void setCommonObjectList(IntakeList obj) {
248         this.intakeList = obj;
249     }
250
251     /**
252      * getQProperty converts the given property to qualified schema property
253      * @param prop
254      * @return
255      */
256     private String getQProperty(String prop) {
257         return IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + prop;
258     }
259 }
260