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