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.servicegroup.nuxeo;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.List;
32 import javax.ws.rs.WebApplicationException;
33 import javax.ws.rs.core.Response;
35 import org.collectionspace.services.ServiceGroupListItemJAXBSchema;
36 import org.collectionspace.services.nuxeo.client.java.CommonList;
37 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
38 import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClientImpl;
39 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
40 import org.collectionspace.services.jaxb.AbstractCommonList;
41 import org.collectionspace.services.client.IQueryManager;
42 import org.collectionspace.services.client.PoxPayloadIn;
43 import org.collectionspace.services.client.PoxPayloadOut;
44 import org.collectionspace.services.common.ServiceMain;
45 import org.collectionspace.services.common.ServiceMessages;
46 import org.collectionspace.services.common.api.Tools;
47 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
48 import org.collectionspace.services.common.context.ServiceBindingUtils;
49 import org.collectionspace.services.common.context.ServiceContext;
50 import org.collectionspace.services.common.document.DocumentException;
51 import org.collectionspace.services.common.document.DocumentFilter;
52 import org.collectionspace.services.common.document.DocumentNotFoundException;
53 import org.collectionspace.services.common.document.DocumentWrapper;
54 import org.collectionspace.services.common.query.QueryManager;
55 import org.collectionspace.services.common.repository.RepositoryClient;
56 import org.collectionspace.services.common.security.SecurityUtils;
57 import org.collectionspace.services.config.service.ServiceBindingType;
58 import org.collectionspace.services.config.service.ServiceObjectType;
59 import org.collectionspace.services.servicegroup.ServicegroupsCommon;
60 import org.nuxeo.ecm.core.api.DocumentModel;
61 import org.nuxeo.ecm.core.api.DocumentModelList;
62 import org.nuxeo.ecm.core.api.model.PropertyException;
63 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
67 public class ServiceGroupDocumentModelHandler
68 extends DocHandlerBase<ServicegroupsCommon> {
70 protected final Logger logger = LoggerFactory.getLogger(this.getClass());
72 protected static final int NUM_META_FIELDS = 3;
73 protected static final String DOC_TYPE_FIELD = "docType";
74 protected static final String DOC_NUMBER_FIELD = "docNumber";
75 protected static final String DOC_NAME_FIELD = "docName";
77 public AbstractCommonList getItemsForGroup(
78 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
79 List<String> serviceGroupNames) throws Exception {
80 CommonList commonList = new CommonList();
81 AbstractCommonList list = (AbstractCommonList)commonList;
82 RepositoryInstance repoSession = null;
83 boolean releaseRepoSession = false;
86 RepositoryJavaClientImpl repoClient = (RepositoryJavaClientImpl)this.getRepositoryClient(ctx);
87 repoSession = this.getRepositorySession();
88 if (repoSession == null) {
89 repoSession = repoClient.getRepositorySession(ctx);
90 releaseRepoSession = true;
92 DocumentFilter myFilter = getDocumentFilter();
93 // Make sure we pick up workflow state, etc.
94 int pageSize = myFilter.getPageSize();
95 int pageNum = myFilter.getStartPage();
96 list.setPageNum(pageNum);
97 list.setPageSize(pageSize);
100 Map<String, ServiceBindingType> queriedServiceBindings = new HashMap<String, ServiceBindingType>();
101 RepositoryJavaClientImpl nuxeoRepoClient = (RepositoryJavaClientImpl)repoClient;
102 // Get the service bindings for this tenant
103 TenantBindingConfigReaderImpl tReader =
104 ServiceMain.getInstance().getTenantBindingConfigReader();
105 // We need to get all the procedures, authorities, and objects.
106 List<ServiceBindingType> servicebindings =
107 tReader.getServiceBindingsByType(ctx.getTenantId(), serviceGroupNames);
108 if (servicebindings == null || servicebindings.isEmpty()) {
109 Response response = Response.status(Response.Status.NOT_FOUND).entity(
110 ServiceMessages.READ_FAILED +
111 ServiceMessages.resourceNotFoundMsg(implode(serviceGroupNames, ","))).type("text/plain").build();
112 throw new WebApplicationException(response);
114 servicebindings = SecurityUtils.getReadableServiceBindingsForCurrentUser(servicebindings);
115 // Build the list of docTypes for allowed serviceBindings
116 ArrayList<String> docTypes = new ArrayList<String>();
117 for(ServiceBindingType binding:servicebindings) {
118 ServiceObjectType serviceObj = binding.getObject();
119 if(serviceObj!=null) {
120 String docType = serviceObj.getName();
121 docTypes.add(docType);
122 queriedServiceBindings.put(docType, binding);
126 // This should be "Document" but CMIS is gagging on that right now.
127 ctx.getQueryParams().add(IQueryManager.SELECT_DOC_TYPE_FIELD, "cmis:document");
129 // Now we have to issue the search
130 // findDocs qill build a QueryContext, which wants to see a docType for our context
131 ctx.setDocumentType("Document");
132 DocumentWrapper<DocumentModelList> docListWrapper =
133 nuxeoRepoClient.findDocs(ctx, this, repoSession, docTypes );
134 // Now we gather the info for each document into the list and return
135 DocumentModelList docList = docListWrapper.getWrappedObject();
137 if (docList == null) { // found no authRef fields - nothing to process
140 processDocList(docList, queriedServiceBindings, commonList);
141 list.setItemsInPage(docList.size());
142 list.setTotalItems(docList.totalSize());
143 } catch (DocumentException de) {
145 } catch (Exception e) {
146 if (logger.isDebugEnabled()) {
147 logger.debug("Caught exception ", e);
149 throw new DocumentException(e);
151 if (releaseRepoSession && repoSession != null) {
152 repoClient.releaseRepositorySession(ctx, repoSession);
155 } catch (Exception e) {
156 if (logger.isDebugEnabled()) {
157 logger.debug("Caught exception ", e);
159 throw new DocumentException(e);
164 // Move this to a Utils class!
165 public static String implode(List<String> stringList, String sep) {
166 StringBuilder sb = new StringBuilder();
167 boolean fFirst = false;
168 for(String name:stringList) {
176 return sb.toString();
179 private String getUriFromServiceBinding(ServiceBindingType sb, String csid) {
180 return "/" + sb.getName().toLowerCase() + "/" + csid;
183 private void processDocList(
184 DocumentModelList docList,
185 Map<String, ServiceBindingType> queriedServiceBindings,
187 int nFields = NUM_META_FIELDS+NUM_STANDARD_LIST_RESULT_FIELDS;
188 String fields[] = new String[nFields];
189 fields[0] = STANDARD_LIST_CSID_FIELD;
190 fields[1] = STANDARD_LIST_URI_FIELD;
191 fields[2] = STANDARD_LIST_UPDATED_AT_FIELD;
192 fields[3] = STANDARD_LIST_WORKFLOW_FIELD;
193 fields[4] = DOC_NAME_FIELD;
194 fields[5] = DOC_NUMBER_FIELD;
195 fields[6] = DOC_TYPE_FIELD;
196 list.setFieldsReturned(fields);
197 Iterator<DocumentModel> iter = docList.iterator();
198 HashMap<String, Object> item = new HashMap<String, Object>();
199 while (iter.hasNext()) {
200 DocumentModel docModel = iter.next();
201 String docType = docModel.getDocumentType().getName();
202 docType = ServiceBindingUtils.getUnqualifiedTenantDocType(docType);
203 ServiceBindingType sb = queriedServiceBindings.get(docType);
205 throw new RuntimeException(
206 "processDocList: No Service Binding for docType: " + docType);
208 String csid = NuxeoUtils.getCsid(docModel);
209 item.put(STANDARD_LIST_CSID_FIELD, csid);
210 // Need to get the URI for the document, by it's type.
211 item.put(STANDARD_LIST_URI_FIELD, getUriFromServiceBinding(sb, csid));
213 item.put(STANDARD_LIST_UPDATED_AT_FIELD, getUpdatedAtAsString(docModel));
214 item.put(STANDARD_LIST_WORKFLOW_FIELD, docModel.getCurrentLifeCycleState());
215 } catch(Exception e) {
216 logger.error("Error getting core values for doc ["+csid+"]: "+e.getLocalizedMessage());
219 String value = ServiceBindingUtils.getMappedFieldInDoc(sb,
220 ServiceBindingUtils.OBJ_NUMBER_PROP, docModel);
221 item.put(DOC_NUMBER_FIELD, value);
222 value = ServiceBindingUtils.getMappedFieldInDoc(sb,
223 ServiceBindingUtils.OBJ_NAME_PROP, docModel);
224 item.put(DOC_NAME_FIELD, value);
225 item.put(DOC_TYPE_FIELD, docType);