]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
8caa93dce9d8d7de87a62672b25bb24f710d3e4b
[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.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;
66
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;
77
78 @Path(ServiceGroupClient.SERVICE_PATH)
79 @Produces({"application/xml"})
80 @Consumes({"application/xml"})
81 public class ServiceGroupResource extends AbstractCollectionSpaceResourceImpl {
82
83     protected final Logger logger = LoggerFactory.getLogger(this.getClass());
84     
85     private final static boolean EXCLUDE_AUTHORITIES = false;
86     private final static boolean INCLUDE_AUTHORITIES = true;
87     
88     @Override
89     public String getServiceName(){
90         return ServiceGroupClient.SERVICE_NAME;
91     }
92
93     public String getServicePathComponent(){
94         return ServiceGroupClient.SERVICE_NAME.toLowerCase();
95     }
96
97     @Override
98     protected String getVersionString() {
99         final String lastChangeRevision = "$LastChangedRevision: 2108 $";
100         return lastChangeRevision;
101     }
102
103     @Override
104     //public Class<ServicegroupsCommon> getCommonPartClass() {
105     public Class getCommonPartClass() {
106         try {
107             return Class.forName("org.collectionspace.services.servicegroup.ServicegroupsCommon");//.class;
108         } catch (ClassNotFoundException e){
109             return null;
110         }
111     }
112
113     @Override
114     public ServiceContextFactory<PoxPayloadIn, PoxPayloadOut> getServiceContextFactory() {
115         return MultipartServiceContextFactory.get();
116     }
117
118
119     //======================= GET without specifier: List  =====================================
120     @GET
121     public AbstractCommonList getList(@Context UriInfo ui) {
122         try {
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
132                 // for each one.
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.
135                 list.setPageNum(0);
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);
149                     item.clear();
150                 }
151                 return list;
152         } catch (Exception e) {
153             throw bigReThrow(e, ServiceMessages.LIST_FAILED);
154         }
155         
156     }
157
158     
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!!!
162     @GET
163     @Path("{csid}")
164     public byte[] get(
165             @Context UriInfo ui,
166             @PathParam("csid") String groupname) {
167         PoxPayloadOut result = null;
168         ensureCSID(groupname, ResourceBase.READ);
169         try {
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
177                 } else {
178                         groupsList = new ArrayList<String>();
179                         groupsList.add(groupname);
180                 }
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);
187             }
188                 //Otherwise, build the response with a list
189             ServicegroupsCommon common = new ServicegroupsCommon();
190             common.setName(groupname);
191             String uri = "/" + getServicePathComponent() + "/" + groupname;
192             common.setUri(uri);
193             result = new PoxPayloadOut(getServicePathComponent());
194             result.addPart("ServicegroupsCommon", common);
195             
196                 ServicegroupsCommon.HasDocTypes wrapper = common.getHasDocTypes();
197                 if(wrapper==null) {
198                         wrapper = new ServicegroupsCommon.HasDocTypes();
199                         common.setHasDocTypes(wrapper);
200                 }
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);
207                         }
208                 }
209         } catch (Exception e) {
210             throw bigReThrow(e, ServiceMessages.READ_FAILED, groupname);
211         }
212
213         return result.getBytes();
214     }
215
216
217     @GET
218     @Path("{csid}/items")
219     public AbstractCommonList getItems(
220             @Context UriInfo ui,
221             @PathParam("csid") String serviceGroupName) {
222         ensureCSID(serviceGroupName, ResourceBase.READ);
223         AbstractCommonList list = null;
224         try {
225             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(ui);
226                 ServiceGroupDocumentModelHandler handler = (ServiceGroupDocumentModelHandler)
227                                                 createDocumentHandler(ctx);
228                 ArrayList<String> groupsList = null;  
229                 if("common".equalsIgnoreCase(serviceGroupName)) {
230                         groupsList = ServiceBindingUtils.getCommonServiceTypes(EXCLUDE_AUTHORITIES); //CSPACE-5359: Exclude authorities to remain backward compat with v2.4
231                 } else {
232                         groupsList = new ArrayList<String>();
233                         groupsList.add(serviceGroupName);
234                 }
235                 // set up a keyword search
236             MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
237             String keywords = queryParams.getFirst(IQueryManager.SEARCH_TYPE_KEYWORDS_KW);
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+"]");
243                         }
244                     } else {
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());
249                             }
250                     }
251                 }
252             list = handler.getItemsForGroup(ctx, groupsList);
253         } catch (Exception e) {
254             throw bigReThrow(e, ServiceMessages.READ_FAILED, serviceGroupName);
255         }
256
257         return list;
258     }
259
260
261 }