-- packages/project-manager/sql/project-manager-drop.sql
-- drop script
--
-- @author jade@bread.com
-- @creation-date 2003-05-15
-- @cvs-id $Id: project-manager-drop.sql,v 1.6 2003/08/13 18:56:29 jader Exp $
--

--------
-- TASKS
--------

-- task constraint types
drop table pm_task_constraint_types cascade;
drop table pm_task_constraints cascade;
drop sequence pm_task_constraint_seq;
drop sequence pm_tasks_number_seq;

drop table pm_task_default_roles;
drop table pm_task_role_map;
drop table pm_task_roles;
drop sequence pm_task_role_seq;

create function inline_0 ()
returns integer as '
declare
    v_item RECORD;
        
begin
        for v_item in select 
                item_id
                from 
                cr_items
                where 
                content_type = ''pm_task''
        LOOP
                
                PERFORM pm_task__delete_task_item(v_item);
        end loop;

    return 0;
end;
' language 'plpgsql';

-- select inline_0();
drop function inline_0();

select drop_package('pm_task');


--drop permissions
delete from acs_permissions where object_id in (select project_id from pm_projects);

-- unregister content_types from folder
create function inline_0 ()
returns integer as '
declare
    v_folder_id	  cr_folders.folder_id%TYPE;
    v_item_id     cr_items.item_id%TYPE;
    v_item_cursor RECORD;
        
begin
    select content_item__get_id(''projects'', null, ''f'') into v_folder_id from dual;

    -- delete all contents of projects folder
    FOR v_item_cursor IN
        select item_id
        from   cr_items
        where  parent_id = v_folder_id
    LOOP
       	PERFORM pm_projects__delete_project_item(v_item_cursor.item_id);
    END LOOP;

    -- unregister_content_types
    PERFORM content_folder__unregister_content_type (
        v_folder_id,		-- folder_id
        ''pm_project'',		-- content_type
        ''t''			-- include_subtypes
    );

    -- this table must not hold reference to ''pm_project'' type
    delete from cr_folder_type_map where content_type = ''pm_project'';

    -- delete projects folder
    PERFORM content_folder__delete(v_folder_id);

    return 0;
end;
' language 'plpgsql';

select inline_0();
drop function inline_0();

-----------
-- PROJECTS
-----------

-- delete content_type 'pm_project'

create function inline_0 ()
returns integer as '
declare
        v_pm_item       cr_items%ROWTYPE;
begin
        FOR v_pm_item in select
                item_id
                from
                cr_items
                where
                content_type = ''pm_project''
        LOOP
                PERFORM pm_project__delete_project_item(v_pm_item.item_id);
        END LOOP;
        
        return 0;
end;
' language 'plpgsql';

select inline_0();
drop function inline_0();

-- drop package, which drops all functions created with define_function_args
select drop_package('pm_project');

--drop table
drop table pm_project_default_roles;
drop table pm_project_role_map;
drop table pm_project_roles;
drop table pm_projects cascade;

drop sequence pm_project_role_seq;

select content_type__drop_type('pm_task', 't', 'f');

drop table pm_tasks cascade;
drop table pm_tasks_revisions cascade;

select content_type__drop_type('pm_project', 't', 'f');

-- note that the Project Repository folder is not deleted

