Search in sources :

Example 6 with BadRequestException

use of org.hisp.dhis.webapi.controller.exception.BadRequestException in project dhis2-core by dhis2.

the class DeduplicationControllerMvcTest method shouldThrowUpdatePotentialDuplicateToMergedStatus.

@Test
void shouldThrowUpdatePotentialDuplicateToMergedStatus() throws Exception {
    String uid = "uid";
    PotentialDuplicate potentialDuplicate = new PotentialDuplicate(teiA, teiB);
    when(deduplicationService.getPotentialDuplicateByUid(uid)).thenReturn(potentialDuplicate);
    mockMvc.perform(put(ENDPOINT + "/" + uid).param("status", DeduplicationStatus.MERGED.name()).content(objectMapper.writeValueAsString(potentialDuplicate)).contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)).andExpect(result -> assertTrue(result.getResolvedException() instanceof BadRequestException));
    verify(deduplicationService).getPotentialDuplicateByUid(uid);
}
Also used : PotentialDuplicate(org.hisp.dhis.deduplication.PotentialDuplicate) BadRequestException(org.hisp.dhis.webapi.controller.exception.BadRequestException) Test(org.junit.jupiter.api.Test)

Example 7 with BadRequestException

use of org.hisp.dhis.webapi.controller.exception.BadRequestException in project dhis2-core by dhis2.

the class MetadataVersionController method createSystemVersion.

// Creates version in versioning table, exports the metadata and saves the
// snapshot in datastore
@PreAuthorize("hasRole('ALL') or hasRole('F_METADATA_MANAGE')")
@PostMapping(value = MetadataVersionSchemaDescriptor.API_ENDPOINT + "/create", produces = ContextUtils.CONTENT_TYPE_JSON)
@ResponseBody
public MetadataVersion createSystemVersion(@RequestParam(value = "type") VersionType versionType) throws MetadataVersionException, BadRequestException {
    MetadataVersion versionToReturn = null;
    boolean enabled = isMetadataVersioningEnabled();
    try {
        if (!enabled) {
            throw new BadRequestException("Metadata versioning is not enabled for this instance.");
        }
        synchronized (versionService) {
            versionService.saveVersion(versionType);
            versionToReturn = versionService.getCurrentVersion();
            return versionToReturn;
        }
    } catch (MetadataVersionServiceException ex) {
        throw new MetadataVersionException("Unable to create version in system. " + ex.getMessage());
    }
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) BadRequestException(org.hisp.dhis.webapi.controller.exception.BadRequestException) MetadataVersionException(org.hisp.dhis.webapi.controller.exception.MetadataVersionException) PostMapping(org.springframework.web.bind.annotation.PostMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 8 with BadRequestException

use of org.hisp.dhis.webapi.controller.exception.BadRequestException in project dhis2-core by dhis2.

the class MetadataVersionController method getMetaDataVersionHistory.

// Gets the list of all versions in between the passed version name and
// latest system version
@GetMapping(value = MetadataVersionSchemaDescriptor.API_ENDPOINT + "/history", produces = ContextUtils.CONTENT_TYPE_JSON)
@ResponseBody
public RootNode getMetaDataVersionHistory(@RequestParam(value = "baseline", required = false) String versionName) throws MetadataVersionException, BadRequestException {
    List<MetadataVersion> allVersionsInBetween = new ArrayList<>();
    boolean enabled = isMetadataVersioningEnabled();
    try {
        if (!enabled) {
            throw new BadRequestException("Metadata versioning is not enabled for this instance.");
        }
        Date startDate;
        if (versionName == null || versionName.isEmpty()) {
            MetadataVersion initialVersion = versionService.getInitialVersion();
            if (initialVersion == null) {
                return getMetadataVersionsAsNode(allVersionsInBetween);
            }
            startDate = initialVersion.getCreated();
        } else {
            startDate = versionService.getCreatedDate(versionName);
        }
        if (startDate == null) {
            throw new MetadataVersionException("There is no such metadata version. The latest version is Version " + versionService.getCurrentVersion().getName());
        }
        Date endDate = new Date();
        allVersionsInBetween = versionService.getAllVersionsInBetween(startDate, endDate);
        if (allVersionsInBetween != null) {
            // now remove the baseline version details
            for (Iterator<MetadataVersion> iterator = allVersionsInBetween.iterator(); iterator.hasNext(); ) {
                MetadataVersion m = iterator.next();
                if (m.getName().equals(versionName)) {
                    iterator.remove();
                    break;
                }
            }
            if (!allVersionsInBetween.isEmpty()) {
                return getMetadataVersionsAsNode(allVersionsInBetween);
            }
        }
    } catch (MetadataVersionServiceException ex) {
        throw new MetadataVersionException(ex.getMessage(), ex);
    }
    return null;
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) ArrayList(java.util.ArrayList) BadRequestException(org.hisp.dhis.webapi.controller.exception.BadRequestException) MetadataVersionException(org.hisp.dhis.webapi.controller.exception.MetadataVersionException) Date(java.util.Date) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 9 with BadRequestException

use of org.hisp.dhis.webapi.controller.exception.BadRequestException in project dhis2-core by dhis2.

the class MetadataSyncController method metadataSync.

@PreAuthorize("hasRole('ALL') or hasRole('F_METADATA_MANAGE')")
@GetMapping
public ResponseEntity<? extends WebMessageResponse> metadataSync(HttpServletRequest request, HttpServletResponse response) throws MetadataSyncException, BadRequestException, MetadataImportConflictException, OperationNotAllowedException {
    MetadataSyncParams syncParams;
    MetadataSyncSummary metadataSyncSummary = null;
    synchronized (metadataSyncService) {
        try {
            syncParams = metadataSyncService.getParamsFromMap(contextService.getParameterValuesMap());
        } catch (RemoteServerUnavailableException exception) {
            throw new MetadataSyncException(exception.getMessage(), exception);
        } catch (MetadataSyncServiceException serviceException) {
            throw new BadRequestException("Error in parsing inputParams " + serviceException.getMessage(), serviceException);
        }
        try {
            boolean isSyncRequired = metadataSyncService.isSyncRequired(syncParams);
            if (isSyncRequired) {
                metadataSyncSummary = metadataSyncService.doMetadataSync(syncParams);
                validateSyncSummaryResponse(metadataSyncSummary);
            } else {
                throw new MetadataImportConflictException("Version already exists in system and hence not starting the sync.");
            }
        } catch (MetadataSyncImportException importerException) {
            throw new MetadataSyncException("Runtime exception occurred while doing import: " + importerException.getMessage());
        } catch (MetadataSyncServiceException serviceException) {
            throw new MetadataSyncException("Exception occurred while doing metadata sync: " + serviceException.getMessage());
        } catch (DhisVersionMismatchException versionMismatchException) {
            throw new OperationNotAllowedException("Exception occurred while doing metadata sync: " + versionMismatchException.getMessage());
        }
    }
    return new ResponseEntity<MetadataSyncSummary>(metadataSyncSummary, HttpStatus.OK);
}
Also used : MetadataSyncServiceException(org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncServiceException) MetadataSyncParams(org.hisp.dhis.dxf2.metadata.sync.MetadataSyncParams) ResponseEntity(org.springframework.http.ResponseEntity) RemoteServerUnavailableException(org.hisp.dhis.dxf2.metadata.sync.exception.RemoteServerUnavailableException) BadRequestException(org.hisp.dhis.webapi.controller.exception.BadRequestException) MetadataSyncImportException(org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncImportException) DhisVersionMismatchException(org.hisp.dhis.dxf2.metadata.sync.exception.DhisVersionMismatchException) MetadataSyncSummary(org.hisp.dhis.dxf2.metadata.sync.MetadataSyncSummary) OperationNotAllowedException(org.hisp.dhis.webapi.controller.exception.OperationNotAllowedException) MetadataSyncException(org.hisp.dhis.webapi.controller.exception.MetadataSyncException) MetadataImportConflictException(org.hisp.dhis.webapi.controller.exception.MetadataImportConflictException) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 10 with BadRequestException

use of org.hisp.dhis.webapi.controller.exception.BadRequestException in project dhis2-core by dhis2.

the class AbstractGistReadOnlyController method getObjectPropertyGistAsCsv.

@GetMapping(value = "/{uid}/{property}/gist", produces = "text/csv")
public void getObjectPropertyGistAsCsv(@PathVariable("uid") String uid, @PathVariable("property") String property, HttpServletRequest request, HttpServletResponse response) throws Exception {
    Property objProperty = getSchema().getProperty(property);
    if (objProperty == null) {
        throw new BadRequestException("No such property: " + property);
    }
    gistToCsvResponse(response, createPropertyQuery(uid, property, request, objProperty));
}
Also used : BadRequestException(org.hisp.dhis.webapi.controller.exception.BadRequestException) Property(org.hisp.dhis.schema.Property) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

BadRequestException (org.hisp.dhis.webapi.controller.exception.BadRequestException)13 PotentialDuplicate (org.hisp.dhis.deduplication.PotentialDuplicate)7 Test (org.junit.jupiter.api.Test)7 OperationNotAllowedException (org.hisp.dhis.webapi.controller.exception.OperationNotAllowedException)5 GetMapping (org.springframework.web.bind.annotation.GetMapping)5 PotentialDuplicateConflictException (org.hisp.dhis.deduplication.PotentialDuplicateConflictException)4 MetadataVersionServiceException (org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException)4 ConflictException (org.hisp.dhis.webapi.controller.exception.ConflictException)4 MetadataVersionException (org.hisp.dhis.webapi.controller.exception.MetadataVersionException)4 NotFoundException (org.hisp.dhis.webapi.controller.exception.NotFoundException)4 MetadataVersion (org.hisp.dhis.metadata.version.MetadataVersion)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 MetadataSyncParams (org.hisp.dhis.dxf2.metadata.sync.MetadataSyncParams)1 MetadataSyncSummary (org.hisp.dhis.dxf2.metadata.sync.MetadataSyncSummary)1 DhisVersionMismatchException (org.hisp.dhis.dxf2.metadata.sync.exception.DhisVersionMismatchException)1 MetadataSyncImportException (org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncImportException)1