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.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
31 import org.collectionspace.services.PersonJAXBSchema;
32 import org.collectionspace.services.common.document.DocumentWrapper;
33 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandler;
34 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
35 import org.collectionspace.services.person.PersonsCommon;
36 import org.collectionspace.services.person.PersonsCommonList;
37 import org.collectionspace.services.person.PersonsCommonList.PersonListItem;
38 import org.nuxeo.ecm.core.api.DocumentModel;
39 import org.nuxeo.ecm.core.api.DocumentModelList;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * PersonDocumentModelHandler
46 * $LastChangedRevision: $
53 public class PersonDocumentModelHandler
54 extends RemoteDocumentModelHandler<PersonsCommon, PersonsCommonList> {
56 private final Logger logger = LoggerFactory.getLogger(PersonDocumentModelHandler.class);
58 * person is used to stash JAXB object to use when handle is called
59 * for Action.CREATE, Action.UPDATE or Action.GET
61 private PersonsCommon person;
63 * personList is stashed when handle is called
66 private PersonsCommonList personList;
69 * inAuthority is the parent OrgAuthority for this context
71 private String inAuthority;
73 public String getInAuthority() {
77 public void setInAuthority(String inAuthority) {
78 this.inAuthority = inAuthority;
82 public void prepare(Action action) throws Exception {
83 //no specific action needed
86 /* Override handleGet so we can deal with defaulting the displayName
87 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
90 public void handleGet(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
91 DocumentModel docModel = wrapDoc.getWrappedObject();
92 String displayName = (String) docModel.getProperty(getServiceContext().getCommonPartLabel("persons"),
93 PersonJAXBSchema.DISPLAY_NAME);
94 if(displayName == null) {
95 docModel.setProperty(getServiceContext().getCommonPartLabel("persons"),
96 PersonJAXBSchema.DISPLAY_NAME, prepareDefaultDisplayName(docModel));
98 super.handleGet(wrapDoc);
101 private String prepareDefaultDisplayName(DocumentModel docModel) throws Exception {
102 String commonPartLabel = getServiceContext().getCommonPartLabel("persons");
103 return prepareDefaultDisplayName(
104 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.FORE_NAME ),
105 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.MIDDLE_NAME ),
106 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.SUR_NAME ),
107 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.BIRTH_DATE ),
108 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.DEATH_DATE )
114 * Produces a default displayName from the basic name and dates fields.
115 * @see PersonAuthorityClientUtils.prepareDefaultDisplayName() which
116 * duplicates this logic, until we define a service-general utils package
117 * that is neither client nor service specific.
126 private static String prepareDefaultDisplayName(
127 String foreName, String middleName, String surName,
128 String birthDate, String deathDate ) throws Exception {
129 StringBuilder newStr = new StringBuilder();
130 final String sep = " ";
131 final String dateSep = "-";
132 List<String> nameStrings =
133 Arrays.asList(foreName, middleName, surName);
134 boolean firstAdded = false;
135 for(String partStr : nameStrings ){
136 if(null != partStr ) {
140 newStr.append(partStr);
144 // Now we add the dates. In theory could have dates with no name, but that is their problem.
145 boolean foundBirth = false;
146 if(null != birthDate) {
150 newStr.append(birthDate);
151 newStr.append(dateSep); // Put this in whether there is a death date or not
154 if(null != deathDate) {
159 newStr.append(dateSep);
161 newStr.append(deathDate);
163 return newStr.toString();
167 * getCommonPart get associated person
171 public PersonsCommon getCommonPart() {
176 * setCommonPart set associated person
180 public void setCommonPart(PersonsCommon person) {
181 this.person = person;
185 * getCommonPartList get associated person (for index/GET_ALL)
189 public PersonsCommonList getCommonPartList() {
194 public void setCommonPartList(PersonsCommonList personList) {
195 this.personList = personList;
199 public PersonsCommon extractCommonPart(DocumentWrapper wrapDoc)
201 throw new UnsupportedOperationException();
205 public void fillCommonPart(PersonsCommon personObject, DocumentWrapper wrapDoc) throws Exception {
206 throw new UnsupportedOperationException();
210 public PersonsCommonList extractCommonPartList(DocumentWrapper wrapDoc)
212 PersonsCommonList coList = new PersonsCommonList();
214 DocumentModelList docList = (DocumentModelList) wrapDoc.getWrappedObject();
216 List<PersonsCommonList.PersonListItem> list =
217 coList.getPersonListItem();
219 //FIXME: iterating over a long list of documents is not a long term
220 //strategy...need to change to more efficient iterating in future
221 Iterator<DocumentModel> iter = docList.iterator();
222 while(iter.hasNext()){
223 DocumentModel docModel = iter.next();
224 PersonListItem ilistItem = new PersonListItem();
225 // We look for a set display name, and fall back to teh short name if there is none
226 String displayName = (String) docModel.getProperty(getServiceContext().getCommonPartLabel("persons"),
227 PersonJAXBSchema.DISPLAY_NAME);
228 if(displayName == null)
229 displayName = prepareDefaultDisplayName(docModel);
230 ilistItem.setDisplayName( displayName );
231 ilistItem.setRefName((String) docModel.getProperty(getServiceContext().getCommonPartLabel("persons"),
232 PersonJAXBSchema.REF_NAME));
233 String id = NuxeoUtils.extractId(docModel.getPathAsString());
234 ilistItem.setUri("/personauthorities/"+inAuthority+"/items/" + id);
235 ilistItem.setCsid(id);
239 if(logger.isDebugEnabled()){
240 logger.debug("Caught exception in extractCommonPartList", e);
248 * getQProperty converts the given property to qualified schema property
253 public String getQProperty(String prop) {
254 return PersonConstants.NUXEO_SCHEMA_NAME + ":" + prop;