@Override public UUID getIdFromKey(final String externalKey, final InternalTenantContext context) throws AccountApiException { if (externalKey == null) { throw new AccountApiException(ErrorCode.ACCOUNT_CANNOT_MAP_NULL_KEY, ""); } return transactionalSqlDao.execute(true, new EntitySqlDaoTransactionWrapper<UUID>() { @Override public UUID inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception { return entitySqlDaoWrapperFactory.become(AccountSqlDao.class).getIdFromKey(externalKey, context); } }); }
protected Account getAccountByRecordId(final Long recordId, final InternalTenantContext context) throws AccountApiException { final Account account = getAccountByRecordIdInternal(recordId, context); if (account == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_RECORD_ID, recordId); } return account; }
private AccountModelDao getAccountModelDaoByRecordId(final Long recordId, final InternalTenantContext context) throws AccountApiException { final AccountModelDao accountModelDao = accountDao.getByRecordId(recordId, context); if (accountModelDao == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_RECORD_ID, recordId); } return accountModelDao; }
@Override protected AccountApiException generateAlreadyExistsException(final AccountModelDao account, final InternalCallContext context) { return new AccountApiException(ErrorCode.ACCOUNT_ALREADY_EXISTS, account.getExternalKey()); }
@Override public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception { final AccountEmailSqlDao transactional = entitySqlDaoWrapperFactory.become(AccountEmailSqlDao.class); if (transactional.getById(email.getId().toString(), context) != null) { throw new AccountApiException(ErrorCode.ACCOUNT_EMAIL_ALREADY_EXISTS, email.getId()); } createAndRefresh(transactional, email, context); return null; } });
@Override public void updateAccount(final String externalKey, final AccountData accountData, final CallContext context) throws AccountApiException { final Account currentAccount = getAccountByKey(externalKey, context); if (currentAccount == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_KEY, externalKey); } updateAccount(currentAccount.getId(), accountData, false, context); }
protected Account getAccountById(final UUID accountId, final InternalTenantContext context) throws AccountApiException { final Long recordId = nonEntityDao.retrieveRecordIdFromObject(accountId, ObjectType.ACCOUNT, recordIdCacheController); final Account account = getAccountByRecordIdInternal(recordId, context); if (account == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_ID, accountId); } accountCacheController.putIfAbsent(recordId, new DefaultImmutableAccountData(account)); return account; }
@Override public void updateBCD(final String externalKey, final int bcd, final InternalCallContext context) throws AccountApiException { final Account currentAccount = getAccountByKey(externalKey, context); if (currentAccount == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_KEY, externalKey); } if (currentAccount.getBillCycleDayLocal() != DefaultMutableAccountData.DEFAULT_BILLING_CYCLE_DAY_LOCAL) { throw new AccountApiException(ErrorCode.ACCOUNT_UPDATE_FAILED); } final MutableAccountData mutableAccountData = currentAccount.toMutableAccountData(); mutableAccountData.setBillCycleDayLocal(bcd); final AccountModelDao accountToUpdate = new AccountModelDao(currentAccount.getId(), mutableAccountData); bcdCacheController.remove(currentAccount.getId()); bcdCacheController.putIfAbsent(currentAccount.getId(), new Integer(bcd)); accountDao.update(accountToUpdate, true, context); }
@Override public Account createAccount(final AccountData data, final CallContext context) throws AccountApiException { // Not transactional, but there is a db constraint on that column if (data.getExternalKey() != null && getIdFromKey(data.getExternalKey(), context) != null) { throw new AccountApiException(ErrorCode.ACCOUNT_ALREADY_EXISTS, data.getExternalKey()); } final InternalCallContext internalContext = internalCallContextFactory.createInternalCallContextWithoutAccountRecordId(context); if (data.getParentAccountId() != null) { // verify that parent account exists if parentAccountId is not null final ImmutableAccountData immutableAccountData = immutableAccountInternalApi.getImmutableAccountDataById(data.getParentAccountId(), internalContext); if (immutableAccountData == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_ID, data.getParentAccountId()); } } final AccountModelDao account = new AccountModelDao(data); if (null != account.getExternalKey() && account.getExternalKey().length() > 255) { throw new AccountApiException(ErrorCode.EXTERNAL_KEY_LIMIT_EXCEEDED); } accountDao.create(account, internalCallContextFactory.createInternalCallContextWithoutAccountRecordId(context)); return new DefaultAccount(account); }
protected Account getAccountByKey(final String key, final InternalTenantContext context) throws AccountApiException { final AccountModelDao accountModelDao = accountDao.getAccountByKey(key, context); if (accountModelDao == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_KEY, key); } final Account account = new DefaultAccount(accountModelDao); final Long recordId = nonEntityDao.retrieveRecordIdFromObject(account.getId(), ObjectType.ACCOUNT, recordIdCacheController); accountCacheController.putIfAbsent(recordId, new DefaultImmutableAccountData(account)); return account; }
@Override public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws EventBusException, AccountApiException { final AccountSqlDao transactional = entitySqlDaoWrapperFactory.become(AccountSqlDao.class); final UUID accountId = specifiedAccount.getId(); final AccountModelDao currentAccount = transactional.getById(accountId.toString(), context); if (currentAccount == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_ID, accountId); } specifiedAccount.validateAccountUpdateInput(currentAccount, treatNullValueAsReset); if (!treatNullValueAsReset) { // Set unspecified (null) fields to their current values specifiedAccount.mergeWithDelegate(currentAccount); } transactional.update(specifiedAccount, context); final AccountChangeInternalEvent changeEvent = new DefaultAccountChangeEvent(accountId, currentAccount, specifiedAccount, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken(), context.getCreatedDate()); try { eventBus.postFromTransaction(changeEvent, entitySqlDaoWrapperFactory.getHandle().getConnection()); } catch (final EventBusException e) { log.warn("Failed to post account change event for accountId='{}'", accountId, e); } return null; } });
@Override public void updatePaymentMethod(final UUID accountId, final UUID paymentMethodId, final InternalCallContext context) throws AccountApiException { final AccountModelDao currentAccountModelDao = getById(accountId, context); if (currentAccountModelDao == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_ID, accountId); } final DefaultAccount currentAccount = new DefaultAccount(currentAccountModelDao); final DefaultMutableAccountData updatedAccount = new DefaultMutableAccountData(currentAccount); updatedAccount.setPaymentMethodId(paymentMethodId); update(new AccountModelDao(accountId, updatedAccount), context); }
protected Account getAccountByRecordId(final Long recordId, final InternalTenantContext context) throws AccountApiException { final Account account = getAccountByRecordIdInternal(recordId, context); if (account == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_RECORD_ID, recordId); } return account; }
private AccountModelDao getAccountModelDaoByRecordId(final Long recordId, final InternalTenantContext context) throws AccountApiException { final AccountModelDao accountModelDao = accountDao.getByRecordId(recordId, context); if (accountModelDao == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_RECORD_ID, recordId); } return accountModelDao; }
@Override public UUID getIdFromKey(final String externalKey, final InternalTenantContext context) throws AccountApiException { if (externalKey == null) { throw new AccountApiException(ErrorCode.ACCOUNT_CANNOT_MAP_NULL_KEY, ""); } return transactionalSqlDao.execute(true, new EntitySqlDaoTransactionWrapper<UUID>() { @Override public UUID inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception { return entitySqlDaoWrapperFactory.become(AccountSqlDao.class).getIdFromKey(externalKey, context); } }); }
@Override protected AccountApiException generateAlreadyExistsException(final AccountModelDao account, final InternalCallContext context) { return new AccountApiException(ErrorCode.ACCOUNT_ALREADY_EXISTS, account.getExternalKey()); }
@Override public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception { final AccountEmailSqlDao transactional = entitySqlDaoWrapperFactory.become(AccountEmailSqlDao.class); if (transactional.getById(email.getId().toString(), context) != null) { throw new AccountApiException(ErrorCode.ACCOUNT_EMAIL_ALREADY_EXISTS, email.getId()); } createAndRefresh(transactional, email, context); return null; } });
@Override public void updateAccount(final String externalKey, final AccountData accountData, final CallContext context) throws AccountApiException { final Account currentAccount = getAccountByKey(externalKey, context); if (currentAccount == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_KEY, externalKey); } updateAccount(currentAccount.getId(), accountData, false, context); }
protected Account getAccountById(final UUID accountId, final InternalTenantContext context) throws AccountApiException { final Long recordId = nonEntityDao.retrieveRecordIdFromObject(accountId, ObjectType.ACCOUNT, recordIdCacheController); final Account account = getAccountByRecordIdInternal(recordId, context); if (account == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_ID, accountId); } accountCacheController.putIfAbsent(recordId, new DefaultImmutableAccountData(account)); return account; }
protected Account getAccountByKey(final String key, final InternalTenantContext context) throws AccountApiException { final AccountModelDao accountModelDao = accountDao.getAccountByKey(key, context); if (accountModelDao == null) { throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_KEY, key); } final Account account = new DefaultAccount(accountModelDao); final Long recordId = nonEntityDao.retrieveRecordIdFromObject(account.getId(), ObjectType.ACCOUNT, recordIdCacheController); accountCacheController.putIfAbsent(recordId, new DefaultImmutableAccountData(account)); return account; }