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.ResourceBase;
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.RemoteServiceContextFactory;
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.DocumentException;
49 import org.collectionspace.services.common.document.DocumentFilter;
50 import org.collectionspace.services.common.document.DocumentHandler;
51 import org.collectionspace.services.common.document.DocumentNotFoundException;
52 import org.collectionspace.services.common.document.DocumentWrapper;
53 import org.collectionspace.services.common.query.QueryManager;
54 import org.collectionspace.services.common.repository.RepositoryClient;
55 import org.collectionspace.services.common.repository.RepositoryClientFactory;
56 import org.collectionspace.services.config.service.ServiceBindingType;
57 import org.collectionspace.services.config.service.ServiceObjectType;
58 import org.collectionspace.services.nuxeo.client.java.CommonList;
59 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
60 import org.collectionspace.services.servicegroup.nuxeo.ServiceGroupDocumentModelHandler;
61 import org.nuxeo.ecm.core.api.DocumentModel;
62 import org.nuxeo.ecm.core.api.DocumentModelList;
63 import org.nuxeo.ecm.core.api.model.PropertyException;
64 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
68 import javax.ws.rs.Consumes;
69 import javax.ws.rs.GET;
70 import javax.ws.rs.Path;
71 import javax.ws.rs.PathParam;
72 import javax.ws.rs.Produces;
73 import javax.ws.rs.WebApplicationException;
74 import javax.ws.rs.core.Context;
75 import javax.ws.rs.core.MultivaluedMap;
76 import javax.ws.rs.core.Response;
77 import javax.ws.rs.core.UriInfo;
79 @Path(ServiceGroupClient.SERVICE_PATH)
80 @Produces({"application/xml"})
81 @Consumes({"application/xml"})
82 public class ServiceGroupResource extends AbstractCollectionSpaceResourceImpl {
84 protected final Logger logger = LoggerFactory.getLogger(this.getClass());
86 private final static boolean EXCLUDE_AUTHORITIES = false;
87 private final static boolean INCLUDE_AUTHORITIES = true;
90 public String getServiceName(){
91 return ServiceGroupClient.SERVICE_NAME;
94 public String getServicePathComponent(){
95 return ServiceGroupClient.SERVICE_NAME.toLowerCase();
99 protected String getVersionString() {
100 final String lastChangeRevision = "$LastChangedRevision: 2108 $";
101 return lastChangeRevision;
105 //public Class<ServicegroupsCommon> getCommonPartClass() {
106 public Class getCommonPartClass() {
108 return Class.forName("org.collectionspace.services.servicegroup.ServicegroupsCommon");//.class;
109 } catch (ClassNotFoundException e){
115 public ServiceContextFactory<PoxPayloadIn, PoxPayloadOut> getServiceContextFactory() {
116 return MultipartServiceContextFactory.get();
120 //======================= GET without specifier: List =====================================
122 public AbstractCommonList getList(@Context UriInfo ui) {
124 CommonList commonList = new CommonList();
125 AbstractCommonList list = (AbstractCommonList)commonList;
126 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
127 String commonSchema = ctx.getCommonPartLabel();
128 ArrayList<String> svcGroups = new ArrayList<String>();
129 svcGroups.add("procedure");
130 svcGroups.add("object");
131 svcGroups.add("authority");
132 // Fetch the list of groups from the tenant-bindings config, and prepare a list item
134 // We always declare this a full list, of the size that we are returning.
135 // Not quite in the spirit of what paging means, but tells callers not to ask for more.
137 list.setPageSize(svcGroups.size());
138 list.setItemsInPage(svcGroups.size());
139 list.setTotalItems(svcGroups.size());
140 String fields[] = new String[2];
141 fields[0] = ServiceGroupListItemJAXBSchema.NAME;
142 fields[1] = ServiceGroupListItemJAXBSchema.URI;
143 commonList.setFieldsReturned(fields);
144 HashMap<String, Object> item = new HashMap<String, Object>();
145 for(String groupName:svcGroups){
146 item.put(ServiceGroupListItemJAXBSchema.NAME, groupName);
147 String uri = "/" + getServiceName().toLowerCase() + "/" + groupName;
148 item.put(ServiceGroupListItemJAXBSchema.URI, uri);
149 commonList.addItem(item);
153 } catch (Exception e) {
154 throw bigReThrow(e, ServiceMessages.LIST_FAILED);
160 //======================= GET ====================================================
161 // NOTE that csid is not a good name for the specifier, but if we name it anything else,
162 // our AuthZ gets confused!!!
167 @PathParam("csid") String groupname) {
168 PoxPayloadOut result = null;
169 ensureCSID(groupname, ResourceBase.READ);
171 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
172 TenantBindingConfigReaderImpl tReader =
173 ServiceMain.getInstance().getTenantBindingConfigReader();
174 // We need to get all the procedures, authorities, and objects.
175 ArrayList<String> groupsList = null;
176 if("common".equalsIgnoreCase(groupname)) {
177 groupsList = ServiceBindingUtils.getCommonServiceTypes(INCLUDE_AUTHORITIES);
179 groupsList = new ArrayList<String>();
180 groupsList.add(groupname);
182 List<ServiceBindingType> servicebindings = tReader.getServiceBindingsByType(ctx.getTenantId(), groupsList);
183 if (servicebindings == null || servicebindings.isEmpty()) {
184 // 404 if there are no mappings.
185 Response response = Response.status(Response.Status.NOT_FOUND).entity(
186 ServiceMessages.READ_FAILED + ServiceMessages.resourceNotFoundMsg(groupname)).type("text/plain").build();
187 throw new CSWebApplicationException(response);
189 //Otherwise, build the response with a list
190 ServicegroupsCommon common = new ServicegroupsCommon();
191 common.setName(groupname);
192 String uri = "/" + getServicePathComponent() + "/" + groupname;
194 result = new PoxPayloadOut(getServicePathComponent());
195 result.addPart("ServicegroupsCommon", common);
197 ServicegroupsCommon.HasDocTypes wrapper = common.getHasDocTypes();
199 wrapper = new ServicegroupsCommon.HasDocTypes();
200 common.setHasDocTypes(wrapper);
202 List<String> hasDocTypes = wrapper.getHasDocType();
203 for(ServiceBindingType binding:servicebindings) {
204 ServiceObjectType serviceObj = binding.getObject();
205 if(serviceObj!=null) {
206 String docType = serviceObj.getName();
207 hasDocTypes.add(docType);
210 } catch (Exception e) {
211 throw bigReThrow(e, ServiceMessages.READ_FAILED, groupname);
214 return result.getBytes();
219 @Path("{csid}/items")
220 public AbstractCommonList getItems(
222 @PathParam("csid") String serviceGroupName) {
223 ensureCSID(serviceGroupName, ResourceBase.READ);
224 AbstractCommonList list = null;
226 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(ui);
227 ServiceGroupDocumentModelHandler handler = (ServiceGroupDocumentModelHandler)
228 createDocumentHandler(ctx);
229 ArrayList<String> groupsList = null;
230 if("common".equalsIgnoreCase(serviceGroupName)) {
231 groupsList = ServiceBindingUtils.getCommonServiceTypes(INCLUDE_AUTHORITIES);
233 groupsList = new ArrayList<String>();
234 groupsList.add(serviceGroupName);
236 // set up a keyword search
237 MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
238 String keywords = queryParams.getFirst(IQueryManager.SEARCH_TYPE_KEYWORDS_KW);
239 if (keywords != null && !keywords.isEmpty()) {
240 String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
241 if(Tools.isEmpty(whereClause)) {
242 if (logger.isDebugEnabled()) {
243 logger.debug("The WHERE clause is empty for keywords: ["+keywords+"]");
246 DocumentFilter documentFilter = handler.getDocumentFilter();
247 documentFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND);
248 if (logger.isDebugEnabled()) {
249 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
253 list = handler.getItemsForGroup(ctx, groupsList);
254 } catch (Exception e) {
255 throw bigReThrow(e, ServiceMessages.READ_FAILED, serviceGroupName);