Search in sources :

Example 6 with WebMessageUtils.created

use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.created 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 7 with WebMessageUtils.created

use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.created 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)

Example 8 with WebMessageUtils.created

use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.created in project dhis2-core by dhis2.

the class SqlViewController method executeView.

// -------------------------------------------------------------------------
// Post
// -------------------------------------------------------------------------
@RequestMapping(value = "/{uid}/execute", method = RequestMethod.POST)
public void executeView(@PathVariable("uid") String uid, @RequestParam(required = false) Set<String> var, HttpServletResponse response, HttpServletRequest request) throws WebMessageException {
    SqlView sqlView = sqlViewService.getSqlViewByUid(uid);
    if (sqlView == null) {
        throw new WebMessageException(WebMessageUtils.notFound("SQL view not found"));
    }
    if (sqlView.isQuery()) {
        throw new WebMessageException(WebMessageUtils.conflict("SQL view is a query, no view to create"));
    }
    String result = sqlViewService.createViewTable(sqlView);
    if (result != null) {
        throw new WebMessageException(WebMessageUtils.conflict(result));
    } else {
        response.addHeader("Location", SqlViewSchemaDescriptor.API_ENDPOINT + "/" + sqlView.getUid());
        webMessageService.send(WebMessageUtils.created("SQL view created"), response, request);
    }
}
Also used : SqlView(org.hisp.dhis.sqlview.SqlView) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with WebMessageUtils.created

use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.created in project dhis2-core by dhis2.

the class InterpretationController method like.

// -------------------------------------------------------------------------
// Likes
// -------------------------------------------------------------------------
@RequestMapping(value = "/{uid}/like", method = RequestMethod.POST)
public void like(@PathVariable("uid") String uid, HttpServletResponse response, HttpServletRequest request) throws WebMessageException {
    Interpretation interpretation = interpretationService.getInterpretation(uid);
    if (interpretation == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Interpretation does not exist: " + uid));
    }
    boolean like = interpretationService.likeInterpretation(interpretation.getId());
    if (like) {
        webMessageService.send(WebMessageUtils.created("Like added to interpretation"), response, request);
    } else {
        webMessageService.send(WebMessageUtils.conflict("Could not add like, user had already liked interpretation"), response, request);
    }
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Interpretation(org.hisp.dhis.interpretation.Interpretation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with WebMessageUtils.created

use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.created in project dhis2-core by dhis2.

the class DashboardController method postJsonItem.

// -------------------------------------------------------------------------
// Dashboard items
// -------------------------------------------------------------------------
@RequestMapping(value = "/{uid}/items", method = RequestMethod.POST, consumes = "application/json")
public void postJsonItem(@PathVariable String uid, HttpServletRequest request, HttpServletResponse response) throws Exception {
    Dashboard dashboard = dashboardService.getDashboard(uid);
    if (dashboard == null) {
        throw new WebMessageException(WebMessageUtils.notFound("Dashboard does not exist: " + uid));
    }
    if (!aclService.canUpdate(currentUserService.getCurrentUser(), dashboard)) {
        throw new UpdateAccessDeniedException("You don't have the proper permissions to update this dashboard.");
    }
    DashboardItem item = renderService.fromJson(request.getInputStream(), DashboardItem.class);
    dashboardService.mergeDashboardItem(item);
    dashboard.getItems().add(0, item);
    dashboardService.updateDashboard(dashboard);
    response.addHeader("Location", DashboardItemSchemaDescriptor.API_ENDPOINT + "/" + item.getUid());
    webMessageService.send(WebMessageUtils.created("Dashboard item created"), response, request);
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) Dashboard(org.hisp.dhis.dashboard.Dashboard) DashboardItem(org.hisp.dhis.dashboard.DashboardItem) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)10 Interpretation (org.hisp.dhis.interpretation.Interpretation)3 ArrayList (java.util.ArrayList)2 Dashboard (org.hisp.dhis.dashboard.Dashboard)2 DashboardItem (org.hisp.dhis.dashboard.DashboardItem)2 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)2 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)2 User (org.hisp.dhis.user.User)2 UserKeyJsonValue (org.hisp.dhis.userkeyjsonvalue.UserKeyJsonValue)2 DataSet (org.hisp.dhis.dataset.DataSet)1 LockException (org.hisp.dhis.dataset.LockException)1 ReadAccessDeniedException (org.hisp.dhis.hibernate.exception.ReadAccessDeniedException)1 InterpretationComment (org.hisp.dhis.interpretation.InterpretationComment)1 KeyJsonValue (org.hisp.dhis.keyjsonvalue.KeyJsonValue)1 Period (org.hisp.dhis.period.Period)1 SqlView (org.hisp.dhis.sqlview.SqlView)1 UserGroup (org.hisp.dhis.user.UserGroup)1