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;
30 import org.collectionspace.services.client.OrgAuthorityClient;
31 import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityItemDocumentModelHandler;
32 import org.collectionspace.services.OrganizationJAXBSchema;
33 import org.collectionspace.services.common.document.DocumentWrapper;
34 import org.collectionspace.services.common.service.ObjectPartType;
35 import org.collectionspace.services.jaxb.AbstractCommonList;
36 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
37 import org.collectionspace.services.organization.OrganizationsCommon;
38 import org.collectionspace.services.organization.OrganizationsCommonList;
39 import org.collectionspace.services.organization.OrganizationsCommonList.OrganizationListItem;
40 import org.nuxeo.ecm.core.api.DocumentModel;
41 import org.nuxeo.ecm.core.api.DocumentModelList;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * OrganizationDocumentModelHandler
48 * $LastChangedRevision$
51 public class OrganizationDocumentModelHandler
52 extends AuthorityItemDocumentModelHandler<OrganizationsCommon, OrganizationsCommonList> {
55 private final Logger logger = LoggerFactory.getLogger(OrganizationDocumentModelHandler.class);
57 * Common part schema label
59 private static final String COMMON_PART_LABEL = "organizations_common";
61 public OrganizationDocumentModelHandler() {
62 super(COMMON_PART_LABEL);
66 public String getAuthorityServicePath(){
67 return OrgAuthorityClient.SERVICE_PATH_COMPONENT; // CSPACE-3932
71 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
74 public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
75 // first fill all the parts of the document
76 super.handleCreate(wrapDoc);
77 handleDisplayNames(wrapDoc.getWrappedObject());
81 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
84 public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
85 super.handleUpdate(wrapDoc);
86 handleDisplayNames(wrapDoc.getWrappedObject());
90 * Check the logic around the computed displayName
94 * @throws Exception the exception
96 private void handleDisplayNames(DocumentModel docModel) throws Exception {
97 String commonPartLabel = getServiceContext().getCommonPartLabel("organizations");
98 Boolean displayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
99 OrganizationJAXBSchema.DISPLAY_NAME_COMPUTED);
100 Boolean shortDisplayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
101 OrganizationJAXBSchema.SHORT_DISPLAY_NAME_COMPUTED);
102 if(displayNameComputed==null)
103 displayNameComputed = Boolean.TRUE;
104 if(shortDisplayNameComputed==null)
105 shortDisplayNameComputed = Boolean.TRUE;
106 if (displayNameComputed.booleanValue() || shortDisplayNameComputed.booleanValue()) {
107 String shortName = getStringValueInPrimaryRepeatingComplexProperty(
108 docModel, commonPartLabel, OrganizationJAXBSchema.MAIN_BODY_GROUP_LIST,
109 OrganizationJAXBSchema.SHORT_NAME);
110 // FIXME: Determine how to handle cases where primary short name is null or empty.
111 if(shortDisplayNameComputed.booleanValue()) {
112 String displayName = prepareDefaultDisplayName(shortName, null);
113 docModel.setProperty(commonPartLabel, OrganizationJAXBSchema.SHORT_DISPLAY_NAME,
116 if(displayNameComputed.booleanValue()) {
117 String foundingPlace = (String) docModel.getProperty(commonPartLabel,
118 OrganizationJAXBSchema.FOUNDING_PLACE);
119 String displayName = prepareDefaultDisplayName(shortName, foundingPlace);
120 docModel.setProperty(commonPartLabel, OrganizationJAXBSchema.DISPLAY_NAME,
127 * Produces a default displayName from the basic name and foundingPlace fields.
128 * @see OrgAuthorityClientUtils.prepareDefaultDisplayName() which
129 * duplicates this logic, until we define a service-general utils package
130 * that is neither client nor service specific.
132 * @param foundingPlace
136 private static String prepareDefaultDisplayName(
137 String shortName, String foundingPlace ) throws Exception {
138 StringBuilder newStr = new StringBuilder();
139 final String sep = " ";
140 boolean firstAdded = false;
141 if(null != shortName ) {
142 newStr.append(shortName);
145 // Now we add the place
146 if(null != foundingPlace ) {
150 newStr.append(foundingPlace);
152 return newStr.toString();
156 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
159 public OrganizationsCommonList extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc)
161 OrganizationsCommonList coList = this.extractPagingInfo(new OrganizationsCommonList(), wrapDoc);
162 AbstractCommonList commonList = (AbstractCommonList) coList;
163 commonList.setFieldsReturned("displayName|refName|shortIdentifier|uri|csid");
164 List<OrganizationsCommonList.OrganizationListItem> list = coList.getOrganizationListItem();
165 Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();
166 String commonPartLabel = getServiceContext().getCommonPartLabel("organizations");
167 while(iter.hasNext()){
168 DocumentModel docModel = iter.next();
169 OrganizationListItem ilistItem = new OrganizationListItem();
170 ilistItem.setDisplayName((String)
171 docModel.getProperty(commonPartLabel,OrganizationJAXBSchema.DISPLAY_NAME ));
172 ilistItem.setShortIdentifier((String) docModel.getProperty(commonPartLabel,
173 OrganizationJAXBSchema.SHORT_IDENTIFIER));
174 ilistItem.setRefName((String)
175 docModel.getProperty(commonPartLabel, OrganizationJAXBSchema.REF_NAME));
176 String id = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
177 ilistItem.setUri("/orgauthorities/" + this.inAuthority + "/items/" + id);
178 ilistItem.setCsid(id);
186 * getQProperty converts the given property to qualified schema property
191 public String getQProperty(String prop) {
192 return OrganizationConstants.NUXEO_SCHEMA_NAME + ":" + prop;