]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9d3dce195650dfba7a2ad4cae2d5aaac37955d2d
[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.account;
25
26 import java.util.List;
27 import java.util.ArrayList;
28
29 import javax.persistence.PersistenceException;
30
31 import org.collectionspace.authentication.AuthN;
32 import org.collectionspace.services.account.storage.AccountRoleDocumentHandler;
33 import org.collectionspace.services.authorization.AccountRole;
34 import org.collectionspace.services.authorization.AccountRoleRel;
35 import org.collectionspace.services.authorization.Role;
36 import org.collectionspace.services.authorization.RoleValue;
37 import org.collectionspace.services.authorization.SubjectType;
38
39 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
40 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
41 import org.collectionspace.services.common.context.ServiceContext;
42 import org.collectionspace.services.common.context.ServiceContextFactory;
43 import org.collectionspace.services.common.document.DocumentHandler;
44 import org.collectionspace.services.common.storage.StorageClient;
45 import org.collectionspace.services.common.storage.jpa.JPATransactionContext;
46 import org.collectionspace.services.common.storage.jpa.JpaRelationshipStorageClient;
47 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
48 import org.collectionspace.services.common.context.ServiceContextProperties;
49
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 /**
54  * AccountRoleSubResource is used to manage account-role relationship
55  * @author
56  */
57 @SuppressWarnings("rawtypes")
58 public class AccountRoleSubResource
59 //        extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRolesList> {
60         extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRole> {
61         
62     final public static String ACCOUNT_ACCOUNTROLE_SERVICE = "accounts/accountroles";
63     final public static String ROLE_ACCOUNTROLE_SERVICE = "authorization/roles/accountroles";
64     //this service is never exposed as standalone RESTful service...just use unique
65     //service name to identify binding
66     /** The service name. */
67     private String serviceName = ACCOUNT_ACCOUNTROLE_SERVICE;
68     /** The logger. */
69     final Logger logger = LoggerFactory.getLogger(AccountRoleSubResource.class);
70     /** The storage client. */
71     final StorageClient storageClient = new JpaRelationshipStorageClient<AccountRole>();
72
73     /**
74      *
75      * @param serviceName qualified service path
76      */
77     public AccountRoleSubResource(String serviceName) {
78         this.serviceName = serviceName;
79     }
80
81     /* (non-Javadoc)
82      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
83      */
84     @Override
85     protected String getVersionString() {
86         /** The last change revision. */
87         final String lastChangeRevision = "$LastChangedRevision: 1165 $";
88         return lastChangeRevision;
89     }
90
91     /* (non-Javadoc)
92      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
93      */
94     @Override
95     public String getServiceName() {
96         return serviceName;
97     }
98
99     /* (non-Javadoc)
100      * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
101      */
102     @Override
103     public Class<AccountRole> getCommonPartClass() {
104         return AccountRole.class;
105     }
106
107     /* (non-Javadoc)
108      * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
109      */
110     @SuppressWarnings("unchecked")
111         @Override
112     public ServiceContextFactory<AccountRole, AccountRole> getServiceContextFactory() {
113         return RemoteServiceContextFactory.get();
114     }
115
116     /**
117      * Creates the service context.
118      * 
119      * @param input the input
120      * @param subject the subject
121      * 
122      * @return the service context< account role, account role>
123      * 
124      * @throws Exception the exception
125      */
126     private ServiceContext<AccountRole, AccountRole> createServiceContext(
127                 ServiceContext parentCtx, 
128                 AccountRole input,
129             SubjectType subject) throws Exception {
130         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input);
131         JPATransactionContext parentTransactionContext = parentCtx != null ? (JPATransactionContext)parentCtx.getCurrentTransactionContext() : null;
132         //
133         // If the parent context has an active JPA connection then we'll use it.
134         //
135         if (parentTransactionContext != null) {
136                 ctx.setTransactionContext(parentTransactionContext);
137         }
138         //
139         // Set other context values
140         //
141         ctx.setDocumentType(AccountRole.class.getPackage().getName()); //persistence unit
142         ctx.setProperty(ServiceContextProperties.ENTITY_NAME, AccountRoleRel.class.getName());
143         ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, AccountRoleRel.class);
144         //subject name is necessary to indicate if role or account is a subject
145         ctx.setProperty(ServiceContextProperties.SUBJECT, subject);
146         
147         //set context for the relationship query
148         if (subject == SubjectType.ROLE) {
149             ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, AccountsCommon.class);
150             ctx.setProperty(ServiceContextProperties.OBJECT_ID, "account_id");
151         } else if (subject == SubjectType.ACCOUNT) {
152             ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, Role.class);
153             ctx.setProperty(ServiceContextProperties.OBJECT_ID, "role_id");
154         }
155         
156         return ctx;
157     }
158
159     /* (non-Javadoc)
160      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
161      */
162     @Override
163     public StorageClient getStorageClient(ServiceContext<AccountRole, AccountRole> ctx) {
164         //FIXME use ctx to identify storage client
165         return storageClient;
166     }
167
168     /**
169      * createAccountRole creates one or more account-role relationships
170      * between object (account/role) and subject (role/account)
171      * @param input
172      * @param subject
173      * @return
174      * @throws Exception
175      */
176     public String createAccountRole(ServiceContext parentCtx, AccountRole input, SubjectType subject)
177             throws Exception {
178         
179         //
180         // We need to associate every new account with the Spring Security Admin role so we can make
181         // changes to the Spring Security ACL tables.  The Spring Security Admin role has NO CollectionSpace
182         // specific permissions.  It is an internal/private role that service consumers and end-users NEVER see.
183         //
184         
185         //Preserve the original incoming list of roles
186         List<RoleValue> inputRoleValues = input.getRole();
187
188         //Change the role list to be just the Spring role
189         List<RoleValue> springRoles = new ArrayList<RoleValue>();
190         input.setRole(springRoles);
191         RoleValue springAdminRole = new RoleValue();
192         springRoles.add(springAdminRole);
193         springAdminRole.setRoleId(AuthN.ROLE_SPRING_ADMIN_ID);
194         springAdminRole.setRoleName(AuthN.ROLE_SPRING_ADMIN_NAME);
195
196         // The Spring role relationship may already exist, if it does then we'll get a PersistenceException that
197         // we'll just ignore.
198         try {
199                 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(parentCtx, input, subject);
200                 DocumentHandler handler = createDocumentHandler(ctx);        
201                 getStorageClient(ctx).create(ctx, handler);
202         } catch (PersistenceException e) {
203                 //If we get this exception, it means that the role relationship already exists, so
204                 //we can just ignore this exception.
205                 if (logger.isTraceEnabled() == true) {
206                         logger.trace(AuthN.ROLE_SPRING_ADMIN_NAME +
207                                         " relationship already exists for account: " +
208                                         input.getAccount().get(0).getAccountId(), e);
209                 }
210         }
211         
212         //
213         // Now we'll add the account relationships for the original incoming roles.
214         //
215         input.setRole(inputRoleValues);
216         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(parentCtx, input, subject);
217         DocumentHandler handler = createDocumentHandler(ctx);        
218         String bogusCsid = getStorageClient(ctx).create(ctx, handler);
219         
220         return bogusCsid;
221     }
222
223     /**
224      * getAccountRole retrieves account-role relationships using given
225      * csid of object (account/role) and subject (role/account)
226      * @param csid
227      * @param subject
228      * @return
229      * @throws Exception
230      */
231     public AccountRole getAccountRole(ServiceContext parentCtx,
232             String csid, SubjectType subject) throws Exception {
233
234         if (logger.isDebugEnabled()) {
235             logger.debug("getAccountRole with csid=" + csid);
236         }
237         AccountRole result = null;
238         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(parentCtx, (AccountRole) null, subject);
239         DocumentHandler handler = createDocumentHandler(ctx);
240         getStorageClient(ctx).get(ctx, csid, handler);
241         result = (AccountRole) ctx.getOutput();
242
243         return result;
244     }
245
246     /**
247      * Gets the account role.
248      *
249      * @param csid the csid
250      * @param subject the subject
251      * @param accountRoleCsid the account role csid
252      * @return the account role
253      * @throws Exception the exception
254      */
255     public AccountRoleRel getAccountRoleRel(
256                 ServiceContext parentCtx,
257                 String csid,
258                 SubjectType subject,
259                 String accountRoleCsid) throws Exception {
260
261         if (logger.isDebugEnabled()) {
262             logger.debug("getAccountRole with csid=" + csid);
263         }
264         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(parentCtx, (AccountRole)null, subject);
265         AccountRoleDocumentHandler handler = (AccountRoleDocumentHandler)createDocumentHandler(ctx);
266         handler.setAccountRoleCsid(accountRoleCsid);
267         AccountRoleRel accountRoleRel = (AccountRoleRel)JpaStorageUtils.getEntity(new Long(accountRoleCsid).longValue(), AccountRoleRel.class);
268         
269         return accountRoleRel;
270     }
271
272     /**
273      * X_delete account role.
274      *
275      * @param csid the csid
276      * @param subject the subject
277      * @throws Exception the exception
278      */
279     public void x_deleteAccountRole(
280                 ServiceContext parentCtx,
281                 String csid,
282             SubjectType subject) throws Exception {
283
284         if (logger.isDebugEnabled()) {
285             logger.debug("deleteAccountRole with csid=" + csid);
286         }
287         AccountRole toDelete = getAccountRole(parentCtx, csid, subject);
288         deleteAccountRole(parentCtx, csid, subject, toDelete);
289     }
290     
291     /**
292      * deleteAccountRole deletes all account-role relationships using given
293      * csid of object (account/role) and subject (role/account)
294      * @param csid of the object
295      * @param subject
296      * @return
297      * @throws Exception
298      */
299     public void deleteAccountRole(
300                 ServiceContext parentCtx,
301                 String csid,
302             SubjectType subject) throws Exception {
303
304         if (logger.isDebugEnabled()) {
305             logger.debug("deleteAccountRole with csid=" + csid);
306         }
307         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(parentCtx, (AccountRole) null, subject);
308         getStorageClient(ctx).delete(ctx, csid);
309     }
310
311     /**
312      * deleteAccountRole deletes given account-role relationships using given
313      * csid of object (account/role) and subject (role/account)
314      * @param csid of the object
315      * @param subject
316      * @param input with account role relationships to delete
317      * @return
318      * @throws Exception
319      */
320         public void deleteAccountRole(
321                 ServiceContext parentCtx,
322                 String csid, SubjectType subject, AccountRole input)
323             throws Exception {
324
325         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(parentCtx, input, subject);
326         DocumentHandler handler = createDocumentHandler(ctx);
327         getStorageClient(ctx).delete(ctx, csid, handler);
328     }
329 }