]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
bc776a1bdfa0b9f9aadf0a2a38c2ea592196510a
[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
28 import org.collectionspace.services.account.storage.AccountRoleDocumentHandler;
29 //import org.collectionspace.services.authorization.AccountRolesList;
30 //import org.collectionspace.services.authorization.AccountRolesList.AccountRoleListItem;
31 import org.collectionspace.services.authorization.AccountRole;
32 import org.collectionspace.services.authorization.AccountValue;
33 import org.collectionspace.services.authorization.AccountRoleRel;
34 import org.collectionspace.services.authorization.Permission;
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.JpaRelationshipStorageClient;
46 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
47 import org.collectionspace.services.common.context.ServiceContextProperties;
48
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 /**
53  * AccountRoleSubResource is used to manage account-role relationship
54  * @author
55  */
56 public class AccountRoleSubResource
57 //        extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRolesList> {
58         extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRole> {
59
60         //FIXME: These belong in an Authorization class, not here
61     private static String ROLE_SPRING_ADMIN_ID = "-1";
62     private static String ROLE_SPRING_ADMIN_NAME = "ROLE_SPRING_ADMIN";    
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         RoleValue springAdminRole = new RoleValue();
175         springAdminRole.setRoleId(ROLE_SPRING_ADMIN_ID);
176         springAdminRole.setRoleName(ROLE_SPRING_ADMIN_NAME);
177         List<RoleValue> roleValues = input.getRoles();
178         roleValues.add(springAdminRole);
179
180         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
181         DocumentHandler handler = createDocumentHandler(ctx);        
182         String bogusCsid = getStorageClient(ctx).create(ctx, handler);
183         
184         return bogusCsid;
185     }
186
187     /**
188      * getAccountRole retrieves account-role relationships using given
189      * csid of object (account/role) and subject (role/account)
190      * @param csid
191      * @param subject
192      * @return
193      * @throws Exception
194      */
195     public AccountRole getAccountRole(
196             String csid, SubjectType subject) throws Exception {
197
198         if (logger.isDebugEnabled()) {
199             logger.debug("getAccountRole with csid=" + csid);
200         }
201         AccountRole result = null;
202         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
203         DocumentHandler handler = createDocumentHandler(ctx);
204         getStorageClient(ctx).get(ctx, csid, handler);
205         result = (AccountRole) ctx.getOutput();
206
207         return result;
208     }
209
210     /**
211      * Gets the account role.
212      *
213      * @param csid the csid
214      * @param subject the subject
215      * @param accountRoleCsid the account role csid
216      * @return the account role
217      * @throws Exception the exception
218      */
219     public AccountRoleRel getAccountRoleRel(String csid,
220                 SubjectType subject,
221                 String accountRoleCsid) throws Exception {
222
223         if (logger.isDebugEnabled()) {
224             logger.debug("getAccountRole with csid=" + csid);
225         }
226 //        AccountRolesList result = new AccountRolesList();
227         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
228         AccountRoleDocumentHandler handler = (AccountRoleDocumentHandler)createDocumentHandler(ctx);
229         handler.setAccountRoleCsid(accountRoleCsid);
230         //getStorageClient(ctx).get(ctx, csid, handler);
231         AccountRoleRel accountRoleRel = (AccountRoleRel)JpaStorageUtils.getEntity(new Long(accountRoleCsid).longValue(), AccountRoleRel.class);
232 //        List<AccountRoleListItem> accountRoleList = result.getAccountRoleListItems();
233 //        AccountRoleListItem listItem = new AccountRoleListItem();
234 //        // fill the item
235 //        listItem.setCsid(accountRoleRel.getHjid().toString());
236 //        listItem.setRoleId(accountRoleRel.getRoleId());
237 //        listItem.setRoleName(accountRoleRel.getRoleName());
238         // add item to result list
239 //        result = (AccountRolesList) ctx.getOutput();
240         
241         return accountRoleRel;
242     }
243
244     /**
245      * X_delete account role.
246      *
247      * @param csid the csid
248      * @param subject the subject
249      * @throws Exception the exception
250      */
251     public void x_deleteAccountRole(String csid,
252             SubjectType subject) throws Exception {
253
254         if (logger.isDebugEnabled()) {
255             logger.debug("deleteAccountRole with csid=" + csid);
256         }
257         AccountRole toDelete = getAccountRole(csid, subject);
258         deleteAccountRole(csid, subject, toDelete);
259     }
260     
261     /**
262      * deleteAccountRole deletes all account-role relationships using given
263      * csid of object (account/role) and subject (role/account)
264      * @param csid of the object
265      * @param subject
266      * @return
267      * @throws Exception
268      */
269     public void deleteAccountRole(String csid,
270             SubjectType subject) throws Exception {
271
272         if (logger.isDebugEnabled()) {
273             logger.debug("deleteAccountRole with csid=" + csid);
274         }
275         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
276         getStorageClient(ctx).delete(ctx, csid);
277     }
278
279     /**
280      * deleteAccountRole deletes given account-role relationships using given
281      * csid of object (account/role) and subject (role/account)
282      * @param csid of the object
283      * @param subject
284      * @param input with account role relationships to delete
285      * @return
286      * @throws Exception
287      */
288     public void deleteAccountRole(String csid, SubjectType subject, AccountRole input)
289             throws Exception {
290
291         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
292         DocumentHandler handler = createDocumentHandler(ctx);
293         getStorageClient(ctx).delete(ctx, csid, handler);
294     }
295 }