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.common.vocabulary.nuxeo.AuthorityItemDocumentModelHandler;
31 import org.collectionspace.services.OrganizationJAXBSchema;
32 import org.collectionspace.services.common.document.DocumentWrapper;
33 import org.collectionspace.services.common.service.ObjectPartType;
34 import org.collectionspace.services.jaxb.AbstractCommonList;
35 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
36 import org.collectionspace.services.organization.OrganizationsCommon;
37 import org.collectionspace.services.organization.OrganizationsCommonList;
38 import org.collectionspace.services.organization.OrganizationsCommonList.OrganizationListItem;
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 * OrganizationDocumentModelHandler
47 * $LastChangedRevision$
50 public class OrganizationDocumentModelHandler
51 extends AuthorityItemDocumentModelHandler<OrganizationsCommon, OrganizationsCommonList> {
54 private final Logger logger = LoggerFactory.getLogger(OrganizationDocumentModelHandler.class);
56 * Common part schema label
58 private static final String COMMON_PART_LABEL = "organizations_common";
60 public OrganizationDocumentModelHandler() {
61 super(COMMON_PART_LABEL);
65 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
68 public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
69 // first fill all the parts of the document
70 super.handleCreate(wrapDoc);
71 handleDisplayNames(wrapDoc.getWrappedObject());
75 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
78 public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
79 super.handleUpdate(wrapDoc);
80 handleDisplayNames(wrapDoc.getWrappedObject());
84 * Check the logic around the computed displayName
88 * @throws Exception the exception
90 private void handleDisplayNames(DocumentModel docModel) throws Exception {
91 String commonPartLabel = getServiceContext().getCommonPartLabel("organizations");
92 Boolean displayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
93 OrganizationJAXBSchema.DISPLAY_NAME_COMPUTED);
94 Boolean shortDisplayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
95 OrganizationJAXBSchema.SHORT_DISPLAY_NAME_COMPUTED);
96 if(displayNameComputed==null)
97 displayNameComputed = true;
98 if(shortDisplayNameComputed==null)
99 shortDisplayNameComputed = true;
100 if (displayNameComputed || shortDisplayNameComputed) {
101 List<Object> mainBodyGroupList = (List<Object>) docModel.getProperty(commonPartLabel,
102 OrganizationJAXBSchema.MAIN_BODY_GROUP_LIST);
103 // FIXME: Determine how to handle cases where primary short name is null or empty.
104 String shortName = primaryValueFromMultivalue(mainBodyGroupList,
105 OrganizationJAXBSchema.SHORT_NAME);
106 if(shortDisplayNameComputed) {
107 String displayName = prepareDefaultDisplayName(shortName, null);
108 docModel.setProperty(commonPartLabel, OrganizationJAXBSchema.SHORT_DISPLAY_NAME,
111 if(displayNameComputed) {
112 String foundingPlace = (String) docModel.getProperty(commonPartLabel,
113 OrganizationJAXBSchema.FOUNDING_PLACE);
114 String displayName = prepareDefaultDisplayName(shortName, foundingPlace);
115 docModel.setProperty(commonPartLabel, OrganizationJAXBSchema.DISPLAY_NAME,
122 * Produces a default displayName from the basic name and foundingPlace fields.
123 * @see OrgAuthorityClientUtils.prepareDefaultDisplayName() which
124 * duplicates this logic, until we define a service-general utils package
125 * that is neither client nor service specific.
127 * @param foundingPlace
131 private static String prepareDefaultDisplayName(
132 String shortName, String foundingPlace ) throws Exception {
133 StringBuilder newStr = new StringBuilder();
134 final String sep = " ";
135 boolean firstAdded = false;
136 if(null != shortName ) {
137 newStr.append(shortName);
140 // Now we add the place
141 if(null != foundingPlace ) {
145 newStr.append(foundingPlace);
147 return newStr.toString();
151 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
154 public OrganizationsCommonList extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc)
156 OrganizationsCommonList coList = this.extractPagingInfo(new OrganizationsCommonList(), wrapDoc);
157 AbstractCommonList commonList = (AbstractCommonList) coList;
158 commonList.setFieldsReturned("displayName|refName|shortIdentifier|uri|csid");
159 List<OrganizationsCommonList.OrganizationListItem> list = coList.getOrganizationListItem();
160 Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();
161 String commonPartLabel = getServiceContext().getCommonPartLabel("organizations");
162 while(iter.hasNext()){
163 DocumentModel docModel = iter.next();
164 OrganizationListItem ilistItem = new OrganizationListItem();
165 ilistItem.setDisplayName((String)
166 docModel.getProperty(commonPartLabel,OrganizationJAXBSchema.DISPLAY_NAME ));
167 ilistItem.setShortIdentifier((String) docModel.getProperty(commonPartLabel,
168 OrganizationJAXBSchema.SHORT_IDENTIFIER));
169 ilistItem.setRefName((String)
170 docModel.getProperty(commonPartLabel, OrganizationJAXBSchema.REF_NAME));
171 String id = NuxeoUtils.extractId(docModel.getPathAsString());
172 ilistItem.setUri("/orgauthorities/" + this.inAuthority + "/items/" + id);
173 ilistItem.setCsid(id);
181 * getQProperty converts the given property to qualified schema property
186 public String getQProperty(String prop) {
187 return OrganizationConstants.NUXEO_SCHEMA_NAME + ":" + prop;