]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
dfebf095bb14f287a188e0ca4b1c4a0c0ef57e42
[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         @Override
76     public void prepare(Action action) throws Exception {
77         //no specific action needed
78     }
79
80     /* Override handleGet so we can deal with defaulting the displayName
81      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
82      */
83     @Override
84     public void handleGet(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
85         DocumentModel docModel = wrapDoc.getWrappedObject();
86         String displayName = (String) docModel.getProperty(getServiceContext().getCommonPartLabel("organizations"),
87                         OrganizationJAXBSchema.DISPLAY_NAME);
88         if(displayName == null) {
89                 displayName = (String) docModel.getProperty(getServiceContext().getCommonPartLabel("organizations"),
90                                 OrganizationJAXBSchema.SHORT_NAME);
91                 docModel.setProperty(getServiceContext().getCommonPartLabel("organizations"),
92                                 OrganizationJAXBSchema.DISPLAY_NAME, displayName);
93         }
94         super.handleGet(wrapDoc);
95     }
96
97     /**
98      * getCommonPart get associated organization
99      * @return
100      */
101     @Override
102     public OrganizationsCommon getCommonPart() {
103         return organization;
104     }
105
106     /**
107      * setCommonPart set associated organization
108      * @param organization
109      */
110     @Override
111     public void setCommonPart(OrganizationsCommon organization) {
112         this.organization = organization;
113     }
114
115     /**
116      * getCommonPartList get associated organization (for index/GET_ALL)
117      * @return
118      */
119     @Override
120     public OrganizationsCommonList getCommonPartList() {
121         return organizationList;
122     }
123
124     @Override
125     public void setCommonPartList(OrganizationsCommonList organizationList) {
126         this.organizationList = organizationList;
127     }
128
129     @Override
130     public OrganizationsCommon extractCommonPart(DocumentWrapper wrapDoc)
131             throws Exception {
132         throw new UnsupportedOperationException();
133     }
134
135     @Override
136     public void fillCommonPart(OrganizationsCommon organizationObject, DocumentWrapper wrapDoc) throws Exception {
137         throw new UnsupportedOperationException();
138     }
139
140     @Override
141     public OrganizationsCommonList extractCommonPartList(DocumentWrapper wrapDoc) 
142         throws Exception {
143         OrganizationsCommonList coList = new OrganizationsCommonList();
144         try{
145                 DocumentModelList docList = (DocumentModelList) wrapDoc.getWrappedObject();
146         
147                 List<OrganizationsCommonList.OrganizationListItem> list = 
148                         coList.getOrganizationListItem();
149         
150                 //FIXME: iterating over a long list of documents is not a long term
151                 //strategy...need to change to more efficient iterating in future
152                 Iterator<DocumentModel> iter = docList.iterator();
153                 while(iter.hasNext()){
154                     DocumentModel docModel = iter.next();
155                     OrganizationListItem ilistItem = new OrganizationListItem();
156                     // We look for a set display name, and fall back to teh short name if there is none
157                     String displayName = (String) docModel.getProperty(getServiceContext().getCommonPartLabel("organizations"),
158                                                                                                 OrganizationJAXBSchema.DISPLAY_NAME);
159                     if(displayName == null)
160                             displayName = (String) docModel.getProperty(getServiceContext().getCommonPartLabel("organizations"),
161                                                         OrganizationJAXBSchema.SHORT_NAME);
162                     ilistItem.setDisplayName( displayName );
163                     ilistItem.setRefName(
164                                                                         (String) docModel.getProperty(getServiceContext().getCommonPartLabel("organizations"),
165                                                                         OrganizationJAXBSchema.REF_NAME));
166                                                         /*
167                                                          * These are not currently included in the listing - only in the details
168                     ilistItem.setLongName(
169                                                                         (String) docModel.getProperty(getServiceContext().getCommonPartLabel("organizations"),
170                                                                         OrganizationJAXBSchema.LONG_NAME));
171                     ilistItem.setDescription(
172                                                                         (String) docModel.getProperty(getServiceContext().getCommonPartLabel("organizations"),
173                                                                         OrganizationJAXBSchema.DESCRIPTION));
174                                                          */
175                                                         String id = NuxeoUtils.extractId(docModel.getPathAsString());
176                     ilistItem.setUri("/orgauthorities/"+inAuthority+"/items/" + id);
177                     ilistItem.setCsid(id);
178                     list.add(ilistItem);
179                 }
180         }catch(Exception e){
181             if(logger.isDebugEnabled()){
182                 logger.debug("Caught exception in extractCommonPartList", e);
183             }
184             throw e;
185         }
186         return coList;
187     }
188
189     /**
190      * getQProperty converts the given property to qualified schema property
191      * @param prop
192      * @return
193      */
194     @Override
195     public String getQProperty(String prop) {
196         return OrganizationConstants.NUXEO_SCHEMA_NAME + ":" + prop;
197     }
198 }
199