Commit f171926c authored by dietricf's avatar dietricf
Browse files

SI-154 ImportData komplett raus + Update-Verhalten für Importierte Meldungen (Meta-Id)

parent 6a058e57
......@@ -28,6 +28,7 @@ public final class Constants {
public static final String INTERNAL_SHORT = "I";
public static final String EXTERNAL_SHORT = "E";
public static final String FAILURE_INFO_UUID_NOT_EXISTING = "failure.info.uuid.not.existing";
public static final String STATUS_NOT_EXISTING = "status.not.existing";
private Constants() {
// empty Constructor for the sake of SONAR
......
/*
*******************************************************************************
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
package org.eclipse.openk.gridfailureinformation.controller;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.log4j.Log4j2;
import org.eclipse.openk.gridfailureinformation.service.ImportDataService;
import org.eclipse.openk.gridfailureinformation.viewmodel.ImportDataDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.UUID;
@Log4j2
@RestController
@RequestMapping("/import-data")
public class ImportDataController {
@Autowired
private ImportDataService importDataService;
@GetMapping
@Secured({"ROLE_GRID-FAILURE-ADMIN", "ROLE_GRID-FAILURE-CREATOR", "ROLE_GRID-FAILURE-QUALIFIER", "ROLE_GRID-FAILURE-PUBLISHER"})
@ApiOperation(value = "Anzeigen aller importierten Störungsmeldungen")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Erfolgreich durchgeführt"),
@ApiResponse(code = 404, message = "Importdaten wurden nicht gefunden")})
@ResponseStatus(HttpStatus.OK)
public List<ImportDataDto> getImportData() {
return importDataService.getImportData();
}
@GetMapping("/{uuid}")
@Secured({"ROLE_GRID-FAILURE-ADMIN", "ROLE_GRID-FAILURE-CREATOR", "ROLE_GRID-FAILURE-QUALIFIER", "ROLE_GRID-FAILURE-PUBLISHER"})
@ApiOperation(value = "Anzeigen einer bestimmten importierten Störungsmeldung")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Erfolgreich durchgeführt"),
@ApiResponse(code = 404, message = "Importierte Störungsmeldung wurde nicht gefunden")})
@ResponseStatus(HttpStatus.OK)
public ImportDataDto readImportMessage(
@PathVariable UUID uuid) {
return importDataService.getImportDataByUuid(uuid);
}
}
package org.eclipse.openk.gridfailureinformation.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.log4j.Log4j2;
import org.eclipse.openk.gridfailureinformation.service.JobManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@EnableScheduling
@Component
@Log4j2
public class JobManager {
@Autowired
JobManagerService jobManagerService;
// Job to import external grid failure information messages
@Scheduled(cron="0/3 0/1 * 1/1 * *") // every 3 seconds
private void importOnTrigger() throws JsonProcessingException {
jobManagerService.importExternalFailureInformation();
}
}
package org.eclipse.openk.gridfailureinformation.controller;
import org.eclipse.openk.gridfailureinformation.service.ImportDataService;
import org.eclipse.openk.gridfailureinformation.service.JobManagerService;
import org.eclipse.openk.gridfailureinformation.util.ImportDataValidator;
import org.eclipse.openk.gridfailureinformation.viewmodel.ImportDataDto;
import org.springframework.amqp.core.Message;
......@@ -15,7 +15,7 @@ import java.util.Objects;
public class MessageConsumer {
@Autowired
private ImportDataService importDataService;
private JobManagerService jobManagerService;
@Autowired
private ImportDataValidator importDataValidator;
......@@ -36,19 +36,6 @@ public class MessageConsumer {
importDataDto.setDescription((String) message.getMessageProperties().getHeaders().get("description"));
importDataDto.setSource((String) message.getMessageProperties().getHeaders().get("source"));
if( importDataValidator.readSafeForeignFailureInfo(importDataDto) != null ) {
storeImportData(importDataDto);
}
}
private void storeImportData(ImportDataDto importDataDto) {
ImportDataDto importDataDtoToSearch = importDataService.getImportDataByMetaId(importDataDto.getMetaId());
if (importDataDtoToSearch == null) {
importDataService.insertImportData(importDataDto);
} else {
importDataService.updateImportDataByMetaID(importDataDto);
}
jobManagerService.validateAndImport(importDataDto);
}
}
\ No newline at end of file
......@@ -36,6 +36,8 @@ public interface FailureInformationRepository extends PagingAndSortingRepository
List<TblFailureInformation> findByUuidIn(List<UUID> uuidList);
Optional<TblFailureInformation> findByObjectReferenceExternalSystem(String extRef );
@Query("select fi from TblFailureInformation fi where fi.tblFailureInformationCondensed.uuid = :uuid")
List<TblFailureInformation> findByFailureInformationCondensedUuid(@Param("uuid") UUID uuid);
}
......@@ -119,6 +119,17 @@ public class FailureInformationService {
return listFailureInformationDtos;
}
public FailureInformationDto findByObjectReferenceExternalSystem( String extRef ) {
Optional<TblFailureInformation> tblFailureInformation
= failureInformationRepository.findByObjectReferenceExternalSystem( extRef );
if( tblFailureInformation.isPresent() ) {
return failureInformationMapper.toFailureInformationDto(tblFailureInformation.get());
}
else {
return null;
}
}
@Transactional
public FailureInformationDto insertFailureInfo(FailureInformationDto failureInfoDto, GfiProcessState initialState) {
failureInfoDto.setUuid(null); // force null here
......
/*
*******************************************************************************
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
package org.eclipse.openk.gridfailureinformation.service;
import lombok.extern.log4j.Log4j2;
import org.eclipse.openk.gridfailureinformation.exceptions.NotFoundException;
import org.eclipse.openk.gridfailureinformation.mapper.ImportDataMapper;
import org.eclipse.openk.gridfailureinformation.model.TblImportData;
import org.eclipse.openk.gridfailureinformation.repository.ImportDataRepository;
import org.eclipse.openk.gridfailureinformation.viewmodel.ImportDataDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
@Service
@Log4j2
public class ImportDataService {
@Value("${dataimport.maxrowcount}")
private Long maxRowCount;
@Autowired
private ImportDataRepository importDataRepository;
@Autowired
private ImportDataMapper importDataMapper;
public List<ImportDataDto> getImportData() {
return importDataRepository.findAll().stream()
.map( importDataMapper::toImportDataDto )
.collect(Collectors.toList());
}
public ImportDataDto getImportDataByUuid(UUID uuid) {
TblImportData tblImportData = importDataRepository.findByUuid(uuid)
.orElseThrow(NotFoundException::new);
return importDataMapper.toImportDataDto(tblImportData);
}
public ImportDataDto getImportDataByMetaId(String metaId) {
Optional<TblImportData> tblImportData = importDataRepository.findByMetaId(metaId);
return tblImportData.isPresent() ? importDataMapper.toImportDataDto(tblImportData.get())
: null;
}
public ImportDataDto insertImportData(ImportDataDto importDataDto){
// Check Rowcount
long cntImportData = countImportData();
if (cntImportData > maxRowCount) {
log.warn(cntImportData + " messages are currently waiting and unprocessed! New ImportMessage is dropped!" );
return null;
}
TblImportData tblImportDataToSave = importDataMapper.toTblImportData(importDataDto);
tblImportDataToSave.setUuid(UUID.randomUUID());
return importDataMapper.toImportDataDto(importDataRepository.save(tblImportDataToSave));
}
@Transactional
public ImportDataDto updateImportDataByMetaID(ImportDataDto importDataDto) {
TblImportData tblImportData = importDataRepository.findByMetaId(importDataDto.getMetaId())
.orElseThrow(() -> new NotFoundException("import.data.uuid.not.existing"));
TblImportData tblImportDataToSave = importDataMapper.toTblImportData(importDataDto);
tblImportDataToSave.setId(tblImportData.getId());
tblImportDataToSave.setUuid(tblImportData.getUuid());
TblImportData savedImportData = importDataRepository.save(tblImportDataToSave);
return importDataMapper.toImportDataDto(savedImportData);
}
public Long countImportData() {
return importDataRepository.countImportData();
}
@Transactional
public void deleteImportData(UUID importDataUuid) {
TblImportData tblImportData = importDataRepository.findByUuid(importDataUuid)
.orElseThrow(() -> new NotFoundException("import.data.uuid.not.existing"));
importDataRepository.delete(tblImportData);
}
@Transactional
public void deleteAll() {
importDataRepository.deleteAll();
}
}
......@@ -21,9 +21,6 @@ import java.util.UUID;
@Service
public class JobManagerService {
@Autowired
ImportDataService importDataService;
@Autowired
FailureInformationService failureInformationService;
......@@ -33,47 +30,72 @@ public class JobManagerService {
@Autowired
RadiusService radiusService;
@Autowired
StatusService statusService;
@Autowired
FailureInformationMapper failureInformationMapper;
@Autowired
ImportDataValidator importDataValidator;
public void importExternalFailureInformation () {
importDataService.getImportData().forEach( this::validateAndImport );
}
private void validateAndImport(ImportDataDto importDataDto) {
public boolean validateAndImport(ImportDataDto importDataDto) {
ForeignFailureDataDto foreignFailureDataDto = importDataValidator.readSafeForeignFailureInfo(importDataDto);
if( foreignFailureDataDto != null ) {
doImport(importDataDto, foreignFailureDataDto);
doImport(importDataDto, foreignFailureDataDto);
return true;
}
else {
return false;
}
importDataService.deleteImportData(importDataDto.getUuid());
}
private void doImport(ImportDataDto importDataDto, ForeignFailureDataDto foreignFailureDataDto) {
FailureInformationDto failureInformationDto = setFromForeignDto(importDataDto, foreignFailureDataDto);
FailureInformationDto existingDto = failureInformationService.findByObjectReferenceExternalSystem(importDataDto.getAssembledRefId());
FailureInformationDto failureInformationDto;
if( existingDto == null ) {
failureInformationDto = setNewFromForeignDto(importDataDto, foreignFailureDataDto);
GfiProcessState gfiProcessState;
if (foreignFailureDataDto.isPlanned()) {
gfiProcessState = GfiProcessState.PLANNED;
} else {
gfiProcessState = GfiProcessState.NEW;
GfiProcessState gfiProcessState;
if (foreignFailureDataDto.isPlanned()) {
gfiProcessState = GfiProcessState.PLANNED;
} else {
gfiProcessState = GfiProcessState.NEW;
}
failureInformationService.insertFailureInfo(failureInformationDto, gfiProcessState);
log.info("External failure information [" + importDataDto.getMetaId() + "] from " + importDataDto.getSource() + " imported (inserted).");
}
else {
setFromForeignDto(importDataDto, foreignFailureDataDto, existingDto);
if( existingDto.getStatusInternId().equals(
statusService.getStatusFromId(GfiProcessState.QUALIFIED.getStatusValue()).getUuid()
)) {
existingDto.setStatusInternId(
statusService.getStatusFromId(GfiProcessState.UPDATED.getStatusValue()).getUuid()
);
}
failureInformationService.updateFailureInfo(existingDto);
log.info("External failure information [" + importDataDto.getMetaId() + "] from " + importDataDto.getSource() + " imported (updated).");
failureInformationService.insertFailureInfo(failureInformationDto, gfiProcessState );
log.info("External failure information [" + importDataDto.getMetaId() + "] from " + importDataDto.getSource() + " imported.");
}
}
private FailureInformationDto setFromForeignDto(ImportDataDto importDataDto, ForeignFailureDataDto foreignFailureDataDto) {
private FailureInformationDto setNewFromForeignDto(ImportDataDto importDataDto, ForeignFailureDataDto foreignFailureDataDto) {
FailureInformationDto failureInformationDto = failureInformationMapper.mapForeignFiDtoToGfiDto(foreignFailureDataDto);
setFromForeignDto(importDataDto, foreignFailureDataDto, failureInformationDto);
failureInformationDto.setCreateUser(importDataDto.getSource());
return failureInformationDto;
}
private void setFromForeignDto(ImportDataDto importDataDto, ForeignFailureDataDto foreignFailureDataDto, FailureInformationDto failureInformationDto) {
failureInformationDto.setBranchId(branchService.findByName(foreignFailureDataDto.getBranch()).getUuid());
failureInformationDto.setBranch(foreignFailureDataDto.getBranch());
......@@ -82,8 +104,7 @@ public class JobManagerService {
}
failureInformationDto.setRadius(foreignFailureDataDto.getRadiusInMeters());
failureInformationDto.setDescription(importDataDto.getDescription());
failureInformationDto.setCreateUser(importDataDto.getSource());
return failureInformationDto;
failureInformationDto.setObjectReferenceExternalSystem(importDataDto.getAssembledRefId());
}
private void importRadius(long foreignRadius, FailureInformationDto failureInformationDto) {
......
......@@ -15,6 +15,8 @@
package org.eclipse.openk.gridfailureinformation.service;
import org.eclipse.openk.gridfailureinformation.constants.Constants;
import org.eclipse.openk.gridfailureinformation.exceptions.NotFoundException;
import org.eclipse.openk.gridfailureinformation.mapper.StatusMapper;
import org.eclipse.openk.gridfailureinformation.repository.StatusRepository;
import org.eclipse.openk.gridfailureinformation.viewmodel.StatusDto;
......@@ -34,7 +36,11 @@ public class StatusService {
@Autowired
private StatusMapper statusMapper;
public StatusDto getStatusFromId(long id) {
return statusMapper.toStatusDto(
statusRepository.findById(id)
.orElseThrow(()->new NotFoundException(Constants.STATUS_NOT_EXISTING + "->"+id)));
}
public List<StatusDto> getStatus() {
return statusRepository.findAll().stream()
......
......@@ -26,6 +26,15 @@ import java.util.UUID;
@Data
public class ImportDataDto implements Serializable {
public String getAssembledRefId()
{
return getAssembledExtRefId(this.source, this.metaId);
}
public static String getAssembledExtRefId(String source, String metaId) {
return source + "@@@" + metaId;
}
@JsonProperty("id")
private UUID uuid;
......@@ -45,6 +54,8 @@ public class ImportDataDto implements Serializable {
@NotNull
private @Valid String messageContent;
}
......@@ -65,9 +65,6 @@ email:
sender: sender@olaf.senderson
smtpHost: entdockergss
port: 1025
dataimport:
maxrowcount: 1000
---
spring:
......
......@@ -63,8 +63,6 @@ email:
smtpHost: entdockergss
port: 1025
dataimport:
maxrowcount: 1000
---
spring:
......
......@@ -146,9 +146,6 @@ public class TestConfiguration {
return new ExpectedReasonService();
}
@Bean
public ImportDataService myImportDataService() {return new ImportDataService(); }
@Bean
public AddressService myAddressService() { return new AddressService(); }
......
/*
*******************************************************************************
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
package org.eclipse.openk.gridfailureinformation.controller;
import org.eclipse.openk.gridfailureinformation.GridFailureInformationApplication;
import org.eclipse.openk.gridfailureinformation.service.ImportDataService;
import org.eclipse.openk.gridfailureinformation.support.MockDataHelper;
import org.eclipse.openk.gridfailureinformation.viewmodel.ImportDataDto;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import java.util.List;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest(classes = GridFailureInformationApplication.class)
@AutoConfigureMockMvc
@ActiveProfiles("test")
public class ImportDataControllerTest {
@MockBean
private ImportDataService importDataService;
@Autowired
private MockMvc mockMvc;
@Test
public void shouldReturnDataImports() throws Exception {
List<ImportDataDto> importDataDtoList = MockDataHelper.mockImportDataDtoList();
when(importDataService.getImportData()).thenReturn(importDataDtoList);
mockMvc.perform(get("/import-data"))
.andExpect(status().is2xxSuccessful())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
}
}
\ No newline at end of file
/*
*******************************************************************************
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
package org.eclipse.openk.gridfailureinformation.service;
import org.eclipse.openk.gridfailureinformation.config.TestConfiguration;
import org.eclipse.openk.gridfailureinformation.exceptions.NotFoundException;
import org.eclipse.openk.gridfailureinformation.model.TblImportData;
import org.eclipse.openk.gridfailureinformation.repository.ImportDataRepository;
import org.eclipse.openk.gridfailureinformation.support.MockDataHelper;
import org.eclipse.openk.gridfailureinformation.viewmodel.ImportDataDto;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;