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.AccountValue;
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.AccountFactory;
43 import org.collectionspace.services.client.AccountRoleFactory;
44 import org.collectionspace.services.common.storage.TransactionContext;
45 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
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();
72 account.setStatus(Status.ACTIVE);
73 // We do not allow creation of locked accounts through the services.
74 account.setMetadataProtection(null);
75 account.setRolesProtection(null);
79 public void handleUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
80 AccountsCommon accountFound = wrapDoc.getWrappedObject();
81 AccountsCommon accountReceived = getCommonPart();
82 // If marked as metadata immutable, do not do update
83 if (!AccountClient.IMMUTABLE.equals(accountFound.getMetadataProtection())) {
84 merge(accountReceived, accountFound);
87 // Update the accountroles if supplied
89 List<RoleValue> roleValueList = accountReceived.getRole();
90 if (roleValueList != null && roleValueList.size() > 0) {
91 AccountRoleSubResource subResource =
92 new AccountRoleSubResource(AccountRoleSubResource.ACCOUNT_ACCOUNTROLE_SERVICE);
94 // First, delete the exist accountroles
96 subResource.deleteAccountRole(getServiceContext(), accountFound.getCsid(), SubjectType.ROLE);
98 // Next, create the new accountroles
100 AccountRole accountRole = AccountRoleFactory.createAccountRoleInstance(accountFound,
101 roleValueList, true, true);
102 String accountRoleCsid = subResource.createAccountRole(getServiceContext(), accountRole, SubjectType.ROLE);
104 // Finally, set the updated role list in the result
106 AccountRole newAccountRole = subResource.getAccountRole(getServiceContext(), accountFound.getCsid(), SubjectType.ROLE);
107 accountFound.setRole(AccountRoleFactory.convert(newAccountRole.getRole()));
112 * merge manually merges the from account to the to account
113 * -this method is created due to inefficiency of JPA EM merge
116 * @return merged account
118 private AccountsCommon merge(AccountsCommon from, AccountsCommon to) {
119 Date now = new Date();
120 to.setUpdatedAtItem(now);
121 if (from.getEmail() != null) {
122 to.setEmail(from.getEmail());
124 if (from.getPhone() != null) {
125 to.setPhone(from.getPhone());
127 if (from.getMobile() != null) {
128 to.setMobile(from.getMobile());
130 if (from.getScreenName() != null) {
131 to.setScreenName(from.getScreenName());
133 if (from.getStatus() != null) {
134 to.setStatus(from.getStatus());
136 if (from.getPersonRefName() != null) {
137 to.setPersonRefName(from.getPersonRefName());
139 // Note that we do not allow update of locks
140 //fixme update for tenant association
142 if (logger.isDebugEnabled()) {
143 logger.debug("merged account="
144 + JaxbUtils.toString(to, AccountsCommon.class));
151 * If the create payload included a list of role, relate them to the account.
153 public void completeCreate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
154 AccountsCommon accountsCommon = wrapDoc.getWrappedObject();
155 List<RoleValue> roleValueList = account.getRole();
156 if (roleValueList != null && roleValueList.size() > 0) {
158 // To prevent new Accounts being created (especially low-level Spring Security accounts/SIDs), we'll first flush the current
159 // JPA context to ensure our Account can be successfully persisted.
161 TransactionContext jpaTransactionContext = this.getServiceContext().getCurrentTransactionContext();
162 jpaTransactionContext.flush();
164 AccountRoleSubResource subResource = new AccountRoleSubResource(AccountRoleSubResource.ACCOUNT_ACCOUNTROLE_SERVICE);
165 AccountRole accountRole = AccountRoleFactory.createAccountRoleInstance(accountsCommon, roleValueList, true, true);
166 subResource.createAccountRole(this.getServiceContext(), accountRole, SubjectType.ROLE);
171 public void completeUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
172 AccountsCommon upAcc = wrapDoc.getWrappedObject();
173 getServiceContext().setOutput(upAcc);
178 public void handleGet(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
179 setCommonPart(extractCommonPart(wrapDoc));
180 sanitize(getCommonPart());
181 getServiceContext().setOutput(account);
185 public void handleGetAll(DocumentWrapper<List<AccountsCommon>> wrapDoc) throws Exception {
186 AccountsCommonList accList = extractCommonPartList(wrapDoc);
187 setCommonPartList(accList);
188 getServiceContext().setOutput(getCommonPartList());
192 public AccountsCommon extractCommonPart(
193 DocumentWrapper<AccountsCommon> wrapDoc)
195 return wrapDoc.getWrappedObject();
199 public void fillCommonPart(AccountsCommon obj, DocumentWrapper<AccountsCommon> wrapDoc)
201 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
205 public AccountsCommonList extractCommonPartList(
206 DocumentWrapper<List<AccountsCommon>> wrapDoc)
209 AccountsCommonList accList = this.extractPagingInfo(new AccountsCommonList(), wrapDoc);
210 // AccountsCommonList accList = new AccountsCommonList();
211 List<AccountListItem> list = accList.getAccountListItem();
213 for (Object obj : wrapDoc.getWrappedObject()) {
214 AccountsCommon account = (AccountsCommon) obj;
215 AccountListItem accListItem = new AccountListItem();
216 accListItem.setScreenName(account.getScreenName());
217 accListItem.setUserid(account.getUserId());
218 accListItem.setTenantid(account.getTenants().get(0).getTenantId()); // pick the default/first tenant
219 accListItem.setTenants(account.getTenants());
220 accListItem.setEmail(account.getEmail());
221 accListItem.setStatus(account.getStatus());
222 String id = account.getCsid();
223 accListItem.setUri(getServiceContextPath() + id);
224 accListItem.setCsid(id);
225 list.add(accListItem);
231 public AccountsCommon getCommonPart() {
236 public void setCommonPart(AccountsCommon account) {
237 this.account = account;
241 public AccountsCommonList getCommonPartList() {
246 public void setCommonPartList(AccountsCommonList accountList) {
247 this.accountList = accountList;
251 public String getQProperty(
257 public DocumentFilter createDocumentFilter() {
258 DocumentFilter filter = new AccountJpaFilter(this.getServiceContext());
262 private void setTenant(AccountsCommon account) {
263 //set tenant only if not available from input
264 ServiceContext ctx = getServiceContext();
265 if (account.getTenants() == null || account.getTenants().size() == 0) {
266 if (ctx.getTenantId() != null) {
267 AccountTenant at = new AccountTenant();
268 at.setTenantId(ctx.getTenantId());
269 List<AccountTenant> atList = new ArrayList<AccountTenant>();
271 account.setTenants(atList);
277 * sanitize removes data not needed to be sent to the consumer
280 private void sanitize(AccountsCommon account) {
281 account.setPassword(null);
282 if (!SecurityUtils.isCSpaceAdmin()) {
283 account.setTenants(new ArrayList<AccountTenant>(0));
288 * @see org.collectionspace.services.common.document.DocumentHandler#initializeDocumentFilter(org.collectionspace.services.common.context.ServiceContext)
290 public void initializeDocumentFilter(ServiceContext ctx) {
291 // set a default document filter in this method