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.CSWebApplicationException;
38 import org.collectionspace.services.common.NuxeoBasedResource;
39 import org.collectionspace.services.common.ServiceMain;
40 import org.collectionspace.services.common.ServiceMessages;
41 import org.collectionspace.services.common.api.Tools;
42 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
43 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
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.DocumentFilter;
48 import org.collectionspace.services.common.query.QueryManager;
49 import org.collectionspace.services.config.service.ServiceBindingType;
50 import org.collectionspace.services.config.service.ServiceObjectType;
51 import org.collectionspace.services.nuxeo.client.java.CommonList;
52 import org.collectionspace.services.servicegroup.nuxeo.ServiceGroupDocumentModelHandler;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
57 import javax.ws.rs.Consumes;
58 import javax.ws.rs.GET;
59 import javax.ws.rs.Path;
60 import javax.ws.rs.PathParam;
61 import javax.ws.rs.Produces;
62 import javax.ws.rs.core.Context;
63 import javax.ws.rs.core.MultivaluedMap;
64 import javax.ws.rs.core.Response;
65 import javax.ws.rs.core.UriInfo;
67 @Path(ServiceGroupClient.SERVICE_PATH)
68 @Produces({"application/xml"})
69 @Consumes({"application/xml"})
70 public class ServiceGroupResource extends AbstractCollectionSpaceResourceImpl<PoxPayloadIn, PoxPayloadOut> {
72 protected final Logger logger = LoggerFactory.getLogger(this.getClass());
74 private final static boolean EXCLUDE_AUTHORITIES = false;
75 private final static boolean INCLUDE_AUTHORITIES = true;
78 public String getServiceName(){
79 return ServiceGroupClient.SERVICE_NAME;
82 public String getServicePathComponent(){
83 return ServiceGroupClient.SERVICE_NAME.toLowerCase();
87 protected String getVersionString() {
88 final String lastChangeRevision = "$LastChangedRevision: 2108 $";
89 return lastChangeRevision;
93 //public Class<ServicegroupsCommon> getCommonPartClass() {
94 public Class<?> getCommonPartClass() {
96 return Class.forName("org.collectionspace.services.servicegroup.ServicegroupsCommon");//.class;
97 } catch (ClassNotFoundException e){
103 public ServiceContextFactory<PoxPayloadIn, PoxPayloadOut> getServiceContextFactory() {
104 return MultipartServiceContextFactory.get();
108 //======================= GET without specifier: List =====================================
110 public AbstractCommonList getList(@Context UriInfo ui) {
112 CommonList commonList = new CommonList();
113 AbstractCommonList list = (AbstractCommonList)commonList;
114 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
115 String commonSchema = ctx.getCommonPartLabel();
116 ArrayList<String> svcGroups = new ArrayList<String>();
117 svcGroups.add("procedure");
118 svcGroups.add("object");
119 svcGroups.add("authority");
120 // Fetch the list of groups from the tenant-bindings config, and prepare a list item
122 // We always declare this a full list, of the size that we are returning.
123 // Not quite in the spirit of what paging means, but tells callers not to ask for more.
125 list.setPageSize(svcGroups.size());
126 list.setItemsInPage(svcGroups.size());
127 list.setTotalItems(svcGroups.size());
128 String fields[] = new String[2];
129 fields[0] = ServiceGroupListItemJAXBSchema.NAME;
130 fields[1] = ServiceGroupListItemJAXBSchema.URI;
131 commonList.setFieldsReturned(fields);
132 HashMap<String, Object> item = new HashMap<String, Object>();
133 for(String groupName:svcGroups){
134 item.put(ServiceGroupListItemJAXBSchema.NAME, groupName);
135 String uri = "/" + getServiceName().toLowerCase() + "/" + groupName;
136 item.put(ServiceGroupListItemJAXBSchema.URI, uri);
137 commonList.addItem(item);
141 } catch (Exception e) {
142 throw bigReThrow(e, ServiceMessages.LIST_FAILED);
148 //======================= GET ====================================================
149 // NOTE that csid is not a good name for the specifier, but if we name it anything else,
150 // our AuthZ gets confused!!!
155 @PathParam("csid") String groupname) {
156 PoxPayloadOut result = null;
157 ensureCSID(groupname, NuxeoBasedResource.READ);
159 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
160 TenantBindingConfigReaderImpl tReader =
161 ServiceMain.getInstance().getTenantBindingConfigReader();
162 // We need to get all the procedures, authorities, and objects.
163 ArrayList<String> groupsList = null;
164 if("common".equalsIgnoreCase(groupname)) {
165 groupsList = ServiceBindingUtils.getCommonServiceTypes(INCLUDE_AUTHORITIES);
167 groupsList = new ArrayList<String>();
168 groupsList.add(groupname);
170 List<ServiceBindingType> servicebindings = tReader.getServiceBindingsByType(ctx.getTenantId(), groupsList);
171 if (servicebindings == null || servicebindings.isEmpty()) {
172 // 404 if there are no mappings.
173 Response response = Response.status(Response.Status.NOT_FOUND).entity(
174 ServiceMessages.READ_FAILED + ServiceMessages.resourceNotFoundMsg(groupname)).type("text/plain").build();
175 throw new CSWebApplicationException(response);
177 //Otherwise, build the response with a list
178 ServicegroupsCommon common = new ServicegroupsCommon();
179 common.setName(groupname);
180 String uri = "/" + getServicePathComponent() + "/" + groupname;
182 result = new PoxPayloadOut(getServicePathComponent());
183 result.addPart("ServicegroupsCommon", common);
185 ServicegroupsCommon.HasDocTypes wrapper = common.getHasDocTypes();
187 wrapper = new ServicegroupsCommon.HasDocTypes();
188 common.setHasDocTypes(wrapper);
190 List<String> hasDocTypes = wrapper.getHasDocType();
191 for(ServiceBindingType binding:servicebindings) {
192 ServiceObjectType serviceObj = binding.getObject();
193 if(serviceObj!=null) {
194 String docType = serviceObj.getName();
195 hasDocTypes.add(docType);
198 } catch (Exception e) {
199 throw bigReThrow(e, ServiceMessages.READ_FAILED, groupname);
202 return result.getBytes();
207 @Path("{csid}/items")
208 public AbstractCommonList getItems(
210 @PathParam("csid") String serviceGroupName) {
211 ensureCSID(serviceGroupName, NuxeoBasedResource.READ);
212 AbstractCommonList list = null;
214 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(ui);
215 ServiceGroupDocumentModelHandler handler = (ServiceGroupDocumentModelHandler)
216 createDocumentHandler(ctx);
217 ArrayList<String> groupsList = null;
218 if("common".equalsIgnoreCase(serviceGroupName)) {
219 groupsList = ServiceBindingUtils.getCommonServiceTypes(INCLUDE_AUTHORITIES);
221 groupsList = new ArrayList<String>();
222 groupsList.add(serviceGroupName);
224 // set up a keyword search
225 MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
226 String keywords = queryParams.getFirst(IQueryManager.SEARCH_TYPE_KEYWORDS_KW);
227 if (keywords != null && !keywords.isEmpty()) {
228 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
229 if(Tools.isEmpty(whereClause)) {
230 if (logger.isDebugEnabled()) {
231 logger.debug("The WHERE clause is empty for keywords: ["+keywords+"]");
234 DocumentFilter documentFilter = handler.getDocumentFilter();
235 documentFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND);
236 if (logger.isDebugEnabled()) {
237 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
241 list = handler.getItemsForGroup(ctx, groupsList);
242 } catch (Exception e) {
243 throw bigReThrow(e, ServiceMessages.READ_FAILED, serviceGroupName);