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