use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.notFound in project dhis2-core by dhis2.
the class DataSetController method getFormJson.
@RequestMapping(value = "/{uid}/form", method = RequestMethod.GET, produces = "application/json")
public void getFormJson(@PathVariable("uid") String uid, @RequestParam(value = "ou", required = false) String orgUnit, @RequestParam(value = "pe", required = false) String period, @RequestParam(value = "categoryOptions", required = false) String categoryOptions, @RequestParam(required = false) boolean metaData, TranslateParams translateParams, HttpServletResponse response) throws IOException, WebMessageException {
setUserContext(translateParams);
List<DataSet> dataSets = getEntity(uid, NO_WEB_OPTIONS);
if (dataSets.isEmpty()) {
throw new WebMessageException(WebMessageUtils.notFound("DataSet not found for uid: " + uid));
}
OrganisationUnit ou = manager.get(OrganisationUnit.class, orgUnit);
if (ou == null) {
throw new WebMessageException(WebMessageUtils.notFound("Organisation unit does not exist: " + orgUnit));
}
Period pe = PeriodType.getPeriodFromIsoString(period);
Form form = getForm(dataSets, ou, pe, categoryOptions, metaData);
renderService.toJson(response.getOutputStream(), form);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.notFound in project dhis2-core by dhis2.
the class InterpretationController method updateInterpretation.
// -------------------------------------------------------------------------
// Interpretation update
// -------------------------------------------------------------------------
@RequestMapping(value = "/{uid}", method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.NO_CONTENT)
public void updateInterpretation(@PathVariable("uid") String uid, @RequestBody String text, HttpServletResponse response) throws WebMessageException {
Interpretation interpretation = interpretationService.getInterpretation(uid);
if (interpretation == null) {
throw new WebMessageException(WebMessageUtils.notFound("Interpretation does not exist: " + uid));
}
if (!currentUserService.getCurrentUser().equals(interpretation.getUser()) && !currentUserService.currentUserIsSuper()) {
throw new AccessDeniedException("You are not allowed to update this interpretation.");
}
interpretation.setText(text);
interpretationService.updateInterpretation(interpretation);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.notFound in project dhis2-core by dhis2.
the class KeyJsonValueController method deleteKeyJsonValue.
/**
* Delete a key from the given namespace.
*/
@RequestMapping(value = "/{namespace}/{key}", method = RequestMethod.DELETE, produces = "application/json")
public void deleteKeyJsonValue(@PathVariable String namespace, @PathVariable String key, HttpServletResponse response) throws 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."));
}
KeyJsonValue keyJsonValue = keyJsonValueService.getKeyJsonValue(namespace, key);
if (keyJsonValue == null) {
throw new WebMessageException(WebMessageUtils.notFound("The key '" + key + "' was not found in the namespace '" + namespace + "'."));
}
keyJsonValueService.deleteKeyJsonValue(keyJsonValue);
messageService.sendJson(WebMessageUtils.ok("Key '" + key + "' deleted from namespace '" + namespace + "'."), response);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.notFound in project dhis2-core by dhis2.
the class DefaultCollectionService method clearCollectionItems.
@Override
@SuppressWarnings("unchecked")
public void clearCollectionItems(IdentifiableObject object, String pvProperty) throws WebMessageException, InvocationTargetException, IllegalAccessException {
Schema schema = schemaService.getDynamicSchema(object.getClass());
if (!schema.haveProperty(pvProperty)) {
throw new WebMessageException(WebMessageUtils.notFound("Property " + pvProperty + " does not exist on " + object.getClass().getName()));
}
Property property = schema.getProperty(pvProperty);
if (!property.isCollection() || !property.isIdentifiableObject()) {
throw new WebMessageException(WebMessageUtils.conflict("Only identifiable collections are allowed to be cleared."));
}
Collection<IdentifiableObject> collection = (Collection<IdentifiableObject>) property.getGetterMethod().invoke(object);
manager.refresh(object);
if (property.isOwner()) {
collection.clear();
manager.update(object);
} else {
for (IdentifiableObject itemObject : collection) {
Schema itemSchema = schemaService.getDynamicSchema(property.getItemKlass());
Property itemProperty = itemSchema.propertyByRole(property.getOwningRole());
Collection<IdentifiableObject> itemCollection = (Collection<IdentifiableObject>) itemProperty.getGetterMethod().invoke(itemObject);
itemCollection.remove(object);
manager.update(itemObject);
manager.refresh(itemObject);
}
}
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.notFound in project dhis2-core by dhis2.
the class DefaultCollectionService method addCollectionItems.
@Override
@SuppressWarnings("unchecked")
public void addCollectionItems(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 added to."));
}
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.add(item);
}
manager.update(object);
} 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.add(object);
manager.update(item);
}
} catch (Exception ex) {
}
}
}
dbmsManager.clearSession();
cacheManager.clearCache();
}
Aggregations