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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.organization.nuxeo;
26 import java.util.Iterator;
27 import java.util.List;
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;
44 * OrganizationDocumentModelHandler
46 * $LastChangedRevision: $
49 public class OrganizationDocumentModelHandler
50 extends RemoteDocumentModelHandlerImpl<OrganizationsCommon, OrganizationsCommonList> {
52 private final Logger logger = LoggerFactory.getLogger(OrganizationDocumentModelHandler.class);
54 * organization is used to stash JAXB object to use when handle is called
55 * for Action.CREATE, Action.UPDATE or Action.GET
57 private OrganizationsCommon organization;
59 * organizationList is stashed when handle is called
62 private OrganizationsCommonList organizationList;
65 * inAuthority is the parent OrgAuthority for this context
67 private String inAuthority;
69 public String getInAuthority() {
73 public void setInAuthority(String inAuthority) {
74 this.inAuthority = inAuthority;
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());
86 public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
87 super.handleUpdate(wrapDoc);
88 handleDisplayName(wrapDoc.getWrappedObject());
92 * Check the logic around the computed displayName
96 * @throws Exception the exception
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,
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.
117 * @param foundingPlace
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);
130 // Now we add the place
131 if(null != foundingPlace ) {
135 newStr.append(foundingPlace);
137 return newStr.toString();
141 * getCommonPart get associated organization
145 public OrganizationsCommon getCommonPart() {
150 * setCommonPart set associated organization
151 * @param organization
154 public void setCommonPart(OrganizationsCommon organization) {
155 this.organization = organization;
159 * getCommonPartList get associated organization (for index/GET_ALL)
163 public OrganizationsCommonList getCommonPartList() {
164 return organizationList;
168 public void setCommonPartList(OrganizationsCommonList organizationList) {
169 this.organizationList = organizationList;
173 public OrganizationsCommon extractCommonPart(DocumentWrapper wrapDoc)
175 throw new UnsupportedOperationException();
179 public void fillCommonPart(OrganizationsCommon organizationObject, DocumentWrapper wrapDoc) throws Exception {
180 throw new UnsupportedOperationException();
184 public OrganizationsCommonList extractCommonPartList(DocumentWrapper wrapDoc)
186 OrganizationsCommonList coList = new OrganizationsCommonList();
188 DocumentModelList docList = (DocumentModelList) wrapDoc.getWrappedObject();
190 List<OrganizationsCommonList.OrganizationListItem> list =
191 coList.getOrganizationListItem();
193 DocumentFilter filter = getDocumentFilter();
194 long pageNum, pageSize;
199 pageSize = filter.getPageSize();
200 pageNum = filter.getOffset()/pageSize;
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);
222 if(logger.isDebugEnabled()){
223 logger.debug("Caught exception in extractCommonPartList", e);
231 * getQProperty converts the given property to qualified schema property
236 public String getQProperty(String prop) {
237 return OrganizationConstants.NUXEO_SCHEMA_NAME + ":" + prop;