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