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: $
49 public class PersonDocumentModelHandler
50 extends RemoteDocumentModelHandler<PersonsCommon, PersonsCommonList> {
52 private final Logger logger = LoggerFactory.getLogger(PersonDocumentModelHandler.class);
54 * person is used to stash JAXB object to use when handle is called
55 * for Action.CREATE, Action.UPDATE or Action.GET
57 private PersonsCommon person;
59 * personList is stashed when handle is called
62 private PersonsCommonList personList;
65 * inAuthority is the parent OrgAuthority for this context
67 private String inAuthority;
69 public String getInAuthority() {
73 public void setInAuthority(String inAuthority) {
74 this.inAuthority = inAuthority;
78 public void prepare(Action action) throws Exception {
79 //no specific action needed
82 /* Override handleGet so we can deal with defaulting the displayName
83 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
86 public void handleGet(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
87 DocumentModel docModel = wrapDoc.getWrappedObject();
88 String displayName = (String) docModel.getProperty(getServiceContext().getCommonPartLabel("persons"),
89 PersonJAXBSchema.DISPLAY_NAME);
90 if(displayName == null) {
91 docModel.setProperty(getServiceContext().getCommonPartLabel("persons"),
92 PersonJAXBSchema.DISPLAY_NAME, prepareDefaultDisplayName(docModel));
94 super.handleGet(wrapDoc);
97 private String prepareDefaultDisplayName(DocumentModel docModel) throws Exception {
98 StringBuilder newStr = new StringBuilder();
100 final String sep = " ";
101 final String dateSep = "-";
102 List<String> nameStrings =
103 Arrays.asList(PersonJAXBSchema.FORE_NAME, PersonJAXBSchema.MIDDLE_NAME,
104 PersonJAXBSchema.SUR_NAME);
105 boolean firstAdded = false;
106 for(String partStr : nameStrings ){
107 if(null != (part = (String)
108 docModel.getProperty(getServiceContext().getCommonPartLabel("persons"), partStr ))) {
116 // Now we add the dates. In theory could have dates with no name, but that is their problem.
117 boolean foundBirth = false;
118 if(null != (part = (String)
119 docModel.getProperty(getServiceContext().getCommonPartLabel("persons"),
120 PersonJAXBSchema.BIRTH_DATE ))) {
125 newStr.append(dateSep); // Put this in whether there is a death date or not
128 if(null != (part = (String)
129 docModel.getProperty(getServiceContext().getCommonPartLabel("persons"),
130 PersonJAXBSchema.DEATH_DATE ))) {
135 newStr.append(dateSep);
139 return newStr.toString();
143 * getCommonPart get associated person
147 public PersonsCommon getCommonPart() {
152 * setCommonPart set associated person
156 public void setCommonPart(PersonsCommon person) {
157 this.person = person;
161 * getCommonPartList get associated person (for index/GET_ALL)
165 public PersonsCommonList getCommonPartList() {
170 public void setCommonPartList(PersonsCommonList personList) {
171 this.personList = personList;
175 public PersonsCommon extractCommonPart(DocumentWrapper wrapDoc)
177 throw new UnsupportedOperationException();
181 public void fillCommonPart(PersonsCommon personObject, DocumentWrapper wrapDoc) throws Exception {
182 throw new UnsupportedOperationException();
186 public PersonsCommonList extractCommonPartList(DocumentWrapper wrapDoc)
188 PersonsCommonList coList = new PersonsCommonList();
190 DocumentModelList docList = (DocumentModelList) wrapDoc.getWrappedObject();
192 List<PersonsCommonList.PersonListItem> list =
193 coList.getPersonListItem();
195 //FIXME: iterating over a long list of documents is not a long term
196 //strategy...need to change to more efficient iterating in future
197 Iterator<DocumentModel> iter = docList.iterator();
198 while(iter.hasNext()){
199 DocumentModel docModel = iter.next();
200 PersonListItem ilistItem = new PersonListItem();
201 // We look for a set display name, and fall back to teh short name if there is none
202 String displayName = (String) docModel.getProperty(getServiceContext().getCommonPartLabel("persons"),
203 PersonJAXBSchema.DISPLAY_NAME);
204 if(displayName == null)
205 displayName = prepareDefaultDisplayName(docModel);
206 ilistItem.setDisplayName( displayName );
207 ilistItem.setRefName((String) docModel.getProperty(getServiceContext().getCommonPartLabel("persons"),
208 PersonJAXBSchema.REF_NAME));
209 String id = NuxeoUtils.extractId(docModel.getPathAsString());
210 ilistItem.setUri("/personauthorities/"+inAuthority+"/items/" + id);
211 ilistItem.setCsid(id);
215 if(logger.isDebugEnabled()){
216 logger.debug("Caught exception in extractCommonPartList", e);
224 * getQProperty converts the given property to qualified schema property
229 public String getQProperty(String prop) {
230 return PersonConstants.NUXEO_SCHEMA_NAME + ":" + prop;