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