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