]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
10d70b1bb981c29161e1cf9c6ba07902970e0694
[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.organization.nuxeo;
25
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityItemDocumentModelHandler;
31 import org.collectionspace.services.OrganizationJAXBSchema;
32 import org.collectionspace.services.common.document.DocumentWrapper;
33 import org.collectionspace.services.common.service.ObjectPartType;
34 import org.collectionspace.services.jaxb.AbstractCommonList;
35 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
36 import org.collectionspace.services.organization.OrganizationsCommon;
37 import org.collectionspace.services.organization.OrganizationsCommonList;
38 import org.collectionspace.services.organization.OrganizationsCommonList.OrganizationListItem;
39 import org.nuxeo.ecm.core.api.DocumentModel;
40 import org.nuxeo.ecm.core.api.DocumentModelList;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * OrganizationDocumentModelHandler
46  *
47  * $LastChangedRevision$
48  * $LastChangedDate$
49  */
50 public class OrganizationDocumentModelHandler
51                 extends AuthorityItemDocumentModelHandler<OrganizationsCommon, OrganizationsCommonList> {
52
53     /** The logger. */
54     private final Logger logger = LoggerFactory.getLogger(OrganizationDocumentModelHandler.class);
55     /**
56      * Common part schema label
57      */
58     private static final String COMMON_PART_LABEL = "organizations_common";   
59     
60     public OrganizationDocumentModelHandler() {
61         super(COMMON_PART_LABEL);
62     }
63         
64     /* (non-Javadoc)
65      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
66      */
67     @Override
68     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
69         // first fill all the parts of the document
70         super.handleCreate(wrapDoc);            
71         handleDisplayNames(wrapDoc.getWrappedObject());
72     }
73     
74     /* (non-Javadoc)
75      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
76      */
77     @Override
78     public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
79         super.handleUpdate(wrapDoc);
80         handleDisplayNames(wrapDoc.getWrappedObject());
81     }
82
83     /**
84      * Check the logic around the computed displayName
85      * 
86      * @param docModel
87      * 
88      * @throws Exception the exception
89      */
90     private void handleDisplayNames(DocumentModel docModel) throws Exception {
91         String commonPartLabel = getServiceContext().getCommonPartLabel("organizations");
92         Boolean displayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
93                         OrganizationJAXBSchema.DISPLAY_NAME_COMPUTED);
94         Boolean shortDisplayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
95                         OrganizationJAXBSchema.SHORT_DISPLAY_NAME_COMPUTED);
96         if(displayNameComputed==null)
97                 displayNameComputed = true;
98         if(shortDisplayNameComputed==null)
99                 shortDisplayNameComputed = true;
100         if (displayNameComputed || shortDisplayNameComputed) {
101                  List<Object> mainBodyGroupList = (List<Object>) docModel.getProperty(commonPartLabel,
102                          OrganizationJAXBSchema.MAIN_BODY_GROUP_LIST);
103                  // FIXME: Determine how to handle cases where primary short name is null or empty.
104                 String shortName = primaryValueFromMultivalue(mainBodyGroupList,
105                         OrganizationJAXBSchema.SHORT_NAME);
106                 if(shortDisplayNameComputed) {
107                         String displayName = prepareDefaultDisplayName(shortName, null);
108                         docModel.setProperty(commonPartLabel, OrganizationJAXBSchema.SHORT_DISPLAY_NAME,
109                                         displayName);
110                 }
111                 if(displayNameComputed) {
112                 String foundingPlace = (String) docModel.getProperty(commonPartLabel,
113                                                 OrganizationJAXBSchema.FOUNDING_PLACE);
114                         String displayName = prepareDefaultDisplayName(shortName, foundingPlace);
115                                 docModel.setProperty(commonPartLabel, OrganizationJAXBSchema.DISPLAY_NAME,
116                                                         displayName);
117                 }
118         }
119     }
120
121     /**
122      * Produces a default displayName from the basic name and foundingPlace fields.
123      * @see OrgAuthorityClientUtils.prepareDefaultDisplayName() which
124      * duplicates this logic, until we define a service-general utils package
125      * that is neither client nor service specific.
126      * @param shortName
127      * @param foundingPlace
128      * @return
129      * @throws Exception
130      */
131     private static String prepareDefaultDisplayName(
132                 String shortName, String foundingPlace ) throws Exception {
133         StringBuilder newStr = new StringBuilder();
134                 final String sep = " ";
135                 boolean firstAdded = false;
136                 if(null != shortName ) {
137                         newStr.append(shortName);
138                         firstAdded = true;
139                 }
140         // Now we add the place
141                 if(null != foundingPlace ) {
142                         if(firstAdded) {
143                                 newStr.append(sep);
144                         }
145                         newStr.append(foundingPlace);
146                 }
147                 return newStr.toString();
148     }
149     
150     /* (non-Javadoc)
151      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
152      */
153     @Override
154     public OrganizationsCommonList extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc) 
155                 throws Exception {
156         OrganizationsCommonList coList = this.extractPagingInfo(new OrganizationsCommonList(), wrapDoc);
157         AbstractCommonList commonList = (AbstractCommonList) coList;
158         commonList.setFieldsReturned("displayName|refName|shortIdentifier|uri|csid");
159         List<OrganizationsCommonList.OrganizationListItem> list = coList.getOrganizationListItem();
160         Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();
161         String commonPartLabel = getServiceContext().getCommonPartLabel("organizations");
162         while(iter.hasNext()){
163             DocumentModel docModel = iter.next();
164             OrganizationListItem ilistItem = new OrganizationListItem();
165             ilistItem.setDisplayName((String)
166                         docModel.getProperty(commonPartLabel,OrganizationJAXBSchema.DISPLAY_NAME ));
167                         ilistItem.setShortIdentifier((String) docModel.getProperty(commonPartLabel,
168                                         OrganizationJAXBSchema.SHORT_IDENTIFIER));
169                         ilistItem.setRefName((String) 
170                                         docModel.getProperty(commonPartLabel, OrganizationJAXBSchema.REF_NAME));
171                         String id = NuxeoUtils.extractId(docModel.getPathAsString());
172             ilistItem.setUri("/orgauthorities/" + this.inAuthority + "/items/" + id);
173             ilistItem.setCsid(id);
174             list.add(ilistItem);
175         }
176
177         return coList;
178     }
179
180     /**
181      * getQProperty converts the given property to qualified schema property
182      * @param prop
183      * @return
184      */
185     @Override
186     public String getQProperty(String prop) {
187         return OrganizationConstants.NUXEO_SCHEMA_NAME + ":" + prop;
188     }
189
190     /**
191      * Returns the primary value from a list of values.
192      *
193      * Assumes that the first value is the primary value.
194      * This assumption may change when and if the primary value
195      * is identified explicitly.
196      *
197      * @param values a list of values.
198      * @param propertyName the name of a property through
199      *     which the value can be extracted.
200      * @return the primary value.
201      */
202     private String primaryValueFromMultivalue(List<Object> values, String propertyName) {
203         String primaryValue = "";
204         if (values == null || values.size() == 0) {
205             return primaryValue;
206         }
207         Object value = values.get(0);
208         if (value instanceof String) {
209             if (value != null) {
210                 primaryValue = (String) value;
211             }
212        // Multivalue group of fields
213        } else if (value instanceof Map) {
214             if (value != null) {
215                 Map map = (Map) value;
216                 if (map.values().size() > 0) {
217                     if (map.get(propertyName) != null) {
218                       primaryValue = (String) map.get(propertyName);
219                     }
220                 }
221             }
222        }
223        return primaryValue;
224     }
225 }
226