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());
86 public String getServiceName(){
87 return ServiceGroupClient.SERVICE_NAME;
90 public String getServicePathComponent(){
91 return ServiceGroupClient.SERVICE_NAME.toLowerCase();
95 protected String getVersionString() {
96 final String lastChangeRevision = "$LastChangedRevision: 2108 $";
97 return lastChangeRevision;
101 //public Class<ServicegroupsCommon> getCommonPartClass() {
102 public Class getCommonPartClass() {
104 return Class.forName("org.collectionspace.services.servicegroup.ServicegroupsCommon");//.class;
105 } catch (ClassNotFoundException e){
111 public ServiceContextFactory<PoxPayloadIn, PoxPayloadOut> getServiceContextFactory() {
112 return MultipartServiceContextFactory.get();
116 //======================= GET without specifier: List =====================================
118 public AbstractCommonList getList(@Context UriInfo ui) {
120 CommonList commonList = new CommonList();
121 AbstractCommonList list = (AbstractCommonList)commonList;
122 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
123 String commonSchema = ctx.getCommonPartLabel();
124 ArrayList<String> svcGroups = new ArrayList<String>();
125 svcGroups.add("procedure");
126 svcGroups.add("object");
127 svcGroups.add("authority");
128 // Fetch the list of groups from the tenant-bindings config, and prepare a list item
130 // We always declare this a full list, of the size that we are returning.
131 // Not quite in the spirit of what paging means, but tells callers not to ask for more.
133 list.setPageSize(svcGroups.size());
134 list.setItemsInPage(svcGroups.size());
135 list.setTotalItems(svcGroups.size());
136 String fields[] = new String[2];
137 fields[0] = ServiceGroupListItemJAXBSchema.NAME;
138 fields[1] = ServiceGroupListItemJAXBSchema.URI;
139 commonList.setFieldsReturned(fields);
140 HashMap<String, Object> item = new HashMap<String, Object>();
141 for(String groupName:svcGroups){
142 item.put(ServiceGroupListItemJAXBSchema.NAME, groupName);
143 String uri = "/" + getServiceName().toLowerCase() + "/" + groupName;
144 item.put(ServiceGroupListItemJAXBSchema.URI, uri);
145 commonList.addItem(item);
149 } catch (Exception e) {
150 throw bigReThrow(e, ServiceMessages.LIST_FAILED);
156 //======================= GET ====================================================
157 // NOTE that csid is not a good name for the specifier, but if we name it anything else,
158 // our AuthZ gets confused!!!
163 @PathParam("csid") String groupname) {
164 PoxPayloadOut result = null;
165 ensureCSID(groupname, ResourceBase.READ);
167 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
168 TenantBindingConfigReaderImpl tReader =
169 ServiceMain.getInstance().getTenantBindingConfigReader();
170 // We need to get all the procedures, authorities, and objects.
171 ArrayList<String> groupsList = null;
172 if("common".equalsIgnoreCase(groupname)) {
173 groupsList = ServiceBindingUtils.getCommonServiceTypes();
175 groupsList = new ArrayList<String>();
176 groupsList.add(groupname);
178 List<ServiceBindingType> servicebindings = tReader.getServiceBindingsByType(ctx.getTenantId(), groupsList);
179 if (servicebindings == null || servicebindings.isEmpty()) {
180 // 404 if there are no mappings.
181 Response response = Response.status(Response.Status.NOT_FOUND).entity(
182 ServiceMessages.READ_FAILED + ServiceMessages.resourceNotFoundMsg(groupname)).type("text/plain").build();
183 throw new WebApplicationException(response);
185 //Otherwise, build the response with a list
186 ServicegroupsCommon common = new ServicegroupsCommon();
187 common.setName(groupname);
188 String uri = "/" + getServicePathComponent() + "/" + groupname;
190 result = new PoxPayloadOut(getServicePathComponent());
191 result.addPart("ServicegroupsCommon", common);
193 ServicegroupsCommon.HasDocTypes wrapper = common.getHasDocTypes();
195 wrapper = new ServicegroupsCommon.HasDocTypes();
196 common.setHasDocTypes(wrapper);
198 List<String> hasDocTypes = wrapper.getHasDocType();
199 for(ServiceBindingType binding:servicebindings) {
200 ServiceObjectType serviceObj = binding.getObject();
201 if(serviceObj!=null) {
202 String docType = serviceObj.getName();
203 hasDocTypes.add(docType);
206 } catch (Exception e) {
207 throw bigReThrow(e, ServiceMessages.READ_FAILED, groupname);
210 return result.getBytes();
215 @Path("{csid}/items")
216 public AbstractCommonList getItems(
218 @PathParam("csid") String serviceGroupName) {
219 ensureCSID(serviceGroupName, ResourceBase.READ);
220 AbstractCommonList list = null;
222 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
223 String keywords = queryParams.getFirst(IQueryManager.SEARCH_TYPE_KEYWORDS_KW);
224 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(queryParams);
225 ServiceGroupDocumentModelHandler handler = (ServiceGroupDocumentModelHandler)
226 createDocumentHandler(ctx);
227 ArrayList<String> groupsList = null;
228 if("common".equalsIgnoreCase(serviceGroupName)) {
229 groupsList = ServiceBindingUtils.getCommonServiceTypes();
231 groupsList = new ArrayList<String>();
232 groupsList.add(serviceGroupName);
234 // set up a keyword search
235 if (keywords != null && !keywords.isEmpty()) {
236 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
237 if(Tools.isEmpty(whereClause)) {
238 if (logger.isDebugEnabled()) {
239 logger.debug("The WHERE clause is empty for keywords: ["+keywords+"]");
242 DocumentFilter documentFilter = handler.getDocumentFilter();
243 documentFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND);
244 if (logger.isDebugEnabled()) {
245 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
249 list = handler.getItemsForGroup(ctx, groupsList);
250 } catch (Exception e) {
251 throw bigReThrow(e, ServiceMessages.READ_FAILED, serviceGroupName);