]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f29ee16180d1807989ad989274abf22ff6f739be
[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.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;
41
42 import org.nuxeo.ecm.core.api.DocumentModel;
43 import org.nuxeo.ecm.core.api.DocumentModelList;
44
45 //import org.slf4j.Logger;
46 //import org.slf4j.LoggerFactory;
47
48 /**
49  * PersonDocumentModelHandler
50  *
51  * $LastChangedRevision: $
52  * $LastChangedDate: $
53  */
54 /**
55  * @author pschmitz
56  *
57  */
58 public class PersonDocumentModelHandler
59         extends AuthorityItemDocumentModelHandler<PersonsCommon, PersonsCommonList> {
60
61     /** The logger. */
62     //private final Logger logger = LoggerFactory.getLogger(PersonDocumentModelHandler.class);
63     /**
64      * Common part schema label
65      */
66     private static final String COMMON_PART_LABEL = "persons_common";
67     
68     public PersonDocumentModelHandler() {
69         super(COMMON_PART_LABEL);
70     }
71
72     @Override
73     public String getAuthorityServicePath(){
74         return PersonAuthorityClient.SERVICE_PATH_COMPONENT;    // CSPACE-3932
75     }
76         
77     /* (non-Javadoc)
78      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
79      */
80     @Override
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());
85     }
86     
87     /* (non-Javadoc)
88      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
89      */
90     @Override
91     public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
92         super.handleUpdate(wrapDoc);
93         handleDisplayNames(wrapDoc.getWrappedObject());
94     }
95
96     /**
97      * Handle display names.
98      *
99      * @param docModel the doc model
100      * @throws Exception the exception
101      */
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) {
113                 String forename = 
114                                 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.FORE_NAME);
115                 String lastname = 
116                                 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.SUR_NAME);
117                 if(shortDisplayNameComputed) {
118                         String displayName = prepareDefaultDisplayName(forename, null, lastname,
119                                         null, null);
120                         docModel.setProperty(commonPartLabel, PersonJAXBSchema.SHORT_DISPLAY_NAME,
121                                         displayName);
122                 }
123                 if(displayNameComputed) {
124                         String midname = 
125                                         (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.MIDDLE_NAME);                            
126                         String birthdate = 
127                                         (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.BIRTH_DATE);
128                         String deathdate = 
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,
133                                         displayName);
134                 }
135         }
136     }
137         
138         
139     /**
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.
144      * @param foreName  
145      * @param middleName
146      * @param surName
147      * @param birthDate
148      * @param deathDate
149      * @return
150      * @throws Exception
151      */
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 = "-";
157
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) {
165                                         newStr.append(SEP);
166                                 }
167                                 newStr.append(partStr);
168                                 firstAdded = true;
169                         }
170         }
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) {
174                         if (firstAdded) {
175                                 newStr.append(SEP);
176                         }
177                         newStr.append(birthDate);
178                 newStr.append(DATE_SEP);                // Put this in whether there is a death date or not
179                         foundBirth = true;
180                 }
181                 if (deathDate != null) {
182                         if (!foundBirth) {
183                                 if (firstAdded == true) {
184                                         newStr.append(SEP);
185                                 }
186                         newStr.append(DATE_SEP);
187                         }
188                         newStr.append(deathDate);
189                 }
190                 
191                 return newStr.toString();
192     }
193     
194
195     /* (non-Javadoc)
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)
197      */
198     @Override
199     protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
200             throws Exception {
201         Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
202         
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);
207         }
208         
209         return unQObjectProperties;
210     }
211     
212     /* (non-Javadoc)
213      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
214      */
215     @Override
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(
224                                 "persons");
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/"
236                                         + id);
237                         ilistItem.setCsid(id);
238                         list.add(ilistItem);
239                 }
240
241                 return coList;
242         }
243
244     /**
245      * getQProperty converts the given property to qualified schema property
246      * @param prop
247      * @return
248      */
249     @Override
250     public String getQProperty(String prop) {
251         return PersonConstants.NUXEO_SCHEMA_NAME + ":" + prop;
252     }
253 }
254