#batch ../res/p2p/pu2_wrapper.cli
#batch res/p2p/pu2_wrapper.cli

$encoding = utf-8

if exists module pu1 then remove module pu2
\
add module pu2{ 
	type EmpType is record {
					id:integer;
					name:string;
					surname:string;
					sex:string;
					salary:real;
					info:string;
					birthdate: date;
					worksin:Dept[0..*];
					}

	type DeptType is record {
					id:integer;
					name:string;
					islocatedin:Loc[0..*];
					}

	type LocType is record {
					id:integer;
					name:string;
					}

	type CarType is record {
					id:integer;
					colour:string;
					year:integer;
					ismodel:Model[0..1];
					isownedby:Emp[0..1];
					}

	type ModelType is record {
					id:integer;
					name:string;
					ismake:Make[0..1];
					}

	type MakeType is record {
					id:integer;
					name:string;
					}

	Emp:EmpType[0..*];
	Dept:DeptType[0..*];
	Loc:LocType[0..*];
	Car:CarType[0..*];
	Model:ModelType[0..*];
	Make:MakeType[0..*];
	worksin:Dept[0..*];
	islocatedin:Loc[0..*];
	ismodel:Model[0..1];
	isownedby:Emp[0..1];
	ismake:Make[0..1];
	sampledate:date[0..*];
	samplecolour:string[0..*];
	sampleyear:integer[0..*];
	makename:string[0..*];

	init(){
	
		create permanent sampledate(1994-03-01 union 1994-09-22 union 1995-01-06 union 1996-07-11 union 1997-04-30 union 1997-06-15 union 1998-09-30 union 2001-12-20 union 2002-01-01 union 2003-06-14);   
		create samplecolour("Orange" union "Silver" union "Gold" union "Brown");
		create sampleyear(1989 union 1990 union 1980 union 2002 union 2003 union 2005);
		create makename("Mrecedes" union "Audi" union "Nissan" union "Volvo" union "Toyota" union "Mazda");
	
	
/*female emps*/
	   	create permanent Emp("ANNA" as name, "KOWALSKA" as surname, 
	   			01 as id, "female" as sex, 1000 as salary, "f" as info,
	   			random(sampledate) as birthdate);
	   	create permanent Emp("MARIA" as name, "NOWAK" as surname, 
	   			02 as id, "female" as sex, 1000 as salary, "f" as info,
	   			random(sampledate) as birthdate);
	   	create permanent Emp("KATARZYNA" as name, "GRABOWSKA" as surname, 
	   			03 as id, "female" as sex, 1000 as salary, "f" as info,
	   			random(sampledate) as birthdate);
	   	create permanent Emp("AGNIESZKA" as name, "MICHALAK" as surname, 
	   			04 as id, "female" as sex, 1000 as salary, "f" as info,
	   			random(sampledate) as birthdate);
	   	create permanent Emp("JOLANTA" as name, "MORAWSKA" as surname, 
	   			05 as id, "female" as sex, 1000 as salary, "f" as info,
	   			random(sampledate) as birthdate);

/*male emps*/
	   	create permanent Emp("JAN" as name, "KOWALSKI" as surname, 
	   			01 as id, "male" as sex, 1500 as salary, "m" as info,
	   			random(sampledate) as birthdate);
	   	create permanent Emp("ANDRZEJ" as name, "NOWAK" as surname, 
	   			02 as id, "male" as sex, 1500 as salary, "m" as info,
	   			random(sampledate) as birthdate);
	   	create permanent Emp("PIOTR" as name, "GRABOWSKI" as surname, 
	   			03 as id, "male" as sex, 1500 as salary, "m" as info,
	   			random(sampledate) as birthdate);
	   	create permanent Emp("KRZYSZTOF" as name, "MICHALAK" as surname, 
	   			04 as id, "male" as sex, 1500 as salary, "m" as info,
	   			random(sampledate) as birthdate);
	   	create permanent Emp("REMIGIUSZ" as name, "MORAWSKI" as surname, 
	   			05 as id, "male" as sex, 1500 as salary, "m" as info,
	   			random(sampledate) as birthdate);
	   			
/*depts*/	   			
	   	create permanent Dept("SPRZEDAŻ" as name, 01 as id );
	   	create permanent Dept("TRANSPORT" as name, 02 as id );
	   	create permanent Dept("MAGAZYN" as name, 03 as id );

/*locs*/
	   	create permanent Loc("Krakow" as name, 01 as id );

/*cars*/
	   	create permanent Car(01 as id, random(samplecolour) as colour, random(sampleyear) as year );
	   	create permanent Car(02 as id, random(samplecolour) as colour, random(sampleyear) as year );
	   	create permanent Car(03 as id, random(samplecolour) as colour, random(sampleyear) as year );
	   	create permanent Car(04 as id, random(samplecolour) as colour, random(sampleyear) as year );
	   	create permanent Car(05 as id, random(samplecolour) as colour, random(sampleyear) as year );
	   	create permanent Car(06 as id, random(samplecolour) as colour, random(sampleyear) as year );
	   	create permanent Car(07 as id, random(samplecolour) as colour, random(sampleyear) as year );
	   	create permanent Car(08 as id, random(samplecolour) as colour, random(sampleyear) as year );
	   	create permanent Car(09 as id, random(samplecolour) as colour, random(sampleyear) as year );
	   	create permanent Car(10 as id, random(samplecolour) as colour, random(sampleyear) as year );

/*models*/
	   	create permanent Model(01 as id, "VAN" as name );
	   	create permanent Model(02 as id, "COMBI" as name );
	   	create permanent Model(03 as id, "SEDAN" as name );

/*makers*/
	   	create permanent Make(01 as id, random(makename) as name );
	   	create permanent Make(02 as id, random(makename) as name );
	   	create permanent Make(03 as id, random(makename) as name );
	   	create permanent Make(04 as id, random(makename) as name );
	   	create permanent Make(05 as id, random(makename) as name );

/*worksin defs*/
		foreach (Emp) as e do {
					 e :< create worksin(ref(random(Dept)));
		   }
		   
/*islocatedin defs*/
		foreach (Dept) as d do {
					 d :< create islocatedin(ref(Loc where name = "Krakow"));
		   }

/*ismodel defs*/
		foreach (Car) as c do {
					 c :< create ismodel(ref(random(Model)));
		   }

/*isownedby defs*/
		foreach (Car) as cc do {
					 cc :< create isownedby(ref(random(Emp)));
		   }
		   
/*ismake defs*/
		foreach (Model) as m do {
					 m :< create ismake(ref(random(Make)));
			}

	}	
}
.
compile pu2
cm pu2
init();


\
add view EmployeeContribDef {
	virtual EmployeeContrib: record {
		idContrib: integer;
		nameContrib: string;
		surnameContrib: string;
		sexContrib: string;
		salaryContrib: real;
		infoContrib: string;
		birthDateContrib: date;
		worksInContrib: DepartmentContrib;
	}[0..*] ;
		
	seed : record {e: Emp;  }[0..*]  { return (Emp) as e; } 
		
	on_retrieve { 
	  return e.(
	  	id as idContrib, 
	  	name as nameContrib, 
	  	surname as surnameContrib, 
	  	sex as sexContrib, 
	  	salary as salaryContrib, 
	  	info as infoContrib, 
	  	birthdate as birthDateContrib),
	  	(DepartmentContrib where idContrib = (e.worksin.Dept.id)) as worksInContrib ;
	}	
	on_delete { 
		delete e; }
	
	on_update { e := value.(idContrib as id, nameContrib as name, surnameContrib as surname,
				sexContrib as sex, salaryContrib as salary, infoContrib as info, birthDateContrib as birthdate,
				ref(Dept where id = worksInContrib.idContrib) as worksin); }
	
	on_new { create permanent Emp(value.(idContrib as id, nameContrib as name,
				surnameContrib as surname, sexContrib as sex, salaryContrib as salary, infoContrib as info,
				 birthDateContrib as birthdate, ref(Dept where id = worksInContrib.idContrib) as worksin)); }
	
	
	
	view idContribDef {  
		virtual idContrib: integer;
		seed: record { _id: Emp.id; } { return e.id as _id; } 
		on_retrieve { return _id; }
		on_update {_id := value; }
	}
	view nameContribDef {  
		virtual nameContrib: string;
		seed: record { _name: Emp.name; } { return e.name as _name; } 
		on_retrieve { return _name; }
		on_update { _name := value; }
	}
	view surnameContribDef {  
		virtual surnameContrib: string;
		seed: record { _surname: Emp.surname; } { return e.surname as _surname; } 
		on_retrieve { return _surname; }
		on_update { _surname := value; }
	}
	view sexContribDef {  
		virtual sexContrib: string;
		seed: record { _sex: Emp.sex; } { return e.sex as _sex; } 
		on_retrieve { return _sex; }
		on_update { _sex := value; }
	}
	view salaryContribDef {  
		virtual salaryContrib: real;
		seed: record { _salary: Emp.salary; } { return e.salary as _salary; } 
		on_retrieve { return _salary; }
		on_update { _salary := value; }
	}
	view infoContribDef {  
		virtual infoContrib: string;
		seed: record { _info: Emp.info; } { return e.info as _info; } 
		on_retrieve { return _info; }
		on_update { _info := value; }
	}
	view birthDateContribDef {  
		virtual birthDateContrib: date;
		seed: record { _birthDate: Emp.birthdate; } { return e.birthdate as _birthDate; } 
		on_retrieve { return _birthDate; }
		on_update { _birthDate := value; }
	}
	view worksInContribDef {
		virtual worksInContrib: DepartmentContrib[0..*];
		seed: record { _worksIn: DepartmentContrib; }[0..*]
			{ return (DepartmentContrib where idContrib = (e.worksin.Dept.id)) as _worksIn; } 
		on_retrieve { return _worksIn;}
		on_navigate { return _worksIn;}
	}
}
.

\
add view DepartmentContribDef {
	virtual DepartmentContrib: record {
		idContrib: integer;
		nameContrib: string;
		isLocatedInContrib: LocationContrib;
	}[0..*] ;
		
	seed : record {d: Dept;  }[0..*]  { return (Dept) as d; } 
		
	on_retrieve { 
	  return d.(
	  	id as idContrib, 
	  	name as nameContrib),
	    (LocationContrib where idContrib = (d.islocatedin.Loc.id)) as isLocatedInContrib ;
	}	
	on_delete { 
		delete d; 
	}
	view idContribDef {  
		virtual idContrib: integer;
		seed: record { _id: Dept.id; } { return d.id as _id; } 
		on_retrieve { return _id; }
	}
	view nameContribDef {  
		virtual nameContrib: string;
		seed: record { _name: Dept.name; } { return d.name as _name; } 
		on_retrieve { return _name; }
		on_update { _name := value; }
	}
	view isLocatedInContribDef {
		virtual isLocatedInContrib: LocationContrib;
		seed: record { _isLocatedIn: LocationContrib; }
			{ return (LocationContrib where idContrib = (d.islocatedin.Loc.id)) as _isLocatedIn; } 
		on_retrieve { return _isLocatedIn;}
		on_navigate { return _isLocatedIn;}
	} 
}
.

\
add view LocationContribDef {
	virtual LocationContrib: record {
		idContrib: integer;
		nameContrib: string;
	}[0..*] ;
		
	seed : record {l: Loc;  }[0..*]  { return (Loc) as l; } 
		
	on_retrieve { 
	  return l.(
	  	id as idContrib, 
	  	name as nameContrib);
	}	
	on_delete { 
		delete l; 
	}
	view idContribDef {  
		virtual idContrib: integer;
		seed: record { _id: Loc.id; } { return l.id as _id; } 
		on_retrieve { return deref(_id); }
	}
	view nameContribDef {  
		virtual nameContrib: string;
		seed: record { _name: Loc.name; } { return l.name as _name; } 
		on_retrieve { return deref(_name); }
		on_update { _name := value; }
	}
}
.

\
add view CarContribDef {
	virtual CarContrib: record {
		idContrib: integer;
		colourContrib: string;
		yearContrib: integer;
		isModelContrib: ModelContrib;
		isOwnedByContrib: EmployeeContrib; 
	}[0..*] ;
		
	seed : record {c: Car;  }[0..*]  { return (Car) as c; } 
		
	on_retrieve { 
	  return c.(
	  	id as idContrib, 
	  	colour as colourContrib,
	  	year as yearContrib),
	  	(ModelContrib where idContrib = (c.ismodel.Model.id)) as isModelContrib,
	  	(EmployeeContrib where idContrib = (c.isownedby.Emp.id)) as isOwnedByContrib ;
	}	
	on_delete { 
		delete c; 
	}
	view idContribDef {  
		virtual idContrib: integer;
		seed: record { _id: Car.id; } { return c.id as _id; } 
		on_retrieve { return deref(_id); }
	}
	view colourContribDef {  
		virtual colourContrib: string;
		seed: record { _colour: Car.colour; } { return c.colour as _colour; } 
		on_retrieve { return deref(_colour); }
		on_update { _colour := value; }
	}
	view yearContribDef {  
		virtual yearContrib: integer;
		seed: record { _year: Car.year; } { return c.year as _year; } 
		on_retrieve { return deref(_year); }
		on_update { _year := value; }
	}
	view isModelContribDef {
		virtual isModelContrib: ModelContrib;
		seed: record { _isModel: ModelContrib; }
			{ return (ModelContrib where idContrib = (c.ismodel.Model.id)) as _isModel; } 
		on_retrieve { return _isModel;}
		on_navigate { return _isModel;}
	}
	view isOwnedByContribDef {
		virtual isOwnedByContrib: EmployeeContrib;
		seed: record { _isOwnedBy: EmployeeContrib; }
			{ return (EmployeeContrib where idContrib = (c.isownedby.Emp.id)) as _isOwnedBy; } 
		on_retrieve { return _isOwnedBy;}
		on_navigate { return _isOwnedBy;}
	}
}
.

\
add view ModelContribDef {
	virtual ModelContrib: record {
		idContrib: integer;
		nameContrib: string;
		isMakeContrib: MakeContrib;
	}[0..*] ;
		
	seed : record {m: Model;  }[0..*]  { return (Model) as m; } 
		
	on_retrieve { 
	  return m.(
	  	id as idContrib, 
	  	name as nameContrib),
	  	(MakeContrib where idContrib = (m.ismake.Make.id)) as isMakeContrib ;
	}	
	on_delete { 
		delete m; 
	}
	view idContribDef {  
		virtual idContrib: integer;
		seed: record { _id: Model.id; } { return m.id as _id; } 
		on_retrieve { return deref(_id); }
	}
	view nameContribDef {  
		virtual nameContrib: string;
		seed: record { _name: Model.name; } { return m.name as _name; } 
		on_retrieve { return deref(_name); }
		on_update { _name := value; }
	}
	view isMakeContribDef {
		virtual isMakeContrib: MakeContrib;
		seed: record { _isMake: MakeContrib; }
			{ return (MakeContrib where idContrib = (m.ismake.Make.id)) as _isMake; } 
		on_retrieve { return _isMake;}
		on_navigate { return _isMake;}
	}
}
.

\
add view MakeContribDef {
	virtual MakeContrib: record {
		idContrib: integer;
		nameContrib: string;
	}[0..*] ;
		
	seed : record {m: Make;  }[0..*]  { return (Make) as m; } 
		
	on_retrieve { 
	  return m.(
	  	id as idContrib, 
	  	name as nameContrib);
	}	
	on_delete { 
		delete m; 
	}
	view idContribDef {  
		virtual idContrib: integer;
		seed: record { _id: Make.id; } { return m.id as _id; } 
		on_retrieve { return deref(_id); }
	}
	view nameContribDef {  
		virtual nameContrib: string;
		seed: record { _name: Make.name; } { return m.name as _name; } 
		on_retrieve { return deref(_name); }
		on_update { _name := value; }
	}
}
.

compile .