drop FOREIGN table if exists  public.repository_es;
CREATE FOREIGN TABLE public.repository_es (
    id bigint NOT NULL,
    owner_id bigint,
    owner_name character varying(255),
    lower_name character varying(255) NOT NULL,
    name character varying(255) NOT NULL,
    description text,
    website character varying(2048),
    original_service_type integer,
    original_url character varying(2048),
    default_branch character varying(255),
    num_watches integer,
    num_stars integer,
    num_forks integer,
    num_issues integer,
    num_closed_issues integer,
    num_pulls integer,
    num_closed_pulls integer,
    num_milestones integer DEFAULT 0 NOT NULL,
    num_closed_milestones integer DEFAULT 0 NOT NULL,
    is_private boolean,
    is_empty boolean,
    is_archived boolean,
    is_mirror boolean,
    status integer DEFAULT 0 NOT NULL,
    is_fork boolean DEFAULT false NOT NULL,
    fork_id bigint,
    is_template boolean DEFAULT false NOT NULL,
    template_id bigint,
    size bigint DEFAULT 0 NOT NULL,
    is_fsck_enabled boolean DEFAULT true NOT NULL,
    close_issues_via_commit_in_any_branch boolean DEFAULT false NOT NULL,
    topics text,
    avatar character varying(64),
    created_unix bigint,
    updated_unix bigint,
    contract_address character varying(255),
    block_chain_status integer DEFAULT 0 NOT NULL,
    balance character varying(255) DEFAULT '0'::character varying NOT NULL,
    clone_cnt bigint DEFAULT 0 NOT NULL,
    license character varying(100),
    download_cnt bigint DEFAULT 0 NOT NULL,
    num_commit bigint DEFAULT 0 NOT NULL,
    git_clone_cnt bigint DEFAULT 0 NOT NULL,
	lang character varying(2048)
) SERVER multicorn_es
OPTIONS
    (
        host '192.168.207.94',
        port '9200',
        index 'repository-es-index',
        rowid_column 'id',
        default_sort '_id'
    )
;
delete from  public.repository_es;
 INSERT INTO public.repository_es (id,
                                    owner_id,
                                    owner_name,
                                    lower_name,
                                    name,
                                    description,
                                    website,
                                    original_service_type,
                                    original_url,
                                    default_branch,
                                    num_watches,
                                    num_stars,
                                    num_forks,
                                    num_issues,
                                    num_closed_issues,
                                    num_pulls,
                                    num_closed_pulls,
                                    num_milestones,
                                    num_closed_milestones,
                                    is_private,
                                    is_empty,
                                    is_archived,
                                    is_mirror,
                                    status,
                                    is_fork,
                                    fork_id,
                                    is_template,
                                    template_id,
                                    size,
                                    is_fsck_enabled,
                                    close_issues_via_commit_in_any_branch,
                                    topics,
                                    avatar,
                                    created_unix,
                                    updated_unix,
                                    contract_address,
                                    block_chain_status,
                                    balance,
                                    clone_cnt,
                                    num_commit,
                                    git_clone_cnt) 
                                    SELECT
                                    id,
                                    owner_id,
                                    owner_name,
                                    lower_name,
                                    name,
                                    description,
                                    website,
                                    original_service_type,
                                    original_url,
                                    default_branch,
                                    num_watches,
                                    num_stars,
                                    num_forks,
                                    num_issues,
                                    num_closed_issues,
                                    num_pulls,
                                    num_closed_pulls,
                                    num_milestones,
                                    num_closed_milestones,
                                    is_private,
                                    is_empty,
                                    is_archived,
                                    is_mirror,
                                    status,
                                    is_fork,
                                    fork_id,
                                    is_template,
                                    template_id,
                                    size,
                                    is_fsck_enabled,
                                    close_issues_via_commit_in_any_branch,
                                    topics,
                                    avatar,
                                    created_unix,
                                    updated_unix,
                                    contract_address,
                                    block_chain_status,
                                    balance,
                                    clone_cnt,
                                    num_commit,
                                    git_clone_cnt
                                    FROM  public.repository;


CREATE OR REPLACE FUNCTION public.insert_repository_data() RETURNS trigger AS 
$def$
    BEGIN
        INSERT INTO public.repository_es (id,
                                    owner_id,
                                    owner_name,
                                    lower_name,
                                    name,
                                    description,
                                    website,
                                    original_service_type,
                                    original_url,
                                    default_branch,
                                    num_watches,
                                    num_stars,
                                    num_forks,
                                    num_issues,
                                    num_closed_issues,
                                    num_pulls,
                                    num_closed_pulls,
                                    num_milestones,
                                    num_closed_milestones,
                                    is_private,
                                    is_empty,
                                    is_archived,
                                    is_mirror,
                                    status,
                                    is_fork,
                                    fork_id,
                                    is_template,
                                    template_id,
                                    size,
                                    is_fsck_enabled,
                                    close_issues_via_commit_in_any_branch,
                                    topics,
                                    avatar,
                                    created_unix,
                                    updated_unix,
                                    contract_address,
                                    block_chain_status,
                                    balance,
                                    clone_cnt,
                                    num_commit,
                                    git_clone_cnt) VALUES
                                    (NEW.id,
                                    NEW.owner_id,
                                    NEW.owner_name,
                                    NEW.lower_name,
                                    NEW.name,
                                    NEW.description,
                                    NEW.website,
                                    NEW.original_service_type,
                                    NEW.original_url,
                                    NEW.default_branch,
                                    NEW.num_watches,
                                    NEW.num_stars,
                                    NEW.num_forks,
                                    NEW.num_issues,
                                    NEW.num_closed_issues,
                                    NEW.num_pulls,
                                    NEW.num_closed_pulls,
                                    NEW.num_milestones,
                                    NEW.num_closed_milestones,
                                    NEW.is_private,
                                    NEW.is_empty,
                                    NEW.is_archived,
                                    NEW.is_mirror,
                                    NEW.status,
                                    NEW.is_fork,
                                    NEW.fork_id,
                                    NEW.is_template,
                                    NEW.template_id,
                                    NEW.size,
                                    NEW.is_fsck_enabled,
                                    NEW.close_issues_via_commit_in_any_branch,
                                    NEW.topics,
                                    NEW.avatar,
                                    NEW.created_unix,
                                    NEW.updated_unix,
                                    NEW.contract_address,
                                    NEW.block_chain_status,
                                    NEW.balance,
                                    NEW.clone_cnt,
                                    NEW.num_commit,
                                    NEW.git_clone_cnt);
        RETURN NEW;
    END;
$def$ 
LANGUAGE plpgsql;

DROP TRIGGER  IF EXISTS es_insert_repository on public.repository;


CREATE TRIGGER es_insert_repository
    AFTER INSERT ON public.repository
    FOR EACH ROW EXECUTE PROCEDURE insert_repository_data();


CREATE OR REPLACE FUNCTION public.update_repository() RETURNS trigger AS 
$def$
    BEGIN
	  update public.repository_es SET description=NEW.description,
	   name=NEW.name,
	   lower_name=NEW.lower_name,
	   owner_name=NEW.owner_name,
	   website=NEW.website,
       updated_unix=NEW.updated_unix,
       num_watches=NEW.num_watches,
       num_stars=NEW.num_stars,
       num_forks=NEW.num_forks,
       topics=NEW.topics
	   where id=NEW.id;
       return new;
    END
$def$ 
LANGUAGE plpgsql;

DROP TRIGGER  IF EXISTS es_update_repository on public.repository;
CREATE TRIGGER es_update_repository
    AFTER UPDATE ON public.repository
    FOR EACH ROW EXECUTE PROCEDURE update_repository();


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

DROP TRIGGER IF EXISTS es_delete_repository on public.repository;
CREATE TRIGGER es_delete_repository
    AFTER DELETE ON public.repository
    FOR EACH ROW EXECUTE PROCEDURE delete_repository();


CREATE OR REPLACE FUNCTION public.udpate_repository_lang() RETURNS trigger AS 
$def$
    BEGIN
       if (TG_OP = 'DELETE') then
	       update public.repository_es  SET lang=(select string_agg(language, ',') from public.language_stat where repo_id=OLD.repo_id) where id=OLD.repo_id;
	   elsif (TG_OP = 'UPDATE') then
	       update public.repository_es  SET lang=(select string_agg(language, ',') from public.language_stat where repo_id=NEW.repo_id) where id=NEW.repo_id;
	   elsif (TG_OP = 'INSERT') then
	       update public.repository_es  SET lang=(select string_agg(language, ',') from public.language_stat where repo_id=NEW.repo_id) where id=NEW.repo_id;
	   end if;
	   
       return null;
    END;
$def$ 
LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS es_udpate_repository_lang on public.language_stat;
CREATE TRIGGER es_udpate_repository_lang
    AFTER INSERT OR UPDATE OR DELETE ON public.language_stat
    FOR EACH ROW EXECUTE PROCEDURE udpate_repository_lang();