]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c72d69122c9b566a3c3af2edaa986bdd01d410f5
[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.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * AccountRoleSubResource is used to manage account-role relationship
42  * @author
43  */
44 public class AccountRoleSubResource
45         extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRole> {
46
47     //this service is never exposed as standalone RESTful service...just use unique
48     //service name to identify binding
49     /** The service name. */
50     final private String serviceName = "accounts/accountroles";
51     
52     /** The logger. */
53     final Logger logger = LoggerFactory.getLogger(AccountRoleSubResource.class);
54     
55     /** The storage client. */
56     final StorageClient storageClient = new JpaRelationshipStorageClient();
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("entity-name", AccountRoleRel.class.getName());
107         //subject name is necessary to indicate if role or account is a subject
108         ctx.setProperty("subject", subject);
109         //set context for the relationship query
110         ctx.setProperty("objectId", "account_id");
111         return ctx;
112     }
113
114     /* (non-Javadoc)
115      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
116      */
117     @Override
118     public StorageClient getStorageClient(ServiceContext<AccountRole, AccountRole> ctx) {
119         //FIXME use ctx to identify storage client
120         return storageClient;
121     }
122
123
124     /**
125      * createAccountRole creates one or more account-role relationships
126      * between object (account/role) and subject (role/account)
127      * @param input
128      * @param subject
129      * @return
130      * @throws Exception
131      */
132     public String createAccountRole(AccountRole input, SubjectType subject)
133             throws Exception {
134
135         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
136         DocumentHandler handler = createDocumentHandler(ctx);
137         return getStorageClient(ctx).create(ctx, handler);
138     }
139
140     /**
141      * getAccountRole retrieves account-role relationships using given
142      * csid of object (account/role) and subject (role/account)
143      * @param csid
144      * @param subject
145      * @return
146      * @throws Exception
147      */
148     public AccountRole getAccountRole(
149             String csid, SubjectType subject) throws Exception {
150
151         if (logger.isDebugEnabled()) {
152             logger.debug("getAccountRole with csid=" + csid);
153         }
154         AccountRole result = null;
155         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
156         DocumentHandler handler = createDocumentHandler(ctx);
157         getStorageClient(ctx).get(ctx, csid, handler);
158         result = (AccountRole) ctx.getOutput();
159
160         return result;
161     }
162
163     /**
164      * deleteAccountRole deletes 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 void deleteAccountRole(String csid,
172             SubjectType subject) throws Exception {
173
174         if (logger.isDebugEnabled()) {
175             logger.debug("deleteAccountRole with csid=" + csid);
176         }
177         ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
178         getStorageClient(ctx).delete(ctx, csid);
179     }
180 }