]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a3e076a4759d31bacb4fd1f6412d239885fbd9f2
[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.Arrays;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.collectionspace.services.client.PersonAuthorityClient;
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.person.PersonsCommon;
36 import org.nuxeo.ecm.core.api.DocumentModel;
37
38 /**
39  * PersonDocumentModelHandler
40  *
41  * $LastChangedRevision: $
42  * $LastChangedDate: $
43  */
44 /**
45  * @author pschmitz
46  *
47  */
48 public class PersonDocumentModelHandler
49         extends AuthorityItemDocumentModelHandler<PersonsCommon> {
50
51     /** The logger. */
52     //private final Logger logger = LoggerFactory.getLogger(PersonDocumentModelHandler.class);
53     /**
54      * Common part schema label
55      */
56     private static final String COMMON_PART_LABEL = "persons_common";
57     
58     public PersonDocumentModelHandler() {
59         super(COMMON_PART_LABEL);
60     }
61
62     @Override
63     public String getAuthorityServicePath(){
64         return PersonAuthorityClient.SERVICE_PATH_COMPONENT;    // CSPACE-3932
65     }
66         
67     /* (non-Javadoc)
68      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
69      */
70     @Override
71     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
72         // first fill all the parts of the document
73         super.handleCreate(wrapDoc);            
74         handleDisplayNames(wrapDoc.getWrappedObject());
75     }
76     
77     /* (non-Javadoc)
78      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
79      */
80     @Override
81     public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
82         super.handleUpdate(wrapDoc);
83         handleDisplayNames(wrapDoc.getWrappedObject());
84     }
85
86     /**
87      * Handle display names.
88      *
89      * @param docModel the doc model
90      * @throws Exception the exception
91      */
92     private void handleDisplayNames(DocumentModel docModel) throws Exception {
93         String commonPartLabel = getServiceContext().getCommonPartLabel("persons");
94         Boolean displayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
95                         PersonJAXBSchema.DISPLAY_NAME_COMPUTED);
96         Boolean shortDisplayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
97                         PersonJAXBSchema.SHORT_DISPLAY_NAME_COMPUTED);
98         if(displayNameComputed==null)
99                 displayNameComputed = true;
100         if(shortDisplayNameComputed==null)
101                 shortDisplayNameComputed = true;
102         if (displayNameComputed || shortDisplayNameComputed) {
103                 String forename = 
104                                 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.FORE_NAME);
105                 String lastname = 
106                                 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.SUR_NAME);
107                 if(shortDisplayNameComputed) {
108                         String displayName = prepareDefaultDisplayName(forename, null, lastname,
109                                         null, null);
110                         docModel.setProperty(commonPartLabel, PersonJAXBSchema.SHORT_DISPLAY_NAME,
111                                         displayName);
112                 }
113                 if(displayNameComputed) {
114                         String midname = 
115                                         (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.MIDDLE_NAME);                            
116                         String birthdate = 
117                                         (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.BIRTH_DATE);
118                         String deathdate = 
119                                         (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.DEATH_DATE);
120                         String displayName = prepareDefaultDisplayName(forename, midname, lastname,
121                                         birthdate, deathdate);
122                         docModel.setProperty(commonPartLabel, PersonJAXBSchema.DISPLAY_NAME,
123                                         displayName);
124                 }
125         }
126     }
127         
128         
129     /**
130      * Produces a default displayName from the basic name and dates fields.
131      * see PersonAuthorityClientUtils.prepareDefaultDisplayName(String,String,String,String,String) which
132      * duplicates this logic, until we define a service-general utils package
133      * that is neither client nor service specific.
134      * @param foreName  
135      * @param middleName
136      * @param surName
137      * @param birthDate
138      * @param deathDate
139      * @return
140      * @throws Exception
141      */
142     private static String prepareDefaultDisplayName(
143                 String foreName, String middleName, String surName,
144                 String birthDate, String deathDate ) throws Exception {
145                 final String SEP = " ";
146                 final String DATE_SEP = "-";
147
148                 StringBuilder newStr = new StringBuilder();
149                 List<String> nameStrings = 
150                         Arrays.asList(foreName, middleName, surName);
151                 boolean firstAdded = false;
152         for (String partStr : nameStrings ){
153                         if (partStr != null) {
154                                 if (firstAdded == true) {
155                                         newStr.append(SEP);
156                                 }
157                                 newStr.append(partStr);
158                                 firstAdded = true;
159                         }
160         }
161         // Now we add the dates. In theory could have dates with no name, but that is their problem.
162         boolean foundBirth = false;
163                 if (birthDate != null) {
164                         if (firstAdded) {
165                                 newStr.append(SEP);
166                         }
167                         newStr.append(birthDate);
168                 newStr.append(DATE_SEP);                // Put this in whether there is a death date or not
169                         foundBirth = true;
170                 }
171                 if (deathDate != null) {
172                         if (!foundBirth) {
173                                 if (firstAdded == true) {
174                                         newStr.append(SEP);
175                                 }
176                         newStr.append(DATE_SEP);
177                         }
178                         newStr.append(deathDate);
179                 }
180                 
181                 return newStr.toString();
182     }
183     
184
185     /* (non-Javadoc)
186      * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#extractPart(org.nuxeo.ecm.core.api.DocumentModel, java.lang.String, org.collectionspace.services.common.service.ObjectPartType)
187      */
188     @Override
189     protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
190             throws Exception {
191         Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
192         
193         // Add the CSID to the common part
194         if (partMeta.getLabel().equalsIgnoreCase(COMMON_PART_LABEL)) {
195                 String csid = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
196                 unQObjectProperties.put("csid", csid);
197         }
198         
199         return unQObjectProperties;
200     }
201     
202     /**
203      * getQProperty converts the given property to qualified schema property
204      * @param prop
205      * @return
206      */
207     @Override
208     public String getQProperty(String prop) {
209         return PersonConstants.NUXEO_SCHEMA_NAME + ":" + prop;
210     }
211 }
212