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;
86 private final static boolean INCLUDE_AUTHORITIES = true;
89 public String getServiceName(){
90 return ServiceGroupClient.SERVICE_NAME;
93 public String getServicePathComponent(){
94 return ServiceGroupClient.SERVICE_NAME.toLowerCase();
98 protected String getVersionString() {
99 final String lastChangeRevision = "$LastChangedRevision: 2108 $";
100 return lastChangeRevision;
104 //public Class<ServicegroupsCommon> getCommonPartClass() {
105 public Class getCommonPartClass() {
107 return Class.forName("org.collectionspace.services.servicegroup.ServicegroupsCommon");//.class;
108 } catch (ClassNotFoundException e){
114 public ServiceContextFactory<PoxPayloadIn, PoxPayloadOut> getServiceContextFactory() {
115 return MultipartServiceContextFactory.get();
119 //======================= GET without specifier: List =====================================
121 public AbstractCommonList getList(@Context UriInfo ui) {
123 CommonList commonList = new CommonList();
124 AbstractCommonList list = (AbstractCommonList)commonList;
125 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
126 String commonSchema = ctx.getCommonPartLabel();
127 ArrayList<String> svcGroups = new ArrayList<String>();
128 svcGroups.add("procedure");
129 svcGroups.add("object");
130 svcGroups.add("authority");
131 // Fetch the list of groups from the tenant-bindings config, and prepare a list item
133 // We always declare this a full list, of the size that we are returning.
134 // Not quite in the spirit of what paging means, but tells callers not to ask for more.
136 list.setPageSize(svcGroups.size());
137 list.setItemsInPage(svcGroups.size());
138 list.setTotalItems(svcGroups.size());
139 String fields[] = new String[2];
140 fields[0] = ServiceGroupListItemJAXBSchema.NAME;
141 fields[1] = ServiceGroupListItemJAXBSchema.URI;
142 commonList.setFieldsReturned(fields);
143 HashMap<String, Object> item = new HashMap<String, Object>();
144 for(String groupName:svcGroups){
145 item.put(ServiceGroupListItemJAXBSchema.NAME, groupName);
146 String uri = "/" + getServiceName().toLowerCase() + "/" + groupName;
147 item.put(ServiceGroupListItemJAXBSchema.URI, uri);
148 commonList.addItem(item);
152 } catch (Exception e) {
153 throw bigReThrow(e, ServiceMessages.LIST_FAILED);
159 //======================= GET ====================================================
160 // NOTE that csid is not a good name for the specifier, but if we name it anything else,
161 // our AuthZ gets confused!!!
166 @PathParam("csid") String groupname) {
167 PoxPayloadOut result = null;
168 ensureCSID(groupname, ResourceBase.READ);
170 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
171 TenantBindingConfigReaderImpl tReader =
172 ServiceMain.getInstance().getTenantBindingConfigReader();
173 // We need to get all the procedures, authorities, and objects.
174 ArrayList<String> groupsList = null;
175 if("common".equalsIgnoreCase(groupname)) {
176 groupsList = ServiceBindingUtils.getCommonServiceTypes(EXCLUDE_AUTHORITIES); // CSPACE-5359: Excluding Authority type to stay backward compat with v2.4
178 groupsList = new ArrayList<String>();
179 groupsList.add(groupname);
181 List<ServiceBindingType> servicebindings = tReader.getServiceBindingsByType(ctx.getTenantId(), groupsList);
182 if (servicebindings == null || servicebindings.isEmpty()) {
183 // 404 if there are no mappings.
184 Response response = Response.status(Response.Status.NOT_FOUND).entity(
185 ServiceMessages.READ_FAILED + ServiceMessages.resourceNotFoundMsg(groupname)).type("text/plain").build();
186 throw new WebApplicationException(response);
188 //Otherwise, build the response with a list
189 ServicegroupsCommon common = new ServicegroupsCommon();
190 common.setName(groupname);
191 String uri = "/" + getServicePathComponent() + "/" + groupname;
193 result = new PoxPayloadOut(getServicePathComponent());
194 result.addPart("ServicegroupsCommon", common);
196 ServicegroupsCommon.HasDocTypes wrapper = common.getHasDocTypes();
198 wrapper = new ServicegroupsCommon.HasDocTypes();
199 common.setHasDocTypes(wrapper);
201 List<String> hasDocTypes = wrapper.getHasDocType();
202 for(ServiceBindingType binding:servicebindings) {
203 ServiceObjectType serviceObj = binding.getObject();
204 if(serviceObj!=null) {
205 String docType = serviceObj.getName();
206 hasDocTypes.add(docType);
209 } catch (Exception e) {
210 throw bigReThrow(e, ServiceMessages.READ_FAILED, groupname);
213 return result.getBytes();
218 @Path("{csid}/items")
219 public AbstractCommonList getItems(
221 @PathParam("csid") String serviceGroupName) {
222 ensureCSID(serviceGroupName, ResourceBase.READ);
223 AbstractCommonList list = null;
225 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
226 String keywords = queryParams.getFirst(IQueryManager.SEARCH_TYPE_KEYWORDS_KW);
227 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(queryParams);
228 ServiceGroupDocumentModelHandler handler = (ServiceGroupDocumentModelHandler)
229 createDocumentHandler(ctx);
230 ArrayList<String> groupsList = null;
231 if("common".equalsIgnoreCase(serviceGroupName)) {
232 groupsList = ServiceBindingUtils.getCommonServiceTypes(EXCLUDE_AUTHORITIES); //CSPACE-5359: Exclude authorities to remain backward compat with v2.4
234 groupsList = new ArrayList<String>();
235 groupsList.add(serviceGroupName);
237 // set up a keyword search
238 if (keywords != null && !keywords.isEmpty()) {
239 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
240 if(Tools.isEmpty(whereClause)) {
241 if (logger.isDebugEnabled()) {
242 logger.debug("The WHERE clause is empty for keywords: ["+keywords+"]");
245 DocumentFilter documentFilter = handler.getDocumentFilter();
246 documentFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND);
247 if (logger.isDebugEnabled()) {
248 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
252 list = handler.getItemsForGroup(ctx, groupsList);
253 } catch (Exception e) {
254 throw bigReThrow(e, ServiceMessages.READ_FAILED, serviceGroupName);