[ADD] AVANCES

parent f3731d0c
package web.multitask.trismegistoservices.config; package web.multitask.trismegistoservices.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import io.github.cdimascio.dotenv.Dotenv; import io.github.cdimascio.dotenv.Dotenv;
import web.multitask.trismegistoservices.utils.dotEnvUtils; import web.multitask.trismegistoservices.utils.DotEnvUtils;
@Configuration @Configuration
public class DotEnvConfig { public class DotEnvConfig {
// @Bean @Bean
Dotenv getDotEnvPath() { Dotenv getDotEnvPath() {
return Dotenv.configure().directory(dotEnvUtils.getDotEnvPath("trismegisto-asistencia")).load(); return Dotenv.configure().directory(DotEnvUtils.getDotEnvPath("trismegisto-asistencia_ssl")).load();
} }
} }
\ No newline at end of file
...@@ -3,7 +3,6 @@ package web.multitask.trismegistoservices.config; ...@@ -3,7 +3,6 @@ package web.multitask.trismegistoservices.config;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.ChannelRegistration; import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry; import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
......
package web.multitask.trismegistoservices.rest.MH;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import web.multitask.trismegistoservices.services.MH.MHService;
import web.multitask.trismegistoservices.utils.DataSourceUtils;
import web.multitask.trismegistoservices.utils.RoutineUtils;
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("mh")
public class MHRest {
private final DataSourceUtils dataSourceUtils;
private final RoutineUtils routineUtils;
public MHRest(DataSourceUtils dataSourceUtils, RoutineUtils routineUtils) {
this.dataSourceUtils = dataSourceUtils;
this.routineUtils = routineUtils;
}
@PostMapping("/private/consultar_datos_trabajador")
public ResponseEntity<?> datosTrabajador(@RequestBody String json) {
MHService service = new MHService(dataSourceUtils, routineUtils);
JSONObject jsonRequest = new JSONObject(json);
jsonRequest.put("opcion", "consultar_datos");
JSONObject jsonResponse = service.consultarTrabajador(jsonRequest);
return ResponseEntity.ok(jsonResponse.toMap());
}
@PostMapping("/private/consultar_saldo_trabajador")
public ResponseEntity<?> saldoTrabajador(@RequestBody String json) {
MHService service = new MHService(dataSourceUtils, routineUtils);
JSONObject jsonRequest = new JSONObject(json);
jsonRequest.put("opcion", "consultar_saldo");
JSONObject jsonResponse = service.consultarTrabajador(jsonRequest);
return ResponseEntity.ok(jsonResponse.toMap());
}
@PostMapping("/private/consultar_cargo_trabajador")
public ResponseEntity<?> cargoTrabajador(@RequestBody String json) {
MHService service = new MHService(dataSourceUtils, routineUtils);
JSONObject jsonRequest = new JSONObject(json);
jsonRequest.put("opcion", "consultar_cargo");
JSONObject jsonResponse = service.consultarTrabajador(jsonRequest);
return ResponseEntity.ok(jsonResponse.toMap());
}
@PostMapping("/private/registrar_venta")
public ResponseEntity<?> registrarVenta(@RequestBody String json) {
MHService service = new MHService(dataSourceUtils, routineUtils);
JSONObject jsonRequest = new JSONObject(json);
jsonRequest.put("opcion", "consultar_cargo");
JSONObject jsonResponse = service.registrarVenta(jsonRequest);
return ResponseEntity.ok(jsonResponse.toMap());
}
}
...@@ -34,7 +34,7 @@ public class consoleRest { ...@@ -34,7 +34,7 @@ public class consoleRest {
output.append("\n").append(line); output.append("\n").append(line);
} }
process.waitFor(); process.waitFor();
return ResponseEntity.ok(output.toString().replace("[sudo] contraseña para wildfly: ","")); return ResponseEntity.ok(output.toString().replace("[sudo] contraseña para wildfly: ", ""));
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
......
...@@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.*; ...@@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import web.multitask.trismegistoservices.model.DriveRequest; import web.multitask.trismegistoservices.model.DriveRequest;
import web.multitask.trismegistoservices.services.google.DriveService; import web.multitask.trismegistoservices.services.google.DriveService;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@RestController @RestController
...@@ -38,7 +39,7 @@ public class driveRest { ...@@ -38,7 +39,7 @@ public class driveRest {
response.put("message", "File uploaded successfully."); response.put("message", "File uploaded successfully.");
response.put("file_id", id_file_response); response.put("file_id", id_file_response);
response.put("file_url", "https://drive.google.com/file/d/" + id_file_response + "/view?usp=sharing"); response.put("file_url", "https://drive.google.com/file/d/" + id_file_response + "/view?usp=sharing");
response.put("download_url", "https://drive.google.com/uc?export=download&id="+id_file_response); response.put("download_url", "https://drive.google.com/uc?export=download&id=" + id_file_response);
return ResponseEntity.ok(response.toMap()); return ResponseEntity.ok(response.toMap());
} }
} catch (Exception e) { } catch (Exception e) {
...@@ -47,7 +48,7 @@ public class driveRest { ...@@ -47,7 +48,7 @@ public class driveRest {
} }
@GetMapping(path = "/public/download/{id}") @GetMapping(path = "/public/download/{id}")
public ResponseEntity<?> downloadFile(@PathVariable String id, @Nullable @RequestParam(name = "base64") Boolean base64) { public ResponseEntity<?> downloadFile(@PathVariable String id, @Nullable @RequestParam(name = "base64") Boolean base64) {
return driveService.getFile(id, !(base64 == null)); return driveService.getFile(id, !(base64 == null));
} }
......
// package web.multitask.app.api; // package web.multitask.app.api;
package web.multitask.trismegistoservices.rest; package web.multitask.trismegistoservices.rest;
import javax.mail.internet.MimeMessage;
import lombok.AllArgsConstructor; import javax.mail.internet.MimeMessage;
import org.json.JSONObject;
import org.springframework.http.MediaType; import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity; import org.json.JSONObject;
import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RestController; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;
import web.multitask.trismegistoservices.model.GmailRequest; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import web.multitask.trismegistoservices.model.GmailRequest;
// import web.multitask.trismegistoservices.mysql.EmailMysql; // import web.multitask.trismegistoservices.mysql.EmailMysql;
import web.multitask.trismegistoservices.services.google.GmailService; import web.multitask.trismegistoservices.services.google.GmailService;
@RestController @RestController
@RequestMapping("/gmail") @RequestMapping("/gmail")
@AllArgsConstructor @AllArgsConstructor
public class emailRest { public class emailRest {
private final GmailService emailService; private final GmailService emailService;
@RequestMapping(path = "/public/generate", method = RequestMethod.POST, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}) @RequestMapping(path = "/public/generate", method = RequestMethod.POST, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public ResponseEntity<?> full(@ModelAttribute GmailRequest request) { public ResponseEntity<?> full(@ModelAttribute GmailRequest request) {
try { try {
MimeMessage message = emailService.htmlMessage(request); MimeMessage message = emailService.htmlMessage(request);
JSONObject response = emailService.send(message,request.getFrom()); JSONObject response = emailService.send(message, request.getFrom());
return ResponseEntity.ok(response.toMap()); return ResponseEntity.ok(response.toMap());
} catch (Exception e) { } catch (Exception e) {
return ResponseEntity.internalServerError().body(new JSONObject().put("message", e.getMessage()).put("status", false).toMap()); return ResponseEntity.internalServerError().body(new JSONObject().put("message", e.getMessage()).put("status", false).toMap());
} }
} }
} }
\ No newline at end of file \ No newline at end of file
...@@ -31,7 +31,7 @@ public class excelRest { ...@@ -31,7 +31,7 @@ public class excelRest {
int size = json.getBytes().length; int size = json.getBytes().length;
System.out.println("Size: " + size); System.out.println("Size: " + size);
byte[] excelByte = excelService.generateExcel(jsonBody, size); byte[] excelByte = excelService.generateExcel(jsonBody, size);
if(excelByte == null) { if (excelByte == null) {
return ResponseEntity.internalServerError().body("Error al generar el archivo"); return ResponseEntity.internalServerError().body("Error al generar el archivo");
} }
if (jsonBody.optBoolean("base64", false)) { if (jsonBody.optBoolean("base64", false)) {
......
...@@ -5,7 +5,7 @@ import org.json.JSONArray; ...@@ -5,7 +5,7 @@ import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import web.multitask.trismegistoservices.mysql.RoutineSql; import web.multitask.trismegistoservices.utils.RoutineUtils;
@RestController @RestController
@CrossOrigin("*") @CrossOrigin("*")
...@@ -13,26 +13,26 @@ import web.multitask.trismegistoservices.mysql.RoutineSql; ...@@ -13,26 +13,26 @@ import web.multitask.trismegistoservices.mysql.RoutineSql;
@AllArgsConstructor @AllArgsConstructor
public class routineRest { public class routineRest {
final RoutineSql procedureMysql; // final RoutineUtils procedureMysql;
//
@PostMapping("/public/routine") // @PostMapping("/public/routine")
public ResponseEntity<?> callRoutine(@RequestBody String body) { // public ResponseEntity<?> callRoutine(@RequestBody String body) {
JSONObject json = new JSONObject(body); // JSONObject json = new JSONObject(body);
if (json.has("routine")) { // if (json.has("routine")) {
try { // try {
JSONArray params = json.isNull("params") ? new JSONArray() : json.getJSONArray("params"); // JSONArray params = json.isNull("params") ? new JSONArray() : json.getJSONArray("params");
String routine = json.getString("routine"); // String routine = json.getString("routine");
String database = json.optString("database"); // String database = json.optString("database");
JSONObject response = procedureMysql.routineExecutor(routine, // JSONObject response = procedureMysql.routineExecutor(routine,
database, // database,
params.toList().toArray()); // params.toList().toArray());
return ResponseEntity.ok(response.toMap()); // return ResponseEntity.ok(response.toMap());
} catch (Exception e) { // } catch (Exception e) {
return ResponseEntity.internalServerError().body(new JSONObject().put("message", e.getMessage()).put("status", false).toMap()); // return ResponseEntity.internalServerError().body(new JSONObject().put("message", e.getMessage()).put("status", false).toMap());
} // }
} else { // } else {
return ResponseEntity.badRequest().body(new JSONObject().put("message", "Invalid Request").put("status", false).toMap()); // return ResponseEntity.badRequest().body(new JSONObject().put("message", "Invalid Request").put("status", false).toMap());
} // }
} // }
} }
\ No newline at end of file
package web.multitask.trismegistoservices.services.MH;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import web.multitask.trismegistoservices.utils.RoutineUtils;
import web.multitask.trismegistoservices.utils.DataSourceUtils;
import javax.sql.DataSource;
@Service
public class MHService {
private final JdbcTemplate jdbcTemplate = new JdbcTemplate();
private final DataSourceUtils dataSourceUtils;
private final RoutineUtils routineUtils;
public MHService( DataSourceUtils dataSourceUtils, RoutineUtils routineUtils) {
this.dataSourceUtils = dataSourceUtils;
this.routineUtils = routineUtils;
}
public JSONObject consultarTrabajador(JSONObject json){
JSONObject response = new JSONObject();
try{
DataSource dataSource = dataSourceUtils.getDataSourceByName("tp");
jdbcTemplate.setDataSource(dataSource);
String p_opcion = json.optString("opcion");
String p_numero_documento = json.optString("numero_documento");
String p_tipo_documento = json.optString("tipo_documento");
JSONArray params = new JSONArray();
params.put(p_opcion);
params.put(p_numero_documento);
params.put(p_tipo_documento);
response = routineUtils.mysqlProcedure("SP_TP_REST_DATOS_PERSONAL_MH", "nuevo", params, jdbcTemplate);
JSONArray data = response.optJSONArray("data", new JSONArray());
if(data.isEmpty()){
return response;
}else{
JSONObject first = data.optJSONObject(0);
first.put("status", first.optInt("status",0) == 1);
return first;
}
} catch (Exception e) {
response.put("status", false)
.put("message", e.getMessage());
}
return response;
}
public JSONObject registrarVenta(JSONObject json){
JSONObject response = new JSONObject();
try{
DataSource dataSource = dataSourceUtils.getDataSourceByName("tp");
jdbcTemplate.setDataSource(dataSource);
String p_numero_documento = json.optString("numero_documento");
String p_tipo_documento = json.optString("tipo_documento");
String p_observacion = json.optString("observacion");
Double p_monto = json.optDouble("monto");
int p_cuotas = json.optInt("cuotas");
JSONArray params = new JSONArray()
.put(p_numero_documento)
.put(p_tipo_documento)
.put(p_observacion)
.put(p_monto)
.put(p_cuotas);
response = routineUtils.mysqlProcedure("SP_TP_REST_REGISTRAR_VENTA_PERSONAL", "nuevo", params, jdbcTemplate);
JSONArray data = response.optJSONArray("data", new JSONArray());
if(data.isEmpty()){
return response;
}else{
JSONObject first = data.optJSONObject(0);
first.put("status", first.optInt("status",0) == 1);
return first;
}
} catch (Exception e) {
response.put("status", false)
.put("message", e.getMessage());
}
return response;
}
}
package web.multitask.trismegistoservices.utils;
import io.github.cdimascio.dotenv.Dotenv;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
@Component
public class DataSourceUtils {
private final Dotenv dotenv;
public DataSourceUtils(Dotenv dotenv) {
this.dotenv = dotenv;
}
public DataSource getDataSourceByName(String connectionName) {
String normalizedName = connectionName.toUpperCase();
String suffix;
switch (normalizedName) {
case "TP":
suffix = "NUEVO";
break;
default:
throw new IllegalArgumentException("Invalid connection name: " + connectionName + ". Must be 'nuevo' or 'sacoolive3'.");
}
return createDataSource(suffix, normalizedName);
}
private DataSource createDataSource(String suffix, String connectionName) {
// Construct the variable keys dynamically
String host = dotenv.get("MYSQL_TRISMEGISTOASISTENCIA_DB_" + suffix);
String port = dotenv.get("MYSQL_TRISMEGISTOASISTENCIA_DB_PORT_" + suffix);
String databaseName = dotenv.get("MYSQL_TRISMEGISTOASISTENCIA_DB_NAME_" + suffix);
String username = dotenv.get("MYSQL_TRISMEGISTOASISTENCIA_DB_USER_" + suffix);
String password = dotenv.get("MYSQL_TRISMEGISTOASISTENCIA_DB_PASS_" + suffix);
if (host == null || port == null || databaseName == null || username == null || password == null) {
throw new IllegalStateException(
"Missing required environment variables for " + connectionName + " connection."
);
}
DriverManagerDataSource dataSource = new DriverManagerDataSource();
String url = String.format("jdbc:mysql://%s:%s/%s?useSSL=false&serverTimezone=UTC",
host, port, databaseName);
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
package web.multitask.trismegistoservices.utils; package web.multitask.trismegistoservices.utils;
import org.springframework.stereotype.Component;
import java.io.File; import java.io.File;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
public class dotEnvUtils { @Component
public class DotEnvUtils {
static String detectedOS; static String detectedOS;
...@@ -36,14 +38,13 @@ public class dotEnvUtils { ...@@ -36,14 +38,13 @@ public class dotEnvUtils {
public static String getDotEnvPath(String projectName) { public static String getDotEnvPath(String projectName) {
String osType = getOperatingSystemType(); String osType = getOperatingSystemType();
return OS_PATH_MAP.getOrDefault(osType, "") + return ("Windows".equals(osType) ? getWindowsPath(projectName) : projectName);
("Windows".equals(osType) ? getWindowsPath(projectName) : projectName);
} }
private static String getWindowsPath(String projectName) { private static String getWindowsPath(String projectName) {
return OS_PATH_MAP.get("Windows") return OS_PATH_MAP.get("Windows")
.chars() .chars()
.mapToObj(letter -> "/" + (char) letter + ":/dotenv/" + projectName) .mapToObj(letter -> (char) letter + ":/dotenv/" + projectName)
.filter(path -> new File(path).exists()) .filter(path -> new File(path).exists())
.findFirst() .findFirst()
.orElse(""); .orElse("");
......
package web.multitask.trismegistoservices.mysql; package web.multitask.trismegistoservices.utils;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@Service @Component
@AllArgsConstructor @AllArgsConstructor
public class RoutineSql { public class RoutineUtils {
private final web.multitask.trismegistoservices.singleton.threadLocalSingleton threadLocalSingleton; private final web.multitask.trismegistoservices.singleton.threadLocalSingleton threadLocalSingleton;
public JSONObject routineExecutor(String routine, String database, Object[] params) { // public JSONObject routineExecutor(String routine, String database, Object[] params) {
//
String jdbcUrl; // String jdbcUrl;
JdbcTemplate jdbcTemplate = threadLocalSingleton.getJdbcTemplate(); // JdbcTemplate jdbcTemplate = threadLocalSingleton.getJdbcTemplate();
// try {
// jdbcUrl = Objects.requireNonNull(jdbcTemplate.getDataSource()).getConnection().getMetaData().getURL();
// } catch (Exception e) {
// return new JSONObject().put("message", e.getMessage()).put("status", false);
// }
//
// switch (jdbcUrl.split(":")[1]) {
// case "mysql":
// return mysqlProcedure(routine, database, params, jdbcTemplate);
// case "postgresql":
// return postgreFunction(routine, database, params, jdbcTemplate);
// default:
// return new JSONObject().put("message", "Database not supported").put("status", false);
// }
// }
public JSONObject mysqlProcedure(String routine, String database, JSONArray jsonArray, JdbcTemplate jdbcTemplate) {
try { try {
jdbcUrl = Objects.requireNonNull(jdbcTemplate.getDataSource()).getConnection().getMetaData().getURL();
} catch (Exception e) {
return new JSONObject().put("message", e.getMessage()).put("status", false);
}
switch (jdbcUrl.split(":")[1]) { Object[] params = jsonArray.toList().toArray();
case "mysql":
return mysqlProcedure(routine, database, params, jdbcTemplate);
case "postgresql":
return postgreFunction(routine, database, params, jdbcTemplate);
default:
return new JSONObject().put("message", "Database not supported").put("status", false);
}
}
private JSONObject mysqlProcedure(String routine, String database, Object[] params, JdbcTemplate jdbcTemplate) {
try {
StringBuilder query = new StringBuilder("CALL " + database + "." + routine); StringBuilder query = new StringBuilder("CALL " + database + "." + routine);
if (params.length > 0) { if (params.length > 0) {
...@@ -68,7 +72,7 @@ public class RoutineSql { ...@@ -68,7 +72,7 @@ public class RoutineSql {
} }
} }
private JSONObject postgreFunction(String routine, String database, Object[] params,JdbcTemplate jdbcTemplate) { public JSONObject postgreFunction(String routine, String database, Object[] params,JdbcTemplate jdbcTemplate) {
try { try {
StringBuilder query = new StringBuilder("SELECT * FROM " + database + "." + routine); StringBuilder query = new StringBuilder("SELECT * FROM " + database + "." + routine);
......
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