--
-- packages/project-manager/sql/postgresql/project-manager-functions-create.sql
--
-- @author jade@bread.com
-- @creation-date 2003-05-15
-- @cvs-id $Id: project-manager-functions-create.sql,v 1.1 2003/05/21 22:42:54 jader Exp $
--
--

select define_function_args('pm_project__new','project_id,project_name,project_code,parent_project_id,goal,description,deadline_scheduling,planned_start_date,planned_end_date,actual_start_date,actual_end_date,ongoing_p,creation_date,creation_user,creation_ip,context_id'); 

create or replace function pm_project__new (integer,varchar,varchar,integer,varchar,varchar,char(1),timestamptz,timestamptz,timestamptz,timestamptz,char(1),timestamptz,integer,varchar,integer)
returns integer as '
declare
  p_project_id                                  alias for $1;
  p_project_name                                alias for $2;
  p_project_code                                alias for $3;
  p_parent_project_id                           alias for $4;
  p_goal                                        alias for $5;
  p_description                                 alias for $6;
  p_deadline_scheduling                         alias for $7;
  p_planned_start_date                          alias for $8;
  p_planned_end_date                            alias for $9;
  p_actual_start_date                           alias for $10;
  p_actual_end_date                             alias for $11;
  p_ongoing_p                                   alias for $12;
  p_creation_date                               alias for $13;
  p_creation_user                               alias for $14;
  p_creation_ip                                 alias for $15;
  p_context_id                                  alias for $16;
  v_pm_project_id                               int;
begin
        v_pm_project_id := acs_object__new (
                p_project_id,
                ''pm_project'',
                p_creation_date,
                p_creation_user,
                p_creation_ip,
                p_context_id
        );
        insert into pm_project
          (project_id,project_name,project_code,parent_project_id,goal,description,deadline_scheduling,planned_start_date,planned_end_date,actual_start_date,actual_end_date,ongoing_p) 
        values
          (v_pm_project_id, p_project_name, p_project_code, p_parent_project_id, p_goal, p_description, p_deadline_scheduling, p_planned_start_date, p_planned_end_date, p_actual_start_date, p_actual_end_date, p_ongoing_p);
        PERFORM acs_permission__grant_permission(
          v_pm_project_id,
          p_creation_user,
          ''admin''
    );
        return v_pm_project_id;
end;' language 'plpgsql';
/* The __delete function deletes a record and all related overhead. */
  

select define_function_args('pm_project___delete','project_id');
create or replace function pm_project__delete (integer)
returns integer as '
declare
  p_pm_project_id                               alias for $1;
begin
    delete from acs_permissions
                   where object_id = p_pm_project_id;
        delete from pm_project
                   where project_id = p_pm_project_id;
        raise NOTICE ''Deleting pm_project...'';
        PERFORM acs_object__delete(p_pm_project_id);
        return 0;
end;' language 'plpgsql';
/* When we created the acs object type above, we specified a
'name_method'.  This is the name of a function that will return the
name of the object.  This is a convention ensuring that all objects
can be identified.  Now we have to build that function.  In this case,
we'll return a field called title as the name. */

select define_function_args('pm_project___name','project_id');

create or replace function pm_project__name (integer)
returns varchar as '
declare
    p_pm_project_id      alias for $1;
    v_pm_project_name    pm_project.project_name%TYPE;
begin
        select project_name into v_pm_project_name
                from pm_project
                where project_id = p_pm_project_id;
    return v_pm_project_name;
end;
' language 'plpgsql';
