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