use of io.apiman.manager.api.beans.actions.ContractActionDto in project apiman by apiman.
the class ActionService method approveContract.
public void approveContract(ContractActionDto action, String approverId) {
// Must exist
ContractBean contract = tryAction(() -> storage.getContract(action.getContractId()));
if (contract == null) {
throw ExceptionFactory.actionException(Messages.i18n.format("ContractDoesNotExist"));
}
// Must be in AwaitingApproval state (no need to approve otherwise!)
if (contract.getStatus() != ContractStatus.AwaitingApproval) {
throw ExceptionFactory.invalidContractStatus(ContractStatus.AwaitingApproval, contract.getStatus());
}
// We probably need an optimised query :-).
ClientVersionBean cvb = contract.getClient();
ApiVersionBean avb = contract.getApi();
OrganizationBean org = avb.getApi().getOrganization();
PlanVersionBean plan = contract.getPlan();
OrganizationBean orgA = avb.getApi().getOrganization();
OrganizationBean orgC = cvb.getClient().getOrganization();
UserBean approver = tryAction(() -> storage.getUser(approverId));
// Set the contract to approved state and send approved event.
contract.setStatus(ContractStatus.Created);
LOGGER.debug("{0} approved a contract: {1} -> {2}", approverId, contract, action);
// In the second phase we need to check the other contracts to see whether they are all in the 'ready' state
// If so, then it's ready to publish.
List<ContractBean> contracts = tryAction(() -> Streams.stream((storage.getAllContracts(org.getId(), cvb.getClient().getId(), cvb.getVersion())))).collect(Collectors.toList());
List<ContractBean> awaitingApprovalList = contracts.stream().filter(c -> c.getStatus().equals(ContractStatus.AwaitingApproval)).collect(Collectors.toList());
if (awaitingApprovalList.size() > 0) {
LOGGER.debug("A contract was approved, but {0} other contracts are still awaiting approval, " + "so client version {1} will remain in its pending state until the remaining contract approvals " + "are granted: {2}.", awaitingApprovalList.size(), cvb.getVersion(), awaitingApprovalList);
} else {
LOGGER.debug("All contracts for {0} have been approved", cvb.getVersion());
tryAction(() -> {
if (clientValidator.isReady(cvb)) {
// Set client to ready status and fire change status event
LOGGER.debug("Client set to ready as all contracts have been approved");
cvb.setStatus(ClientStatus.Ready);
clientAppService.fireClientStatusChangeEvent(cvb, ClientStatus.AwaitingApproval);
// If auto-promote, then we immediately register.
if (action.isAutoPromote()) {
LOGGER.debug("Auto approving: {0}", cvb);
registerClient(orgC.getId(), cvb.getClient().getId(), cvb.getVersion());
}
}
});
}
// storage.flush();
fireContractApprovedEvent(approver, contract, orgC, cvb, orgA, avb, plan);
}
Aggregations