use of org.hisp.dhis.dxf2.webmessage.WebMessageException in project dhis2-core by dhis2.
the class InterpretationController method writeReportTableInterpretation.
// -------------------------------------------------------------------------
// Intepretation create
// -------------------------------------------------------------------------
@RequestMapping(value = "/reportTable/{uid}", method = RequestMethod.POST, consumes = { "text/html", "text/plain" })
public void writeReportTableInterpretation(@PathVariable("uid") String reportTableUid, @RequestParam(value = "pe", required = false) String isoPeriod, @RequestParam(value = "ou", required = false) String orgUnitUid, @RequestBody String text, HttpServletResponse response, HttpServletRequest request) throws WebMessageException {
ReportTable reportTable = idObjectManager.get(ReportTable.class, reportTableUid);
if (reportTable == null) {
throw new WebMessageException(WebMessageUtils.conflict("Report table does not exist or is not accessible: " + reportTableUid));
}
Period period = PeriodType.getPeriodFromIsoString(isoPeriod);
OrganisationUnit orgUnit = getUserOrganisationUnit(orgUnitUid, reportTable, currentUserService.getCurrentUser());
createIntepretation(new Interpretation(reportTable, period, orgUnit, text), request, response);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageException in project dhis2-core by dhis2.
the class KeyJsonValueController method getKeyJsonValueMetaData.
/**
* Retrieves the KeyJsonValue represented by the given key from the given namespace.
*/
@RequestMapping(value = "/{namespace}/{key}/metaData", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public KeyJsonValue getKeyJsonValueMetaData(@PathVariable String namespace, @PathVariable String key, HttpServletResponse response) throws Exception {
if (!hasAccess(namespace)) {
throw new WebMessageException(WebMessageUtils.forbidden("The namespace '" + namespace + "' is protected, and you don't have the right authority to access it."));
}
KeyJsonValue keyJsonValue = keyJsonValueService.getKeyJsonValue(namespace, key);
if (keyJsonValue == null) {
throw new WebMessageException(WebMessageUtils.notFound("The key '" + key + "' was not found in the namespace '" + namespace + "'."));
}
KeyJsonValue metaDataValue = new KeyJsonValue();
BeanUtils.copyProperties(metaDataValue, keyJsonValue);
metaDataValue.setValue(null);
return metaDataValue;
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageException in project dhis2-core by dhis2.
the class KeyJsonValueController method updateKeyJsonValue.
/**
* Update a key in the given namespace.
*/
@RequestMapping(value = "/{namespace}/{key}", method = RequestMethod.PUT, produces = "application/json", consumes = "application/json")
public void updateKeyJsonValue(@PathVariable String namespace, @PathVariable String key, @RequestBody String body, HttpServletRequest request, HttpServletResponse response) throws WebMessageException, IOException {
if (!hasAccess(namespace)) {
throw new WebMessageException(WebMessageUtils.forbidden("The namespace '" + namespace + "' is protected, and you don't have the right authority to access it."));
}
KeyJsonValue keyJsonValue = keyJsonValueService.getKeyJsonValue(namespace, key);
if (keyJsonValue == null) {
throw new WebMessageException(WebMessageUtils.notFound("The key '" + key + "' was not found in the namespace '" + namespace + "'."));
}
if (!renderService.isValidJson(body)) {
throw new WebMessageException(WebMessageUtils.badRequest("The data is not valid JSON."));
}
keyJsonValue.setValue(body);
keyJsonValueService.updateKeyJsonValue(keyJsonValue);
response.setStatus(HttpServletResponse.SC_OK);
messageService.sendJson(WebMessageUtils.ok("Key '" + key + "' updated."), response);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageException in project dhis2-core by dhis2.
the class DefaultCollectionService method delCollectionItems.
@Override
@SuppressWarnings("unchecked")
public void delCollectionItems(IdentifiableObject object, String propertyName, List<IdentifiableObject> objects) throws Exception {
Schema schema = schemaService.getDynamicSchema(object.getClass());
if (!aclService.canUpdate(currentUserService.getCurrentUser(), object)) {
throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
}
if (!schema.haveProperty(propertyName)) {
throw new WebMessageException(WebMessageUtils.notFound("Property " + propertyName + " does not exist on " + object.getClass().getName()));
}
Property property = schema.getProperty(propertyName);
if (!property.isCollection() || !property.isIdentifiableObject()) {
throw new WebMessageException(WebMessageUtils.conflict("Only identifiable object collections can be removed from."));
}
Collection<String> itemCodes = objects.stream().map(IdentifiableObject::getUid).collect(Collectors.toList());
if (itemCodes.isEmpty()) {
return;
}
List<? extends IdentifiableObject> items = manager.get(((Class<? extends IdentifiableObject>) property.getItemKlass()), itemCodes);
manager.refresh(object);
if (property.isOwner()) {
Collection<IdentifiableObject> collection = (Collection<IdentifiableObject>) property.getGetterMethod().invoke(object);
for (IdentifiableObject item : items) {
if (collection.contains(item))
collection.remove(item);
}
} else {
Schema owningSchema = schemaService.getDynamicSchema(property.getItemKlass());
Property owningProperty = owningSchema.propertyByRole(property.getOwningRole());
for (IdentifiableObject item : items) {
try {
Collection<IdentifiableObject> collection = (Collection<IdentifiableObject>) owningProperty.getGetterMethod().invoke(item);
if (collection.contains(object)) {
collection.remove(object);
manager.update(item);
}
} catch (Exception ex) {
}
}
}
manager.update(object);
dbmsManager.clearSession();
cacheManager.clearCache();
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageException in project dhis2-core by dhis2.
the class UserController method resendInvite.
@RequestMapping(value = "/{id}" + INVITE_PATH, method = RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
public void resendInvite(@PathVariable String id, HttpServletRequest request) throws Exception {
User user = userService.getUser(id);
if (user == null) {
throw new WebMessageException(WebMessageUtils.conflict("User not found: " + id));
}
if (user.getUserCredentials() == null || !user.getUserCredentials().isInvitation()) {
throw new WebMessageException(WebMessageUtils.conflict("User account is not an invitation: " + id));
}
String valid = securityService.validateRestore(user.getUserCredentials());
if (valid != null) {
throw new WebMessageException(WebMessageUtils.conflict(valid));
}
boolean isInviteUsername = securityService.isInviteUsername(user.getUsername());
RestoreOptions restoreOptions = isInviteUsername ? RestoreOptions.INVITE_WITH_USERNAME_CHOICE : RestoreOptions.INVITE_WITH_DEFINED_USERNAME;
securityService.sendRestoreMessage(user.getUserCredentials(), ContextUtils.getContextPath(request), restoreOptions);
}
Aggregations