use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.conflict in project dhis2-core by dhis2.
the class UserController method validateInviteUser.
/**
* Validates whether a user can be invited / created.
*
* @param user the user.
*/
private boolean validateInviteUser(User user, User currentUser) throws WebMessageException {
if (!validateCreateUser(user, currentUser)) {
return false;
}
UserCredentials credentials = user.getUserCredentials();
if (credentials == null) {
throw new WebMessageException(WebMessageUtils.conflict("User credentials is not present"));
}
credentials.setUserInfo(user);
String valid = securityService.validateInvite(user.getUserCredentials());
if (valid != null) {
throw new WebMessageException(WebMessageUtils.conflict(valid + ": " + user.getUserCredentials()));
}
return true;
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.conflict in project dhis2-core by dhis2.
the class LockExceptionController method addLockException.
@RequestMapping(method = RequestMethod.POST)
public void addLockException(@RequestParam("ou") String organisationUnitId, @RequestParam("pe") String periodId, @RequestParam("ds") String dataSetId, HttpServletRequest request, HttpServletResponse response) throws WebMessageException {
User user = userService.getCurrentUser();
DataSet dataSet = dataSetService.getDataSet(dataSetId);
Period period = periodService.reloadPeriod(PeriodType.getPeriodFromIsoString(periodId));
if (dataSet == null || period == null) {
throw new WebMessageException(WebMessageUtils.conflict(" DataSet or Period is invalid"));
}
if (!aclService.canUpdate(user, dataSet)) {
throw new ReadAccessDeniedException("You don't have the proper permissions to update this object");
}
boolean created = false;
List<String> listOrgUnitIds = new ArrayList<>();
if (organisationUnitId.startsWith("[") && organisationUnitId.endsWith("]")) {
String[] arrOrgUnitIds = organisationUnitId.substring(1, organisationUnitId.length() - 1).split(",");
Collections.addAll(listOrgUnitIds, arrOrgUnitIds);
} else {
listOrgUnitIds.add(organisationUnitId);
}
if (listOrgUnitIds.size() == 0) {
throw new WebMessageException(WebMessageUtils.conflict(" OrganisationUnit ID is invalid."));
}
for (String id : listOrgUnitIds) {
OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(id);
if (organisationUnit == null) {
throw new WebMessageException(WebMessageUtils.conflict("Can't find OrganisationUnit with id =" + id));
}
if (organisationUnit.getDataSets().contains(dataSet)) {
LockException lockException = new LockException();
lockException.setOrganisationUnit(organisationUnit);
lockException.setDataSet(dataSet);
lockException.setPeriod(period);
dataSetService.addLockException(lockException);
created = true;
}
}
if (created) {
webMessageService.send(WebMessageUtils.created("LockException created successfully."), response, request);
}
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.conflict in project dhis2-core by dhis2.
the class MaintenanceController method pruneDataByDataElement.
@RequestMapping(value = "/dataPruning/dataElements/{uid}", method = { RequestMethod.PUT, RequestMethod.POST })
@PreAuthorize("hasRole('ALL')")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void pruneDataByDataElement(@PathVariable String uid, HttpServletResponse response) throws Exception {
DataElement dataElement = dataElementService.getDataElement(uid);
if (dataElement == null) {
webMessageService.sendJson(WebMessageUtils.conflict("Data element does not exist: " + uid), response);
return;
}
boolean result = maintenanceService.pruneData(dataElement);
WebMessage message = result ? WebMessageUtils.ok("Data was pruned successfully") : WebMessageUtils.conflict("Data could not be pruned");
webMessageService.sendJson(message, response);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.conflict in project dhis2-core by dhis2.
the class MaintenanceController method pruneDataByOrganisationUnit.
@RequestMapping(value = "/dataPruning/organisationUnits/{uid}", method = { RequestMethod.PUT, RequestMethod.POST })
@PreAuthorize("hasRole('ALL')")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void pruneDataByOrganisationUnit(@PathVariable String uid, HttpServletResponse response) throws Exception {
OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(uid);
if (organisationUnit == null) {
webMessageService.sendJson(WebMessageUtils.conflict("Organisation unit does not exist: " + uid), response);
return;
}
boolean result = maintenanceService.pruneData(organisationUnit);
WebMessage message = result ? WebMessageUtils.ok("Data was pruned successfully") : WebMessageUtils.conflict("Data could not be pruned");
webMessageService.sendJson(message, response);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.conflict in project dhis2-core by dhis2.
the class KeyJsonValueController method addKeyJsonValue.
/**
* Creates a new KeyJsonValue Object on the given namespace with the key and value supplied.
*/
@RequestMapping(value = "/{namespace}/{key}", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
public void addKeyJsonValue(@PathVariable String namespace, @PathVariable String key, @RequestBody String body, @RequestParam(defaultValue = "false") boolean encrypt, HttpServletResponse response) throws IOException, WebMessageException {
if (!hasAccess(namespace)) {
throw new WebMessageException(WebMessageUtils.forbidden("The namespace '" + namespace + "' is protected, and you don't have the right authority to access it."));
}
if (keyJsonValueService.getKeyJsonValue(namespace, key) != null) {
throw new WebMessageException(WebMessageUtils.conflict("The key '" + key + "' already exists on the namespace '" + namespace + "'."));
}
if (!renderService.isValidJson(body)) {
throw new WebMessageException(WebMessageUtils.badRequest("The data is not valid JSON."));
}
KeyJsonValue keyJsonValue = new KeyJsonValue();
keyJsonValue.setKey(key);
keyJsonValue.setNamespace(namespace);
keyJsonValue.setValue(body);
keyJsonValue.setEncrypted(encrypt);
keyJsonValueService.addKeyJsonValue(keyJsonValue);
response.setStatus(HttpServletResponse.SC_CREATED);
messageService.sendJson(WebMessageUtils.created("Key '" + key + "' created."), response);
}
Aggregations