Search in sources :

Example 31 with MetadataImportParams

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

the class MapController method putJsonObject.

// --------------------------------------------------------------------------
// CRUD
// --------------------------------------------------------------------------
@Override
@PutMapping(value = "/{uid}", consumes = APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public WebMessage putJsonObject(@PathVariable String uid, @CurrentUser User currentUser, HttpServletRequest request) throws Exception {
    Map map = mappingService.getMap(uid);
    if (map == null) {
        return notFound("Map does not exist: " + uid);
    }
    MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap());
    Map newMap = deserializeJsonEntity(request);
    newMap.setUid(uid);
    mergeService.merge(new MergeParams<>(newMap, map).setMergeMode(params.getMergeMode()).setSkipSharing(params.isSkipSharing()).setSkipTranslation(params.isSkipTranslation()));
    mappingService.updateMap(map);
    return null;
}
Also used : MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) MergeParams(org.hisp.dhis.schema.MergeParams) Map(org.hisp.dhis.mapping.Map) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PutMapping(org.springframework.web.bind.annotation.PutMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 32 with MetadataImportParams

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

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

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

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

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