]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
ea7f3275785c10b3933c8cadd9a24b3b36d4e134
[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
29 import org.collectionspace.services.client.PersonAuthorityClient;
30 import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityItemDocumentModelHandler;
31 import org.collectionspace.services.PersonJAXBSchema;
32 import org.collectionspace.services.person.PersonsCommon;
33 import org.nuxeo.ecm.core.api.DocumentModel;
34
35 /**
36  * PersonDocumentModelHandler
37  *
38  * $LastChangedRevision: $
39  * $LastChangedDate: $
40  */
41 /**
42  * @author pschmitz
43  *
44  */
45 public class PersonDocumentModelHandler
46         extends AuthorityItemDocumentModelHandler<PersonsCommon> {
47
48     /** The logger. */
49     //private final Logger logger = LoggerFactory.getLogger(PersonDocumentModelHandler.class);
50     /**
51      * Common part schema label
52      */
53     private static final String COMMON_PART_LABEL = "persons_common";
54     
55     public PersonDocumentModelHandler() {
56         super(COMMON_PART_LABEL);
57     }
58
59     @Override
60     public String getAuthorityServicePath(){
61         return PersonAuthorityClient.SERVICE_PATH_COMPONENT;    // CSPACE-3932
62     }
63         
64     /**
65      * Handle display names.
66      *
67      * @param docModel the doc model
68      * @throws Exception the exception
69      */
70     @Override
71     protected void handleComputedDisplayNames(DocumentModel docModel) throws Exception {
72         String commonPartLabel = getServiceContext().getCommonPartLabel("persons");
73         Boolean displayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
74                         PersonJAXBSchema.DISPLAY_NAME_COMPUTED);
75         Boolean shortDisplayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
76                         PersonJAXBSchema.SHORT_DISPLAY_NAME_COMPUTED);
77         if(displayNameComputed==null)
78                 displayNameComputed = true;
79         if(shortDisplayNameComputed==null)
80                 shortDisplayNameComputed = true;
81         if (displayNameComputed || shortDisplayNameComputed) {
82                 String forename = 
83                                 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.FORE_NAME);
84                 String lastname = 
85                                 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.SUR_NAME);
86                 if(shortDisplayNameComputed) {
87                         String displayName = prepareDefaultDisplayName(forename, null, lastname,
88                                         null, null);
89                         docModel.setProperty(commonPartLabel, PersonJAXBSchema.SHORT_DISPLAY_NAME,
90                                         displayName);
91                 }
92                 if(displayNameComputed) {
93                         String midname = 
94                                         (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.MIDDLE_NAME);                            
95                         String birthdate = 
96                                         (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.BIRTH_DATE);
97                         String deathdate = 
98                                         (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.DEATH_DATE);
99                         String displayName = prepareDefaultDisplayName(forename, midname, lastname,
100                                         birthdate, deathdate);
101                         docModel.setProperty(commonPartLabel, PersonJAXBSchema.DISPLAY_NAME,
102                                         displayName);
103                 }
104         }
105     }
106         
107         
108     /**
109      * Produces a default displayName from the basic name and dates fields.
110      * see PersonAuthorityClientUtils.prepareDefaultDisplayName(String,String,String,String,String) which
111      * duplicates this logic, until we define a service-general utils package
112      * that is neither client nor service specific.
113      * @param foreName  
114      * @param middleName
115      * @param surName
116      * @param birthDate
117      * @param deathDate
118      * @return
119      * @throws Exception
120      */
121     private static String prepareDefaultDisplayName(
122                 String foreName, String middleName, String surName,
123                 String birthDate, String deathDate ) throws Exception {
124                 final String SEP = " ";
125                 final String DATE_SEP = "-";
126
127                 StringBuilder newStr = new StringBuilder();
128                 List<String> nameStrings = 
129                         Arrays.asList(foreName, middleName, surName);
130                 boolean firstAdded = false;
131         for (String partStr : nameStrings ){
132                         if (partStr != null) {
133                                 if (firstAdded == true) {
134                                         newStr.append(SEP);
135                                 }
136                                 newStr.append(partStr);
137                                 firstAdded = true;
138                         }
139         }
140         // Now we add the dates. In theory could have dates with no name, but that is their problem.
141         boolean foundBirth = false;
142                 if (birthDate != null) {
143                         if (firstAdded) {
144                                 newStr.append(SEP);
145                         }
146                         newStr.append(birthDate);
147                 newStr.append(DATE_SEP);                // Put this in whether there is a death date or not
148                         foundBirth = true;
149                 }
150                 if (deathDate != null) {
151                         if (!foundBirth) {
152                                 if (firstAdded == true) {
153                                         newStr.append(SEP);
154                                 }
155                         newStr.append(DATE_SEP);
156                         }
157                         newStr.append(deathDate);
158                 }
159                 
160                 return newStr.toString();
161     }
162     
163     /**
164      * getQProperty converts the given property to qualified schema property
165      * @param prop
166      * @return
167      */
168     @Override
169     public String getQProperty(String prop) {
170         return PersonConstants.NUXEO_SCHEMA_NAME + ":" + prop;
171     }
172 }
173