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.RepositoryInstanceInterface;
39 import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClientImpl;
40 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
41 import org.collectionspace.services.jaxb.AbstractCommonList;
42 import org.collectionspace.services.client.IQueryManager;
43 import org.collectionspace.services.client.PoxPayloadIn;
44 import org.collectionspace.services.client.PoxPayloadOut;
45 import org.collectionspace.services.common.CSWebApplicationException;
46 import org.collectionspace.services.common.ServiceMain;
47 import org.collectionspace.services.common.ServiceMessages;
48 import org.collectionspace.services.common.StoredValuesUriTemplate;
49 import org.collectionspace.services.common.UriTemplateFactory;
50 import org.collectionspace.services.common.UriTemplateRegistry;
51 import org.collectionspace.services.common.UriTemplateRegistryKey;
52 import org.collectionspace.services.common.api.Tools;
53 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
54 import org.collectionspace.services.common.context.ServiceBindingUtils;
55 import org.collectionspace.services.common.context.ServiceContext;
56 import org.collectionspace.services.common.document.DocumentException;
57 import org.collectionspace.services.common.document.DocumentFilter;
58 import org.collectionspace.services.common.document.DocumentNotFoundException;
59 import org.collectionspace.services.common.document.DocumentWrapper;
60 import org.collectionspace.services.common.query.QueryManager;
61 import org.collectionspace.services.common.repository.RepositoryClient;
62 import org.collectionspace.services.common.security.SecurityUtils;
63 import org.collectionspace.services.config.service.ServiceBindingType;
64 import org.collectionspace.services.config.service.ServiceObjectType;
65 import org.collectionspace.services.servicegroup.ServicegroupsCommon;
66 import org.nuxeo.ecm.core.api.DocumentModel;
67 import org.nuxeo.ecm.core.api.DocumentModelList;
68 import org.nuxeo.ecm.core.api.model.PropertyException;
69 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
73 public class ServiceGroupDocumentModelHandler
74 extends DocHandlerBase<ServicegroupsCommon> {
76 protected final Logger logger = LoggerFactory.getLogger(this.getClass());
78 protected static final int NUM_META_FIELDS = 3;
79 protected static final String DOC_TYPE_FIELD = "docType";
80 protected static final String DOC_NUMBER_FIELD = "docNumber";
81 protected static final String DOC_NAME_FIELD = "docName";
83 public AbstractCommonList getItemsForGroup(
84 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
85 List<String> serviceGroupNames) throws Exception {
86 CommonList commonList = new CommonList();
87 AbstractCommonList list = (AbstractCommonList)commonList;
88 RepositoryInstanceInterface repoSession = null;
89 boolean releaseRepoSession = false;
92 RepositoryJavaClientImpl repoClient = (RepositoryJavaClientImpl)this.getRepositoryClient(ctx);
93 repoSession = this.getRepositorySession();
94 if (repoSession == null) {
95 repoSession = repoClient.getRepositorySession(ctx);
96 releaseRepoSession = true;
98 DocumentFilter myFilter = getDocumentFilter();
99 // Make sure we pick up workflow state, etc.
100 int pageSize = myFilter.getPageSize();
101 int pageNum = myFilter.getStartPage();
102 list.setPageNum(pageNum);
103 list.setPageSize(pageSize);
106 Map<String, ServiceBindingType> queriedServiceBindings = new HashMap<String, ServiceBindingType>();
107 RepositoryJavaClientImpl nuxeoRepoClient = (RepositoryJavaClientImpl)repoClient;
108 // Get the service bindings for this tenant
109 TenantBindingConfigReaderImpl tReader =
110 ServiceMain.getInstance().getTenantBindingConfigReader();
111 // We need to get all the procedures, authorities, and objects.
112 List<ServiceBindingType> servicebindings =
113 tReader.getServiceBindingsByType(ctx.getTenantId(), serviceGroupNames);
114 if (servicebindings == null || servicebindings.isEmpty()) {
115 Response response = Response.status(Response.Status.NOT_FOUND).entity(
116 ServiceMessages.READ_FAILED +
117 ServiceMessages.resourceNotFoundMsg(implode(serviceGroupNames, ","))).type("text/plain").build();
118 throw new CSWebApplicationException(response);
120 servicebindings = SecurityUtils.getReadableServiceBindingsForCurrentUser(servicebindings);
121 // Build the list of docTypes for allowed serviceBindings
122 ArrayList<String> docTypes = new ArrayList<String>();
123 for(ServiceBindingType binding:servicebindings) {
124 ServiceObjectType serviceObj = binding.getObject();
125 if(serviceObj!=null) {
126 String docType = serviceObj.getName();
127 docTypes.add(docType);
128 queriedServiceBindings.put(docType, binding);
132 // This should be "Document" but CMIS is gagging on that right now.
133 ctx.getQueryParams().add(IQueryManager.SELECT_DOC_TYPE_FIELD, "CollectionSpaceDocument");
135 // Now we have to issue the search
136 // findDocs qill build a QueryContext, which wants to see a docType for our context
137 ctx.setDocumentType("Document");
138 DocumentWrapper<DocumentModelList> docListWrapper =
139 nuxeoRepoClient.findDocs(ctx, this, repoSession, docTypes );
140 // Now we gather the info for each document into the list and return
141 DocumentModelList docList = docListWrapper.getWrappedObject();
143 if (docList == null) { // found no authRef fields - nothing to process
146 processDocList(ctx.getTenantId(), docList, queriedServiceBindings, commonList);
147 list.setItemsInPage(docList.size());
148 list.setTotalItems(docList.totalSize());
149 } catch (DocumentException de) {
151 } catch (Exception e) {
152 if (logger.isDebugEnabled()) {
153 logger.debug("Caught exception ", e);
155 throw new DocumentException(e);
157 if (releaseRepoSession && repoSession != null) {
158 repoClient.releaseRepositorySession(ctx, repoSession);
161 } catch (Exception e) {
162 if (logger.isDebugEnabled()) {
163 logger.debug("Caught exception ", e);
165 throw new DocumentException(e);
171 // Move this to a Utils class!
172 public static String implode(List<String> stringList, String sep) {
173 StringBuilder sb = new StringBuilder();
174 boolean fFirst = false;
175 for(String name:stringList) {
183 return sb.toString();
186 private String getUriFromServiceBinding(ServiceBindingType sb, String csid) {
187 return "/" + sb.getName().toLowerCase() + "/" + csid;
190 private void processDocList(
192 DocumentModelList docList,
193 Map<String, ServiceBindingType> queriedServiceBindings,
195 int nFields = NUM_META_FIELDS+NUM_STANDARD_LIST_RESULT_FIELDS;
196 String fields[] = new String[nFields];
197 fields[0] = STANDARD_LIST_CSID_FIELD;
198 fields[1] = STANDARD_LIST_URI_FIELD;
199 fields[2] = STANDARD_LIST_UPDATED_AT_FIELD;
200 fields[3] = STANDARD_LIST_WORKFLOW_FIELD;
201 fields[4] = STANDARD_LIST_REFNAME_FIELD;
202 fields[5] = DOC_NAME_FIELD;
203 fields[6] = DOC_NUMBER_FIELD;
204 fields[7] = DOC_TYPE_FIELD;
205 list.setFieldsReturned(fields);
206 Iterator<DocumentModel> iter = docList.iterator();
207 HashMap<String, Object> item = new HashMap<String, Object>();
208 while (iter.hasNext()) {
209 DocumentModel docModel = iter.next();
210 String docType = docModel.getDocumentType().getName();
211 docType = ServiceBindingUtils.getUnqualifiedTenantDocType(docType);
212 ServiceBindingType sb = queriedServiceBindings.get(docType);
214 throw new RuntimeException(
215 "processDocList: No Service Binding for docType: " + docType);
217 String csid = NuxeoUtils.getCsid(docModel);
218 item.put(STANDARD_LIST_CSID_FIELD, csid);
220 UriTemplateRegistry uriTemplateRegistry = ServiceMain.getInstance().getUriTemplateRegistry();
221 StoredValuesUriTemplate storedValuesResourceTemplate = uriTemplateRegistry.get(new UriTemplateRegistryKey(tenantId, docType));
222 Map<String, String> additionalValues = new HashMap<String, String>();
223 if (storedValuesResourceTemplate.getUriTemplateType() == UriTemplateFactory.ITEM) {
225 String inAuthorityCsid = (String) docModel.getPropertyValue("inAuthority"); // AuthorityItemJAXBSchema.IN_AUTHORITY
226 additionalValues.put(UriTemplateFactory.IDENTIFIER_VAR, inAuthorityCsid);
227 additionalValues.put(UriTemplateFactory.ITEM_IDENTIFIER_VAR, csid);
228 } catch (Exception e) {
229 logger.warn("Could not extract inAuthority property from authority item record: " + e.getMessage());
232 additionalValues.put(UriTemplateFactory.IDENTIFIER_VAR, csid);
234 String uriStr = storedValuesResourceTemplate.buildUri(additionalValues);
235 item.put(STANDARD_LIST_URI_FIELD, uriStr);
238 item.put(STANDARD_LIST_UPDATED_AT_FIELD, getUpdatedAtAsString(docModel));
239 item.put(STANDARD_LIST_WORKFLOW_FIELD, docModel.getCurrentLifeCycleState());
240 item.put(STANDARD_LIST_REFNAME_FIELD, getRefname(docModel));
241 } catch(Exception e) {
242 logger.error("Error getting core values for doc ["+csid+"]: "+e.getLocalizedMessage());
245 String value = ServiceBindingUtils.getMappedFieldInDoc(sb,
246 ServiceBindingUtils.OBJ_NUMBER_PROP, docModel);
247 item.put(DOC_NUMBER_FIELD, value);
248 value = ServiceBindingUtils.getMappedFieldInDoc(sb,
249 ServiceBindingUtils.OBJ_NAME_PROP, docModel);
250 item.put(DOC_NAME_FIELD, value);
251 item.put(DOC_TYPE_FIELD, docType);