]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d03e5e9c6bb4ff2c03711ae3d09753dcf5f48071
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.person.nuxeo;
25
26 import java.util.Iterator;
27 import java.util.Arrays;
28 import java.util.List;
29 import java.util.Map;
30
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;
40
41 import org.nuxeo.ecm.core.api.DocumentModel;
42 import org.nuxeo.ecm.core.api.DocumentModelList;
43
44 //import org.slf4j.Logger;
45 //import org.slf4j.LoggerFactory;
46
47 /**
48  * PersonDocumentModelHandler
49  *
50  * $LastChangedRevision: $
51  * $LastChangedDate: $
52  */
53 /**
54  * @author pschmitz
55  *
56  */
57 public class PersonDocumentModelHandler
58         extends AuthorityItemDocumentModelHandler<PersonsCommon, PersonsCommonList> {
59
60     /** The logger. */
61     //private final Logger logger = LoggerFactory.getLogger(PersonDocumentModelHandler.class);
62     /**
63      * Common part schema label
64      */
65     private static final String COMMON_PART_LABEL = "persons_common";
66     
67     public PersonDocumentModelHandler() {
68         super(COMMON_PART_LABEL);
69     }
70         
71     /* (non-Javadoc)
72      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
73      */
74     @Override
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());
79     }
80     
81     /* (non-Javadoc)
82      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
83      */
84     @Override
85     public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
86         super.handleUpdate(wrapDoc);
87         handleDisplayNames(wrapDoc.getWrappedObject());
88     }
89
90     /**
91      * Handle display names.
92      *
93      * @param docModel the doc model
94      * @throws Exception the exception
95      */
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) {
107                 String forename = 
108                                 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.FORE_NAME);
109                 String lastname = 
110                                 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.SUR_NAME);
111                 if(shortDisplayNameComputed) {
112                         String displayName = prepareDefaultDisplayName(forename, null, lastname,
113                                         null, null);
114                         docModel.setProperty(commonPartLabel, PersonJAXBSchema.SHORT_DISPLAY_NAME,
115                                         displayName);
116                 }
117                 if(displayNameComputed) {
118                         String midname = 
119                                         (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.MIDDLE_NAME);                            
120                         String birthdate = 
121                                         (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.BIRTH_DATE);
122                         String deathdate = 
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,
127                                         displayName);
128                 }
129         }
130     }
131         
132         
133     /**
134      * Produces a default displayName from the basic name and dates fields.
135      * see PersonAuthorityClientUtils.prepareDefaultDisplayName(String,String,String,String,String) which
136      * duplicates this logic, until we define a service-general utils package
137      * that is neither client nor service specific.
138      * @param foreName  
139      * @param middleName
140      * @param surName
141      * @param birthDate
142      * @param deathDate
143      * @return
144      * @throws Exception
145      */
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 = "-";
151
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) {
159                                         newStr.append(SEP);
160                                 }
161                                 newStr.append(partStr);
162                                 firstAdded = true;
163                         }
164         }
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) {
168                         if (firstAdded) {
169                                 newStr.append(SEP);
170                         }
171                         newStr.append(birthDate);
172                 newStr.append(DATE_SEP);                // Put this in whether there is a death date or not
173                         foundBirth = true;
174                 }
175                 if (deathDate != null) {
176                         if (!foundBirth) {
177                                 if (firstAdded == true) {
178                                         newStr.append(SEP);
179                                 }
180                         newStr.append(DATE_SEP);
181                         }
182                         newStr.append(deathDate);
183                 }
184                 
185                 return newStr.toString();
186     }
187     
188
189     /* (non-Javadoc)
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)
191      */
192     @Override
193     protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
194             throws Exception {
195         Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
196         
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);
201         }
202         
203         return unQObjectProperties;
204     }
205     
206     /* (non-Javadoc)
207      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
208      */
209     @Override
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(
218                                 "persons");
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/"
230                                         + id);
231                         ilistItem.setCsid(id);
232                         list.add(ilistItem);
233                 }
234
235                 return coList;
236         }
237
238     /**
239      * getQProperty converts the given property to qualified schema property
240      * @param prop
241      * @return
242      */
243     @Override
244     public String getQProperty(String prop) {
245         return PersonConstants.NUXEO_SCHEMA_NAME + ":" + prop;
246     }
247 }
248