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.ResourceMap;
40 import org.collectionspace.services.common.ServiceMain;
41 import org.collectionspace.services.common.ServiceMessages;
42 import org.collectionspace.services.common.UriInfoWrapper;
43 import org.collectionspace.services.common.api.Tools;
44 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
45 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
46 import org.collectionspace.services.common.context.ServiceBindingUtils;
47 import org.collectionspace.services.common.context.ServiceContext;
48 import org.collectionspace.services.common.context.ServiceContextFactory;
49 import org.collectionspace.services.common.document.DocumentFilter;
50 import org.collectionspace.services.common.query.QueryManager;
51 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils;
52 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.Specifier;
53 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.SpecifierForm;
54 import org.collectionspace.services.config.service.ServiceBindingType;
55 import org.collectionspace.services.config.service.ServiceObjectType;
56 import org.collectionspace.services.nuxeo.client.java.CommonList;
57 import org.collectionspace.services.nuxeo.client.java.NuxeoDocumentFilter;
58 import org.collectionspace.services.servicegroup.nuxeo.ServiceGroupDocumentModelHandler;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
62 import javax.ws.rs.Consumes;
63 import javax.ws.rs.GET;
64 import javax.ws.rs.Path;
65 import javax.ws.rs.PathParam;
66 import javax.ws.rs.Produces;
67 import javax.ws.rs.core.Context;
68 import javax.ws.rs.core.MultivaluedMap;
69 import javax.ws.rs.core.Response;
70 import javax.ws.rs.core.UriInfo;
72 @Path(ServiceGroupClient.SERVICE_PATH)
73 @Produces({"application/xml"})
74 @Consumes({"application/xml"})
75 public class ServiceGroupResource extends AbstractCollectionSpaceResourceImpl<PoxPayloadIn, PoxPayloadOut> {
77 protected final Logger logger = LoggerFactory.getLogger(this.getClass());
79 private final static boolean EXCLUDE_AUTHORITIES = false;
80 private final static boolean INCLUDE_AUTHORITIES = true;
83 public String getServiceName(){
84 return ServiceGroupClient.SERVICE_NAME;
87 public String getServicePathComponent(){
88 return ServiceGroupClient.SERVICE_NAME.toLowerCase();
92 protected String getVersionString() {
93 final String lastChangeRevision = "$LastChangedRevision: 2108 $";
94 return lastChangeRevision;
98 //public Class<ServicegroupsCommon> getCommonPartClass() {
99 public Class<?> getCommonPartClass() {
101 return Class.forName("org.collectionspace.services.servicegroup.ServicegroupsCommon");//.class;
102 } catch (ClassNotFoundException e){
108 public ServiceContextFactory<PoxPayloadIn, PoxPayloadOut> getServiceContextFactory() {
109 return MultipartServiceContextFactory.get();
113 //======================= GET without specifier: List =====================================
115 public AbstractCommonList getList(@Context UriInfo ui) {
117 CommonList commonList = new CommonList();
118 AbstractCommonList list = (AbstractCommonList)commonList;
119 ArrayList<String> svcGroups = new ArrayList<String>();
120 svcGroups.add("procedure");
121 svcGroups.add("object");
122 svcGroups.add("authority");
123 // Fetch the list of groups from the tenant-bindings config, and prepare a list item
125 // We always declare this a full list, of the size that we are returning.
126 // Not quite in the spirit of what paging means, but tells callers not to ask for more.
128 list.setPageSize(svcGroups.size());
129 list.setItemsInPage(svcGroups.size());
130 list.setTotalItems(svcGroups.size());
131 String fields[] = new String[2];
132 fields[0] = ServiceGroupListItemJAXBSchema.NAME;
133 fields[1] = ServiceGroupListItemJAXBSchema.URI;
134 commonList.setFieldsReturned(fields);
135 HashMap<String, Object> item = new HashMap<String, Object>();
136 for(String groupName:svcGroups){
137 item.put(ServiceGroupListItemJAXBSchema.NAME, groupName);
138 String uri = "/" + getServiceName().toLowerCase() + "/" + groupName;
139 item.put(ServiceGroupListItemJAXBSchema.URI, uri);
140 commonList.addItem(item);
144 } catch (Exception e) {
145 throw bigReThrow(e, ServiceMessages.LIST_FAILED);
151 //======================= GET ====================================================
152 // NOTE that csid is not a good name for the specifier, but if we name it anything else,
153 // our AuthZ gets confused!!!
158 @PathParam("csid") String groupname) {
159 PoxPayloadOut result = null;
160 ensureCSID(groupname, NuxeoBasedResource.READ);
162 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
163 TenantBindingConfigReaderImpl tReader =
164 ServiceMain.getInstance().getTenantBindingConfigReader();
165 // We need to get all the procedures, authorities, and objects.
166 ArrayList<String> groupsList = null;
167 if("common".equalsIgnoreCase(groupname)) {
168 groupsList = ServiceBindingUtils.getCommonServiceTypes(INCLUDE_AUTHORITIES);
170 groupsList = new ArrayList<String>();
171 groupsList.add(groupname);
173 List<ServiceBindingType> servicebindings = tReader.getServiceBindingsByType(ctx.getTenantId(), groupsList);
174 if (servicebindings == null || servicebindings.isEmpty()) {
175 // 404 if there are no mappings.
176 Response response = Response.status(Response.Status.NOT_FOUND).entity(
177 ServiceMessages.READ_FAILED + ServiceMessages.resourceNotFoundMsg(groupname)).type("text/plain").build();
178 throw new CSWebApplicationException(response);
180 //Otherwise, build the response with a list
181 ServicegroupsCommon common = new ServicegroupsCommon();
182 common.setName(groupname);
183 String uri = "/" + getServicePathComponent() + "/" + groupname;
185 result = new PoxPayloadOut(getServicePathComponent());
186 result.addPart("ServicegroupsCommon", common);
188 ServicegroupsCommon.HasDocTypes wrapper = common.getHasDocTypes();
190 wrapper = new ServicegroupsCommon.HasDocTypes();
191 common.setHasDocTypes(wrapper);
193 List<String> hasDocTypes = wrapper.getHasDocType();
194 for(ServiceBindingType binding:servicebindings) {
195 ServiceObjectType serviceObj = binding.getObject();
196 if(serviceObj!=null) {
197 String docType = serviceObj.getName();
198 hasDocTypes.add(docType);
201 } catch (Exception e) {
202 throw bigReThrow(e, ServiceMessages.READ_FAILED, groupname);
205 return result.getBytes();
209 @Path("{csid}/items")
210 public AbstractCommonList getItemList(
211 @Context UriInfo uriInfo,
212 @PathParam("csid") String serviceGroupName) {
213 UriInfoWrapper ui = new UriInfoWrapper(uriInfo);
214 ensureCSID(serviceGroupName, NuxeoBasedResource.READ);
215 AbstractCommonList list = null;
217 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(ui);
218 ServiceGroupDocumentModelHandler handler = (ServiceGroupDocumentModelHandler)
219 createDocumentHandler(ctx);
220 ArrayList<String> groupsList = null;
221 if("common".equalsIgnoreCase(serviceGroupName)) {
222 groupsList = ServiceBindingUtils.getCommonServiceTypes(INCLUDE_AUTHORITIES);
224 groupsList = new ArrayList<String>();
225 groupsList.add(serviceGroupName);
228 // check first for a csid query parameter
229 MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
230 String csid = queryParams.getFirst(IQueryManager.CSID_QUERY_PARAM);
231 if (csid != null && !csid.isEmpty()) {
232 String whereClause = QueryManager.createWhereClauseFromCsid(csid);
233 if (Tools.isEmpty(whereClause)) {
234 if (logger.isDebugEnabled()) {
235 logger.debug("The WHERE clause is empty for csid: ["+csid+"]");
238 DocumentFilter documentFilter = handler.getDocumentFilter();
239 documentFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND);
240 if (logger.isDebugEnabled()) {
241 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
245 // check to see if we have to set up a keyword search
246 String keywords = queryParams.getFirst(IQueryManager.SEARCH_TYPE_KEYWORDS_KW);
247 if (keywords != null && !keywords.isEmpty()) {
248 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
249 if(Tools.isEmpty(whereClause)) {
250 if (logger.isDebugEnabled()) {
251 logger.debug("The WHERE clause is empty for keywords: ["+keywords+"]");
254 DocumentFilter documentFilter = handler.getDocumentFilter();
255 documentFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND);
256 if (logger.isDebugEnabled()) {
257 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
264 list = handler.getItemListForGroup(ctx, groupsList);
265 } catch (Exception e) {
266 throw bigReThrow(e, ServiceMessages.READ_FAILED, serviceGroupName);
273 @Path("{csid}/items/{specifier}")
274 public byte[] getItem(
275 @Context ResourceMap resourceMap,
276 @Context UriInfo uriInfo,
277 @PathParam("csid") String serviceGroupName,
278 @PathParam("specifier") String specifier) {
279 UriInfoWrapper ui = new UriInfoWrapper(uriInfo);
280 ensureCSID(serviceGroupName, NuxeoBasedResource.READ);
281 PoxPayloadOut result = null;
284 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(ui);
285 ServiceGroupDocumentModelHandler handler = (ServiceGroupDocumentModelHandler)
286 createDocumentHandler(ctx);
287 ArrayList<String> groupsList = null;
288 if("common".equalsIgnoreCase(serviceGroupName)) {
289 groupsList = ServiceBindingUtils.getCommonServiceTypes(INCLUDE_AUTHORITIES);
291 groupsList = new ArrayList<String>();
292 groupsList.add(serviceGroupName);
295 String whereClause = QueryManager.createWhereClauseFromCsid(specifier);
296 DocumentFilter myFilter = new NuxeoDocumentFilter(whereClause, 0, 1);
297 handler.setDocumentFilter(myFilter);
299 result = handler.getItemForCsid(ctx, groupsList, specifier);
300 } catch (Exception e) {
301 throw bigReThrow(e, ServiceMessages.READ_FAILED, serviceGroupName);
304 return result.getBytes();