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;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
30 import org.collectionspace.services.ServiceGroupListItemJAXBSchema;
31 import org.collectionspace.services.client.IQueryManager;
32 import org.collectionspace.services.client.PoxPayloadIn;
33 import org.collectionspace.services.client.PoxPayloadOut;
34 import org.collectionspace.services.client.ServiceGroupClient;
35 import org.collectionspace.services.jaxb.AbstractCommonList;
36 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
37 import org.collectionspace.services.common.ResourceBase;
38 import org.collectionspace.services.common.ServiceMain;
39 import org.collectionspace.services.common.ServiceMessages;
40 import org.collectionspace.services.common.api.Tools;
41 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
42 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
43 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
44 import org.collectionspace.services.common.context.ServiceBindingUtils;
45 import org.collectionspace.services.common.context.ServiceContext;
46 import org.collectionspace.services.common.context.ServiceContextFactory;
47 import org.collectionspace.services.common.document.DocumentException;
48 import org.collectionspace.services.common.document.DocumentFilter;
49 import org.collectionspace.services.common.document.DocumentHandler;
50 import org.collectionspace.services.common.document.DocumentNotFoundException;
51 import org.collectionspace.services.common.document.DocumentWrapper;
52 import org.collectionspace.services.common.query.QueryManager;
53 import org.collectionspace.services.common.repository.RepositoryClient;
54 import org.collectionspace.services.common.repository.RepositoryClientFactory;
55 import org.collectionspace.services.config.service.ServiceBindingType;
56 import org.collectionspace.services.config.service.ServiceObjectType;
57 import org.collectionspace.services.nuxeo.client.java.CommonList;
58 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
59 import org.collectionspace.services.servicegroup.nuxeo.ServiceGroupDocumentModelHandler;
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 import javax.ws.rs.Consumes;
68 import javax.ws.rs.GET;
69 import javax.ws.rs.Path;
70 import javax.ws.rs.PathParam;
71 import javax.ws.rs.Produces;
72 import javax.ws.rs.WebApplicationException;
73 import javax.ws.rs.core.Context;
74 import javax.ws.rs.core.MultivaluedMap;
75 import javax.ws.rs.core.Response;
76 import javax.ws.rs.core.UriInfo;
78 @Path(ServiceGroupClient.SERVICE_PATH)
79 @Produces({"application/xml"})
80 @Consumes({"application/xml"})
81 public class ServiceGroupResource extends AbstractCollectionSpaceResourceImpl {
83 protected final Logger logger = LoggerFactory.getLogger(this.getClass());
85 private final static boolean EXCLUDE_AUTHORITIES = false;
88 public String getServiceName(){
89 return ServiceGroupClient.SERVICE_NAME;
92 public String getServicePathComponent(){
93 return ServiceGroupClient.SERVICE_NAME.toLowerCase();
97 protected String getVersionString() {
98 final String lastChangeRevision = "$LastChangedRevision: 2108 $";
99 return lastChangeRevision;
103 //public Class<ServicegroupsCommon> getCommonPartClass() {
104 public Class getCommonPartClass() {
106 return Class.forName("org.collectionspace.services.servicegroup.ServicegroupsCommon");//.class;
107 } catch (ClassNotFoundException e){
113 public ServiceContextFactory<PoxPayloadIn, PoxPayloadOut> getServiceContextFactory() {
114 return MultipartServiceContextFactory.get();
118 //======================= GET without specifier: List =====================================
120 public AbstractCommonList getList(@Context UriInfo ui) {
122 CommonList commonList = new CommonList();
123 AbstractCommonList list = (AbstractCommonList)commonList;
124 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
125 String commonSchema = ctx.getCommonPartLabel();
126 ArrayList<String> svcGroups = new ArrayList<String>();
127 svcGroups.add("procedure");
128 svcGroups.add("object");
129 svcGroups.add("authority");
130 // Fetch the list of groups from the tenant-bindings config, and prepare a list item
132 // We always declare this a full list, of the size that we are returning.
133 // Not quite in the spirit of what paging means, but tells callers not to ask for more.
135 list.setPageSize(svcGroups.size());
136 list.setItemsInPage(svcGroups.size());
137 list.setTotalItems(svcGroups.size());
138 String fields[] = new String[2];
139 fields[0] = ServiceGroupListItemJAXBSchema.NAME;
140 fields[1] = ServiceGroupListItemJAXBSchema.URI;
141 commonList.setFieldsReturned(fields);
142 HashMap<String, Object> item = new HashMap<String, Object>();
143 for(String groupName:svcGroups){
144 item.put(ServiceGroupListItemJAXBSchema.NAME, groupName);
145 String uri = "/" + getServiceName().toLowerCase() + "/" + groupName;
146 item.put(ServiceGroupListItemJAXBSchema.URI, uri);
147 commonList.addItem(item);
151 } catch (Exception e) {
152 throw bigReThrow(e, ServiceMessages.LIST_FAILED);
158 //======================= GET ====================================================
159 // NOTE that csid is not a good name for the specifier, but if we name it anything else,
160 // our AuthZ gets confused!!!
165 @PathParam("csid") String groupname) {
166 PoxPayloadOut result = null;
167 ensureCSID(groupname, ResourceBase.READ);
169 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
170 TenantBindingConfigReaderImpl tReader =
171 ServiceMain.getInstance().getTenantBindingConfigReader();
172 // We need to get all the procedures, authorities, and objects.
173 ArrayList<String> groupsList = null;
174 if("common".equalsIgnoreCase(groupname)) {
175 groupsList = ServiceBindingUtils.getCommonServiceTypes(false); // CSPACE-5359: Excluding Authority type to stay backward compat with v2.4
177 groupsList = new ArrayList<String>();
178 groupsList.add(groupname);
180 List<ServiceBindingType> servicebindings = tReader.getServiceBindingsByType(ctx.getTenantId(), groupsList);
181 if (servicebindings == null || servicebindings.isEmpty()) {
182 // 404 if there are no mappings.
183 Response response = Response.status(Response.Status.NOT_FOUND).entity(
184 ServiceMessages.READ_FAILED + ServiceMessages.resourceNotFoundMsg(groupname)).type("text/plain").build();
185 throw new WebApplicationException(response);
187 //Otherwise, build the response with a list
188 ServicegroupsCommon common = new ServicegroupsCommon();
189 common.setName(groupname);
190 String uri = "/" + getServicePathComponent() + "/" + groupname;
192 result = new PoxPayloadOut(getServicePathComponent());
193 result.addPart("ServicegroupsCommon", common);
195 ServicegroupsCommon.HasDocTypes wrapper = common.getHasDocTypes();
197 wrapper = new ServicegroupsCommon.HasDocTypes();
198 common.setHasDocTypes(wrapper);
200 List<String> hasDocTypes = wrapper.getHasDocType();
201 for(ServiceBindingType binding:servicebindings) {
202 ServiceObjectType serviceObj = binding.getObject();
203 if(serviceObj!=null) {
204 String docType = serviceObj.getName();
205 hasDocTypes.add(docType);
208 } catch (Exception e) {
209 throw bigReThrow(e, ServiceMessages.READ_FAILED, groupname);
212 return result.getBytes();
217 @Path("{csid}/items")
218 public AbstractCommonList getItems(
220 @PathParam("csid") String serviceGroupName) {
221 ensureCSID(serviceGroupName, ResourceBase.READ);
222 AbstractCommonList list = null;
224 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
225 String keywords = queryParams.getFirst(IQueryManager.SEARCH_TYPE_KEYWORDS_KW);
226 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(queryParams);
227 ServiceGroupDocumentModelHandler handler = (ServiceGroupDocumentModelHandler)
228 createDocumentHandler(ctx);
229 ArrayList<String> groupsList = null;
230 if("common".equalsIgnoreCase(serviceGroupName)) {
231 groupsList = ServiceBindingUtils.getCommonServiceTypes(); //CSPACE-5359: Exclude authorities to remain backward compat with v2.4
233 groupsList = new ArrayList<String>();
234 groupsList.add(serviceGroupName);
236 // set up a keyword search
237 if (keywords != null && !keywords.isEmpty()) {
238 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
239 if(Tools.isEmpty(whereClause)) {
240 if (logger.isDebugEnabled()) {
241 logger.debug("The WHERE clause is empty for keywords: ["+keywords+"]");
244 DocumentFilter documentFilter = handler.getDocumentFilter();
245 documentFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND);
246 if (logger.isDebugEnabled()) {
247 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
251 list = handler.getItemsForGroup(ctx, groupsList);
252 } catch (Exception e) {
253 throw bigReThrow(e, ServiceMessages.READ_FAILED, serviceGroupName);