Search in sources :

Example 1 with ImportReportMode

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

the class DefaultMetadataImportService method getParamsFromMap.

@Override
@Transactional(readOnly = true)
public MetadataImportParams getParamsFromMap(Map<String, List<String>> parameters) {
    MetadataImportParams params = new MetadataImportParams();
    if (params.getUser() == null) {
        params.setUser(currentUserService.getCurrentUser());
    }
    params.setSkipSharing(getBooleanWithDefault(parameters, "skipSharing", false));
    params.setSkipTranslation(getBooleanWithDefault(parameters, "skipTranslation", false));
    params.setSkipValidation(getBooleanWithDefault(parameters, "skipValidation", false));
    params.setUserOverrideMode(getEnumWithDefault(UserOverrideMode.class, parameters, "userOverrideMode", UserOverrideMode.NONE));
    params.setImportMode(getEnumWithDefault(ObjectBundleMode.class, parameters, "importMode", ObjectBundleMode.COMMIT));
    params.setPreheatMode(getEnumWithDefault(PreheatMode.class, parameters, "preheatMode", PreheatMode.REFERENCE));
    params.setIdentifier(getEnumWithDefault(PreheatIdentifier.class, parameters, "identifier", PreheatIdentifier.UID));
    params.setImportStrategy(getEnumWithDefault(ImportStrategy.class, parameters, "importStrategy", ImportStrategy.CREATE_AND_UPDATE));
    params.setAtomicMode(getEnumWithDefault(AtomicMode.class, parameters, "atomicMode", AtomicMode.ALL));
    params.setMergeMode(getEnumWithDefault(MergeMode.class, parameters, "mergeMode", MergeMode.REPLACE));
    params.setFlushMode(getEnumWithDefault(FlushMode.class, parameters, "flushMode", FlushMode.AUTO));
    params.setImportReportMode(getEnumWithDefault(ImportReportMode.class, parameters, "importReportMode", ImportReportMode.ERRORS));
    params.setFirstRowIsHeader(getBooleanWithDefault(parameters, "firstRowIsHeader", true));
    if (getBooleanWithDefault(parameters, "async", false)) {
        JobConfiguration jobId = new JobConfiguration("metadataImport", JobType.METADATA_IMPORT, params.getUser().getUid(), true);
        notifier.clear(jobId);
        params.setId(jobId);
    }
    if (params.getUserOverrideMode() == UserOverrideMode.SELECTED) {
        User overrideUser = null;
        if (parameters.containsKey("overrideUser")) {
            List<String> overrideUsers = parameters.get("overrideUser");
            overrideUser = manager.get(User.class, overrideUsers.get(0));
        }
        if (overrideUser == null) {
            throw new MetadataImportException("UserOverrideMode.SELECTED is enabled, but overrideUser parameter does not point to a valid user.");
        } else {
            params.setOverrideUser(overrideUser);
        }
    }
    return params;
}
Also used : User(org.hisp.dhis.user.User) ObjectBundleMode(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundleMode) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier) ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) MergeMode(org.hisp.dhis.common.MergeMode) ImportReportMode(org.hisp.dhis.dxf2.metadata.feedback.ImportReportMode) JobConfiguration(org.hisp.dhis.scheduling.JobConfiguration) PreheatMode(org.hisp.dhis.preheat.PreheatMode) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with ImportReportMode

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

the class DefaultMetadataImportService method importMetadata.

@Override
@Transactional
public ImportReport importMetadata(MetadataImportParams params) {
    Timer timer = new SystemTimer().start();
    ImportReport importReport = new ImportReport();
    importReport.setImportParams(params);
    importReport.setStatus(Status.OK);
    if (params.getUser() == null) {
        params.setUser(currentUserService.getCurrentUser());
    }
    if (params.getUserOverrideMode() == UserOverrideMode.CURRENT) {
        params.setOverrideUser(currentUserService.getCurrentUser());
    }
    String message = "(" + params.getUsername() + ") Import:Start";
    log.info(message);
    if (params.hasJobId()) {
        notifier.notify(params.getId(), message);
    }
    preCreateBundle(params);
    ObjectBundleParams bundleParams = params.toObjectBundleParams();
    handleDeprecationIfEventReport(bundleParams);
    ObjectBundle bundle = objectBundleService.create(bundleParams);
    postCreateBundle(bundle, bundleParams);
    ObjectBundleValidationReport validationReport = objectBundleValidationService.validate(bundle);
    importReport.addTypeReports(validationReport);
    if (!validationReport.hasErrorReports() || AtomicMode.NONE == bundle.getAtomicMode()) {
        Timer commitTimer = new SystemTimer().start();
        ObjectBundleCommitReport commitReport = objectBundleService.commit(bundle);
        importReport.addTypeReports(commitReport);
        if (importReport.hasErrorReports()) {
            importReport.setStatus(Status.WARNING);
        }
        log.info("(" + bundle.getUsername() + ") Import:Commit took " + commitTimer.toString());
    } else {
        importReport.getStats().ignored();
        importReport.getTypeReports().forEach(tr -> tr.getStats().ignored());
        importReport.setStatus(Status.ERROR);
    }
    message = "(" + bundle.getUsername() + ") Import:Done took " + timer.toString();
    log.info(message);
    if (bundle.hasJobId()) {
        notifier.notify(bundle.getJobId(), NotificationLevel.INFO, message, true).addJobSummary(bundle.getJobId(), importReport, ImportReport.class);
    }
    if (ObjectBundleMode.VALIDATE == params.getImportMode()) {
        return importReport;
    }
    importReport.clean();
    importReport.forEachTypeReport(typeReport -> {
        ImportReportMode mode = params.getImportReportMode();
        if (ImportReportMode.ERRORS == mode) {
            typeReport.clean();
        }
        if (ImportReportMode.DEBUG != mode) {
            typeReport.getObjectReports().forEach(objectReport -> objectReport.setDisplayName(null));
        }
    });
    return importReport;
}
Also used : ObjectBundleParams(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundleParams) ObjectBundleCommitReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleCommitReport) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) SystemTimer(org.hisp.dhis.commons.timer.SystemTimer) Timer(org.hisp.dhis.commons.timer.Timer) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) ImportReportMode(org.hisp.dhis.dxf2.metadata.feedback.ImportReportMode) SystemTimer(org.hisp.dhis.commons.timer.SystemTimer) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with ImportReportMode

use of org.hisp.dhis.dxf2.metadata.feedback.ImportReportMode 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)

Example 4 with ImportReportMode

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

the class MetadataImportParams method toObjectBundleParams.

public ObjectBundleParams toObjectBundleParams() {
    ObjectBundleParams params = new ObjectBundleParams();
    params.setUser(user);
    params.setUserOverrideMode(userOverrideMode);
    params.setOverrideUser(overrideUser);
    params.setSkipSharing(skipSharing);
    params.setSkipTranslation(skipTranslation);
    params.setSkipValidation(skipValidation);
    params.setJobId(id);
    params.setImportStrategy(importStrategy);
    params.setAtomicMode(atomicMode);
    params.setObjects(objects);
    params.setPreheatIdentifier(identifier);
    params.setPreheatMode(preheatMode);
    params.setObjectBundleMode(importMode);
    params.setMergeMode(mergeMode);
    params.setFlushMode(flushMode);
    params.setImportReportMode(importReportMode);
    params.setMetadataSyncImport(metadataSyncImport);
    return params;
}
Also used : ObjectBundleParams(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundleParams)

Aggregations

ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)2 ImportReportMode (org.hisp.dhis.dxf2.metadata.feedback.ImportReportMode)2 ObjectBundleParams (org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundleParams)2 Transactional (org.springframework.transaction.annotation.Transactional)2 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)1 MergeMode (org.hisp.dhis.common.MergeMode)1 JsonPatch (org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch)1 SystemTimer (org.hisp.dhis.commons.timer.SystemTimer)1 Timer (org.hisp.dhis.commons.timer.Timer)1 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)1 ObjectBundle (org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle)1 ObjectBundleMode (org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundleMode)1 ObjectBundleCommitReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleCommitReport)1 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)1 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)1 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)1 ImportStrategy (org.hisp.dhis.importexport.ImportStrategy)1 BulkJsonPatch (org.hisp.dhis.jsonpatch.BulkJsonPatch)1