Search in sources :

Example 11 with MetadataImportParams

use of org.hisp.dhis.dxf2.metadata.MetadataImportParams in project dhis2-core by dhis2.

the class DefaultGmlImportService method importGml.

// -------------------------------------------------------------------------
// GmlImportService implementation
// -------------------------------------------------------------------------
@Transactional
@Override
public ImportReport importGml(InputStream inputStream, MetadataImportParams importParams) {
    ImportReport importReport = new ImportReport();
    if (!importParams.getImportStrategy().isUpdate()) {
        importParams.setImportStrategy(ImportStrategy.UPDATE);
        log.warn("Changed GML import strategy to update. Only updates are supported.");
    }
    PreProcessingResult preProcessed = preProcessGml(inputStream);
    if (preProcessed.isSuccess && preProcessed.metaData != null) {
        importParams.addMetadata(schemaService.getMetadataSchemas(), preProcessed.metaData);
        importReport = importService.importMetadata(importParams);
    } else {
        Throwable throwable = preProcessed.throwable;
        notifier.notify(importParams.getId(), NotificationLevel.ERROR, createNotifierErrorMessage(throwable), false);
        importReport.setStatus(Status.ERROR);
        ObjectReport objectReport = new ObjectReport(getClass(), 0);
        objectReport.addErrorReport(new ErrorReport(getClass(), new ErrorMessage(ErrorCode.E7010, createNotifierErrorMessage(throwable))));
        TypeReport typeReport = new TypeReport(getClass());
        typeReport.addObjectReport(objectReport);
        importReport.addTypeReport(typeReport);
        log.error("GML import failed: ", throwable);
    }
    return importReport;
}
Also used : ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) Transactional(org.springframework.transaction.annotation.Transactional)

Example 12 with MetadataImportParams

use of org.hisp.dhis.dxf2.metadata.MetadataImportParams in project dhis2-core by dhis2.

the class DefaultMetadataWorkflowService method acceptAdd.

private ImportReport acceptAdd(MetadataProposal proposal, ObjectBundleMode mode) {
    Class<? extends IdentifiableObject> objType = proposal.getTarget().getType();
    IdentifiableObject obj = mapJsonChangeToObject(proposal.getChange(), objType);
    if (obj == null) {
        return createJsonErrorReport(proposal);
    }
    MetadataImportParams params = createImportParams(mode, ImportStrategy.CREATE, obj);
    ImportReport report = importService.importMetadata(params);
    if (report.getStatus() == Status.OK) {
        TypeReport typeReport = report.getTypeReport(objType);
        ObjectReport objectReport = new ObjectReport(obj, object -> 0);
        typeReport.addObjectReport(objectReport);
    }
    return report;
}
Also used : MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) TypeReport(org.hisp.dhis.feedback.TypeReport) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) ObjectReport(org.hisp.dhis.feedback.ObjectReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 13 with MetadataImportParams

use of org.hisp.dhis.dxf2.metadata.MetadataImportParams in project dhis2-core by dhis2.

the class AbstractCrudController method deleteObject.

// --------------------------------------------------------------------------
// DELETE
// --------------------------------------------------------------------------
@DeleteMapping(value = "/{uid}")
@ResponseBody
public WebMessage deleteObject(@PathVariable("uid") String pvUid, @CurrentUser User currentUser, HttpServletRequest request, HttpServletResponse response) throws Exception {
    List<T> objects = getEntity(pvUid);
    if (objects.isEmpty()) {
        return notFound(getEntityClass(), pvUid);
    }
    if (!aclService.canDelete(currentUser, objects.get(0))) {
        throw new DeleteAccessDeniedException("You don't have the proper permissions to delete this object.");
    }
    preDeleteEntity(objects.get(0));
    MetadataImportParams params = new MetadataImportParams().setImportReportMode(ImportReportMode.FULL).setUser(currentUser).setImportStrategy(ImportStrategy.DELETE).addObject(objects.get(0));
    ImportReport importReport = importService.importMetadata(params);
    postDeleteEntity(pvUid);
    return objectReport(importReport);
}
Also used : MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) DeleteAccessDeniedException(org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 14 with MetadataImportParams

use of org.hisp.dhis.dxf2.metadata.MetadataImportParams in project dhis2-core by dhis2.

the class AbstractCrudController method bulkSharing.

@ResponseBody
@PatchMapping(path = "/sharing", consumes = "application/json-patch+json", produces = APPLICATION_JSON_VALUE)
public WebMessage bulkSharing(@RequestParam(required = false, defaultValue = "false") boolean atomic, HttpServletRequest request) throws Exception {
    final BulkJsonPatch bulkJsonPatch = jsonMapper.readValue(request.getInputStream(), BulkJsonPatch.class);
    BulkPatchParameters patchParams = BulkPatchParameters.builder().validators(BulkPatchValidatorFactory.SHARING).build();
    List<IdentifiableObject> patchedObjects = bulkPatchManager.applyPatch(bulkJsonPatch, patchParams);
    if (patchedObjects.isEmpty() || (atomic && patchParams.hasErrorReports())) {
        ImportReport importReport = new ImportReport();
        importReport.addTypeReports(patchParams.getTypeReports());
        importReport.setStatus(Status.ERROR);
        return importReport(importReport);
    }
    Map<String, List<String>> parameterValuesMap = contextService.getParameterValuesMap();
    MetadataImportParams params = importService.getParamsFromMap(parameterValuesMap);
    params.setUser(currentUserService.getCurrentUser()).setImportStrategy(ImportStrategy.UPDATE).addObjects(patchedObjects);
    ImportReport importReport = importService.importMetadata(params);
    if (patchParams.hasErrorReports()) {
        importReport.addTypeReports(patchParams.getTypeReports());
        importReport.setStatus(importReport.getStatus() == Status.OK ? Status.WARNING : importReport.getStatus());
    }
    return importReport(importReport);
}
Also used : MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) BulkPatchParameters(org.hisp.dhis.jsonpatch.BulkPatchParameters) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) BulkJsonPatch(org.hisp.dhis.jsonpatch.BulkJsonPatch) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) PatchMapping(org.springframework.web.bind.annotation.PatchMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 15 with MetadataImportParams

use of org.hisp.dhis.dxf2.metadata.MetadataImportParams in project dhis2-core by dhis2.

the class AbstractCrudController method patchObject.

// --------------------------------------------------------------------------
// PATCH
// --------------------------------------------------------------------------
/**
 * Adds support for HTTP Patch using JSON Patch (RFC 6902), updated object
 * is run through normal metadata importer and internally looks like a
 * normal PUT (after the JSON Patch has been applied).
 *
 * For now we only support the official mimetype
 * "application/json-patch+json" but in future releases we might also want
 * to support "application/json" after the old patch behavior has been
 * removed.
 */
@ResponseBody
@PatchMapping(path = "/{uid}", consumes = "application/json-patch+json")
public WebMessage patchObject(@PathVariable("uid") String pvUid, @RequestParam Map<String, String> rpParameters, @CurrentUser User currentUser, HttpServletRequest request) throws Exception {
    WebOptions options = new WebOptions(rpParameters);
    List<T> entities = getEntity(pvUid, options);
    if (entities.isEmpty()) {
        return notFound(getEntityClass(), pvUid);
    }
    final T persistedObject = entities.get(0);
    if (!aclService.canUpdate(currentUser, persistedObject)) {
        throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
    }
    manager.resetNonOwnerProperties(persistedObject);
    prePatchEntity(persistedObject);
    final JsonPatch patch = jsonMapper.readValue(request.getInputStream(), JsonPatch.class);
    final T patchedObject = jsonPatchManager.apply(patch, persistedObject);
    // we don't allow changing IDs
    ((BaseIdentifiableObject) patchedObject).setId(persistedObject.getId());
    // we don't allow changing UIDs
    ((BaseIdentifiableObject) patchedObject).setUid(persistedObject.getUid());
    // Only supports new Sharing format
    ((BaseIdentifiableObject) patchedObject).clearLegacySharingCollections();
    prePatchEntity(persistedObject, patchedObject);
    Map<String, List<String>> parameterValuesMap = contextService.getParameterValuesMap();
    if (!parameterValuesMap.containsKey("importReportMode")) {
        parameterValuesMap.put("importReportMode", Collections.singletonList("ERRORS_NOT_OWNER"));
    }
    MetadataImportParams params = importService.getParamsFromMap(parameterValuesMap);
    params.setUser(currentUser).setImportStrategy(ImportStrategy.UPDATE).addObject(patchedObject);
    ImportReport importReport = importService.importMetadata(params);
    WebMessage webMessage = objectReport(importReport);
    if (importReport.getStatus() == Status.OK) {
        T entity = manager.get(getEntityClass(), pvUid);
        postPatchEntity(entity);
    } else {
        webMessage.setStatus(Status.ERROR);
    }
    return webMessage;
}
Also used : BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) BulkJsonPatch(org.hisp.dhis.jsonpatch.BulkJsonPatch) JsonPatch(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) PatchMapping(org.springframework.web.bind.annotation.PatchMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)58 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)49 List (java.util.List)38 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)38 Test (org.junit.jupiter.api.Test)38 ClassPathResource (org.springframework.core.io.ClassPathResource)31 User (org.hisp.dhis.user.User)30 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)29 DataSet (org.hisp.dhis.dataset.DataSet)12 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)12 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)10 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)9 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)8 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)8 HashMap (java.util.HashMap)6 UserGroup (org.hisp.dhis.user.UserGroup)6 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)6 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5