]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c5c399b3cdc21b81ce7d5a61c2e40ac74c30305d
[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
29 import org.collectionspace.services.OrganizationJAXBSchema;
30 import org.collectionspace.services.common.context.MultipartServiceContext;
31 import org.collectionspace.services.common.document.DocumentFilter;
32 import org.collectionspace.services.common.document.DocumentWrapper;
33 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
34 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
35 import org.collectionspace.services.organization.OrganizationsCommon;
36 import org.collectionspace.services.organization.OrganizationsCommonList;
37 import org.collectionspace.services.organization.OrganizationsCommonList.OrganizationListItem;
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  * OrganizationDocumentModelHandler
45  *
46  * $LastChangedRevision: $
47  * $LastChangedDate: $
48  */
49 public class OrganizationDocumentModelHandler
50         extends RemoteDocumentModelHandlerImpl<OrganizationsCommon, OrganizationsCommonList> {
51
52     private final Logger logger = LoggerFactory.getLogger(OrganizationDocumentModelHandler.class);
53     /**
54      * organization is used to stash JAXB object to use when handle is called
55      * for Action.CREATE, Action.UPDATE or Action.GET
56      */
57     private OrganizationsCommon organization;
58     /**
59      * organizationList is stashed when handle is called
60      * for ACTION.GET_ALL
61      */
62     private OrganizationsCommonList organizationList;
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         
78     @Override
79     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
80         // first fill all the parts of the document
81         super.handleCreate(wrapDoc);            
82         handleDisplayName(wrapDoc.getWrappedObject());
83     }
84     
85     @Override
86     public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
87         super.handleUpdate(wrapDoc);
88         handleDisplayName(wrapDoc.getWrappedObject());
89     }
90
91     /**
92      * Check the logic around the computed displayName
93      * 
94      * @param docModel
95      * 
96      * @throws Exception the exception
97      */
98     private void handleDisplayName(DocumentModel docModel) throws Exception {
99         String commonPartLabel = getServiceContext().getCommonPartLabel("organizations");
100         Boolean displayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
101                         OrganizationJAXBSchema.DISPLAY_NAME_COMPUTED);
102         if (displayNameComputed) {
103                 String displayName = prepareDefaultDisplayName(
104                                 (String) docModel.getProperty(commonPartLabel,OrganizationJAXBSchema.SHORT_NAME),
105                                 (String) docModel.getProperty(commonPartLabel,OrganizationJAXBSchema.FOUNDING_PLACE));
106                         docModel.setProperty(commonPartLabel, OrganizationJAXBSchema.DISPLAY_NAME,
107                                                 displayName);
108         }
109     }
110
111     /**
112      * Produces a default displayName from the basic name and foundingPlace fields.
113      * @see OrgAuthorityClientUtils.prepareDefaultDisplayName() which
114      * duplicates this logic, until we define a service-general utils package
115      * that is neither client nor service specific.
116      * @param shortName
117      * @param foundingPlace
118      * @return
119      * @throws Exception
120      */
121     private static String prepareDefaultDisplayName(
122                 String shortName, String foundingPlace ) throws Exception {
123         StringBuilder newStr = new StringBuilder();
124                 final String sep = " ";
125                 boolean firstAdded = false;
126                 if(null != shortName ) {
127                         newStr.append(shortName);
128                         firstAdded = true;
129                 }
130         // Now we add the place
131                 if(null != foundingPlace ) {
132                         if(firstAdded) {
133                                 newStr.append(sep);
134                         }
135                         newStr.append(foundingPlace);
136                 }
137                 return newStr.toString();
138     }
139     
140     /**
141      * getCommonPart get associated organization
142      * @return
143      */
144     @Override
145     public OrganizationsCommon getCommonPart() {
146         return organization;
147     }
148
149     /**
150      * setCommonPart set associated organization
151      * @param organization
152      */
153     @Override
154     public void setCommonPart(OrganizationsCommon organization) {
155         this.organization = organization;
156     }
157
158     /**
159      * getCommonPartList get associated organization (for index/GET_ALL)
160      * @return
161      */
162     @Override
163     public OrganizationsCommonList getCommonPartList() {
164         return organizationList;
165     }
166
167     @Override
168     public void setCommonPartList(OrganizationsCommonList organizationList) {
169         this.organizationList = organizationList;
170     }
171
172     @Override
173     public OrganizationsCommon extractCommonPart(DocumentWrapper wrapDoc)
174             throws Exception {
175         throw new UnsupportedOperationException();
176     }
177
178     @Override
179     public void fillCommonPart(OrganizationsCommon organizationObject, DocumentWrapper wrapDoc) throws Exception {
180         throw new UnsupportedOperationException();
181     }
182
183     @Override
184     public OrganizationsCommonList extractCommonPartList(DocumentWrapper wrapDoc) 
185         throws Exception {
186         OrganizationsCommonList coList = new OrganizationsCommonList();
187         try{
188                 DocumentModelList docList = (DocumentModelList) wrapDoc.getWrappedObject();
189         
190                 List<OrganizationsCommonList.OrganizationListItem> list = 
191                         coList.getOrganizationListItem();
192         
193                 DocumentFilter filter = getDocumentFilter();
194                 long pageNum, pageSize;
195                 if(filter==null) {
196                         pageNum = 0;
197                         pageSize = 0;
198                 } else {
199                         pageSize = filter.getPageSize();
200                         pageNum = filter.getOffset()/pageSize;
201                 }
202                 coList.setPageNum(pageNum);
203                 coList.setPageSize(pageSize);
204                 coList.setTotalItems(docList.totalSize());
205                 //FIXME: iterating over a long list of documents is not a long term
206                 //strategy...need to change to more efficient iterating in future
207                 Iterator<DocumentModel> iter = docList.iterator();
208                 String commonPartLabel = getServiceContext().getCommonPartLabel("organizations");
209                 while(iter.hasNext()){
210                     DocumentModel docModel = iter.next();
211                     OrganizationListItem ilistItem = new OrganizationListItem();
212                     ilistItem.setDisplayName((String)
213                                 docModel.getProperty(commonPartLabel,OrganizationJAXBSchema.DISPLAY_NAME ));
214                                 ilistItem.setRefName((String) 
215                                                 docModel.getProperty(commonPartLabel, OrganizationJAXBSchema.REF_NAME));
216                                 String id = NuxeoUtils.extractId(docModel.getPathAsString());
217                     ilistItem.setUri("/orgauthorities/"+inAuthority+"/items/" + id);
218                     ilistItem.setCsid(id);
219                     list.add(ilistItem);
220                 }
221         }catch(Exception e){
222             if(logger.isDebugEnabled()){
223                 logger.debug("Caught exception in extractCommonPartList", e);
224             }
225             throw e;
226         }
227         return coList;
228     }
229
230     /**
231      * getQProperty converts the given property to qualified schema property
232      * @param prop
233      * @return
234      */
235     @Override
236     public String getQProperty(String prop) {
237         return OrganizationConstants.NUXEO_SCHEMA_NAME + ":" + prop;
238     }
239 }
240