SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET row_security = off;


drop table if exists "DMP" cascade;
drop table if exists "DMPOrganisation" cascade;
drop table if exists "DMPProfile" cascade;
drop table if exists "DMPResearcher" cascade;
drop table if exists "Dataset" cascade;
drop table if exists "DatasetProfile" cascade;
drop table if exists "DatasetProfileRuleset" cascade;
drop table if exists "DatasetProfileViewstyle" cascade;
drop table if exists "Organisation" cascade;
drop table if exists "Project" cascade;
drop table if exists "Researcher" cascade;
drop table if exists "Service" cascade;
drop table if exists "DataRepository" cascade;
drop table if exists "Registry" cascade;
drop table if exists "DatasetService" cascade;
drop table if exists "DatasetRegistry" cascade;
drop table if exists "DatasetDataRepository" cascade;
DROP table if exists "UserDMP" cascade;
DROP table if exists "UserInfo" cascade;
DROP table if exists "UserAuth" cascade;



-- CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;


-- CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

SET search_path = public, pg_catalog;

SET default_tablespace = '';

SET default_with_oids = false;


CREATE TABLE "DMP" (
    "ID" uuid DEFAULT uuid_generate_v4() NOT NULL,
    "Previous" uuid,
    "Label" character varying(250) NOT NULL,
    "Version" integer NOT NULL,
    "Project" uuid NOT NULL,
    "ProfileData" xml,
    "Creator" uuid not null,
    "Status" smallint not null default 0,
    "Created" timestamp not null default NOW(),
    "Modified" timestamp not null default NOW(),
    "Description" text,
    "Profile" uuid
);


ALTER TABLE "DMP" OWNER TO dmptool;


COMMENT ON COLUMN "DMP"."ProfileData" IS 'More data about the DMP as defined by the profile';


CREATE TABLE "DMPOrganisation" (
    "DMP" uuid NOT NULL,
    "Organisation" uuid NOT NULL,
    "Role" integer,
    "ID" uuid DEFAULT uuid_generate_v4() NOT NULL
);


ALTER TABLE "DMPOrganisation" OWNER TO dmptool;


COMMENT ON TABLE "DMPOrganisation" IS 'Linking of DMPs to Organisations';


COMMENT ON COLUMN "DMPOrganisation"."Role" IS 'Enumerator of roles';


CREATE TABLE "DMPProfile" (
    "ID" uuid  DEFAULT uuid_generate_v4() NOT NULL,
    "Label" character varying(250) NOT NULL,
    "Status" smallint not null default 0,
    "Created" timestamp not null default NOW(),
    "Modified" timestamp not null default NOW(),
    "Definition" xml
);


ALTER TABLE "DMPProfile" OWNER TO dmptool;


CREATE TABLE "DMPResearcher" (
    "DMP" uuid NOT NULL,
    "Researcher" uuid NOT NULL,
    "Role" integer,
    "ID" uuid DEFAULT uuid_generate_v4() NOT NULL
);


ALTER TABLE "DMPResearcher" OWNER TO dmptool;

COMMENT ON TABLE "DMPResearcher" IS 'Linking of DMPs to researchers';

COMMENT ON COLUMN "DMPResearcher"."Role" IS 'Enumerator of roles';


CREATE TABLE "Dataset" (
    "ID" uuid  DEFAULT uuid_generate_v4() NOT NULL,
    "Label" character varying(250) NOT NULL,
    "DMP" uuid,
    "Uri" character varying(250),
    "Properties" xml,
    "Reference" xml,
    "Creator" uuid not null,
    "Status" smallint not null default 0,
    "Created" timestamp not null default NOW(),
    "Modified" timestamp not null default NOW(),
    "Description" text,
    "Profile" uuid
);


ALTER TABLE "Dataset" OWNER TO dmptool;


COMMENT ON COLUMN "Dataset"."Uri" IS 'URI of item';


COMMENT ON COLUMN "Dataset"."Properties" IS 'More data about the dataset such as Uri, data types etc as defined by the profile';


CREATE TABLE "DatasetProfile" (
    "ID" uuid  DEFAULT uuid_generate_v4() NOT NULL,
    "Label" character varying(250) NOT NULL,
	"Ruleset" uuid,
	"Viewstyle" uuid,
	"Status" smallint not null default 0,
    "Created" timestamp not null default NOW(),
    "Modified" timestamp not null default NOW(),
    "Description" text,
    "Definition" xml NOT NULL
);

-- SHould also force the 1-1 relation between the datasetprofile <-> ruleset and datasetprofile <-> viewstyle
ALTER TABLE "DatasetProfile" ADD CONSTRAINT datasetprofile_unique_ruleset UNIQUE ("Ruleset");
ALTER TABLE "DatasetProfile" ADD CONSTRAINT datasetprofile_unique_viewstyle UNIQUE ("Viewstyle");

ALTER TABLE "DatasetProfile" OWNER TO dmptool;


COMMENT ON TABLE "DatasetProfile" IS 'Profiles for dmp datasets';


CREATE TABLE "DatasetProfileRuleset" (
    "ID" uuid  DEFAULT uuid_generate_v4() NOT NULL,
    "Label" character varying(250) NOT NULL,
    "Definition" xml NOT NULL
);

ALTER TABLE "DatasetProfileRuleset" OWNER TO dmptool;

COMMENT ON TABLE "DatasetProfileRuleset" IS 'Sets of Rules for dmp dataset profiles';


CREATE TABLE "DatasetProfileViewstyle" (
    "ID" uuid  DEFAULT uuid_generate_v4() NOT NULL,
    "Label" character varying(250) NOT NULL,
    "Definition" xml NOT NULL
);

ALTER TABLE "DatasetProfileViewstyle" OWNER TO dmptool;

COMMENT ON TABLE "DatasetProfileViewstyle" IS 'Style sets for dmp dataset profiles';


CREATE TABLE "Organisation" (
    "ID" uuid  DEFAULT uuid_generate_v4() NOT NULL,
    "Label" character varying(250) NOT NULL,
    "Abbreviation" character varying(50),
    "Reference" xml,
    "Uri" character varying(250),
    "Status" smallint not null default 0,
    "Created" timestamp not null default NOW(),
    "Modified" timestamp not null default NOW(),
    "Definition" xml
);


ALTER TABLE "Organisation" OWNER TO dmptool;

COMMENT ON TABLE "Organisation" IS 'Table of organizations utilized in the project';

COMMENT ON COLUMN "Organisation"."ID" IS 'Unique identifier and primary key of item';

COMMENT ON COLUMN "Organisation"."Label" IS 'A human readable long label of the item';

COMMENT ON COLUMN "Organisation"."Abbreviation" IS 'A human readable abbreviation of the item';

COMMENT ON COLUMN "Organisation"."Reference" IS 'Reference to the URI of the item along with information to allow how the item reached the system (e.g. via an external vocabulary)';

COMMENT ON COLUMN "Organisation"."Uri" IS 'URI of item';

COMMENT ON COLUMN "Organisation"."Definition" IS 'More data about the Organisation such as web site, type etc';



CREATE TABLE "Project" (
    "ID" uuid  DEFAULT uuid_generate_v4() NOT NULL,
    "Label" character varying(250) NOT NULL,
    "Abbreviation" character varying(50),
    "Reference" xml,
    "Uri" character varying(250),
    "CreationUser" uuid not null,
    "Status" smallint not null default 0,
    "Created" timestamp not null default NOW(),
    "Modified" timestamp not null default NOW(),
    "StartDate" timestamp,
    "EndDate" timestamp,
    "Description" text,
    "Definition" xml
);


ALTER TABLE "Project" OWNER TO dmptool;


COMMENT ON TABLE "Project" IS 'Table of project managed in the system';


COMMENT ON COLUMN "Project"."ID" IS 'Unique identifier and primary key of item';


COMMENT ON COLUMN "Project"."Label" IS 'A human readable long label of the item';



COMMENT ON COLUMN "Project"."Abbreviation" IS 'A human readable abbreviation of the item';


COMMENT ON COLUMN "Project"."Reference" IS 'Additional reference data for the item along with information to allow how the item reached the system (e.g. via an external vocabulary)';



COMMENT ON COLUMN "Project"."Uri" IS 'URI of item';



COMMENT ON COLUMN "Project"."Definition" IS 'More data about the project such as web site, start/stop, etc';



CREATE TABLE "Researcher" (
    "ID" uuid  DEFAULT uuid_generate_v4() NOT NULL,
    "Label" character varying(250) NOT NULL,
    "Uri" character varying(250),
    "PrimaryEmail" character varying(250),
    "Definition" xml,
    "Status" smallint not null default 0,
    "Created" timestamp not null default NOW(),
    "Modified" timestamp not null default NOW(),
    "Reference" xml
);


ALTER TABLE "Researcher" OWNER TO dmptool;



COMMENT ON TABLE "Researcher" IS 'Table of Researcher managed in the system';



COMMENT ON COLUMN "Researcher"."ID" IS 'Unique identifier and primary key of item';



COMMENT ON COLUMN "Researcher"."Label" IS 'Full name of the researcher (as presented by the system, and composed automatically by data or provided by the reference service)';



COMMENT ON COLUMN "Researcher"."Uri" IS 'URI of item';



COMMENT ON COLUMN "Researcher"."Definition" IS 'More data about the researcher such as: email addresses, affiliations etc';


COMMENT ON COLUMN "Researcher"."Reference" IS 'Additional reference data for the item along with information to allow how the item reached the system (e.g. via an external vocabulary)';


ALTER TABLE ONLY "DMPProfile"
    ADD CONSTRAINT "DMPPRofile_pkey" PRIMARY KEY ("ID");


ALTER TABLE ONLY "DMP"
    ADD CONSTRAINT "DMP_pkey" PRIMARY KEY ("ID");


ALTER TABLE ONLY "DatasetProfile"
    ADD CONSTRAINT "DatasetProfile_pkey" PRIMARY KEY ("ID");


ALTER TABLE ONLY "DatasetProfileRuleset"
    ADD CONSTRAINT "DatasetProfileRuleset_pkey" PRIMARY KEY ("ID");


ALTER TABLE ONLY "DatasetProfileViewstyle"
    ADD CONSTRAINT "DatasetProfileViewstyle_pkey" PRIMARY KEY ("ID");


ALTER TABLE ONLY "Dataset"
    ADD CONSTRAINT "Dataset_pkey" PRIMARY KEY ("ID");


ALTER TABLE ONLY "Organisation"
    ADD CONSTRAINT "Organisation_pkey" PRIMARY KEY ("ID");

ALTER TABLE ONLY "DMPOrganisation"
    ADD CONSTRAINT "PKey_DMPOrganisation" PRIMARY KEY ("ID");

ALTER TABLE ONLY "DMPResearcher"
    ADD CONSTRAINT "PKey_DMPResearcher" PRIMARY KEY ("ID");


ALTER TABLE ONLY "Project"
    ADD CONSTRAINT "Project_pkey" PRIMARY KEY ("ID");


ALTER TABLE ONLY "Researcher"
    ADD CONSTRAINT "Researcher_pkey" PRIMARY KEY ("ID");


CREATE INDEX "fki_DMPDMPProfileReference" ON "DMP" USING btree ("Profile");

CREATE INDEX "fki_DatasetDatasetProfileReference" ON "Dataset" USING btree ("Profile");


ALTER TABLE ONLY "DMP"
    ADD CONSTRAINT "DMPDMPProfileReference" FOREIGN KEY ("Profile") REFERENCES "DMPProfile"("ID");


ALTER TABLE ONLY "DMPOrganisation"
    ADD CONSTRAINT "DMPOrganisationDMPReference" FOREIGN KEY ("Organisation") REFERENCES "Organisation"("ID");


ALTER TABLE ONLY "DMPOrganisation"
    ADD CONSTRAINT "DMPOrganisationOrganisationReference" FOREIGN KEY ("DMP") REFERENCES "DMP"("ID");


ALTER TABLE ONLY "DMP"
    ADD CONSTRAINT "DMPProjectReference" FOREIGN KEY ("Project") REFERENCES "Project"("ID");

ALTER TABLE ONLY "DMPResearcher"
    ADD CONSTRAINT "DMPResearcherDMPReference" FOREIGN KEY ("Researcher") REFERENCES "Researcher"("ID");


ALTER TABLE ONLY "DMPResearcher"
    ADD CONSTRAINT "DMPResearcherResearcherReference" FOREIGN KEY ("DMP") REFERENCES "DMP"("ID");


ALTER TABLE ONLY "Dataset"
    ADD CONSTRAINT "DatasetDatasetProfileReference" FOREIGN KEY ("Profile") REFERENCES "DatasetProfile"("ID");


ALTER TABLE ONLY "Dataset"
    ADD CONSTRAINT "DatasetDMPReference" FOREIGN KEY ("DMP") REFERENCES "DMP"("ID");


ALTER TABLE ONLY "DatasetProfile"
    ADD CONSTRAINT "DatasetProfileDatasetProfileRulesetReference" FOREIGN KEY ("Ruleset") REFERENCES "DatasetProfileRuleset"("ID");

ALTER TABLE ONLY "DatasetProfile"
    ADD CONSTRAINT "DatasetProfileDatasetProfileViewstyleReference" FOREIGN KEY ("Viewstyle") REFERENCES "DatasetProfileViewstyle"("ID");


CREATE TABLE "Service" (
    "ID" uuid  DEFAULT uuid_generate_v4() NOT NULL,
    "Label" character varying(250),
    "Abbreviation" character varying(50),
    "Reference" xml,
    "Uri" character varying(250),
    "Status" smallint not null default 0,
    "Created" timestamp not null default NOW(),
    "Modified" timestamp not null default NOW(),
    "Definition" xml
);

ALTER TABLE "Service" OWNER TO dmptool;

ALTER TABLE ONLY "Service"
    ADD CONSTRAINT "PKey_Service" PRIMARY KEY ("ID");


CREATE TABLE "DataRepository" (
    "ID" uuid  DEFAULT uuid_generate_v4() NOT NULL,
    "Label" character varying(250),
    "Abbreviation" character varying(50),
    "Reference" xml,
    "Uri" character varying(250),
    "Status" smallint not null default 0,
    "Created" timestamp not null default NOW(),
    "Modified" timestamp not null default NOW(),
    "Definition" xml
);

ALTER TABLE "DataRepository" OWNER TO dmptool;

ALTER TABLE ONLY "DataRepository"
    ADD CONSTRAINT "PKey_DataRepository" PRIMARY KEY ("ID");



CREATE TABLE "Registry" (
    "ID" uuid  DEFAULT uuid_generate_v4() NOT NULL,
    "Label" character varying(250),
    "Abbreviation" character varying(50),
    "Reference" xml,
    "Uri" character varying(250),
    "Status" smallint not null default 0,
    "Created" timestamp not null default NOW(),
    "Modified" timestamp not null default NOW(),
    "Definition" xml
);

ALTER TABLE "Registry" OWNER TO dmptool;

ALTER TABLE ONLY "Registry"
    ADD CONSTRAINT "PKey_Registry" PRIMARY KEY ("ID");



CREATE TABLE "DatasetService" (
    "Dataset" uuid NOT NULL,
    "Service" uuid NOT NULL,
    "Role" integer,
    "ID" uuid DEFAULT uuid_generate_v4() NOT NULL
);

ALTER TABLE "DatasetService" OWNER TO dmptool;

COMMENT ON TABLE "DatasetService" IS 'Linking Dataset to Service';


CREATE TABLE "DatasetRegistry" (
    "Dataset" uuid NOT NULL,
    "Registry" uuid NOT NULL,
    "Role" integer,
    "ID" uuid DEFAULT uuid_generate_v4() NOT NULL
);

ALTER TABLE "DatasetRegistry" OWNER TO dmptool;

COMMENT ON TABLE "DatasetRegistry" IS 'Linking Dataset to Registry';


CREATE TABLE "DatasetDataRepository" (
    "Dataset" uuid NOT NULL,
    "DataRepository" uuid NOT NULL,
    "Role" integer,
    "ID" uuid DEFAULT uuid_generate_v4() NOT NULL
);


ALTER TABLE "DatasetDataRepository" OWNER TO dmptool;

COMMENT ON TABLE "DatasetDataRepository" IS 'Linking Dataset to DataRepository';


ALTER TABLE ONLY "DatasetDataRepository"
    ADD CONSTRAINT "DatasetDataRepositoryDatasetReference" FOREIGN KEY ("Dataset") REFERENCES "Dataset"("ID");

ALTER TABLE ONLY "DatasetDataRepository"
    ADD CONSTRAINT "DatasetDataRepositoryDataRepositoryReference" FOREIGN KEY ("DataRepository") REFERENCES "DataRepository"("ID");


ALTER TABLE ONLY "DatasetRegistry"
    ADD CONSTRAINT "DatasetRegistryDatasetReference" FOREIGN KEY ("Dataset") REFERENCES "Dataset"("ID");

ALTER TABLE ONLY "DatasetRegistry"
    ADD CONSTRAINT "DatasetRegistryRegistryReference" FOREIGN KEY ("Registry") REFERENCES "Registry"("ID");


ALTER TABLE ONLY "DatasetService"
    ADD CONSTRAINT "DatasetServiceDatasetReference" FOREIGN KEY ("Dataset") REFERENCES "Dataset"("ID");

ALTER TABLE ONLY "DatasetService"
    ADD CONSTRAINT "DatasetServiceServiceReference" FOREIGN KEY ("Service") REFERENCES "Service"("ID");



CREATE TABLE "UserInfo" (
    "id" uuid  DEFAULT uuid_generate_v4() UNIQUE NOT NULL,
    "email" character varying(250) UNIQUE NOT NULL,
    "authorization_level" smallint NOT NULL,
    "usertype" smallint NOT NULL,
    "authentication" uuid,
    "verified_email" boolean,
    "name" character varying(250),
    "created" timestamp not null,
    "lastloggedin" timestamp,
    "additionalinfo" xml,
    PRIMARY KEY (id)
);

COMMENT ON COLUMN "UserInfo"."authorization_level" IS 'This stores the authorization level of the user: 0 admin, 1 user, being able to be extended furthermore';
COMMENT ON COLUMN "UserInfo"."usertype" IS 'This stores the type of user: 0 -> internal, 1 external';


CREATE TABLE "UserAuth" (
    "id" uuid  DEFAULT uuid_generate_v4() NOT NULL UNIQUE,
    "username" character varying(200) NOT NULL,
    "password" character varying(250) NOT NULL,
     PRIMARY KEY (username)
);

CREATE INDEX idx_userauth_username ON "UserAuth"(username);

ALTER TABLE "UserInfo" ADD CONSTRAINT fkey_userinfo_userauth FOREIGN KEY ("authentication") REFERENCES "UserAuth"(id);

COMMENT ON COLUMN "UserAuth"."password" IS 'This field stores a password hash';


create table "UserDMP" (
   "id" uuid  DEFAULT uuid_generate_v4() NOT NULL,
   "usr" uuid NOT NULL,
   "dmp" uuid NOT NUll,
   "role" integer
);

ALTER TABLE "UserDMP" ADD CONSTRAINT fkey_userdmp_user FOREIGN KEY (usr) REFERENCES "UserInfo"("id");
ALTER TABLE "UserDMP" ADD CONSTRAINT fkey_userdmp_dmp FOREIGN KEY (dmp) REFERENCES "DMP"("ID");


ALTER TABLE "UserInfo" OWNER TO dmptool;
ALTER TABLE "UserAuth" OWNER TO dmptool;
ALTER TABLE "UserDMP" OWNER TO dmptool;



REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM postgres;
GRANT ALL ON SCHEMA public TO postgres;
GRANT ALL ON SCHEMA public TO PUBLIC;


