CREATE FOREIGN TABLE public."dataset_es"
(
    id bigint NOT NULL,
    title character varying(255),
    status integer,
    category character varying(255),
    description text,
    download_times bigint,
    license character varying(255),
    task character varying(255),
    release_id bigint,
    user_id bigint,
    repo_id bigint,
    created_unix bigint,
    updated_unix bigint,
    file_name text
)SERVER multicorn_es
OPTIONS
    (
        host '192.168.207.94',
        port '9200',
        index 'user_es-index',
        rowid_column 'id'
    )
;

CREATE OR REPLACE FUNCTION public.insert_dataset_data() RETURNS trigger AS 
$def$
    BEGIN
        INSERT INTO public.dataset_es(
	    id, 
        title, 
        status, 
        category, 
        description, 
        download_times, 
        license, task, 
        release_id, 
        user_id, 
        repo_id, 
        created_unix, 
        updated_unix)
	    VALUES (
        NEW.id, 
        NEW.title, 
        NEW.status, 
        NEW.category, 
        NEW.description, 
        NEW.download_times, 
        NEW.license, 
        NEW.task, 
        NEW.release_id, 
        NEW.user_id, 
        NEW.repo_id, 
        NEW.created_unix, 
        NEW.updated_unix
        );
 RETURN NEW;
    END;
$def$ 
LANGUAGE plpgsql;

DROP TRIGGER  IF EXISTS es_insert_dataset on public.dataset;

CREATE TRIGGER es_insert_dataset
    AFTER INSERT ON public.dataset
    FOR EACH ROW EXECUTE PROCEDURE insert_dataset_data();

CREATE OR REPLACE FUNCTION public.udpate_dataset_file_name() RETURNS trigger AS 
$def$
    BEGIN
       if (TG_OP = 'DELETE') then
	       update public.dataset_es  SET file_name=(select array_to_string(array_agg(name order by created_unix desc),',') from public.attachment where dataset_id=OLD.dataset_id order by created_unix desc) where id=OLD.dataset_id;
	   elsif (TG_OP = 'INSERT') then
	       update public.dataset_es  SET file_name=(select array_to_string(array_agg(name order by created_unix desc),',') from public.attachment where dataset_id=NEW.dataset_id order by created_unix desc) where id=NEW.dataset_id;
	   end if;
	   
       return null;
    END;
$def$ 
LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS es_udpate_dataset_file_name on public.attachment;
CREATE TRIGGER es_udpate_dataset_file_name
    AFTER INSERT OR DELETE ON public.attachment
    FOR EACH ROW EXECUTE PROCEDURE udpate_dataset_file_name();


CREATE OR REPLACE FUNCTION public.update_dataset_description() RETURNS trigger AS 
$def$
    declare
    BEGIN
	   UPDATE public.dataset_es  SET description=NEW.description where id=NEW.id;
       return new;
    END
$def$ 
LANGUAGE plpgsql;

DROP TRIGGER  IF EXISTS es_update_dataset_description on public.dataset;

CREATE TRIGGER es_update_dataset_description
    AFTER UPDATE OF "description" ON public.dataset
    FOR EACH ROW EXECUTE PROCEDURE update_dataset_description();


CREATE OR REPLACE FUNCTION public.update_dataset_title() RETURNS trigger AS 
$def$
    declare
    BEGIN
	   UPDATE public.dataset_es  SET title=NEW.title where id=NEW.id;
       return new;
    END
$def$ 
LANGUAGE plpgsql;

DROP TRIGGER  IF EXISTS es_update_dataset_title on public.dataset;

CREATE TRIGGER es_update_dataset_title
    AFTER UPDATE OF "title" ON public.dataset
    FOR EACH ROW EXECUTE PROCEDURE update_dataset_title();


CREATE OR REPLACE FUNCTION public.update_dataset_category() RETURNS trigger AS 
$def$
    declare
    BEGIN
	   UPDATE public.dataset_es  SET category=NEW.category where id=NEW.id;
       return new;
    END
$def$ 
LANGUAGE plpgsql;

DROP TRIGGER  IF EXISTS es_update_dataset_category on public.dataset;

CREATE TRIGGER es_update_dataset_category
    AFTER UPDATE OF "category" ON public.dataset
    FOR EACH ROW EXECUTE PROCEDURE update_dataset_category();


CREATE OR REPLACE FUNCTION public.delete_dataset() RETURNS trigger AS 
$def$
    declare
    BEGIN
	   DELETE FROM public.dataset_es  where id=OLD.id;
       return new;
    END
$def$ 
LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS es_delete_dataset on public.dataset;
CREATE TRIGGER es_delete_dataset
    AFTER DELETE ON public.dataset
    FOR EACH ROW EXECUTE PROCEDURE delete_dataset();


