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;
}
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.");
}
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;
}
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;
}
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;
}
Aggregations