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.UriInfoWrapper;
42 import org.collectionspace.services.common.api.Tools;
43 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
44 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
45 import org.collectionspace.services.common.context.ServiceBindingUtils;
46 import org.collectionspace.services.common.context.ServiceContext;
47 import org.collectionspace.services.common.context.ServiceContextFactory;
48 import org.collectionspace.services.common.document.DocumentFilter;
49 import org.collectionspace.services.common.query.QueryManager;
50 import org.collectionspace.services.config.service.ServiceBindingType;
51 import org.collectionspace.services.config.service.ServiceObjectType;
52 import org.collectionspace.services.nuxeo.client.java.CommonList;
53 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 ArrayList<String> svcGroups = new ArrayList<String>();
115 svcGroups.add("procedure");
116 svcGroups.add("object");
117 svcGroups.add("authority");
118 // Fetch the list of groups from the tenant-bindings config, and prepare a list item
120 // We always declare this a full list, of the size that we are returning.
121 // Not quite in the spirit of what paging means, but tells callers not to ask for more.
123 list.setPageSize(svcGroups.size());
124 list.setItemsInPage(svcGroups.size());
125 list.setTotalItems(svcGroups.size());
126 String fields[] = new String[2];
127 fields[0] = ServiceGroupListItemJAXBSchema.NAME;
128 fields[1] = ServiceGroupListItemJAXBSchema.URI;
129 commonList.setFieldsReturned(fields);
130 HashMap<String, Object> item = new HashMap<String, Object>();
131 for(String groupName:svcGroups){
132 item.put(ServiceGroupListItemJAXBSchema.NAME, groupName);
133 String uri = "/" + getServiceName().toLowerCase() + "/" + groupName;
134 item.put(ServiceGroupListItemJAXBSchema.URI, uri);
135 commonList.addItem(item);
139 } catch (Exception e) {
140 throw bigReThrow(e, ServiceMessages.LIST_FAILED);
146 //======================= GET ====================================================
147 // NOTE that csid is not a good name for the specifier, but if we name it anything else,
148 // our AuthZ gets confused!!!
153 @PathParam("csid") String groupname) {
154 PoxPayloadOut result = null;
155 ensureCSID(groupname, NuxeoBasedResource.READ);
157 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
158 TenantBindingConfigReaderImpl tReader =
159 ServiceMain.getInstance().getTenantBindingConfigReader();
160 // We need to get all the procedures, authorities, and objects.
161 ArrayList<String> groupsList = null;
162 if("common".equalsIgnoreCase(groupname)) {
163 groupsList = ServiceBindingUtils.getCommonServiceTypes(INCLUDE_AUTHORITIES);
165 groupsList = new ArrayList<String>();
166 groupsList.add(groupname);
168 List<ServiceBindingType> servicebindings = tReader.getServiceBindingsByType(ctx.getTenantId(), groupsList);
169 if (servicebindings == null || servicebindings.isEmpty()) {
170 // 404 if there are no mappings.
171 Response response = Response.status(Response.Status.NOT_FOUND).entity(
172 ServiceMessages.READ_FAILED + ServiceMessages.resourceNotFoundMsg(groupname)).type("text/plain").build();
173 throw new CSWebApplicationException(response);
175 //Otherwise, build the response with a list
176 ServicegroupsCommon common = new ServicegroupsCommon();
177 common.setName(groupname);
178 String uri = "/" + getServicePathComponent() + "/" + groupname;
180 result = new PoxPayloadOut(getServicePathComponent());
181 result.addPart("ServicegroupsCommon", common);
183 ServicegroupsCommon.HasDocTypes wrapper = common.getHasDocTypes();
185 wrapper = new ServicegroupsCommon.HasDocTypes();
186 common.setHasDocTypes(wrapper);
188 List<String> hasDocTypes = wrapper.getHasDocType();
189 for(ServiceBindingType binding:servicebindings) {
190 ServiceObjectType serviceObj = binding.getObject();
191 if(serviceObj!=null) {
192 String docType = serviceObj.getName();
193 hasDocTypes.add(docType);
196 } catch (Exception e) {
197 throw bigReThrow(e, ServiceMessages.READ_FAILED, groupname);
200 return result.getBytes();
205 @Path("{csid}/items")
206 public AbstractCommonList getItems(
207 @Context UriInfo uriInfo,
208 @PathParam("csid") String serviceGroupName) {
209 UriInfoWrapper ui = new UriInfoWrapper(uriInfo);
210 ensureCSID(serviceGroupName, NuxeoBasedResource.READ);
211 AbstractCommonList list = null;
213 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(ui);
214 ServiceGroupDocumentModelHandler handler = (ServiceGroupDocumentModelHandler)
215 createDocumentHandler(ctx);
216 ArrayList<String> groupsList = null;
217 if("common".equalsIgnoreCase(serviceGroupName)) {
218 groupsList = ServiceBindingUtils.getCommonServiceTypes(INCLUDE_AUTHORITIES);
220 groupsList = new ArrayList<String>();
221 groupsList.add(serviceGroupName);
223 // set up a keyword search
224 MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
225 String keywords = queryParams.getFirst(IQueryManager.SEARCH_TYPE_KEYWORDS_KW);
226 if (keywords != null && !keywords.isEmpty()) {
227 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
228 if(Tools.isEmpty(whereClause)) {
229 if (logger.isDebugEnabled()) {
230 logger.debug("The WHERE clause is empty for keywords: ["+keywords+"]");
233 DocumentFilter documentFilter = handler.getDocumentFilter();
234 documentFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND);
235 if (logger.isDebugEnabled()) {
236 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
240 list = handler.getItemsForGroup(ctx, groupsList);
241 } catch (Exception e) {
242 throw bigReThrow(e, ServiceMessages.READ_FAILED, serviceGroupName);