]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
648ec8d5717eed2191dbb2314d722608cdfaa79c
[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.RemoteServiceContextFactory;
43 import org.collectionspace.services.common.context.ServiceContext;
44 import org.collectionspace.services.common.context.ServiceContextFactory;
45 import org.collectionspace.services.common.document.DocumentFilter;
46 import org.collectionspace.services.common.document.DocumentHandler;
47 import org.collectionspace.services.common.document.DocumentNotFoundException;
48 import org.collectionspace.services.common.query.QueryManager;
49 import org.collectionspace.services.common.service.ServiceBindingType;
50 import org.collectionspace.services.common.service.ServiceObjectType;
51 import org.collectionspace.services.nuxeo.client.java.CommonList;
52 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
53 import org.nuxeo.ecm.core.api.DocumentModel;
54 import org.nuxeo.ecm.core.api.DocumentModelList;
55
56 import javax.ws.rs.Consumes;
57 import javax.ws.rs.GET;
58 import javax.ws.rs.Path;
59 import javax.ws.rs.PathParam;
60 import javax.ws.rs.Produces;
61 import javax.ws.rs.WebApplicationException;
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 {
71         
72     @Override
73     public String getServiceName(){
74         return ServiceGroupClient.SERVICE_NAME;
75     }
76
77     public String getServicePathComponent(){
78         return ServiceGroupClient.SERVICE_NAME.toLowerCase();
79     }
80
81     @Override
82     protected String getVersionString() {
83         final String lastChangeRevision = "$LastChangedRevision: 2108 $";
84         return lastChangeRevision;
85     }
86
87     @Override
88     //public Class<ServicegroupsCommon> getCommonPartClass() {
89     public Class getCommonPartClass() {
90         try {
91             return Class.forName("org.collectionspace.services.servicegroup.ServicegroupsCommon");//.class;
92         } catch (ClassNotFoundException e){
93             return null;
94         }
95     }
96
97     @Override
98     public ServiceContextFactory<PoxPayloadIn, PoxPayloadOut> getServiceContextFactory() {
99         return RemoteServiceContextFactory.get();
100     }
101
102
103     //======================= GET ====================================================
104     // NOTE that csid is not a good name for the specifier, but if we name it anything else, 
105     // our AuthZ gets confused!!!
106     @GET
107     @Path("{csid}")
108     public byte[] get(
109             @Context UriInfo ui,
110             @PathParam("csid") String groupname) {
111         PoxPayloadOut result = null;
112         ensureCSID(groupname, ResourceBase.READ);
113         try {
114                 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
115             TenantBindingConfigReaderImpl tReader =
116                     ServiceMain.getInstance().getTenantBindingConfigReader();
117             // We need to get all the procedures, authorities, and objects.
118             List<ServiceBindingType> servicebindings = tReader.getServiceBindingsByType(ctx.getTenantId(), groupname);
119             if (servicebindings == null || servicebindings.isEmpty()) {
120                 // 404 if there are no mappings.
121                 Response response = Response.status(Response.Status.NOT_FOUND).entity(
122                         ServiceMessages.READ_FAILED + ServiceMessages.resourceNotFoundMsg(groupname)).type("text/plain").build();
123                 throw new WebApplicationException(response);
124             }
125                 //Otherwise, build the response with a list
126             ServicegroupsCommon common = new ServicegroupsCommon();
127             common.setName(groupname);
128             String uri = "/" + getServicePathComponent() + "/" + groupname;
129             common.setUri(uri);
130             result = new PoxPayloadOut(getServicePathComponent());
131             result.addPart("ServicegroupsCommon", common);
132             
133                 ServicegroupsCommon.HasDocTypes wrapper = common.getHasDocTypes();
134                 if(wrapper==null) {
135                         wrapper = new ServicegroupsCommon.HasDocTypes();
136                         common.setHasDocTypes(wrapper);
137                 }
138                 List<String> hasDocTypes = wrapper.getHasDocType();
139                 for(ServiceBindingType binding:servicebindings) {
140                         ServiceObjectType serviceObj = binding.getObject();
141                         if(serviceObj!=null) {
142                         String docType = serviceObj.getName();
143                         hasDocTypes.add(docType);
144                         }
145                 }
146         } catch (Exception e) {
147             throw bigReThrow(e, ServiceMessages.READ_FAILED, groupname);
148         }
149
150         return result.getBytes();
151     }
152
153
154     //======================= GET without specifier: List  =====================================
155     @GET
156     public AbstractCommonList getList(@Context UriInfo ui) {
157         try {
158             CommonList commonList = new CommonList();
159             AbstractCommonList list = (AbstractCommonList)commonList;
160                 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
161                 String commonSchema = ctx.getCommonPartLabel();
162                 ArrayList<String> svcGroups = new ArrayList<String>();
163                 svcGroups.add("procedure");
164                 svcGroups.add("object");
165                 svcGroups.add("authority");
166                 // Fetch the list of groups from the tenant-bindings config, and prepare a list item
167                 // for each one.
168                 // We always declare this a full list, of the size that we are returning. 
169                 // Not quite in the spirit of what paging means, but tells callers not to ask for more.
170                 list.setPageNum(0);
171                 list.setPageSize(svcGroups.size());
172                 list.setItemsInPage(svcGroups.size());
173                 list.setTotalItems(svcGroups.size());
174                 String fields[] = new String[2];
175                 fields[0] = ServiceGroupListItemJAXBSchema.NAME;
176                 fields[1] = ServiceGroupListItemJAXBSchema.URI;
177                 commonList.setFieldsReturned(fields);
178                         HashMap<String,String> item = new HashMap<String,String>();
179                 for(String groupName:svcGroups){
180                     item.put(ServiceGroupListItemJAXBSchema.NAME, groupName);
181                     String uri = "/" + getServiceName().toLowerCase() + "/" + groupName;
182                     item.put(ServiceGroupListItemJAXBSchema.URI, uri);
183                     commonList.addItem(item);
184                     item.clear();
185                 }
186                 return list;
187         } catch (Exception e) {
188             throw bigReThrow(e, ServiceMessages.LIST_FAILED);
189         }
190         
191     }
192
193     
194 }