use of org.activiti.rest.exception.ActivitiConflictException in project Activiti by Activiti.
the class GroupMembershipCollectionResource method createMembership.
@RequestMapping(value = "/identity/groups/{groupId}/members", method = RequestMethod.POST, produces = "application/json")
public MembershipResponse createMembership(@PathVariable String groupId, @RequestBody MembershipRequest memberShip, HttpServletRequest request, HttpServletResponse response) {
Group group = getGroupFromRequest(groupId);
if (memberShip.getUserId() == null) {
throw new ActivitiIllegalArgumentException("UserId cannot be null.");
}
// Check if user is member of group since API doesn't return typed exception
if (identityService.createUserQuery().memberOfGroup(group.getId()).userId(memberShip.getUserId()).count() > 0) {
throw new ActivitiConflictException("User '" + memberShip.getUserId() + "' is already part of group '" + group.getId() + "'.");
}
identityService.createMembership(memberShip.getUserId(), group.getId());
response.setStatus(HttpStatus.CREATED.value());
return restResponseFactory.createMembershipResponse(memberShip.getUserId(), group.getId());
}
use of org.activiti.rest.exception.ActivitiConflictException in project Activiti by Activiti.
the class UserCollectionResource method createUser.
@RequestMapping(value = "/identity/users", method = RequestMethod.POST, produces = "application/json")
public UserResponse createUser(@RequestBody UserRequest userRequest, HttpServletRequest request, HttpServletResponse response) {
if (userRequest.getId() == null) {
throw new ActivitiIllegalArgumentException("Id cannot be null.");
}
// Check if a user with the given ID already exists so we return a CONFLICT
if (identityService.createUserQuery().userId(userRequest.getId()).count() > 0) {
throw new ActivitiConflictException("A user with id '" + userRequest.getId() + "' already exists.");
}
User created = identityService.newUser(userRequest.getId());
created.setEmail(userRequest.getEmail());
created.setFirstName(userRequest.getFirstName());
created.setLastName(userRequest.getLastName());
created.setPassword(userRequest.getPassword());
identityService.saveUser(created);
response.setStatus(HttpStatus.CREATED.value());
return restResponseFactory.createUserResponse(created, true);
}
use of org.activiti.rest.exception.ActivitiConflictException in project Activiti by Activiti.
the class TaskVariableCollectionResource method createOrUpdateTaskVariables.
private Object createOrUpdateTaskVariables(HttpServletRequest request, HttpServletResponse response, Task task, boolean override) {
Object result = null;
if (request instanceof MultipartHttpServletRequest) {
result = setBinaryVariable((MultipartHttpServletRequest) request, task, true);
} else {
List<RestVariable> inputVariables = new ArrayList<RestVariable>();
List<RestVariable> resultVariables = new ArrayList<RestVariable>();
result = resultVariables;
try {
@SuppressWarnings("unchecked") List<Object> variableObjects = (List<Object>) objectMapper.readValue(request.getInputStream(), List.class);
for (Object restObject : variableObjects) {
RestVariable restVariable = objectMapper.convertValue(restObject, RestVariable.class);
inputVariables.add(restVariable);
}
} catch (Exception e) {
throw new ActivitiIllegalArgumentException("Failed to serialize to a RestVariable instance", e);
}
if (inputVariables == null || inputVariables.size() == 0) {
throw new ActivitiIllegalArgumentException("Request didn't contain a list of variables to create.");
}
RestVariableScope sharedScope = null;
RestVariableScope varScope = null;
Map<String, Object> variablesToSet = new HashMap<String, Object>();
for (RestVariable var : inputVariables) {
// Validate if scopes match
varScope = var.getVariableScope();
if (var.getName() == null) {
throw new ActivitiIllegalArgumentException("Variable name is required");
}
if (varScope == null) {
varScope = RestVariableScope.LOCAL;
}
if (sharedScope == null) {
sharedScope = varScope;
}
if (varScope != sharedScope) {
throw new ActivitiIllegalArgumentException("Only allowed to update multiple variables in the same scope.");
}
if (!override && hasVariableOnScope(task, var.getName(), varScope)) {
throw new ActivitiConflictException("Variable '" + var.getName() + "' is already present on task '" + task.getId() + "'.");
}
Object actualVariableValue = restResponseFactory.getVariableValue(var);
variablesToSet.put(var.getName(), actualVariableValue);
resultVariables.add(restResponseFactory.createRestVariable(var.getName(), actualVariableValue, varScope, task.getId(), RestResponseFactory.VARIABLE_TASK, false));
}
if (!variablesToSet.isEmpty()) {
if (sharedScope == RestVariableScope.LOCAL) {
taskService.setVariablesLocal(task.getId(), variablesToSet);
} else {
if (task.getExecutionId() != null) {
// Explicitly set on execution, setting non-local variables on task will override local-variables if exists
runtimeService.setVariables(task.getExecutionId(), variablesToSet);
} else {
// Standalone task, no global variables possible
throw new ActivitiIllegalArgumentException("Cannot set global variables on task '" + task.getId() + "', task is not part of process.");
}
}
}
}
response.setStatus(HttpStatus.CREATED.value());
return result;
}
use of org.activiti.rest.exception.ActivitiConflictException in project Activiti by Activiti.
the class UserInfoCollectionResource method setUserInfo.
@RequestMapping(value = "/identity/users/{userId}/info", method = RequestMethod.POST, produces = "application/json")
public UserInfoResponse setUserInfo(@PathVariable String userId, @RequestBody UserInfoRequest userRequest, HttpServletRequest request, HttpServletResponse response) {
User user = getUserFromRequest(userId);
if (userRequest.getKey() == null) {
throw new ActivitiIllegalArgumentException("The key cannot be null.");
}
if (userRequest.getValue() == null) {
throw new ActivitiIllegalArgumentException("The value cannot be null.");
}
String existingValue = identityService.getUserInfo(user.getId(), userRequest.getKey());
if (existingValue != null) {
throw new ActivitiConflictException("User info with key '" + userRequest.getKey() + "' already exists for this user.");
}
identityService.setUserInfo(user.getId(), userRequest.getKey(), userRequest.getValue());
response.setStatus(HttpStatus.CREATED.value());
return restResponseFactory.createUserInfoResponse(userRequest.getKey(), userRequest.getValue(), user.getId());
}
use of org.activiti.rest.exception.ActivitiConflictException in project Activiti by Activiti.
the class GroupCollectionResource method createGroup.
@RequestMapping(value = "/identity/groups", method = RequestMethod.POST, produces = "application/json")
public GroupResponse createGroup(@RequestBody GroupRequest groupRequest, HttpServletRequest httpRequest, HttpServletResponse response) {
if (groupRequest.getId() == null) {
throw new ActivitiIllegalArgumentException("Id cannot be null.");
}
// Check if a user with the given ID already exists so we return a CONFLICT
if (identityService.createGroupQuery().groupId(groupRequest.getId()).count() > 0) {
throw new ActivitiConflictException("A group with id '" + groupRequest.getId() + "' already exists.");
}
Group created = identityService.newGroup(groupRequest.getId());
created.setId(groupRequest.getId());
created.setName(groupRequest.getName());
created.setType(groupRequest.getType());
identityService.saveGroup(created);
response.setStatus(HttpStatus.CREATED.value());
return restResponseFactory.createGroupResponse(created);
}
Aggregations