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