JPA+Hibernate+H2
This commit is contained in:
@@ -2,8 +2,10 @@ package me.bearns.fias;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableJpaRepositories("me.bearns.fias.repository")
|
||||
public class FiasApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package me.bearns.fias.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
@EnableJpaRepositories
|
||||
@EnableTransactionManagement
|
||||
@PropertySource("application.properties")
|
||||
class ApplicationConfig {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
@Bean
|
||||
//@Profile("test")
|
||||
public DataSource dataSource() {
|
||||
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||
/*dataSource.setDriverClassName("org.h2.Driver");
|
||||
dataSource.setUrl("jdbc:h2:mem:db;DB_CLOSE_DELAY=-1");
|
||||
dataSource.setUsername("sa");
|
||||
dataSource.setPassword("sa");*/
|
||||
dataSource.setDriverClassName(env.getProperty("spring.datasource.driverClassName"));
|
||||
dataSource.setUrl(env.getProperty("spring.datasource.url"));
|
||||
dataSource.setUsername(env.getProperty("spring.datasource.username"));
|
||||
dataSource.setPassword(env.getProperty("spring.datasource.password"));
|
||||
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||
|
||||
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
vendorAdapter.setGenerateDdl(true);
|
||||
|
||||
//DefaultPersistenceUnitManager persistenceUnitManager = new DefaultPersistenceUnitManager();
|
||||
//persistenceUnitManager.setDefaultDataSource(dataSource());
|
||||
|
||||
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
|
||||
factory.setJpaVendorAdapter(vendorAdapter);
|
||||
factory.setPackagesToScan(new String[] {"me.bearns.fias.domain", "me.bearns.fias.repository"});
|
||||
//factory.setPersistenceUnitManager(persistenceUnitManager);
|
||||
factory.setDataSource(dataSource());
|
||||
return factory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
|
||||
|
||||
JpaTransactionManager txManager = new JpaTransactionManager();
|
||||
txManager.setEntityManagerFactory(entityManagerFactory);
|
||||
return txManager;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package me.bearns.fias.domain;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "version")
|
||||
public class FiasVersion {
|
||||
@Id
|
||||
@Column(name = "version_id")
|
||||
protected Long versionId;
|
||||
|
||||
protected String textVersion;
|
||||
|
||||
protected String fiasCompleteXmlUrl;
|
||||
|
||||
protected String fiasDeltaXmlUrl;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package me.bearns.fias.repository;
|
||||
|
||||
import me.bearns.fias.domain.FiasVersion;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface FiasVersionRepository extends JpaRepository<FiasVersion, Long> {
|
||||
|
||||
FiasVersion findTopByOrderByVersionIdDesc();
|
||||
|
||||
}
|
||||
@@ -1,85 +1,28 @@
|
||||
package me.bearns.fias.service;
|
||||
|
||||
import me.bearns.fias.domain.FiasVersion;
|
||||
import me.bearns.fias.repository.FiasVersionRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.nalog.fias.ArrayOfDownloadFileInfo;
|
||||
import ru.nalog.fias.DownloadFileInfo;
|
||||
import ru.nalog.fias.IDownloadService;
|
||||
import ru.nalog.fias.IDownloadService_Service;
|
||||
import sun.net.www.protocol.http.HttpURLConnection;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.ws.Binding;
|
||||
import javax.xml.ws.BindingProvider;
|
||||
import java.net.URL;
|
||||
import java.util.Map;
|
||||
|
||||
import static javax.xml.ws.BindingProvider.ENDPOINT_ADDRESS_PROPERTY;
|
||||
import static ru.nalog.fias.IDownloadService_Service.BasicHttpBindingIDownloadService;
|
||||
|
||||
@Component
|
||||
@Service
|
||||
public class FiasUpdater implements Updater {
|
||||
|
||||
@Autowired
|
||||
FiasVersionRepository versions;
|
||||
|
||||
@Autowired
|
||||
OnlineVersion downloadService;
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
|
||||
IDownloadService_Service service = new IDownloadService_Service();
|
||||
long lastVersion = downloadService.getLastVersionId();
|
||||
|
||||
//QName BasicHttpBindingIDownloadService = new QName("https://fias.nalog.ru/WebServices/Public/DownloadService.asmx/", "BasicHttpBinding_IDownloadService");
|
||||
//String wsUrl="http://fias.nalog.ru/WebServices/Public/DownloadService.asmx";
|
||||
|
||||
//service.addPort(BasicHttpBindingIDownloadService, javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING, wsUrl);
|
||||
IDownloadService downloadService = service.getBasicHttpBindingIDownloadService();
|
||||
|
||||
|
||||
//BindingProvider bindingProvider = (BindingProvider) service.getPort(IDownloadService.class);
|
||||
//Map map = bindingProvider.getRequestContext();
|
||||
|
||||
//map.put(ENDPOINT_ADDRESS_PROPERTY, "https://fias.nalog.ru/WebServices/Public/DownloadService.asmx");
|
||||
|
||||
if(downloadService instanceof BindingProvider ) {
|
||||
|
||||
checkRedirect((BindingProvider) downloadService);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//HttpURLConnection.setFollowRedirects(true);
|
||||
//BindingProvider.ENDPOINT_ADDRESS_PROPERTY
|
||||
|
||||
|
||||
ArrayOfDownloadFileInfo info = downloadService.getAllDownloadFileInfo();
|
||||
|
||||
if(info != null){
|
||||
for (DownloadFileInfo fileInfo : info.getDownloadFileInfo()) {
|
||||
System.out.println("id=" + fileInfo.getVersionId());
|
||||
System.out.println("text=" + fileInfo.getTextVersion());
|
||||
System.out.println("full=" + fileInfo.getFiasCompleteXmlUrl());
|
||||
System.out.println("delta" + fileInfo.getFiasDeltaXmlUrl());
|
||||
}
|
||||
|
||||
}
|
||||
//Max value
|
||||
FiasVersion dbVersion = versions.findTopByOrderByVersionIdDesc();
|
||||
|
||||
System.out.println("get Ok");
|
||||
}
|
||||
|
||||
private static void checkRedirect(final BindingProvider bindingProvider) {
|
||||
try {
|
||||
final URL url = new URL((String) bindingProvider.getRequestContext().get(ENDPOINT_ADDRESS_PROPERTY));
|
||||
|
||||
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setInstanceFollowRedirects(true);
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type", "text/html; charset='UTF-8'");
|
||||
connection.setDoOutput(true);
|
||||
|
||||
final int responseCode = connection.getResponseCode();
|
||||
|
||||
if(responseCode >= 300 && responseCode < 400) {
|
||||
final String redirectToUrl = connection.getHeaderField("location");
|
||||
|
||||
bindingProvider.getRequestContext().put(ENDPOINT_ADDRESS_PROPERTY, redirectToUrl);
|
||||
}
|
||||
} catch(final Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package me.bearns.fias.service;
|
||||
|
||||
public interface OnlineVersion {
|
||||
|
||||
long getLastVersionId();
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package me.bearns.fias.service;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.nalog.fias.DownloadFileInfo;
|
||||
import ru.nalog.fias.IDownloadService;
|
||||
import ru.nalog.fias.IDownloadService_Service;
|
||||
import sun.net.www.protocol.http.HttpURLConnection;
|
||||
|
||||
import javax.xml.ws.BindingProvider;
|
||||
import javax.xml.ws.Service;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.Map;
|
||||
|
||||
import static javax.xml.ws.BindingProvider.ENDPOINT_ADDRESS_PROPERTY;
|
||||
|
||||
@Component
|
||||
public class SOAPClient implements OnlineVersion {
|
||||
|
||||
private final static String RESOURCE_WSDL = "/DownloadService.wsdl";
|
||||
|
||||
IDownloadService downloadService;
|
||||
|
||||
public SOAPClient() {
|
||||
|
||||
try (InputStream stream = getClass().getResourceAsStream(RESOURCE_WSDL)) {
|
||||
|
||||
final File tempFile = File.createTempFile(RESOURCE_WSDL, "tmp");
|
||||
|
||||
tempFile.deleteOnExit();
|
||||
|
||||
if(Files.copy(stream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING) > 0) {
|
||||
|
||||
//copy some bytes ok
|
||||
|
||||
//Init soap client
|
||||
IDownloadService_Service service = new IDownloadService_Service(tempFile.toURI().toURL());
|
||||
|
||||
downloadService = service.getBasicHttpBindingIDownloadService();
|
||||
|
||||
setRedirect(downloadService);
|
||||
}
|
||||
|
||||
}catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLastVersionId() {
|
||||
|
||||
DownloadFileInfo fileInfo = downloadService.getLastDownloadFileInfo();
|
||||
|
||||
if(fileInfo != null) return fileInfo.getVersionId();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static void setRedirect(final Object bindingProvider) {
|
||||
try {
|
||||
if(bindingProvider instanceof BindingProvider) {
|
||||
|
||||
Map<String, Object> requestContext = ((BindingProvider) bindingProvider).getRequestContext();
|
||||
|
||||
Object endpoint = requestContext.get(ENDPOINT_ADDRESS_PROPERTY);
|
||||
|
||||
if(endpoint instanceof String) {
|
||||
|
||||
final URL url = new URL((String) endpoint);
|
||||
|
||||
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setInstanceFollowRedirects(true);
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type", "text/html; charset='UTF-8'");
|
||||
connection.setDoOutput(true);
|
||||
|
||||
final int responseCode = connection.getResponseCode();
|
||||
|
||||
if (responseCode >= 300 && responseCode < 400) {
|
||||
final String redirect = connection.getHeaderField("location");
|
||||
|
||||
requestContext.put(ENDPOINT_ADDRESS_PROPERTY, redirect);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch(final Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1 +1,5 @@
|
||||
|
||||
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=password
|
||||
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||
|
||||
Reference in New Issue
Block a user