@VisibleForTesting <T extends OverdueCheckNotificationKey> Iterable<NotificationEventWithMetadata<T>> getFutureNotificationsForAccountInTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final NotificationQueue checkOverdueQueue, final Class<T> clazz, final InternalCallContext context) { return checkOverdueQueue.getFutureNotificationFromTransactionForSearchKeys(context.getAccountRecordId(), context.getTenantRecordId(), entitySqlDaoWrapperFactory.getHandle().getConnection()); }
@Override public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception { // Check if we already have notifications for that key final Class<T> clazz = (Class<T>) notificationKey.getClass(); final Iterable<NotificationEventWithMetadata<T>> futureNotifications = getFutureNotificationsForAccountInTransaction(entitySqlDaoWrapperFactory, overdueQueue, clazz, context); final boolean shouldInsertNewNotification = cleanupFutureNotificationsFormTransaction(entitySqlDaoWrapperFactory, futureNotifications, futureNotificationTime, overdueQueue); if (shouldInsertNewNotification) { log.debug("Queuing overdue check notification. Account id: {}, timestamp: {}", accountId.toString(), futureNotificationTime.toString()); overdueQueue.recordFutureNotificationFromTransaction(entitySqlDaoWrapperFactory.getHandle().getConnection(), futureNotificationTime, notificationKey, context.getUserToken(), context.getAccountRecordId(), context.getTenantRecordId()); } else { log.debug("Skipping queuing overdue check notification. Account id: {}, timestamp: {}", accountId.toString(), futureNotificationTime.toString()); } return null; } });
@Override public void create(final TagModelDao tag, final InternalCallContext context) throws TagApiException { if (tagStore.get(tag.getObjectId()) == null) { tagStore.put(tag.getObjectId(), new ArrayList<TagModelDao>()); } // add it to the account tags if (tagStore.get(getAccountId(context.getAccountRecordId())) == null) { tagStore.put(getAccountId(context.getAccountRecordId()), new ArrayList<TagModelDao>()); } tagStore.get(tag.getObjectId()).add(tag); tagStore.get(getAccountId(context.getAccountRecordId())).add(tag); }
private OverdueChangeInternalEvent createOverdueEvent(final ImmutableAccountData overdueable, final String previousOverdueStateName, final String nextOverdueStateName, final boolean isBlockedBilling, final boolean isUnblockedBilling, final InternalCallContext context) throws BlockingApiException { return new DefaultOverdueChangeEvent(overdueable.getId(), previousOverdueStateName, nextOverdueStateName, isBlockedBilling, isUnblockedBilling, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); }
private void recordFutureNotificationFromTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final DateTime effectiveDate, final NotificationEvent notificationKey, final InternalCallContext context) { try { final NotificationQueue subscriptionEventQueue = notificationQueueService.getNotificationQueue(KILLBILL_SERVICES.SUBSCRIPTION_BASE_SERVICE.getServiceName(), DefaultSubscriptionBaseService.NOTIFICATION_QUEUE_NAME); subscriptionEventQueue.recordFutureNotificationFromTransaction(entitySqlDaoWrapperFactory.getHandle().getConnection(), effectiveDate, notificationKey, context.getUserToken(), context.getAccountRecordId(), context.getTenantRecordId()); } catch (final NoSuchNotificationQueue e) { throw new RuntimeException(e); } catch (final IOException e) { throw new RuntimeException(e); } }
private boolean isAccountTaggedWith_OVERDUE_ENFORCEMENT_OFF(final InternalCallContext context) throws OverdueException { try { final UUID accountId = accountApi.getByRecordId(context.getAccountRecordId(), context); final List<Tag> accountTags = tagApi.getTags(accountId, ObjectType.ACCOUNT, context); for (final Tag cur : accountTags) { if (cur.getTagDefinitionId().equals(ControlTagType.OVERDUE_ENFORCEMENT_OFF.getId())) { return true; } } return false; } catch (final AccountApiException e) { throw new OverdueException(e); } } }
private void notifyBusOfRequestedChange(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final DefaultSubscriptionBase subscription, final SubscriptionBaseEvent nextEvent, final SubscriptionBaseTransitionType transitionType, final InternalCallContext context) { try { eventBus.postFromTransaction(new DefaultRequestedSubscriptionEvent(subscription, nextEvent, transitionType, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()), entitySqlDaoWrapperFactory.getHandle().getConnection()); } catch (final EventBusException e) { log.warn("Failed to post requested change event for subscriptionId='{}'", subscription.getId(), e); } }
@Test(groups = "slow") public void testCreateInternalCallContextWithAccountRecordIdFromAccountObjectType() throws Exception { final UUID accountId = UUID.randomUUID(); final Long accountRecordId = 19384012L; final ImmutableAccountData immutableAccountData = Mockito.mock(ImmutableAccountData.class); Mockito.when(immutableAccountInternalApi.getImmutableAccountDataByRecordId(Mockito.<Long>eq(accountRecordId), Mockito.<InternalTenantContext>any())).thenReturn(immutableAccountData); dbi.withHandle(new HandleCallback<Void>() { @Override public Void withHandle(final Handle handle) throws Exception { // Note: we always create an accounts table, see MysqlTestingHelper handle.execute("insert into accounts (record_id, id, external_key, email, name, first_name_length, reference_time, time_zone, created_date, created_by, updated_date, updated_by) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", accountRecordId, accountId.toString(), accountId.toString(), "yo@t.com", "toto", 4, new Date(), "UTC", new Date(), "i", new Date(), "j"); return null; } }); final InternalCallContext context = internalCallContextFactory.createInternalCallContext(accountId, ObjectType.ACCOUNT, callContext); // The account record id should have been looked up in the accounts table Assert.assertEquals(context.getAccountRecordId(), accountRecordId); verifyInternalCallContext(context); }
protected void postBusEventFromTransaction(final TagDefinitionModelDao tagDefinition, final TagDefinitionModelDao savedTagDefinition, final ChangeType changeType, final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final InternalCallContext context) throws BillingExceptionBase { final TagDefinitionInternalEvent tagDefinitionEvent; final boolean isControlTag = TagModelDaoHelper.isControlTag(tagDefinition.getName()); switch (changeType) { case INSERT: tagDefinitionEvent = (isControlTag) ? tagEventBuilder.newControlTagDefinitionCreationEvent(tagDefinition.getId(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()) : tagEventBuilder.newUserTagDefinitionCreationEvent(tagDefinition.getId(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); break; case DELETE: tagDefinitionEvent = (isControlTag) ? tagEventBuilder.newControlTagDefinitionDeletionEvent(tagDefinition.getId(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()) : tagEventBuilder.newUserTagDefinitionDeletionEvent(tagDefinition.getId(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); break; default: return; } try { bus.postFromTransaction(tagDefinitionEvent, entitySqlDaoWrapperFactory.getHandle().getConnection()); } catch (final PersistentBus.EventBusException e) { log.warn("Failed to post tag definition event for tagDefinitionId='{}'", tagDefinition.getId().toString(), e); } }
private void notifyBusOfEffectiveImmediateChange(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final DefaultSubscriptionBase subscription, final SubscriptionBaseEvent immediateEvent, final int seqId, final InternalCallContext context) { try { final SubscriptionBaseTransitionData transition = subscription.getTransitionFromEvent(immediateEvent, seqId); if (transition != null) { final BusEvent busEvent = new DefaultEffectiveSubscriptionEvent(transition, subscription.getAlignStartDate(), context.getUserToken(), context.getAccountRecordId(), context.getTenantRecordId()); eventBus.postFromTransaction(busEvent, entitySqlDaoWrapperFactory.getHandle().getConnection()); } } catch (final EventBusException e) { log.warn("Failed to post effective event for subscriptionId='{}'", subscription.getId(), e); } }
@Override public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws EntityPersistenceException, EventBusException { final AccountSqlDao transactional = entitySqlDaoWrapperFactory.become(AccountSqlDao.class); final AccountModelDao currentAccount = transactional.getById(accountId.toString(), context); if (currentAccount == null) { throw new EntityPersistenceException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_ID, accountId); } // Check if an update is really needed. If not, bail early to avoid sending an extra event on the bus if ((currentAccount.getPaymentMethodId() == null && paymentMethodId == null) || (currentAccount.getPaymentMethodId() != null && currentAccount.getPaymentMethodId().equals(paymentMethodId))) { return null; } final String thePaymentMethodId = paymentMethodId != null ? paymentMethodId.toString() : null; final AccountModelDao account = (AccountModelDao) transactional.updatePaymentMethod(accountId.toString(), thePaymentMethodId, context); final AccountChangeInternalEvent changeEvent = new DefaultAccountChangeEvent(accountId, currentAccount, account, 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; } });
private void recordFutureNotificationFromTransaction(final EntitySqlDaoWrapperFactory transactionalDao, final DateTime effectiveDate, final NotificationEvent notificationKey, final InternalCallContext context) { try { final NotificationQueue subscriptionEventQueue = notificationQueueService.getNotificationQueue(KILLBILL_SERVICES.SUBSCRIPTION_BASE_SERVICE.getServiceName(), DefaultSubscriptionBaseService.NOTIFICATION_QUEUE_NAME); subscriptionEventQueue.recordFutureNotificationFromTransaction(null, effectiveDate, notificationKey, context.getUserToken(), context.getAccountRecordId(), context.getTenantRecordId()); } catch (final NoSuchNotificationQueue e) { throw new RuntimeException(e); } catch (final IOException e) { throw new RuntimeException(e); } }
@VisibleForTesting DateTime getEffectiveDateForNewBCD(final int bcd, @Nullable final LocalDate effectiveFromDate, final InternalCallContext internalCallContext) { if (internalCallContext.getAccountRecordId() == null) { throw new IllegalStateException("Need to have a valid context with accountRecordId"); } // Today as seen by this account final LocalDate startDate = effectiveFromDate != null ? effectiveFromDate : internalCallContext.toLocalDate(internalCallContext.getCreatedDate()); // We want to compute a LocalDate in account TZ which maps to the provided 'bcd' and then compute an effectiveDate for when that BCD_CHANGE event needs to be triggered // // There is a bit of complexity to make sure the date we chose exists (e.g: a BCD of 31 in a february month would not make sense). final int currentDay = startDate.getDayOfMonth(); final int lastDayOfMonth = startDate.dayOfMonth().getMaximumValue(); final LocalDate requestedDate; if (bcd < currentDay) { final LocalDate startDatePlusOneMonth = startDate.plusMonths(1); final int lastDayOfNextMonth = startDatePlusOneMonth.dayOfMonth().getMaximumValue(); final int originalBCDORLastDayOfMonth = bcd <= lastDayOfNextMonth ? bcd : lastDayOfNextMonth; requestedDate = new LocalDate(startDatePlusOneMonth.getYear(), startDatePlusOneMonth.getMonthOfYear(), originalBCDORLastDayOfMonth); } else if (bcd == currentDay && effectiveFromDate == null) { // will default to immediate event requestedDate = null; } else if (bcd <= lastDayOfMonth) { requestedDate = new LocalDate(startDate.getYear(), startDate.getMonthOfYear(), bcd); } else /* bcd > lastDayOfMonth && bcd > currentDay */ { requestedDate = new LocalDate(startDate.getYear(), startDate.getMonthOfYear(), lastDayOfMonth); } return requestedDate == null ? internalCallContext.getCreatedDate() : internalCallContext.toUTCDateTime(requestedDate); }
@Override protected void postBusEventFromTransaction(final CustomFieldModelDao customField, final CustomFieldModelDao savedCustomField, final ChangeType changeType, final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final InternalCallContext context) throws BillingExceptionBase { BusInternalEvent customFieldEvent = null; switch (changeType) { case INSERT: customFieldEvent = new DefaultCustomFieldCreationEvent(customField.getId(), customField.getObjectId(), customField.getObjectType(), context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); break; case DELETE: customFieldEvent = new DefaultCustomFieldDeletionEvent(customField.getId(), customField.getObjectId(), customField.getObjectType(), context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); break; default: return; } try { bus.postFromTransaction(customFieldEvent, entitySqlDaoWrapperFactory.getHandle().getConnection()); } catch (final PersistentBus.EventBusException e) { log.warn("Failed to post tag event for customFieldId='{}'", customField.getId().toString(), e); } }
tagEvent = (isControlTag) ? tagEventBuilder.newControlTagCreationEvent(tag.getId(), tag.getObjectId(), tag.getObjectType(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()) : tagEventBuilder.newUserTagCreationEvent(tag.getId(), tag.getObjectId(), tag.getObjectType(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); break; case DELETE: tagEvent = (isControlTag) ? tagEventBuilder.newControlTagDeletionEvent(tag.getId(), tag.getObjectId(), tag.getObjectType(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()) : tagEventBuilder.newUserTagDeletionEvent(tag.getId(), tag.getObjectId(), tag.getObjectType(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); break; default:
@Override protected void postBusEventFromTransaction(final AccountModelDao account, final AccountModelDao savedAccount, final ChangeType changeType, final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final InternalCallContext context) throws BillingExceptionBase { // This is only called for the create call (see update below) switch (changeType) { case INSERT: break; default: return; } final Long recordId = savedAccount.getRecordId(); // We need to re-hydrate the callcontext with the account record id final InternalCallContext rehydratedContext = internalCallContextFactory.createInternalCallContext(savedAccount, recordId, context); final AccountCreationInternalEvent creationEvent = new DefaultAccountCreationEvent(new DefaultAccountData(savedAccount), savedAccount.getId(), rehydratedContext.getAccountRecordId(), rehydratedContext.getTenantRecordId(), rehydratedContext.getUserToken()); try { eventBus.postFromTransaction(creationEvent, entitySqlDaoWrapperFactory.getHandle().getConnection()); } catch (final EventBusException e) { log.warn("Failed to post account creation event for accountId='{}'", savedAccount.getId(), e); } }
public InternalCallContext(final InternalCallContext context, final DateTime updatedDate) { this(context.getTenantRecordId(), context.getAccountRecordId(), context.getFixedOffsetTimeZone(), context.getReferenceDateTime(), context.getUserToken(), context.getCreatedBy(), context.getCallOrigin(), context.getContextUserType(), context.getReasonCode(), context.getComments(), context.getCreatedDate(), updatedDate); }
@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; } });
private void notifyBusOfEffectiveImmediateChange(final DefaultSubscriptionBase subscription, final SubscriptionBaseEvent immediateEvent, final int seqId, final InternalCallContext context) { try { final SubscriptionBaseTransitionData transition = subscription.getTransitionFromEvent(immediateEvent, seqId); final BusEvent busEvent = new DefaultEffectiveSubscriptionEvent(transition, subscription.getAlignStartDate(), context.getUserToken(), context.getAccountRecordId(), context.getTenantRecordId()); eventBus.post(busEvent); } catch (final EventBusException e) { log.warn("Failed to post effective event for subscription " + subscription.getId(), e); } }
@Override public TagDefinitionModelDao inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception { final TagDefinitionSqlDao tagDefinitionSqlDao = entitySqlDaoWrapperFactory.become(TagDefinitionSqlDao.class); // Make sure the tag definition doesn't exist already final TagDefinitionModelDao existingDefinition = tagDefinitionSqlDao.getByName(definitionName, context); if (existingDefinition != null) { throw new TagDefinitionApiException(ErrorCode.TAG_DEFINITION_ALREADY_EXISTS, definitionName); } // Create it final TagDefinitionModelDao tagDefinition = new TagDefinitionModelDao(context.getCreatedDate(), definitionName, description, tagDefinitionObjectTypes); createAndRefresh(tagDefinitionSqlDao, tagDefinition, context); // Post an event to the bus final boolean isControlTag = TagModelDaoHelper.isControlTag(tagDefinition.getName()); final TagDefinitionInternalEvent tagDefinitionEvent; if (isControlTag) { tagDefinitionEvent = tagEventBuilder.newControlTagDefinitionCreationEvent(tagDefinition.getId(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); } else { tagDefinitionEvent = tagEventBuilder.newUserTagDefinitionCreationEvent(tagDefinition.getId(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); } try { bus.postFromTransaction(tagDefinitionEvent, entitySqlDaoWrapperFactory.getHandle().getConnection()); } catch (final PersistentBus.EventBusException e) { log.warn("Failed to post tag definition creation event for tagDefinitionId='{}'", tagDefinition.getId(), e); } return tagDefinition; } });