]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
491d7a697e3bfb5b1f7105c84041d97738c00f78
[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     /**
68      * Handle display names.
69      *
70      * @param docModel the doc model
71      * @throws Exception the exception
72      */
73     @Override
74     protected void handleComputedDisplayNames(DocumentModel docModel) throws Exception {
75         String commonPartLabel = getServiceContext().getCommonPartLabel("persons");
76         Boolean displayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
77                         PersonJAXBSchema.DISPLAY_NAME_COMPUTED);
78         Boolean shortDisplayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
79                         PersonJAXBSchema.SHORT_DISPLAY_NAME_COMPUTED);
80         if(displayNameComputed==null)
81                 displayNameComputed = true;
82         if(shortDisplayNameComputed==null)
83                 shortDisplayNameComputed = true;
84         if (displayNameComputed || shortDisplayNameComputed) {
85                 String forename = 
86                                 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.FORE_NAME);
87                 String lastname = 
88                                 (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.SUR_NAME);
89                 if(shortDisplayNameComputed) {
90                         String displayName = prepareDefaultDisplayName(forename, null, lastname,
91                                         null, null);
92                         docModel.setProperty(commonPartLabel, PersonJAXBSchema.SHORT_DISPLAY_NAME,
93                                         displayName);
94                 }
95                 if(displayNameComputed) {
96                         String midname = 
97                                         (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.MIDDLE_NAME);                            
98                         String birthdate = 
99                                         (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.BIRTH_DATE);
100                         String deathdate = 
101                                         (String)docModel.getProperty(commonPartLabel, PersonJAXBSchema.DEATH_DATE);
102                         String displayName = prepareDefaultDisplayName(forename, midname, lastname,
103                                         birthdate, deathdate);
104                         docModel.setProperty(commonPartLabel, PersonJAXBSchema.DISPLAY_NAME,
105                                         displayName);
106                 }
107         }
108     }
109         
110         
111     /**
112      * Produces a default displayName from the basic name and dates fields.
113      * see PersonAuthorityClientUtils.prepareDefaultDisplayName(String,String,String,String,String) which
114      * duplicates this logic, until we define a service-general utils package
115      * that is neither client nor service specific.
116      * @param foreName  
117      * @param middleName
118      * @param surName
119      * @param birthDate
120      * @param deathDate
121      * @return
122      * @throws Exception
123      */
124     private static String prepareDefaultDisplayName(
125                 String foreName, String middleName, String surName,
126                 String birthDate, String deathDate ) throws Exception {
127                 final String SEP = " ";
128                 final String DATE_SEP = "-";
129
130                 StringBuilder newStr = new StringBuilder();
131                 List<String> nameStrings = 
132                         Arrays.asList(foreName, middleName, surName);
133                 boolean firstAdded = false;
134         for (String partStr : nameStrings ){
135                         if (partStr != null) {
136                                 if (firstAdded == true) {
137                                         newStr.append(SEP);
138                                 }
139                                 newStr.append(partStr);
140                                 firstAdded = true;
141                         }
142         }
143         // Now we add the dates. In theory could have dates with no name, but that is their problem.
144         boolean foundBirth = false;
145                 if (birthDate != null) {
146                         if (firstAdded) {
147                                 newStr.append(SEP);
148                         }
149                         newStr.append(birthDate);
150                 newStr.append(DATE_SEP);                // Put this in whether there is a death date or not
151                         foundBirth = true;
152                 }
153                 if (deathDate != null) {
154                         if (!foundBirth) {
155                                 if (firstAdded == true) {
156                                         newStr.append(SEP);
157                                 }
158                         newStr.append(DATE_SEP);
159                         }
160                         newStr.append(deathDate);
161                 }
162                 
163                 return newStr.toString();
164     }
165     
166
167     /* (non-Javadoc)
168      * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#extractPart(org.nuxeo.ecm.core.api.DocumentModel, java.lang.String, org.collectionspace.services.common.service.ObjectPartType)
169      */
170     @Override
171     protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
172             throws Exception {
173         Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
174         
175         // Add the CSID to the common part
176         if (partMeta.getLabel().equalsIgnoreCase(COMMON_PART_LABEL)) {
177                 String csid = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
178                 unQObjectProperties.put("csid", csid);
179         }
180         
181         return unQObjectProperties;
182     }
183     
184     /**
185      * getQProperty converts the given property to qualified schema property
186      * @param prop
187      * @return
188      */
189     @Override
190     public String getQProperty(String prop) {
191         return PersonConstants.NUXEO_SCHEMA_NAME + ":" + prop;
192     }
193 }
194