]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
4b05c1bd9d2de143c90b95df09042ee7e7762975
[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.services.account.storage.AccountRoleDocumentHandler;
32 //import org.collectionspace.services.authorization.AccountRolesList;
33 //import org.collectionspace.services.authorization.AccountRolesList.AccountRoleListItem;
34 import org.collectionspace.services.authorization.AccountRole;
35 import org.collectionspace.services.authorization.AccountRoleRel;
36 import org.collectionspace.services.authorization.Role;
37 import org.collectionspace.services.authorization.RoleValue;
38 import org.collectionspace.services.authorization.SubjectType;
39
40 import org.collectionspace.services.common.authorization_mgt.AuthorizationCommon;
41 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
42 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
43 import org.collectionspace.services.common.context.ServiceContext;
44 import org.collectionspace.services.common.context.ServiceContextFactory;
45 import org.collectionspace.services.common.document.DocumentHandler;
46 import org.collectionspace.services.common.storage.StorageClient;
47 import org.collectionspace.services.common.storage.jpa.JpaRelationshipStorageClient;
48 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
49 import org.collectionspace.services.common.context.ServiceContextProperties;
50
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 /**
55  * AccountRoleSubResource is used to manage account-role relationship
56  * @author
57  */
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     @Override
111     public ServiceContextFactory<AccountRole, AccountRole> getServiceContextFactory() {
112 //    public ServiceContextFactory<AccountRole, AccountRolesList> 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(AccountRole input,
127             SubjectType subject) throws Exception {
128         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input);
129         ctx.setDocumentType(AccountRole.class.getPackage().getName()); //persistence unit
130         ctx.setProperty(ServiceContextProperties.ENTITY_NAME, AccountRoleRel.class.getName());
131         ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, AccountRoleRel.class);
132         //subject name is necessary to indicate if role or account is a subject
133         ctx.setProperty(ServiceContextProperties.SUBJECT, subject);
134         
135         //set context for the relationship query
136         if (subject == SubjectType.ROLE) {
137             ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, AccountsCommon.class);
138             ctx.setProperty(ServiceContextProperties.OBJECT_ID, "account_id");
139         } else if (subject == SubjectType.ACCOUNT) {
140             ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, Role.class);
141             ctx.setProperty(ServiceContextProperties.OBJECT_ID, "role_id");
142         }
143         
144         return ctx;
145     }
146
147     /* (non-Javadoc)
148      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
149      */
150     @Override
151     public StorageClient getStorageClient(ServiceContext<AccountRole, AccountRole> ctx) {
152         //FIXME use ctx to identify storage client
153         return storageClient;
154     }
155
156     /**
157      * createAccountRole creates one or more account-role relationships
158      * between object (account/role) and subject (role/account)
159      * @param input
160      * @param subject
161      * @return
162      * @throws Exception
163      */
164     public String createAccountRole(AccountRole input, SubjectType subject)
165             throws Exception {
166         
167         //
168         // We need to associate every new account with the Spring Security Admin role so we can make
169         // changes to the Spring Security ACL tables.  The Spring Security Admin role has NO CollectionSpace
170         // specific permissions.  It is an internal/private role that service consumers and end-users NEVER see.
171         //
172         
173         //Preserve the original incoming list of roles
174         List<RoleValue> inputRoleValues = input.getRole();
175
176         //Change the role list to be just the Spring role
177         List<RoleValue> springRoles = new ArrayList<RoleValue>();
178         input.setRole(springRoles);
179         RoleValue springAdminRole = new RoleValue();
180         springRoles.add(springAdminRole);
181         springAdminRole.setRoleId(AuthorizationCommon.ROLE_SPRING_ADMIN_ID);
182         springAdminRole.setRoleName(AuthorizationCommon.ROLE_SPRING_ADMIN_NAME);
183
184         // The Spring role relationship may already exist, if it does then we'll get a PersistenceException that
185         // we'll just ignore.
186         try {
187                 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
188                 DocumentHandler handler = createDocumentHandler(ctx);        
189                 getStorageClient(ctx).create(ctx, handler);
190         } catch (PersistenceException e) {
191                 //If we get this exception, it means that the role relationship already exists, so
192                 //we can just ignore this exception.
193                 if (logger.isTraceEnabled() == true) {
194                         logger.trace(AuthorizationCommon.ROLE_SPRING_ADMIN_NAME +
195                                         " relationship already exists for account: " +
196                                         input.getAccount().get(0).getAccountId(), e);
197                 }
198         }
199         
200         //
201         // Now we'll add the account relationships for the original incoming roles.
202         //
203         input.setRole(inputRoleValues);
204         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
205         DocumentHandler handler = createDocumentHandler(ctx);        
206         String bogusCsid = getStorageClient(ctx).create(ctx, handler);
207         
208         return bogusCsid;
209     }
210
211     /**
212      * getAccountRole retrieves account-role relationships using given
213      * csid of object (account/role) and subject (role/account)
214      * @param csid
215      * @param subject
216      * @return
217      * @throws Exception
218      */
219     public AccountRole getAccountRole(
220             String csid, SubjectType subject) throws Exception {
221
222         if (logger.isDebugEnabled()) {
223             logger.debug("getAccountRole with csid=" + csid);
224         }
225         AccountRole result = null;
226         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
227         DocumentHandler handler = createDocumentHandler(ctx);
228         getStorageClient(ctx).get(ctx, csid, handler);
229         result = (AccountRole) ctx.getOutput();
230
231         return result;
232     }
233
234     /**
235      * Gets the account role.
236      *
237      * @param csid the csid
238      * @param subject the subject
239      * @param accountRoleCsid the account role csid
240      * @return the account role
241      * @throws Exception the exception
242      */
243     public AccountRoleRel getAccountRoleRel(String csid,
244                 SubjectType subject,
245                 String accountRoleCsid) throws Exception {
246
247         if (logger.isDebugEnabled()) {
248             logger.debug("getAccountRole with csid=" + csid);
249         }
250 //        AccountRolesList result = new AccountRolesList();
251         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
252         AccountRoleDocumentHandler handler = (AccountRoleDocumentHandler)createDocumentHandler(ctx);
253         handler.setAccountRoleCsid(accountRoleCsid);
254         //getStorageClient(ctx).get(ctx, csid, handler);
255         AccountRoleRel accountRoleRel = (AccountRoleRel)JpaStorageUtils.getEntity(new Long(accountRoleCsid).longValue(), AccountRoleRel.class);
256 //        List<AccountRoleListItem> accountRoleList = result.getAccountRoleListItems();
257 //        AccountRoleListItem listItem = new AccountRoleListItem();
258 //        // fill the item
259 //        listItem.setCsid(accountRoleRel.getHjid().toString());
260 //        listItem.setRoleId(accountRoleRel.getRoleId());
261 //        listItem.setRoleName(accountRoleRel.getRoleName());
262         // add item to result list
263 //        result = (AccountRolesList) ctx.getOutput();
264         
265         return accountRoleRel;
266     }
267
268     /**
269      * X_delete account role.
270      *
271      * @param csid the csid
272      * @param subject the subject
273      * @throws Exception the exception
274      */
275     public void x_deleteAccountRole(String csid,
276             SubjectType subject) throws Exception {
277
278         if (logger.isDebugEnabled()) {
279             logger.debug("deleteAccountRole with csid=" + csid);
280         }
281         AccountRole toDelete = getAccountRole(csid, subject);
282         deleteAccountRole(csid, subject, toDelete);
283     }
284     
285     /**
286      * deleteAccountRole deletes all account-role relationships using given
287      * csid of object (account/role) and subject (role/account)
288      * @param csid of the object
289      * @param subject
290      * @return
291      * @throws Exception
292      */
293     public void deleteAccountRole(String csid,
294             SubjectType subject) throws Exception {
295
296         if (logger.isDebugEnabled()) {
297             logger.debug("deleteAccountRole with csid=" + csid);
298         }
299         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
300         getStorageClient(ctx).delete(ctx, csid);
301     }
302
303     /**
304      * deleteAccountRole deletes given account-role relationships using given
305      * csid of object (account/role) and subject (role/account)
306      * @param csid of the object
307      * @param subject
308      * @param input with account role relationships to delete
309      * @return
310      * @throws Exception
311      */
312     public void deleteAccountRole(String csid, SubjectType subject, AccountRole input)
313             throws Exception {
314
315         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
316         DocumentHandler handler = createDocumentHandler(ctx);
317         getStorageClient(ctx).delete(ctx, csid, handler);
318     }
319 }