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.acquisition.nuxeo;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.StringTokenizer;
30 import org.collectionspace.services.AcquisitionListItemJAXBSchema;
31 import org.collectionspace.services.common.document.DocumentWrapper;
32 import org.collectionspace.services.acquisition.AcquisitionsCommon;
33 import org.collectionspace.services.acquisition.AcquisitionsCommonList;
34 import org.collectionspace.services.acquisition.AcquisitionsCommonList.AcquisitionListItem;
35 import org.collectionspace.services.acquisition.AcquisitionSourceList;
36 import org.collectionspace.services.common.document.DocumentHandler.Action;
37 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
38 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
39 import org.nuxeo.ecm.core.api.DocumentModel;
40 import org.nuxeo.ecm.core.api.DocumentModelList;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * AcquisitionDocumentModelHandler
47 * $LastChangedRevision: $
50 public class AcquisitionDocumentModelHandler
51 extends RemoteDocumentModelHandlerImpl<AcquisitionsCommon, AcquisitionsCommonList> {
53 private final Logger logger = LoggerFactory.getLogger(AcquisitionDocumentModelHandler.class);
55 * acquisition is used to stash JAXB object to use when handle is called
56 * for Action.CREATE, Action.UPDATE or Action.GET
58 private AcquisitionsCommon acquisition;
60 * acquisitionList is stashed when handle is called
63 private AcquisitionsCommonList acquisitionList;
67 * getCommonPart get associated acquisition
71 public AcquisitionsCommon getCommonPart() {
76 * setCommonPart set associated acquisition
80 public void setCommonPart(AcquisitionsCommon acquisition) {
81 this.acquisition = acquisition;
85 * getAcquisitionList get associated acquisition (for index/GET_ALL)
89 public AcquisitionsCommonList getCommonPartList() {
90 return acquisitionList;
94 public void setCommonPartList(AcquisitionsCommonList acquisitionList) {
95 this.acquisitionList = acquisitionList;
99 public AcquisitionsCommon extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc)
101 throw new UnsupportedOperationException();
105 public void fillCommonPart(AcquisitionsCommon acquisitionObject, DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
106 throw new UnsupportedOperationException();
110 public AcquisitionsCommonList extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
111 DocumentModelList docList = wrapDoc.getWrappedObject();
112 AcquisitionsCommonList coList = new AcquisitionsCommonList();
113 List<AcquisitionsCommonList.AcquisitionListItem> list = coList.getAcquisitionListItem();
115 //FIXME: iterating over a long list of documents is not a long term
116 //strategy...need to change to more efficient iterating in future
117 Iterator<DocumentModel> iter = docList.iterator();
118 while (iter.hasNext()) {
119 DocumentModel docModel = iter.next();
120 AcquisitionListItem listItem = new AcquisitionListItem();
121 listItem.setAcquisitionReferenceNumber((String) docModel.getProperty(getServiceContext().getCommonPartLabel(),
122 AcquisitionListItemJAXBSchema.ACQUISITION_REFERENCE_NUMBER));
123 // docModel.getProperty returns an ArrayList here.
124 List<String> acquisitionSources =
125 (List<String>) docModel.getProperty(getServiceContext().getCommonPartLabel(),
126 AcquisitionListItemJAXBSchema.ACQUISITION_SOURCES);
127 AcquisitionSourceList acquisitionSourceList = new AcquisitionSourceList();
128 NameValue nv = new NameValue();
129 for (String acquisitionSource : acquisitionSources) {
131 nv = unqualify(acquisitionSource);
132 acquisitionSourceList.getAcquisitionSource().add(nv.value);
133 } catch (IllegalStateException ise) {
134 logger.warn("acquisition source=" + acquisitionSource, ise);
137 listItem.setAcquisitionSources(acquisitionSourceList);
138 //need fully qualified context for URI
139 String id = NuxeoUtils.extractId(docModel.getPathAsString());
140 listItem.setUri(getServiceContextPath() + id);
141 listItem.setCsid(id);
149 public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
151 super.fillAllParts(wrapDoc);
152 fillDublinCoreObject(wrapDoc); //dublincore might not be needed in future
155 private void fillDublinCoreObject(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
156 DocumentModel docModel = wrapDoc.getWrappedObject();
157 //FIXME property setter should be dynamically set using schema inspection
158 //so it does not require hard coding
159 // a default title for the Dublin Core schema
160 docModel.setPropertyValue("dublincore:title", AcquisitionConstants.NUXEO_DC_TITLE);
165 * getQProperty converts the given property to qualified schema property
170 public String getQProperty(String prop) {
171 return AcquisitionConstants.NUXEO_SCHEMA_NAME + ":" + prop;
174 // The following are all private in DocumentUtils;
175 // might be moved to a common class.
176 private static String NAME_VALUE_SEPARATOR = "|";
178 private static class NameValue {
180 // default scoped constructor to remove "synthetic accessor" warning
186 private static NameValue unqualify(String input) {
187 NameValue nv = new NameValue();
188 StringTokenizer stz = new StringTokenizer(input, NAME_VALUE_SEPARATOR);
189 int tokens = stz.countTokens();
191 nv.name = stz.nextToken();
192 nv.value = stz.nextToken();
193 // Allow null or empty values
194 } else if (tokens == 1) {
195 nv.name = stz.nextToken();
198 throw new IllegalStateException("Unexpected format for multi valued element: " + input);