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.common.service.ServiceBindingType;
58 import org.collectionspace.services.common.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 int pageSize = myFilter.getPageSize();
109 int pageNum = myFilter.getStartPage();
110 final boolean computeTotal = true;
111 list.setPageNum(pageSize);
112 list.setPageSize(pageNum);
115 Map<String, ServiceBindingType> queriedServiceBindings = new HashMap<String, ServiceBindingType>();
116 RepositoryJavaClientImpl nuxeoRepoClient = (RepositoryJavaClientImpl)repoClient;
117 // Get the service bindings for this tenant
118 TenantBindingConfigReaderImpl tReader =
119 ServiceMain.getInstance().getTenantBindingConfigReader();
120 // We need to get all the procedures, authorities, and objects.
121 List<ServiceBindingType> servicebindings =
122 tReader.getServiceBindingsByType(ctx.getTenantId(), serviceGroupNames);
123 if (servicebindings == null || servicebindings.isEmpty()) {
124 Response response = Response.status(Response.Status.NOT_FOUND).entity(
125 ServiceMessages.READ_FAILED +
126 ServiceMessages.resourceNotFoundMsg(implode(serviceGroupNames, ","))).type("text/plain").build();
127 throw new WebApplicationException(response);
129 servicebindings = SecurityUtils.getReadableServiceBindingsForCurrentUser(servicebindings);
130 // Build the list of docTypes for allowed serviceBindings
131 ArrayList<String> docTypes = new ArrayList<String>();
132 for(ServiceBindingType binding:servicebindings) {
133 ServiceObjectType serviceObj = binding.getObject();
134 if(serviceObj!=null) {
135 String docType = serviceObj.getName();
136 docTypes.add(docType);
137 queriedServiceBindings.put(docType, binding);
141 // Now we have to issue the search
142 // findDocs qill build a QueryContext, which wants to see a docType for our context
143 ctx.setDocumentType("Document");
144 DocumentWrapper<DocumentModelList> docListWrapper = nuxeoRepoClient.findDocs(ctx, repoSession,
145 docTypes, whereClause, pageSize, pageNum, computeTotal);
146 // Now we gather the info for each document into the list and return
147 DocumentModelList docList = docListWrapper.getWrappedObject();
149 if (docList == null) { // found no authRef fields - nothing to process
152 processDocList(docList, queriedServiceBindings, commonList);
153 list.setItemsInPage(docList.size());
154 list.setTotalItems(docList.totalSize());
155 } catch (DocumentException de) {
157 } catch (Exception e) {
158 if (logger.isDebugEnabled()) {
159 logger.debug("Caught exception ", e);
161 throw new DocumentException(e);
163 if (releaseRepoSession && repoSession != null) {
164 repoClient.releaseRepositorySession(repoSession);
167 } catch (Exception e) {
168 if (logger.isDebugEnabled()) {
169 logger.debug("Caught exception ", e);
171 throw new DocumentException(e);
176 // Move this to a Utils class!
177 public static String implode(List<String> stringList, String sep) {
178 StringBuilder sb = new StringBuilder();
179 boolean fFirst = false;
180 for(String name:stringList) {
188 return sb.toString();
191 private String getUriFromServiceBinding(ServiceBindingType sb, String csid) {
192 return "/" + sb.getName().toLowerCase() + "/" + csid;
195 private void processDocList(
196 DocumentModelList docList,
197 Map<String, ServiceBindingType> queriedServiceBindings,
199 int nFields = NUM_META_FIELDS+NUM_STANDARD_LIST_RESULT_FIELDS;
200 String fields[] = new String[nFields];
203 fields[2] = "updatedAt";
204 fields[3] = DOC_NAME_FIELD;
205 fields[4] = DOC_NUMBER_FIELD;
206 fields[5] = DOC_TYPE_FIELD;
207 list.setFieldsReturned(fields);
208 Iterator<DocumentModel> iter = docList.iterator();
209 HashMap<String,String> item = new HashMap<String,String>();
210 while (iter.hasNext()) {
211 DocumentModel docModel = iter.next();
212 String docType = docModel.getDocumentType().getName();
213 ServiceBindingType sb = queriedServiceBindings.get(docType);
215 throw new RuntimeException(
216 "processDocList: No Service Binding for docType: " + docType);
218 String csid = NuxeoUtils.getCsid(docModel);
219 item.put(STANDARD_LIST_CSID_FIELD, csid);
220 // Need to get the URI for the document, by it's type.
221 item.put(STANDARD_LIST_URI_FIELD, getUriFromServiceBinding(sb, csid));
223 item.put(STANDARD_LIST_UPDATED_AT_FIELD, getUpdatedAtAsString(docModel));
224 } catch(Exception e) {
225 logger.error("Error getting udpatedAt value for doc ["+csid+"]: "+e.getLocalizedMessage());
228 String value = ServiceBindingUtils.getMappedFieldInDoc(sb,
229 ServiceBindingUtils.OBJ_NUMBER_PROP, docModel);
230 item.put(DOC_NUMBER_FIELD, value);
231 value = ServiceBindingUtils.getMappedFieldInDoc(sb,
232 ServiceBindingUtils.OBJ_NAME_PROP, docModel);
233 item.put(DOC_NAME_FIELD, value);
234 item.put(DOC_TYPE_FIELD, docType);