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