unit uDataModuleUsuarios;

interface

uses
  SysUtils, Classes, DB, DBClient, uDADataTable,
  FactuGES_Intf, uIntegerListUtils, uBizEmpresas,
  UCBase, UCDataConnector, uUCROConn, uDARemoteDataAdapter,
  uDARemoteCommand, uROClient, uRORemoteService, uDADataStreamer,
  uDABin2DataStreamer, uDAScriptingProvider, uIDataModuleUsuarios, UCSettings,
  uDataModuleBase, uDAInterfaces, uDAMemDataTable, uBizUsuarios;

type
  TDataModuleUsuarios = class(TDataModuleBase, IDataModuleUsuarios)
    srvUsuarios: TRORemoteService;
    UCSettingsSpanish: TUCSettings;
    rda_Usuarios: TDARemoteDataAdapter;
    Bin2DataStreamer: TDABin2DataStreamer;
    tbl_USUARIOS: TDAMemDataTable;
    ds_USUARIOS: TDADataSource;
    tbl_PERFILES: TDAMemDataTable;
    ds_PERFILES: TDADataSource;
    procedure DataModuleCreate(Sender: TObject);
  private
    procedure InicializarCamposUserControl(AUserControl: TUserControl);
    procedure InicializarSettingsUserControl(AUserControl: TUserControl);
    function CreateConnectorInstance : TUCDataConnector;
  public
    function GetUsuario(const ID : Integer) : IBizUsuario;
    function NuevoUsuario : IBizUsuario;
    function GetUsuarios : IBizUsuario;

    function NuevoPerfil : IBizPerfilUsuario;
    function GetPerfiles : IBizPerfilUsuario;
    function GetPerfil (const ID : Integer) : IBizPerfilUsuario;

    procedure InicializarUserControl (AUserControl : TUserControl);
  end;


implementation

{$R *.DFM}

uses
  Forms, Controls, uDataTableUtils, uDataModuleConexion,
  Dialogs, Windows, uEmpresasController, cxControls,  
  schUsuariosClient_Intf;

{ TDataModuleUsuarios }

function TDataModuleUsuarios.CreateConnectorInstance: TUCDataConnector;
begin
  // El propietario (Owner) es Application para que se encarge de la
  // liberación del conector que estamos creando.
  Result := TUCROConn.Create(Application);
  with TUCROConn(Result) do
  begin
    ROServiceName := srvUsuarios.ServiceName;
    ROChannel := dmConexion.ROChannel;
    ROMessage := dmConexion.ROMessage;
  end;
end;

function TDataModuleUsuarios.NuevoPerfil: IBizPerfilUsuario;
begin
  Result := GetPerfil(ID_NULO)
end;

function TDataModuleUsuarios.NuevoUsuario: IBizUsuario;
begin
  Result := GetUsuario(ID_NULO)
end;

procedure TDataModuleUsuarios.DataModuleCreate(Sender: TObject);
begin
  inherited;
  srvUsuarios.Channel := dmConexion.Channel;
  srvUsuarios.Message := dmConexion.Message;
end;

function TDataModuleUsuarios.GetPerfil(const ID: Integer): IBizPerfilUsuario;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetPerfiles;

    with Result.DataTable.DynamicWhere do
    begin
      Clear;
      // (ID = :ID)
      Expression := NewBinaryExpression(NewField('', fld_PERFILESID), NewConstant(ID, datInteger), dboEqual);
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleUsuarios.GetPerfiles: IBizPerfilUsuario;
var
  AUsuarios : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AUsuarios := CloneDataTable(tbl_PERFILES);
    AUsuarios.BusinessRulesID := BIZ_CLIENT_PERFIL_USUARIO;

    Result := (AUsuarios as IBizPerfilUsuario);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleUsuarios.GetUsuario(const ID: Integer): IBizUsuario;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetUsuarios;

    with Result.DataTable.DynamicWhere do
    begin
      Clear;
      // (ID = :ID)
      Expression := NewBinaryExpression(NewField('', fld_USUARIOSID), NewConstant(ID, datInteger), dboEqual);
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleUsuarios.GetUsuarios: IBizUsuario;
var
  AUsuarios : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AUsuarios := CloneDataTable(tbl_Usuarios);
    AUsuarios.BusinessRulesID := BIZ_CLIENT_USUARIO;

    Result := (AUsuarios as IBizUsuario);
  finally
    HideHourglassCursor;
  end;
end;

procedure TDataModuleUsuarios.InicializarCamposUserControl(
  AUserControl: TUserControl);
begin
  if not Assigned(AUserControl) then
    raise Exception.Create('UserControl no asignado (InicializarUserControl)');

  with AUserControl do
  begin
    DataConnector := CreateConnectorInstance;

    with TableUsers do
    begin
      TableName := nme_USUARIOS;
      FieldUserID := fld_USUARIOSID;
      FieldUserName := fld_USUARIOSUSERNAME;
      FieldLogin := fld_USUARIOSLOGIN;
      FieldPassword := fld_USUARIOSPASS;
      FieldEmail := fld_USUARIOSEMAIL;
      FieldPrivileged := fld_USUARIOSPRIVILEGED;
      FieldTypeRec := fld_USUARIOSTIPO;
      FieldProfile := fld_USUARIOSID_PERFIL;
      FieldUserExpired := fld_USUARIOSBLOQUEADO;
      FieldDateExpired := fld_USUARIOSPASSEXPIRED;
      FieldUserDaysSun := fld_USUARIOSUSERDAYSSUN;
      FieldKey := fld_USUARIOSCHECKSUM;
    end;

    with TableRights do
    begin
      TableName := nme_PERMISOS;
      FieldUserID := fld_PERMISOSID_USUARIO;
      FieldModule := fld_PERMISOSMODULO;
      FieldComponentName := fld_PERMISOSNOMBRECOMP;
      FieldFormName := fld_PERMISOSEXNOMBREFORM;
      FieldKey := fld_PERMISOSCHECKSUM;
    end;

    with TableUsersLogged do
    begin
      TableName := nme_USUARIOS_LOGON;
      FieldLogonID := fld_USUARIOS_LOGONLOGONID;
      FieldUserID := fld_USUARIOS_LOGONID_USUARIO;
      FieldApplicationID := fld_USUARIOS_LOGONAPLICACION;
      FieldMachineName := fld_USUARIOS_LOGONEQUIPO;
      FieldData := fld_USUARIOS_LOGONDATA;
    end;

    with TableHistory do
    begin
      TableName := nme_USUARIOS_EVENTOS;
      FieldApplicationID := fld_USUARIOS_EVENTOSAPLICACION;
      FieldUserID := fld_USUARIOS_EVENTOSID_USUARIO;
      FieldEventDate := fld_USUARIOS_EVENTOSFECHA;
      FieldEventTime := fld_USUARIOS_EVENTOSHORA;
      FieldForm := fld_USUARIOS_EVENTOSFORM;
      FieldCaptionForm := fld_USUARIOS_EVENTOSTITULO_FORM;
      FieldEvent := fld_USUARIOS_EVENTOSEVENTO;
      FieldObs := fld_USUARIOS_EVENTOSNOTAS;
      FieldTableName := fld_USUARIOS_EVENTOSTNAME;
    end;

  end;
end;

procedure TDataModuleUsuarios.InicializarSettingsUserControl(
  AUserControl: TUserControl);
var
  SourceSettings : TUCSettings;
begin
  SourceSettings := UCSettingsSpanish;

  with AUserControl do
  begin
    with UserSettings.CommonMessages do
    begin
      BlankPassword         := SourceSettings.CommonMessages.BlankPassword;
      PasswordChanged       := SourceSettings.CommonMessages.PasswordChanged;
      InitialMessage.Text   := SourceSettings.CommonMessages.InitialMessage.Text;
      MaxLoginAttemptsError := SourceSettings.CommonMessages.MaxLoginAttemptsError;
      InvalidLogin          := SourceSettings.CommonMessages.InvalidLogin;
      AutoLogonError        := SourceSettings.CommonMessages.AutoLogonError;
      UsuarioExiste         := SourceSettings.CommonMessages.UsuarioExiste; // Luiz Benevenuto 20/04/06
      PasswordExpired       := SourceSettings.CommonMessages.PasswordExpired; // vicente barros leonel
      ForcaTrocaSenha       := SourceSettings.CommonMessages.ForcaTrocaSenha;
    end;

    with UserSettings.Login do
    begin
      BtCancel        := SourceSettings.Login.BtCancel;
      BtOK            := SourceSettings.Login.BtOK;
      LabelPassword   := SourceSettings.Login.LabelPassword;
      LabelUser       := SourceSettings.Login.LabelUser;
      WindowCaption   := SourceSettings.Login.WindowCaption;
      LabelTentativa  := SourceSettings.Login.LabelTentativa;
      LabelTentativas := SourceSettings.Login.LabelTentativas;

      if Assigned(SourceSettings.Login.LeftImage.Bitmap) then
        LeftImage.Bitmap := SourceSettings.Login.LeftImage.Bitmap
      else
        LeftImage.Bitmap := nil;

      if Assigned(SourceSettings.Login.TopImage.Bitmap) then
        TopImage.Bitmap := SourceSettings.Login.TopImage.Bitmap
      else
        TopImage.Bitmap := nil;

      if Assigned(SourceSettings.Login.BottomImage.Bitmap) then
        BottomImage.Bitmap := SourceSettings.Login.BottomImage.Bitmap
      else
        BottomImage.Bitmap := nil;
    end;

    with UserSettings.UsersForm do
    begin
      WindowCaption              := SourceSettings.UsersForm.WindowCaption;
      LabelDescription           := SourceSettings.UsersForm.LabelDescription;
      ColName                    := SourceSettings.UsersForm.ColName;
      ColLogin                   := SourceSettings.UsersForm.ColLogin;
      ColEmail                   := SourceSettings.UsersForm.ColEmail;
      BtAdd                      := SourceSettings.UsersForm.BtAdd;
      BtChange                   := SourceSettings.UsersForm.BtChange;
      BtDelete                   := SourceSettings.UsersForm.BtDelete;
      BtRights                   := SourceSettings.UsersForm.BtRights;
      BtPassword                 := SourceSettings.UsersForm.BtPassword;
      BtClose                    := SourceSettings.UsersForm.BtClose;
      PromptDelete               := SourceSettings.UsersForm.PromptDelete;
      PromptDelete_WindowCaption := SourceSettings.UsersForm.PromptDelete_WindowCaption; //added by fduenas
    end;

    with UserSettings.UsersProfile do
    begin
      WindowCaption              := SourceSettings.UsersProfile.WindowCaption;
      LabelDescription           := SourceSettings.UsersProfile.LabelDescription;
      ColProfile                 := SourceSettings.UsersProfile.ColProfile;
      BtAdd                      := SourceSettings.UsersProfile.BtAdd;
      BtChange                   := SourceSettings.UsersProfile.BtChange;
      BtDelete                   := SourceSettings.UsersProfile.BtDelete;
      BtRights                   := SourceSettings.UsersProfile.BtRights; //added by fduenas
      BtClose                    := SourceSettings.UsersProfile.BtClose;
      PromptDelete               := SourceSettings.UsersProfile.PromptDelete;
      PromptDelete_WindowCaption := SourceSettings.UsersProfile.PromptDelete_WindowCaption; //added by fduenas
    end;

    with UserSettings.AddChangeUser do
    begin
      WindowCaption   := SourceSettings.AddChangeUser.WindowCaption;
      LabelAdd        := SourceSettings.AddChangeUser.LabelAdd;
      LabelChange     := SourceSettings.AddChangeUser.LabelChange;
      LabelName       := SourceSettings.AddChangeUser.LabelName;
      LabelLogin      := SourceSettings.AddChangeUser.LabelLogin;
      LabelEmail      := SourceSettings.AddChangeUser.LabelEmail;
      CheckPrivileged := SourceSettings.AddChangeUser.CheckPrivileged;
      BtSave          := SourceSettings.AddChangeUser.BtSave;
      BtCancel        := SourceSettings.AddChangeUser.BtCancel;
      CheckExpira     := SourceSettings.AddChangeUser.CheckExpira;
      Day             := SourceSettings.AddChangeUser.Day;
      ExpiredIn       := SourceSettings.AddChangeUser.ExpiredIn;
    end;

    with UserSettings.AddChangeProfile do
    begin
      WindowCaption := SourceSettings.AddChangeProfile.WindowCaption;
      LabelAdd      := SourceSettings.AddChangeProfile.LabelAdd;
      LabelChange   := SourceSettings.AddChangeProfile.LabelChange;
      LabelName     := SourceSettings.AddChangeProfile.LabelName;
      BtSave        := SourceSettings.AddChangeProfile.BtSave;
      BtCancel      := SourceSettings.AddChangeProfile.BtCancel;
    end;

    with UserSettings.Rights do
    begin
      WindowCaption := SourceSettings.Rights.WindowCaption;
      LabelUser     := SourceSettings.Rights.LabelUser;
      LabelProfile  := SourceSettings.Rights.LabelProfile;
      PageMenu      := SourceSettings.Rights.PageMenu;
      PageActions   := SourceSettings.Rights.PageActions;
      PageControls  := SourceSettings.Rights.PageControls;
      BtUnlock      := SourceSettings.Rights.BtUnlock;
      BtLock        := SourceSettings.Rights.BtLock;
      BtSave        := SourceSettings.Rights.BtSave;
      BtCancel      := SourceSettings.Rights.BtCancel;
    end;

    with UserSettings.ChangePassword do
    begin
      WindowCaption        := SourceSettings.ChangePassword.WindowCaption;
      LabelDescription     := SourceSettings.ChangePassword.LabelDescription;
      LabelCurrentPassword := SourceSettings.ChangePassword.LabelCurrentPassword;
      LabelNewPassword     := SourceSettings.ChangePassword.LabelNewPassword;
      LabelConfirm         := SourceSettings.ChangePassword.LabelConfirm;
      BtSave               := SourceSettings.ChangePassword.BtSave;
      BtCancel             := SourceSettings.ChangePassword.BtCancel;
    end;

    with UserSettings.CommonMessages.ChangePasswordError do
    begin
      InvalidCurrentPassword := SourceSettings.CommonMessages.ChangePasswordError.InvalidCurrentPassword;
      NewPasswordError       := SourceSettings.CommonMessages.ChangePasswordError.NewPasswordError;
      NewEqualCurrent        := SourceSettings.CommonMessages.ChangePasswordError.NewEqualCurrent;
      PasswordRequired       := SourceSettings.CommonMessages.ChangePasswordError.PasswordRequired;
      MinPasswordLength      := SourceSettings.CommonMessages.ChangePasswordError.MinPasswordLength;
      InvalidNewPassword     := SourceSettings.CommonMessages.ChangePasswordError.InvalidNewPassword;
    end;

    with UserSettings.ResetPassword do
    begin
      WindowCaption := SourceSettings.ResetPassword.WindowCaption;
      LabelPassword := SourceSettings.ResetPassword.LabelPassword;
    end;

    with UserSettings.Log do
    begin
      WindowCaption              := SourceSettings.Log.WindowCaption;
      LabelDescription           := SourceSettings.Log.LabelDescription;
      LabelUser                  := SourceSettings.Log.LabelUser;
      LabelDate                  := SourceSettings.Log.LabelDate;
      LabelLevel                 := SourceSettings.Log.LabelLevel;
      ColLevel                   := SourceSettings.Log.ColLevel;
      ColMessage                 := SourceSettings.Log.ColMessage;
      ColUser                    := SourceSettings.Log.ColUser;
      ColDate                    := SourceSettings.Log.ColDate;
      BtFilter                   := SourceSettings.Log.BtFilter;
      BtDelete                   := SourceSettings.Log.BtDelete;
      BtClose                    := SourceSettings.Log.BtClose;
      PromptDelete               := SourceSettings.Log.PromptDelete;
      PromptDelete_WindowCaption := SourceSettings.Log.PromptDelete_WindowCaption; //added by fduenas
      OptionUserAll              := SourceSettings.Log.OptionUserAll;  //added by fduenas
      OptionLevelLow             := SourceSettings.Log.OptionLevelLow; //added by fduenas
      OptionLevelNormal          := SourceSettings.Log.OptionLevelNormal; //added by fduenas
      OptionLevelHigh            := SourceSettings.Log.OptionLevelHigh; //added by fduenas
      OptionLevelCritic          := SourceSettings.Log.OptionLevelCritic; //added by fduenas
      DeletePerformed            := SourceSettings.Log.DeletePerformed; //added by fduenas
    end;

    with UserSettings.AppMessages do
    begin
      MsgsForm_BtNew                            := SourceSettings.AppMessages.MsgsForm_BtNew;
      MsgsForm_BtReplay                         := SourceSettings.AppMessages.MsgsForm_BtReplay;
      MsgsForm_BtForward                        := SourceSettings.AppMessages.MsgsForm_BtForward;
      MsgsForm_BtDelete                         := SourceSettings.AppMessages.MsgsForm_BtDelete;
      MsgsForm_BtClose                          := SourceSettings.AppMessages.MsgsForm_BtClose; //added by fduenas
      MsgsForm_WindowCaption                    := SourceSettings.AppMessages.MsgsForm_WindowCaption;
      MsgsForm_ColFrom                          := SourceSettings.AppMessages.MsgsForm_ColFrom;
      MsgsForm_ColSubject                       := SourceSettings.AppMessages.MsgsForm_ColSubject;
      MsgsForm_ColDate                          := SourceSettings.AppMessages.MsgsForm_ColDate;
      MsgsForm_PromptDelete                     := SourceSettings.AppMessages.MsgsForm_PromptDelete;
      MsgsForm_PromptDelete_WindowCaption       := SourceSettings.AppMessages.MsgsForm_PromptDelete_WindowCaption; //added by fduenas
      MsgsForm_NoMessagesSelected               := SourceSettings.AppMessages.MsgsForm_NoMessagesSelected; //added by fduenas
      MsgsForm_NoMessagesSelected_WindowCaption := SourceSettings.AppMessages.MsgsForm_NoMessagesSelected_WindowCaption; //added by fduenas

      MsgRec_BtClose           := SourceSettings.AppMessages.MsgRec_BtClose;
      MsgRec_WindowCaption     := SourceSettings.AppMessages.MsgRec_WindowCaption;
      MsgRec_Title             := SourceSettings.AppMessages.MsgRec_Title;
      MsgRec_LabelFrom         := SourceSettings.AppMessages.MsgRec_LabelFrom;
      MsgRec_LabelDate         := SourceSettings.AppMessages.MsgRec_LabelDate;
      MsgRec_LabelSubject      := SourceSettings.AppMessages.MsgRec_LabelSubject;
      MsgRec_LabelMessage      := SourceSettings.AppMessages.MsgRec_LabelMessage;
      MsgSend_BtSend           := SourceSettings.AppMessages.MsgSend_BtSend;
      MsgSend_BtCancel         := SourceSettings.AppMessages.MsgSend_BtCancel;
      MsgSend_WindowCaption    := SourceSettings.AppMessages.MsgSend_WindowCaption;
      MsgSend_Title            := SourceSettings.AppMessages.MsgSend_Title;
      MsgSend_GroupTo          := SourceSettings.AppMessages.MsgSend_GroupTo;
      MsgSend_RadioUser        := SourceSettings.AppMessages.MsgSend_RadioUser;
      MsgSend_RadioAll         := SourceSettings.AppMessages.MsgSend_RadioAll;
      MsgSend_GroupMessage     := SourceSettings.AppMessages.MsgSend_GroupMessage;
      MsgSend_LabelSubject     := SourceSettings.AppMessages.MsgSend_LabelSubject; //added by fduenas
      MsgSend_LabelMessageText := SourceSettings.AppMessages.MsgSend_LabelMessageText; //added by fduenas
    end;

    With UserSettings.History do
      Begin
        Evento_edit         := SourceSettings.History.Evento_edit;
        Evento_NewRecord    := SourceSettings.History.Evento_NewRecord;
        Evento_Insert       := SourceSettings.History.Evento_Insert;
        Evento_delete       := SourceSettings.History.Evento_Delete;
        LabelTabela         := SourceSettings.History.LabelTabela;
        Msg_LogEmptyHistory := SourceSettings.History.Msg_LogEmptyHistory;
        Msg_MensConfirma    := SourceSettings.History.Msg_MensConfirma;
        LabelDescricao      := SourceSettings.History.LabelDescricao;
        Hist_BtnExcluir     := SourceSettings.History.Hist_BtnExcluir;
        Hist_BtnFiltro      := SourceSettings.History.Hist_BtnFiltro;
        LabelForm           := SourceSettings.History.LabelForm;
        Hist_BtnFechar      := SourceSettings.History.Hist_BtnFechar;
        LabelDataEvento     := SourceSettings.History.LabelDataEvento;
        LabelEvento         := SourceSettings.History.LabelEvento;
        Msg_NewRecord       := SourceSettings.History.Msg_NewRecord;
        Hist_All            := SourceSettings.History.Hist_All;
        Msg_LimpHistorico   := SourceSettings.History.Msg_LimpHistorico;
        LabelHoraEvento     := SourceSettings.History.LabelHoraEvento;
        LabelUser           := SourceSettings.History.LabelUser;
        Hist_MsgExceptPropr := SourceSettings.History.Hist_MsgExceptPropr;
      End;

    with UserSettings.TypeFieldsDB do
      Begin
        Type_VarChar   := SourceSettings.TypeFieldsDB.Type_VarChar;
        Type_Char      := SourceSettings.TypeFieldsDB.Type_Char;
        Type_Int       := SourceSettings.TypeFieldsDB.Type_Int;
        Type_MemoField := SourceSettings.TypeFieldsDB.Type_MemoField;
      end;

    UserSettings.WindowsPosition := SourceSettings.WindowsPosition;
  end;
end;

procedure TDataModuleUsuarios.InicializarUserControl(AUserControl: TUserControl);
begin
  if Assigned(AUserControl) then
  begin
    InicializarCamposUserControl(AUserControl);
    InicializarSettingsUserControl(AUserControl);
  end;
end;


end.
