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.person.nuxeo;
26 import java.util.Iterator;
27 import java.util.Arrays;
28 import java.util.List;
31 import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityItemDocumentModelHandler;
32 import org.collectionspace.services.PersonJAXBSchema;
33 import org.collectionspace.services.common.document.DocumentWrapper;
34 import org.collectionspace.services.common.service.ObjectPartType;
35 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
36 import org.collectionspace.services.jaxb.AbstractCommonList;
37 import org.collectionspace.services.person.PersonsCommon;
38 import org.collectionspace.services.person.PersonsCommonList;
39 import org.collectionspace.services.person.PersonsCommonList.PersonListItem;
41 import org.nuxeo.ecm.core.api.DocumentModel;
42 import org.nuxeo.ecm.core.api.DocumentModelList;
44 //import org.slf4j.Logger;
45 //import org.slf4j.LoggerFactory;
48 * PersonDocumentModelHandler
50 * $LastChangedRevision: $
57 public class PersonDocumentModelHandler
58 extends AuthorityItemDocumentModelHandler<PersonsCommon, PersonsCommonList> {
61 //private final Logger logger = LoggerFactory.getLogger(PersonDocumentModelHandler.class);
63 * Common part schema label
65 private static final String COMMON_PART_LABEL = "persons_common";
67 public PersonDocumentModelHandler() {
68 super(COMMON_PART_LABEL);
72 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
75 public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
76 // first fill all the parts of the document
77 super.handleCreate(wrapDoc);
78 handleDisplayNames(wrapDoc.getWrappedObject());
82 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
85 public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
86 super.handleUpdate(wrapDoc);
87 handleDisplayNames(wrapDoc.getWrappedObject());
91 * Handle display names.
93 * @param docModel the doc model
94 * @throws Exception the exception
96 private void handleDisplayNames(DocumentModel docModel) throws Exception {
97 String commonPartLabel = getServiceContext().getCommonPartLabel("persons");
98 Boolean displayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
99 PersonJAXBSchema.DISPLAY_NAME_COMPUTED);
100 Boolean shortDisplayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
101 PersonJAXBSchema.SHORT_DISPLAY_NAME_COMPUTED);
102 if(displayNameComputed==null)
103 displayNameComputed = true;
104 if(shortDisplayNameComputed==null)
105 shortDisplayNameComputed = true;
106 if (displayNameComputed || shortDisplayNameComputed) {
108 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.FORE_NAME);
110 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.SUR_NAME);
111 if(shortDisplayNameComputed) {
112 String displayName = prepareDefaultDisplayName(forename, null, lastname,
114 docModel.setProperty(commonPartLabel, PersonJAXBSchema.SHORT_DISPLAY_NAME,
117 if(displayNameComputed) {
119 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.MIDDLE_NAME);
121 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.BIRTH_DATE);
123 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.DEATH_DATE);
124 String displayName = prepareDefaultDisplayName(forename, midname, lastname,
125 birthdate, deathdate);
126 docModel.setProperty(commonPartLabel, PersonJAXBSchema.DISPLAY_NAME,
134 * Produces a default displayName from the basic name and dates fields.
135 * @see PersonAuthorityClientUtils.prepareDefaultDisplayName() which
136 * duplicates this logic, until we define a service-general utils package
137 * that is neither client nor service specific.
146 private static String prepareDefaultDisplayName(
147 String foreName, String middleName, String surName,
148 String birthDate, String deathDate ) throws Exception {
149 final String SEP = " ";
150 final String DATE_SEP = "-";
152 StringBuilder newStr = new StringBuilder();
153 List<String> nameStrings =
154 Arrays.asList(foreName, middleName, surName);
155 boolean firstAdded = false;
156 for (String partStr : nameStrings ){
157 if (partStr != null) {
158 if (firstAdded == true) {
161 newStr.append(partStr);
165 // Now we add the dates. In theory could have dates with no name, but that is their problem.
166 boolean foundBirth = false;
167 if (birthDate != null) {
171 newStr.append(birthDate);
172 newStr.append(DATE_SEP); // Put this in whether there is a death date or not
175 if (deathDate != null) {
177 if (firstAdded == true) {
180 newStr.append(DATE_SEP);
182 newStr.append(deathDate);
185 return newStr.toString();
190 * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#extractPart(org.nuxeo.ecm.core.api.DocumentModel, java.lang.String, org.collectionspace.services.common.service.ObjectPartType)
193 protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
195 Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
197 // Add the CSID to the common part
198 if (partMeta.getLabel().equalsIgnoreCase(COMMON_PART_LABEL)) {
199 String csid = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
200 unQObjectProperties.put("csid", csid);
203 return unQObjectProperties;
207 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
210 public PersonsCommonList extractCommonPartList(
211 DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
212 PersonsCommonList coList = extractPagingInfo(new PersonsCommonList(), wrapDoc);
213 AbstractCommonList commonList = (AbstractCommonList) coList;
214 commonList.setFieldsReturned("displayName|refName|shortIdentifier|uri|csid");
215 List<PersonsCommonList.PersonListItem> list = coList.getPersonListItem();
216 Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();
217 String commonPartLabel = getServiceContext().getCommonPartLabel(
219 while (iter.hasNext()) {
220 DocumentModel docModel = iter.next();
221 PersonListItem ilistItem = new PersonListItem();
222 ilistItem.setDisplayName((String) docModel.getProperty(
223 commonPartLabel, PersonJAXBSchema.DISPLAY_NAME));
224 ilistItem.setShortIdentifier((String) docModel.getProperty(commonPartLabel,
225 PersonJAXBSchema.SHORT_IDENTIFIER));
226 ilistItem.setRefName((String) docModel.getProperty(commonPartLabel,
227 PersonJAXBSchema.REF_NAME));
228 String id = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
229 ilistItem.setUri("/personauthorities/" + inAuthority + "/items/"
231 ilistItem.setCsid(id);
239 * getQProperty converts the given property to qualified schema property
244 public String getQProperty(String prop) {
245 return PersonConstants.NUXEO_SCHEMA_NAME + ":" + prop;