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