]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
6ee9045ede082a3a40baeae633f9e5d8c5aeb8d9
[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 = Boolean.TRUE;
98         if(shortDisplayNameComputed==null)
99                 shortDisplayNameComputed = Boolean.TRUE;
100         if (displayNameComputed.booleanValue() || shortDisplayNameComputed.booleanValue()) {
101                 String shortName = getStringValueInPrimaryRepeatingComplexProperty(
102                                 docModel, commonPartLabel, OrganizationJAXBSchema.MAIN_BODY_GROUP_LIST, 
103                                 OrganizationJAXBSchema.SHORT_NAME);
104             // FIXME: Determine how to handle cases where primary short name is null or empty.
105                 if(shortDisplayNameComputed.booleanValue()) {
106                         String displayName = prepareDefaultDisplayName(shortName, null);
107                         docModel.setProperty(commonPartLabel, OrganizationJAXBSchema.SHORT_DISPLAY_NAME,
108                                         displayName);
109                 }
110                 if(displayNameComputed.booleanValue()) {
111                 String foundingPlace = (String) docModel.getProperty(commonPartLabel,
112                                                 OrganizationJAXBSchema.FOUNDING_PLACE);
113                         String displayName = prepareDefaultDisplayName(shortName, foundingPlace);
114                                 docModel.setProperty(commonPartLabel, OrganizationJAXBSchema.DISPLAY_NAME,
115                                                         displayName);
116                 }
117         }
118     }
119
120     /**
121      * Produces a default displayName from the basic name and foundingPlace fields.
122      * @see OrgAuthorityClientUtils.prepareDefaultDisplayName() which
123      * duplicates this logic, until we define a service-general utils package
124      * that is neither client nor service specific.
125      * @param shortName
126      * @param foundingPlace
127      * @return
128      * @throws Exception
129      */
130     private static String prepareDefaultDisplayName(
131                 String shortName, String foundingPlace ) throws Exception {
132         StringBuilder newStr = new StringBuilder();
133                 final String sep = " ";
134                 boolean firstAdded = false;
135                 if(null != shortName ) {
136                         newStr.append(shortName);
137                         firstAdded = true;
138                 }
139         // Now we add the place
140                 if(null != foundingPlace ) {
141                         if(firstAdded) {
142                                 newStr.append(sep);
143                         }
144                         newStr.append(foundingPlace);
145                 }
146                 return newStr.toString();
147     }
148     
149     /* (non-Javadoc)
150      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
151      */
152     @Override
153     public OrganizationsCommonList extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc) 
154                 throws Exception {
155         OrganizationsCommonList coList = this.extractPagingInfo(new OrganizationsCommonList(), wrapDoc);
156         AbstractCommonList commonList = (AbstractCommonList) coList;
157         commonList.setFieldsReturned("displayName|refName|shortIdentifier|uri|csid");
158         List<OrganizationsCommonList.OrganizationListItem> list = coList.getOrganizationListItem();
159         Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();
160         String commonPartLabel = getServiceContext().getCommonPartLabel("organizations");
161         while(iter.hasNext()){
162             DocumentModel docModel = iter.next();
163             OrganizationListItem ilistItem = new OrganizationListItem();
164             ilistItem.setDisplayName((String)
165                         docModel.getProperty(commonPartLabel,OrganizationJAXBSchema.DISPLAY_NAME ));
166                         ilistItem.setShortIdentifier((String) docModel.getProperty(commonPartLabel,
167                                         OrganizationJAXBSchema.SHORT_IDENTIFIER));
168                         ilistItem.setRefName((String) 
169                                         docModel.getProperty(commonPartLabel, OrganizationJAXBSchema.REF_NAME));
170                         String id = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
171             ilistItem.setUri("/orgauthorities/" + this.inAuthority + "/items/" + id);
172             ilistItem.setCsid(id);
173             list.add(ilistItem);
174         }
175
176         return coList;
177     }
178
179     /**
180      * getQProperty converts the given property to qualified schema property
181      * @param prop
182      * @return
183      */
184     @Override
185     public String getQProperty(String prop) {
186         return OrganizationConstants.NUXEO_SCHEMA_NAME + ":" + prop;
187     }
188
189 }
190