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