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