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;
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;
46 * OrganizationDocumentModelHandler
48 * $LastChangedRevision: $
51 public class OrganizationDocumentModelHandler
52 extends RemoteDocumentModelHandlerImpl<OrganizationsCommon, OrganizationsCommonList> {
54 private final Logger logger = LoggerFactory.getLogger(OrganizationDocumentModelHandler.class);
56 * Common part schema label
58 private static final String COMMON_PART_LABEL = "organizations_common";
60 * organization is used to stash JAXB object to use when handle is called
61 * for Action.CREATE, Action.UPDATE or Action.GET
63 private OrganizationsCommon organization;
65 * organizationList is stashed when handle is called
68 private OrganizationsCommonList organizationList;
71 * inAuthority is the parent OrgAuthority for this context
73 private String inAuthority;
75 public String getInAuthority() {
79 public void setInAuthority(String inAuthority) {
80 this.inAuthority = inAuthority;
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());
92 public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
93 super.handleUpdate(wrapDoc);
94 handleDisplayName(wrapDoc.getWrappedObject());
98 * Check the logic around the computed displayName
102 * @throws Exception the exception
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,
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.
123 * @param foundingPlace
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);
136 // Now we add the place
137 if(null != foundingPlace ) {
141 newStr.append(foundingPlace);
143 return newStr.toString();
147 * getCommonPart get associated organization
151 public OrganizationsCommon getCommonPart() {
156 * setCommonPart set associated organization
157 * @param organization
160 public void setCommonPart(OrganizationsCommon organization) {
161 this.organization = organization;
165 * getCommonPartList get associated organization (for index/GET_ALL)
169 public OrganizationsCommonList getCommonPartList() {
170 return organizationList;
174 public void setCommonPartList(OrganizationsCommonList organizationList) {
175 this.organizationList = organizationList;
179 protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
181 Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
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);
189 return unQObjectProperties;
193 public OrganizationsCommon extractCommonPart(DocumentWrapper wrapDoc)
195 throw new UnsupportedOperationException();
199 public void fillCommonPart(OrganizationsCommon organizationObject, DocumentWrapper wrapDoc) throws Exception {
200 throw new UnsupportedOperationException();
204 public OrganizationsCommonList extractCommonPartList(DocumentWrapper wrapDoc)
206 OrganizationsCommonList coList = new OrganizationsCommonList();
208 DocumentModelList docList = (DocumentModelList) wrapDoc.getWrappedObject();
210 List<OrganizationsCommonList.OrganizationListItem> list =
211 coList.getOrganizationListItem();
213 DocumentFilter filter = getDocumentFilter();
214 long pageNum, pageSize;
219 pageSize = filter.getPageSize();
220 pageNum = filter.getOffset()/pageSize;
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);
242 if(logger.isDebugEnabled()){
243 logger.debug("Caught exception in extractCommonPartList", e);
251 * getQProperty converts the given property to qualified schema property
256 public String getQProperty(String prop) {
257 return OrganizationConstants.NUXEO_SCHEMA_NAME + ":" + prop;