if (quantity > 0 && quantity <= entitlement.getQuantity()) {
Consumer sourceConsumer = entitlement.getConsumer();
Consumer destinationConsumer = consumerCurator.verifyAndLookupConsumer(
uuid);
if (!sourceConsumer.getType().isManifest()) {
throw new BadRequestException(i18n.tr(
"Entitlement migration is not permissible for units of type ''{0}''",
sourceConsumer.getType().getLabel()));
}
if (!destinationConsumer.getType().isManifest()) {
throw new BadRequestException(i18n.tr(
"Entitlement migration is not permissible for units of type ''{0}''",
destinationConsumer.getType().getLabel()));
}
if (!sourceConsumer.getOwner().getKey().equals(destinationConsumer.getOwner().getKey())) {
throw new BadRequestException(i18n.tr(
"Source and destination units must belong to the same organization"));
}
// test to ensure destination can use the pool
ValidationResult result = enforcer.preEntitlement(destinationConsumer,
entitlement.getPool(), 0, CallerType.BIND);
if (!result.isSuccessful()) {
throw new BadRequestException(i18n.tr(
"The entitlement cannot be utilized by the destination unit: ") +
messageTranslator.poolErrorToMessage(entitlement.getPool(),
result.getErrors().get(0)));
}
if (quantity.intValue() == entitlement.getQuantity()) {
unbind(id);
}
else {
entitler.adjustEntitlementQuantity(sourceConsumer, entitlement,
entitlement.getQuantity() - quantity);
}
Pool pool = entitlement.getPool();
entitlements.addAll(entitler.bindByPool(pool.getId(), destinationConsumer,
quantity));
// Trigger events:
entitler.sendEvents(entitlements);
}
else {
throw new BadRequestException(i18n.tr(
"The quantity specified must be greater than zero " +
"and less than or equal to the total for this entitlement"));
}
}
else {