Commit 11e677a9 authored by dietricf's avatar dietricf
Browse files

SI-379-Import Adressen Alles asynchron

parent 3bc76720
......@@ -4,7 +4,7 @@ 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.importadresses.service.AddressImportService;
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;
......@@ -18,7 +18,7 @@ import org.springframework.web.bind.annotation.RestController;
public class AdressImportController {
@Autowired
private AddressImportService addressImportService;
private JobManager jobManager;
@PostMapping
@ApiOperation(value = "Import von Adressen und Stromstationen")
......@@ -29,6 +29,6 @@ public class AdressImportController {
@RequestMapping("/import")
public void startImportAddresses(
@RequestParam( value="cleanup", required = false, defaultValue = "true") boolean cleanup ) {
addressImportService.importAddresses(cleanup);
jobManager.triggerStartImport();
}
}
\ No newline at end of file
......@@ -4,10 +4,16 @@ import lombok.extern.log4j.Log4j2;
import org.eclipse.openk.gridfailureinformation.importadresses.service.AddressImportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.xml.ws.http.HTTPException;
import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;
@EnableScheduling
@Component
@Log4j2
......@@ -19,17 +25,41 @@ public class JobManager {
@Autowired
AddressImportService addressImportService;
// job for impoerting address and station data
// @Scheduled(initialDelay = 1000, fixedRate = 900000000)
// public void importAddressdata() {
// boolean cleanUp = true;
// addressImportService.importAddresses(true);
// }
//
// }
private Boolean startImport = FALSE;
public void triggerStartImport() {
startImport = TRUE;
}
@Scheduled(cron="0/3 0/1 * 1/1 * *") // every 3 seconds
private void importOnTrigger() {
boolean go = false;
synchronized (startImport) {
if( startImport == TRUE ) {
go = true;
startImport = FALSE;
}
}
if( go ) {
importAddressdata();
}
}
@Scheduled(cron= "${adressimport.cron}")
public void importAddressdata() {
private void importAddressdata() {
try {
addressImportService.importAddresses(cleanUp);
}
catch( HTTPException e ) {
if( e.getStatusCode() != HttpStatus.PROCESSING.value() ) {
throw( e );
}
else {
log.info("Jobmanager triggered import, but import already in work");
}
}
}
}
......@@ -11,8 +11,10 @@ import org.eclipse.openk.gridfailureinformation.importadresses.viewmodel.Address
import org.eclipse.openk.gridfailureinformation.importadresses.viewmodel.StationDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.xml.ws.http.HTTPException;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
......@@ -42,7 +44,19 @@ public class AddressImportService {
@Value("${adressimport.file.power-stations}")
public String filePowerStations;
private Boolean importInWork=Boolean.FALSE;
@Async
public void importAddresses(boolean cleanUp) {
// Don't import twice
synchronized (importInWork) {
if(importInWork == Boolean.TRUE) {
throw new HTTPException(HttpStatus.PROCESSING.value());
}
else {
importInWork = Boolean.TRUE;
}
}
if (cleanUp) {
log.info("CleanUp START");
......@@ -63,6 +77,10 @@ public class AddressImportService {
log.info("Import Power Stations START");
importPowerStationFile(cleanUp);
log.info("Stationsimport ENDE");
synchronized (importInWork) {
importInWork = Boolean.FALSE;
}
}
......@@ -81,46 +99,39 @@ public class AddressImportService {
.build();
String[]line;
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]));
addressDto.setPostcode(line[3]);
addressDto.setCommunity(line[4]);
addressDto.setDistrict(line[5]);
addressDto.setStreet(line[6]);
addressDto.setHousenumber(line[7]);
List<BigDecimal> decimals = converter.convertUTMToDeg(addressDto.getSdox1().doubleValue(), addressDto.getSdoy1().doubleValue());
addressDto.setLongitude(decimals.get(0));
addressDto.setLatitude(decimals.get(1));
AddressDto savedAddressDto;
if (isTableClean) {
savedAddressDto = addressService.insertAddress(addressDto);
} else {
savedAddressDto = addressService.updateOrInsertAddressByG3efid(addressDto);
}
storeAddress(isTableClean, line);
/* System.out.println("Adresse [sdo_x1= " + line[0]
+ ", sdo_y1= " + line[1]
+ ", g3e_fid=" + line[2]
+ ", long= " + addressDto.getLongitude()
+ ", lat= " + addressDto.getLatitude()
+ ", plz=" + line[3]
+ ", ort=" + line[4]
+ ", ortsteil=" + line[5]
+ ", strasse=" + line[6]
+ ", hausnummer=" + line[7]
+ "]");*/
}
} catch (IOException | CsvValidationException e) {
e.printStackTrace();
}
}
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]));
addressDto.setPostcode(line[3]);
addressDto.setCommunity(line[4]);
addressDto.setDistrict(line[5]);
addressDto.setStreet(line[6]);
addressDto.setHousenumber(line[7]);
List<BigDecimal> decimals = converter.convertUTMToDeg(addressDto.getSdox1().doubleValue(), addressDto.getSdoy1().doubleValue());
addressDto.setLongitude(decimals.get(0));
addressDto.setLatitude(decimals.get(1));
AddressDto savedAddressDto;
if (isTableClean) {
savedAddressDto = addressService.insertAddress(addressDto);
} else {
savedAddressDto = addressService.updateOrInsertAddressByG3efid(addressDto);
}
}
public void importPowerConnectionsAddressFile() {
......
......@@ -87,6 +87,6 @@ public class AddressService {
}
public void deleteAllAddresses() {
addressRepository.deleteAll();
addressRepository.deleteAllInBatch();
}
}
......@@ -72,7 +72,7 @@ public class StationService {
}
@Transactional
public void deleteAllStations() {
stationRepository.deleteAll();
stationRepository.deleteAllInBatch();
}
}
......@@ -22,7 +22,7 @@ utm:
adressimport:
cleanup: true
cron: 0 27 9 3 4 FRI
cron: 0 0 0 1 * ?
file:
addresses: C:/temp/import_files/adressen.csv
power-connections: C:/temp/import_files/strom_hausanschluss.csv
......
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