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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.account.storage;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.List;
29 import java.util.UUID;
31 import org.collectionspace.services.account.AccountTenant;
32 import org.collectionspace.services.account.AccountsCommon;
33 import org.collectionspace.services.account.AccountsCommonList;
34 import org.collectionspace.services.account.AccountListItem;
35 import org.collectionspace.services.account.AccountRoleSubResource;
36 import org.collectionspace.services.account.Status;
37 import org.collectionspace.services.authorization.AccountRole;
38 import org.collectionspace.services.authorization.PermissionRole;
39 import org.collectionspace.services.authorization.PermissionRoleSubResource;
40 import org.collectionspace.services.authorization.SubjectType;
41 import org.collectionspace.services.account.RoleValue;
42 import org.collectionspace.services.client.AccountClient;
43 import org.collectionspace.services.client.AccountRoleFactory;
44 import org.collectionspace.services.client.RoleClient;
45 import org.collectionspace.services.common.storage.TransactionContext;
46 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
47 import org.collectionspace.services.common.api.Tools;
48 import org.collectionspace.services.common.context.ServiceContext;
49 import org.collectionspace.services.common.document.DocumentFilter;
50 import org.collectionspace.services.common.document.DocumentWrapper;
51 import org.collectionspace.services.common.document.JaxbUtils;
52 import org.collectionspace.services.common.security.SecurityUtils;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
61 public class AccountDocumentHandler
62 extends JpaDocumentHandler<AccountsCommon, AccountsCommonList, AccountsCommon, List<AccountsCommon>> {
64 private final Logger logger = LoggerFactory.getLogger(AccountDocumentHandler.class);
65 private AccountsCommon account;
66 private AccountsCommonList accountList;
69 public void handleCreate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
70 String id = UUID.randomUUID().toString();
71 AccountsCommon account = wrapDoc.getWrappedObject();
74 account.setStatus(Status.ACTIVE);
75 // We do not allow creation of locked accounts through the services.
76 account.setMetadataProtection(null);
77 account.setRolesProtection(null);
81 public void handleUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
82 AccountsCommon accountFound = wrapDoc.getWrappedObject();
83 AccountsCommon accountReceived = getCommonPart();
84 // If marked as metadata immutable, do not do update
85 if (!AccountClient.IMMUTABLE.equals(accountFound.getMetadataProtection())) {
86 merge(accountReceived, accountFound);
89 // Update the accountroles if supplied
91 List<RoleValue> roleValueList = accountReceived.getRole();
92 if (roleValueList != null && roleValueList.size() > 0) {
93 AccountRoleSubResource subResource =
94 new AccountRoleSubResource(AccountRoleSubResource.ACCOUNT_ACCOUNTROLE_SERVICE);
96 // First, delete the exist accountroles
98 subResource.deleteAccountRole(getServiceContext(), accountFound.getCsid(), SubjectType.ROLE);
100 // Next, create the new accountroles
102 AccountRole accountRole = AccountRoleFactory.createAccountRoleInstance(accountFound,
103 roleValueList, true, true);
104 String accountRoleCsid = subResource.createAccountRole(getServiceContext(), accountRole, SubjectType.ROLE);
106 // Finally, set the updated role list in the result
108 AccountRole newAccountRole = subResource.getAccountRole(getServiceContext(), accountFound.getCsid(), SubjectType.ROLE);
109 accountFound.setRole(AccountRoleFactory.convert(newAccountRole.getRole()));
114 * merge manually merges the from account to the to account
115 * -this method is created due to inefficiency of JPA EM merge
118 * @return merged account
120 private AccountsCommon merge(AccountsCommon from, AccountsCommon to) {
121 Date now = new Date();
122 to.setUpdatedAtItem(now);
123 if (from.getEmail() != null) {
124 to.setEmail(from.getEmail());
126 if (from.getPhone() != null) {
127 to.setPhone(from.getPhone());
129 if (from.getMobile() != null) {
130 to.setMobile(from.getMobile());
132 if (from.getScreenName() != null) {
133 to.setScreenName(from.getScreenName());
135 if (from.getStatus() != null) {
136 to.setStatus(from.getStatus());
138 if (from.getPersonRefName() != null) {
139 to.setPersonRefName(from.getPersonRefName());
141 // Note that we do not allow update of locks
142 //fixme update for tenant association
144 if (logger.isDebugEnabled()) {
145 logger.debug("merged account="
146 + JaxbUtils.toString(to, AccountsCommon.class));
153 * If the create payload included a list of role, relate them to the account.
155 public void completeCreate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
156 AccountsCommon accountsCommon = wrapDoc.getWrappedObject();
157 List<RoleValue> roleValueList = account.getRole();
158 if (roleValueList != null && roleValueList.size() > 0) {
160 // To prevent new Accounts being created (especially low-level Spring Security accounts/SIDs), we'll first flush the current
161 // JPA context to ensure our Account can be successfully persisted.
163 TransactionContext jpaTransactionContext = this.getServiceContext().getCurrentTransactionContext();
164 jpaTransactionContext.flush();
166 AccountRoleSubResource subResource = new AccountRoleSubResource(AccountRoleSubResource.ACCOUNT_ACCOUNTROLE_SERVICE);
167 AccountRole accountRole = AccountRoleFactory.createAccountRoleInstance(accountsCommon, roleValueList, true, true);
168 subResource.createAccountRole(this.getServiceContext(), accountRole, SubjectType.ROLE);
173 public void completeUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
174 AccountsCommon upAcc = wrapDoc.getWrappedObject();
175 getServiceContext().setOutput(upAcc);
179 public void handleGet(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
180 setCommonPart(extractCommonPart(wrapDoc));
181 sanitize(getCommonPart());
182 getServiceContext().setOutput(account);
186 public void handleGetAll(DocumentWrapper<List<AccountsCommon>> wrapDoc) throws Exception {
187 AccountsCommonList accList = extractCommonPartList(wrapDoc);
188 setCommonPartList(accList);
189 getServiceContext().setOutput(getCommonPartList());
192 @SuppressWarnings("unchecked")
194 public AccountsCommon extractCommonPart(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
195 AccountsCommon account = wrapDoc.getWrappedObject();
197 String includeRolesQueryParamValue = (String) getServiceContext().getQueryParams().getFirst(AccountClient.INCLUDE_ROLES_QP);
198 boolean includeRoles = Tools.isTrue(includeRolesQueryParamValue);
200 AccountRoleSubResource accountRoleResource = new AccountRoleSubResource(
201 AccountRoleSubResource.ACCOUNT_ACCOUNTROLE_SERVICE);
202 AccountRole accountRole = accountRoleResource.getAccountRole(getServiceContext(), account.getCsid(),
204 account.setRole(AccountRoleFactory.convert(accountRole.getRole()));
207 return wrapDoc.getWrappedObject();
211 public void fillCommonPart(AccountsCommon obj, DocumentWrapper<AccountsCommon> wrapDoc)
213 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
217 public AccountsCommonList extractCommonPartList(
218 DocumentWrapper<List<AccountsCommon>> wrapDoc)
221 AccountsCommonList accList = this.extractPagingInfo(new AccountsCommonList(), wrapDoc);
222 // AccountsCommonList accList = new AccountsCommonList();
223 List<AccountListItem> list = accList.getAccountListItem();
225 for (Object obj : wrapDoc.getWrappedObject()) {
226 AccountsCommon account = (AccountsCommon) obj;
227 AccountListItem accListItem = new AccountListItem();
228 accListItem.setScreenName(account.getScreenName());
229 accListItem.setUserid(account.getUserId());
230 accListItem.setTenantid(account.getTenants().get(0).getTenantId()); // pick the default/first tenant
231 accListItem.setTenants(account.getTenants());
232 accListItem.setEmail(account.getEmail());
233 accListItem.setStatus(account.getStatus());
234 String id = account.getCsid();
235 accListItem.setUri(getServiceContextPath() + id);
236 accListItem.setCsid(id);
237 list.add(accListItem);
243 public AccountsCommon getCommonPart() {
248 public void setCommonPart(AccountsCommon account) {
249 this.account = account;
253 public AccountsCommonList getCommonPartList() {
258 public void setCommonPartList(AccountsCommonList accountList) {
259 this.accountList = accountList;
263 public String getQProperty(
269 public DocumentFilter createDocumentFilter() {
270 DocumentFilter filter = new AccountJpaFilter(this.getServiceContext());
274 private void setTenant(AccountsCommon account) {
275 //set tenant only if not available from input
276 ServiceContext ctx = getServiceContext();
277 if (account.getTenants() == null || account.getTenants().size() == 0) {
278 if (ctx.getTenantId() != null) {
279 AccountTenant at = new AccountTenant();
280 at.setTenantId(ctx.getTenantId());
281 List<AccountTenant> atList = new ArrayList<AccountTenant>();
283 account.setTenants(atList);
289 * sanitize removes data not needed to be sent to the consumer
293 public void sanitize(DocumentWrapper<AccountsCommon> wrapDoc) {
294 AccountsCommon account = wrapDoc.getWrappedObject();
298 private void sanitize(AccountsCommon account) {
299 account.setPassword(null);
300 if (!SecurityUtils.isCSpaceAdmin()) {
301 account.setTenants(new ArrayList<AccountTenant>(0));
306 * @see org.collectionspace.services.common.document.DocumentHandler#initializeDocumentFilter(org.collectionspace.services.common.context.ServiceContext)
308 public void initializeDocumentFilter(ServiceContext<AccountsCommon, AccountsCommon> ctx) {
309 // set a default document filter in this method