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