CREATE TEMPORARY TABLE ADD_INITIAL_PAGE_TO_TOP_SECTION(SECTION INTEGER,FUNCTIONALITY INTEGER);

insert into ADD_INITIAL_PAGE_TO_TOP_SECTION(SECTION) 
SELECT C2.ID_INTERNAL FROM CONTENT C1, CONTENT C2, NODE N WHERE C1.OJB_CONCRETE_CLASS='net.sourceforge.fenixedu.domain.DepartmentSite' 
AND N.KEY_PARENT=C1.ID_INTERNAL AND N.KEY_CHILD=C2.ID_INTERNAL AND C2.NAME LIKE '%Topo%';

update ADD_INITIAL_PAGE_TO_TOP_SECTION set FUNCTIONALITY = (SELECT ID_INTERNAL FROM CONTENT WHERE CONTENT_ID='c150f816-fdde-102a-b5ba-0013d3b09da0');

update NODE SET NODE_ORDER = NODE_ORDER+1 WHERE KEY_PARENT = (SELECT SECTION FROM ADD_INITIAL_PAGE_TO_TOP_SECTION);


insert into NODE(OJB_CONCRETE_CLASS, KEY_PARENT, KEY_CHILD, NODE_ORDER, ASCENDING, VISIBLE) select 
'net.sourceforge.fenixedu.domain.contents.ExplicitOrderNode', SECTION, FUNCTIONALITY, '-1','1','1' FROM ADD_INITIAL_PAGE_TO_TOP_SECTION;

DROP TEMPORARY TABLE ADD_INITIAL_PAGE_TO_TOP_SECTION;

-- Add Initial Page to Pool also.

update CONTENT C, CONTENT C2, META_DOMAIN_OBJECT MDO set C.KEY_PORTAL=C2.ID_INTERNAL WHERE 
C.CONTENT_ID='c150f816-fdde-102a-b5ba-0013d3b09da0' AND C2.KEY_META_DOMAIN_OBJECT=MDO.ID_INTERNAL AND 
MDO.TYPE='net.sourceforge.fenixedu.domain.DepartmentSite';


-- Add Initial Content

update CONTENT C1, CONTENT C2, META_DOMAIN_OBJECT MDO SET C1.KEY_INITIAL_CONTENT=C2.ID_INTERNAL WHERE 
C1.OJB_CONCRETE_CLASS = 'net.sourceforge.fenixedu.domain.contents.MetaDomainObjectPortal' AND 
C2.CONTENT_ID='c150f816-fdde-102a-b5ba-0013d3b09da0' AND 
MDO.TYPE='net.sourceforge.fenixedu.domain.DepartmentSite' AND C1.KEY_META_DOMAIN_OBJECT=MDO.ID_INTERNAL;