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