use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.conflict in project dhis2-core by dhis2.
the class UserSettingController method getUserSetting.
@RequestMapping(value = "/{key}", method = RequestMethod.GET)
@ResponseBody
public String getUserSetting(@PathVariable("key") String key, @RequestParam(value = "user", required = false) String username, HttpServletRequest request, HttpServletResponse response) throws IOException, WebMessageException {
Optional<UserSettingKey> keyEnum = UserSettingKey.getByName(key);
if (!keyEnum.isPresent()) {
throw new WebMessageException(WebMessageUtils.conflict("Key is not supported: " + key));
}
User user = null;
if (username != null) {
UserCredentials credentials = userService.getUserCredentialsByUsername(username);
if (credentials != null) {
user = credentials.getUserInfo();
} else {
throw new WebMessageException(WebMessageUtils.conflict("User does not exist: " + username));
}
}
Serializable value = userSettingService.getUserSetting(keyEnum.get(), user);
if (value == null) {
throw new WebMessageException(WebMessageUtils.notFound("User setting not found for key: " + key));
}
return String.valueOf(value);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.conflict in project dhis2-core by dhis2.
the class ProgramMessageController method getProgramMessages.
// -------------------------------------------------------------------------
// GET
// -------------------------------------------------------------------------
@PreAuthorize("hasRole('ALL') or hasRole('F_MOBILE_SENDSMS')")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" })
public void getProgramMessages(@RequestParam(required = false) Set<String> ou, @RequestParam(required = false) String programInstance, @RequestParam(required = false) String programStageInstance, @RequestParam(required = false) ProgramMessageStatus messageStatus, @RequestParam(required = false) Date afterDate, @RequestParam(required = false) Date beforeDate, @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer pageSize, HttpServletRequest request, HttpServletResponse response) throws IOException, WebMessageException {
ProgramMessageQueryParams params = programMessageService.getFromUrl(ou, programInstance, programStageInstance, messageStatus, page, pageSize, afterDate, beforeDate);
if (programInstance == null && programStageInstance == null) {
throw new WebMessageException(WebMessageUtils.conflict("ProgramInstance or ProgramStageInstance must be specified."));
}
List<ProgramMessage> programMessages = programMessageService.getProgramMessages(params);
renderService.toJson(response.getOutputStream(), programMessages);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.conflict in project dhis2-core by dhis2.
the class MeController method getSetting.
@RequestMapping(value = "/settings/{key}")
public void getSetting(HttpServletResponse response, @PathVariable String key) throws IOException, WebMessageException, NotAuthenticatedException {
User currentUser = currentUserService.getCurrentUser();
if (currentUser == null) {
throw new NotAuthenticatedException();
}
Optional<UserSettingKey> keyEnum = UserSettingKey.getByName(key);
if (!keyEnum.isPresent()) {
throw new WebMessageException(WebMessageUtils.conflict("Key is not supported: " + key));
}
Serializable value = userSettingService.getUserSetting(keyEnum.get(), currentUser);
if (value == null) {
throw new WebMessageException(WebMessageUtils.notFound("User setting not found for key: " + key));
}
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
renderService.toJson(response.getOutputStream(), value);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.conflict in project dhis2-core by dhis2.
the class UserController method putJsonObject.
@Override
@RequestMapping(value = "/{uid}", method = RequestMethod.PUT, consumes = "application/json")
public void putJsonObject(@PathVariable("uid") String pvUid, HttpServletRequest request, HttpServletResponse response) throws Exception {
List<User> users = getEntity(pvUid, NO_WEB_OPTIONS);
if (users.isEmpty()) {
throw new WebMessageException(WebMessageUtils.conflict(getEntityName() + " does not exist: " + pvUid));
}
User currentUser = currentUserService.getCurrentUser();
if (!aclService.canUpdate(currentUser, users.get(0))) {
throw new UpdateAccessDeniedException("You don't have the proper permissions to update this user.");
}
User parsed = renderService.fromJson(request.getInputStream(), getEntityClass());
parsed.setUid(pvUid);
if (!userService.canAddOrUpdateUser(IdentifiableObjectUtils.getUids(parsed.getGroups()), currentUser)) {
throw new WebMessageException(WebMessageUtils.conflict("You must have permissions to create user, or ability to manage at least one user group for the user."));
}
MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap());
params.setImportReportMode(ImportReportMode.FULL);
params.setImportStrategy(ImportStrategy.UPDATE);
params.addObject(parsed);
ImportReport importReport = importService.importMetadata(params);
if (importReport.getStatus() == Status.OK && importReport.getStats().getUpdated() == 1) {
User user = userService.getUser(pvUid);
userGroupService.updateUserGroups(user, IdentifiableObjectUtils.getUids(parsed.getGroups()), currentUser);
}
renderService.toJson(response.getOutputStream(), importReport);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.conflict in project dhis2-core by dhis2.
the class UserController method putXmlObject.
// -------------------------------------------------------------------------
// PUT
// -------------------------------------------------------------------------
@Override
@RequestMapping(value = "/{uid}", method = RequestMethod.PUT, consumes = { "application/xml", "text/xml" })
public void putXmlObject(@PathVariable("uid") String pvUid, HttpServletRequest request, HttpServletResponse response) throws Exception {
List<User> users = getEntity(pvUid, NO_WEB_OPTIONS);
if (users.isEmpty()) {
throw new WebMessageException(WebMessageUtils.conflict(getEntityName() + " does not exist: " + pvUid));
}
User currentUser = currentUserService.getCurrentUser();
if (!aclService.canUpdate(currentUser, users.get(0))) {
throw new UpdateAccessDeniedException("You don't have the proper permissions to update this user.");
}
User parsed = renderService.fromXml(request.getInputStream(), getEntityClass());
parsed.setUid(pvUid);
if (!userService.canAddOrUpdateUser(IdentifiableObjectUtils.getUids(parsed.getGroups()), currentUser)) {
throw new WebMessageException(WebMessageUtils.conflict("You must have permissions to create user, or ability to manage at least one user group for the user."));
}
MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap());
params.setImportReportMode(ImportReportMode.FULL);
params.setImportStrategy(ImportStrategy.UPDATE);
params.addObject(parsed);
ImportReport importReport = importService.importMetadata(params);
if (importReport.getStatus() == Status.OK && importReport.getStats().getUpdated() == 1) {
User user = userService.getUser(pvUid);
userGroupService.updateUserGroups(user, IdentifiableObjectUtils.getUids(parsed.getGroups()), currentUser);
}
renderService.toXml(response.getOutputStream(), importReport);
}
Aggregations