

procedure ConfiguraCDSFromDTO(pCDS: TClientDataSet; pDTOClass: TSQLRecord);
var
  J, K: Integer;

  TypeData: PTypeData;
  TypeInfo: PTypeInfo;
  PropList: PPropList;
  PropInfo: PPropInfo;

  Caminho, Nome, Tamanho, Tipo: String;

  Documento: TXMLDocument;
  Node: IXmlNode;
begin
  try
    TypeInfo := pDTOClass.ClassInfo;
    TypeData := GetTypeData(TypeInfo);

    // Lê no arquivo xml no disco
    Caminho := 'C:\ERP Lite\Delphi\Retaguarda\Comum\DTO\' + TypeData^.UnitName + '.xml';
    Documento.LoadFromFile(Caminho);
    Node := Documento.DocumentElement.ChildNodes['class'];

    // Configura ClientDataset
    pCDS.Close;
    pCDS.FieldDefs.Clear;
    pCDS.IndexDefs.Clear;

    // Preenche os nomes dos campos do CDS
    GetMem(PropList, TypeData^.PropCount * SizeOf(Pointer));
    GetPropInfos(TypeInfo, PropList);

    //Adiciona o ID
    pCDS.FieldDefs.add('ID', ftInteger);

    for J := 0 to (Node.ChildNodes.Count - 1) do
    begin

      //Adiciona os demais campos
      if Node.ChildNodes[J].NodeName = 'property' then
      begin
        for K := 0 to 4 do
        begin
          Nome := Node.ChildNodes[J].Attributes['column'].NodeValue;
          Tamanho := Node.ChildNodes[J].Attributes['width'].NodeValue;
          Tipo := Node.ChildNodes[J].Attributes['type'].NodeValue;
        end;

        if (Tipo = 'String') or (Tipo = 'Memo') then
          pCDS.FieldDefs.add(Nome, ftString, StrToInt(Tamanho))
        else if Tipo = 'FMTBcdField' then
          pCDS.FieldDefs.add(Nome, ftFloat)
        else if Tipo = 'Integer' then
          pCDS.FieldDefs.add(Nome, ftInteger)
        else if Tipo = 'Date' then
          pCDS.FieldDefs.add(Nome, ftDateTime);
      end;
    end;

    pCDS.CreateDataSet;

    (*
    Exercício:

    Arrume uma maneira de configurar automaticamente as máscaras.
    *)
  finally
    FreeMem(PropList);
  end;
end;

procedure ConfiguraGridFromDTO(pGrid: TJvDBUltimGrid; pDTOClass: TSQLRecord);
var
  J, K: Integer;

  TypeData: PTypeData;
  TypeInfo: PTypeInfo;
  PropList: PPropList;
  PropInfo: PPropInfo;

  Caminho, Nome, Tamanho, Tipo, Caption: String;

  Documento: TXMLDocument;
  Node: IXmlNode;
begin
  try
    TypeInfo := pDTOClass.ClassInfo;
    TypeData := GetTypeData(TypeInfo);

    // Lê no arquivo xml no disco
    Caminho := 'C:\ERP Lite\Delphi\Retaguarda\Comum\DTO\' + TypeData^.UnitName + '.xml';
    Documento.LoadFromFile(Caminho);
    Node := Documento.DocumentElement.ChildNodes['class'];

    //Configura o ID
    AtualizaCaptionGrid(pGrid, 'ID', 'ID');

    // Configura a Grid
    GetMem(PropList, TypeData^.PropCount * SizeOf(Pointer));
    GetPropInfos(TypeInfo, PropList);
    for J := 0 to (Node.ChildNodes.Count - 1) do
    begin
      //Configura os demais campos
      if Node.ChildNodes[J].NodeName = 'property' then
      begin
        for K := 0 to 4 do
        begin
          Nome := Node.ChildNodes[J].Attributes['column'].NodeValue;
          Tamanho := Node.ChildNodes[J].Attributes['width'].NodeValue;
          Caption := Node.ChildNodes[J].Attributes['caption'].NodeValue;
        end;

        AtualizaCaptionGrid(pGrid, Nome, Caption);
        ConfiguraTamanhoColunaGrid(pGrid, Nome, StrToInt(Tamanho), Caption);
      end;
    end;

  finally
    FreeMem(PropList);
  end;
end;



function TrataNomeProperty(NomeCampo: String): String;
var
  I: Integer;
begin
  NomeCampo := UpperCase(Copy(NomeCampo, 1, 1)) + LowerCase
    (Copy(NomeCampo, 2, length(NomeCampo) - 1));

  for I := 1 to length(NomeCampo) do
  begin
    if copy(NomeCampo,I,1) = '_' then
    begin
      Delete(NomeCampo,I,1);
      NomeCampo := Copy(NomeCampo,1,I-1) + UpperCase(Copy(NomeCampo,I,1)) + LowerCase(Copy(NomeCampo,I+1,length(NomeCampo)-1));
    end;
  end;

  Result := NomeCampo;
end;

