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