use of org.killbill.billing.util.callcontext.TenantContext in project killbill by killbill.
the class BeatrixListener method computeExtBusEventEntryFromBusInternalEvent.
private BusEvent computeExtBusEventEntryFromBusInternalEvent(final BusInternalEvent event, final InternalCallContext context) throws JsonProcessingException {
ObjectType objectType = null;
UUID objectId = null;
ExtBusEventType eventBusType = null;
String metaData = null;
UUID accountId = null;
switch(event.getBusEventType()) {
case ACCOUNT_CREATE:
final AccountCreationInternalEvent realEventACR = (AccountCreationInternalEvent) event;
objectType = ObjectType.ACCOUNT;
objectId = realEventACR.getId();
eventBusType = ExtBusEventType.ACCOUNT_CREATION;
break;
case ACCOUNT_CHANGE:
final AccountChangeInternalEvent realEventACH = (AccountChangeInternalEvent) event;
objectType = ObjectType.ACCOUNT;
objectId = realEventACH.getAccountId();
eventBusType = ExtBusEventType.ACCOUNT_CHANGE;
break;
case SUBSCRIPTION_TRANSITION:
final SubscriptionInternalEvent realEventST = (SubscriptionInternalEvent) event;
objectType = ObjectType.SUBSCRIPTION;
objectId = realEventST.getSubscriptionId();
if (realEventST.getTransitionType() == SubscriptionBaseTransitionType.CREATE || realEventST.getTransitionType() == SubscriptionBaseTransitionType.TRANSFER) {
eventBusType = ExtBusEventType.SUBSCRIPTION_CREATION;
} else if (realEventST.getTransitionType() == SubscriptionBaseTransitionType.CANCEL) {
eventBusType = ExtBusEventType.SUBSCRIPTION_CANCEL;
} else if (realEventST.getTransitionType() == SubscriptionBaseTransitionType.PHASE) {
eventBusType = ExtBusEventType.SUBSCRIPTION_PHASE;
} else if (realEventST.getTransitionType() == SubscriptionBaseTransitionType.CHANGE) {
eventBusType = ExtBusEventType.SUBSCRIPTION_CHANGE;
} else if (realEventST.getTransitionType() == SubscriptionBaseTransitionType.UNCANCEL) {
eventBusType = ExtBusEventType.SUBSCRIPTION_UNCANCEL;
} else if (realEventST.getTransitionType() == SubscriptionBaseTransitionType.BCD_CHANGE) {
eventBusType = ExtBusEventType.SUBSCRIPTION_BCD_CHANGE;
}
SubscriptionMetadata.ActionType actionType = (event instanceof EffectiveSubscriptionInternalEvent) ? ActionType.EFFECTIVE : ActionType.REQUESTED;
final SubscriptionMetadata subscriptionMetadataObj = new SubscriptionMetadata(actionType, realEventST.getBundleExternalKey());
metaData = objectMapper.writeValueAsString(subscriptionMetadataObj);
break;
case BLOCKING_STATE:
final BlockingTransitionInternalEvent realEventBS = (BlockingTransitionInternalEvent) event;
if (realEventBS.getBlockingType() == BlockingStateType.ACCOUNT) {
objectType = ObjectType.ACCOUNT;
} else if (realEventBS.getBlockingType() == BlockingStateType.SUBSCRIPTION_BUNDLE) {
objectType = ObjectType.BUNDLE;
} else if (realEventBS.getBlockingType() == BlockingStateType.SUBSCRIPTION) {
objectType = ObjectType.SUBSCRIPTION;
}
objectId = realEventBS.getBlockableId();
if (EntitlementService.ENTITLEMENT_SERVICE_NAME.equals(realEventBS.getService())) {
if (DefaultEntitlementApi.ENT_STATE_START.equals(realEventBS.getStateName())) {
eventBusType = ExtBusEventType.ENTITLEMENT_CREATION;
} else if (DefaultEntitlementApi.ENT_STATE_BLOCKED.equals(realEventBS.getStateName())) {
eventBusType = ExtBusEventType.BUNDLE_PAUSE;
} else if (DefaultEntitlementApi.ENT_STATE_CLEAR.equals(realEventBS.getStateName())) {
eventBusType = ExtBusEventType.BUNDLE_RESUME;
} else if (DefaultEntitlementApi.ENT_STATE_CANCELLED.equals(realEventBS.getStateName())) {
eventBusType = ExtBusEventType.ENTITLEMENT_CANCEL;
}
} else {
eventBusType = ExtBusEventType.BLOCKING_STATE;
final BlockingStateMetadata metaDataObj = new BlockingStateMetadata(realEventBS.getBlockableId(), realEventBS.getService(), realEventBS.getStateName(), realEventBS.getBlockingType(), realEventBS.getEffectiveDate(), realEventBS.isTransitionedToBlockedBilling(), realEventBS.isTransitionedToUnblockedBilling(), realEventBS.isTransitionedToBlockedEntitlement(), realEventBS.isTransitionedToUnblockedEntitlement());
metaData = objectMapper.writeValueAsString(metaDataObj);
}
break;
case INVOICE_CREATION:
final InvoiceCreationInternalEvent realEventInv = (InvoiceCreationInternalEvent) event;
objectType = ObjectType.INVOICE;
objectId = realEventInv.getInvoiceId();
eventBusType = ExtBusEventType.INVOICE_CREATION;
break;
case INVOICE_NOTIFICATION:
final InvoiceNotificationInternalEvent realEventInvNotification = (InvoiceNotificationInternalEvent) event;
objectType = ObjectType.INVOICE;
objectId = null;
// has to be set here because objectId is null with a dryRun Invoice
accountId = realEventInvNotification.getAccountId();
eventBusType = ExtBusEventType.INVOICE_NOTIFICATION;
break;
case INVOICE_ADJUSTMENT:
final InvoiceAdjustmentInternalEvent realEventInvAdj = (InvoiceAdjustmentInternalEvent) event;
objectType = ObjectType.INVOICE;
objectId = realEventInvAdj.getInvoiceId();
eventBusType = ExtBusEventType.INVOICE_ADJUSTMENT;
break;
case INVOICE_PAYMENT_INFO:
final InvoicePaymentInfoInternalEvent realEventInvPay = (InvoicePaymentInfoInternalEvent) event;
objectType = ObjectType.INVOICE;
objectId = realEventInvPay.getInvoiceId();
eventBusType = ExtBusEventType.INVOICE_PAYMENT_SUCCESS;
break;
case INVOICE_PAYMENT_ERROR:
final InvoicePaymentErrorInternalEvent realEventInvPayErr = (InvoicePaymentErrorInternalEvent) event;
objectType = ObjectType.INVOICE;
objectId = realEventInvPayErr.getInvoiceId();
eventBusType = ExtBusEventType.INVOICE_PAYMENT_FAILED;
break;
case PAYMENT_INFO:
final PaymentInfoInternalEvent realEventPay = (PaymentInfoInternalEvent) event;
objectType = ObjectType.PAYMENT;
objectId = realEventPay.getPaymentId();
eventBusType = ExtBusEventType.PAYMENT_SUCCESS;
final PaymentMetadata paymentInfoMetaDataObj = new PaymentMetadata(realEventPay.getPaymentTransactionId(), realEventPay.getAmount(), realEventPay.getCurrency(), realEventPay.getStatus(), realEventPay.getTransactionType(), realEventPay.getEffectiveDate());
metaData = objectMapper.writeValueAsString(paymentInfoMetaDataObj);
break;
case PAYMENT_ERROR:
final PaymentErrorInternalEvent realEventPayErr = (PaymentErrorInternalEvent) event;
objectType = ObjectType.PAYMENT;
objectId = realEventPayErr.getPaymentId();
eventBusType = ExtBusEventType.PAYMENT_FAILED;
accountId = realEventPayErr.getAccountId();
final PaymentMetadata paymentErrorMetaDataObj = new PaymentMetadata(realEventPayErr.getPaymentTransactionId(), realEventPayErr.getAmount(), realEventPayErr.getCurrency(), realEventPayErr.getStatus(), realEventPayErr.getTransactionType(), realEventPayErr.getEffectiveDate());
metaData = objectMapper.writeValueAsString(paymentErrorMetaDataObj);
break;
case PAYMENT_PLUGIN_ERROR:
final PaymentPluginErrorInternalEvent realEventPayPluginErr = (PaymentPluginErrorInternalEvent) event;
objectType = ObjectType.PAYMENT;
objectId = realEventPayPluginErr.getPaymentId();
eventBusType = ExtBusEventType.PAYMENT_FAILED;
final PaymentMetadata pluginErrorMetaDataObj = new PaymentMetadata(realEventPayPluginErr.getPaymentTransactionId(), realEventPayPluginErr.getAmount(), realEventPayPluginErr.getCurrency(), realEventPayPluginErr.getStatus(), realEventPayPluginErr.getTransactionType(), realEventPayPluginErr.getEffectiveDate());
metaData = objectMapper.writeValueAsString(pluginErrorMetaDataObj);
break;
case OVERDUE_CHANGE:
final OverdueChangeInternalEvent realEventOC = (OverdueChangeInternalEvent) event;
objectType = ObjectType.ACCOUNT;
objectId = realEventOC.getOverdueObjectId();
eventBusType = ExtBusEventType.OVERDUE_CHANGE;
break;
case USER_TAG_CREATION:
final UserTagCreationInternalEvent realUserTagEventCr = (UserTagCreationInternalEvent) event;
objectType = ObjectType.TAG;
objectId = realUserTagEventCr.getTagId();
eventBusType = ExtBusEventType.TAG_CREATION;
break;
case CONTROL_TAG_CREATION:
final ControlTagCreationInternalEvent realTagEventCr = (ControlTagCreationInternalEvent) event;
objectType = ObjectType.TAG;
objectId = realTagEventCr.getTagId();
eventBusType = ExtBusEventType.TAG_CREATION;
break;
case USER_TAG_DELETION:
final UserTagDeletionInternalEvent realUserTagEventDel = (UserTagDeletionInternalEvent) event;
objectType = ObjectType.TAG;
objectId = realUserTagEventDel.getTagId();
eventBusType = ExtBusEventType.TAG_DELETION;
break;
case CONTROL_TAG_DELETION:
final ControlTagDeletionInternalEvent realTagEventDel = (ControlTagDeletionInternalEvent) event;
objectType = ObjectType.TAG;
objectId = realTagEventDel.getTagId();
eventBusType = ExtBusEventType.TAG_DELETION;
break;
case CUSTOM_FIELD_CREATION:
final CustomFieldCreationEvent realCustomFieldEventCr = (CustomFieldCreationEvent) event;
objectType = ObjectType.CUSTOM_FIELD;
objectId = realCustomFieldEventCr.getCustomFieldId();
eventBusType = ExtBusEventType.CUSTOM_FIELD_CREATION;
break;
case CUSTOM_FIELD_DELETION:
final CustomFieldDeletionEvent realCustomFieldEventDel = (CustomFieldDeletionEvent) event;
objectType = ObjectType.CUSTOM_FIELD;
objectId = realCustomFieldEventDel.getCustomFieldId();
eventBusType = ExtBusEventType.CUSTOM_FIELD_DELETION;
break;
case TENANT_CONFIG_CHANGE:
final TenantConfigChangeInternalEvent realTenantConfigEventChg = (TenantConfigChangeInternalEvent) event;
objectType = ObjectType.TENANT_KVS;
objectId = realTenantConfigEventChg.getId();
eventBusType = ExtBusEventType.TENANT_CONFIG_CHANGE;
metaData = realTenantConfigEventChg.getKey();
break;
case TENANT_CONFIG_DELETION:
final TenantConfigDeletionInternalEvent realTenantConfigEventDel = (TenantConfigDeletionInternalEvent) event;
objectType = ObjectType.TENANT_KVS;
objectId = null;
eventBusType = ExtBusEventType.TENANT_CONFIG_DELETION;
metaData = realTenantConfigEventDel.getKey();
break;
case BROADCAST_SERVICE:
final BroadcastInternalEvent realBroadcastEvent = (BroadcastInternalEvent) event;
objectType = ObjectType.SERVICE_BROADCAST;
objectId = null;
eventBusType = ExtBusEventType.BROADCAST_SERVICE;
final BroadcastMetadata metaDataObj = new BroadcastMetadata(realBroadcastEvent.getServiceName(), realBroadcastEvent.getType(), realBroadcastEvent.getJsonEvent());
metaData = objectMapper.writeValueAsString(metaDataObj);
break;
default:
}
final TenantContext tenantContext = internalCallContextFactory.createTenantContext(context);
// See #275
accountId = (accountId == null) ? getAccountId(event.getBusEventType(), objectId, objectType, tenantContext) : accountId;
return eventBusType != null ? new DefaultBusExternalEvent(objectId, objectType, eventBusType, accountId, tenantContext.getTenantId(), metaData, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()) : null;
}
use of org.killbill.billing.util.callcontext.TenantContext in project killbill by killbill.
the class InvoiceResource method getTemplateResource.
private Response getTemplateResource(@Nullable final String localeStr, final TenantKey tenantKey, final HttpServletRequest request) throws InvoiceApiException, TenantApiException {
final TenantContext tenantContext = context.createContext(request);
final String tenantKeyStr = localeStr != null ? LocaleUtils.localeString(LocaleUtils.toLocale(localeStr), tenantKey.toString()) : tenantKey.toString();
final List<String> result = tenantApi.getTenantValuesForKey(tenantKeyStr, tenantContext);
return result.isEmpty() ? Response.status(Status.NOT_FOUND).build() : Response.status(Status.OK).entity(result.get(0)).build();
}
use of org.killbill.billing.util.callcontext.TenantContext in project killbill by killbill.
the class InvoiceResource method getPayments.
@TimedResource
@GET
@Path("/{invoiceId:" + UUID_PATTERN + "}/" + PAYMENTS)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve payments associated with an invoice", response = InvoicePaymentJson.class, responseContainer = "List")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid invoice id supplied"), @ApiResponse(code = 404, message = "Invoice not found") })
public Response getPayments(@PathParam("invoiceId") final String invoiceId, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @QueryParam(QUERY_WITH_PLUGIN_INFO) @DefaultValue("false") final Boolean withPluginInfo, @QueryParam(QUERY_WITH_ATTEMPTS) @DefaultValue("false") final Boolean withAttempts, @javax.ws.rs.core.Context final HttpServletRequest request) throws PaymentApiException, InvoiceApiException {
final TenantContext tenantContext = context.createContext(request);
final Invoice invoice = invoiceApi.getInvoice(UUID.fromString(invoiceId), tenantContext);
// Extract unique set of paymentId for this invoice
final Set<UUID> invoicePaymentIds = ImmutableSet.copyOf(Iterables.transform(invoice.getPayments(), new Function<InvoicePayment, UUID>() {
@Override
public UUID apply(final InvoicePayment input) {
return input.getPaymentId();
}
}));
if (invoicePaymentIds.isEmpty()) {
return Response.status(Status.OK).entity(ImmutableList.<InvoicePaymentJson>of()).build();
}
final List<Payment> payments = new ArrayList<Payment>();
for (final UUID paymentId : invoicePaymentIds) {
final Payment payment = paymentApi.getPayment(paymentId, withPluginInfo, withAttempts, ImmutableList.<PluginProperty>of(), tenantContext);
payments.add(payment);
}
final Iterable<InvoicePaymentJson> result = INVOICE_PAYMENT_ORDERING.sortedCopy(Iterables.transform(payments, new Function<Payment, InvoicePaymentJson>() {
@Override
public InvoicePaymentJson apply(final Payment input) {
return new InvoicePaymentJson(input, invoice.getId(), null);
}
}));
return Response.status(Status.OK).entity(result).build();
}
use of org.killbill.billing.util.callcontext.TenantContext in project killbill by killbill.
the class InvoiceResource method getTags.
@TimedResource
@GET
@Path("/{invoiceId:" + UUID_PATTERN + "}/" + TAGS)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve invoice tags", response = TagJson.class, responseContainer = "List")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid invoice id supplied"), @ApiResponse(code = 404, message = "Invoice not found") })
public Response getTags(@PathParam(ID_PARAM_NAME) final String invoiceIdString, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @QueryParam(QUERY_INCLUDED_DELETED) @DefaultValue("false") final Boolean includedDeleted, @javax.ws.rs.core.Context final HttpServletRequest request) throws TagDefinitionApiException, InvoiceApiException {
final UUID invoiceId = UUID.fromString(invoiceIdString);
final TenantContext tenantContext = context.createContext(request);
final Invoice invoice = invoiceApi.getInvoice(invoiceId, tenantContext);
return super.getTags(invoice.getAccountId(), invoiceId, auditMode, includedDeleted, tenantContext);
}
use of org.killbill.billing.util.callcontext.TenantContext in project killbill by killbill.
the class AccountResource method getAccountTimeline.
@TimedResource
@GET
@Path("/{accountId:" + UUID_PATTERN + "}/" + TIMELINE)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve account timeline", response = AccountTimelineJson.class)
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid account id supplied"), @ApiResponse(code = 404, message = "Account not found") })
public Response getAccountTimeline(@PathParam("accountId") final String accountIdString, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @QueryParam(QUERY_PARALLEL) @DefaultValue("false") final Boolean parallel, @javax.ws.rs.core.Context final HttpServletRequest request) throws AccountApiException, PaymentApiException, SubscriptionApiException, InvoiceApiException, CatalogApiException {
final TenantContext tenantContext = context.createContext(request);
final UUID accountId = UUID.fromString(accountIdString);
final Account account = accountUserApi.getAccountById(accountId, tenantContext);
final Callable<List<SubscriptionBundle>> bundlesCallable = new Callable<List<SubscriptionBundle>>() {
@Override
public List<SubscriptionBundle> call() throws Exception {
return subscriptionApi.getSubscriptionBundlesForAccountId(accountId, tenantContext);
}
};
final Callable<List<Invoice>> invoicesCallable = new Callable<List<Invoice>>() {
@Override
public List<Invoice> call() throws Exception {
return invoiceApi.getInvoicesByAccount(accountId, false, tenantContext);
}
};
final Callable<List<InvoicePayment>> invoicePaymentsCallable = new Callable<List<InvoicePayment>>() {
@Override
public List<InvoicePayment> call() throws Exception {
return invoicePaymentApi.getInvoicePaymentsByAccount(accountId, tenantContext);
}
};
final Callable<List<Payment>> paymentsCallable = new Callable<List<Payment>>() {
@Override
public List<Payment> call() throws Exception {
return paymentApi.getAccountPayments(accountId, false, false, ImmutableList.<PluginProperty>of(), tenantContext);
}
};
final Callable<AccountAuditLogs> auditsCallable = new Callable<AccountAuditLogs>() {
@Override
public AccountAuditLogs call() throws Exception {
return auditUserApi.getAccountAuditLogs(accountId, auditMode.getLevel(), tenantContext);
}
};
final AccountTimelineJson json;
List<Invoice> invoices = null;
List<SubscriptionBundle> bundles = null;
List<InvoicePayment> invoicePayments = null;
List<Payment> payments = null;
AccountAuditLogs accountAuditLogs = null;
if (parallel) {
final ExecutorService executor = jaxrsExecutors.getJaxrsExecutorService();
final Future<List<SubscriptionBundle>> futureBundlesCallable = executor.submit(bundlesCallable);
final Future<List<Invoice>> futureInvoicesCallable = executor.submit(invoicesCallable);
final Future<List<InvoicePayment>> futureInvoicePaymentsCallable = executor.submit(invoicePaymentsCallable);
final Future<List<Payment>> futurePaymentsCallable = executor.submit(paymentsCallable);
final Future<AccountAuditLogs> futureAuditsCallable = executor.submit(auditsCallable);
final ImmutableList<Future> toBeCancelled = ImmutableList.<Future>of(futureBundlesCallable, futureInvoicesCallable, futureInvoicePaymentsCallable, futurePaymentsCallable, futureAuditsCallable);
final int timeoutMsec = 100;
final long ini = System.currentTimeMillis();
do {
bundles = (bundles == null) ? waitOnFutureAndHandleTimeout("bundles", futureBundlesCallable, timeoutMsec, toBeCancelled) : bundles;
invoices = (invoices == null) ? waitOnFutureAndHandleTimeout("invoices", futureInvoicesCallable, timeoutMsec, toBeCancelled) : invoices;
invoicePayments = (invoicePayments == null) ? waitOnFutureAndHandleTimeout("invoicePayments", futureInvoicePaymentsCallable, timeoutMsec, toBeCancelled) : invoicePayments;
payments = (payments == null) ? waitOnFutureAndHandleTimeout("payments", futurePaymentsCallable, timeoutMsec, toBeCancelled) : payments;
accountAuditLogs = (accountAuditLogs == null) ? waitOnFutureAndHandleTimeout("accountAuditLogs", futureAuditsCallable, timeoutMsec, toBeCancelled) : accountAuditLogs;
} while ((System.currentTimeMillis() - ini < jaxrsConfig.getJaxrsTimeout().getMillis()) && (bundles == null || invoices == null || invoicePayments == null || payments == null || accountAuditLogs == null));
if (bundles == null || invoices == null || invoicePayments == null || payments == null || accountAuditLogs == null) {
Response.status(Status.SERVICE_UNAVAILABLE).build();
}
} else {
invoices = runCallable("invoices", invoicesCallable);
payments = runCallable("payments", paymentsCallable);
bundles = runCallable("bundles", bundlesCallable);
accountAuditLogs = runCallable("accountAuditLogs", auditsCallable);
invoicePayments = runCallable("invoicePayments", invoicePaymentsCallable);
}
json = new AccountTimelineJson(account, invoices, payments, invoicePayments, bundles, accountAuditLogs);
return Response.status(Status.OK).entity(json).build();
}
Aggregations