Search in sources :

Example 6 with MetadataImportParams

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

the class MapController method putJsonObject.

@Override
@RequestMapping(value = "/{uid}", method = RequestMethod.PUT, consumes = "application/json")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void putJsonObject(@PathVariable String uid, HttpServletRequest request, HttpServletResponse response) throws Exception {
    Map map = mappingService.getMap(uid);
    if (map == null) {
        throw new WebMessageException(WebMessageUtils.notFound("Map does not exist: " + uid));
    }
    MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap());
    Map newMap = deserializeJsonEntity(request, response);
    newMap.setUid(uid);
    mergeService.merge(new MergeParams<>(newMap, map).setMergeMode(params.getMergeMode()));
    mappingService.updateMap(map);
}
Also used : MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) MergeParams(org.hisp.dhis.schema.MergeParams) Map(org.hisp.dhis.mapping.Map) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with MetadataImportParams

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

the class DefaultMetadataSyncService method getParamsFromMap.

@Override
public MetadataSyncParams getParamsFromMap(Map<String, List<String>> parameters) {
    List<String> versionName = getVersionsFromParams(parameters);
    MetadataImportParams importParams = new MetadataImportParams();
    importParams.setMetadataSyncImport(true);
    MetadataSyncParams syncParams = new MetadataSyncParams();
    syncParams.setImportParams(importParams);
    String versionNameStr = versionName.get(0);
    if (StringUtils.isNotEmpty(versionNameStr)) {
        MetadataVersion version;
        try {
            version = metadataVersionDelegate.getRemoteMetadataVersion(versionNameStr);
        } catch (MetadataVersionServiceException e) {
            throw new MetadataSyncServiceException(e.getMessage(), e);
        }
        if (version == null) {
            throw new MetadataSyncServiceException("The MetadataVersion could not be fetched from the remote server for the versionName: " + versionNameStr);
        }
        syncParams.setVersion(version);
    }
    syncParams.setParameters(parameters);
    return syncParams;
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) MetadataSyncServiceException(org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncServiceException) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams)

Example 8 with MetadataImportParams

use of org.hisp.dhis.dxf2.metadata.MetadataImportParams 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 9 with MetadataImportParams

use of org.hisp.dhis.dxf2.metadata.MetadataImportParams 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 10 with MetadataImportParams

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

the class MetadataImportParams method addMetadata.

@SuppressWarnings("unchecked")
public MetadataImportParams addMetadata(List<Schema> schemas, Metadata metadata) {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> objectMap = new HashMap<>();
    for (Schema schema : schemas) {
        if (schema.isIdentifiableObject()) {
            Class<? extends IdentifiableObject> key = (Class<? extends IdentifiableObject>) schema.getKlass();
            List<? extends IdentifiableObject> schemaObjects = metadata.getValues(key);
            if (!schemaObjects.isEmpty()) {
                objectMap.put(key, new ArrayList<>(schemaObjects));
            }
        }
    }
    setObjects(objectMap);
    return this;
}
Also used : HashMap(java.util.HashMap) Schema(org.hisp.dhis.schema.Schema) CsvImportClass(org.hisp.dhis.dxf2.csv.CsvImportClass) ArrayList(java.util.ArrayList) List(java.util.List) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

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