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,
80 String keywords) throws Exception {
81 CommonList commonList = new CommonList();
82 AbstractCommonList list = (AbstractCommonList)commonList;
83 RepositoryInstance repoSession = null;
84 boolean releaseRepoSession = false;
87 RepositoryJavaClientImpl repoClient = (RepositoryJavaClientImpl)this.getRepositoryClient(ctx);
88 repoSession = this.getRepositorySession();
89 if (repoSession == null) {
90 repoSession = repoClient.getRepositorySession();
91 releaseRepoSession = true;
93 DocumentFilter myFilter = getDocumentFilter();
94 String whereClause = null;
95 if (keywords != null) {
96 whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
97 if(Tools.isEmpty(whereClause)) {
98 if (logger.isDebugEnabled()) {
99 logger.debug("The WHERE clause is empty for keywords: ["+keywords+"]");
102 myFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND);
103 if (logger.isDebugEnabled()) {
104 logger.debug("The WHERE clause is: " + myFilter.getWhereClause());
108 // Make sure we pick up workflow state, etc.
109 whereClause = myFilter.getWhereClause();
110 int pageSize = myFilter.getPageSize();
111 int pageNum = myFilter.getStartPage();
112 final boolean computeTotal = true;
113 list.setPageNum(pageNum);
114 list.setPageSize(pageSize);
117 Map<String, ServiceBindingType> queriedServiceBindings = new HashMap<String, ServiceBindingType>();
118 RepositoryJavaClientImpl nuxeoRepoClient = (RepositoryJavaClientImpl)repoClient;
119 // Get the service bindings for this tenant
120 TenantBindingConfigReaderImpl tReader =
121 ServiceMain.getInstance().getTenantBindingConfigReader();
122 // We need to get all the procedures, authorities, and objects.
123 List<ServiceBindingType> servicebindings =
124 tReader.getServiceBindingsByType(ctx.getTenantId(), serviceGroupNames);
125 if (servicebindings == null || servicebindings.isEmpty()) {
126 Response response = Response.status(Response.Status.NOT_FOUND).entity(
127 ServiceMessages.READ_FAILED +
128 ServiceMessages.resourceNotFoundMsg(implode(serviceGroupNames, ","))).type("text/plain").build();
129 throw new WebApplicationException(response);
131 servicebindings = SecurityUtils.getReadableServiceBindingsForCurrentUser(servicebindings);
132 // Build the list of docTypes for allowed serviceBindings
133 ArrayList<String> docTypes = new ArrayList<String>();
134 for(ServiceBindingType binding:servicebindings) {
135 ServiceObjectType serviceObj = binding.getObject();
136 if(serviceObj!=null) {
137 String docType = serviceObj.getName();
138 docTypes.add(docType);
139 queriedServiceBindings.put(docType, binding);
143 // Now we have to issue the search
144 // findDocs qill build a QueryContext, which wants to see a docType for our context
145 ctx.setDocumentType("Document");
146 DocumentWrapper<DocumentModelList> docListWrapper = nuxeoRepoClient.findDocs(ctx, repoSession,
147 docTypes, whereClause, pageSize, pageNum, computeTotal);
148 // Now we gather the info for each document into the list and return
149 DocumentModelList docList = docListWrapper.getWrappedObject();
151 if (docList == null) { // found no authRef fields - nothing to process
154 processDocList(docList, queriedServiceBindings, commonList);
155 list.setItemsInPage(docList.size());
156 list.setTotalItems(docList.totalSize());
157 } catch (DocumentException de) {
159 } catch (Exception e) {
160 if (logger.isDebugEnabled()) {
161 logger.debug("Caught exception ", e);
163 throw new DocumentException(e);
165 if (releaseRepoSession && repoSession != null) {
166 repoClient.releaseRepositorySession(repoSession);
169 } catch (Exception e) {
170 if (logger.isDebugEnabled()) {
171 logger.debug("Caught exception ", e);
173 throw new DocumentException(e);
178 // Move this to a Utils class!
179 public static String implode(List<String> stringList, String sep) {
180 StringBuilder sb = new StringBuilder();
181 boolean fFirst = false;
182 for(String name:stringList) {
190 return sb.toString();
193 private String getUriFromServiceBinding(ServiceBindingType sb, String csid) {
194 return "/" + sb.getName().toLowerCase() + "/" + csid;
197 private void processDocList(
198 DocumentModelList docList,
199 Map<String, ServiceBindingType> queriedServiceBindings,
201 int nFields = NUM_META_FIELDS+NUM_STANDARD_LIST_RESULT_FIELDS;
202 String fields[] = new String[nFields];
203 fields[0] = STANDARD_LIST_CSID_FIELD;
204 fields[1] = STANDARD_LIST_URI_FIELD;
205 fields[2] = STANDARD_LIST_UPDATED_AT_FIELD;
206 fields[3] = STANDARD_LIST_WORKFLOW_FIELD;
207 fields[4] = DOC_NAME_FIELD;
208 fields[5] = DOC_NUMBER_FIELD;
209 fields[6] = DOC_TYPE_FIELD;
210 list.setFieldsReturned(fields);
211 Iterator<DocumentModel> iter = docList.iterator();
212 HashMap<String, Object> item = new HashMap<String, Object>();
213 while (iter.hasNext()) {
214 DocumentModel docModel = iter.next();
215 String docType = docModel.getDocumentType().getName();
216 docType = ServiceBindingUtils.getUnqualifiedTenantDocType(docType);
217 ServiceBindingType sb = queriedServiceBindings.get(docType);
219 throw new RuntimeException(
220 "processDocList: No Service Binding for docType: " + docType);
222 String csid = NuxeoUtils.getCsid(docModel);
223 item.put(STANDARD_LIST_CSID_FIELD, csid);
224 // Need to get the URI for the document, by it's type.
225 item.put(STANDARD_LIST_URI_FIELD, getUriFromServiceBinding(sb, csid));
227 item.put(STANDARD_LIST_UPDATED_AT_FIELD, getUpdatedAtAsString(docModel));
228 item.put(STANDARD_LIST_WORKFLOW_FIELD, docModel.getCurrentLifeCycleState());
229 } catch(Exception e) {
230 logger.error("Error getting core values for doc ["+csid+"]: "+e.getLocalizedMessage());
233 String value = ServiceBindingUtils.getMappedFieldInDoc(sb,
234 ServiceBindingUtils.OBJ_NUMBER_PROP, docModel);
235 item.put(DOC_NUMBER_FIELD, value);
236 value = ServiceBindingUtils.getMappedFieldInDoc(sb,
237 ServiceBindingUtils.OBJ_NAME_PROP, docModel);
238 item.put(DOC_NAME_FIELD, value);
239 item.put(DOC_TYPE_FIELD, docType);