Search in sources :

Example 6 with LockException

use of org.hisp.dhis.dataset.LockException in project dhis2-core by dhis2.

the class AddLockExceptionAction method execute.

// -------------------------------------------------------------------------
// Action Implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    if (organisationUnitId.length() == 0) {
        return INPUT;
    }
    DataSet dataSet = dataSetService.getDataSet(dataSetId);
    Period period = periodService.reloadPeriod(PeriodType.getPeriodFromIsoString(periodId));
    if (dataSet == null || period == null) {
        return ERROR;
    }
    for (String id : organisationUnitId.split(",")) {
        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(id);
        if (organisationUnit == null) {
            return ERROR;
        }
        if (organisationUnit.getDataSets().contains(dataSet)) {
            LockException lockException = new LockException();
            lockException.setOrganisationUnit(organisationUnit);
            lockException.setDataSet(dataSet);
            lockException.setPeriod(period);
            dataSetService.addLockException(lockException);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("OrganisationUnit " + organisationUnit.getName() + " does not contain DataSet " + dataSet.getName() + ", ignoring.");
            }
        }
    }
    return SUCCESS;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataSet(org.hisp.dhis.dataset.DataSet) LockException(org.hisp.dhis.dataset.LockException) Period(org.hisp.dhis.period.Period)

Example 7 with LockException

use of org.hisp.dhis.dataset.LockException in project dhis2-core by dhis2.

the class LockExceptionController method addLockException.

@PostMapping
@ResponseBody
public WebMessage addLockException(@RequestParam("ou") String organisationUnitId, @RequestParam("pe") String periodId, @RequestParam("ds") String dataSetId) {
    User user = userService.getCurrentUser();
    DataSet dataSet = dataSetService.getDataSet(dataSetId);
    Period period = periodService.reloadPeriod(PeriodType.getPeriodFromIsoString(periodId));
    if (dataSet == null || period == null) {
        return 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");
    }
    List<String> listOrgUnitIds = new ArrayList<>();
    if (organisationUnitId.startsWith("[") && organisationUnitId.endsWith("]")) {
        String[] arrOrgUnitIds = organisationUnitId.substring(1, organisationUnitId.length() - 1).split(",");
        Collections.addAll(listOrgUnitIds, arrOrgUnitIds);
    } else if (!organisationUnitId.isEmpty()) {
        listOrgUnitIds.add(organisationUnitId);
    }
    if (listOrgUnitIds.isEmpty()) {
        return conflict("OrganisationUnit ID is invalid.");
    }
    boolean added = false;
    for (String id : listOrgUnitIds) {
        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(id);
        if (organisationUnit == null) {
            return 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);
            added = true;
        }
    }
    if (!added) {
        return conflict(String.format("None of the target organisation unit(s) %s is linked to the specified data set: %s", String.join(",", listOrgUnitIds), dataSetId));
    }
    return created("LockException created successfully.");
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) DataSet(org.hisp.dhis.dataset.DataSet) LockException(org.hisp.dhis.dataset.LockException) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) ReadAccessDeniedException(org.hisp.dhis.hibernate.exception.ReadAccessDeniedException) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 8 with LockException

use of org.hisp.dhis.dataset.LockException in project dhis2-core by dhis2.

the class LockExceptionController method getLockExceptions.

// -------------------------------------------------------------------------
// Resources
// -------------------------------------------------------------------------
@GetMapping(produces = ContextUtils.CONTENT_TYPE_JSON)
@ResponseBody
public RootNode getLockExceptions(@RequestParam(required = false) String key, @RequestParam Map<String, String> rpParameters, HttpServletRequest request, HttpServletResponse response) throws WebMessageException {
    List<String> filters = Lists.newArrayList(contextService.getParameterValues("filter"));
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    if (fields.isEmpty()) {
        fields.addAll(Preset.ALL.getFields());
    }
    List<LockException> lockExceptions = new ArrayList<>();
    if (key != null) {
        LockException lockException = dataSetService.getLockException(MathUtils.parseInt(key));
        if (lockException == null) {
            throw new WebMessageException(notFound("Cannot find LockException with key: " + key));
        }
        lockExceptions.add(lockException);
    } else if (!filters.isEmpty()) {
        lockExceptions = dataSetService.filterLockExceptions(filters);
    } else {
        lockExceptions = dataSetService.getAllLockExceptions();
    }
    WebOptions options = new WebOptions(rpParameters);
    WebMetadata metadata = new WebMetadata();
    Pager pager = metadata.getPager();
    if (options.hasPaging() && pager == null) {
        pager = new Pager(options.getPage(), lockExceptions.size(), options.getPageSize());
        lockExceptions = PagerUtils.pageCollection(lockExceptions, pager);
    }
    RootNode rootNode = NodeUtils.createMetadata();
    if (pager != null) {
        rootNode.addChild(NodeUtils.createPager(pager));
    }
    I18nFormat format = this.i18nManager.getI18nFormat();
    for (LockException lockException : lockExceptions) {
        lockException.getPeriod().setName(format.formatPeriod(lockException.getPeriod()));
    }
    rootNode.addChild(fieldFilterService.toCollectionNode(LockException.class, new FieldFilterParams(lockExceptions, fields)));
    return rootNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) LockException(org.hisp.dhis.dataset.LockException) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Pager(org.hisp.dhis.common.Pager) ArrayList(java.util.ArrayList) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) I18nFormat(org.hisp.dhis.i18n.I18nFormat) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) WebMetadata(org.hisp.dhis.webapi.webdomain.WebMetadata) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 9 with LockException

use of org.hisp.dhis.dataset.LockException in project dhis2-core by dhis2.

the class LockExceptionController method getLockExceptionCombinations.

@GetMapping(value = "/combinations", produces = ContextUtils.CONTENT_TYPE_JSON)
@ResponseBody
public RootNode getLockExceptionCombinations() {
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    if (fields.isEmpty()) {
        fields.addAll(Preset.ALL.getFields());
    }
    List<LockException> lockExceptions = this.dataSetService.getLockExceptionCombinations();
    I18nFormat format = this.i18nManager.getI18nFormat();
    for (LockException lockException : lockExceptions) {
        lockException.getPeriod().setName(format.formatPeriod(lockException.getPeriod()));
    }
    Collections.sort(lockExceptions, new LockExceptionNameComparator());
    RootNode rootNode = NodeUtils.createMetadata();
    rootNode.addChild(fieldFilterService.toCollectionNode(LockException.class, new FieldFilterParams(lockExceptions, fields)));
    return rootNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) LockExceptionNameComparator(org.hisp.dhis.dataset.comparator.LockExceptionNameComparator) LockException(org.hisp.dhis.dataset.LockException) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) I18nFormat(org.hisp.dhis.i18n.I18nFormat) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 10 with LockException

use of org.hisp.dhis.dataset.LockException in project dhis2-core by dhis2.

the class HibernateLockExceptionStore method getCombinations.

@Override
public List<LockException> getCombinations() {
    final String sql = "select distinct datasetid, periodid from lockexception";
    final List<LockException> lockExceptions = new ArrayList<>();
    jdbcTemplate.query(sql, new RowCallbackHandler() {

        @Override
        public void processRow(ResultSet rs) throws SQLException {
            int dataSetId = rs.getInt(1);
            int periodId = rs.getInt(2);
            LockException lockException = new LockException();
            Period period = periodService.getPeriod(periodId);
            DataSet dataSet = dataSetStore.get(dataSetId);
            lockException.setDataSet(dataSet);
            lockException.setPeriod(period);
            lockExceptions.add(lockException);
        }
    });
    return lockExceptions;
}
Also used : LockException(org.hisp.dhis.dataset.LockException) SQLException(java.sql.SQLException) DataSet(org.hisp.dhis.dataset.DataSet) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) Period(org.hisp.dhis.period.Period) RowCallbackHandler(org.springframework.jdbc.core.RowCallbackHandler)

Aggregations

LockException (org.hisp.dhis.dataset.LockException)10 DataSet (org.hisp.dhis.dataset.DataSet)6 Period (org.hisp.dhis.period.Period)6 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)5 ArrayList (java.util.ArrayList)4 LockExceptionNameComparator (org.hisp.dhis.dataset.comparator.LockExceptionNameComparator)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)2 FieldFilterParams (org.hisp.dhis.fieldfilter.FieldFilterParams)2 ReadAccessDeniedException (org.hisp.dhis.hibernate.exception.ReadAccessDeniedException)2 I18nFormat (org.hisp.dhis.i18n.I18nFormat)2 RootNode (org.hisp.dhis.node.types.RootNode)2 PeriodType (org.hisp.dhis.period.PeriodType)2 User (org.hisp.dhis.user.User)2 DhisWebSpringTest (org.hisp.dhis.webapi.DhisWebSpringTest)2 Test (org.junit.Test)2 MockHttpSession (org.springframework.mock.web.MockHttpSession)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1