--
-- acs_message__new_file/13
--
create or replace function acs_message__new_file(
  p_message_id integer,
  p_file_id integer,
  p_file_name character varying,
  p_title character varying,
  p_description text,
  p_mime_type character varying,
  p_data integer,
  p_creation_date timestamp with time zone,
  p_creation_user integer,
  p_creation_ip character varying,
  p_is_live boolean,
  p_storage_type cr_item_storage_type_enum,
  p_package_id integer DEFAULT NULL::integer
) returns int4 as $$

DECLARE
    v_file_id      cr_items.item_id%TYPE;
    v_revision_id  cr_revisions.revision_id%TYPE;
BEGIN
    v_file_id := content_item__new (
        p_file_name,			   -- name           
        p_message_id,			   -- parent_id      
        p_file_id,			   -- item_id        
        null,				   -- locale
        p_creation_date,		   -- creation_date  
        p_creation_user,		   -- creation_user  
        null,				   -- context_id
        p_creation_ip,			   -- creation_ip    
        'content_item',		   	   -- item_subtype
        'content_revision',		   -- content_type
        null,				   -- title
        null,				   -- description
        'text/plain',			   -- mime_type
        null,				   -- nls_language
        null,				   -- text
	null,  				   -- data
	null,  				   -- relation_tag
	false, 				   -- is_live
	p_storage_type,			   -- storage_type
        p_package_id,			   -- package_id
        true                               -- with_child_rels
    );

    -- create an initial revision for the new attachment
    v_revision_id := acs_message__edit_file (
         v_file_id,			-- file_id        
         p_title,			-- title          
         p_description,			-- description    
         p_mime_type,			-- mime_type      
         p_data,			-- data        
         p_creation_date,		-- creation_date  
         p_creation_user,		-- creation_user  
         p_creation_ip,			-- creation_ip    
         p_is_live			-- is_live        
    );

    return v_file_id;
END;
$$ language plpgsql;