]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a2e0973ff726e7d81a49f867e493df258844cd04
[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.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30
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;
42
43 /**
44  * PersonDocumentModelHandler
45  *
46  * $LastChangedRevision: $
47  * $LastChangedDate: $
48  */
49 /**
50  * @author pschmitz
51  *
52  */
53 public class PersonDocumentModelHandler
54         extends RemoteDocumentModelHandler<PersonsCommon, PersonsCommonList> {
55
56     private final Logger logger = LoggerFactory.getLogger(PersonDocumentModelHandler.class);
57     /**
58      * person is used to stash JAXB object to use when handle is called
59      * for Action.CREATE, Action.UPDATE or Action.GET
60      */
61     private PersonsCommon person;
62     /**
63      * personList is stashed when handle is called
64      * for ACTION.GET_ALL
65      */
66     private PersonsCommonList personList;
67     
68     /**
69      * inAuthority is the parent OrgAuthority for this context
70      */
71     private String inAuthority;
72
73     public String getInAuthority() {
74                 return inAuthority;
75         }
76
77         public void setInAuthority(String inAuthority) {
78                 this.inAuthority = inAuthority;
79         }
80
81         
82     @Override
83     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
84         // first fill all the parts of the document
85         super.handleCreate(wrapDoc);            
86         handleGetDisplayName(wrapDoc.getWrappedObject());
87     }
88     
89     private String handleGetDisplayName(DocumentModel docModel) throws Exception {
90         return handleGetDisplayName(docModel, true);
91     }
92     
93     private String handleGetDisplayName(DocumentModel docModel, boolean updateDocModel) throws Exception {
94         String displayName = (String) docModel.getProperty(getServiceContext().getCommonPartLabel("persons"),
95                         PersonJAXBSchema.DISPLAY_NAME);
96         if (displayName == null) {
97                 displayName = prepareDefaultDisplayName(docModel);
98                         if (updateDocModel == true) {
99                                 docModel.setProperty(getServiceContext().getCommonPartLabel(
100                                                 "persons"), PersonJAXBSchema.DISPLAY_NAME, displayName);
101                         }
102         }
103         
104         return displayName;
105     }
106         
107     /* Override handleGet so we can deal with defaulting the displayName
108      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
109      */
110     @Override
111     public void handleGet(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
112         handleGetDisplayName(wrapDoc.getWrappedObject());
113         super.handleGet(wrapDoc);
114     }
115     
116     private String prepareDefaultDisplayName(DocumentModel docModel) throws Exception {
117         String commonPartLabel = getServiceContext().getCommonPartLabel("persons");
118         return prepareDefaultDisplayName(
119                         (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.FORE_NAME ),                     
120                         (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.MIDDLE_NAME ),                           
121                         (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.SUR_NAME ),                      
122                         (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.BIRTH_DATE ),                            
123                         (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.DEATH_DATE )                     
124                         );
125     }
126
127     
128     /**
129      * Produces a default displayName from the basic name and dates fields.
130      * @see PersonAuthorityClientUtils.prepareDefaultDisplayName() which
131      * duplicates this logic, until we define a service-general utils package
132      * that is neither client nor service specific.
133      * @param foreName  
134      * @param middleName
135      * @param surName
136      * @param birthDate
137      * @param deathDate
138      * @return
139      * @throws Exception
140      */
141     private static String prepareDefaultDisplayName(
142                 String foreName, String middleName, String surName,
143                 String birthDate, String deathDate ) throws Exception {
144         StringBuilder newStr = new StringBuilder();
145                 final String sep = " ";
146                 final String dateSep = "-";
147                 List<String> nameStrings = 
148                         Arrays.asList(foreName, middleName, surName);
149                 boolean firstAdded = false;
150         for(String partStr : nameStrings ){
151                         if(null != partStr ) {
152                                 if(firstAdded) {
153                                         newStr.append(sep);
154                                 }
155                                 newStr.append(partStr);
156                                 firstAdded = true;
157                         }
158         }
159         // Now we add the dates. In theory could have dates with no name, but that is their problem.
160         boolean foundBirth = false;
161                 if(null != birthDate) {
162                         if(firstAdded) {
163                                 newStr.append(sep);
164                         }
165                         newStr.append(birthDate);
166                 newStr.append(dateSep);         // Put this in whether there is a death date or not
167                         foundBirth = true;
168                 }
169                 if(null != deathDate) {
170                         if(!foundBirth) {
171                                 if(firstAdded) {
172                                         newStr.append(sep);
173                                 }
174                         newStr.append(dateSep);
175                         }
176                         newStr.append(deathDate);
177                 }
178                 return newStr.toString();
179     }
180     
181     /**
182      * getCommonPart get associated person
183      * @return
184      */
185     @Override
186     public PersonsCommon getCommonPart() {
187         return person;
188     }
189
190     /**
191      * setCommonPart set associated person
192      * @param person
193      */
194     @Override
195     public void setCommonPart(PersonsCommon person) {
196         this.person = person;
197     }
198
199     /**
200      * getCommonPartList get associated person (for index/GET_ALL)
201      * @return
202      */
203     @Override
204     public PersonsCommonList getCommonPartList() {
205         return personList;
206     }
207
208     @Override
209     public void setCommonPartList(PersonsCommonList personList) {
210         this.personList = personList;
211     }
212
213     @Override
214     public PersonsCommon extractCommonPart(DocumentWrapper wrapDoc)
215             throws Exception {
216         throw new UnsupportedOperationException();
217     }
218
219     @Override
220     public void fillCommonPart(PersonsCommon personObject, DocumentWrapper wrapDoc) throws Exception {
221         throw new UnsupportedOperationException();
222     }
223
224     @Override
225     public PersonsCommonList extractCommonPartList(DocumentWrapper wrapDoc) 
226         throws Exception {
227         PersonsCommonList coList = new PersonsCommonList();
228         try{
229                 DocumentModelList docList = (DocumentModelList) wrapDoc.getWrappedObject();
230         
231                 List<PersonsCommonList.PersonListItem> list = 
232                         coList.getPersonListItem();
233         
234                 //FIXME: iterating over a long list of documents is not a long term
235                 //strategy...need to change to more efficient iterating in future
236                 Iterator<DocumentModel> iter = docList.iterator();
237                 while(iter.hasNext()){
238                     DocumentModel docModel = iter.next();
239                     PersonListItem ilistItem = new PersonListItem();
240                     // We look for a set display name, and fall back to teh short name if there is none
241                     String displayName = handleGetDisplayName(docModel, false);             
242                                 ilistItem.setDisplayName(displayName);
243                     ilistItem.setRefName((String) docModel.getProperty(getServiceContext().getCommonPartLabel(
244                                 "persons"), PersonJAXBSchema.REF_NAME));
245                                 String id = NuxeoUtils.extractId(docModel.getPathAsString());
246                     ilistItem.setUri("/personauthorities/"+inAuthority+"/items/" + id);
247                     ilistItem.setCsid(id);
248                     list.add(ilistItem);
249                 }
250         }catch(Exception e){
251             if(logger.isDebugEnabled()){
252                 logger.debug("Caught exception in extractCommonPartList", e);
253             }
254             throw e;
255         }
256         return coList;
257     }
258
259     /**
260      * getQProperty converts the given property to qualified schema property
261      * @param prop
262      * @return
263      */
264     @Override
265     public String getQProperty(String prop) {
266         return PersonConstants.NUXEO_SCHEMA_NAME + ":" + prop;
267     }
268 }
269