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