From 27897e53cb8826f4c7969a72bcb008ed4882fc96 Mon Sep 17 00:00:00 2001 From: Patrick Schmitz Date: Wed, 7 Mar 2012 16:12:54 -0800 Subject: [PATCH] CSPACE-4875 Added missing class, and tweaked utils to manage group configuration. --- .../common/context/ServiceBindingUtils.java | 17 +- .../ServiceGroupDocumentModelHandler.java | 244 ++++++++++++++++++ 2 files changed, 260 insertions(+), 1 deletion(-) create mode 100644 services/servicegroup/service/src/main/java/org/collectionspace/services/servicegroup/nuxeo/ServiceGroupDocumentModelHandler.java diff --git a/services/common/src/main/java/org/collectionspace/services/common/context/ServiceBindingUtils.java b/services/common/src/main/java/org/collectionspace/services/common/context/ServiceBindingUtils.java index bdbead40a..248ad43c8 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/context/ServiceBindingUtils.java +++ b/services/common/src/main/java/org/collectionspace/services/common/context/ServiceBindingUtils.java @@ -11,8 +11,13 @@ import org.collectionspace.services.common.service.ServiceObjectType; import org.collectionspace.services.common.tenant.TenantBindingType; import org.collectionspace.services.common.types.PropertyItemType; import org.collectionspace.services.common.types.PropertyType; +import org.collectionspace.services.nuxeo.util.NuxeoUtils; import org.nuxeo.ecm.core.api.ClientException; import org.nuxeo.ecm.core.api.DocumentModel; +import java.lang.IndexOutOfBoundsException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class ServiceBindingUtils { public static final boolean QUALIFIED_PROP_NAMES = true; @@ -27,6 +32,8 @@ public class ServiceBindingUtils { public static final String SERVICE_TYPE_AUTHORITY = "authority"; public static final String SERVICE_TYPE_UTILITY = "utility"; public static final String SERVICE_TYPE_SECURITY = "security"; + + private static final Logger logger = LoggerFactory.getLogger(ServiceBindingUtils.class); public static String getTenantQualifiedDocType(String tenantId, String docType) { String result = docType + ServiceContext.TENANT_SUFFIX + tenantId; @@ -152,6 +159,14 @@ public class ServiceBindingUtils { return null; try { return (String)docModel.getPropertyValue(propName); + } catch(IndexOutOfBoundsException ioobe) { + // Should not happen, but may with certain array forms + if(logger.isTraceEnabled()) { + logger.trace("SBUtils.getMappedField caught OOB exc, for Prop: "+propName + + " in: " + docModel.getDocumentType().getName() + + " csid: " + NuxeoUtils.getCsid(docModel)); + } + return null; } catch(ClientException ce) { throw new RuntimeException( "getMappedFieldInDoc: Problem fetching: "+propName+" logicalfieldName: "+logicalFieldName+" docModel: "+docModel, ce); @@ -163,7 +178,7 @@ public class ServiceBindingUtils { public static ArrayList getCommonServiceTypes() { if(commonServiceTypes == null) { commonServiceTypes = new ArrayList(); - commonServiceTypes.add(SERVICE_TYPE_AUTHORITY); + // Problematic at this point: commonServiceTypes.add(SERVICE_TYPE_AUTHORITY); commonServiceTypes.add(SERVICE_TYPE_OBJECT); commonServiceTypes.add(SERVICE_TYPE_PROCEDURE); } diff --git a/services/servicegroup/service/src/main/java/org/collectionspace/services/servicegroup/nuxeo/ServiceGroupDocumentModelHandler.java b/services/servicegroup/service/src/main/java/org/collectionspace/services/servicegroup/nuxeo/ServiceGroupDocumentModelHandler.java new file mode 100644 index 000000000..b82bbdde1 --- /dev/null +++ b/services/servicegroup/service/src/main/java/org/collectionspace/services/servicegroup/nuxeo/ServiceGroupDocumentModelHandler.java @@ -0,0 +1,244 @@ +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + + * http://www.collectionspace.org + * http://wiki.collectionspace.org + + * Copyright 2009 University of California at Berkeley + + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + + * You may obtain a copy of the ECL 2.0 License at + + * https://source.collectionspace.org/collection-space/LICENSE.txt + + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.collectionspace.services.servicegroup.nuxeo; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; + +import org.collectionspace.services.ServiceGroupListItemJAXBSchema; +import org.collectionspace.services.nuxeo.client.java.CommonList; +import org.collectionspace.services.nuxeo.client.java.DocHandlerBase; +import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClientImpl; +import org.collectionspace.services.nuxeo.util.NuxeoUtils; +import org.collectionspace.services.jaxb.AbstractCommonList; +import org.collectionspace.services.client.IQueryManager; +import org.collectionspace.services.client.PoxPayloadIn; +import org.collectionspace.services.client.PoxPayloadOut; +import org.collectionspace.services.common.ServiceMain; +import org.collectionspace.services.common.ServiceMessages; +import org.collectionspace.services.common.api.Tools; +import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl; +import org.collectionspace.services.common.context.ServiceBindingUtils; +import org.collectionspace.services.common.context.ServiceContext; +import org.collectionspace.services.common.document.DocumentException; +import org.collectionspace.services.common.document.DocumentFilter; +import org.collectionspace.services.common.document.DocumentNotFoundException; +import org.collectionspace.services.common.document.DocumentWrapper; +import org.collectionspace.services.common.query.QueryManager; +import org.collectionspace.services.common.repository.RepositoryClient; +import org.collectionspace.services.common.security.SecurityUtils; +import org.collectionspace.services.common.service.ServiceBindingType; +import org.collectionspace.services.common.service.ServiceObjectType; +import org.collectionspace.services.servicegroup.ServicegroupsCommon; +import org.nuxeo.ecm.core.api.DocumentModel; +import org.nuxeo.ecm.core.api.DocumentModelList; +import org.nuxeo.ecm.core.api.model.PropertyException; +import org.nuxeo.ecm.core.api.repository.RepositoryInstance; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ServiceGroupDocumentModelHandler + extends DocHandlerBase { + + protected final Logger logger = LoggerFactory.getLogger(this.getClass()); + + protected static final int NUM_META_FIELDS = 3; + protected static final String DOC_TYPE_FIELD = "docType"; + protected static final String DOC_NUMBER_FIELD = "docNumber"; + protected static final String DOC_NAME_FIELD = "docName"; + + public AbstractCommonList getItemsForGroup( + ServiceContext ctx, + List serviceGroupNames, + String keywords) throws Exception { + CommonList commonList = new CommonList(); + AbstractCommonList list = (AbstractCommonList)commonList; + RepositoryInstance repoSession = null; + boolean releaseRepoSession = false; + + try { + RepositoryJavaClientImpl repoClient = (RepositoryJavaClientImpl)this.getRepositoryClient(ctx); + repoSession = this.getRepositorySession(); + if (repoSession == null) { + repoSession = repoClient.getRepositorySession(); + releaseRepoSession = true; + } + DocumentFilter myFilter = getDocumentFilter(); + String whereClause = null; + if (keywords != null) { + whereClause = QueryManager.createWhereClauseFromKeywords(keywords); + if(Tools.isEmpty(whereClause)) { + if (logger.isDebugEnabled()) { + logger.debug("The WHERE clause is empty for keywords: ["+keywords+"]"); + } + } else { + myFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND); + if (logger.isDebugEnabled()) { + logger.debug("The WHERE clause is: " + myFilter.getWhereClause()); + } + } + } + int pageSize = myFilter.getPageSize(); + int pageNum = myFilter.getStartPage(); + final boolean computeTotal = true; + list.setPageNum(pageSize); + list.setPageSize(pageNum); + + try { + Map queriedServiceBindings = new HashMap(); + RepositoryJavaClientImpl nuxeoRepoClient = (RepositoryJavaClientImpl)repoClient; + // Get the service bindings for this tenant + TenantBindingConfigReaderImpl tReader = + ServiceMain.getInstance().getTenantBindingConfigReader(); + // We need to get all the procedures, authorities, and objects. + List servicebindings = + tReader.getServiceBindingsByType(ctx.getTenantId(), serviceGroupNames); + if (servicebindings == null || servicebindings.isEmpty()) { + Response response = Response.status(Response.Status.NOT_FOUND).entity( + ServiceMessages.READ_FAILED + + ServiceMessages.resourceNotFoundMsg(implode(serviceGroupNames, ","))).type("text/plain").build(); + throw new WebApplicationException(response); + } + servicebindings = SecurityUtils.getReadableServiceBindingsForCurrentUser(servicebindings); + // Build the list of docTypes for allowed serviceBindings + ArrayList docTypes = new ArrayList(); + for(ServiceBindingType binding:servicebindings) { + ServiceObjectType serviceObj = binding.getObject(); + if(serviceObj!=null) { + String docType = serviceObj.getName(); + docTypes.add(docType); + queriedServiceBindings.put(docType, binding); + } + } + + // Now we have to issue the search + // findDocs qill build a QueryContext, which wants to see a docType for our context + ctx.setDocumentType("Document"); + DocumentWrapper docListWrapper = nuxeoRepoClient.findDocs(ctx, repoSession, + docTypes, whereClause, pageSize, pageNum, computeTotal); + // Now we gather the info for each document into the list and return + DocumentModelList docList = docListWrapper.getWrappedObject(); + + if (docList == null) { // found no authRef fields - nothing to process + return list; + } + processDocList(docList, queriedServiceBindings, commonList); + list.setItemsInPage(docList.size()); + list.setTotalItems(docList.totalSize()); + } catch (DocumentException de) { + throw de; + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception ", e); + } + throw new DocumentException(e); + } finally { + if (releaseRepoSession && repoSession != null) { + repoClient.releaseRepositorySession(repoSession); + } + } + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception ", e); + } + throw new DocumentException(e); + } + return list; + } + + // Move this to a Utils class! + public static String implode(List stringList, String sep) { + StringBuilder sb = new StringBuilder(); + boolean fFirst = false; + for(String name:stringList) { + if(fFirst) { + fFirst = false; + } else { + sb.append(sep); + } + sb.append(name); + } + return sb.toString(); + } + + private String getUriFromServiceBinding(ServiceBindingType sb, String csid) { + return "/" + sb.getName().toLowerCase() + "/" + csid; + } + + private void processDocList( + DocumentModelList docList, + Map queriedServiceBindings, + CommonList list ) { + int nFields = NUM_META_FIELDS+NUM_STANDARD_LIST_RESULT_FIELDS; + String fields[] = new String[nFields]; + fields[0] = "csid"; + fields[1] = "uri"; + fields[2] = "updatedAt"; + fields[3] = DOC_NAME_FIELD; + fields[4] = DOC_NUMBER_FIELD; + fields[5] = DOC_TYPE_FIELD; + list.setFieldsReturned(fields); + Iterator iter = docList.iterator(); + HashMap item = new HashMap(); + while (iter.hasNext()) { + DocumentModel docModel = iter.next(); + String docType = docModel.getDocumentType().getName(); + ServiceBindingType sb = queriedServiceBindings.get(docType); + if (sb == null) { + throw new RuntimeException( + "processDocList: No Service Binding for docType: " + docType); + } + String csid = NuxeoUtils.getCsid(docModel); + item.put(STANDARD_LIST_CSID_FIELD, csid); + // Need to get the URI for the document, by it's type. + item.put(STANDARD_LIST_URI_FIELD, getUriFromServiceBinding(sb, csid)); + try { + item.put(STANDARD_LIST_UPDATED_AT_FIELD, getUpdatedAtAsString(docModel)); + } catch(Exception e) { + logger.error("Error getting udpatedAt value for doc ["+csid+"]: "+e.getLocalizedMessage()); + } + + String value = ServiceBindingUtils.getMappedFieldInDoc(sb, + ServiceBindingUtils.OBJ_NUMBER_PROP, docModel); + item.put(DOC_NUMBER_FIELD, value); + value = ServiceBindingUtils.getMappedFieldInDoc(sb, + ServiceBindingUtils.OBJ_NAME_PROP, docModel); + item.put(DOC_NAME_FIELD, value); + item.put(DOC_TYPE_FIELD, docType); + + list.addItem(item); + item.clear(); + } + + } + + +} + -- 2.47.3