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 = Boolean.TRUE;
98 if(shortDisplayNameComputed==null)
99 shortDisplayNameComputed = Boolean.TRUE;
100 if (displayNameComputed.booleanValue() || shortDisplayNameComputed.booleanValue()) {
101 String shortName = getStringValueInPrimaryRepeatingComplexProperty(
102 docModel, commonPartLabel, OrganizationJAXBSchema.MAIN_BODY_GROUP_LIST,
103 OrganizationJAXBSchema.SHORT_NAME);
104 // FIXME: Determine how to handle cases where primary short name is null or empty.
105 if(shortDisplayNameComputed.booleanValue()) {
106 String displayName = prepareDefaultDisplayName(shortName, null);
107 docModel.setProperty(commonPartLabel, OrganizationJAXBSchema.SHORT_DISPLAY_NAME,
110 if(displayNameComputed.booleanValue()) {
111 String foundingPlace = (String) docModel.getProperty(commonPartLabel,
112 OrganizationJAXBSchema.FOUNDING_PLACE);
113 String displayName = prepareDefaultDisplayName(shortName, foundingPlace);
114 docModel.setProperty(commonPartLabel, OrganizationJAXBSchema.DISPLAY_NAME,
121 * Produces a default displayName from the basic name and foundingPlace fields.
122 * @see OrgAuthorityClientUtils.prepareDefaultDisplayName() which
123 * duplicates this logic, until we define a service-general utils package
124 * that is neither client nor service specific.
126 * @param foundingPlace
130 private static String prepareDefaultDisplayName(
131 String shortName, String foundingPlace ) throws Exception {
132 StringBuilder newStr = new StringBuilder();
133 final String sep = " ";
134 boolean firstAdded = false;
135 if(null != shortName ) {
136 newStr.append(shortName);
139 // Now we add the place
140 if(null != foundingPlace ) {
144 newStr.append(foundingPlace);
146 return newStr.toString();
150 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
153 public OrganizationsCommonList extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc)
155 OrganizationsCommonList coList = this.extractPagingInfo(new OrganizationsCommonList(), wrapDoc);
156 AbstractCommonList commonList = (AbstractCommonList) coList;
157 commonList.setFieldsReturned("displayName|refName|shortIdentifier|uri|csid");
158 List<OrganizationsCommonList.OrganizationListItem> list = coList.getOrganizationListItem();
159 Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();
160 String commonPartLabel = getServiceContext().getCommonPartLabel("organizations");
161 while(iter.hasNext()){
162 DocumentModel docModel = iter.next();
163 OrganizationListItem ilistItem = new OrganizationListItem();
164 ilistItem.setDisplayName((String)
165 docModel.getProperty(commonPartLabel,OrganizationJAXBSchema.DISPLAY_NAME ));
166 ilistItem.setShortIdentifier((String) docModel.getProperty(commonPartLabel,
167 OrganizationJAXBSchema.SHORT_IDENTIFIER));
168 ilistItem.setRefName((String)
169 docModel.getProperty(commonPartLabel, OrganizationJAXBSchema.REF_NAME));
170 String id = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
171 ilistItem.setUri("/orgauthorities/" + this.inAuthority + "/items/" + id);
172 ilistItem.setCsid(id);
180 * getQProperty converts the given property to qualified schema property
185 public String getQProperty(String prop) {
186 return OrganizationConstants.NUXEO_SCHEMA_NAME + ":" + prop;