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.apache.commons.text.RandomStringGenerator;
35 import org.collectionspace.services.account.AccountListItem;
36 import org.collectionspace.services.account.AccountRoleSubResource;
37 import org.collectionspace.services.account.Status;
38 import org.collectionspace.services.authorization.AccountRole;
39 import org.collectionspace.services.authorization.SubjectType;
40 import org.collectionspace.services.account.RoleValue;
41 import org.collectionspace.services.client.AccountClient;
42 import org.collectionspace.services.client.AccountRoleFactory;
43 import org.collectionspace.services.common.storage.TransactionContext;
44 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
45 import org.collectionspace.services.common.api.Tools;
46 import org.collectionspace.services.common.context.ServiceContext;
47 import org.collectionspace.services.common.document.DocumentFilter;
48 import org.collectionspace.services.common.document.DocumentWrapper;
49 import org.collectionspace.services.common.document.JaxbUtils;
50 import org.collectionspace.services.common.security.SecurityUtils;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
59 public class AccountDocumentHandler
60 extends JpaDocumentHandler<AccountsCommon, AccountsCommonList, AccountsCommon, List<AccountsCommon>> {
62 private final Logger logger = LoggerFactory.getLogger(AccountDocumentHandler.class);
63 private AccountsCommon account;
64 private AccountsCommonList accountList;
67 public void handleCreate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
68 String id = UUID.randomUUID().toString();
69 AccountsCommon account = wrapDoc.getWrappedObject();
75 if (account.getStatus() == null) {
76 account.setStatus(Status.ACTIVE);
79 // We do not allow creation of locked accounts through the services.
80 account.setMetadataProtection(null);
81 account.setRolesProtection(null);
85 public void handleUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
86 AccountsCommon accountFound = wrapDoc.getWrappedObject();
87 AccountsCommon accountReceived = getCommonPart();
88 // If marked as metadata immutable, do not do update
89 if (!AccountClient.IMMUTABLE.equals(accountFound.getMetadataProtection())) {
90 merge(accountReceived, accountFound);
93 // Update the accountroles if supplied
95 if (accountReceived.getRoleList() != null) { // if null, no <roleList> element was supplied so we don't do anything to the account-role relationships
97 // First, delete the existing accountroles
99 AccountRoleSubResource subResource =
100 new AccountRoleSubResource(AccountRoleSubResource.ACCOUNT_ACCOUNTROLE_SERVICE);
101 subResource.deleteAccountRole(getServiceContext(), accountFound.getCsid(), SubjectType.ROLE);
103 // Check to see if the payload has new roles to relate to the account
105 List<RoleValue> roleValueList = accountReceived.getRoleList().getRole();
106 if (roleValueList != null && roleValueList.size() > 0) {
108 // Next, create the new accountroles
110 AccountRole accountRole = AccountRoleFactory.createAccountRoleInstance(accountFound,
111 roleValueList, true, true);
112 String accountRoleCsid = subResource.createAccountRole(getServiceContext(), accountRole, SubjectType.ROLE);
114 // Finally, set the updated role list in the result
116 AccountRole newAccountRole = subResource.getAccountRole(getServiceContext(), accountFound.getCsid(), SubjectType.ROLE);
117 accountFound.setRoleList(AccountRoleFactory.convert(newAccountRole.getRole()));
123 * merge manually merges the from account to the to account
124 * -this method is created due to inefficiency of JPA EM merge
127 * @return merged account
129 private AccountsCommon merge(AccountsCommon from, AccountsCommon to) {
130 Date now = new Date();
131 to.setUpdatedAtItem(now);
132 if (from.getEmail() != null) {
133 to.setEmail(from.getEmail());
135 if (from.getPhone() != null) {
136 to.setPhone(from.getPhone());
138 if (from.getMobile() != null) {
139 to.setMobile(from.getMobile());
141 if (from.getScreenName() != null) {
142 to.setScreenName(from.getScreenName());
144 if (from.getStatus() != null) {
145 to.setStatus(from.getStatus());
147 if (from.getPersonRefName() != null) {
148 to.setPersonRefName(from.getPersonRefName());
151 // Note that we do not allow update of locks
152 //fixme update for tenant association
154 if (logger.isDebugEnabled()) {
155 logger.debug("merged account="
156 + JaxbUtils.toString(to, AccountsCommon.class));
163 * If the create payload included a list of role, relate them to the account.
165 public void completeCreate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
166 AccountsCommon accountsCommon = wrapDoc.getWrappedObject();
167 List<RoleValue> roleValueList = account.getRoleList() != null ? account.getRoleList().getRole() : null;
168 if (roleValueList != null && roleValueList.size() > 0) {
170 // To prevent new Accounts being created (especially low-level Spring Security accounts/SIDs), we'll first flush the current
171 // JPA context to ensure our Account can be successfully persisted.
173 TransactionContext jpaTransactionContext = this.getServiceContext().getCurrentTransactionContext();
174 jpaTransactionContext.flush();
176 AccountRoleSubResource subResource = new AccountRoleSubResource(AccountRoleSubResource.ACCOUNT_ACCOUNTROLE_SERVICE);
177 AccountRole accountRole = AccountRoleFactory.createAccountRoleInstance(accountsCommon, roleValueList, true, true);
178 subResource.createAccountRole(this.getServiceContext(), accountRole, SubjectType.ROLE);
183 public void completeUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
184 AccountsCommon upAcc = wrapDoc.getWrappedObject();
185 getServiceContext().setOutput(upAcc);
189 public void handleGet(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
190 setCommonPart(extractCommonPart(wrapDoc));
191 sanitize(getCommonPart());
192 getServiceContext().setOutput(account);
196 public void handleGetAll(DocumentWrapper<List<AccountsCommon>> wrapDoc) throws Exception {
197 AccountsCommonList accList = extractCommonPartList(wrapDoc);
198 setCommonPartList(accList);
199 getServiceContext().setOutput(getCommonPartList());
202 @SuppressWarnings("unchecked")
204 public AccountsCommon extractCommonPart(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
205 AccountsCommon account = wrapDoc.getWrappedObject();
207 String includeRolesQueryParamValue = (String) getServiceContext().getQueryParams().getFirst(AccountClient.INCLUDE_ROLES_QP);
208 boolean includeRoles = Tools.isTrue(includeRolesQueryParamValue);
210 AccountRoleSubResource accountRoleResource = new AccountRoleSubResource(
211 AccountRoleSubResource.ACCOUNT_ACCOUNTROLE_SERVICE);
212 AccountRole accountRole = accountRoleResource.getAccountRole(getServiceContext(), account.getCsid(),
214 account.setRoleList(AccountRoleFactory.convert(accountRole.getRole()));
217 return wrapDoc.getWrappedObject();
221 public void fillCommonPart(AccountsCommon obj, DocumentWrapper<AccountsCommon> wrapDoc)
223 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
227 public AccountsCommonList extractCommonPartList(
228 DocumentWrapper<List<AccountsCommon>> wrapDoc)
231 AccountsCommonList accList = this.extractPagingInfo(new AccountsCommonList(), wrapDoc);
232 // AccountsCommonList accList = new AccountsCommonList();
233 List<AccountListItem> list = accList.getAccountListItem();
235 for (Object obj : wrapDoc.getWrappedObject()) {
236 AccountsCommon account = (AccountsCommon) obj;
237 AccountListItem accListItem = new AccountListItem();
238 accListItem.setScreenName(account.getScreenName());
239 accListItem.setUserid(account.getUserId());
241 // Since accounts can be associated with more than 1 tenant, we only want to include
242 // the tenant information for the current service context.
244 String tenantInCtx = this.getServiceContext().getTenantId();
245 List<AccountTenant> associatedTenantList = account.getTenants();
246 for (AccountTenant associatedTenant : associatedTenantList) {
247 if (associatedTenant != null && associatedTenant.getTenantId() != null) {
248 if (associatedTenant.getTenantId().equalsIgnoreCase(tenantInCtx)) {
249 accListItem.setTenantid(associatedTenant.getTenantId());
255 accListItem.setTenants(account.getTenants());
256 accListItem.setEmail(account.getEmail());
257 accListItem.setStatus(account.getStatus());
258 String id = account.getCsid();
259 accListItem.setUri(getServiceContextPath() + id);
260 accListItem.setCsid(id);
261 list.add(accListItem);
267 public AccountsCommon getCommonPart() {
272 public void setCommonPart(AccountsCommon account) {
273 this.account = account;
277 public AccountsCommonList getCommonPartList() {
282 public void setCommonPartList(AccountsCommonList accountList) {
283 this.accountList = accountList;
287 public String getQProperty(
293 public DocumentFilter createDocumentFilter() {
294 DocumentFilter filter = new AccountJpaFilter(this.getServiceContext());
298 private void setTenant(AccountsCommon account) {
299 //set tenant only if not available from input
300 ServiceContext ctx = getServiceContext();
301 if (account.getTenants() == null || account.getTenants().size() == 0) {
302 if (ctx.getTenantId() != null) {
303 AccountTenant at = new AccountTenant();
304 at.setTenantId(ctx.getTenantId());
305 List<AccountTenant> atList = new ArrayList<AccountTenant>();
307 account.setTenants(atList);
313 * sanitize removes data not needed to be sent to the consumer
317 public void sanitize(DocumentWrapper<AccountsCommon> wrapDoc) {
318 AccountsCommon account = wrapDoc.getWrappedObject();
322 private void sanitize(AccountsCommon account) {
323 account.setPassword(null);
324 if (!SecurityUtils.isCSpaceAdmin()) {
325 account.setTenants(new ArrayList<AccountTenant>(0));
330 * @see org.collectionspace.services.common.document.DocumentHandler#initializeDocumentFilter(org.collectionspace.services.common.context.ServiceContext)
332 public void initializeDocumentFilter(ServiceContext<AccountsCommon, AccountsCommon> ctx) {
333 // set a default document filter in this method