]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b83e0d89df3cb36cb7b7d275f057ee91a235ca43
[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.common.vocabulary.nuxeo.AuthorityItemDocumentModelHandler;
32 import org.collectionspace.services.PersonJAXBSchema;
33 import org.collectionspace.services.common.document.DocumentWrapper;
34 import org.collectionspace.services.common.service.ObjectPartType;
35 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
36 import org.collectionspace.services.jaxb.AbstractCommonList;
37 import org.collectionspace.services.person.PersonsCommon;
38 import org.collectionspace.services.person.PersonsCommonList;
39 import org.collectionspace.services.person.PersonsCommonList.PersonListItem;
40
41 import org.nuxeo.ecm.core.api.DocumentModel;
42 import org.nuxeo.ecm.core.api.DocumentModelList;
43
44 //import org.slf4j.Logger;
45 //import org.slf4j.LoggerFactory;
46
47 /**
48  * PersonDocumentModelHandler
49  *
50  * $LastChangedRevision: $
51  * $LastChangedDate: $
52  */
53 /**
54  * @author pschmitz
55  *
56  */
57 public class PersonDocumentModelHandler
58         extends AuthorityItemDocumentModelHandler<PersonsCommon, PersonsCommonList> {
59
60     /** The logger. */
61     //private final Logger logger = LoggerFactory.getLogger(PersonDocumentModelHandler.class);
62     /**
63      * Common part schema label
64      */
65     private static final String COMMON_PART_LABEL = "persons_common";
66     
67     public PersonDocumentModelHandler() {
68         super(COMMON_PART_LABEL);
69     }
70         
71     /* (non-Javadoc)
72      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
73      */
74     @Override
75     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
76         // first fill all the parts of the document
77         super.handleCreate(wrapDoc);            
78         handleDisplayName(wrapDoc.getWrappedObject());
79     }
80     
81     /* (non-Javadoc)
82      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
83      */
84     @Override
85     public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
86         super.handleUpdate(wrapDoc);
87         handleDisplayName(wrapDoc.getWrappedObject());
88     }
89
90     /**
91      * Handle display name.
92      *
93      * @param docModel the doc model
94      * @throws Exception the exception
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 == true) {
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                 docModel.setProperty(commonPartLabel, PersonJAXBSchema.DISPLAY_NAME,
108                                 displayName);
109         }
110     }
111         
112     /**
113      * Produces a default displayName from the basic name and dates fields.
114      * @see PersonAuthorityClientUtils.prepareDefaultDisplayName() which
115      * duplicates this logic, until we define a service-general utils package
116      * that is neither client nor service specific.
117      * @param foreName  
118      * @param middleName
119      * @param surName
120      * @param birthDate
121      * @param deathDate
122      * @return
123      * @throws Exception
124      */
125     private static String prepareDefaultDisplayName(
126                 String foreName, String middleName, String surName,
127                 String birthDate, String deathDate ) throws Exception {
128                 final String SEP = " ";
129                 final String DATE_SEP = "-";
130
131                 StringBuilder newStr = new StringBuilder();
132                 List<String> nameStrings = 
133                         Arrays.asList(foreName, middleName, surName);
134                 boolean firstAdded = false;
135         for (String partStr : nameStrings ){
136                         if (partStr != null) {
137                                 if (firstAdded == true) {
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 (birthDate != null) {
147                         if (firstAdded) {
148                                 newStr.append(SEP);
149                         }
150                         newStr.append(birthDate);
151                 newStr.append(DATE_SEP);                // Put this in whether there is a death date or not
152                         foundBirth = true;
153                 }
154                 if (deathDate != null) {
155                         if (!foundBirth) {
156                                 if (firstAdded == true) {
157                                         newStr.append(SEP);
158                                 }
159                         newStr.append(DATE_SEP);
160                         }
161                         newStr.append(deathDate);
162                 }
163                 
164                 return newStr.toString();
165     }
166     
167
168     /* (non-Javadoc)
169      * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#extractPart(org.nuxeo.ecm.core.api.DocumentModel, java.lang.String, org.collectionspace.services.common.service.ObjectPartType)
170      */
171     @Override
172     protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
173             throws Exception {
174         Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
175         
176         // Add the CSID to the common part
177         if (partMeta.getLabel().equalsIgnoreCase(COMMON_PART_LABEL)) {
178                 String csid = NuxeoUtils.extractId(docModel.getPathAsString());
179                 unQObjectProperties.put("csid", csid);
180         }
181         
182         return unQObjectProperties;
183     }
184     
185     /* (non-Javadoc)
186      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
187      */
188     @Override
189         public PersonsCommonList extractCommonPartList(
190                         DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
191                 PersonsCommonList coList = extractPagingInfo(new PersonsCommonList(), wrapDoc);
192         AbstractCommonList commonList = (AbstractCommonList) coList;
193         commonList.setFieldsReturned("displayName|refName|uri|csid");
194                 List<PersonsCommonList.PersonListItem> list = coList.getPersonListItem();
195                 Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();
196                 String commonPartLabel = getServiceContext().getCommonPartLabel(
197                                 "persons");
198                 while (iter.hasNext()) {
199                         DocumentModel docModel = iter.next();
200                         PersonListItem ilistItem = new PersonListItem();
201                         ilistItem.setDisplayName((String) docModel.getProperty(
202                                         commonPartLabel, PersonJAXBSchema.DISPLAY_NAME));
203                         ilistItem.setRefName((String) docModel.getProperty(commonPartLabel,
204                                         PersonJAXBSchema.REF_NAME));
205                         String id = NuxeoUtils.extractId(docModel.getPathAsString());
206                         ilistItem.setUri("/personauthorities/" + inAuthority + "/items/"
207                                         + id);
208                         ilistItem.setCsid(id);
209                         list.add(ilistItem);
210                 }
211
212                 return coList;
213         }
214
215     /**
216      * getQProperty converts the given property to qualified schema property
217      * @param prop
218      * @return
219      */
220     @Override
221     public String getQProperty(String prop) {
222         return PersonConstants.NUXEO_SCHEMA_NAME + ":" + prop;
223     }
224 }
225