use of org.hisp.dhis.dxf2.webmessage.WebMessageException in project dhis2-core by dhis2.
the class AbstractCrudController method partialUpdateObject.
@RequestMapping(value = "/{uid}", method = RequestMethod.PATCH)
public void partialUpdateObject(@PathVariable("uid") String pvUid, @RequestParam Map<String, String> rpParameters, HttpServletRequest request, HttpServletResponse response) throws Exception {
WebOptions options = new WebOptions(rpParameters);
List<T> entities = getEntity(pvUid, options);
if (entities.isEmpty()) {
throw new WebMessageException(WebMessageUtils.notFound(getEntityClass(), pvUid));
}
T persistedObject = entities.get(0);
User user = currentUserService.getCurrentUser();
if (!aclService.canUpdate(user, persistedObject)) {
throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
}
String payload = StreamUtils.copyToString(request.getInputStream(), Charset.forName("UTF-8"));
List<String> properties = new ArrayList<>();
T object = null;
if (isJson(request)) {
properties = getJsonProperties(payload);
object = renderService.fromJson(payload, getEntityClass());
} else if (isXml(request)) {
properties = getXmlProperties(payload);
object = renderService.fromXml(payload, getEntityClass());
}
prePatchEntity(persistedObject, object);
properties = getPersistedProperties(properties);
if (properties.isEmpty() || object == null) {
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
return;
}
Schema schema = getSchema();
for (String keyProperty : properties) {
Property property = schema.getProperty(keyProperty);
Object value = property.getGetterMethod().invoke(object);
property.getSetterMethod().invoke(persistedObject, value);
}
manager.update(persistedObject);
postPatchEntity(persistedObject);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageException in project dhis2-core by dhis2.
the class UserController method replicateUser.
@SuppressWarnings("unchecked")
@PreAuthorize("hasRole('ALL') or hasRole('F_REPLICATE_USER')")
@RequestMapping(value = "/{uid}/replica", method = RequestMethod.POST)
public void replicateUser(@PathVariable String uid, HttpServletRequest request, HttpServletResponse response) throws IOException, WebMessageException {
User existingUser = userService.getUser(uid);
if (existingUser == null || existingUser.getUserCredentials() == null) {
throw new WebMessageException(WebMessageUtils.conflict("User not found: " + uid));
}
User currentUser = currentUserService.getCurrentUser();
if (!validateCreateUser(existingUser, currentUser)) {
return;
}
Map<String, String> auth = renderService.fromJson(request.getInputStream(), Map.class);
String username = StringUtils.trimToNull(auth != null ? auth.get(KEY_USERNAME) : null);
String password = StringUtils.trimToNull(auth != null ? auth.get(KEY_PASSWORD) : null);
if (auth == null || username == null) {
throw new WebMessageException(WebMessageUtils.conflict("Username must be specified"));
}
if (userService.getUserCredentialsByUsername(username) != null) {
throw new WebMessageException(WebMessageUtils.conflict("Username already taken: " + username));
}
if (password == null) {
throw new WebMessageException(WebMessageUtils.conflict("Password must be specified"));
}
if (!ValidationUtils.passwordIsValid(password)) {
throw new WebMessageException(WebMessageUtils.conflict("Password must have at least 8 characters, one digit, one uppercase"));
}
User userReplica = new User();
mergeService.merge(new MergeParams<>(existingUser, userReplica).setMergeMode(MergeMode.MERGE));
userReplica.setUid(CodeGenerator.generateUid());
userReplica.setCode(null);
userReplica.setCreated(new Date());
UserCredentials credentialsReplica = new UserCredentials();
mergeService.merge(new MergeParams<>(existingUser.getUserCredentials(), credentialsReplica).setMergeMode(MergeMode.MERGE));
credentialsReplica.setUid(CodeGenerator.generateUid());
credentialsReplica.setCode(null);
credentialsReplica.setCreated(new Date());
credentialsReplica.setLdapId(null);
credentialsReplica.setOpenId(null);
credentialsReplica.setUsername(username);
userService.encodeAndSetPassword(credentialsReplica, password);
userReplica.setUserCredentials(credentialsReplica);
credentialsReplica.setUserInfo(userReplica);
userService.addUser(userReplica);
userService.addUserCredentials(credentialsReplica);
userGroupService.addUserToGroups(userReplica, IdentifiableObjectUtils.getUids(existingUser.getGroups()), currentUser);
// ---------------------------------------------------------------------
// Replicate user settings
// ---------------------------------------------------------------------
List<UserSetting> settings = userSettingService.getUserSettings(existingUser);
for (UserSetting setting : settings) {
Optional<UserSettingKey> key = UserSettingKey.getByName(setting.getName());
key.ifPresent(userSettingKey -> userSettingService.saveUserSetting(userSettingKey, setting.getValue(), userReplica));
}
response.addHeader("Location", UserSchemaDescriptor.API_ENDPOINT + "/" + userReplica.getUid());
webMessageService.send(WebMessageUtils.created("User replica created"), response, request);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageException in project dhis2-core by dhis2.
the class MapController method putJsonObject.
@Override
@RequestMapping(value = "/{uid}", method = RequestMethod.PUT, consumes = "application/json")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void putJsonObject(@PathVariable String uid, HttpServletRequest request, HttpServletResponse response) throws Exception {
Map map = mappingService.getMap(uid);
if (map == null) {
throw new WebMessageException(WebMessageUtils.notFound("Map does not exist: " + uid));
}
MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap());
Map newMap = deserializeJsonEntity(request, response);
newMap.setUid(uid);
mergeService.merge(new MergeParams<>(newMap, map).setMergeMode(params.getMergeMode()));
mappingService.updateMap(map);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageException in project dhis2-core by dhis2.
the class MapViewController method getMapViewData.
//--------------------------------------------------------------------------
// Get data
//--------------------------------------------------------------------------
@RequestMapping(value = { "/{uid}/data", "/{uid}/data.png" }, method = RequestMethod.GET)
public void getMapViewData(@PathVariable String uid, HttpServletResponse response) throws Exception {
MapView mapView = mappingService.getMapView(uid);
if (mapView == null) {
throw new WebMessageException(WebMessageUtils.notFound("Map view does not exist: " + uid));
}
renderMapViewPng(mapView, response);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageException in project dhis2-core by dhis2.
the class FilledOrganisationUnitLevelController method setList.
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public void setList(HttpServletRequest request, HttpServletResponse response) throws Exception {
Metadata metadata = DefaultRenderService.getJsonMapper().readValue(request.getInputStream(), Metadata.class);
List<OrganisationUnitLevel> levels = metadata.getOrganisationUnitLevels();
for (OrganisationUnitLevel level : levels) {
if (level.getLevel() <= 0) {
throw new WebMessageException(WebMessageUtils.conflict("Level must be greater than zero"));
}
if (StringUtils.isBlank(level.getName())) {
throw new WebMessageException(WebMessageUtils.conflict("Name must be specified"));
}
organisationUnitService.addOrUpdateOrganisationUnitLevel(new OrganisationUnitLevel(level.getLevel(), level.getName(), level.getOfflineLevels()));
}
}
Aggregations