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