CREATE SEQUENCE GTBHTML_ELEMENTO;

SET GENERATOR GTBHTML_ELEMENTO TO 12;


CREATE TRIGGER TTBHTML_ELEMENTO_B01I FOR TBHTML_ELEMENTO
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBHTML_ELEMENTO,1);
END;

CREATE TRIGGER TTBHTML_ELEMENTO_B02I FOR TBHTML_ELEMENTO
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'HTML incluido';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBHTML_ELEMENTO',NEW.ID, HISTORICO);
    END
end;

CREATE TRIGGER TTBHTML_ELEMENTO_B01U FOR TBHTML_ELEMENTO
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.DESCRICAO <> NEW.DESCRICAO) THEN
    BEGIN
        HISTORICO = HISTORICO||'Descricao:'|| OLD.DESCRICAO||'->'||NEW.DESCRICAO;
    END

    IF ( OLD.CONTEUDO <> NEW.CONTEUDO) THEN
    BEGIN
        HISTORICO = HISTORICO||',Conteudo HTML Alterado.';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBHTML_ELEMENTO',NEW.ID, HISTORICO);
    END
end;

CREATE TRIGGER TTBHTML_ELEMENTO_B01D FOR TBHTML_ELEMENTO
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Registro Excluido';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBHTML_ELEMENTO',old.ID, HISTORICO);
    END
end;



