use of pro.taskana.exceptions.InvalidWorkbasketException in project taskana by Taskana.
the class WorkbasketController method updateWorkbasket.
@PutMapping(path = "/{workbasketId}")
@Transactional(rollbackFor = Exception.class)
public ResponseEntity<WorkbasketResource> updateWorkbasket(@PathVariable(value = "workbasketId") String workbasketId, @RequestBody WorkbasketResource workbasketResource) throws InvalidWorkbasketException, WorkbasketNotFoundException, NotAuthorizedException {
ResponseEntity<WorkbasketResource> result;
if (workbasketId.equals(workbasketResource.workbasketId)) {
Workbasket workbasket = workbasketMapper.toModel(workbasketResource);
workbasket = workbasketService.updateWorkbasket(workbasket);
result = ResponseEntity.ok(workbasketMapper.toResource(workbasket));
} else {
throw new InvalidWorkbasketException("Target-WB-ID('" + workbasketId + "') is not identical with the WB-ID of to object which should be updated. ID=('" + workbasketResource.getId() + "')");
}
return result;
}
use of pro.taskana.exceptions.InvalidWorkbasketException in project taskana by Taskana.
the class WorkbasketDefinitionController method importWorkbaskets.
/**
* This method imports a <b>list of {@link WorkbasketDefinition}</b>. This does not exactly match the REST norm, but
* we want to have an option to import all settings at once. When a logical equal (key and domain are equal)
* workbasket already exists an update will be executed. Otherwise a new workbasket will be created.
*
* @param definitions the list of workbasket definitions which will be imported to the current system.
* @return Return answer is determined by the status code: 200 - all good 400 - list state error (referring to non
* existing id's) 401 - not authorized
*/
@PostMapping(path = "/import")
@Transactional(rollbackFor = Exception.class)
public ResponseEntity<String> importWorkbaskets(@RequestBody List<WorkbasketDefinition> definitions) {
try {
// key: logical ID
// value: system ID (in database)
Map<String, String> systemIds = workbasketService.createWorkbasketQuery().list().stream().collect(Collectors.toMap(this::logicalId, WorkbasketSummary::getId));
// key: old system ID
// value: system ID
Map<String, String> idConversion = new HashMap<>();
// STEP 1: update or create workbaskets from the import
for (WorkbasketDefinition definition : definitions) {
WorkbasketResource res = definition.workbasketResource;
Workbasket workbasket;
String oldId = res.workbasketId;
if (systemIds.containsKey(logicalId(res))) {
res.workbasketId = systemIds.get(logicalId(res));
workbasket = workbasketService.updateWorkbasket(workbasketMapper.toModel(res));
} else {
res.workbasketId = null;
workbasket = workbasketService.createWorkbasket(workbasketMapper.toModel(res));
}
res.workbasketId = oldId;
// simply delete all existing accessItems and create new ones.
for (WorkbasketAccessItem accessItem : workbasketService.getWorkbasketAccessItems(workbasket.getId())) {
workbasketService.deleteWorkbasketAccessItem(accessItem.getId());
}
for (WorkbasketAccessItemResource authorization : definition.authorizations) {
workbasketService.createWorkbasketAccessItem(workbasketAccessItemMapper.toModel(authorization));
}
idConversion.put(definition.workbasketResource.workbasketId, workbasket.getId());
}
// This can not be done in step 1 because the system IDs are only known after step 1
for (WorkbasketDefinition definition : definitions) {
List<String> distributionTargets = new ArrayList<>();
for (String oldId : definition.distributionTargets) {
if (idConversion.containsKey(oldId)) {
distributionTargets.add(idConversion.get(oldId));
} else {
throw new InvalidWorkbasketException(String.format("invalid import state: Workbasket '%s' does not exist in the given import list", oldId));
}
}
workbasketService.setDistributionTargets(// no verification necessary since the workbasket was already imported in step 1.
idConversion.get(definition.workbasketResource.workbasketId), distributionTargets);
}
return new ResponseEntity<>(HttpStatus.OK);
} catch (WorkbasketNotFoundException e) {
TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
} catch (InvalidWorkbasketException e) {
TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
} catch (NotAuthorizedException e) {
TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
} catch (InvalidArgumentException e) {
TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
return new ResponseEntity<>(HttpStatus.PRECONDITION_FAILED);
} catch (WorkbasketAlreadyExistException e) {
TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
return new ResponseEntity<>(HttpStatus.CONFLICT);
} catch (DomainNotFoundException e) {
TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
use of pro.taskana.exceptions.InvalidWorkbasketException in project taskana by Taskana.
the class DeleteWorkbasketAccTest method testCreateAndDeleteWorkbasket.
@WithAccessId(userName = "user_1_2", groupNames = { "businessadmin" })
@Test
public void testCreateAndDeleteWorkbasket() throws SQLException, NotAuthorizedException, InvalidArgumentException, WorkbasketNotFoundException, InvalidWorkbasketException, WorkbasketAlreadyExistException, DomainNotFoundException {
WorkbasketService workbasketService = taskanaEngine.getWorkbasketService();
int before = workbasketService.createWorkbasketQuery().domainIn("DOMAIN_A").list().size();
Workbasket workbasket = workbasketService.newWorkbasket("NT1234", "DOMAIN_A");
workbasket.setName("TheUltimate");
workbasket.setType(WorkbasketType.GROUP);
workbasket.setOrgLevel1("company");
workbasket = workbasketService.createWorkbasket(workbasket);
try {
workbasketService.deleteWorkbasket(workbasket.getId());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of pro.taskana.exceptions.InvalidWorkbasketException in project taskana by Taskana.
the class DistributionTargetsAccTest method testSetDistributionTargets.
@WithAccessId(userName = "user_2_2", groupNames = { "group_1", "group_2", "businessadmin" })
@Test
public void testSetDistributionTargets() throws NotAuthorizedException, WorkbasketNotFoundException, InvalidWorkbasketException, SQLException {
WorkbasketService workbasketService = taskanaEngine.getWorkbasketService();
Workbasket sourceWorkbasket = workbasketService.getWorkbasket("GPK_KSC_1", "DOMAIN_A");
List<WorkbasketSummary> initialDistributionTargets = workbasketService.getDistributionTargets(sourceWorkbasket.getId());
assertEquals(4, initialDistributionTargets.size());
List<WorkbasketSummary> newDistributionTargets = workbasketService.createWorkbasketQuery().keyIn("USER_1_1", "GPK_B_KSC_1").list();
assertEquals(2, newDistributionTargets.size());
List<String> newDistributionTargetIds = newDistributionTargets.stream().map(t -> t.getId()).collect(Collectors.toList());
workbasketService.setDistributionTargets(sourceWorkbasket.getId(), newDistributionTargetIds);
List<WorkbasketSummary> changedTargets = workbasketService.getDistributionTargets(sourceWorkbasket.getId());
assertEquals(2, changedTargets.size());
// reset DB to original state
resetDb(false);
}
use of pro.taskana.exceptions.InvalidWorkbasketException in project taskana by Taskana.
the class CreateWorkbasketAccTest method testCreateWorkbasketWithMissingRequiredField.
@WithAccessId(userName = "dummy", groupNames = { "businessadmin" })
@Test
public void testCreateWorkbasketWithMissingRequiredField() throws WorkbasketNotFoundException, NotAuthorizedException, WorkbasketAlreadyExistException, DomainNotFoundException {
WorkbasketService workbasketService = taskanaEngine.getWorkbasketService();
Workbasket workbasket = workbasketService.newWorkbasket(null, "novatec");
workbasket.setName("Megabasket");
workbasket.setType(WorkbasketType.GROUP);
workbasket.setOrgLevel1("company");
try {
// missing key
workbasketService.createWorkbasket(workbasket);
fail("InvalidWorkbasketException was expected");
} catch (InvalidWorkbasketException e) {
}
workbasket = workbasketService.newWorkbasket("key", "novatec");
workbasket.setType(WorkbasketType.GROUP);
workbasket.setOrgLevel1("company");
try {
// missing name
workbasketService.createWorkbasket(workbasket);
fail("InvalidWorkbasketException was expected");
} catch (InvalidWorkbasketException e) {
}
workbasket = workbasketService.newWorkbasket("key", "novatec");
workbasket.setName("Megabasket");
workbasket.setOrgLevel1("company");
try {
// missing type
workbasketService.createWorkbasket(workbasket);
fail("InvalidWorkbasketException was expected");
} catch (InvalidWorkbasketException e) {
}
workbasket = workbasketService.newWorkbasket("key", null);
workbasket.setName("Megabasket");
workbasket.setType(WorkbasketType.GROUP);
workbasket.setOrgLevel1("company");
try {
// missing domain
workbasketService.createWorkbasket(workbasket);
fail("InvalidWorkbasketException was expected");
} catch (InvalidWorkbasketException e) {
}
}
Aggregations