create or replace trigger olaf_title_trg
	AFTER INSERT ON FABIAN_DATA
	FOR EACH ROW
	DECLARE
		VTag fabian_tag.name%type;
		VIssueID fabian_tag.id%type;
		VArticleID fabian_tag.id%type;
		VVolumeID	fabian_tag.id%type;
		VNumberID	fabian_tag.id%type;
	BEGIN
		select name into VTag from fabian_tag where id=:new.tagid;
		if (VTag = 'title') then
			if (:new.data like '%Database%') then
				select parent into VArticleID from fabian_ptr where child=:new.tagid;
				select parent into VIssueID  from fabian_ptr where child in (select parent from fabian_ptr where child=VArticleID);
				 
				select id into VVolumeID from fabian_tag where name='volume' and id in (select child from fabian_ptr where parent=VIssueID);
				select id into VNumberID from fabian_tag where  name='number' and id in (select child from fabian_ptr where parent=VIssueID);
				Insert into fabian_art (articleid, VolumeID, NumberID, title)  values (VArticleID, VVolumeID, VNumberID, :new.data);
			End if;
		end if;
	END;
/

CREATE OR REPLACE TRIGGER olaf_author_trg
	AFTER INSERT ON FABIAN_DATA
	FOR EACH ROW
	DECLARE
		errmsg VARCHAR2(255);
		VTagtype fabian_tag.name%type;
		VArticleID fabian_tag.id%type;
		VName fabian_data.data%type;
		VPosition fabian_attr.value%type;
	BEGIN
		select name into VTagtype from fabian_tag where id=:new.tagid;
		if (VTagtype = 'author') then
			select value into VPosition from fabian_attr where name='position' and tagid = :new.tagid;
			if (VPosition = '00') then 
				insert into log values (:new.tagid);
				select parent into VArticleID from fabian_ptr where child in (select parent from fabian_ptr where child=:new.tagid);
				update fabian_art set authorid=:new.tagid where articleid=VArticleID;
			end if;
		end if;
		EXCEPTION
			WHEN OTHERS THEN
				errmsg:= SQLERRM;
				insert into log values(errmsg);
	END;
/


-- CREATE OR REPLACE TRIGGER fabian_doc_tag_ref_trg
--	BEFORE DELETE ON FABIAN_DOC
--	BEGIN
--		delete from fabian_tag where docid is null;
--	END;
--/


-- CREATE OR REPLACE TRIGGER fabian_ptr_del_trg
--	BEFORE DELETE ON FABIAN_PTR
--	FOR EACH ROW
--	BEGIN
--		delete from fabian_tag where id=:old.child;
--	END;
--/