]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
7361a90c3592bb11b5a6b1b3c620ad7c93fc4558
[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.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;
61
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;
71
72 @Path(ServiceGroupClient.SERVICE_PATH)
73 @Produces({"application/xml"})
74 @Consumes({"application/xml"})
75 public class ServiceGroupResource extends AbstractCollectionSpaceResourceImpl<PoxPayloadIn, PoxPayloadOut> {
76
77     protected final Logger logger = LoggerFactory.getLogger(this.getClass());
78     
79     private final static boolean EXCLUDE_AUTHORITIES = false;
80     private final static boolean INCLUDE_AUTHORITIES = true;
81     
82     @Override
83     public String getServiceName(){
84         return ServiceGroupClient.SERVICE_NAME;
85     }
86
87     public String getServicePathComponent(){
88         return ServiceGroupClient.SERVICE_NAME.toLowerCase();
89     }
90
91     @Override
92     protected String getVersionString() {
93         final String lastChangeRevision = "$LastChangedRevision: 2108 $";
94         return lastChangeRevision;
95     }
96
97     @Override
98     //public Class<ServicegroupsCommon> getCommonPartClass() {
99     public Class<?> getCommonPartClass() {
100         try {
101             return Class.forName("org.collectionspace.services.servicegroup.ServicegroupsCommon");//.class;
102         } catch (ClassNotFoundException e){
103             return null;
104         }
105     }
106
107     @Override
108     public ServiceContextFactory<PoxPayloadIn, PoxPayloadOut> getServiceContextFactory() {
109         return MultipartServiceContextFactory.get();
110     }
111
112
113     //======================= GET without specifier: List  =====================================
114     @GET
115     public AbstractCommonList getList(@Context UriInfo ui) {
116         try {
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
124                 // for each one.
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.
127                 list.setPageNum(0);
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);
141                     item.clear();
142                 }
143                 return list;
144         } catch (Exception e) {
145             throw bigReThrow(e, ServiceMessages.LIST_FAILED);
146         }
147         
148     }
149
150     
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!!!
154     @GET
155     @Path("{csid}")
156     public byte[] get(
157             @Context UriInfo ui,
158             @PathParam("csid") String groupname) {
159         PoxPayloadOut result = null;
160         ensureCSID(groupname, NuxeoBasedResource.READ);
161         try {
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);
169                 } else {
170                         groupsList = new ArrayList<String>();
171                         groupsList.add(groupname);
172                 }
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);
179             }
180                 //Otherwise, build the response with a list
181             ServicegroupsCommon common = new ServicegroupsCommon();
182             common.setName(groupname);
183             String uri = "/" + getServicePathComponent() + "/" + groupname;
184             common.setUri(uri);
185             result = new PoxPayloadOut(getServicePathComponent());
186             result.addPart("ServicegroupsCommon", common);
187             
188                 ServicegroupsCommon.HasDocTypes wrapper = common.getHasDocTypes();
189                 if(wrapper==null) {
190                         wrapper = new ServicegroupsCommon.HasDocTypes();
191                         common.setHasDocTypes(wrapper);
192                 }
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);
199                         }
200                 }
201         } catch (Exception e) {
202             throw bigReThrow(e, ServiceMessages.READ_FAILED, groupname);
203         }
204
205         return result.getBytes();
206     }
207
208     @GET
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;
216         try {
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);
223                 } else {
224                         groupsList = new ArrayList<String>();
225                         groupsList.add(serviceGroupName);
226                 }
227                 
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+"]");
236                         }
237                     } else {
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());
242                             }
243                     }
244                 } else {
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+"]");
252                                 }
253                             } else {
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());
258                                     }
259                             }
260                         }                       
261                 }
262                 
263                 // make the query
264             list = handler.getItemListForGroup(ctx, groupsList);
265         } catch (Exception e) {
266             throw bigReThrow(e, ServiceMessages.READ_FAILED, serviceGroupName);
267         }
268
269         return list;
270     }
271
272     @GET
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;
282         
283         try {
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);
290                 } else {
291                         groupsList = new ArrayList<String>();
292                         groupsList.add(serviceGroupName);
293                 }
294                 
295             String whereClause = QueryManager.createWhereClauseFromCsid(specifier);
296             DocumentFilter myFilter = new NuxeoDocumentFilter(whereClause, 0, 1);
297             handler.setDocumentFilter(myFilter);    
298                 
299             result = handler.getItemForCsid(ctx, groupsList, specifier);
300         } catch (Exception e) {
301             throw bigReThrow(e, ServiceMessages.READ_FAILED, serviceGroupName);
302         }
303
304         return result.getBytes();
305     }
306 }