]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a59bd93fa145cd536dedcd61fd6591407c857aaa
[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
29 import org.collectionspace.services.ServiceGroupListItemJAXBSchema;
30 import org.collectionspace.services.client.IQueryManager;
31 import org.collectionspace.services.client.PoxPayloadIn;
32 import org.collectionspace.services.client.PoxPayloadOut;
33 import org.collectionspace.services.client.ServiceGroupClient;
34 import org.collectionspace.services.jaxb.AbstractCommonList;
35 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
36 import org.collectionspace.services.common.ResourceBase;
37 import org.collectionspace.services.common.ServiceMessages;
38 import org.collectionspace.services.common.api.Tools;
39 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
40 import org.collectionspace.services.common.context.ServiceContext;
41 import org.collectionspace.services.common.context.ServiceContextFactory;
42 import org.collectionspace.services.common.document.DocumentFilter;
43 import org.collectionspace.services.common.document.DocumentHandler;
44 import org.collectionspace.services.common.document.DocumentNotFoundException;
45 import org.collectionspace.services.common.query.QueryManager;
46 import org.collectionspace.services.nuxeo.client.java.CommonList;
47 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
48 import org.nuxeo.ecm.core.api.DocumentModel;
49 import org.nuxeo.ecm.core.api.DocumentModelList;
50
51 import javax.ws.rs.Consumes;
52 import javax.ws.rs.GET;
53 import javax.ws.rs.Path;
54 import javax.ws.rs.PathParam;
55 import javax.ws.rs.Produces;
56 import javax.ws.rs.WebApplicationException;
57 import javax.ws.rs.core.Context;
58 import javax.ws.rs.core.MultivaluedMap;
59 import javax.ws.rs.core.Response;
60 import javax.ws.rs.core.UriInfo;
61
62 @Path(ServiceGroupClient.SERVICE_PATH)
63 @Produces({"application/xml"})
64 @Consumes({"application/xml"})
65 public class ServiceGroupResource extends AbstractCollectionSpaceResourceImpl {
66         
67     @Override
68     public String getServiceName(){
69         return ServiceGroupClient.SERVICE_NAME;
70     }
71
72     public String getServicePathComponent(){
73         return ServiceGroupClient.SERVICE_NAME.toLowerCase();
74     }
75
76     @Override
77     protected String getVersionString() {
78         final String lastChangeRevision = "$LastChangedRevision: 2108 $";
79         return lastChangeRevision;
80     }
81
82     @Override
83     //public Class<ServicegroupsCommon> getCommonPartClass() {
84     public Class getCommonPartClass() {
85         try {
86             return Class.forName("org.collectionspace.services.servicegroup.ServicegroupsCommon");//.class;
87         } catch (ClassNotFoundException e){
88             return null;
89         }
90     }
91
92     @Override
93     public ServiceContextFactory<PoxPayloadIn, PoxPayloadOut> getServiceContextFactory() {
94         return RemoteServiceContextFactory.get();
95     }
96
97
98     //======================= GET ====================================================
99     // NOTE that csid is not a good name for the specifier, but if we name it anything else, 
100     // our AuthZ gets confused!!!
101     @GET
102     @Path("{csid}")
103     public byte[] get(
104             @Context UriInfo ui,
105             @PathParam("csid") String groupname) {
106         PoxPayloadOut result = null;
107         ensureCSID(groupname, ResourceBase.READ);
108         try {
109             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
110             ServicegroupsCommon common = new ServicegroupsCommon();
111             common.setName(groupname);
112             String uri = "/" + getServicePathComponent() + "/" + groupname;
113             common.setUri(uri);
114             result = new PoxPayloadOut(getServicePathComponent());
115             result.addPart("ServicegroupsCommon", common);
116             
117             /* If we cannot get the result, then...
118             if (result == null) {
119                 Response response = Response.status(Response.Status.NOT_FOUND).entity(
120                         ServiceMessages.READ_FAILED + ServiceMessages.resourceNotFoundMsg(csid)).type("text/plain").build();
121                 throw new WebApplicationException(response);
122             }
123             */
124         } catch (Exception e) {
125             throw bigReThrow(e, ServiceMessages.READ_FAILED, groupname);
126         }
127
128         return result.getBytes();
129     }
130
131
132     //======================= GET without specifier: List  =====================================
133     @GET
134     public AbstractCommonList getList(@Context UriInfo ui) {
135         try {
136             CommonList commonList = new CommonList();
137             AbstractCommonList list = (AbstractCommonList)commonList;
138                 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
139                 String commonSchema = ctx.getCommonPartLabel();
140                 ArrayList<String> svcGroups = new ArrayList<String>();
141                 svcGroups.add("procedure");
142                 svcGroups.add("object");
143                 svcGroups.add("authority");
144                 // Fetch the list of groups from the tenant-bindings config, and prepare a list item
145                 // for each one.
146                 // We always declare this a full list, of the size that we are returning. 
147                 // Not quite in the spirit of what paging means, but tells callers not to ask for more.
148                 list.setPageNum(0);
149                 list.setPageSize(svcGroups.size());
150                 list.setItemsInPage(svcGroups.size());
151                 list.setTotalItems(svcGroups.size());
152                 String fields[] = new String[2];
153                 fields[0] = ServiceGroupListItemJAXBSchema.NAME;
154                 fields[1] = ServiceGroupListItemJAXBSchema.URI;
155                 commonList.setFieldsReturned(fields);
156                         HashMap<String,String> item = new HashMap<String,String>();
157                 for(String groupName:svcGroups){
158                     item.put(ServiceGroupListItemJAXBSchema.NAME, groupName);
159                     String uri = "/" + getServiceName().toLowerCase() + "/" + groupName;
160                     item.put(ServiceGroupListItemJAXBSchema.URI, uri);
161                     commonList.addItem(item);
162                     item.clear();
163                 }
164                 return list;
165         } catch (Exception e) {
166             throw bigReThrow(e, ServiceMessages.LIST_FAILED);
167         }
168         
169     }
170
171     
172 }