Commit 5a428b22 authored by Cristian Aguirre's avatar Cristian Aguirre

Update Writter Process

parent f7f7fad2
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.utils.JsonUtils;
import com.bytesw.xdf.exception.NotFoundException;
......@@ -14,14 +19,19 @@ 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;
import java.util.Optional;
......@@ -29,6 +39,11 @@ import java.util.Optional;
@Log4j2
public class DataSensibleJPAWriter implements ItemWriter<DynaBean>, StepExecutionListener {
@Autowired
DeleteDataSensibleControlRepository deleteDataSensibleControlRepository;
@Autowired
DeleteDataSensibleLogRepository deleteDataSensibleLogRepository;
@Autowired
IntentRepository intentRepository;
......@@ -46,7 +61,7 @@ public class DataSensibleJPAWriter implements ItemWriter<DynaBean>, StepExecutio
@Transactional
public void write(List<? extends DynaBean> list) throws Exception {
for (DynaBean dynaBean : list) {
log.debug("LLegue ACA");
Long id = (Long) dynaBean.get("id");
String json = (String) dynaBean.get("data");
String event = (String) JsonUtils.getFieldFromJson(json, "$.event");
if (EventTypeEnum.USER.getName().equals(event)) {
......@@ -54,7 +69,6 @@ public class DataSensibleJPAWriter implements ItemWriter<DynaBean>, StepExecutio
if (agent.getChannelValue().equals(toAgent)) {
String intent = (String) JsonUtils.getFieldFromJson(json, "$.parse_data.intent.name");
if (intentByAgent.indexOf(intent) >= 0) {
log.debug("Llegue aca");
String SmSMessageSid = (String) JsonUtils.getFieldFromJson(json, "$.metadata.SmsMessageSid");
//Nuevos parametros para el llamado del metodo
......@@ -62,13 +76,42 @@ public class DataSensibleJPAWriter implements ItemWriter<DynaBean>, StepExecutio
Optional<IntentBean> intenId = this.intentRepository.findIntenNameById(intent, agent.getAgenId());
if (!intenId.isPresent()){
//Exception
throw new Exception("Intent no esta presente");
}
String senderId = (String) JsonUtils.getFieldFromJson(json, "$.metadata.sender_id");;
List<DeleteDataSensRegistryBean> deleteSensibleBean = service.deleteMessage(agent.getAgenId()
,SmSMessageSid, intenId.get().getId(), senderId);
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()));
deleteDataSensibleLogRepository.save(reg);
}
DeleteDataSensibleControl control = new DeleteDataSensibleControl();
Optional<DeleteDataSensibleControl> controlBd = deleteDataSensibleControlRepository.findEventIdByAgentId(agent.getAgenId());
if (controlBd.isPresent()){
// Actualizamos
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);
deleteDataSensibleControlRepository.save(control);
}else{
// Guardamos
control.setAgentId(agent.getAgenId());
control.setEventId(id);
control.setDate(Timestamp.valueOf(OffsetDateTime.now().toString()));
deleteDataSensibleControlRepository.save(control);
}
}
}
}
......@@ -76,6 +119,14 @@ public class DataSensibleJPAWriter implements ItemWriter<DynaBean>, StepExecutio
//service.saveDataSensControl(eventBean.getId(), eventBean.getSenderId());
}
protected DeleteDataSensibleControl toModel(DeleteDataSensibleControl model, DeleteDataSensControlBean bean){
if(model == null){
model = new DeleteDataSensibleControl();
}
BeanUtils.copyProperties(model, bean);
return model;
}
@Override
public void beforeStep(StepExecution stepExecution) {
String tenantId = stepExecution.getJobParameters().getString("tenantId");
......
package com.bytesw.bytebot.etl.dao;
import com.bytesw.bytebot.etl.model.DeleteDataSensibleControl;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import java.util.Optional;
public interface DeleteDataSensibleControlRepository extends CrudRepository<DeleteDataSensibleControl, Long> {
@Query("SELECT s from avb_delete_sens_msg_control s WHERE s.agentId = :agentId")
Optional<DeleteDataSensibleControl> findEventIdByAgentId(@Param("agentId") Long agentId);
}
......@@ -2,9 +2,11 @@ package com.bytesw.bytebot.etl.services;
import com.bytesw.bytebot.etl.batch.beans.DeleteDataSensRegistryBean;
import com.bytesw.bytebot.etl.dao.DeleteDataSensibleControlRepository;
import com.bytesw.bytebot.etl.model.DeleteDataSensibleLog;
import com.bytesw.bytebot.service.provider.TwilioService;
import com.bytesw.xdf.exception.NotFoundException;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -34,7 +36,7 @@ public class DeleteSensMessageService {
data = twilioService.generateInfo(SmsMessage,params);
if(data.isEmpty()){
//Excepcion
throw new Exception("Parámetros vacíos");
}
log.debug("RESPUESTA: "+data);
......@@ -45,6 +47,9 @@ public class DeleteSensMessageService {
List<String> images = new ArrayList<String>();
images = (List<String>) data.get("data");
for(String image : images){
if (image.trim().isEmpty()){
continue;
}
// Info del Bean
registry.setMultimedia_sid(image);
registry.setMessage_sid(SmsMessage);
......@@ -58,13 +63,13 @@ public class DeleteSensMessageService {
}catch (NotFoundException e){
log.debug(e);
log.debug("Imagen no encontrada");
}catch (Exception e ){
log.debug(e);
log.debug("Error consumiento servicio de Proveedor");
}finally {
return deleteMessages;
}
return deleteMessages;
}
}
......@@ -11,7 +11,7 @@ import java.util.Map;
@Log4j2
public abstract class ProviderService {
abstract void completeData(Map<String, Object> info, String SmsId, int Agent_id) throws AuthenticationException;
abstract void completeData(Map<String, Object> info, String SmsId, int Agent_id) throws AuthenticationException, Exception;
public Map<String,Object> generateInfo(String sid, Map<String, Object> params) throws AuthenticationException {
......@@ -32,6 +32,8 @@ public abstract class ProviderService {
throw new AuthenticationException(e.getMessage());
}catch (InternalError e){
throw new InternalError(e.getMessage());
}catch (Exception e){
throw new InternalError(e.getMessage());
}
return info;
......
......@@ -37,7 +37,7 @@ public class TwilioService extends ProviderService{
}
@Override
protected void completeData(Map<String, Object> info, String SmsId,int agent_id) throws AuthenticationException{
protected void completeData(Map<String, Object> info, String SmsId,int agent_id) throws AuthenticationException, Exception{
long start_time = System.nanoTime();
List<DeploymentChannelParamValueBean> agents = getAllParamsWithRefresh();
long end_time = System.nanoTime();
......@@ -66,7 +66,7 @@ public class TwilioService extends ProviderService{
log.debug("EXISTE AGENT: "+exist_agent);
if(!exist_agent){
log.info("No existe un agente con el ID especificado");
throw new NotFoundException("No se encontró un agente con canales deployados con dicho ID");
throw new Exception("No se encontró un agente con canales deployados con dicho ID");
}
String[] images_droped = new String[MAX_LIMIT];
Twilio.init(sid, token);
......@@ -103,6 +103,8 @@ public class TwilioService extends ProviderService{
log.info("El mensaje no existe para tal SID");
throw new NotFoundException("No se encontro el mensaje en el Proveedor");
}
}catch (Exception e){
throw new 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