CREATE VIEW resultvex2 AS (
	SELECT result.assignment, 
		result.type AS typeid, 
		resulttype.name AS type, 
		result.step AS stepid, 
		resultstep.name AS step, 
		result.updatedon,
		obs_concl.value AS conclusion,
		obs_mark_value.value AS mark_value,
		obs_mark_degradation.value AS mark_degradation,
		obs_mark_details.value AS mark_details,
		obs_decision.value AS decision,
		obs_decision_ex.value AS decision_ex,
		obs_comparison_need_better_known.value AS comparison_need_better_known,
		obs_comparison_l3details.value AS comparison_l3details,
		obs_comparison_qualquant.value AS comparison_qualquant
	FROM resulttype,resultstep,result 
		LEFT OUTER JOIN observation AS obs_concl ON obs_concl.assignment = result.assignment 
			AND obs_concl.restype = result.type 
			AND obs_concl.resstep = result.step 
			AND obs_concl.type = (SELECT id FROM observationtype WHERE name = 'conclusion') 
		LEFT OUTER JOIN observation AS obs_mark_value ON obs_mark_value.assignment = result.assignment 
			AND obs_mark_value.restype = result.type 
			AND obs_mark_value.resstep = result.step 
			AND obs_mark_value.type = (SELECT id FROM observationtype WHERE name = 'mark-value') 
		LEFT OUTER JOIN observation AS obs_mark_degradation ON obs_mark_degradation.assignment = result.assignment 
			AND obs_mark_degradation.restype = result.type 
			AND obs_mark_degradation.resstep = result.step 
			AND obs_mark_degradation.type = (SELECT id FROM observationtype WHERE name = 'mark-degradation') 
		LEFT OUTER JOIN observation AS obs_mark_details ON obs_mark_details.assignment = result.assignment 
			AND obs_mark_details.restype = result.type 
			AND obs_mark_details.resstep = result.step 
			AND obs_mark_details.type = (SELECT id FROM observationtype WHERE name = 'mark-details') 
		LEFT OUTER JOIN observation AS obs_decision ON obs_decision.assignment = result.assignment 
			AND obs_decision.restype = result.type 
			AND obs_decision.resstep = result.step 
			AND obs_decision.type = (SELECT id FROM observationtype WHERE name = 'comparison-decision') 
		LEFT OUTER JOIN observation AS obs_decision_ex ON obs_decision_ex.assignment = result.assignment 
			AND obs_decision_ex.restype = result.type 
			AND obs_decision_ex.resstep = result.step 
			AND obs_decision_ex.type = (SELECT id FROM observationtype WHERE name = 'comparison-decision-ex') 
		LEFT OUTER JOIN observation AS obs_comparison_need_better_known ON obs_comparison_need_better_known.assignment = result.assignment 
			AND obs_comparison_need_better_known.restype = result.type 
			AND obs_comparison_need_better_known.resstep = result.step 
			AND obs_comparison_need_better_known.type = (SELECT id FROM observationtype WHERE name = 'comparison-need-better-known') 
		LEFT OUTER JOIN observation AS obs_comparison_l3details ON obs_comparison_l3details.assignment = result.assignment 
			AND obs_comparison_l3details.restype = result.type 
			AND obs_comparison_l3details.resstep = result.step 
			AND obs_comparison_l3details.type = (SELECT id FROM observationtype WHERE name = 'comparison-l3details') 
		LEFT OUTER JOIN observation AS obs_comparison_qualquant ON obs_comparison_qualquant.assignment = result.assignment 
			AND obs_comparison_qualquant.restype = result.type 
			AND obs_comparison_qualquant.resstep = result.step 
			AND obs_comparison_qualquant.type = (SELECT id FROM observationtype WHERE name = 'comparison-qualquant') 
	WHERE result.type = resulttype.id 
		AND result.step = resultstep.id
);

CREATE TABLE messagetype (
	id serial UNIQUE NOT NULL,
	name text UNIQUE NOT NULL,
	PRIMARY KEY (id)
) WITHOUT OIDs;
INSERT INTO messagetype(name) VALUES (E'home');

CREATE TABLE messagevisibility (
	id serial UNIQUE NOT NULL,
	name text UNIQUE NOT NULL,
	PRIMARY KEY (id)
) WITHOUT OIDs;
INSERT INTO messagevisibility(name) VALUES (E'hidden');
INSERT INTO messagevisibility(name) VALUES (E'tutors');
INSERT INTO messagevisibility(name) VALUES (E'users');
INSERT INTO messagevisibility(name) VALUES (E'all');

CREATE TABLE message (
	FOREIGN KEY (id) REFERENCES object(id),
	id int UNIQUE NOT NULL,
	FOREIGN KEY (type) REFERENCES messagetype(id),
	type int NOT NULL,
	FOREIGN KEY (visibility) REFERENCES messagevisibility(id),
	visibility int NOT NULL,
	FOREIGN KEY (parent) REFERENCES message(id),
	parent int,
	title text NOT NULL,
	value text NOT NULL,
	PRIMARY KEY (id)
) WITHOUT OIDs;
CREATE TRIGGER message_object_Inherit_Trigger
 BEFORE INSERT ON message
 FOR EACH ROW
 EXECUTE PROCEDURE Generic_object_Inherit_Trigger_Hook();

CREATE VIEW messagev AS (
	SELECT message.id,messagetype.id AS typeid,messagetype.name AS type,message.visibility AS visibilityid,messagevisibility.name AS visibility,message.title,message.value,message.parent,object.createdon,object.creator AS creatorid,person.name AS creator FROM message,messagevisibility,messagetype,object,person WHERE person.id=object.creator AND object.id=message.id AND message.visibility=messagevisibility.id AND message.type=messagetype.id
);

INSERT INTO observationtype(name) VALUES ('mark-gp');
