]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e605ecf502b7ca235216098f5d2bf3ffe50d6b98
[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 org.collectionspace.services.authorization.AccountRole;
27 import org.collectionspace.services.authorization.AccountRoleRel;
28 import org.collectionspace.services.authorization.SubjectType;
29
30 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
31 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
32 import org.collectionspace.services.common.context.ServiceContext;
33 import org.collectionspace.services.common.context.ServiceContextFactory;
34 import org.collectionspace.services.common.document.DocumentHandler;
35 import org.collectionspace.services.common.storage.StorageClient;
36 import org.collectionspace.services.common.storage.jpa.JpaRelationshipStorageClient;
37 import org.collectionspace.services.common.context.ServiceContextProperties;
38
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * AccountRoleSubResource is used to manage account-role relationship
44  * @author
45  */
46 public class AccountRoleSubResource
47         extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRole> {
48
49     final public static String ACCOUNT_ACCOUNTROLE_SERVICE = "accounts/accountroles";
50     final public static String ROLE_ACCOUNTROLE_SERVICE = "roles/accountroles";
51     //this service is never exposed as standalone RESTful service...just use unique
52     //service name to identify binding
53     /** The service name. */
54     private String serviceName = ACCOUNT_ACCOUNTROLE_SERVICE;
55     /** The logger. */
56     final Logger logger = LoggerFactory.getLogger(AccountRoleSubResource.class);
57     /** The storage client. */
58     final StorageClient storageClient = new JpaRelationshipStorageClient<AccountRole>();
59
60     /**
61      *
62      * @param serviceName qualified service path
63      */
64     AccountRoleSubResource(String serviceName) {
65         this.serviceName = serviceName;
66     }
67
68     /* (non-Javadoc)
69      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
70      */
71     @Override
72     protected String getVersionString() {
73         /** The last change revision. */
74         final String lastChangeRevision = "$LastChangedRevision: 1165 $";
75         return lastChangeRevision;
76     }
77
78     /* (non-Javadoc)
79      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
80      */
81     @Override
82     public String getServiceName() {
83         return serviceName;
84     }
85
86     /* (non-Javadoc)
87      * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
88      */
89     @Override
90     public Class<AccountRole> getCommonPartClass() {
91         return AccountRole.class;
92     }
93
94     /* (non-Javadoc)
95      * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
96      */
97     @Override
98     public ServiceContextFactory<AccountRole, AccountRole> getServiceContextFactory() {
99         return RemoteServiceContextFactory.get();
100     }
101
102     /**
103      * Creates the service context.
104      * 
105      * @param input the input
106      * @param subject the subject
107      * 
108      * @return the service context< account role, account role>
109      * 
110      * @throws Exception the exception
111      */
112     private ServiceContext<AccountRole, AccountRole> createServiceContext(AccountRole input,
113             SubjectType subject) throws Exception {
114         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input);
115         ctx.setDocumentType(AccountRole.class.getPackage().getName()); //persistence unit
116         ctx.setProperty(ServiceContextProperties.ENTITY_NAME, AccountRoleRel.class.getName());
117         ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, AccountRoleRel.class);
118         //subject name is necessary to indicate if role or account is a subject
119         ctx.setProperty(ServiceContextProperties.SUBJECT, subject);
120         //set context for the relationship query
121         ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, AccountsCommon.class);
122         ctx.setProperty(ServiceContextProperties.OBJECT_ID, "account_id");
123         return ctx;
124     }
125
126     /* (non-Javadoc)
127      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
128      */
129     @Override
130     public StorageClient getStorageClient(ServiceContext<AccountRole, AccountRole> ctx) {
131         //FIXME use ctx to identify storage client
132         return storageClient;
133     }
134
135     /**
136      * createAccountRole creates one or more account-role relationships
137      * between object (account/role) and subject (role/account)
138      * @param input
139      * @param subject
140      * @return
141      * @throws Exception
142      */
143     public String createAccountRole(AccountRole input, SubjectType subject)
144             throws Exception {
145
146         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
147         DocumentHandler handler = createDocumentHandler(ctx);
148         return getStorageClient(ctx).create(ctx, handler);
149     }
150
151     /**
152      * getAccountRole retrieves account-role relationships using given
153      * csid of object (account/role) and subject (role/account)
154      * @param csid
155      * @param subject
156      * @return
157      * @throws Exception
158      */
159     public AccountRole getAccountRole(
160             String csid, SubjectType subject) throws Exception {
161
162         if (logger.isDebugEnabled()) {
163             logger.debug("getAccountRole with csid=" + csid);
164         }
165         AccountRole result = null;
166         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
167         DocumentHandler handler = createDocumentHandler(ctx);
168         getStorageClient(ctx).get(ctx, csid, handler);
169         result = (AccountRole) ctx.getOutput();
170
171         return result;
172     }
173
174     /**
175      * deleteAccountRole deletes all account-role relationships using given
176      * csid of object (account/role) and subject (role/account)
177      * @param csid of the object
178      * @param subject
179      * @return
180      * @throws Exception
181      */
182     public void deleteAccountRole(String csid,
183             SubjectType subject) throws Exception {
184
185         if (logger.isDebugEnabled()) {
186             logger.debug("deleteAccountRole with csid=" + csid);
187         }
188         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
189         getStorageClient(ctx).delete(ctx, csid);
190     }
191
192     /**
193      * deleteAccountRole deletes given account-role relationships using given
194      * csid of object (account/role) and subject (role/account)
195      * @param csid of the object
196      * @param subject
197      * @param input with account role relationships to delete
198      * @return
199      * @throws Exception
200      */
201     public void deleteAccountRole(String csid, SubjectType subject, AccountRole input)
202             throws Exception {
203
204         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
205         DocumentHandler handler = createDocumentHandler(ctx);
206         getStorageClient(ctx).delete(ctx, csid, handler);
207     }
208 }