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.location.nuxeo;
26 import java.util.Iterator;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
32 import org.collectionspace.services.LocationJAXBSchema;
33 import org.collectionspace.services.common.document.DocumentFilter;
34 import org.collectionspace.services.common.document.DocumentWrapper;
35 import org.collectionspace.services.common.service.ObjectPartType;
36 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
37 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
38 import org.collectionspace.services.location.LocationsCommon;
39 import org.collectionspace.services.location.LocationsCommonList;
40 import org.collectionspace.services.location.LocationsCommonList.LocationListItem;
41 import org.nuxeo.ecm.core.api.DocumentModel;
42 import org.nuxeo.ecm.core.api.DocumentModelList;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * LocationDocumentModelHandler
49 * $LastChangedRevision: $
56 public class LocationDocumentModelHandler
57 extends RemoteDocumentModelHandlerImpl<LocationsCommon, LocationsCommonList> {
60 private final Logger logger = LoggerFactory.getLogger(LocationDocumentModelHandler.class);
62 * Common part schema label
64 private static final String COMMON_PART_LABEL = "locations_common";
67 * location is used to stash JAXB object to use when handle is called
68 * for Action.CREATE, Action.UPDATE or Action.GET
70 private LocationsCommon location;
72 * locationList is stashed when handle is called
75 private LocationsCommonList locationList;
78 * inAuthority is the parent OrgAuthority for this context
80 private String inAuthority;
83 * Gets the in authority.
85 * @return the in authority
87 public String getInAuthority() {
92 * Sets the in authority.
94 * @param inAuthority the new in authority
96 public void setInAuthority(String inAuthority) {
97 this.inAuthority = inAuthority;
102 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
105 public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
106 // first fill all the parts of the document
107 super.handleCreate(wrapDoc);
108 handleDisplayName(wrapDoc.getWrappedObject());
112 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
115 public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
116 super.handleUpdate(wrapDoc);
117 handleDisplayName(wrapDoc.getWrappedObject());
121 * Handle display name.
123 * @param docModel the doc model
124 * @throws Exception the exception
126 private void handleDisplayName(DocumentModel docModel) throws Exception {
127 String commonPartLabel = getServiceContext().getCommonPartLabel("locations");
128 Boolean displayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
129 LocationJAXBSchema.DISPLAY_NAME_COMPUTED);
130 if (displayNameComputed) {
131 String displayName = prepareDefaultDisplayName(
132 (String)docModel.getProperty(commonPartLabel, LocationJAXBSchema.NAME ));
133 docModel.setProperty(commonPartLabel, LocationJAXBSchema.DISPLAY_NAME,
139 * Produces a default displayName from the basic name and dates fields.
140 * @see LocationAuthorityClientUtils.prepareDefaultDisplayName() which
141 * duplicates this logic, until we define a service-general utils package
142 * that is neither client nor service specific.
151 private static String prepareDefaultDisplayName(
152 String name ) throws Exception {
153 StringBuilder newStr = new StringBuilder();
155 return newStr.toString();
159 * getCommonPart get associated location
163 public LocationsCommon getCommonPart() {
168 * setCommonPart set associated location
172 public void setCommonPart(LocationsCommon location) {
173 this.location = location;
177 * getCommonPartList get associated location (for index/GET_ALL)
181 public LocationsCommonList getCommonPartList() {
186 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#setCommonPartList(java.lang.Object)
189 public void setCommonPartList(LocationsCommonList locationList) {
190 this.locationList = locationList;
194 * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#extractPart(org.nuxeo.ecm.core.api.DocumentModel, java.lang.String, org.collectionspace.services.common.service.ObjectPartType)
197 protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
199 Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
201 // Add the CSID to the common part
202 if (partMeta.getLabel().equalsIgnoreCase(COMMON_PART_LABEL)) {
203 String csid = NuxeoUtils.extractId(docModel.getPathAsString());
204 unQObjectProperties.put("csid", csid);
207 return unQObjectProperties;
211 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
214 public LocationsCommon extractCommonPart(DocumentWrapper wrapDoc)
216 throw new UnsupportedOperationException();
220 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
223 public void fillCommonPart(LocationsCommon locationObject, DocumentWrapper wrapDoc) throws Exception {
224 throw new UnsupportedOperationException();
228 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
231 public LocationsCommonList extractCommonPartList(
232 DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
233 LocationsCommonList coList = extractPagingInfo(new LocationsCommonList(), wrapDoc);
234 List<LocationsCommonList.LocationListItem> list = coList.getLocationListItem();
235 Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();
236 String commonPartLabel = getServiceContext().getCommonPartLabel(
238 while (iter.hasNext()) {
239 DocumentModel docModel = iter.next();
240 LocationListItem ilistItem = new LocationListItem();
241 ilistItem.setDisplayName((String) docModel.getProperty(
242 commonPartLabel, LocationJAXBSchema.DISPLAY_NAME));
243 ilistItem.setRefName((String) docModel.getProperty(commonPartLabel,
244 LocationJAXBSchema.REF_NAME));
245 String id = NuxeoUtils.extractId(docModel.getPathAsString());
246 ilistItem.setUri("/locationauthorities/" + inAuthority + "/items/"
248 ilistItem.setCsid(id);
256 * getQProperty converts the given property to qualified schema property
261 public String getQProperty(String prop) {
262 return LocationConstants.NUXEO_SCHEMA_NAME + ":" + prop;