Search in sources :

Example 21 with ImportReport

use of org.hisp.dhis.dxf2.metadata.feedback.ImportReport 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 22 with ImportReport

use of org.hisp.dhis.dxf2.metadata.feedback.ImportReport 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 23 with ImportReport

use of org.hisp.dhis.dxf2.metadata.feedback.ImportReport 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 24 with ImportReport

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

the class MetadataSyncImportHandler method importMetadata.

public MetadataSyncSummary importMetadata(MetadataSyncParams syncParams, String versionSnapShot) {
    MetadataVersion version = getMetadataVersion(syncParams);
    MetadataImportParams importParams = syncParams.getImportParams();
    MetadataSyncSummary metadataSyncSummary = new MetadataSyncSummary();
    if (importParams == null) {
        throw new MetadataSyncServiceException("MetadataImportParams for the Sync cant be null.");
    }
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> classListMap = parseClassListMap(versionSnapShot);
    if (classListMap == null) {
        throw new MetadataSyncServiceException("ClassListMap can't be null");
    }
    importParams.setObjects(classListMap);
    ImportReport importReport = null;
    try {
        importReport = metadataImportService.importMetadata(importParams);
    } catch (Exception e) {
        String message = "Exception occurred while trying to import the metadata. " + e.getMessage();
        log.error(message, e);
        throw new MetadataSyncImportException(message, e);
    }
    boolean addNewVersion = handleImportReport(importReport, version);
    if (addNewVersion) {
        try {
            metadataVersionDelegate.addNewMetadataVersion(version);
        } catch (MetadataVersionServiceException e) {
            throw new MetadataSyncServiceException(e.getMessage(), e);
        }
    }
    metadataSyncSummary.setImportReport(importReport);
    metadataSyncSummary.setMetadataVersion(version);
    return metadataSyncSummary;
}
Also used : MetadataSyncServiceException(org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncServiceException) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) IOException(java.io.IOException) MetadataSyncImportException(org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncImportException) MetadataSyncServiceException(org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncServiceException) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) MetadataSyncImportException(org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncImportException) List(java.util.List)

Example 25 with ImportReport

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

the class MetadataSyncPostProcessor method handleSyncNotificationsAndAbortStatus.

public boolean handleSyncNotificationsAndAbortStatus(MetadataSyncSummary metadataSyncSummary, MetadataRetryContext retryContext, MetadataVersion dataVersion) {
    ImportReport importReport = metadataSyncSummary.getImportReport();
    if (importReport == null) {
        handleImportFailedContext(null, retryContext, dataVersion);
        return true;
    }
    Status syncStatus = importReport.getStatus();
    log.info("Import completed. Import Status: " + syncStatus);
    if (Status.OK.equals(syncStatus) || (Status.WARNING.equals(syncStatus) && VersionType.BEST_EFFORT.equals(dataVersion.getType()))) {
        sendSuccessMailToAdmin(metadataSyncSummary);
        return false;
    }
    if (Status.ERROR.equals(syncStatus)) {
        handleImportFailedContext(metadataSyncSummary, retryContext, dataVersion);
        return true;
    }
    return false;
}
Also used : Status(org.hisp.dhis.feedback.Status) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport)

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