Search in sources :

Example 26 with WebMessageUtils.conflict

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;
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) UserCredentials(org.hisp.dhis.user.UserCredentials)

Example 27 with WebMessageUtils.conflict

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);
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) DataSet(org.hisp.dhis.dataset.DataSet) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) LockException(org.hisp.dhis.dataset.LockException) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) ReadAccessDeniedException(org.hisp.dhis.hibernate.exception.ReadAccessDeniedException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 28 with WebMessageUtils.conflict

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);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 29 with WebMessageUtils.conflict

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);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 30 with WebMessageUtils.conflict

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

Aggregations

WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)51 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)44 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)20 Period (org.hisp.dhis.period.Period)14 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)12 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)11 DataSet (org.hisp.dhis.dataset.DataSet)10 Interpretation (org.hisp.dhis.interpretation.Interpretation)10 ArrayList (java.util.ArrayList)9 User (org.hisp.dhis.user.User)9 Date (java.util.Date)7 DataElement (org.hisp.dhis.dataelement.DataElement)7 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)7 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)6 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)6 Serializable (java.io.Serializable)4 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)4 DataValue (org.hisp.dhis.datavalue.DataValue)4 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)4 ByteSource (com.google.common.io.ByteSource)3