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.intake.nuxeo;
26 import java.util.Iterator;
27 import java.util.List;
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;
42 * IntakeDocumentModelHandler
44 * $LastChangedRevision: $
47 public class IntakeRepresenationHandler
48 extends RepresentationHandler<Intake, IntakeList> {
50 private final Logger logger = LoggerFactory.getLogger(IntakeRepresenationHandler.class);
52 * intakeObj is used to stash JAXB object to use when handle is called
53 * for Action.CREATE, Action.UPDATE or Action.GET
55 private Intake intake;
57 * intakeListObject is stashed when handle is called
60 private IntakeList intakeList;
63 public void prepare(Action action) throws Exception {
71 private void prepare() {
72 Map<String, String> queryParams = getQueryParams();
73 Intake intakeObject = getCommonObject();
74 if(intakeObject.getCurrentOwner() != null){
75 queryParams.put(IntakeConstants.NUXEO_SCHEMA_NAME + ":" +
76 IntakeJAXBSchema.CURRENT_OWNER, intakeObject.getCurrentOwner());
79 if(intakeObject.getDepositor() != null){
80 queryParams.put(IntakeConstants.NUXEO_SCHEMA_NAME + ":" +
81 IntakeJAXBSchema.DEPOSITOR, intakeObject.getDepositor());
84 if(intakeObject.getDepositorsRequirements() != null){
85 queryParams.put(IntakeConstants.NUXEO_SCHEMA_NAME + ":" +
86 IntakeJAXBSchema.DEPOSITORS_REQUIREMENTS, intakeObject.getDepositorsRequirements());
89 if(intakeObject.getEntryDate() != null){
90 queryParams.put(IntakeConstants.NUXEO_SCHEMA_NAME + ":" +
91 IntakeJAXBSchema.ENTRY_DATE, intakeObject.getEntryDate());
94 if(intakeObject.getEntryMethod() != null){
95 queryParams.put(IntakeConstants.NUXEO_SCHEMA_NAME + ":" +
96 IntakeJAXBSchema.ENTRY_METHOD, intakeObject.getEntryMethod());
99 if(intakeObject.getEntryNote() != null){
100 queryParams.put(IntakeConstants.NUXEO_SCHEMA_NAME + ":" +
101 IntakeJAXBSchema.ENTRY_NOTE, intakeObject.getEntryNote());
104 if(intakeObject.getEntryNumber() != null){
105 queryParams.put(IntakeConstants.NUXEO_SCHEMA_NAME + ":" +
106 IntakeJAXBSchema.ENTRY_NUMBER, intakeObject.getEntryNumber());
109 if(intakeObject.getEntryReason() != null){
110 queryParams.put(IntakeConstants.NUXEO_SCHEMA_NAME + ":" +
111 IntakeJAXBSchema.ENTRY_REASON, intakeObject.getEntryReason());
114 if(intakeObject.getPackingNote() != null){
115 queryParams.put(IntakeConstants.NUXEO_SCHEMA_NAME + ":" +
116 IntakeJAXBSchema.PACKING_NOTE, intakeObject.getPackingNote());
119 if(intakeObject.getReturnDate() != null){
120 queryParams.put(IntakeConstants.NUXEO_SCHEMA_NAME + ":" +
121 IntakeJAXBSchema.RETURN_DATE, intakeObject.getReturnDate());
126 public Intake extractCommonObject(DocumentWrapper wrapDoc)
128 Document document = (Document) wrapDoc.getWrappedObject();
129 Intake intakeObj = new Intake();
131 //FIXME property get should be dynamically set using schema inspection
132 //so it does not require hard coding
133 Element root = document.getRootElement();
135 // TODO: recognize schema thru namespace uri
136 // Namespace ns = new Namespace("intakeObj",
137 // "http://collectionspace.org/intakeObj");
139 Iterator<Element> siter = root.elementIterator("schema");
140 while(siter.hasNext()){
142 Element schemaElement = siter.next();
143 if(logger.isDebugEnabled()){
144 logger.debug("getCommonObject() populating Common Object");
146 // TODO: recognize schema thru namespace uri
147 if(IntakeConstants.NUXEO_SCHEMA_NAME.equals(schemaElement.attribute("name").getValue())){
148 Element ele = schemaElement.element(IntakeJAXBSchema.CURRENT_OWNER);
150 intakeObj.setCurrentOwner((String) ele.getData());
152 ele = schemaElement.element(IntakeJAXBSchema.DEPOSITOR);
154 intakeObj.setDepositor((String) ele.getData());
156 ele = schemaElement.element(IntakeJAXBSchema.DEPOSITORS_REQUIREMENTS);
158 intakeObj.setDepositorsRequirements((String) ele.getData());
160 ele = schemaElement.element(IntakeJAXBSchema.ENTRY_DATE);
162 intakeObj.setEntryDate((String) ele.getData());
164 ele = schemaElement.element(IntakeJAXBSchema.ENTRY_METHOD);
166 intakeObj.setEntryMethod((String) ele.getData());
168 ele = schemaElement.element(IntakeJAXBSchema.ENTRY_NOTE);
170 intakeObj.setEntryNote((String) ele.getData());
172 ele = schemaElement.element(IntakeJAXBSchema.ENTRY_NUMBER);
174 intakeObj.setEntryNumber((String) ele.getData());
176 ele = schemaElement.element(IntakeJAXBSchema.ENTRY_REASON);
178 intakeObj.setEntryReason((String) ele.getData());
180 ele = schemaElement.element(IntakeJAXBSchema.PACKING_NOTE);
182 intakeObj.setPackingNote((String) ele.getData());
184 ele = schemaElement.element(IntakeJAXBSchema.RETURN_DATE);
186 intakeObj.setReturnDate((String) ele.getData());
194 public void fillCommonObject(Intake co, DocumentWrapper wrapDoc)
196 //Nuxeo REST takes create/update through queryParams, nothing to do here
200 public IntakeList extractCommonObjectList(DocumentWrapper wrapDoc) throws Exception {
201 Document document = (Document) wrapDoc.getWrappedObject();
202 if(logger.isDebugEnabled()){
203 logger.debug(document.asXML());
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();){
210 Element element = (Element) i.next();
211 if(logger.isDebugEnabled()){
212 logger.debug(element.asXML());
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);
222 return intakeListObject;
226 public void fillCommonObjectList(IntakeList obj, DocumentWrapper wrapDoc)
228 throw new UnsupportedOperationException();
232 public Intake getCommonObject() {
237 public void setCommonObject(Intake obj) {
242 public IntakeList getCommonObjectList() {
247 public void setCommonObjectList(IntakeList obj) {
248 this.intakeList = obj;
252 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#getDocumentType()
254 public String getDocumentType() {
255 return IntakeConstants.NUXEO_DOCTYPE;
259 * getQProperty converts the given property to qualified schema property
263 private String getQProperty(String prop) {
264 return IntakeConstants.NUXEO_SCHEMA_NAME + ":" + prop;