This commit is contained in:
LORENZO\pacio 2024-10-23 14:35:50 +02:00
parent 0237ecc4f9
commit bfc8b965f4
8 changed files with 408 additions and 0 deletions

34
.classpath Normal file
View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

23
.project Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>livecareclient</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

24
launch4jconf.xml Normal file
View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<launch4jConfig>
<dontWrapJar>false</dontWrapJar>
<headerType>gui</headerType>
<jar>A:\gdrive\_PROGETTI ALTRO\POLO\LIVECARE CLIENT API\pi_livecare_client.jar</jar>
<outfile>A:\gdrive\_PROGETTI ALTRO\POLO\LIVECARE CLIENT API\pi_livecare_client.exe</outfile>
<errTitle></errTitle>
<cmdLine></cmdLine>
<chdir>.</chdir>
<priority>normal</priority>
<downloadUrl></downloadUrl>
<supportUrl></supportUrl>
<stayAlive>false</stayAlive>
<restartOnCrash>false</restartOnCrash>
<manifest></manifest>
<icon></icon>
<jre>
<path>%JAVA_HOME%;%PATH%</path>
<requiresJdk>false</requiresJdk>
<requires64Bit>false</requires64Bit>
<minVersion></minVersion>
<maxVersion></maxVersion>
</jre>
</launch4jConfig>

3
manifest Normal file
View File

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: com.poloinformatico.livecareclient.PILCClienct

51
pom.xml Normal file
View File

@ -0,0 +1,51 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pi</groupId>
<artifactId>livecareclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.poloinformatico.livecareclient.PILCClienct</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>9.2.1.jre8</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,15 @@
package com.poloinformatico.livecareclient;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Db {
public static Connection getConnection() throws SQLException {
String url = "jdbc:sqlserver://localhost:1433;databaseName=AHR_PI;";
String username = "sa";
String password = "p0l01nf.";
return DriverManager.getConnection(url, username, password);
}
}

View File

@ -0,0 +1,166 @@
package com.poloinformatico.livecareclient;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.poloinformatico.livecareclient.objs.Company;
public class PILCClienct {
private static Connection _conn;
private static int _datediff = 0;
private static String _fileout = "pi_livecareclient.log";
private static BufferedWriter _writer;
private static Timestamp _timestamp;
public static void main(String[] args) throws Exception {
try {
_datediff = Integer.parseInt(args[0]) * -1;
} catch (Exception e) {
_datediff = 0;
}
try {
_writer = new BufferedWriter(new FileWriter(_fileout, true));
_conn = Db.getConnection();
_timestamp = new Timestamp(System.currentTimeMillis());
_writer.append(System.lineSeparator() +"______________ "+ _timestamp.toString() + " " + _datediff);
callApi();
} catch (Exception e) {
System.out.println(e.getMessage());
_writer.append(e.getMessage());
} finally {
_writer.flush();
_writer.close();
if (!_conn.isClosed()) {
_conn.close();
}
}
}
public static synchronized void callApi() throws Exception {
String sql = "select ANPARIVA,AN_EMAIL,ANDESCRI,ANCODICE,case when anclacl3 like '%BM%' or anclacl3 like '%BZ%' or "
+ "anclacl3 like '%99%' or anclacl3 like '%CONC%' or anclacl3 like '%VISUR%' or anclacl3 like '%1SL%' or "
+ "anclacl3 like '%2SL%' or anclacl3 like '%BL%' or anclacl3 like '%CAUSA%' or anclacl3 like '%DI%' "
+ "or anclacl3 like '%ESEC%' or anclacl3 like '%LEG%' or anclacl3 like '%PDR%' or anclacl3 like '%BV%' "
+ "then 1 else 0 end as blocco " + "from POLOICONTI where ANTIPCON='C' and GETDATE() <= DATEDIFF(DAY,"
+ _datediff + ",UTDV) or GETDATE() <= DATEDIFF(DAY," + _datediff + ",UTDC)";
PreparedStatement stmt = _conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Company comp;
String id = rs.getString("ANCODICE").trim();
// CALL
HttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("https://api.livecare.net/lsws10/company/?external_id=" + id);
httpget.addHeader("Authorization", "Basic MTcyMjc6ZjVkNjdhYzdkOTM0N2ZkMjZiMDkzNzE3MTBkZTNk");
HttpEntity entity = httpclient.execute(httpget).getEntity();
String response = EntityUtils.toString(entity);
// Parse JSON
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(response);
boolean update = false;
try {
String stato = jsonNode.get("result_msg").asText();
if (stato.equals("Company not found")) {
update = false;
} else {
update = true;
}
} catch (Exception e) {
update = true;
}
if (update) {
comp = mapper.readValue(response, Company.class);
} else {
comp = new Company();
comp.setExternal_id(id);
comp.setContract_type(1);
}
comp.setContract_blocked(rs.getInt("blocco"));
comp.setCompany(rs.getString("ANDESCRI").trim());
comp.setEmail(rs.getString("AN_EMAIL").trim());
comp.setVat(rs.getString("ANPARIVA").trim());
System.out.println(comp.getCompany());
_writer.append(System.lineSeparator() + comp.getCompany()+" "+id);
// UPDATE/INSERT
try {
String json = mapper.writeValueAsString(comp);
StringEntity params = new StringEntity(json);
HttpPost post;
HttpPut put;
HttpResponse resp;
if (update) {
post = new HttpPost("https://api.livecare.net/lsws10/company/");
post.addHeader("Authorization", "Basic MTcyMjc6ZjVkNjdhYzdkOTM0N2ZkMjZiMDkzNzE3MTBkZTNk");
post.addHeader("content-type", "application/json");
post.setEntity(params);
resp = httpclient.execute(post);
} else {
put = new HttpPut("https://api.livecare.net/lsws10/company/");
put.addHeader("Authorization", "Basic MTcyMjc6ZjVkNjdhYzdkOTM0N2ZkMjZiMDkzNzE3MTBkZTNk");
put.addHeader("content-type", "application/json");
put.setEntity(params);
resp = httpclient.execute(put);
}
System.out.println(resp.getStatusLine());
_writer.append(System.lineSeparator() + resp.getStatusLine().toString());
} catch (Exception e) {
System.out.println(e.getMessage());
_writer.append(System.lineSeparator() + e.getMessage());
}
}
// Clean up resources
rs.close();
stmt.close();
}
}

View File

@ -0,0 +1,92 @@
package com.poloinformatico.livecareclient.objs;
public class Company {
public Company() {
super();
}
private String external_id="";
private String company="";
private String email="";
private String vat="";
private String contract_expiration="";
private String contract_code="";
private int contract_type=0;
private int contract_blocked=0;
public String getExternal_id() {
return external_id;
}
public void setExternal_id(String external_id) {
this.external_id = external_id;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getVat() {
return vat;
}
public void setVat(String vat) {
this.vat = vat;
}
public String getContract_expiration() {
return contract_expiration;
}
public void setContract_expiration(String contract_expiration) {
this.contract_expiration = contract_expiration;
}
public String getContract_code() {
return contract_code;
}
public void setContract_code(String contract_code) {
this.contract_code = contract_code;
}
public int getContract_type() {
return contract_type;
}
public void setContract_type(int contract_type) {
this.contract_type = contract_type;
}
public int getContract_blocked() {
return contract_blocked;
}
public void setContract_blocked(int contract_blocked) {
this.contract_blocked = contract_blocked;
}
}