]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f0e54a5c12928b74479fe61278ceb962275f95c6
[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.acquisition.nuxeo;
25
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.StringTokenizer;
29
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;
43
44 /**
45  * AcquisitionDocumentModelHandler
46  *
47  * $LastChangedRevision: $
48  * $LastChangedDate: $
49  */
50 public class AcquisitionDocumentModelHandler
51         extends RemoteDocumentModelHandlerImpl<AcquisitionsCommon, AcquisitionsCommonList> {
52
53     private final Logger logger = LoggerFactory.getLogger(AcquisitionDocumentModelHandler.class);
54     /**
55      * acquisition is used to stash JAXB object to use when handle is called
56      * for Action.CREATE, Action.UPDATE or Action.GET
57      */
58     private AcquisitionsCommon acquisition;
59     /**
60      * acquisitionList is stashed when handle is called
61      * for ACTION.GET_ALL
62      */
63     private AcquisitionsCommonList acquisitionList;
64
65
66     /**
67      * getCommonPart get associated acquisition
68      * @return
69      */
70     @Override
71     public AcquisitionsCommon getCommonPart() {
72         return acquisition;
73     }
74
75     /**
76      * setCommonPart set associated acquisition
77      * @param acquisition
78      */
79     @Override
80     public void setCommonPart(AcquisitionsCommon acquisition) {
81         this.acquisition = acquisition;
82     }
83
84     /**
85      * getAcquisitionList get associated acquisition (for index/GET_ALL)
86      * @return
87      */
88     @Override
89     public AcquisitionsCommonList getCommonPartList() {
90         return acquisitionList;
91     }
92
93     @Override
94     public void setCommonPartList(AcquisitionsCommonList acquisitionList) {
95         this.acquisitionList = acquisitionList;
96     }
97
98     @Override
99     public AcquisitionsCommon extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc)
100             throws Exception {
101         throw new UnsupportedOperationException();
102     }
103
104     @Override
105     public void fillCommonPart(AcquisitionsCommon acquisitionObject, DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
106         throw new UnsupportedOperationException();
107     }
108
109     @Override
110     public AcquisitionsCommonList extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
111         AcquisitionsCommonList coList = this.extractPagingInfo(new AcquisitionsCommonList(), wrapDoc);
112         List<AcquisitionsCommonList.AcquisitionListItem> list = coList.getAcquisitionListItem();
113         Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();
114         while (iter.hasNext()) {
115             DocumentModel docModel = iter.next();
116             AcquisitionListItem listItem = new AcquisitionListItem();
117             listItem.setAcquisitionReferenceNumber((String) docModel.getProperty(getServiceContext().getCommonPartLabel(),
118                     AcquisitionListItemJAXBSchema.ACQUISITION_REFERENCE_NUMBER));
119             // docModel.getProperty returns an ArrayList here.
120             List<String> acquisitionSources =
121                  (List<String>) docModel.getProperty(getServiceContext().getCommonPartLabel(),
122                      AcquisitionListItemJAXBSchema.ACQUISITION_SOURCES);
123             AcquisitionSourceList acquisitionSourceList = new AcquisitionSourceList();
124             NameValue nv = new NameValue();
125             for (String acquisitionSource : acquisitionSources) {
126                 try {
127                   nv = unqualify(acquisitionSource);
128                   acquisitionSourceList.getAcquisitionSource().add(nv.value);
129                 } catch (IllegalStateException ise) {
130                     logger.warn("acquisition source=" + acquisitionSource, ise);
131                 }
132             }
133             listItem.setAcquisitionSources(acquisitionSourceList);
134             //need fully qualified context for URI
135             String id = NuxeoUtils.extractId(docModel.getPathAsString());
136             listItem.setUri(getServiceContextPath() + id);
137             listItem.setCsid(id);
138             list.add(listItem);
139         }
140
141         return coList;
142     }
143
144     @Override
145     public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
146
147         super.fillAllParts(wrapDoc, action);
148         fillDublinCoreObject(wrapDoc); //dublincore might not be needed in future
149     }
150
151     private void fillDublinCoreObject(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
152         DocumentModel docModel = wrapDoc.getWrappedObject();
153         //FIXME property setter should be dynamically set using schema inspection
154         //so it does not require hard coding
155         // a default title for the Dublin Core schema
156         docModel.setPropertyValue("dublincore:title", AcquisitionConstants.NUXEO_DC_TITLE);
157     }
158
159
160     /**
161      * getQProperty converts the given property to qualified schema property
162      * @param prop
163      * @return
164      */
165     @Override
166     public String getQProperty(String prop) {
167         return AcquisitionConstants.NUXEO_SCHEMA_NAME + ":" + prop;
168     }
169
170     // The following are all private in DocumentUtils;
171     // might be moved to a common class.
172     private static String NAME_VALUE_SEPARATOR = "|";
173
174     private static class NameValue {
175         NameValue() {
176                         // default scoped constructor to remove "synthetic accessor" warning
177                 }
178         String name;
179         String value;
180     };
181
182     private static NameValue unqualify(String input) {
183         NameValue nv = new NameValue();
184         StringTokenizer stz = new StringTokenizer(input, NAME_VALUE_SEPARATOR);
185         int tokens = stz.countTokens();
186         if (tokens == 2) {
187             nv.name = stz.nextToken();
188             nv.value = stz.nextToken();
189         // Allow null or empty values
190         } else if (tokens == 1) {
191             nv.name = stz.nextToken();
192             nv.value = "";
193         } else {
194             throw new IllegalStateException("Unexpected format for multi valued element: " + input);
195         }
196         return nv;
197     }
198 }
199