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