|
|
@@ -2,6 +2,11 @@ package es.uv.saic.service;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStreamReader;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URI;
|
|
|
+import java.net.http.HttpClient;
|
|
|
+import java.net.http.HttpRequest;
|
|
|
+import java.net.http.HttpResponse;
|
|
|
import java.sql.Connection;
|
|
|
import java.sql.DriverManager;
|
|
|
import java.sql.ResultSet;
|
|
|
@@ -26,9 +31,14 @@ import org.apache.commons.csv.CSVParser;
|
|
|
import org.apache.commons.csv.CSVRecord;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.LinkedCaseInsensitiveMap;
|
|
|
|
|
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
+import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+
|
|
|
import es.uv.saic.domain.IndicadorEnquestaTmp;
|
|
|
import es.uv.saic.domain.Organ;
|
|
|
import es.uv.saic.domain.Usuari;
|
|
|
@@ -40,15 +50,16 @@ import es.uv.saic.domain.Datasource;
|
|
|
public class DataService {
|
|
|
|
|
|
@Autowired
|
|
|
- private IndicadorEnquestaTmpService iets;
|
|
|
- @Autowired
|
|
|
private IndicadorEnquestaService ies;
|
|
|
- @Autowired
|
|
|
- private OrganService orgs;
|
|
|
+
|
|
|
@Autowired
|
|
|
private EmailService ems;
|
|
|
+
|
|
|
@Autowired
|
|
|
private DatasourceService dss;
|
|
|
+
|
|
|
+ @Value("${saic.url.domain}")
|
|
|
+ private String uri;
|
|
|
|
|
|
private HashMap<String, String> titsMap;
|
|
|
private HashMap<String, String> centresMap;
|
|
|
@@ -134,7 +145,7 @@ public class DataService {
|
|
|
Integer centre;
|
|
|
Integer titulacio;
|
|
|
if(clau.equals("ruct") && ambit.equals("T") && ruct != null) {
|
|
|
- Organ o = this.orgs.findByRuct(ruct);
|
|
|
+ Organ o = findOrgByRuct(ruct);
|
|
|
if(o == null) {
|
|
|
errors.append("[WARNING] " + (locale.equals("es") ? "No se ha encontrado el RUCT "+ructStr+", se omiten los registros de esta entrada." : "No s'ha trobat el RUCT "+ructStr+", s'ometen els registres d'aquesta entrada.")).append("<br>").toString();
|
|
|
continue;
|
|
|
@@ -145,7 +156,7 @@ public class DataService {
|
|
|
else{
|
|
|
centre = ambit.equals("T") || ambit.equals("C") ? Integer.parseInt(c.replaceAll("\\D+","")) : null;
|
|
|
titulacio = ambit.equals("T") ? Integer.parseInt(t) : null;
|
|
|
- if(!this.orgs.exists("T", titulacio)){
|
|
|
+ if(!existsOrg("T", titulacio)){
|
|
|
errors.append("[WARNING] " + (locale.equals("es") ? "No se ha encontrado la titulación con código "+titulacio+" pero se importa igualmente." : "No s'ha trobat la titulació amb codi "+titulacio+" però s'importa igualment")).append("<br>").toString();
|
|
|
}
|
|
|
}
|
|
|
@@ -180,7 +191,7 @@ public class DataService {
|
|
|
ie.setValor(null);
|
|
|
}
|
|
|
|
|
|
- iets.save(ie);
|
|
|
+ save(ie);
|
|
|
numRecords++;
|
|
|
}
|
|
|
numTuples++;
|
|
|
@@ -274,7 +285,7 @@ public class DataService {
|
|
|
else{
|
|
|
isValid = false;
|
|
|
}
|
|
|
- Organ o = this.orgs.findByRuct(ruct);
|
|
|
+ Organ o = findOrgByRuct(ruct);
|
|
|
if(o == null) {
|
|
|
errors.append("[WARNING] " + (locale.equals("es") ? "No se ha encontrado el RUCT "+ructStr+", se omiten los registros de esta entrada." : "No s'ha trobat el RUCT "+ructStr+", s'ometen els registres d'aquesta entrada.")).append("<br>").toString();
|
|
|
continue;
|
|
|
@@ -325,7 +336,7 @@ public class DataService {
|
|
|
ie.setValor(null);
|
|
|
}
|
|
|
|
|
|
- iets.save(ie);
|
|
|
+ save(ie);
|
|
|
numRecords++;
|
|
|
}
|
|
|
numTuples++;
|
|
|
@@ -366,36 +377,36 @@ public class DataService {
|
|
|
}
|
|
|
|
|
|
public List<IndicadorEnquestaTmpDup> checkDuplicates(String enquesta) {
|
|
|
- return this.iets.checkDuplicates(enquesta);
|
|
|
+ return checkDuplicatesAux(enquesta);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public Integer consolidateByEnquesta(String enquesta) {
|
|
|
- if(this.iets.checkDuplicates(enquesta).size() > 0) {
|
|
|
+ if(checkDuplicates(enquesta).size() > 0) {
|
|
|
return -1;
|
|
|
}
|
|
|
else {
|
|
|
Integer i = this.ies.consolidateByEnquesta(enquesta);
|
|
|
- this.iets.deleteByEnquesta(enquesta);
|
|
|
+ deleteByEnquesta(enquesta);
|
|
|
return i;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public Integer countByEnquesta(String enquesta) {
|
|
|
- return this.iets.countByEnquesta(enquesta);
|
|
|
+ return countByEnquestaAux(enquesta);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public List<IndicadorEnquestaTmpDup> checkIntegrity(String enquesta){
|
|
|
- return this.iets.checkIntegrity(enquesta);
|
|
|
+ return checkIntegrityAux(enquesta);
|
|
|
}
|
|
|
|
|
|
public Integer deleteFromCurrent(String enquesta){
|
|
|
- return this.iets.deleteFromCurrent(enquesta);
|
|
|
+ return deleteFromCurrentAux(enquesta);
|
|
|
}
|
|
|
|
|
|
public Integer deleteFromPending(String enquesta){
|
|
|
- return this.iets.deleteFromPending(enquesta);
|
|
|
+ return deleteFromPendingAux(enquesta);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
private void setColumnsCod(String ambit, List<String> knownColumns, List<String> mandatoryColumns) {
|
|
|
knownColumns.addAll(Arrays.asList("ruct", "curs_enquesta", "centre_origen", "titulacio_origen", "nenq", "cursd"));
|
|
|
if(ambit.equals("U")) {
|
|
|
@@ -529,4 +540,225 @@ public class DataService {
|
|
|
}
|
|
|
return c;
|
|
|
}
|
|
|
+
|
|
|
+ private Organ findOrgByRuct(Integer ruct) {
|
|
|
+ URI uriObj = URI.create(uri + "/instanciatasca/" + ruct);
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ Organ organ = null;
|
|
|
+ try {
|
|
|
+ HttpClient httpClient = HttpClient.newHttpClient();
|
|
|
+ HttpRequest request = HttpRequest.newBuilder()
|
|
|
+ .uri(uriObj)
|
|
|
+ .GET()
|
|
|
+ .build();
|
|
|
+
|
|
|
+ HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
|
|
+
|
|
|
+ if (response.statusCode() == HttpURLConnection.HTTP_OK) {
|
|
|
+ organ = mapper.readValue(response.body(), new TypeReference<Organ>() {});
|
|
|
+ } else {
|
|
|
+ System.err.println("Failed to find Organ by Ruct. HTTP error code: " + response.statusCode());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return organ;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean existsOrg(String tlugar, Integer titulacio) {
|
|
|
+ URI uriObj = URI.create(uri + "/existOrg/" + tlugar + "/" + titulacio);
|
|
|
+ try {
|
|
|
+ HttpClient httpClient = HttpClient.newHttpClient();
|
|
|
+ HttpRequest request = HttpRequest.newBuilder()
|
|
|
+ .uri(uriObj)
|
|
|
+ .GET()
|
|
|
+ .build();
|
|
|
+
|
|
|
+ HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
|
|
+
|
|
|
+ if (response.statusCode() == HttpURLConnection.HTTP_OK) {
|
|
|
+ return response.body() == "1" ? true : false;
|
|
|
+ } else {
|
|
|
+ System.err.println("Failed to find Organ by Ruct. HTTP error code: " + response.statusCode());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void deleteByEnquesta(String enquesta) {
|
|
|
+ URI uriobj = URI.create(uri + "/delete/by/" + enquesta);
|
|
|
+ try {
|
|
|
+ HttpClient httpClient = HttpClient.newHttpClient();
|
|
|
+ HttpRequest request = HttpRequest.newBuilder()
|
|
|
+ .uri(uriobj)
|
|
|
+ .DELETE()
|
|
|
+ .build();
|
|
|
+
|
|
|
+ HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
|
|
+
|
|
|
+ // Create the json with the response body
|
|
|
+ if (response.statusCode() == HttpURLConnection.HTTP_OK) {
|
|
|
+ System.out.println("Enquesta eliminada correctamente");
|
|
|
+ } else {
|
|
|
+ System.err.println("Failed to fix integrity by delete current/duplicated." +
|
|
|
+ " HTTP error code: " + response.statusCode());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void save(IndicadorEnquestaTmp ie) {
|
|
|
+ URI uriObj = URI.create(uri + "/save/");
|
|
|
+ ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
+
|
|
|
+ try {
|
|
|
+ String requestBody = mapper.writeValueAsString(ie);
|
|
|
+ HttpClient httpClient = HttpClient.newHttpClient();
|
|
|
+ HttpRequest request = HttpRequest.newBuilder()
|
|
|
+ .uri(uriObj)
|
|
|
+ .header("Content-Type", "application/json")
|
|
|
+ .POST(HttpRequest.BodyPublishers.ofString(requestBody))
|
|
|
+ .build();
|
|
|
+
|
|
|
+ HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
|
|
+
|
|
|
+ if (response.statusCode() == HttpURLConnection.HTTP_OK) {
|
|
|
+ System.out.println("");
|
|
|
+ } else {
|
|
|
+ System.err.println("Failed to find save. HTTP error code: " + response.statusCode());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<IndicadorEnquestaTmpDup> checkDuplicatesAux(String enquesta) {
|
|
|
+ URI uriObj = URI.create(uri + "/checkDuplicates/" + enquesta);
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ List<IndicadorEnquestaTmpDup> inds = null;
|
|
|
+ try {
|
|
|
+ HttpClient httpClient = HttpClient.newHttpClient();
|
|
|
+ HttpRequest request = HttpRequest.newBuilder()
|
|
|
+ .uri(uriObj)
|
|
|
+ .GET()
|
|
|
+ .build();
|
|
|
+
|
|
|
+ HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
|
|
+
|
|
|
+ if (response.statusCode() == HttpURLConnection.HTTP_OK) {
|
|
|
+ inds = mapper.readValue(response.body(), new TypeReference<List<IndicadorEnquestaTmpDup>>() {});
|
|
|
+ } else {
|
|
|
+ System.err.println("Failed to check duplicates of IndicadorEnquestaTmpDup by enquesta. HTTP error code: " + response.statusCode());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return inds;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Integer deleteFromPendingAux(String enquesta) {
|
|
|
+ URI uriobj = URI.create(uri + "/delete/pending/" + enquesta);
|
|
|
+
|
|
|
+ try {
|
|
|
+ HttpClient httpClient = HttpClient.newHttpClient();
|
|
|
+ HttpRequest request = HttpRequest.newBuilder()
|
|
|
+ .uri(uriobj)
|
|
|
+ .DELETE()
|
|
|
+ .build();
|
|
|
+
|
|
|
+ HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
|
|
+
|
|
|
+ // Create the json with the response body
|
|
|
+ if (response.statusCode() == HttpURLConnection.HTTP_OK) {
|
|
|
+ return Integer.parseInt(response.body());
|
|
|
+ } else {
|
|
|
+ System.err.println("Failed to delete pending." +
|
|
|
+ " HTTP error code: " + response.statusCode());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Integer deleteFromCurrentAux(String enquesta) {
|
|
|
+ URI uriobj = URI.create(uri + "/delete/current/" + enquesta);
|
|
|
+
|
|
|
+ try {
|
|
|
+ HttpClient httpClient = HttpClient.newHttpClient();
|
|
|
+ HttpRequest request = HttpRequest.newBuilder()
|
|
|
+ .uri(uriobj)
|
|
|
+ .DELETE()
|
|
|
+ .build();
|
|
|
+
|
|
|
+ HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
|
|
+
|
|
|
+ // Create the json with the response body
|
|
|
+ if (response.statusCode() == HttpURLConnection.HTTP_OK) {
|
|
|
+ return Integer.parseInt(response.body());
|
|
|
+ } else {
|
|
|
+ System.err.println("Failed to delete pending." +
|
|
|
+ " HTTP error code: " + response.statusCode());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<IndicadorEnquestaTmpDup> checkIntegrityAux(String enquesta) {
|
|
|
+ URI uriObj = URI.create(uri + "/checkIntegrity/" + enquesta);
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ List<IndicadorEnquestaTmpDup> inds = null;
|
|
|
+ try {
|
|
|
+ HttpClient httpClient = HttpClient.newHttpClient();
|
|
|
+ HttpRequest request = HttpRequest.newBuilder()
|
|
|
+ .uri(uriObj)
|
|
|
+ .GET()
|
|
|
+ .build();
|
|
|
+
|
|
|
+ HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
|
|
+
|
|
|
+ if (response.statusCode() == HttpURLConnection.HTTP_OK) {
|
|
|
+ inds = mapper.readValue(response.body(), new TypeReference<List<IndicadorEnquestaTmpDup>>() {});
|
|
|
+ } else {
|
|
|
+ System.err.println("Failed to check integrity of IndicadorEnquestaTmpDup by enquesta. HTTP error code: " + response.statusCode());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return inds;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Integer countByEnquestaAux(String enquesta) {
|
|
|
+ URI uriObj = URI.create(uri + "/countByEnquesta/" + enquesta);
|
|
|
+ try {
|
|
|
+ HttpClient httpClient = HttpClient.newHttpClient();
|
|
|
+ HttpRequest request = HttpRequest.newBuilder()
|
|
|
+ .uri(uriObj)
|
|
|
+ .GET()
|
|
|
+ .build();
|
|
|
+
|
|
|
+ HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
|
|
+
|
|
|
+ if (response.statusCode() == HttpURLConnection.HTTP_OK) {
|
|
|
+ return Integer.parseInt(response.body());
|
|
|
+ } else {
|
|
|
+ System.err.println("Failed to find Organ by Ruct. HTTP error code: " + response.statusCode());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
}
|