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