]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c5fbde75fbdfa451aec835d3814b53ad2f4f7038
[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.authorization;
25
26 import org.collectionspace.services.authorization.perms.Permission;
27 import org.collectionspace.services.authorization.storage.PermissionRoleDocumentHandler;
28
29 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
30 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
31 import org.collectionspace.services.common.context.ServiceContext;
32 import org.collectionspace.services.common.context.ServiceContextFactory;
33 import org.collectionspace.services.common.document.DocumentHandler;
34 import org.collectionspace.services.common.storage.StorageClient;
35 import org.collectionspace.services.common.storage.jpa.JpaRelationshipStorageClient;
36 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
37 import org.collectionspace.services.common.context.ServiceContextProperties;
38
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * PermissionRoleSubResource is used to manage permission-role relationship
44  * @author
45  */
46 public class PermissionRoleSubResource
47         extends AbstractCollectionSpaceResourceImpl<PermissionRole, PermissionRole> {
48
49     public final static String ROLE_PERMROLE_SERVICE = "authorization/roles/permroles";
50     public final static String PERMISSION_PERMROLE_SERVICE = "authorization/permissions/permroles";
51     //this service is never exposed as standalone RESTful service...just use unique
52     //service name to identify binding
53     /** The service name. */
54     private String serviceName = "authorization/permroles";
55     /** The logger. */
56     final Logger logger = LoggerFactory.getLogger(PermissionRoleSubResource.class);
57     /** The storage client. */
58     final StorageClient storageClient = new JpaRelationshipStorageClient<PermissionRole>();
59     //
60     private String permissionRoleCsid = null;
61
62     /**
63      * Instantiates a new permission role sub resource.
64      *
65      * @param serviceName the service name
66      */
67     public PermissionRoleSubResource(String serviceName) {
68         this.serviceName = serviceName;
69     }
70     
71     /* (non-Javadoc)
72      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
73      */
74     @Override
75     protected String getVersionString() {
76         /** The last change revision. */
77         final String lastChangeRevision = "$LastChangedRevision: 1165 $";
78         return lastChangeRevision;
79     }
80
81     /* (non-Javadoc)
82      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
83      */
84     @Override
85     public String getServiceName() {
86         return serviceName;
87     }
88
89     /* (non-Javadoc)
90      * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
91      */
92     @Override
93     public Class<PermissionRole> getCommonPartClass() {
94         return PermissionRole.class;
95     }
96
97     /* (non-Javadoc)
98      * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
99      */
100     @Override
101     public ServiceContextFactory<PermissionRole, PermissionRole> getServiceContextFactory() {
102         return RemoteServiceContextFactory.get();
103     }
104
105     /**
106      * Creates the service context.
107      * 
108      * @param input the input
109      * @param subject the subject
110      * 
111      * @return the service context< permission role, permission role>
112      * 
113      * @throws Exception the exception
114      */
115     private ServiceContext<PermissionRole, PermissionRole> createServiceContext(PermissionRole input,
116             SubjectType subject) throws Exception {
117         ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext(input);
118         ctx.setDocumentType(PermissionRole.class.getPackage().getName()); //persistence unit
119         ctx.setProperty(ServiceContextProperties.ENTITY_NAME, PermissionRoleRel.class.getName());
120         ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, PermissionRoleRel.class);
121         //subject name is necessary to indicate if role or permission is a subject
122         ctx.setProperty(ServiceContextProperties.SUBJECT, subject);
123         //set context for the relationship query
124         if (subject == SubjectType.ROLE) {
125             ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, Permission.class);
126             ctx.setProperty(ServiceContextProperties.OBJECT_ID, "permission_id");
127         } else if (subject == SubjectType.PERMISSION) {
128             ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, Role.class);
129             ctx.setProperty(ServiceContextProperties.OBJECT_ID, "role_id");
130         }
131         return ctx;
132     }
133
134     /* (non-Javadoc)
135      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
136      */
137     @Override
138     public StorageClient getStorageClient(ServiceContext<PermissionRole, PermissionRole> ctx) {
139         //FIXME use ctx to identify storage client
140         return storageClient;
141     }
142
143 //    @Override
144 //    public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
145 //        DocumentHandler docHandler = ctx.getDocumentHandler();
146 //        docHandler.setCommonPart(ctx.getInput());
147 //        return docHandler;
148 //    }
149     /**
150      * createPermissionRole creates one or more permission-role relationships
151      * between object (permission/role) and subject (role/permission)
152      * @param input
153      * @param subject
154      * @return
155      * @throws Exception
156      */
157     public String createPermissionRole(PermissionRole input, SubjectType subject)
158             throws Exception {
159
160         ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext(input, subject);
161         DocumentHandler handler = createDocumentHandler(ctx);
162         return getStorageClient(ctx).create(ctx, handler);
163     }
164
165     /**
166      * Gets the permission role rel.
167      *
168      * @param csid the csid
169      * @param subject the subject
170      * @param permissionRoleCsid the permission role csid
171      * @return the permission role rel
172      * @throws Exception the exception
173      */
174     public PermissionRoleRel getPermissionRoleRel(String csid,
175                 SubjectType subject,
176                 String permissionRoleCsid) throws Exception {
177
178         if (logger.isDebugEnabled()) {
179             logger.debug("getAccountRole with csid=" + csid);
180         }
181 //        AccountRolesList result = new AccountRolesList();
182         ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext((PermissionRole) null, subject);
183         PermissionRoleDocumentHandler handler = (PermissionRoleDocumentHandler)createDocumentHandler(ctx);
184         handler.setPermissionRoleCsid(permissionRoleCsid);
185         //getStorageClient(ctx).get(ctx, csid, handler);
186         PermissionRoleRel permissionRoleRel = (PermissionRoleRel)JpaStorageUtils.getEntity(
187                         new Long(permissionRoleCsid).longValue(), PermissionRoleRel.class);
188 //        List<AccountRoleListItem> accountRoleList = result.getAccountRoleListItems();
189 //        AccountRoleListItem listItem = new AccountRoleListItem();
190 //        // fill the item
191 //        listItem.setCsid(accountRoleRel.getHjid().toString());
192 //        listItem.setRoleId(accountRoleRel.getRoleId());
193 //        listItem.setRoleName(accountRoleRel.getRoleName());
194         // add item to result list
195 //        result = (AccountRolesList) ctx.getOutput();
196         
197         return permissionRoleRel;
198     }
199     
200     /**
201      * getPermissionRole retrieves permission-role relationships using given
202      * csid of object (permission/role) and subject (role/permission)
203      * @param csid
204      * @param subject
205      * @return
206      * @throws Exception
207      */
208     public PermissionRole getPermissionRole(
209             String csid, SubjectType subject) throws Exception {
210
211         if (logger.isDebugEnabled()) {
212             logger.debug("getPermissionRole with csid=" + csid);
213         }
214         PermissionRole result = null;
215         ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext((PermissionRole) null, subject);
216         DocumentHandler handler = createDocumentHandler(ctx);
217         getStorageClient(ctx).get(ctx, csid, handler);
218         result = (PermissionRole) ctx.getOutput();
219
220         return result;
221     }
222
223     /**
224      * deletePermissionRole deletes permission-role relationships using given
225      * csid of object (permission/role) and subject (role/permission)
226      * @param csid
227      * @param subject
228      * @return
229      * @throws Exception
230      */
231     public void deletePermissionRole(String csid,
232             SubjectType subject) throws Exception {
233
234         if (logger.isDebugEnabled()) {
235             logger.debug("deletePermissionRole with csid=" + csid);
236         }
237         PermissionRole permRole = this.getPermissionRole(csid, subject);
238         this.deletePermissionRole(csid, subject, permRole);
239     }
240
241     /**
242      * deletePermissionRole deletes permission-role relationships using given
243      * csid of object (permission/role) and subject (role/permission)
244      * @param csid
245      * @param subject
246      * @param input with role and permission relationships to delete
247      * @return
248      * @throws Exception
249      */
250     public void deletePermissionRole(String csid,
251             SubjectType subject, PermissionRole input) throws Exception {
252
253         if (logger.isDebugEnabled()) {
254             logger.debug("deletePermissionRole(input) with csid=" + csid);
255         }
256         ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext(input, subject);
257         DocumentHandler handler = createDocumentHandler(ctx);
258         getStorageClient(ctx).delete(ctx, csid, handler);
259     }
260 }