Search in sources :

Example 31 with WebMessageException

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);
}
Also used : User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) Property(org.hisp.dhis.schema.Property) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 32 with WebMessageException

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);
}
Also used : User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) MergeParams(org.hisp.dhis.schema.MergeParams) UserSettingKey(org.hisp.dhis.user.UserSettingKey) UserCredentials(org.hisp.dhis.user.UserCredentials) Date(java.util.Date) UserSetting(org.hisp.dhis.user.UserSetting) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 33 with WebMessageException

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);
}
Also used : MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) MergeParams(org.hisp.dhis.schema.MergeParams) Map(org.hisp.dhis.mapping.Map) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 34 with WebMessageException

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);
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) MapView(org.hisp.dhis.mapping.MapView) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 35 with WebMessageException

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()));
    }
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) OrganisationUnitLevel(org.hisp.dhis.organisationunit.OrganisationUnitLevel) Metadata(org.hisp.dhis.dxf2.metadata.Metadata) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)134 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)118 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)31 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)28 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)27 DataSet (org.hisp.dhis.dataset.DataSet)21 Period (org.hisp.dhis.period.Period)21 User (org.hisp.dhis.user.User)20 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)18 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)15 ArrayList (java.util.ArrayList)14 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)14 Interpretation (org.hisp.dhis.interpretation.Interpretation)13 Date (java.util.Date)9 WebOptions (org.hisp.dhis.webapi.webdomain.WebOptions)9 InputStream (java.io.InputStream)8 Grid (org.hisp.dhis.common.Grid)8 Event (org.hisp.dhis.dxf2.events.event.Event)8 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)8 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)8