]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
7f31172f11626b1822148260ecf3cd4bbcefe9c2
[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         ctx.setDocumentType(PermissionRole.class.getPackage().getName()); //persistence unit
100         ctx.setProperty("entity-name", PermissionRoleRel.class.getName());
101         //subject name is necessary to indicate if role or permission is a subject
102         ctx.setProperty("subject", subject);
103         //set context for the relationship query
104         ctx.setProperty("object-class", Permission.class);
105         ctx.setProperty("object-id", "permission_id");
106         return ctx;
107     }
108
109     /* (non-Javadoc)
110      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
111      */
112     @Override
113     public StorageClient getStorageClient(ServiceContext<PermissionRole, PermissionRole> ctx) {
114         //FIXME use ctx to identify storage client
115         return storageClient;
116     }
117
118 //    @Override
119 //    public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
120 //        DocumentHandler docHandler = ctx.getDocumentHandler();
121 //        docHandler.setCommonPart(ctx.getInput());
122 //        return docHandler;
123 //    }
124     /**
125      * createPermissionRole creates one or more permission-role relationships
126      * between object (permission/role) and subject (role/permission)
127      * @param input
128      * @param subject
129      * @return
130      * @throws Exception
131      */
132     public String createPermissionRole(PermissionRole input, SubjectType subject)
133             throws Exception {
134
135         ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext(input, subject);
136         DocumentHandler handler = createDocumentHandler(ctx);
137         return getStorageClient(ctx).create(ctx, handler);
138     }
139
140     /**
141      * getPermissionRole retrieves permission-role relationships using given
142      * csid of object (permission/role) and subject (role/permission)
143      * @param csid
144      * @param subject
145      * @return
146      * @throws Exception
147      */
148     public PermissionRole getPermissionRole(
149             String csid, SubjectType subject) throws Exception {
150
151         if (logger.isDebugEnabled()) {
152             logger.debug("getPermissionRole with csid=" + csid);
153         }
154         PermissionRole result = null;
155         ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext((PermissionRole) null, subject);
156         DocumentHandler handler = createDocumentHandler(ctx);
157         getStorageClient(ctx).get(ctx, csid, handler);
158         result = (PermissionRole) ctx.getOutput();
159
160         return result;
161     }
162
163     /**
164      * deletePermissionRole deletes permission-role relationships using given
165      * csid of object (permission/role) and subject (role/permission)
166      * @param csid
167      * @param subject
168      * @return
169      * @throws Exception
170      */
171     public void deletePermissionRole(String csid,
172             SubjectType subject) throws Exception {
173
174         if (logger.isDebugEnabled()) {
175             logger.debug("deletePermissionRole with csid=" + csid);
176         }
177         ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext((PermissionRole) null, subject);
178         getStorageClient(ctx).delete(ctx, csid);
179     }
180 }