-- copy funcionalities
INSERT INTO CONTENT (CONTENT_ID, OJB_CONCRETE_CLASS, KEY_ROOT_DOMAIN_OBJECT, KEY_AVAILABILITY_POLICY, NAME, TITLE, DESCRIPTION, EXECUTION_PATH, ENABLED, PREFIX, CREATION_DATE, VISIBLE)
SELECT UUID, 'net.sourceforge.fenixedu.domain.functionalities.Functionality', 1, KEY_AVAILABILITY_POLICY, NAME, TITLE, DESCRIPTION, PATH, ENABLED, PREFIX, NOW(), VISIBLE
FROM ACCESSIBLE_ITEM WHERE OJB_CONCRETE_CLASS = 'net.sourceforge.fenixedu.domain.functionalities.ConcreteFunctionality';

-- copy modules
INSERT INTO CONTENT (CONTENT_ID, OJB_CONCRETE_CLASS, KEY_ROOT_DOMAIN_OBJECT, KEY_AVAILABILITY_POLICY, NAME, TITLE, DESCRIPTION, EXECUTION_PATH, ENABLED, PREFIX, CREATION_DATE, VISIBLE)
SELECT UUID, 'net.sourceforge.fenixedu.domain.functionalities.Module', 1, KEY_AVAILABILITY_POLICY, NAME, TITLE, DESCRIPTION, PATH, ENABLED, PREFIX, NOW(), VISIBLE
FROM ACCESSIBLE_ITEM WHERE OJB_CONCRETE_CLASS = 'net.sourceforge.fenixedu.domain.functionalities.Module';


-- ISCTE
ALTER TABLE CONTENT ADD INDEX CONTENT_ID(CONTENT_ID);

-- create nodes
INSERT INTO NODE (OJB_CONCRETE_CLASS, KEY_PARENT, KEY_CHILD, NODE_ORDER, VISIBLE)
SELECT 'net.sourceforge.fenixedu.domain.contents.ExplicitOrderNode', C_P.ID_INTERNAL, C_C.ID_INTERNAL, AI_C.ORDER_IN_MODULE, AI_C.VISIBLE
FROM CONTENT AS C_P, CONTENT AS C_C, ACCESSIBLE_ITEM AS AI_P, ACCESSIBLE_ITEM AS AI_C
WHERE AI_P.OJB_CONCRETE_CLASS = 'net.sourceforge.fenixedu.domain.functionalities.Module'
  AND (AI_C.KEY_PARENT = AI_P.ID_INTERNAL OR AI_C.KEY_MODULE = AI_P.ID_INTERNAL)
  AND C_P.CONTENT_ID = AI_P.UUID
  AND C_C.CONTENT_ID = AI_C.UUID;

-- Create Node RootModule -> Topos
INSERT INTO NODE (OJB_CONCRETE_CLASS, KEY_PARENT, KEY_CHILD, NODE_ORDER, VISIBLE)
SELECT 'net.sourceforge.fenixedu.domain.contents.ExplicitOrderNode', C_P.ID_INTERNAL, C_C.ID_INTERNAL, AI_C.ORDER_IN_MODULE, AI_C.VISIBLE
FROM CONTENT AS C_P, CONTENT AS C_C, ACCESSIBLE_ITEM AS AI_C
WHERE C_P.OJB_CONCRETE_CLASS = 'net.sourceforge.fenixedu.domain.functionalities.Module' AND C_P.KEY_MODULE_ROOT_DOMAIN_OBJECT IS NOT NULL
	AND C_C.OJB_CONCRETE_CLASS = 'net.sourceforge.fenixedu.domain.functionalities.Module' 
	AND C_C.CONTENT_ID = AI_C.UUID
	AND AI_C.KEY_PARENT IS NULL;

-- Create Node RootModule -> Topos
INSERT INTO NODE (OJB_CONCRETE_CLASS, KEY_PARENT, KEY_CHILD, NODE_ORDER, VISIBLE)
SELECT 'net.sourceforge.fenixedu.domain.contents.ExplicitOrderNode', C_P.ID_INTERNAL, C_C.ID_INTERNAL, AI_C.ORDER_IN_MODULE, AI_C.VISIBLE
FROM CONTENT AS C_P, CONTENT AS C_C, ACCESSIBLE_ITEM AS AI_C
WHERE C_P.OJB_CONCRETE_CLASS = 'net.sourceforge.fenixedu.domain.functionalities.Module' AND C_P.KEY_MODULE_ROOT_DOMAIN_OBJECT IS NOT NULL
	AND C_C.OJB_CONCRETE_CLASS = 'net.sourceforge.fenixedu.domain.functionalities.Functionality' 
	AND C_C.CONTENT_ID = AI_C.UUID
	AND AI_C.KEY_MODULE IS NULL;



-- importar FunctionalitySection, ou seja, criar node que liga pai da FunctionalitySection à Functionality
insert into NODE(KEY_PARENT,KEY_CHILD,NODE_ORDER,ASCENDING,OJB_CONCRETE_CLASS) 
select C2.ID_INTERNAL, C.ID_INTERNAL,AI1.SECTION_ORDER,'1','net.sourceforge.fenixedu.domain.contents.ExplicitOrderNode' 
FROM ACCESSIBLE_ITEM AI1, ACCESSIBLE_ITEM AI2, CONTENT C, CONTENT C2 
WHERE AI1.OJB_CONCRETE_CLASS='net.sourceforge.fenixedu.domain.FunctionalitySection' AND AI1.KEY_FUNCTIONALITY=AI2.ID_INTERNAL 
AND C.CONTENT_ID=AI2.UUID AND AI2.OJB_CONCRETE_CLASS='net.sourceforge.fenixedu.domain.functionalities.ConcreteFunctionality' 
AND C2.OLD_ID_INTERNAL=AI1.KEY_SITE AND AI1.KEY_SUPERIOR_SECTION IS NULL;

insert into NODE(KEY_PARENT,KEY_CHILD,NODE_ORDER,ASCENDING,OJB_CONCRETE_CLASS) 
select C2.ID_INTERNAL, C.ID_INTERNAL,AI1.SECTION_ORDER,'1','net.sourceforge.fenixedu.domain.contents.ExplicitOrderNode' 
FROM ACCESSIBLE_ITEM AI1, ACCESSIBLE_ITEM AI2, CONTENT C, CONTENT C2 
WHERE AI1.OJB_CONCRETE_CLASS='net.sourceforge.fenixedu.domain.FunctionalitySection' AND AI1.KEY_FUNCTIONALITY=AI2.ID_INTERNAL 
AND C.CONTENT_ID=AI2.UUID AND AI2.OJB_CONCRETE_CLASS='net.sourceforge.fenixedu.domain.functionalities.ConcreteFunctionality' 
AND AI1.KEY_SUPERIOR_SECTION IS NOT NULL AND AI1.KEY_SUPERIOR_SECTION=C2.OLD_ID_INTERNAL;



-- SiteTemplates
ALTER TABLE CONTENT ADD KEY_CONTAINER INT(11) DEFAULT NULL;

insert into CONTENT (OJB_CONCRETE_CLASS,NAME,TITLE,ENABLED,VISIBLE,MODIFICATION_DATE, OLD_ID_INTERNAL)
SELECT 'net.sourceforge.fenixedu.domain.Section', CM.NAME, CM.TITLE, CM.ENABLED, CM.VISIBLE, CM.MODIFICATION_DATE, CM.ID_INTERNAL
FROM CONTENT ST, ACCESSIBLE_ITEM M, CONTENT CM 
WHERE ST.OJB_CONCRETE_CLASS LIKE '%.SiteTemplate' AND ST.KEY_MODULE IS NOT NULL
	AND ST.KEY_MODULE = M.ID_INTERNAL AND M.UUID = CM.CONTENT_ID;

UPDATE CONTENT S, CONTENT ST, ACCESSIBLE_ITEM M, CONTENT CM SET ST.KEY_CONTAINER = S.ID_INTERNAL 
WHERE ST.OJB_CONCRETE_CLASS LIKE '%.SiteTemplate' AND ST.KEY_MODULE IS NOT NULL
	AND ST.KEY_MODULE = M.ID_INTERNAL AND M.UUID = CM.CONTENT_ID 
	AND CM.ID_INTERNAL = S.OLD_ID_INTERNAL AND S.OJB_CONCRETE_CLASS LIKE '%.Section';


INSERT INTO NODE (OJB_CONCRETE_CLASS, KEY_PARENT, KEY_CHILD, NODE_ORDER, VISIBLE)
SELECT 'net.sourceforge.fenixedu.domain.contents.ExplicitOrderNode', ST.KEY_CONTAINER, N.KEY_CHILD, N.NODE_ORDER, N.VISIBLE
FROM  CONTENT ST, ACCESSIBLE_ITEM M, CONTENT CM, NODE N
WHERE ST.OJB_CONCRETE_CLASS LIKE '%.SiteTemplate' AND ST.KEY_MODULE IS NOT NULL 
	AND ST.KEY_MODULE = M.ID_INTERNAL
	AND M.UUID = CM.CONTENT_ID 
	AND CM.ID_INTERNAL = N.KEY_PARENT;
