Páginas filhas
  • Integração RM x SmartLink Behavior Sharing - Visão desenvolvedor

Versões comparadas

Chave

  • Esta linha foi adicionada.
  • Esta linha foi removida.
  • A formatação mudou.

...

Bloco de código
languagec#
firstline1
linenumberstrue
using RM.Glb.SGDP.SmartLink.Service.Domain;
using RM.Glb.SGDP.SmartLink.Service.Domain.Interfaces;
using RM.Lib.Log;
using RM.Lib.SmartLink.Domain.DataModel;
using RM.Lib.SmartLink.Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;

namespace RM.Glb.SGDP.SmartLink.Service
{
  public class GlbSGDPPublisherMessageService : IGlbSGDPPublisherMessageService
  {
    private readonly IGlbSGDPResolverService _sgdpResolverService;
    private readonly IRMSSmartLinkPublisherService _smartLinkPublisherService;
    private readonly IRMSLogger _logService;

 	private const string ctSGDPAudience = "SUA_SIGLA_DE_AUDIENCE";

    private const string ctSGDPUpdateTenantMetadataEndPoint = "/api/v1/link/send/SGDPUpdateTenantMetadata/SGDP";
    private const string ctSGDPDataResponseEndPoint = "/api/v1/link/send/SGDPDataResponse/SGDP";

    private const string ctSGDPUpdateTenantMetadata = "SGDPUpdateTenantMetadata";


    private const string ctSGDPDataCommand = "SGDPDataCommand";
    private const string ctSGDPResponseDataCommand = "SGDPResponseDataCommand";

    private const string ctSGDPMaskResponseEndPoint = "/api/v1/link/send/SGDPMaskResponse/SGDP";
    private const string ctSGDPMaskCommand = "SGDPMaskCommand";
    private const string ctSGDPResponseMaskCommand = "SGDPResponseMaskCommand";

    private const string ctSGDPLogResponseEndPoint = "/api/v1/link/send/SGDPLogsResponse/SGDP";
    private const string ctSGDPLogsCommand = "SGDPLogsCommand";
    private const string ctSGDPResponseLogCommand = "SGDPResponseLogsCommand";

    private const string ctSmartLinkSetupCommand = "setup";
    private const string ctSmartLinkUnSetupCommand = "unSetup";


    public GlbSGDPPublisherMessageService(IRMSSmartLinkPublisherService smartLinkPublisherService,
      IGlbSGDPResolverService sgdpResolverService,
      IRMSLogger logService)
    {
      _smartLinkPublisherService = smartLinkPublisherService;
      _sgdpResolverService = sgdpResolverService;
      _logService = logService;
    }

    /// <summary>
    /// Adiciona uma mensagem de reposta para o comando "SGDPResponseDataCommand".
    /// </summary>
    /// <param name="parms"></param>
    public void AddResponseDataCommand(AddResponseDataCommandParms parms)
    {
      SmartLinkPublisherAddMessageParams parPublisher = new SmartLinkPublisherAddMessageParams
      {
        Command = ctSGDPResponseDataCommand,
        CorrelatedId = parms.CorrelatedId,
	 	Audience = ctSGDPAudience
      };
      ;SmartLinkPublisherAddMessageParams       parPublisher.Data = parms.Message;
      parPublisher.RouterMessage = ctSGDPDataResponseEndPoint;
      _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppSGDPResponseDataCommandGravadoNaFila);
      _smartLinkPublisherService.AddMessage(parPublisher);
    }

    /// <summary>
    /// Adiciona uma mensagem de reposta para o comando "SGDPResponseMaskCommand".
    /// </summary>
    /// <param name="parms"></param>
    public void AddResponseMaskCommand(AddResponseMaskCommandParms parms)
    {
      SmartLinkPublisherAddMessageParams parPublisher = new SmartLinkPublisherAddMessageParams
      {
        Command = ctSGDPResponseMaskCommand,
        CorrelatedId = parms.CorrelatedId,
        Data = parms.Message,
        RouterMessage = ctSGDPMaskResponseEndPoint,
		Audience = ctSGDPAudience
      };
      _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppSGDPResponseMaskCommandGravadoNaFila);
      _smartLinkPublisherService.AddMessage(parPublisher);
    }
    /// <summary>
    /// Adiciona uma mensagem de reposta para o comando "SGDPResponseLogCommand".
    /// </summary>
    /// <param name="parms">Parms</param>
    public void AddResponseLogCommand(AddResponseLogCommandParms parms)
    {
      SmartLinkPublisherAddMessageParams parPublisher = new SmartLinkPublisherAddMessageParams
      {
        Command = ctSGDPResponseLogCommand,
        CorrelatedId = parms.CorrelatedId,
        Data = parms.Message,
        RouterMessage = ctSGDPLogResponseEndPoint,
        Audience = ctSGDPAudience       
	  };
      _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppSGDPResponseLogCommandGravadoNaFila);
      _smartLinkPublisherService.AddMessage(parPublisher);
    }

    /// <summary>
    /// Adiciona uma mensagem para envio de um comando de sgldpdateapplicationmetada.
    /// </summary>
    public void AddMessageUpdateTenantMetadata(AddMessageUpdateTenantMetadataParms parms)
    {
      if (CanAddTenantMetadataMessage())
      {
        try
        {
          List<string> jsons = GetUpdateTenandMetadata(parms.TenantId);
          foreach (string json in jsons)
          {
            SmartLinkPublisherAddMessageParams smartLinkParms = new SmartLinkPublisherAddMessageParams();
            smartLinkParms.Command = ctSGDPUpdateTenantMetadata;
            smartLinkParms.CorrelatedId = "";
            smartLinkParms.Data = json;
            smartLinkParms.RouterMessage = ctSGDPUpdateTenantMetadataEndPoint;
            _logServicesmartLinkParms.Audience = ctSGDPAudience;              			 	 

			_logServic e.NotifyLogInfo(Properties.Resources.sconTotvsAppSGDPTenantMetadaGravadoNaFila);
            _smartLinkPublisherService.AddMessage(smartLinkParms);
          }
        }
        catch (Exception ex)
        {
          _logService.NotifyLogWarning(ex, Properties.Resources.sconTotvsAppErroAoEnviarSGDPUpdateTenantMetadata);
        }
      }
    }

    private bool CanAddTenantMetadataMessage()
    {
      //Se existir alguma mensagem de sgdp para ser processada, então não envia atualizaçaõ de medatdos pois esse processo é caro.
      var pendingMessages = _smartLinkPublisherService.GetPendingMessages();
      List<SmartLinkMessageDataModel> messageUpstream = pendingMessages?.Messages?.Where(
        y => y.TypeEvent == ctSGDPUpdateTenantMetadata ||
             y.TypeEvent == ctSGDPDataCommand ||
             y.TypeEvent == ctSGDPMaskCommand ||
             y.TypeEvent == ctSGDPResponseDataCommand ||
             y.TypeEvent == ctSGDPResponseMaskCommand).ToList();
      if (messageUpstream?.Count > 0)//Se existir alguma não inclua na fila...
        return false;
      return true;
    }

    private List<string> GetUpdateTenandMetadata(string tenantId)
    {
      return _sgdpResolverService.GetSGDPTenantMetadata(
        new MetadataTenantParms() { TenantId = tenantId }
        )?.JsonsResult;
    }

    public void AddMessageUnSetup()
    {
      var pendingMessages = _smartLinkPublisherService.GetPendingMessages();
      List<SmartLinkMessageDataModel> messagesSGDP =
        pendingMessages?.Messages?.Where(y => y.TypeEvent.ToUpper() == ctSGDPUpdateTenantMetadata.ToUpper() &&
                                              y.TypeEvent.ToUpper() == ctSGDPDataCommand.ToUpper() &&
                                              y.TypeEvent.ToUpper() == ctSGDPMaskCommand.ToUpper() &&
                                              y.TypeEvent.ToUpper() == ctSGDPLogsCommand.ToUpper() &&
                                              y.TypeEvent.ToUpper() == ctSGDPResponseDataCommand.ToUpper() &&
                                              y.TypeEvent.ToUpper() == ctSGDPResponseMaskCommand.ToUpper()).ToList();

      if (messagesSGDP != null)
      {
        foreach (SmartLinkMessageDataModel model in messagesSGDP)
        {
          _smartLinkPublisherService.RemoveMessageById(model.Id);
        }
      }

      SmartLinkPublisherAddMessageParams parms = new SmartLinkPublisherAddMessageParams
      {
        Command = ctSmartLinkUnSetupCommand,
        CorrelatedId = "",
        Audience = ctSGDPAudience,
        Data = _sgdpResolverService.GetSGDPSetup().JsonResult
      };
      _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppUnSetupGravadoNaFila);
      _smartLinkPublisherService.AddMessage(parms);
    }

    public void AddMessageSetup()
    {
      SmartLinkPublisherAddMessageParams parms = new SmartLinkPublisherAddMessageParams
      {
        Command = ctSmartLinkSetupCommand,
        CorrelatedId = "",
        Audience = ctSGDPAudience,
        Data = _sgdpResolverService.GetSGDPSetup().JsonResult
      };
      _logService.NotifyLogInfo(Properties.Resources.sconTotvsAppSetupGravadoNaFila);
      _smartLinkPublisherService.AddMessage(parms);
    }
  }
}

...