]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e02b5b0553f31b3759e1f1cee1d97b4f427805bb
[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
31 import org.collectionspace.services.PersonJAXBSchema;
32 import org.collectionspace.services.common.document.DocumentWrapper;
33 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandler;
34 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
35 import org.collectionspace.services.person.PersonsCommon;
36 import org.collectionspace.services.person.PersonsCommonList;
37 import org.collectionspace.services.person.PersonsCommonList.PersonListItem;
38 import org.nuxeo.ecm.core.api.DocumentModel;
39 import org.nuxeo.ecm.core.api.DocumentModelList;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * PersonDocumentModelHandler
45  *
46  * $LastChangedRevision: $
47  * $LastChangedDate: $
48  */
49 public class PersonDocumentModelHandler
50         extends RemoteDocumentModelHandler<PersonsCommon, PersonsCommonList> {
51
52     private final Logger logger = LoggerFactory.getLogger(PersonDocumentModelHandler.class);
53     /**
54      * person is used to stash JAXB object to use when handle is called
55      * for Action.CREATE, Action.UPDATE or Action.GET
56      */
57     private PersonsCommon person;
58     /**
59      * personList is stashed when handle is called
60      * for ACTION.GET_ALL
61      */
62     private PersonsCommonList personList;
63     
64     /**
65      * inAuthority is the parent OrgAuthority for this context
66      */
67     private String inAuthority;
68
69     public String getInAuthority() {
70                 return inAuthority;
71         }
72
73         public void setInAuthority(String inAuthority) {
74                 this.inAuthority = inAuthority;
75         }
76
77         @Override
78     public void prepare(Action action) throws Exception {
79         //no specific action needed
80     }
81
82     /* Override handleGet so we can deal with defaulting the displayName
83      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
84      */
85     @Override
86     public void handleGet(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
87         DocumentModel docModel = wrapDoc.getWrappedObject();
88         String displayName = (String) docModel.getProperty(getServiceContext().getCommonPartLabel("persons"),
89                         PersonJAXBSchema.DISPLAY_NAME);
90         if(displayName == null) {
91                 docModel.setProperty(getServiceContext().getCommonPartLabel("persons"),
92                                 PersonJAXBSchema.DISPLAY_NAME, prepareDefaultDisplayName(docModel));
93         }
94         super.handleGet(wrapDoc);
95     }
96     
97     private String prepareDefaultDisplayName(DocumentModel docModel) throws Exception {
98         StringBuilder newStr = new StringBuilder();
99                 String part = null;
100                 final String sep = " ";
101                 final String dateSep = "-";
102                 List<String> nameStrings = 
103                         Arrays.asList(PersonJAXBSchema.FORE_NAME, PersonJAXBSchema.MIDDLE_NAME, 
104                                         PersonJAXBSchema.SUR_NAME);
105                 boolean firstAdded = false;
106         for(String partStr : nameStrings ){
107                         if(null != (part = (String) 
108                                         docModel.getProperty(getServiceContext().getCommonPartLabel("persons"), partStr ))) {
109                                 if(firstAdded) {
110                                         newStr.append(sep);
111                                 }
112                                 newStr.append(part);
113                                 firstAdded = true;
114                         }
115         }
116         // Now we add the dates. In theory could have dates with no name, but that is their problem.
117         boolean foundBirth = false;
118                 if(null != (part = (String) 
119                                 docModel.getProperty(getServiceContext().getCommonPartLabel("persons"), 
120                                                 PersonJAXBSchema.BIRTH_DATE ))) {
121                         if(firstAdded) {
122                                 newStr.append(sep);
123                         }
124                         newStr.append(part);
125                 newStr.append(dateSep);         // Put this in whether there is a death date or not
126                         foundBirth = true;
127                 }
128                 if(null != (part = (String) 
129                                 docModel.getProperty(getServiceContext().getCommonPartLabel("persons"), 
130                                                 PersonJAXBSchema.DEATH_DATE ))) {
131                         if(!foundBirth) {
132                                 if(firstAdded) {
133                                         newStr.append(sep);
134                                 }
135                         newStr.append(dateSep);
136                         }
137                         newStr.append(part);
138                 }
139                 return newStr.toString();
140     }
141
142     /**
143      * getCommonPart get associated person
144      * @return
145      */
146     @Override
147     public PersonsCommon getCommonPart() {
148         return person;
149     }
150
151     /**
152      * setCommonPart set associated person
153      * @param person
154      */
155     @Override
156     public void setCommonPart(PersonsCommon person) {
157         this.person = person;
158     }
159
160     /**
161      * getCommonPartList get associated person (for index/GET_ALL)
162      * @return
163      */
164     @Override
165     public PersonsCommonList getCommonPartList() {
166         return personList;
167     }
168
169     @Override
170     public void setCommonPartList(PersonsCommonList personList) {
171         this.personList = personList;
172     }
173
174     @Override
175     public PersonsCommon extractCommonPart(DocumentWrapper wrapDoc)
176             throws Exception {
177         throw new UnsupportedOperationException();
178     }
179
180     @Override
181     public void fillCommonPart(PersonsCommon personObject, DocumentWrapper wrapDoc) throws Exception {
182         throw new UnsupportedOperationException();
183     }
184
185     @Override
186     public PersonsCommonList extractCommonPartList(DocumentWrapper wrapDoc) 
187         throws Exception {
188         PersonsCommonList coList = new PersonsCommonList();
189         try{
190                 DocumentModelList docList = (DocumentModelList) wrapDoc.getWrappedObject();
191         
192                 List<PersonsCommonList.PersonListItem> list = 
193                         coList.getPersonListItem();
194         
195                 //FIXME: iterating over a long list of documents is not a long term
196                 //strategy...need to change to more efficient iterating in future
197                 Iterator<DocumentModel> iter = docList.iterator();
198                 while(iter.hasNext()){
199                     DocumentModel docModel = iter.next();
200                     PersonListItem ilistItem = new PersonListItem();
201                     // We look for a set display name, and fall back to teh short name if there is none
202                     String displayName = (String) docModel.getProperty(getServiceContext().getCommonPartLabel("persons"),
203                                                                                                 PersonJAXBSchema.DISPLAY_NAME);
204                     if(displayName == null)
205                             displayName = prepareDefaultDisplayName(docModel);
206                     ilistItem.setDisplayName( displayName );
207                     ilistItem.setRefName((String) docModel.getProperty(getServiceContext().getCommonPartLabel("persons"),
208                                                                         PersonJAXBSchema.REF_NAME));
209                                 String id = NuxeoUtils.extractId(docModel.getPathAsString());
210                     ilistItem.setUri("/personauthorities/"+inAuthority+"/items/" + id);
211                     ilistItem.setCsid(id);
212                     list.add(ilistItem);
213                 }
214         }catch(Exception e){
215             if(logger.isDebugEnabled()){
216                 logger.debug("Caught exception in extractCommonPartList", e);
217             }
218             throw e;
219         }
220         return coList;
221     }
222
223     /**
224      * getQProperty converts the given property to qualified schema property
225      * @param prop
226      * @return
227      */
228     @Override
229     public String getQProperty(String prop) {
230         return PersonConstants.NUXEO_SCHEMA_NAME + ":" + prop;
231     }
232 }
233