diff --git a/.gitignore b/.gitignore
index 3df9a13..a4b4a2d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,88 +1,39 @@
-# ---> Java
-# Compiled class file
-*.class
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
-# Log file
-*.log
+### IntelliJ IDEA ###
+.idea/modules.xml
+.idea/jarRepositories.xml
+.idea/compiler.xml
+.idea/libraries/
+*.iws
+*.iml
+*.ipr
-# BlueJ files
-*.ctxt
-
-# Mobile Tools for Java (J2ME)
-.mtj.tmp/
-
-# Package Files #
-*.jar
-*.war
-*.nar
-*.ear
-*.zip
-*.tar.gz
-*.rar
-
-# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
-hs_err_pid*
-replay_pid*
-
-# ---> Eclipse
-.metadata
-bin/
-tmp/
-*.tmp
-*.bak
-*.swp
-*~.nib
-local.properties
-.settings/
-.loadpath
-.recommenders
-
-# External tool builders
-.externalToolBuilders/
-
-# Locally stored "Eclipse launch configurations"
-*.launch
-
-# PyDev specific (Python IDE for Eclipse)
-*.pydevproject
-
-# CDT-specific (C/C++ Development Tooling)
-.cproject
-
-# CDT- autotools
-.autotools
-
-# Java annotation processor (APT)
+### Eclipse ###
+.apt_generated
+.classpath
.factorypath
-
-# PDT-specific (PHP Development Tools)
-.buildpath
-
-# sbteclipse plugin
-.target
-
-# Tern plugin
-.tern-project
-
-# TeXlipse plugin
-.texlipse
-
-# STS (Spring Tool Suite)
+.project
+.settings
.springBeans
+.sts4-cache
-# Code Recommenders
-.recommenders/
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
-# Annotation Processing
-.apt_generated/
-.apt_generated_test/
-
-# Scala IDE specific (Scala & Java development for Eclipse)
-.cache-main
-.scala_dependencies
-.worksheet
-
-# Uncomment this line if you wish to ignore the project description file.
-# Typically, this file would be tracked if it contains build/dependency configurations:
-#.project
+### VS Code ###
+.vscode/
+### Mac OS ###
+.DS_Store
+/bin/
diff --git a/pi_mailupclient.log b/pi_mailupclient.log
new file mode 100644
index 0000000..dda6b6e
--- /dev/null
+++ b/pi_mailupclient.log
@@ -0,0 +1,3 @@
+
+===============>2023-09-15 17:14:02.826 -365
+POST RESULT: HTTP/1.1 200 OK 1100
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..1f6cdd8
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,64 @@
+
+
+ 4.0.0
+
+ com.poloinformatico
+ MailUpClient
+ 1.0-SNAPSHOT
+
+
+
+
+ org.json
+ json
+ 20230618
+
+
+
+
+
+ org.apache.httpcomponents
+ httpclient
+ 4.5.14
+
+
+
+
+ com.fasterxml.jackson.core
+ jackson-databind
+ 2.15.2
+
+
+
+
+ com.fasterxml.jackson.core
+ jackson-annotations
+ 2.15.2
+
+
+
+
+ com.microsoft.sqlserver
+ mssql-jdbc
+ 9.4.1.jre8
+
+
+
+
+
+
+
+
+
+
+
+ 8
+ 8
+ UTF-8
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/java/com/poloinformatico/MailupClient/ApiClient.java b/src/main/java/com/poloinformatico/MailupClient/ApiClient.java
new file mode 100644
index 0000000..bcb2906
--- /dev/null
+++ b/src/main/java/com/poloinformatico/MailupClient/ApiClient.java
@@ -0,0 +1,192 @@
+package com.poloinformatico.MailupClient;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.poloinformatico.MailupClient.obj.Recipient;
+import com.poloinformatico.MailupClient.obj.RecipientField;
+import com.poloinformatico.MailupClient.obj.RecipientsResponse;
+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.entity.StringEntity;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+
+import java.io.BufferedWriter;
+import java.io.FileWriter;
+import java.sql.*;
+import java.util.ArrayList;
+import java.util.Objects;
+
+
+public class ApiClient {
+
+ private static String _urlPostToken = "https://services.mailup.com/Authorization/OAuth/Token/";
+
+ private static String _token, _idGroup, _urlRecipients;
+
+ private static int _datediff = 0;
+
+ private static HttpClient _httpclient;
+
+ private static final String _fileout = "pi_mailupclient.log";
+
+ public static String start(String[] args) throws Exception {
+
+ String res = null;
+
+ try {
+ _datediff = Integer.parseInt(args[0]) * -1;
+ } catch (Exception e) {
+ _datediff = 0;
+ }
+
+
+ try (BufferedWriter writer = new BufferedWriter(new FileWriter(_fileout, true));) {
+
+ writer.append(System.lineSeparator() + "===============>" + new Timestamp(System.currentTimeMillis()) + " " + _datediff);
+
+ _httpclient = HttpClients.createDefault();
+
+ _idGroup = String.valueOf(1114);
+
+ _urlRecipients = "https://services.mailup.com/API/v1.1/Rest/ConsoleService.svc/Console/Group/" + _idGroup + "/Recipients";
+
+ ArrayList recs = readDatabase();
+
+ if (recs.size() > 0) {
+
+ getToken();
+
+ ObjectMapper objectMapper = new ObjectMapper();
+
+ String json = objectMapper.writeValueAsString(recs);
+
+ HttpResponse resp = doPost(_urlRecipients, new StringEntity(json, "UTF-8"));
+
+ String sResult = "POST RESULT: " + resp.getStatusLine() + " " + EntityUtils.toString(resp.getEntity());
+
+ writer.append(System.lineSeparator() + sResult);
+ }
+
+ }
+
+ return res;
+ }
+
+ public static boolean getToken() throws Exception {
+
+ boolean res = false;
+
+ HttpResponse response = null;
+ HttpPost req = new HttpPost(_urlPostToken);
+
+ req.addHeader("Content-Type", "application/x-www-form-urlencoded");
+ req.addHeader("Authorization", "Basic YjkzODE2MjctZjRkYi00MzA2LTljNTYtY2Q0ZjllNDAzMmExOjk3ODMxYTdiLTk2MWQtNDk2MC1iYzkxLTMyODM2NTRlMmRiOA==");
+
+ StringEntity params = new StringEntity("grant_type=password&username=m59820&password=g4st0n3!");
+
+ req.setEntity(params);
+
+ String responseBodyString = EntityUtils.toString(_httpclient.execute(req).getEntity());
+
+ JsonNode jsonNode = new ObjectMapper().readTree(responseBodyString);
+
+ _token = _token == null ? jsonNode.get("access_token").asText() : jsonNode.get("refresh_token").asText();
+
+ return res;
+ }
+
+ //attenzione, il body deve avere encoding UTF-8
+ public static HttpResponse doPost(String uri, StringEntity body) throws Exception {
+ HttpPost req = new HttpPost(uri);
+
+ req.addHeader("Authorization", "Bearer " + _token);
+ req.addHeader("Content-Type", "application/json; charset=utf-8");
+
+ body.setContentType("application/json");
+ //body.setContentEncoding("UTF-8");
+
+ req.setEntity(body);
+
+ HttpResponse resp = _httpclient.execute(req);
+
+ return resp;
+ }
+
+ public static HttpResponse doGet(String uri) throws Exception {
+ HttpGet req = new HttpGet(uri);
+
+ req.addHeader("Authorization", "Bearer " + _token);
+
+ HttpResponse response = _httpclient.execute(req);
+
+ return response;
+ }
+
+ public static String parseResponse(HttpResponse httpResponse) throws Exception {
+ String res = null;
+ HttpEntity entity = httpResponse.getEntity();
+ String response = EntityUtils.toString(entity);
+
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode jsonNode = mapper.readTree(response);
+
+ res = jsonNode.asText();
+
+
+ return res;
+ }
+
+ public static ArrayList readDatabase() throws Exception {
+
+ Recipient rec;
+ ArrayList recs = new ArrayList<>();
+
+ int cnt_err = 0;
+
+ String sql = "SELECT ANPARIVA,AN_EMAIL,ANDESCRI,ANCODICE,ANLOCALI,AN___CAP,ANPROVIN,ANCATCOM,ANCLACL2,";
+ sql += "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 ";
+ sql += " from POLOICONTI ";
+ sql += " where AN_EMAIL is not null and ANDESCRI is not null and ANTIPCON='C' and GETDATE() <= DATEDIFF(DAY,?,UTDV) or GETDATE() <= DATEDIFF(DAY,?,UTDC)";
+
+ try (Connection conn = Db.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql);) {
+
+ stmt.setInt(1, _datediff);
+ stmt.setInt(2, _datediff);
+
+ try (ResultSet rs = stmt.executeQuery();) {
+
+ while (rs.next()) {
+ try {
+ rec = new Recipient();
+ rec.email = rs.getString("AN_EMAIL").trim();
+ rec.name = rs.getString("ANDESCRI").trim();
+
+ rec.fields.add(new RecipientField(3, Objects.toString(rs.getString("ANDESCRI"), "").trim()));
+ rec.fields.add(new RecipientField(4, Objects.toString(rs.getString("ANLOCALI"), "").trim()));
+ rec.fields.add(new RecipientField(5, Objects.toString(rs.getString("ANPROVIN"), "").trim()));
+ rec.fields.add(new RecipientField(6, Objects.toString(rs.getString("AN___CAP"), "").trim()));
+ rec.fields.add(new RecipientField(12, Objects.toString(rs.getString("ANCODICE"), "").trim()));
+ rec.fields.add(new RecipientField(16, Objects.toString(rs.getString("ANCATCOM"), "").trim()));
+ rec.fields.add(new RecipientField(17, Objects.toString(rs.getString("ANCLACL2"), "").trim()));
+ recs.add(rec);
+ } catch (Exception e) {
+ cnt_err++;
+ continue;
+ }
+ }
+ }
+
+ }
+
+ System.out.println("Errori database: " + cnt_err);
+
+
+ return recs;
+ }
+
+
+}
diff --git a/src/main/java/com/poloinformatico/MailupClient/Db.java b/src/main/java/com/poloinformatico/MailupClient/Db.java
new file mode 100644
index 0000000..b88e869
--- /dev/null
+++ b/src/main/java/com/poloinformatico/MailupClient/Db.java
@@ -0,0 +1,15 @@
+package com.poloinformatico.MailupClient;
+
+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);
+ }
+}
diff --git a/src/main/java/com/poloinformatico/MailupClient/Main.java b/src/main/java/com/poloinformatico/MailupClient/Main.java
new file mode 100644
index 0000000..6e1e0c4
--- /dev/null
+++ b/src/main/java/com/poloinformatico/MailupClient/Main.java
@@ -0,0 +1,15 @@
+package com.poloinformatico.MailupClient;
+
+public class Main {
+
+ public static void main(String[] args) {
+
+ try {
+ ApiClient.start(args);
+ } catch (Exception e) {
+ System.out.println("eccezione esterna: " + e.getMessage());
+ }
+ }
+
+
+}
diff --git a/src/main/java/com/poloinformatico/MailupClient/obj/FieldRecipientsResponse.java b/src/main/java/com/poloinformatico/MailupClient/obj/FieldRecipientsResponse.java
new file mode 100644
index 0000000..60304e6
--- /dev/null
+++ b/src/main/java/com/poloinformatico/MailupClient/obj/FieldRecipientsResponse.java
@@ -0,0 +1,10 @@
+package com.poloinformatico.MailupClient.obj;
+import com.fasterxml.jackson.annotation.JsonProperty;
+public class FieldRecipientsResponse {
+ @JsonProperty("Description")
+ public String description;
+ @JsonProperty("Id")
+ public int id;
+ @JsonProperty("Value")
+ public String value;
+}
diff --git a/src/main/java/com/poloinformatico/MailupClient/obj/ItemRecipientsResponse.java b/src/main/java/com/poloinformatico/MailupClient/obj/ItemRecipientsResponse.java
new file mode 100644
index 0000000..9402700
--- /dev/null
+++ b/src/main/java/com/poloinformatico/MailupClient/obj/ItemRecipientsResponse.java
@@ -0,0 +1,18 @@
+package com.poloinformatico.MailupClient.obj;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+import java.util.ArrayList;
+
+public class ItemRecipientsResponse {
+ @JsonProperty("Fields")
+ public ArrayList fieldRecipientsResponses;
+ @JsonProperty("Name")
+ public Object name;
+ public int idRecipient;
+ @JsonProperty("Email")
+ public String email;
+ @JsonProperty("MobileNumber")
+ public String mobileNumber;
+ @JsonProperty("MobilePrefix")
+ public String mobilePrefix;
+}
diff --git a/src/main/java/com/poloinformatico/MailupClient/obj/Recipient.java b/src/main/java/com/poloinformatico/MailupClient/obj/Recipient.java
new file mode 100644
index 0000000..5b0b199
--- /dev/null
+++ b/src/main/java/com/poloinformatico/MailupClient/obj/Recipient.java
@@ -0,0 +1,18 @@
+package com.poloinformatico.MailupClient.obj;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+import java.util.ArrayList;
+
+public class Recipient {
+
+ @JsonProperty("Email")
+ public String email;
+ @JsonProperty("Fields")
+ public ArrayList fields = new ArrayList<>();
+ /*@JsonProperty("MobileNumber")
+ public String mobileNumber;
+ @JsonProperty("MobilePrefix")
+ public String mobilePrefix;*/
+ @JsonProperty("Name")
+ public String name;
+}
diff --git a/src/main/java/com/poloinformatico/MailupClient/obj/RecipientField.java b/src/main/java/com/poloinformatico/MailupClient/obj/RecipientField.java
new file mode 100644
index 0000000..39d9dd4
--- /dev/null
+++ b/src/main/java/com/poloinformatico/MailupClient/obj/RecipientField.java
@@ -0,0 +1,16 @@
+package com.poloinformatico.MailupClient.obj;
+import com.fasterxml.jackson.annotation.JsonProperty;
+public class RecipientField {
+ /*@JsonProperty("Description")
+ public String description;*/
+
+ public RecipientField(int id, String value) {
+ this.id = id;
+ this.value = value;
+ }
+
+ @JsonProperty("Id")
+ public int id;
+ @JsonProperty("Value")
+ public String value;
+}
diff --git a/src/main/java/com/poloinformatico/MailupClient/obj/RecipientsResponse.java b/src/main/java/com/poloinformatico/MailupClient/obj/RecipientsResponse.java
new file mode 100644
index 0000000..a6b3af7
--- /dev/null
+++ b/src/main/java/com/poloinformatico/MailupClient/obj/RecipientsResponse.java
@@ -0,0 +1,19 @@
+package com.poloinformatico.MailupClient.obj;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+import java.util.ArrayList;
+
+public class RecipientsResponse {
+ @JsonProperty("IsPaginated")
+ public boolean isPaginated;
+ @JsonProperty("Items")
+ public ArrayList itemRecipientsResponses;
+ @JsonProperty("PageNumber")
+ public int pageNumber;
+ @JsonProperty("PageSize")
+ public int pageSize;
+ @JsonProperty("Skipped")
+ public int skipped;
+ @JsonProperty("TotalElementsCount")
+ public int totalElementsCount;
+}
diff --git a/src/main/resources/DynamcFields.json b/src/main/resources/DynamcFields.json
new file mode 100644
index 0000000..b3df810
--- /dev/null
+++ b/src/main/resources/DynamcFields.json
@@ -0,0 +1,89 @@
+{
+ "IsPaginated": true,
+ "Items": [
+ {
+ "Description": "compleanno",
+ "Id": 27
+ },
+ {
+ "Description": "idtuttiprodottiacquistati",
+ "Id": 26
+ },
+ {
+ "Description": "totalefatturatoultimi30gg",
+ "Id": 25
+ },
+ {
+ "Description": "totalefatturatoultimi12mesi",
+ "Id": 24
+ },
+ {
+ "Description": "totalefatturato",
+ "Id": 23
+ },
+ {
+ "Description": "idcarrelloabbandonato",
+ "Id": 22
+ },
+ {
+ "Description": "totalecarrelloabbandonato",
+ "Id": 21
+ },
+ {
+ "Description": "datacarrelloabbandonato",
+ "Id": 20
+ },
+ {
+ "Description": "idultimoordinespedito",
+ "Id": 19
+ },
+ {
+ "Description": "dataultimoordinespedito",
+ "Id": 18
+ },
+ {
+ "Description": "idcategorieultimoordine",
+ "Id": 17
+ },
+ {
+ "Description": "idprodottiultimoordine",
+ "Id": 16
+ },
+ {
+ "Description": "totaleultimoordine",
+ "Id": 15
+ },
+ {
+ "Description": "dataultimoordine",
+ "Id": 14
+ },
+ {
+ "Description": "idultimoordine",
+ "Id": 13
+ },
+ {
+ "Description": "idcliente",
+ "Id": 12
+ },
+ {
+ "Description": "telefono",
+ "Id": 11
+ },
+ {
+ "Description": "fax",
+ "Id": 10
+ },
+ {
+ "Description": "indirizzo",
+ "Id": 9
+ },
+ {
+ "Description": "paese",
+ "Id": 8
+ }
+ ],
+ "PageNumber": 0,
+ "PageSize": 20,
+ "Skipped": 0,
+ "TotalElementsCount": 27
+}
\ No newline at end of file
diff --git a/src/main/resources/META-INF/MANIFEST.MF b/src/main/resources/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..4c5ea33
--- /dev/null
+++ b/src/main/resources/META-INF/MANIFEST.MF
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Main-Class: com.poloinformatico.MailupClient.Main
+