Commit f575c3dd authored by dietricf's avatar dietricf
Browse files

SI-379-Import Adressen cleanup

parent 11e677a9
...@@ -8,7 +8,6 @@ import org.eclipse.openk.gridfailureinformation.importadresses.jobs.JobManager; ...@@ -8,7 +8,6 @@ import org.eclipse.openk.gridfailureinformation.importadresses.jobs.JobManager;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
...@@ -20,15 +19,14 @@ public class AdressImportController { ...@@ -20,15 +19,14 @@ public class AdressImportController {
@Autowired @Autowired
private JobManager jobManager; private JobManager jobManager;
@PostMapping
@ApiOperation(value = "Import von Adressen und Stromstationen") @ApiOperation(value = "Import von Adressen und Stromstationen")
@ApiResponses(value = { @ApiResponses(value = {
@ApiResponse(code = 201, message = "Adressen erfolgreich importiert"), @ApiResponse(code = 201, message = "Adressen erfolgreich importiert"),
@ApiResponse(code = 500, message = "Konnte nicht durchgeführt werden") @ApiResponse(code = 500, message = "Konnte nicht durchgeführt werden")
}) })
@RequestMapping("/import") @PostMapping("/import")
public void startImportAddresses( public void startImportAddresses() {
@RequestParam( value="cleanup", required = false, defaultValue = "true") boolean cleanup ) {
jobManager.triggerStartImport(); jobManager.triggerStartImport();
} }
} }
\ No newline at end of file
...@@ -24,19 +24,22 @@ public class JobManager { ...@@ -24,19 +24,22 @@ public class JobManager {
@Autowired @Autowired
AddressImportService addressImportService; AddressImportService addressImportService;
private final Object lock = new Object();
private Boolean startImport = FALSE; private Boolean startImport = FALSE;
public void triggerStartImport() { public void triggerStartImport() {
startImport = TRUE; synchronized (lock) {
startImport = TRUE;
}
} }
@Scheduled(cron="0/3 0/1 * 1/1 * *") // every 3 seconds @Scheduled(cron="0/3 0/1 * 1/1 * *") // every 3 seconds
private void importOnTrigger() { private void importOnTrigger() {
boolean go = false; boolean go = false;
synchronized (startImport) { synchronized (lock) {
if( startImport == TRUE ) { if( startImport.equals(TRUE) ) {
go = true; go = true;
startImport = FALSE; startImport = FALSE;
} }
......
...@@ -44,13 +44,14 @@ public class AddressImportService { ...@@ -44,13 +44,14 @@ public class AddressImportService {
@Value("${adressimport.file.power-stations}") @Value("${adressimport.file.power-stations}")
public String filePowerStations; public String filePowerStations;
private final Object lock = new Object();
private Boolean importInWork=Boolean.FALSE; private Boolean importInWork=Boolean.FALSE;
@Async @Async
public void importAddresses(boolean cleanUp) { public void importAddresses(boolean cleanUp) {
// Don't import twice // Don't import twice
synchronized (importInWork) { synchronized (lock) {
if(importInWork == Boolean.TRUE) { if(importInWork.equals(Boolean.TRUE)) {
throw new HTTPException(HttpStatus.PROCESSING.value()); throw new HTTPException(HttpStatus.PROCESSING.value());
} }
else { else {
...@@ -78,7 +79,7 @@ public class AddressImportService { ...@@ -78,7 +79,7 @@ public class AddressImportService {
importPowerStationFile(cleanUp); importPowerStationFile(cleanUp);
log.info("Stationsimport ENDE"); log.info("Stationsimport ENDE");
synchronized (importInWork) { synchronized (lock) {
importInWork = Boolean.FALSE; importInWork = Boolean.FALSE;
} }
} }
...@@ -103,14 +104,13 @@ public class AddressImportService { ...@@ -103,14 +104,13 @@ public class AddressImportService {
} }
} catch (IOException | CsvValidationException e) { } catch (IOException | CsvValidationException e) {
e.printStackTrace(); log.error(e);
} }
} }
private void storeAddress(boolean isTableClean, String[] line) { private void storeAddress(boolean isTableClean, String[] line) {
AddressDto addressDto = new AddressDto(); AddressDto addressDto = new AddressDto();
// !!! Parsing BigDecimal anpassen, locale // !!! Parsing BigDecimal anpassen, locale
BigDecimal x1 = new BigDecimal(line[0].replace(",", "."));
addressDto.setSdox1(new BigDecimal(line[0].replace(",", "."))); addressDto.setSdox1(new BigDecimal(line[0].replace(",", ".")));
addressDto.setSdoy1(new BigDecimal(line[1].replace(",", "."))); addressDto.setSdoy1(new BigDecimal(line[1].replace(",", ".")));
addressDto.setG3efid(Long.parseLong(line[2])); addressDto.setG3efid(Long.parseLong(line[2]));
...@@ -124,11 +124,10 @@ public class AddressImportService { ...@@ -124,11 +124,10 @@ public class AddressImportService {
addressDto.setLongitude(decimals.get(0)); addressDto.setLongitude(decimals.get(0));
addressDto.setLatitude(decimals.get(1)); addressDto.setLatitude(decimals.get(1));
AddressDto savedAddressDto;
if (isTableClean) { if (isTableClean) {
savedAddressDto = addressService.insertAddress(addressDto); addressService.insertAddress(addressDto);
} else { } else {
savedAddressDto = addressService.updateOrInsertAddressByG3efid(addressDto); addressService.updateOrInsertAddressByG3efid(addressDto);
} }
} }
...@@ -150,7 +149,6 @@ public class AddressImportService { ...@@ -150,7 +149,6 @@ public class AddressImportService {
while ((line = reader.readNext()) != null) { while ((line = reader.readNext()) != null) {
AddressDto addressDto = new AddressDto(); AddressDto addressDto = new AddressDto();
// !!! Parsing BigDecimal anpassen, locale??? // !!! Parsing BigDecimal anpassen, locale???
BigDecimal x1 = new BigDecimal(line[0].replace(",", "."));
addressDto.setSdox1(new BigDecimal(line[0].replace(",", "."))); addressDto.setSdox1(new BigDecimal(line[0].replace(",", ".")));
addressDto.setSdoy1(new BigDecimal(line[1].replace(",", "."))); addressDto.setSdoy1(new BigDecimal(line[1].replace(",", ".")));
addressDto.setG3efid(Long.parseLong(line[2])); addressDto.setG3efid(Long.parseLong(line[2]));
...@@ -166,10 +164,10 @@ public class AddressImportService { ...@@ -166,10 +164,10 @@ public class AddressImportService {
addressDto.setLongitude(decimals.get(0)); addressDto.setLongitude(decimals.get(0));
addressDto.setLatitude(decimals.get(1)); addressDto.setLatitude(decimals.get(1));
AddressDto savedAddressDto = addressService.updateOrInsertAddressByG3efid(addressDto); addressService.updateOrInsertAddressByG3efid(addressDto);
} }
} catch (IOException | CsvValidationException e) { } catch (IOException | CsvValidationException e) {
e.printStackTrace(); log.error(e);
} }
} }
...@@ -191,7 +189,6 @@ public class AddressImportService { ...@@ -191,7 +189,6 @@ public class AddressImportService {
while ((line = reader.readNext()) != null) { while ((line = reader.readNext()) != null) {
AddressDto addressDto = new AddressDto(); AddressDto addressDto = new AddressDto();
// !!! Parsing BigDecimal anpassen, locale??? // !!! Parsing BigDecimal anpassen, locale???
BigDecimal x1 = new BigDecimal(line[0].replace(",", "."));
addressDto.setSdox1(new BigDecimal(line[0].replace(",", "."))); addressDto.setSdox1(new BigDecimal(line[0].replace(",", ".")));
addressDto.setSdoy1(new BigDecimal(line[1].replace(",", "."))); addressDto.setSdoy1(new BigDecimal(line[1].replace(",", ".")));
addressDto.setG3efid(Long.parseLong(line[2])); addressDto.setG3efid(Long.parseLong(line[2]));
...@@ -207,10 +204,10 @@ public class AddressImportService { ...@@ -207,10 +204,10 @@ public class AddressImportService {
addressDto.setLongitude(decimals.get(0)); addressDto.setLongitude(decimals.get(0));
addressDto.setLatitude(decimals.get(1)); addressDto.setLatitude(decimals.get(1));
AddressDto savedAddressDto = addressService.updateOrInsertAddressByG3efid(addressDto); addressService.updateOrInsertAddressByG3efid(addressDto);
} }
} catch (IOException | CsvValidationException e) { } catch (IOException | CsvValidationException e) {
e.printStackTrace(); log.error(e);
} }
} }
...@@ -231,7 +228,6 @@ public class AddressImportService { ...@@ -231,7 +228,6 @@ public class AddressImportService {
while ((line = reader.readNext()) != null) { while ((line = reader.readNext()) != null) {
AddressDto addressDto = new AddressDto(); AddressDto addressDto = new AddressDto();
// !!! Parsing BigDecimal anpassen, locale??? // !!! Parsing BigDecimal anpassen, locale???
BigDecimal x1 = new BigDecimal(line[0].replace(",", "."));
addressDto.setSdox1(new BigDecimal(line[0].replace(",", "."))); addressDto.setSdox1(new BigDecimal(line[0].replace(",", ".")));
addressDto.setSdoy1(new BigDecimal(line[1].replace(",", "."))); addressDto.setSdoy1(new BigDecimal(line[1].replace(",", ".")));
addressDto.setG3efid(Long.parseLong(line[2])); addressDto.setG3efid(Long.parseLong(line[2]));
...@@ -247,10 +243,10 @@ public class AddressImportService { ...@@ -247,10 +243,10 @@ public class AddressImportService {
addressDto.setLongitude(decimals.get(0)); addressDto.setLongitude(decimals.get(0));
addressDto.setLatitude(decimals.get(1)); addressDto.setLatitude(decimals.get(1));
AddressDto savedAddressDto = addressService.updateOrInsertAddressByG3efid(addressDto); addressService.updateOrInsertAddressByG3efid(addressDto);
} }
} catch (IOException | CsvValidationException e) { } catch (IOException | CsvValidationException e) {
e.printStackTrace(); log.error(e);
} }
} }
...@@ -271,7 +267,6 @@ public class AddressImportService { ...@@ -271,7 +267,6 @@ public class AddressImportService {
while ((line = reader.readNext()) != null) { while ((line = reader.readNext()) != null) {
StationDto stationDto = new StationDto(); StationDto stationDto = new StationDto();
// !!! Parsing BigDecimal anpassen, locale // !!! Parsing BigDecimal anpassen, locale
BigDecimal x1 = new BigDecimal(line[0].replace(",", "."));
stationDto.setSdox1(new BigDecimal(line[0].replace(",", "."))); stationDto.setSdox1(new BigDecimal(line[0].replace(",", ".")));
stationDto.setSdoy1(new BigDecimal(line[1].replace(",", "."))); stationDto.setSdoy1(new BigDecimal(line[1].replace(",", ".")));
stationDto.setG3efid(Long.parseLong(line[2])); stationDto.setG3efid(Long.parseLong(line[2]));
...@@ -282,25 +277,15 @@ public class AddressImportService { ...@@ -282,25 +277,15 @@ public class AddressImportService {
stationDto.setLongitude(decimals.get(0)); stationDto.setLongitude(decimals.get(0));
stationDto.setLatitude(decimals.get(1)); stationDto.setLatitude(decimals.get(1));
StationDto savedStationDto;
if (isTableClean) { if (isTableClean) {
savedStationDto = stationService.insertStation(stationDto); stationService.insertStation(stationDto);
} else { } else {
savedStationDto = stationService.updateOrInsertStationByG3efid(stationDto); stationService.updateOrInsertStationByG3efid(stationDto);
} }
/* System.out.println("Adresse [sdo_x1= " + line[0]
+ ", sdo_y1= " + line[1]
+ ", g3e_fid=" + line[2]
+ ", plz=" + line[3]
+ ", ort=" + line[4]
+ ", ortsteil=" + line[5]
+ ", strasse=" + line[6]
+ ", hausnummer=" + line[7]
+ "]");*/
} }
} catch (IOException | CsvValidationException e) { } catch (IOException | CsvValidationException e) {
e.printStackTrace(); log.error(e);
} }
} }
......
...@@ -50,7 +50,7 @@ public class AddressServiceTest { ...@@ -50,7 +50,7 @@ public class AddressServiceTest {
@Test @Test
public void shouldDeleteAllAdressesProperly() { public void shouldDeleteAllAdressesProperly() {
addressService.deleteAllAddresses(); addressService.deleteAllAddresses();
verify(addressRepository, times(1)).deleteAll(); verify(addressRepository, times(1)).deleteAllInBatch();
} }
@Test @Test
......
...@@ -51,7 +51,7 @@ public class StationServiceTest { ...@@ -51,7 +51,7 @@ public class StationServiceTest {
@Test @Test
public void shouldDeleteAllStationsProperly() { public void shouldDeleteAllStationsProperly() {
stationService.deleteAllStations(); stationService.deleteAllStations();
verify(stationRepository, times(1)).deleteAll(); verify(stationRepository, times(1)).deleteAllInBatch();
} }
@Test @Test
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment