Search in sources :

Example 36 with MetadataImportParams

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

Example 37 with MetadataImportParams

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

the class AbstractCrudController method postXmlObject.

@RequestMapping(method = RequestMethod.POST, consumes = { "application/xml", "text/xml" })
public void postXmlObject(HttpServletRequest request, HttpServletResponse response) throws Exception {
    User user = currentUserService.getCurrentUser();
    if (!aclService.canCreate(user, getEntityClass())) {
        throw new CreateAccessDeniedException("You don't have the proper permissions to create this object.");
    }
    T parsed = deserializeXmlEntity(request, response);
    parsed.getTranslations().clear();
    preCreateEntity(parsed);
    MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap()).setImportReportMode(ImportReportMode.FULL).setUser(user).setImportStrategy(ImportStrategy.CREATE).addObject(parsed);
    ImportReport importReport = importService.importMetadata(params);
    ObjectReport objectReport = getObjectReport(importReport);
    WebMessage webMessage = WebMessageUtils.objectReport(objectReport);
    if (objectReport != null && webMessage.getStatus() == Status.OK) {
        String location = contextService.getApiPath() + getSchema().getRelativeApiEndpoint() + "/" + objectReport.getUid();
        webMessage.setHttpStatus(HttpStatus.CREATED);
        response.setHeader(ContextUtils.HEADER_LOCATION, location);
        T entity = manager.get(getEntityClass(), objectReport.getUid());
        postCreateEntity(entity);
    } else {
        webMessage.setStatus(Status.ERROR);
    }
    webMessageService.send(webMessage, response, request);
}
Also used : User(org.hisp.dhis.user.User) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) CreateAccessDeniedException(org.hisp.dhis.hibernate.exception.CreateAccessDeniedException) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) ObjectReport(org.hisp.dhis.feedback.ObjectReport) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 38 with MetadataImportParams

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

the class AbstractCrudController method postJsonObject.

//--------------------------------------------------------------------------
// POST
//--------------------------------------------------------------------------
@RequestMapping(method = RequestMethod.POST, consumes = "application/json")
public void postJsonObject(HttpServletRequest request, HttpServletResponse response) throws Exception {
    User user = currentUserService.getCurrentUser();
    if (!aclService.canCreate(user, getEntityClass())) {
        throw new CreateAccessDeniedException("You don't have the proper permissions to create this object.");
    }
    T parsed = deserializeJsonEntity(request, response);
    parsed.getTranslations().clear();
    preCreateEntity(parsed);
    MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap()).setImportReportMode(ImportReportMode.FULL).setUser(user).setImportStrategy(ImportStrategy.CREATE).addObject(parsed);
    ImportReport importReport = importService.importMetadata(params);
    ObjectReport objectReport = getObjectReport(importReport);
    WebMessage webMessage = WebMessageUtils.objectReport(objectReport);
    if (objectReport != null && webMessage.getStatus() == Status.OK) {
        String location = contextService.getApiPath() + getSchema().getRelativeApiEndpoint() + "/" + objectReport.getUid();
        webMessage.setHttpStatus(HttpStatus.CREATED);
        response.setHeader(ContextUtils.HEADER_LOCATION, location);
        T entity = manager.get(getEntityClass(), objectReport.getUid());
        postCreateEntity(entity);
    } else {
        webMessage.setStatus(Status.ERROR);
    }
    webMessageService.send(webMessage, response, request);
}
Also used : User(org.hisp.dhis.user.User) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) CreateAccessDeniedException(org.hisp.dhis.hibernate.exception.CreateAccessDeniedException) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) ObjectReport(org.hisp.dhis.feedback.ObjectReport) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 39 with MetadataImportParams

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

the class AbstractCrudController method putXmlObject.

@RequestMapping(value = "/{uid}", method = RequestMethod.PUT, consumes = { MediaType.APPLICATION_XML_VALUE, MediaType.TEXT_XML_VALUE })
public void putXmlObject(@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 = deserializeXmlEntity(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 40 with MetadataImportParams

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

the class MetadataSyncTask method handleMetadataSync.

//----------------------------------------------------------------------------------------
// Private Methods
//----------------------------------------------------------------------------------------
private MetadataSyncSummary handleMetadataSync(MetadataRetryContext context, MetadataVersion dataVersion) throws DhisVersionMismatchException {
    MetadataSyncParams syncParams = new MetadataSyncParams(new MetadataImportParams(), dataVersion);
    MetadataSyncSummary metadataSyncSummary = null;
    try {
        metadataSyncSummary = metadataSyncService.doMetadataSync(syncParams);
    } catch (MetadataSyncServiceException e) {
        log.error("Exception happened  while trying to do metadata sync  " + e.getMessage(), e);
        context.updateRetryContext(METADATA_SYNC, e.getMessage(), dataVersion);
        throw e;
    } catch (DhisVersionMismatchException e) {
        context.updateRetryContext(METADATA_SYNC, e.getMessage(), dataVersion);
        throw e;
    }
    return metadataSyncSummary;
}
Also used : MetadataSyncServiceException(org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncServiceException) MetadataSyncParams(org.hisp.dhis.dxf2.metadata.sync.MetadataSyncParams) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) DhisVersionMismatchException(org.hisp.dhis.dxf2.metadata.sync.exception.DhisVersionMismatchException) MetadataSyncSummary(org.hisp.dhis.dxf2.metadata.sync.MetadataSyncSummary)

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