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