Search in sources :

Example 26 with ImportReport

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

the class UserController method putJsonObject.

@Override
@RequestMapping(value = "/{uid}", method = RequestMethod.PUT, consumes = "application/json")
public void putJsonObject(@PathVariable("uid") String pvUid, HttpServletRequest request, HttpServletResponse response) throws Exception {
    List<User> users = getEntity(pvUid, NO_WEB_OPTIONS);
    if (users.isEmpty()) {
        throw new WebMessageException(WebMessageUtils.conflict(getEntityName() + " does not exist: " + pvUid));
    }
    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.");
    }
    User parsed = renderService.fromJson(request.getInputStream(), getEntityClass());
    parsed.setUid(pvUid);
    if (!userService.canAddOrUpdateUser(IdentifiableObjectUtils.getUids(parsed.getGroups()), currentUser)) {
        throw new WebMessageException(WebMessageUtils.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(parsed);
    ImportReport importReport = importService.importMetadata(params);
    if (importReport.getStatus() == Status.OK && importReport.getStats().getUpdated() == 1) {
        User user = userService.getUser(pvUid);
        userGroupService.updateUserGroups(user, IdentifiableObjectUtils.getUids(parsed.getGroups()), currentUser);
    }
    renderService.toJson(response.getOutputStream(), importReport);
}
Also used : 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) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 27 with ImportReport

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

the class UserController method inviteUser.

/**
     * Creates a user invitation and invites the user.
     *
     * @param user user object parsed from the POST request.
     */
private ObjectReport inviteUser(User user, User currentUser, HttpServletRequest request) throws Exception {
    RestoreOptions restoreOptions = user.getUsername() == null || user.getUsername().isEmpty() ? RestoreOptions.INVITE_WITH_USERNAME_CHOICE : RestoreOptions.INVITE_WITH_DEFINED_USERNAME;
    securityService.prepareUserForInvite(user);
    ImportReport importReport = createUser(user, currentUser);
    ObjectReport objectReport = getObjectReport(importReport);
    if (importReport.getStatus() == Status.OK && importReport.getStats().getCreated() == 1) {
        securityService.sendRestoreMessage(user.getUserCredentials(), ContextUtils.getContextPath(request), restoreOptions);
    }
    return objectReport;
}
Also used : RestoreOptions(org.hisp.dhis.security.RestoreOptions) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) ObjectReport(org.hisp.dhis.feedback.ObjectReport)

Example 28 with ImportReport

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

the class UserController method putXmlObject.

// -------------------------------------------------------------------------
// PUT
// -------------------------------------------------------------------------
@Override
@RequestMapping(value = "/{uid}", method = RequestMethod.PUT, consumes = { "application/xml", "text/xml" })
public void putXmlObject(@PathVariable("uid") String pvUid, HttpServletRequest request, HttpServletResponse response) throws Exception {
    List<User> users = getEntity(pvUid, NO_WEB_OPTIONS);
    if (users.isEmpty()) {
        throw new WebMessageException(WebMessageUtils.conflict(getEntityName() + " does not exist: " + pvUid));
    }
    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.");
    }
    User parsed = renderService.fromXml(request.getInputStream(), getEntityClass());
    parsed.setUid(pvUid);
    if (!userService.canAddOrUpdateUser(IdentifiableObjectUtils.getUids(parsed.getGroups()), currentUser)) {
        throw new WebMessageException(WebMessageUtils.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(parsed);
    ImportReport importReport = importService.importMetadata(params);
    if (importReport.getStatus() == Status.OK && importReport.getStats().getUpdated() == 1) {
        User user = userService.getUser(pvUid);
        userGroupService.updateUserGroups(user, IdentifiableObjectUtils.getUids(parsed.getGroups()), currentUser);
    }
    renderService.toXml(response.getOutputStream(), importReport);
}
Also used : 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) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 29 with ImportReport

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

the class CsvMetadataImportTest method testOptionSetImport.

@Test
public void testOptionSetImport() throws Exception {
    input = new ClassPathResource("metadata/optionSets.csv").getInputStream();
    Metadata metadata = csvImportService.fromCsv(input, OptionSet.class);
    assertEquals(4, metadata.getOptionSets().size());
    assertEquals(3, metadata.getOptionSets().get(0).getOptions().size());
    assertEquals(3, metadata.getOptionSets().get(1).getOptions().size());
    assertEquals(3, metadata.getOptionSets().get(2).getOptions().size());
    assertEquals(3, metadata.getOptionSets().get(3).getOptions().size());
    MetadataImportParams params = new MetadataImportParams();
    params.addMetadata(schemaService.getMetadataSchemas(), metadata);
    ImportReport importReport = importService.importMetadata(params);
    assertEquals(16, importReport.getStats().getCreated());
    List<OptionSet> optionSets = new ArrayList<>(optionService.getAllOptionSets());
    assertEquals(4, optionSets.size());
    assertEquals(3, optionSets.get(0).getOptions().size());
    assertEquals(3, optionSets.get(1).getOptions().size());
    assertEquals(3, optionSets.get(2).getOptions().size());
    assertEquals(3, optionSets.get(3).getOptions().size());
}
Also used : ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) ArrayList(java.util.ArrayList) OptionSet(org.hisp.dhis.option.OptionSet) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 30 with ImportReport

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

the class CsvMetadataImportTest method testDataElementImport.

@Test
public void testDataElementImport() throws Exception {
    input = new ClassPathResource("metadata/dataElements.csv").getInputStream();
    Metadata metadata = csvImportService.fromCsv(input, DataElement.class);
    assertEquals(2, metadata.getDataElements().size());
    MetadataImportParams params = new MetadataImportParams();
    params.addMetadata(schemaService.getMetadataSchemas(), metadata);
    ImportReport importReport = importService.importMetadata(params);
    assertEquals(2, importReport.getStats().getCreated());
    Collection<DataElement> dataElements = dataElementService.getAllDataElements();
    assertEquals(2, dataElements.size());
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

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