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.client.PersonAuthorityClient;
32 import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityItemDocumentModelHandler;
33 import org.collectionspace.services.PersonJAXBSchema;
34 import org.collectionspace.services.common.document.DocumentWrapper;
35 import org.collectionspace.services.common.service.ObjectPartType;
36 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
37 import org.collectionspace.services.jaxb.AbstractCommonList;
38 import org.collectionspace.services.person.PersonsCommon;
39 import org.collectionspace.services.person.PersonsCommonList;
40 import org.collectionspace.services.person.PersonsCommonList.PersonListItem;
42 import org.nuxeo.ecm.core.api.DocumentModel;
43 import org.nuxeo.ecm.core.api.DocumentModelList;
45 //import org.slf4j.Logger;
46 //import org.slf4j.LoggerFactory;
49 * PersonDocumentModelHandler
51 * $LastChangedRevision: $
58 public class PersonDocumentModelHandler
59 extends AuthorityItemDocumentModelHandler<PersonsCommon, PersonsCommonList> {
62 //private final Logger logger = LoggerFactory.getLogger(PersonDocumentModelHandler.class);
64 * Common part schema label
66 private static final String COMMON_PART_LABEL = "persons_common";
68 public PersonDocumentModelHandler() {
69 super(COMMON_PART_LABEL);
73 public String getAuthorityServicePath(){
74 return PersonAuthorityClient.SERVICE_PATH_COMPONENT; // CSPACE-3932
78 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
81 public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
82 // first fill all the parts of the document
83 super.handleCreate(wrapDoc);
84 handleDisplayNames(wrapDoc.getWrappedObject());
88 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
91 public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
92 super.handleUpdate(wrapDoc);
93 handleDisplayNames(wrapDoc.getWrappedObject());
97 * Handle display names.
99 * @param docModel the doc model
100 * @throws Exception the exception
102 private void handleDisplayNames(DocumentModel docModel) throws Exception {
103 String commonPartLabel = getServiceContext().getCommonPartLabel("persons");
104 Boolean displayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
105 PersonJAXBSchema.DISPLAY_NAME_COMPUTED);
106 Boolean shortDisplayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
107 PersonJAXBSchema.SHORT_DISPLAY_NAME_COMPUTED);
108 if(displayNameComputed==null)
109 displayNameComputed = true;
110 if(shortDisplayNameComputed==null)
111 shortDisplayNameComputed = true;
112 if (displayNameComputed || shortDisplayNameComputed) {
114 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.FORE_NAME);
116 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.SUR_NAME);
117 if(shortDisplayNameComputed) {
118 String displayName = prepareDefaultDisplayName(forename, null, lastname,
120 docModel.setProperty(commonPartLabel, PersonJAXBSchema.SHORT_DISPLAY_NAME,
123 if(displayNameComputed) {
125 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.MIDDLE_NAME);
127 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.BIRTH_DATE);
129 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.DEATH_DATE);
130 String displayName = prepareDefaultDisplayName(forename, midname, lastname,
131 birthdate, deathdate);
132 docModel.setProperty(commonPartLabel, PersonJAXBSchema.DISPLAY_NAME,
140 * Produces a default displayName from the basic name and dates fields.
141 * see PersonAuthorityClientUtils.prepareDefaultDisplayName(String,String,String,String,String) which
142 * duplicates this logic, until we define a service-general utils package
143 * that is neither client nor service specific.
152 private static String prepareDefaultDisplayName(
153 String foreName, String middleName, String surName,
154 String birthDate, String deathDate ) throws Exception {
155 final String SEP = " ";
156 final String DATE_SEP = "-";
158 StringBuilder newStr = new StringBuilder();
159 List<String> nameStrings =
160 Arrays.asList(foreName, middleName, surName);
161 boolean firstAdded = false;
162 for (String partStr : nameStrings ){
163 if (partStr != null) {
164 if (firstAdded == true) {
167 newStr.append(partStr);
171 // Now we add the dates. In theory could have dates with no name, but that is their problem.
172 boolean foundBirth = false;
173 if (birthDate != null) {
177 newStr.append(birthDate);
178 newStr.append(DATE_SEP); // Put this in whether there is a death date or not
181 if (deathDate != null) {
183 if (firstAdded == true) {
186 newStr.append(DATE_SEP);
188 newStr.append(deathDate);
191 return newStr.toString();
196 * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#extractPart(org.nuxeo.ecm.core.api.DocumentModel, java.lang.String, org.collectionspace.services.common.service.ObjectPartType)
199 protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
201 Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
203 // Add the CSID to the common part
204 if (partMeta.getLabel().equalsIgnoreCase(COMMON_PART_LABEL)) {
205 String csid = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
206 unQObjectProperties.put("csid", csid);
209 return unQObjectProperties;
213 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
216 public PersonsCommonList extractCommonPartList(
217 DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
218 PersonsCommonList coList = extractPagingInfo(new PersonsCommonList(), wrapDoc);
219 AbstractCommonList commonList = (AbstractCommonList) coList;
220 commonList.setFieldsReturned("displayName|refName|shortIdentifier|uri|csid");
221 List<PersonsCommonList.PersonListItem> list = coList.getPersonListItem();
222 Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();
223 String commonPartLabel = getServiceContext().getCommonPartLabel(
225 while (iter.hasNext()) {
226 DocumentModel docModel = iter.next();
227 PersonListItem ilistItem = new PersonListItem();
228 ilistItem.setDisplayName((String) docModel.getProperty(
229 commonPartLabel, PersonJAXBSchema.DISPLAY_NAME));
230 ilistItem.setShortIdentifier((String) docModel.getProperty(commonPartLabel,
231 PersonJAXBSchema.SHORT_IDENTIFIER));
232 ilistItem.setRefName((String) docModel.getProperty(commonPartLabel,
233 PersonJAXBSchema.REF_NAME));
234 String id = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
235 ilistItem.setUri("/personauthorities/" + inAuthority + "/items/"
237 ilistItem.setCsid(id);
245 * getQProperty converts the given property to qualified schema property
250 public String getQProperty(String prop) {
251 return PersonConstants.NUXEO_SCHEMA_NAME + ":" + prop;