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.organization.nuxeo;
26 import java.util.Iterator;
27 import java.util.List;
29 import org.collectionspace.services.OrganizationJAXBSchema;
30 import org.collectionspace.services.common.context.MultipartServiceContext;
31 import org.collectionspace.services.common.document.DocumentWrapper;
32 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
33 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
34 import org.collectionspace.services.organization.OrganizationsCommon;
35 import org.collectionspace.services.organization.OrganizationsCommonList;
36 import org.collectionspace.services.organization.OrganizationsCommonList.OrganizationListItem;
37 import org.nuxeo.ecm.core.api.DocumentModel;
38 import org.nuxeo.ecm.core.api.DocumentModelList;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * OrganizationDocumentModelHandler
45 * $LastChangedRevision: $
48 public class OrganizationDocumentModelHandler
49 extends RemoteDocumentModelHandlerImpl<OrganizationsCommon, OrganizationsCommonList> {
51 private final Logger logger = LoggerFactory.getLogger(OrganizationDocumentModelHandler.class);
53 * organization is used to stash JAXB object to use when handle is called
54 * for Action.CREATE, Action.UPDATE or Action.GET
56 private OrganizationsCommon organization;
58 * organizationList is stashed when handle is called
61 private OrganizationsCommonList organizationList;
64 * inAuthority is the parent OrgAuthority for this context
66 private String inAuthority;
68 public String getInAuthority() {
72 public void setInAuthority(String inAuthority) {
73 this.inAuthority = inAuthority;
78 public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
79 // first fill all the parts of the document
80 super.handleCreate(wrapDoc);
81 handleDisplayName(wrapDoc.getWrappedObject());
85 public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
86 super.handleUpdate(wrapDoc);
87 handleDisplayName(wrapDoc.getWrappedObject());
91 * Check the logic around the computed displayName
95 * @throws Exception the exception
97 private void handleDisplayName(DocumentModel docModel) throws Exception {
98 String commonPartLabel = getServiceContext().getCommonPartLabel("organizations");
99 Boolean displayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
100 OrganizationJAXBSchema.DISPLAY_NAME_COMPUTED);
101 if (displayNameComputed) {
102 String displayName = prepareDefaultDisplayName(
103 (String) docModel.getProperty(commonPartLabel,OrganizationJAXBSchema.SHORT_NAME),
104 (String) docModel.getProperty(commonPartLabel,OrganizationJAXBSchema.FOUNDING_PLACE));
105 docModel.setProperty(commonPartLabel, OrganizationJAXBSchema.DISPLAY_NAME,
108 docModel.getProperty(commonPartLabel,OrganizationJAXBSchema.DISPLAY_NAME)) {
109 throw new IllegalArgumentException("Must provide "+OrganizationJAXBSchema.DISPLAY_NAME
110 +" if " + OrganizationJAXBSchema.DISPLAY_NAME_COMPUTED+" is declared false.");
115 * Produces a default displayName from the basic name and foundingPlace fields.
116 * @see OrgAuthorityClientUtils.prepareDefaultDisplayName() which
117 * duplicates this logic, until we define a service-general utils package
118 * that is neither client nor service specific.
120 * @param foundingPlace
124 private static String prepareDefaultDisplayName(
125 String shortName, String foundingPlace ) throws Exception {
126 StringBuilder newStr = new StringBuilder();
127 final String sep = " ";
128 boolean firstAdded = false;
129 if(null != shortName ) {
130 newStr.append(shortName);
133 // Now we add the place
134 if(null != foundingPlace ) {
138 newStr.append(foundingPlace);
140 return newStr.toString();
144 * getCommonPart get associated organization
148 public OrganizationsCommon getCommonPart() {
153 * setCommonPart set associated organization
154 * @param organization
157 public void setCommonPart(OrganizationsCommon organization) {
158 this.organization = organization;
162 * getCommonPartList get associated organization (for index/GET_ALL)
166 public OrganizationsCommonList getCommonPartList() {
167 return organizationList;
171 public void setCommonPartList(OrganizationsCommonList organizationList) {
172 this.organizationList = organizationList;
176 public OrganizationsCommon extractCommonPart(DocumentWrapper wrapDoc)
178 throw new UnsupportedOperationException();
182 public void fillCommonPart(OrganizationsCommon organizationObject, DocumentWrapper wrapDoc) throws Exception {
183 throw new UnsupportedOperationException();
187 public OrganizationsCommonList extractCommonPartList(DocumentWrapper wrapDoc)
189 OrganizationsCommonList coList = new OrganizationsCommonList();
191 DocumentModelList docList = (DocumentModelList) wrapDoc.getWrappedObject();
193 List<OrganizationsCommonList.OrganizationListItem> list =
194 coList.getOrganizationListItem();
196 //FIXME: iterating over a long list of documents is not a long term
197 //strategy...need to change to more efficient iterating in future
198 Iterator<DocumentModel> iter = docList.iterator();
199 String commonPartLabel = getServiceContext().getCommonPartLabel("organizations");
200 while(iter.hasNext()){
201 DocumentModel docModel = iter.next();
202 OrganizationListItem ilistItem = new OrganizationListItem();
203 ilistItem.setDisplayName((String)
204 docModel.getProperty(commonPartLabel,OrganizationJAXBSchema.DISPLAY_NAME ));
205 ilistItem.setRefName((String)
206 docModel.getProperty(commonPartLabel, OrganizationJAXBSchema.REF_NAME));
207 String id = NuxeoUtils.extractId(docModel.getPathAsString());
208 ilistItem.setUri("/orgauthorities/"+inAuthority+"/items/" + id);
209 ilistItem.setCsid(id);
213 if(logger.isDebugEnabled()){
214 logger.debug("Caught exception in extractCommonPartList", e);
222 * getQProperty converts the given property to qualified schema property
227 public String getQProperty(String prop) {
228 return OrganizationConstants.NUXEO_SCHEMA_NAME + ":" + prop;