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