Commit 24878427 authored by jgomez's avatar jgomez

Correcciones generales en Writter

parent 255d6948
......@@ -7,7 +7,10 @@ import com.bytesw.bytebot.etl.batch.processor.ConvertToBeanProcessor;
import com.bytesw.bytebot.etl.batch.writer.ByteBotJPAWriter;
import com.bytesw.bytebot.etl.batch.writer.DataSensibleJPAWriter;
import com.bytesw.bytebot.etl.config.BatchProperties;
import com.bytesw.bytebot.etl.dao.DeleteDataSensibleControlRepository;
import com.bytesw.bytebot.etl.dao.DeleteDataSensibleLogRepository;
import com.bytesw.bytebot.etl.dao.EventHistoryRepository;
import com.bytesw.bytebot.etl.dao.IntentRepository;
import com.bytesw.bytebot.etl.enums.IntentTypeEnum;
import com.bytesw.bytebot.etl.services.DeleteSensMessageService;
import com.bytesw.bytebot.etl.services.ProcessMessageService;
......@@ -103,6 +106,15 @@ public class ScheduleService implements SchedulingConfigurer {
@Autowired
private DeleteDataSensJDBCRepository deleteDataSensJDBCRepository;
@Autowired
private DeleteDataSensibleControlRepository deleteDataSensibleControlRepository;
@Autowired
private DeleteDataSensibleLogRepository deleteDataSensibleLogRepository;
@Autowired
private IntentRepository intentRepository;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
if (scheduledTaskRegistrar == null) {
......@@ -139,9 +151,9 @@ public class ScheduleService implements SchedulingConfigurer {
/*ETL eliminacion de data sensible*/
List<DeleteDataSensBean> deleteDataSensBeans = deleteDataSensJDBCRepository
.getListAgentChannel( AgentStatusEnum.DEPLOYED.getName(), AgentParameterEnum.ACCESS_TWILIO.getName());
.getListAgentChannel(AgentStatusEnum.DEPLOYED.getName(), AgentParameterEnum.ACCESS_TWILIO.getName());
String keyDataSens = "";
for (DeleteDataSensBean data : deleteDataSensBeans){
for (DeleteDataSensBean data : deleteDataSensBeans) {
keyDataSens = String.format("%s-%s", tenantIdentifier, data.getValue());
futureMap.put(keyDataSens, taskRegistrar.getScheduler()
.schedule(() -> scheduleCron(createJobDataSens(tenantIdentifier, data), tenantIdentifier), trigger));
......@@ -161,7 +173,7 @@ public class ScheduleService implements SchedulingConfigurer {
ByteBotJPAWriter writer = new ByteBotJPAWriter();
writer.setService(service);
return stepBuilderFactory.get("processByteBotData").<DynaBean, DynaBean> chunk(chunk)
return stepBuilderFactory.get("processByteBotData").<DynaBean, DynaBean>chunk(chunk)
.reader(getReader(tenantIdentifier))
.processor(new ConvertToBeanProcessor())
.writer(writer).build();
......@@ -178,11 +190,11 @@ public class ScheduleService implements SchedulingConfigurer {
}
Long id = eventHistoryRepository.maxEventId();
log.debug("ID: "+id);
log.debug("ID: " + id);
Map<String, Object> params = new HashMap<>();
params.putAll(tenantFound.get().getDatasource());
params.put("query", String.format((String) params.get("query"), id != null ? id : 0));
log.debug("PARAMS: "+params);
log.debug("PARAMS: " + params);
return dataBaseItemReaderFactory.createReader(rowMapperFactory.createMapper(new HashMap<>()), params);
}
......@@ -202,9 +214,12 @@ public class ScheduleService implements SchedulingConfigurer {
writer.setIntentByAgent(intentByAgent);
writer.setAgent(data);
writer.setService(deleteSensMessageService);
writer.setIntentRepository(intentRepository);
writer.setDeleteDataSensibleControlRepository(deleteDataSensibleControlRepository);
writer.setDeleteDataSensibleLogRepository(deleteDataSensibleLogRepository);
return stepBuilderFactory.get("processByteBotDatasSens")
.<DynaBean, DynaBean> chunk(chunk)
.<DynaBean, DynaBean>chunk(chunk)
.reader(getReaderDataSens(tenantIdentifier, data))
.processor(new ConvertToBeanProcessor())
.writer(writer).build();
......@@ -222,18 +237,19 @@ public class ScheduleService implements SchedulingConfigurer {
DeleteDataSensControlBean control = deleteDataSensJDBCRepository.getControl(data.getAgenId());
OffsetDateTime endDate = OffsetDateTime.now();
Long id = 8200L; // Prueba
Long id = 0L;
int differenceTime = 0;
if (control != null){
if (control != null) {
OffsetDateTime lastSessionDate = OffsetDateTime.ofInstant(control.getDateDelete().toInstant(), ZoneId.systemDefault());
Duration difference = Duration.between(lastSessionDate, endDate);
differenceTime = (int) (difference.getSeconds()/60);
differenceTime = (int) (difference.getSeconds() / 60);
if (differenceTime < data.getDeletePeriod()) {
log.debug("Agente " + data.getValue() + " aun no cumple con el periodo de espera.");
throw new RuntimeException("Time is not yet fulfilled");
log.info("Agente " + data.getValue() + " aun no cumple con el periodo de espera.");
// throw new RuntimeException("Time is not yet fulfilled");
return null;
} else {
id = control.getEventId();
}
......@@ -242,7 +258,7 @@ public class ScheduleService implements SchedulingConfigurer {
Map<String, Object> params = new HashMap<>();
params.putAll(tenantFound.get().getDatasource());
params.put("query", String.format((String) params.get("query"), id));
log.debug("PARAMS: "+params);
log.debug("PARAMS: " + params);
return dataBaseItemReaderFactory.createReader(rowMapperFactory.createMapper(new HashMap<>()), params);
}
......
......@@ -3,33 +3,27 @@ package com.bytesw.bytebot.etl.batch.writer;
import com.bytesw.bytebot.etl.batch.beans.DeleteDataSensBean;
import com.bytesw.bytebot.etl.batch.beans.DeleteDataSensControlBean;
import com.bytesw.bytebot.etl.batch.beans.DynaBean;
import com.bytesw.bytebot.etl.beans.IntentBean;
import com.bytesw.bytebot.etl.dao.DeleteDataSensibleControlRepository;
import com.bytesw.bytebot.etl.dao.DeleteDataSensibleLogRepository;
import com.bytesw.bytebot.etl.dao.IntentRepository;
import com.bytesw.bytebot.etl.enums.EventTypeEnum;
import com.bytesw.bytebot.etl.model.DeleteDataSensibleControl;
import com.bytesw.bytebot.etl.model.DeleteDataSensibleLog;
import com.bytesw.bytebot.etl.services.ProcessMessageService;
import com.bytesw.bytebot.etl.model.Intent;
import com.bytesw.bytebot.etl.utils.JsonUtils;
import com.bytesw.xdf.exception.NotFoundException;
import com.bytesw.xdf.multitenant.core.ThreadLocalStorage;
import com.bytesw.bytebot.etl.services.DeleteSensMessageService;
import com.bytesw.bytebot.service.provider.TwilioService;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;
import org.bouncycastle.util.Times;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.item.ItemWriter;
import com.bytesw.bytebot.etl.batch.beans.DeleteDataSensRegistryBean;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import javax.transaction.Transactional;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.OffsetDateTime;
import java.util.List;
......@@ -39,77 +33,87 @@ import java.util.Optional;
@Log4j2
public class DataSensibleJPAWriter implements ItemWriter<DynaBean>, StepExecutionListener {
@Autowired
DeleteDataSensibleControlRepository deleteDataSensibleControlRepository;
@Autowired
DeleteDataSensibleLogRepository deleteDataSensibleLogRepository;
@Getter
@Setter
private DeleteDataSensibleControlRepository deleteDataSensibleControlRepository;
@Autowired
IntentRepository intentRepository;
@Getter
@Setter
private DeleteDataSensibleLogRepository deleteDataSensibleLogRepository;
@Getter @Setter
@Getter
@Setter
private IntentRepository intentRepository;
@Getter
@Setter
private DeleteSensMessageService service;
@Getter @Setter
@Getter
@Setter
private List<String> intentByAgent;
@Getter @Setter
@Getter
@Setter
private DeleteDataSensBean agent;
@Override
@Transactional
public void write(List<? extends DynaBean> list) throws Exception {
for (DynaBean dynaBean : list) {
Long id = Long.parseLong(dynaBean.get("id").toString());
Long id = Long.parseLong(dynaBean.get("id").toString());
String json = (String) dynaBean.get("data");
String event = (String) JsonUtils.getFieldFromJson(json, "$.event");
if (EventTypeEnum.USER.getName().equals(event)) {
String toAgent = (String) JsonUtils.getFieldFromJson(json, "$.metadata.To");
if (agent.getChannelValue().equals(toAgent)) {
String intent = (String) JsonUtils.getFieldFromJson(json, "$.parse_data.intent.name");
//log.debug();
if (intentByAgent.indexOf(intent) >= 0) {
String SmSMessageSid = (String) JsonUtils.getFieldFromJson(json, "$.metadata.SmsMessageSid");
//Nuevos parametros para el llamado del metodo
//@TODO evaluar si transformar a MAP
if (intentByAgent.contains(intent)) {
String SmSMessageSid = (String) JsonUtils.getFieldFromJson(json, "$.metadata.SmsMessageSid");
Optional<IntentBean> intenId = this.intentRepository.findIntenNameById(intent, agent.getAgenId());
if (!intenId.isPresent()){
Optional<Intent> intenId = this.intentRepository.findIntenNameById(intent, agent.getAgenId());
if (!intenId.isPresent()) {
throw new Exception("Intent no esta presente");
}
String senderId = (String) JsonUtils.getFieldFromJson(json, "$.metadata.sender_id");;
// Transform present Date
String presentDate = OffsetDateTime.now().toString().replace('T', ' ');
String timeZone = presentDate.substring(presentDate.length() - 6);
presentDate = presentDate.replace(timeZone, "");
String senderId = (String) JsonUtils.getFieldFromJson(json, "$.metadata.sender_id");
List<DeleteDataSensRegistryBean> deleteSensibleBean = service.deleteMessage(agent.getAgenId()
,SmSMessageSid, intenId.get().getId(), senderId);
, SmSMessageSid, intenId.get().getId(), senderId);
for (DeleteDataSensRegistryBean registry: deleteSensibleBean){
for (DeleteDataSensRegistryBean registry : deleteSensibleBean) {
DeleteDataSensibleLog reg = new DeleteDataSensibleLog();
reg.setIntenId(registry.getInten_id());
reg.setMessageId(registry.getMessage_sid());
reg.setMultimediaId(registry.getMultimedia_sid());
reg.setSendId(registry.getSender_id());
reg.setDate(Timestamp.valueOf(OffsetDateTime.now().toString()));
reg.setDate(Timestamp.valueOf(presentDate));
deleteDataSensibleLogRepository.save(reg);
}
DeleteDataSensibleControl control = new DeleteDataSensibleControl();
Optional<DeleteDataSensibleControl> controlBd = deleteDataSensibleControlRepository.findEventIdByAgentId(agent.getAgenId());
if (controlBd.isPresent()){
// Actualizamos
if (controlBd.isPresent()) {
// Update
DeleteDataSensControlBean controlbean = new DeleteDataSensControlBean();
controlbean.setId(controlBd.get().getId());
controlbean.setEventId(id);
controlbean.setAgenId(agent.getAgenId());
controlbean.setDateDelete(Timestamp.valueOf(OffsetDateTime.now().toString()));
control = toModel(control,controlbean);
controlbean.setDateDelete(Timestamp.valueOf(presentDate));
control = toModel(control, controlbean);
deleteDataSensibleControlRepository.save(control);
}else{
// Guardamos
} else {
// Create
control.setAgentId(agent.getAgenId());
control.setEventId(id);
control.setDate(Timestamp.valueOf(OffsetDateTime.now().toString()));
control.setDate(Timestamp.valueOf(presentDate));
deleteDataSensibleControlRepository.save(control);
}
......@@ -120,8 +124,8 @@ public class DataSensibleJPAWriter implements ItemWriter<DynaBean>, StepExecutio
//service.saveDataSensControl(eventBean.getId(), eventBean.getSenderId());
}
protected DeleteDataSensibleControl toModel(DeleteDataSensibleControl model, DeleteDataSensControlBean bean){
if(model == null){
protected DeleteDataSensibleControl toModel(DeleteDataSensibleControl model, DeleteDataSensControlBean bean) {
if (model == null) {
model = new DeleteDataSensibleControl();
}
BeanUtils.copyProperties(model, bean);
......
package com.bytesw.bytebot.etl.dao;
import com.bytesw.bytebot.etl.beans.IntentBean;
import com.bytesw.bytebot.etl.model.Intent;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
......@@ -12,6 +11,6 @@ public interface IntentRepository extends CrudRepository<Intent, Long> {
Optional<Intent> findByIdentifier(String identifier);
@Query("SELECT s from Intent s WHERE s.identifier = :intenIdent and s.agenId = :agenId")
Optional<IntentBean> findIntenNameById(@Param("intenIdent") String intenIdent, @Param("agenId") Long agenId);
Optional<Intent> findIntenNameById(@Param("intenIdent") String intenIdent, @Param("agenId") Long agenId);
}
package com.bytesw.bytebot.etl.model;
import com.bytesw.xdf.model.converter.BooleanToStringConverter;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
......@@ -9,8 +8,10 @@ import javax.persistence.*;
@Cacheable(false)
@Entity
@Getter @Setter @ToString
@Table(name="AVB_ACTION")
@Getter
@Setter
@ToString
@Table(name = "AVB_ACTION")
@NamedQuery(name = "Action.findByPK", query = "Select p from Action p where p.id = ?1")
public class Action {
......@@ -18,7 +19,7 @@ public class Action {
@Column(name = "action_id")
@TableGenerator(name = "AVB_ACTION_GENERATOR", table = "SEQUENCE_TABLE", pkColumnName = "SEQ_NAME",
valueColumnName = "SEQ_COUNT", pkColumnValue = "AVB_ACTION_SEQ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.TABLE,generator = "AVB_ACTION_GENERATOR")
@GeneratedValue(strategy = GenerationType.TABLE, generator = "AVB_ACTION_GENERATOR")
private Long id;
@Column(name = "action_ident")
......
......@@ -100,7 +100,7 @@ public class TwilioService extends ProviderService{
log.error("Fallo temporal en la conexión con el Proveedor");
throw new InternalError("Fallo en la conexión con Twilio API");
}else{
log.info("El mensaje no existe para tal SID");
log.info("El mensaje no existe para : " + SmsId + "\n" + e.getMessage());
throw new NotFoundException("No se encontro el mensaje en el Proveedor");
}
}catch (Exception e){
......
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