use of io.apiman.manager.api.rest.exceptions.PlanAlreadyExistsException in project apiman by apiman.
the class OrganizationResourceImpl method createPlan.
/**
* @see IOrganizationResource#createPlan(java.lang.String,
* io.apiman.manager.api.beans.plans.NewPlanBean)
*/
@Override
public PlanBean createPlan(String organizationId, NewPlanBean bean) throws OrganizationNotFoundException, PlanAlreadyExistsException, NotAuthorizedException, InvalidNameException {
securityContext.checkPermissions(PermissionType.planEdit, organizationId);
FieldValidator.validateName(bean.getName());
PlanBean newPlan = new PlanBean();
newPlan.setName(bean.getName());
newPlan.setDescription(bean.getDescription());
newPlan.setId(BeanUtils.idFromName(bean.getName()));
newPlan.setCreatedOn(new Date());
newPlan.setCreatedBy(securityContext.getCurrentUser());
try {
// Store/persist the new plan
storage.beginTx();
OrganizationBean orgBean = getOrganizationFromStorage(organizationId);
if (storage.getPlan(orgBean.getId(), newPlan.getId()) != null) {
throw ExceptionFactory.planAlreadyExistsException(newPlan.getName());
}
newPlan.setOrganization(orgBean);
storage.createPlan(newPlan);
storage.createAuditEntry(AuditUtils.planCreated(newPlan, securityContext));
if (bean.getInitialVersion() != null) {
NewPlanVersionBean newPlanVersion = new NewPlanVersionBean();
newPlanVersion.setVersion(bean.getInitialVersion());
createPlanVersionInternal(newPlanVersion, newPlan);
}
storage.commitTx();
// $NON-NLS-1$
log.debug(String.format("Created plan: %s", newPlan));
return newPlan;
} catch (AbstractRestException e) {
storage.rollbackTx();
throw e;
} catch (Exception e) {
storage.rollbackTx();
throw new SystemErrorException(e);
}
}
Aggregations