Search in sources :

Example 46 with WebMessageUtils.notFound

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);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataSet(org.hisp.dhis.dataset.DataSet) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) DataEntryForm(org.hisp.dhis.dataentryform.DataEntryForm) Form(org.hisp.dhis.webapi.webdomain.form.Form) Period(org.hisp.dhis.period.Period) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 47 with WebMessageUtils.notFound

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);
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Interpretation(org.hisp.dhis.interpretation.Interpretation) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 48 with WebMessageUtils.notFound

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);
}
Also used : KeyJsonValue(org.hisp.dhis.keyjsonvalue.KeyJsonValue) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 49 with WebMessageUtils.notFound

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);
        }
    }
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Schema(org.hisp.dhis.schema.Schema) Collection(java.util.Collection) Property(org.hisp.dhis.schema.Property) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 50 with WebMessageUtils.notFound

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();
}
Also used : UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Schema(org.hisp.dhis.schema.Schema) Collection(java.util.Collection) Property(org.hisp.dhis.schema.Property) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Aggregations

WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)59 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)51 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)17 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)17 User (org.hisp.dhis.user.User)12 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)11 InputStream (java.io.InputStream)7 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)7 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)7 Dashboard (org.hisp.dhis.dashboard.Dashboard)7 Property (org.hisp.dhis.schema.Property)7 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)6 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)6 Schema (org.hisp.dhis.schema.Schema)6 DashboardItem (org.hisp.dhis.dashboard.DashboardItem)5 Event (org.hisp.dhis.dxf2.events.event.Event)5 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)5 WebOptions (org.hisp.dhis.webapi.webdomain.WebOptions)5 IOException (java.io.IOException)4 DataElement (org.hisp.dhis.dataelement.DataElement)4