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