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