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.SubjectType;
39 import org.collectionspace.services.account.RoleValue;
40 import org.collectionspace.services.client.AccountClient;
41 import org.collectionspace.services.client.AccountRoleFactory;
42 import org.collectionspace.services.common.storage.TransactionContext;
43 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
44 import org.collectionspace.services.common.context.ServiceContext;
45 import org.collectionspace.services.common.document.DocumentFilter;
46 import org.collectionspace.services.common.document.DocumentWrapper;
47 import org.collectionspace.services.common.document.JaxbUtils;
48 import org.collectionspace.services.common.security.SecurityUtils;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
57 public class AccountDocumentHandler
58 extends JpaDocumentHandler<AccountsCommon, AccountsCommonList, AccountsCommon, List<AccountsCommon>> {
60 private final Logger logger = LoggerFactory.getLogger(AccountDocumentHandler.class);
61 private AccountsCommon account;
62 private AccountsCommonList accountList;
65 public void handleCreate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
66 String id = UUID.randomUUID().toString();
67 AccountsCommon account = wrapDoc.getWrappedObject();
70 account.setStatus(Status.ACTIVE);
71 // We do not allow creation of locked accounts through the services.
72 account.setMetadataProtection(null);
73 account.setRolesProtection(null);
77 public void handleUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
78 AccountsCommon accountFound = wrapDoc.getWrappedObject();
79 AccountsCommon accountReceived = getCommonPart();
80 // If marked as metadata immutable, do not do update
81 if (!AccountClient.IMMUTABLE.equals(accountFound.getMetadataProtection())) {
82 merge(accountReceived, accountFound);
85 // Update the accountroles if supplied
87 List<RoleValue> roleValueList = accountReceived.getRole();
88 if (roleValueList != null && roleValueList.size() > 0) {
89 AccountRoleSubResource subResource =
90 new AccountRoleSubResource(AccountRoleSubResource.ACCOUNT_ACCOUNTROLE_SERVICE);
92 // First, delete the exist accountroles
94 subResource.deleteAccountRole(getServiceContext(), accountFound.getCsid(), SubjectType.ROLE);
96 // Next, create the new accountroles
98 AccountRole accountRole = AccountRoleFactory.createAccountRoleInstance(accountFound,
99 roleValueList, true, true);
100 String accountRoleCsid = subResource.createAccountRole(getServiceContext(), accountRole, SubjectType.ROLE);
102 // Finally, set the updated role list in the result
104 AccountRole newAccountRole = subResource.getAccountRole(getServiceContext(), accountFound.getCsid(), SubjectType.ROLE);
105 accountFound.setRole(AccountRoleFactory.convert(newAccountRole.getRole()));
110 * merge manually merges the from account to the to account
111 * -this method is created due to inefficiency of JPA EM merge
114 * @return merged account
116 private AccountsCommon merge(AccountsCommon from, AccountsCommon to) {
117 Date now = new Date();
118 to.setUpdatedAtItem(now);
119 if (from.getEmail() != null) {
120 to.setEmail(from.getEmail());
122 if (from.getPhone() != null) {
123 to.setPhone(from.getPhone());
125 if (from.getMobile() != null) {
126 to.setMobile(from.getMobile());
128 if (from.getScreenName() != null) {
129 to.setScreenName(from.getScreenName());
131 if (from.getStatus() != null) {
132 to.setStatus(from.getStatus());
134 if (from.getPersonRefName() != null) {
135 to.setPersonRefName(from.getPersonRefName());
137 // Note that we do not allow update of locks
138 //fixme update for tenant association
140 if (logger.isDebugEnabled()) {
141 logger.debug("merged account="
142 + JaxbUtils.toString(to, AccountsCommon.class));
149 * If the create payload included a list of role, relate them to the account.
151 public void completeCreate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
152 AccountsCommon accountsCommon = wrapDoc.getWrappedObject();
153 List<RoleValue> roleValueList = account.getRole();
154 if (roleValueList != null && roleValueList.size() > 0) {
156 // To prevent new Accounts being created (especially low-level Spring Security accounts/SIDs), we'll first flush the current
157 // JPA context to ensure our Account can be successfully persisted.
159 TransactionContext jpaTransactionContext = this.getServiceContext().getCurrentTransactionContext();
160 jpaTransactionContext.flush();
162 AccountRoleSubResource subResource = new AccountRoleSubResource(AccountRoleSubResource.ACCOUNT_ACCOUNTROLE_SERVICE);
163 AccountRole accountRole = AccountRoleFactory.createAccountRoleInstance(accountsCommon, roleValueList, true, true);
164 subResource.createAccountRole(this.getServiceContext(), accountRole, SubjectType.ROLE);
169 public void completeUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
170 AccountsCommon upAcc = wrapDoc.getWrappedObject();
171 getServiceContext().setOutput(upAcc);
175 public void handleGet(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
176 setCommonPart(extractCommonPart(wrapDoc));
177 sanitize(getCommonPart());
178 getServiceContext().setOutput(account);
182 public void handleGetAll(DocumentWrapper<List<AccountsCommon>> wrapDoc) throws Exception {
183 AccountsCommonList accList = extractCommonPartList(wrapDoc);
184 setCommonPartList(accList);
185 getServiceContext().setOutput(getCommonPartList());
189 public AccountsCommon extractCommonPart(
190 DocumentWrapper<AccountsCommon> wrapDoc)
192 return wrapDoc.getWrappedObject();
196 public void fillCommonPart(AccountsCommon obj, DocumentWrapper<AccountsCommon> wrapDoc)
198 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
202 public AccountsCommonList extractCommonPartList(
203 DocumentWrapper<List<AccountsCommon>> wrapDoc)
206 AccountsCommonList accList = this.extractPagingInfo(new AccountsCommonList(), wrapDoc);
207 // AccountsCommonList accList = new AccountsCommonList();
208 List<AccountListItem> list = accList.getAccountListItem();
210 for (Object obj : wrapDoc.getWrappedObject()) {
211 AccountsCommon account = (AccountsCommon) obj;
212 AccountListItem accListItem = new AccountListItem();
213 accListItem.setScreenName(account.getScreenName());
214 accListItem.setUserid(account.getUserId());
215 accListItem.setTenantid(account.getTenants().get(0).getTenantId()); // pick the default/first tenant
216 accListItem.setTenants(account.getTenants());
217 accListItem.setEmail(account.getEmail());
218 accListItem.setStatus(account.getStatus());
219 String id = account.getCsid();
220 accListItem.setUri(getServiceContextPath() + id);
221 accListItem.setCsid(id);
222 list.add(accListItem);
228 public AccountsCommon getCommonPart() {
233 public void setCommonPart(AccountsCommon account) {
234 this.account = account;
238 public AccountsCommonList getCommonPartList() {
243 public void setCommonPartList(AccountsCommonList accountList) {
244 this.accountList = accountList;
248 public String getQProperty(
254 public DocumentFilter createDocumentFilter() {
255 DocumentFilter filter = new AccountJpaFilter(this.getServiceContext());
259 private void setTenant(AccountsCommon account) {
260 //set tenant only if not available from input
261 ServiceContext ctx = getServiceContext();
262 if (account.getTenants() == null || account.getTenants().size() == 0) {
263 if (ctx.getTenantId() != null) {
264 AccountTenant at = new AccountTenant();
265 at.setTenantId(ctx.getTenantId());
266 List<AccountTenant> atList = new ArrayList<AccountTenant>();
268 account.setTenants(atList);
274 * sanitize removes data not needed to be sent to the consumer
278 public void sanitize(DocumentWrapper<AccountsCommon> wrapDoc) {
279 AccountsCommon account = wrapDoc.getWrappedObject();
283 private void sanitize(AccountsCommon account) {
284 account.setPassword(null);
285 if (!SecurityUtils.isCSpaceAdmin()) {
286 account.setTenants(new ArrayList<AccountTenant>(0));
291 * @see org.collectionspace.services.common.document.DocumentHandler#initializeDocumentFilter(org.collectionspace.services.common.context.ServiceContext)
293 public void initializeDocumentFilter(ServiceContext<AccountsCommon, AccountsCommon> ctx) {
294 // set a default document filter in this method