Search in sources :

Example 6 with ImportReport

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

the class AbstractCrudController method putJsonObject.

//--------------------------------------------------------------------------
// PUT
//--------------------------------------------------------------------------
@RequestMapping(value = "/{uid}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
public void putJsonObject(@PathVariable("uid") String pvUid, HttpServletRequest request, HttpServletResponse response) throws Exception {
    List<T> objects = getEntity(pvUid);
    if (objects.isEmpty()) {
        throw new WebMessageException(WebMessageUtils.notFound(getEntityClass(), pvUid));
    }
    User user = currentUserService.getCurrentUser();
    if (!aclService.canUpdate(user, objects.get(0))) {
        throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
    }
    T parsed = deserializeJsonEntity(request, response);
    ((BaseIdentifiableObject) parsed).setUid(pvUid);
    preUpdateEntity(objects.get(0), parsed);
    MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap()).setImportReportMode(ImportReportMode.FULL).setUser(user).setImportStrategy(ImportStrategy.UPDATE).addObject(parsed);
    ImportReport importReport = importService.importMetadata(params);
    WebMessage webMessage = WebMessageUtils.objectReport(importReport);
    if (importReport.getStatus() == Status.OK) {
        T entity = manager.get(getEntityClass(), pvUid);
        postUpdateEntity(entity);
    } else {
        webMessage.setStatus(Status.ERROR);
    }
    webMessageService.send(webMessage, response, request);
}
Also used : User(org.hisp.dhis.user.User) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with ImportReport

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

the class DefaultMetadataImportService method importMetadata.

@Override
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());
    }
    String message = "(" + params.getUsername() + ") Import:Start";
    log.info(message);
    if (params.hasTaskId()) {
        notifier.notify(params.getTaskId(), message);
    }
    ObjectBundleParams bundleParams = params.toObjectBundleParams();
    ObjectBundle bundle = objectBundleService.create(bundleParams);
    prepareBundle(bundle);
    ObjectBundleValidationReport validationReport = objectBundleValidationService.validate(bundle);
    importReport.addTypeReports(validationReport.getTypeReportMap());
    if (!(!validationReport.getErrorReports().isEmpty() && AtomicMode.ALL == bundle.getAtomicMode())) {
        Timer commitTimer = new SystemTimer().start();
        ObjectBundleCommitReport commitReport = objectBundleService.commit(bundle);
        importReport.addTypeReports(commitReport.getTypeReportMap());
        if (!importReport.getErrorReports().isEmpty()) {
            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.hasTaskId()) {
        notifier.notify(bundle.getTaskId(), NotificationLevel.INFO, message, true).addTaskSummary(bundle.getTaskId(), importReport);
    }
    if (ObjectBundleMode.VALIDATE == params.getImportMode()) {
        return importReport;
    }
    Lists.newArrayList(importReport.getTypeReportMap().keySet()).forEach(typeReportKey -> {
        if (importReport.getTypeReportMap().get(typeReportKey).getStats().getTotal() == 0) {
            importReport.getTypeReportMap().remove(typeReportKey);
            return;
        }
        TypeReport typeReport = importReport.getTypeReportMap().get(typeReportKey);
        if (ImportReportMode.ERRORS == params.getImportReportMode()) {
            Lists.newArrayList(typeReport.getObjectReportMap().keySet()).forEach(objectReportKey -> {
                if (typeReport.getObjectReportMap().get(objectReportKey).getErrorReportsByCode().isEmpty()) {
                    typeReport.getObjectReportMap().remove(objectReportKey);
                }
            });
        }
        if (ImportReportMode.DEBUG != params.getImportReportMode()) {
            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) TypeReport(org.hisp.dhis.feedback.TypeReport) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) SystemTimer(org.hisp.dhis.commons.timer.SystemTimer)

Example 8 with ImportReport

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

the class MetadataImportController method postXmlMetadata.

@RequestMapping(value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_XML_VALUE)
public void postXmlMetadata(HttpServletRequest request, HttpServletResponse response) throws IOException {
    MetadataImportParams params = metadataImportService.getParamsFromMap(contextService.getParameterValuesMap());
    Metadata metadata = renderService.fromXml(StreamUtils.wrapAndCheckCompressionFormat(request.getInputStream()), Metadata.class);
    params.addMetadata(schemaService.getMetadataSchemas(), metadata);
    if (params.hasTaskId()) {
        startAsync(params);
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
    } else {
        ImportReport importReport = metadataImportService.importMetadata(params);
        renderService.toXml(response.getOutputStream(), importReport);
    }
}
Also used : MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) Metadata(org.hisp.dhis.dxf2.metadata.Metadata) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with ImportReport

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

the class UserController method createUser.

/**
     * Creates a user.
     *
     * @param user user object parsed from the POST request.
     */
private ImportReport createUser(User user, User currentUser) throws Exception {
    user.getUserCredentials().getCogsDimensionConstraints().addAll(currentUser.getUserCredentials().getCogsDimensionConstraints());
    user.getUserCredentials().getCatDimensionConstraints().addAll(currentUser.getUserCredentials().getCatDimensionConstraints());
    MetadataImportParams importParams = new MetadataImportParams().setImportReportMode(ImportReportMode.FULL).setImportStrategy(ImportStrategy.CREATE).addObject(user);
    ImportReport importReport = importService.importMetadata(importParams);
    if (importReport.getStatus() == Status.OK && importReport.getStats().getCreated() == 1) {
        userGroupService.addUserToGroups(user, IdentifiableObjectUtils.getUids(user.getGroups()), currentUser);
    }
    return importReport;
}
Also used : MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport)

Example 10 with ImportReport

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

the class SynchronizationController method importMetaData.

@PreAuthorize("hasRole('ALL')")
@RequestMapping(value = "/metadataPull", method = RequestMethod.POST)
public void importMetaData(@RequestBody String url, HttpServletResponse response) throws IOException {
    ImportReport importReport = synchronizationManager.executeMetadataPull(url);
    response.setContentType(CONTENT_TYPE_JSON);
    renderService.toJson(response.getOutputStream(), importReport);
}
Also used : ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)28 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)15 Test (org.junit.Test)14 DhisSpringTest (org.hisp.dhis.DhisSpringTest)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)10 User (org.hisp.dhis.user.User)8 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)5 ClassPathResource (org.springframework.core.io.ClassPathResource)5 List (java.util.List)4 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)4 MetadataRetryContext (org.hisp.dhis.dxf2.metadata.tasks.MetadataRetryContext)4 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)4 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)4 IntegrationTest (org.hisp.dhis.IntegrationTest)3 ObjectReport (org.hisp.dhis.feedback.ObjectReport)3 TypeReport (org.hisp.dhis.feedback.TypeReport)3 IOException (java.io.IOException)2 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)2 DataElement (org.hisp.dhis.dataelement.DataElement)2 Metadata (org.hisp.dhis.dxf2.metadata.Metadata)2