]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
6303557421254c08e00dc41ebecab45b7a9c5704
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.servicegroup;
25
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29
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.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.ServiceBindingUtils;
45 import org.collectionspace.services.common.context.ServiceContext;
46 import org.collectionspace.services.common.context.ServiceContextFactory;
47 import org.collectionspace.services.common.document.DocumentFilter;
48 import org.collectionspace.services.common.query.QueryManager;
49 import org.collectionspace.services.config.service.ServiceBindingType;
50 import org.collectionspace.services.config.service.ServiceObjectType;
51 import org.collectionspace.services.nuxeo.client.java.CommonList;
52 import org.collectionspace.services.servicegroup.nuxeo.ServiceGroupDocumentModelHandler;
53
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
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;
66
67 @Path(ServiceGroupClient.SERVICE_PATH)
68 @Produces({"application/xml"})
69 @Consumes({"application/xml"})
70 public class ServiceGroupResource extends AbstractCollectionSpaceResourceImpl<PoxPayloadIn, PoxPayloadOut> {
71
72     protected final Logger logger = LoggerFactory.getLogger(this.getClass());
73     
74     private final static boolean EXCLUDE_AUTHORITIES = false;
75     private final static boolean INCLUDE_AUTHORITIES = true;
76     
77     @Override
78     public String getServiceName(){
79         return ServiceGroupClient.SERVICE_NAME;
80     }
81
82     public String getServicePathComponent(){
83         return ServiceGroupClient.SERVICE_NAME.toLowerCase();
84     }
85
86     @Override
87     protected String getVersionString() {
88         final String lastChangeRevision = "$LastChangedRevision: 2108 $";
89         return lastChangeRevision;
90     }
91
92     @Override
93     //public Class<ServicegroupsCommon> getCommonPartClass() {
94     public Class<?> getCommonPartClass() {
95         try {
96             return Class.forName("org.collectionspace.services.servicegroup.ServicegroupsCommon");//.class;
97         } catch (ClassNotFoundException e){
98             return null;
99         }
100     }
101
102     @Override
103     public ServiceContextFactory<PoxPayloadIn, PoxPayloadOut> getServiceContextFactory() {
104         return MultipartServiceContextFactory.get();
105     }
106
107
108     //======================= GET without specifier: List  =====================================
109     @GET
110     public AbstractCommonList getList(@Context UriInfo ui) {
111         try {
112             CommonList commonList = new CommonList();
113             AbstractCommonList list = (AbstractCommonList)commonList;
114                 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
115                 String commonSchema = ctx.getCommonPartLabel();
116                 ArrayList<String> svcGroups = new ArrayList<String>();
117                 svcGroups.add("procedure");
118                 svcGroups.add("object");
119                 svcGroups.add("authority");
120                 // Fetch the list of groups from the tenant-bindings config, and prepare a list item
121                 // for each one.
122                 // We always declare this a full list, of the size that we are returning. 
123                 // Not quite in the spirit of what paging means, but tells callers not to ask for more.
124                 list.setPageNum(0);
125                 list.setPageSize(svcGroups.size());
126                 list.setItemsInPage(svcGroups.size());
127                 list.setTotalItems(svcGroups.size());
128                 String fields[] = new String[2];
129                 fields[0] = ServiceGroupListItemJAXBSchema.NAME;
130                 fields[1] = ServiceGroupListItemJAXBSchema.URI;
131                 commonList.setFieldsReturned(fields);
132                         HashMap<String, Object> item = new HashMap<String, Object>();
133                 for(String groupName:svcGroups){
134                     item.put(ServiceGroupListItemJAXBSchema.NAME, groupName);
135                     String uri = "/" + getServiceName().toLowerCase() + "/" + groupName;
136                     item.put(ServiceGroupListItemJAXBSchema.URI, uri);
137                     commonList.addItem(item);
138                     item.clear();
139                 }
140                 return list;
141         } catch (Exception e) {
142             throw bigReThrow(e, ServiceMessages.LIST_FAILED);
143         }
144         
145     }
146
147     
148     //======================= GET ====================================================
149     // NOTE that csid is not a good name for the specifier, but if we name it anything else, 
150     // our AuthZ gets confused!!!
151     @GET
152     @Path("{csid}")
153     public byte[] get(
154             @Context UriInfo ui,
155             @PathParam("csid") String groupname) {
156         PoxPayloadOut result = null;
157         ensureCSID(groupname, NuxeoBasedResource.READ);
158         try {
159                 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
160             TenantBindingConfigReaderImpl tReader =
161                     ServiceMain.getInstance().getTenantBindingConfigReader();
162             // We need to get all the procedures, authorities, and objects.
163                 ArrayList<String> groupsList = null;  
164                 if("common".equalsIgnoreCase(groupname)) {
165                         groupsList = ServiceBindingUtils.getCommonServiceTypes(INCLUDE_AUTHORITIES);
166                 } else {
167                         groupsList = new ArrayList<String>();
168                         groupsList.add(groupname);
169                 }
170             List<ServiceBindingType> servicebindings = tReader.getServiceBindingsByType(ctx.getTenantId(), groupsList);
171             if (servicebindings == null || servicebindings.isEmpty()) {
172                 // 404 if there are no mappings.
173                 Response response = Response.status(Response.Status.NOT_FOUND).entity(
174                         ServiceMessages.READ_FAILED + ServiceMessages.resourceNotFoundMsg(groupname)).type("text/plain").build();
175                 throw new CSWebApplicationException(response);
176             }
177                 //Otherwise, build the response with a list
178             ServicegroupsCommon common = new ServicegroupsCommon();
179             common.setName(groupname);
180             String uri = "/" + getServicePathComponent() + "/" + groupname;
181             common.setUri(uri);
182             result = new PoxPayloadOut(getServicePathComponent());
183             result.addPart("ServicegroupsCommon", common);
184             
185                 ServicegroupsCommon.HasDocTypes wrapper = common.getHasDocTypes();
186                 if(wrapper==null) {
187                         wrapper = new ServicegroupsCommon.HasDocTypes();
188                         common.setHasDocTypes(wrapper);
189                 }
190                 List<String> hasDocTypes = wrapper.getHasDocType();
191                 for(ServiceBindingType binding:servicebindings) {
192                         ServiceObjectType serviceObj = binding.getObject();
193                         if(serviceObj!=null) {
194                         String docType = serviceObj.getName();
195                         hasDocTypes.add(docType);
196                         }
197                 }
198         } catch (Exception e) {
199             throw bigReThrow(e, ServiceMessages.READ_FAILED, groupname);
200         }
201
202         return result.getBytes();
203     }
204
205
206     @GET
207     @Path("{csid}/items")
208     public AbstractCommonList getItems(
209             @Context UriInfo ui,
210             @PathParam("csid") String serviceGroupName) {
211         ensureCSID(serviceGroupName, NuxeoBasedResource.READ);
212         AbstractCommonList list = null;
213         try {
214             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(ui);
215                 ServiceGroupDocumentModelHandler handler = (ServiceGroupDocumentModelHandler)
216                                                 createDocumentHandler(ctx);
217                 ArrayList<String> groupsList = null;  
218                 if("common".equalsIgnoreCase(serviceGroupName)) {
219                         groupsList = ServiceBindingUtils.getCommonServiceTypes(INCLUDE_AUTHORITIES);
220                 } else {
221                         groupsList = new ArrayList<String>();
222                         groupsList.add(serviceGroupName);
223                 }
224                 // set up a keyword search
225             MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
226             String keywords = queryParams.getFirst(IQueryManager.SEARCH_TYPE_KEYWORDS_KW);
227                 if (keywords != null && !keywords.isEmpty()) {
228                     String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
229                     if(Tools.isEmpty(whereClause)) {
230                         if (logger.isDebugEnabled()) {
231                                 logger.debug("The WHERE clause is empty for keywords: ["+keywords+"]");
232                         }
233                     } else {
234                             DocumentFilter documentFilter = handler.getDocumentFilter();
235                             documentFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND);
236                             if (logger.isDebugEnabled()) {
237                                 logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
238                             }
239                     }
240                 }
241             list = handler.getItemsForGroup(ctx, groupsList);
242         } catch (Exception e) {
243             throw bigReThrow(e, ServiceMessages.READ_FAILED, serviceGroupName);
244         }
245
246         return list;
247     }
248
249
250 }