Search in sources :

Example 36 with ImportReport

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

the class MetadataImportExportController method postCsvMetadata.

@PostMapping(value = "", consumes = "application/csv")
@ResponseBody
public WebMessage postCsvMetadata(HttpServletRequest request) throws IOException {
    MetadataImportParams params = metadataImportService.getParamsFromMap(contextService.getParameterValuesMap());
    String classKey = request.getParameter("classKey");
    if (StringUtils.isEmpty(classKey) || !CsvImportClass.classExists(classKey)) {
        return conflict("Cannot find Csv import class:  " + classKey);
    }
    params.setCsvImportClass(CsvImportClass.valueOf(classKey));
    Metadata metadata = csvImportService.fromCsv(request.getInputStream(), new CsvImportOptions().setImportClass(params.getCsvImportClass()).setFirstRowIsHeader(params.isFirstRowIsHeader()));
    params.addMetadata(schemaService.getMetadataSchemas(), metadata);
    if (params.hasJobId()) {
        return startAsyncMetadata(params);
    }
    ImportReport importReport = metadataImportService.importMetadata(params);
    return importReport(importReport).withPlainResponseBefore(DhisApiVersion.V38);
}
Also used : CsvImportOptions(org.hisp.dhis.dxf2.csv.CsvImportOptions) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) Metadata(org.hisp.dhis.dxf2.metadata.Metadata) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 37 with ImportReport

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

the class MetadataImportExportController method postGmlMetadata.

@PostMapping(value = "/gml", consumes = APPLICATION_XML_VALUE)
@ResponseBody
public WebMessage postGmlMetadata(HttpServletRequest request) throws IOException {
    MetadataImportParams params = metadataImportService.getParamsFromMap(contextService.getParameterValuesMap());
    if (params.hasJobId()) {
        return startAsyncGml(params, request);
    }
    ImportReport importReport = gmlImportService.importGml(request.getInputStream(), params);
    return importReport(importReport).withPlainResponseBefore(DhisApiVersion.V38);
}
Also used : MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 38 with ImportReport

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

the class DefaultSynchronizationManager method executeMetadataPull.

@Override
public ImportReport executeMetadataPull(String url) {
    User user = currentUserService.getCurrentUser();
    String userUid = user != null ? user.getUid() : null;
    log.info(String.format("Metadata pull, url: %s, user: %s", url, userUid));
    String json = restTemplate.getForObject(url, String.class);
    Metadata metadata = null;
    try {
        metadata = jsonMapper.readValue(json, Metadata.class);
    } catch (IOException ex) {
        throw new RuntimeException("Failed to parse remote JSON document", ex);
    }
    MetadataImportParams importParams = new MetadataImportParams();
    importParams.setSkipSharing(true);
    importParams.setAtomicMode(AtomicMode.NONE);
    importParams.addMetadata(schemaService.getMetadataSchemas(), metadata);
    return importService.importMetadata(importParams);
}
Also used : User(org.hisp.dhis.user.User) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) Metadata(org.hisp.dhis.dxf2.metadata.Metadata) IOException(java.io.IOException)

Example 39 with ImportReport

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

the class UserController method updateUser.

protected ImportReport updateUser(String userUid, User parsedUserObject) throws WebMessageException {
    List<User> users = getEntity(userUid, NO_WEB_OPTIONS);
    if (users.isEmpty()) {
        throw new WebMessageException(conflict(getEntityName() + " does not exist: " + userUid));
    }
    User currentUser = currentUserService.getCurrentUser();
    if (!aclService.canUpdate(currentUser, users.get(0))) {
        throw new UpdateAccessDeniedException("You don't have the proper permissions to update this user.");
    }
    // force initialization of all authorities of current user in order to
    // prevent cases where user must be reloaded later
    // (in case it gets detached)
    currentUser.getAllAuthorities();
    parsedUserObject.setId(users.get(0).getId());
    parsedUserObject.setUid(userUid);
    mergeLastLoginAttribute(users.get(0), parsedUserObject);
    boolean isPasswordChangeAttempt = parsedUserObject.getPassword() != null;
    List<String> groupsUids = getUids(parsedUserObject.getGroups());
    if (!userService.canAddOrUpdateUser(groupsUids, currentUser) || !currentUser.canModifyUser(users.get(0))) {
        throw new WebMessageException(conflict("You must have permissions to create user, " + "or ability to manage at least one user group for the user."));
    }
    MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap());
    params.setImportReportMode(ImportReportMode.FULL);
    params.setImportStrategy(ImportStrategy.UPDATE);
    params.addObject(parsedUserObject);
    ImportReport importReport = importService.importMetadata(params);
    if (importReport.getStatus() == Status.OK && importReport.getStats().getUpdated() == 1) {
        updateUserGroups(userUid, parsedUserObject, currentUser);
        // same. i.e. no before & after equals pw check
        if (isPasswordChangeAttempt) {
            userService.expireActiveSessions(parsedUserObject);
        }
    }
    return importReport;
}
Also used : CurrentUser(org.hisp.dhis.user.CurrentUser) User(org.hisp.dhis.user.User) 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)

Example 40 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) {
    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, getUids(user.getGroups()), currentUser);
    }
    return importReport;
}
Also used : MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport)

Aggregations

ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)87 Test (org.junit.jupiter.api.Test)52 List (java.util.List)39 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)37 ClassPathResource (org.springframework.core.io.ClassPathResource)37 User (org.hisp.dhis.user.User)34 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)33 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)32 DhisSpringTest (org.hisp.dhis.DhisSpringTest)12 DataSet (org.hisp.dhis.dataset.DataSet)12 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)11 IOException (java.io.IOException)10 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)10 CsvImportOptions (org.hisp.dhis.dxf2.csv.CsvImportOptions)9 InputStream (java.io.InputStream)8 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)8 UserService (org.hisp.dhis.user.UserService)8 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8