CREATE TABLE "newsmon_article" (
    "id" serial NOT NULL,
    "created" timestamptz NOT NULL,
    "creator" integer NOT NULL,
    "url" character varying NOT NULL,
    "name" character varying NOT NULL,
    "annotation" character varying NULL,
    "source" integer NOT NULL,
    "author" character varying NULL,
    "public" boolean NOT NULL DEFAULT FALSE
);

COMMENT ON COLUMN "newsmon_article"."created" IS 'timestamp vytvorenia';

COMMENT ON COLUMN "newsmon_article"."creator" IS 'osoba ktora zaznam vytvorila (globis.person.id)';

COMMENT ON COLUMN "newsmon_article"."url" IS 'url clanku';

COMMENT ON COLUMN "newsmon_article"."name" IS 'nazov clanku';

COMMENT ON COLUMN "newsmon_article"."annotation" IS 'popis (spravidla zaujimavy kratky uryvok clanku)';

COMMENT ON COLUMN "newsmon_article"."source" IS 'id zdroja';

COMMENT ON COLUMN "newsmon_article"."author" IS 'meno autora';

COMMENT ON COLUMN "newsmon_article"."public" IS 'zverejnit na verejnom webe?';

ALTER TABLE "newsmon_article"
    ADD CONSTRAINT "newsmon_article_id" PRIMARY KEY ("id");

CREATE TABLE "newsmon_source" (
    "id" serial NOT NULL,
    "name" character varying NOT NULL,
    "url_regex" character varying NOT NULL,
    "username" character varying(80) NULL,
    "password" character varying(80) NULL,
    "handler" character varying(40) 'GenericWebPage'
);

COMMENT ON COLUMN "newsmon_source"."name" IS 'nazov zdroja';

COMMENT ON COLUMN "newsmon_source"."url_regex" IS 'regularny vyraz na rozpoznanie tohto zdroja';

COMMENT ON COLUMN "newsmon_source"."username" IS 'meno na prihlasovanie na zdroj, ak je potrebne';

COMMENT ON COLUMN "newsmon_source"."password" IS 'heslo';

ALTER TABLE "newsmon_source"
    ADD CONSTRAINT "newsmon_source_id" PRIMARY KEY ("id");

ALTER TABLE "newsmon_article"
    ADD FOREIGN KEY ("source") REFERENCES "newsmon_source" ("id") ON DELETE RESTRICT ON UPDATE CASCADE;

CREATE TABLE "newsmon_tag" (
    "id" serial NOT NULL,
    "name" character varying(80) NOT NULL,
    "comment" character varying NULL
);

COMMENT ON COLUMN "newsmon_tag"."name" IS 'nazov stitka';

ALTER TABLE "newsmon_tag"
    ADD CONSTRAINT "newsmon_tag_id" PRIMARY KEY ("id");

CREATE TABLE "newsmon_article_tag" (
    "id" serial NOT NULL,
    "article" integer NOT NULL,
    "tag" integer NOT NULL
);

COMMENT ON COLUMN "newsmon_article_tag"."article" IS 'id clanku';

COMMENT ON COLUMN "newsmon_article_tag"."tag" IS 'id stitka';

ALTER TABLE "newsmon_article_tag"
    ADD CONSTRAINT "newsmon_article_tag_id" PRIMARY KEY ("id");

ALTER TABLE "newsmon_article_tag"
    ADD FOREIGN KEY ("article") REFERENCES "newsmon_article" ("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "newsmon_article_tag"
    ADD FOREIGN KEY ("tag") REFERENCES "newsmon_tag" ("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- status: wait, process, failed, success, canceled
CREATE TABLE "newsmon_snapshot" (
    "id" serial NOT NULL,
    "article" integer NOT NULL,
    "processed" timestamptz NULL,
    "url" character varying NOT NULL,
    "file" character (80) NULL,
    "status" character varying(10) NOT NULL,
    "log" text NULL
);

COMMENT ON COLUMN "newsmon_snapshot"."article" IS 'id clanku';

COMMENT ON COLUMN "newsmon_snapshot"."processed" IS 'timestamp spracovania';

COMMENT ON COLUMN "newsmon_snapshot"."url" IS 'url snapshotu';

COMMENT ON COLUMN "newsmon_snapshot"."file" IS 'nazov suboru';

COMMENT ON COLUMN "newsmon_snapshot"."status" IS 'stav spracovania: wait, process, failed, success, canceled';

COMMENT ON COLUMN "newsmon_snapshot"."log" IS 'zaznam spracovania';

INSERT INTO "acl_high" ("name", "opgroup", "opname", "handler_class", "handler_param")
    VALUES ('Právo spravovať monitorovanie článkov nielen o ALPI', 'newsmon', 'admin', NULL, NULL);

