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