diff --git a/db/0.services.sys_config.sql b/db/0.services.sys_config.sql new file mode 100644 index 0000000..bf32ff8 --- /dev/null +++ b/db/0.services.sys_config.sql @@ -0,0 +1,19 @@ +DO $$ +BEGIN + INSERT INTO users(email, firstname, surname, active) VALUES ('admin@wpd.com', 'admin', 'wpd', 1); + + INSERT INTO formsorigins(id, name, active) VALUES (DEFAULT, 'OFICIAL', 1); + INSERT INTO formsorigins(id, name, active) VALUES (DEFAULT, 'WP6.MobileApp', 1); + + INSERT INTO fieldsdatatypes(id, name, description, active) VALUES (DEFAULT, 'integer', 'data type integer', 1); + INSERT INTO fieldsdatatypes(id, name, description, active) VALUES (DEFAULT, 'text', 'data type text', 1); + INSERT INTO fieldsdatatypes(id, name, description, active) VALUES (DEFAULT, 'real', 'data type real', 1); + INSERT INTO fieldsdatatypes(id, name, description, active) VALUES (DEFAULT, 'timestamp', 'data type timestamp', 1); + INSERT INTO fieldsdatatypes(id, name, description, active) VALUES (DEFAULT, 'geom', 'data type geometric', 1); +END $$; + +--delete from formsfields; +--delete from fields; +--delete from forms; +--delete from fieldsdatatypes; +--delete from formsorigins; diff --git a/db/1.sys_config.sql b/db/1.sys_config.sql new file mode 100644 index 0000000..685d5f1 --- /dev/null +++ b/db/1.sys_config.sql @@ -0,0 +1,88 @@ +DO $$ +DECLARE +idformsoriginsinserted bigint; +idformsinserted bigint; +idfieldsinserted bigint; +BEGIN + SELECT id FROM formsorigins INTO idformsoriginsinserted WHERE name = 'WP6.MobileApp' and active = 1; + + --BEGIN FLOODZONES_FORM + INSERT INTO forms(idformsorigins, code, name, description, dtcreation, active) values (idformsoriginsinserted, "FLOODZONES_FORM", "Flood Zones", "Flood Zones Form", DateTime(), 1) + RETURNING id INTO idformsinserted; + + INSERT INTO fields(idfieldsdatatypes, name, description, fillingclue, active) + SELECT fdt.id, 'id', 'id From FloodZones Form', '', 1 FROM fieldsdatatypes fdt WHERE fdt.name = 'integer' + RETURNING id INTO idfieldsinserted; + + INSERT INTO formsfields(id, idforms, idfields, active) VALUES (DEFAULT, idformsinserted, idfieldsinserted, 1); + + INSERT INTO fields(idfieldsdatatypes, name, description, fillingclue, active) + SELECT fdt.id, 'description', 'description From FloodZones Form', '', 1 FROM fieldsdatatypes fdt WHERE fdt.name = 'text' + RETURNING id INTO idfieldsinserted; + + INSERT INTO formsfields(id, idforms, idfields, active) VALUES (DEFAULT, idformsinserted, idfieldsinserted, 1); + + INSERT INTO fields(idfieldsdatatypes, name, description, fillingclue, active) + SELECT fdt.id, 'Images', 'Images From FloodZones Form', '', 1 FROM fieldsdatatypes fdt WHERE fdt.name = 'text' + RETURNING id INTO idfieldsinserted; + + INSERT INTO formsfields(id, idforms, idfields, active) VALUES (DEFAULT, idformsinserted, idfieldsinserted, 1); + + INSERT INTO fields(idfieldsdatatypes, name, description, fillingclue, active) + SELECT fdt.id, 'Latitude', 'Latitude From FloodZones Form', '', 1 FROM fieldsdatatypes fdt WHERE fdt.name = 'real' + RETURNING id INTO idfieldsinserted; + + INSERT INTO formsfields(id, idforms, idfields, active) VALUES (DEFAULT, idformsinserted, idfieldsinserted, 1); + + INSERT INTO fields(idfieldsdatatypes, name, description, fillingclue, active) + SELECT fdt.id, 'Longitude', 'Longitude From FloodZones Form', '', 1 FROM fieldsdatatypes fdt WHERE fdt.name = 'real' + RETURNING id INTO idfieldsinserted; + + INSERT INTO formsfields(id, idforms, idfields, active) VALUES (DEFAULT, idformsinserted, idfieldsinserted, 1); + + INSERT INTO fields(idfieldsdatatypes, name, description, fillingclue, active) + SELECT fdt.id, 'Passable', 'Passable From FloodZones Form', '', 1 FROM fieldsdatatypes fdt WHERE fdt.name = 'integer' + RETURNING id INTO idfieldsinserted; + + INSERT INTO formsfields(id, idforms, idfields, active) VALUES (DEFAULT, idformsinserted, idfieldsinserted, 1); + + INSERT INTO fields(idfieldsdatatypes, name, description, fillingclue, active) + SELECT fdt.id, 'Date', 'Date From FloodZones Form', '', 1 FROM fieldsdatatypes fdt WHERE fdt.name = 'text' + RETURNING id INTO idfieldsinserted; + + INSERT INTO formsfields(id, idforms, idfields, active) VALUES (DEFAULT, idformsinserted, idfieldsinserted, 1); + + INSERT INTO fields(idfieldsdatatypes, name, description, fillingclue, active) + SELECT fdt.id, 'Time', 'Time From FloodZones Form', '', 1 FROM fieldsdatatypes fdt WHERE fdt.name = 'text' + RETURNING id INTO idfieldsinserted; + + INSERT INTO formsfields(id, idforms, idfields, active) VALUES (DEFAULT, idformsinserted, idfieldsinserted, 1); + + INSERT INTO fields(idfieldsdatatypes, name, description, fillingclue, active) + SELECT fdt.id, 'Address', 'Address From FloodZones Form', '', 1 FROM fieldsdatatypes fdt WHERE fdt.name = 'text' + RETURNING id INTO idfieldsinserted; + + INSERT INTO formsfields(id, idforms, idfields, active) VALUES (DEFAULT, idformsinserted, idfieldsinserted, 1); + --END FLOODZONES_FORM + + --BEGIN PLUVIOMETERS_FORM + INSERT INTO forms(idformsorigins, code, name, description, dtcreation, active) values (idformsoriginsinserted, "PLUVIOMETERS_FORM", "Pluviometers", "Pluviometers Form", DateTime(), 1); + --END PLUVIOMETERS_FORM + + --BEGIN RAIN_FORM + INSERT INTO forms(idformsorigins, code, name, description, dtcreation, active) values (idformsoriginsinserted, "RAIN_FORM", "Rain", "Rain zones Form", DateTime(), 1); + --END RAIN_FORM + + --BEGIN RIVERFLOOD_FORM + INSERT INTO forms(idformsorigins, code, name, description, dtcreation, active) values (idformsoriginsinserted, "RIVERFLOOD_FORM", "River Flood", "River Flood Form", DateTime(), 1); + --END RIVERFLOOD_FORM + + --BEGIN PLUVIOMETERS_REGISTRATION + INSERT INTO forms(idformsorigins, code, name, description, dtcreation, active) values (idformsoriginsinserted, "PLUVIOMETERS_REGISTRATION", "Pluviometer registration", "Pluviometer registration", DateTime(), 1); + --END PLUVIOMETERS_REGISTRATION + +END $$; + + + + diff --git a/db/services.ddl.sql b/db/services.ddl.sql new file mode 100644 index 0000000..502c5a0 --- /dev/null +++ b/db/services.ddl.sql @@ -0,0 +1,163 @@ +CREATE TABLE IF NOT EXISTS users ( + id SERIAL PRIMARY KEY, + Email VARCHAR(255) UNIQUE NOT NULL, + firstname VARCHAR(100) NOT NULL, + surname VARCHAR(100) NOT NULL, + avatar VARCHAR(100) NULL, + active INT NOT NULL +); + +CREATE TABLE IF NOT EXISTS profiles +( + id SERIAL PRIMARY KEY, + name VARCHAR(100) NOT NULL, + active INT NOT NULL +); + +CREATE TABLE IF NOT EXISTS usersprofiles +( + id SERIAL PRIMARY KEY, + idusers INT NOT NULL, + idprofiles INT NOT NULL, + active INT NOT NULL, + FOREIGN KEY (idusers) REFERENCES users (id), + FOREIGN KEY (idprofiles) REFERENCES profiles (id) +); + +CREATE TABLE IF NOT EXISTS permissions +( + id SERIAL PRIMARY KEY, + name VARCHAR(100) NOT NULL, + active INT NOT NULL +); + +CREATE TABLE IF NOT EXISTS profilespermissions +( + id SERIAL PRIMARY KEY, + idprofiles INT NOT NULL, + idpermissions INT NOT NULL, + active INT NOT NULL, + FOREIGN KEY (idprofiles) REFERENCES profiles (id), + FOREIGN KEY (idpermissions) REFERENCES permissions (id) +); + +CREATE TABLE IF NOT EXISTS formsorigins +( + id SERIAL PRIMARY KEY, + name VARCHAR(100) NOT NULL, + active INT NOT NULL +); + +CREATE TABLE IF NOT EXISTS forms +( + id SERIAL PRIMARY KEY, + idformsorigins INT NOT NULL, + code VARCHAR(50) UNIQUE NOT NULL, + name VARCHAR(100) NOT NULL, + description VARCHAR(100) NOT NULL, + dtcreation TIMESTAMP NOT NULL DEFAULT NOW(), + active INT NOT NULL, + FOREIGN KEY (idformsorigins) REFERENCES formsorigins (id) +); + +CREATE TABLE IF NOT EXISTS fieldsdatatypes +( + id SERIAL PRIMARY KEY, + name VARCHAR(100) NOT NULL, + description VARCHAR(100) NOT NULL, + active INT NOT NULL +); + +CREATE TABLE IF NOT EXISTS fields +( + id SERIAL PRIMARY KEY, + idfieldsdatatypes INT NOT NULL, + name VARCHAR(100) NOT NULL, + description VARCHAR(100) NOT NULL, + fillingclue VARCHAR(100) NOT NULL, + active INT NOT NULL, + FOREIGN KEY (idfieldsdatatypes) REFERENCES fieldsdatatypes (id) +); + +CREATE TABLE IF NOT EXISTS formsfields +( + id SERIAL PRIMARY KEY, + idforms INT NOT NULL, + idfields INT NOT NULL, + active INT NOT NULL, + FOREIGN KEY (idforms) REFERENCES forms (id), + FOREIGN KEY (idfields) REFERENCES fields (id) +); + +CREATE TABLE IF NOT EXISTS alternatives +( + id SERIAL PRIMARY KEY, + response VARCHAR(100) NOT NULL, + shortresponse VARCHAR(100) NOT NULL, + description VARCHAR(100) NOT NULL, + active INT NOT NULL +); + +CREATE TABLE IF NOT EXISTS fieldsalternatives +( + id SERIAL PRIMARY KEY, + idfields INT NOT NULL, + idalternatives INT NOT NULL, + active INT NOT NULL, + FOREIGN KEY (idfields) REFERENCES fields (id), + FOREIGN KEY (idalternatives) REFERENCES alternatives (id) +); + +CREATE TABLE IF NOT EXISTS formsanswers +( + id SERIAL PRIMARY KEY, + idforms INT NOT NULL, + idusersinformer INT NOT NULL, + dtfilling TIMESTAMP NOT NULL, + latitude NUMERIC NULL, + longitude NUMERIC NULL, + FOREIGN KEY (idforms) REFERENCES forms (id), + FOREIGN KEY (idusersinformer) REFERENCES users (id) +); + +CREATE TABLE IF NOT EXISTS fieldsanswers +( + id SERIAL PRIMARY KEY, + idfields INT NOT NULL, + idformsanswers INT NOT NULL, + value VARCHAR(100) NULL, + FOREIGN KEY (idfields) REFERENCES fields (id), + FOREIGN KEY (idformsanswers) REFERENCES formsanswers (id) +); +SELECT AddGeometryColumn('','fieldsanswers','geom','4326','MULTIPOLYGON',2); + +CREATE TABLE IF NOT EXISTS usersendorsementfieldsanswers +( + id SERIAL PRIMARY KEY, + idusersendorsement INT NOT NULL, + idfieldsanswers INT NOT NULL, + latitude NUMERIC NULL, + longitude NUMERIC NULL, + istrustable INT NOT NULL, + active INT NOT NULL, + FOREIGN KEY (idusersendorsement) REFERENCES users (id), + FOREIGN KEY (idfieldsanswers) REFERENCES fieldsanswers (id) +); + +CREATE TABLE IF NOT EXISTS preliminarydata +( + id SERIAL PRIMARY KEY, + idfieldsanswers INT NOT NULL, + dtinsert TIMESTAMP NOT NULL, + active INT NOT NULL, + FOREIGN KEY (idfieldsanswers) REFERENCES fieldsanswers (id) +); + +CREATE TABLE IF NOT EXISTS trusteddata +( + id SERIAL PRIMARY KEY, + idfieldsanswers INT NOT NULL, + dtinsert TIMESTAMP NOT NULL, + active INT NOT NULL, + FOREIGN KEY (idfieldsanswers) REFERENCES fieldsanswers (id) +); diff --git a/db/services.erd.pdf b/db/services.erd.pdf new file mode 100644 index 0000000..4c4f2ce Binary files /dev/null and b/db/services.erd.pdf differ