-- WAP creation data model
-- written by Andrew Grumet (aegrumet@arsdigita.com).
-- ported to acs40 by Shan Shan Huang (shuang@arsdigita.com)

-- We will store data about known WAP user agents, to hand off
-- incoming requests to the right place.
--
-- This is free software distributed under the terms of the GNU Public
-- License.  Full text of the license is available from the GNU Project:
-- http://www.fsf.org/copyleft/gpl.html

select  acs_object_type__create_type (
    'wap_user_agent',   -- object_type
    'WAP User Agent',   -- pretty_name
    'WAP User Agents',  -- pretty_plural
    'acs_object',       -- supertype
    'wap_user_agents',  -- table_name
    'user_agent_id',    -- id_column
    null,               -- package_name
    'f',                -- abstract_p
    null,               -- type_extension_table
    null                -- name_method
  );

select acs_attribute__create_attribute (
    'wap_user_agent',   -- object_type
    'NAME',             -- attribute_name
    'string',           -- datatype
    'Name',             -- pretty_name
    'Names',            -- pretty_plural
    null,               -- table_name default null
    null,               -- column_name default null
    null,               -- default_value default null
    1,                  -- min_n_values default 1
    1,                  -- max_n_values default 1
    null,               -- sort_order default null
    'type_specific',    -- storage ''type_specific''
    'f'                 -- static_p default ''f''
    );

select acs_attribute__create_attribute (
    'wap_user_agent',   -- object_type
    'CREATION_COMMENT', -- attribute_name
    'string',           -- datatype
    'Comment',          -- pretty_name
    'Comments',         -- pretty_plural
    null,               -- table_name default null
    null,               -- column_name default null
    null,               -- default_value default null
    1,                  -- min_n_values default 1
    1,                  -- max_n_values default 1
    null,               -- sort_order default null
    'type_specific',    -- storage ''type_specific''
    'f'                 -- static_p default ''f''
  );

select acs_attribute__create_attribute (
    'wap_user_agent',   -- object_type
    'ACTIVE_P',         -- attribute_name
    'string',           -- datatype
    'Active Status',    -- pretty_name
    'Active Status',    -- pretty_plural
    null,               -- table_name default null
    null,               -- column_name default null
    null,               -- default_value default null
    1,                  -- min_n_values default 1
    1,                  -- max_n_values default 1
    null,               -- sort_order default null
    'type_specific',    -- storage ''type_specific''
    'f'                 -- static_p default ''f''
  );

create table wap_user_agents (
	user_agent_id		integer references acs_objects(object_id) primary key,
	name			varchar(200)
				  constraint wap_user_agent_name_nn not null,
	creation_comment        varchar(4000),
        active_p		boolean
);

-- package body for wap_user_agent
create function wap_user_agent__new (
    integer,            -- v_user_agent_id	in wap_user_agents.user_agent_id%TYPE default null,
    varchar,            -- v_name		in wap_user_agents.name%TYPE,
    varchar,            -- v_creation_comment	in wap_user_agents.creation_comment%TYPE,
    boolean,            -- v_active_p		in wap_user_agents.creation_comment%TYPE default 't',
    integer,            -- v_creation_user	in acs_objects.creation_user%TYPE,
    varchar             -- v_creation_ip	in acs_objects.creation_ip%TYPE
  ) returns integer as '
  declare
    v_user_agent_id     alias for $1;
    v_name              alias for $2;
    v_creation_comment  alias for $3;
    v_active_p          alias for $4;
    v_creation_user     alias for $5;
    v_creation_ip	alias for $6;
    new_id	integer;
  begin
    new_id := acs_object__new (
      v_user_agent_id,          -- object_id
      ''wap_user_agent'',       -- object_type
      current_timestamp,        -- creation_date
      v_creation_user,          -- creation_user
      v_creation_ip,            -- creation_ip
      null                      -- context_id
    );

    insert into wap_user_agents
      (user_agent_id, name, creation_comment, active_p)
    values
      (new_id, v_name, v_creation_comment, v_active_p);

    return new_id;
  end;' language 'plpgsql';

  create function  wap_user_agent__delete (
    integer     -- v_user_agent_id	in wap_user_agents.user_agent_id%TYPE
  ) returns integer as '
  declare
    v_user_agent_id     alias for $1;
  begin
    delete from wap_user_agents where user_agent_id = v_user_agent_id;

    acs_object__delete(v_user_agent_id);
    return 0;
  end;' language 'plpgsql';

  create function  wap_user_agent__toggle_active_p (
    integer     -- v_user_agent_id	in wap_user_agents.user_agent_id%TYPE
  ) returns integer as '
  declare
    v_user_agent_id     alias for $1;
    v_active_p          boolean;
  begin
    select active_p into v_active_p
    from wap_user_agents where user_agent_id = v_user_agent_id;

    update wap_user_agents
    set active_p = case v_active_p when ''t'' then ''f'' else  ''t'' end
    where user_agent_id = v_user_agent_id ;
    return 0;
  end;' language 'plpgsql';

  create function wap_user_agent__modify (
    integer,            -- v_user_agent_id	in wap_user_agents.user_agent_id%TYPE,
    varchar,            -- v_name		in wap_user_agents.name%TYPE,
    varchar,            -- v_creation_comment	in wap_user_agents.creation_comment%TYPE,
    boolean             -- v_active_p		in wap_user_agents.active_p%TYPE
  ) returns integer as '
  declare
    v_user_agent_id     alias for $1;
    v_name              alias for $2;
    v_creation_comment  alias for $3;
    v_active_p          alias for $4;
  begin
    update wap_user_agents
      set name = v_name,
          creation_comment = v_creation_comment,
 	  active_p = v_active_p
    where user_agent_id = v_user_agent_id;
    return 0;
  end;' language 'plpgsql';


-- A bunch of user-agent data

  select wap_user_agent__new(null, 'ALAV UP/4.0.7', null, 't',null,null);
  select wap_user_agent__new(null, 'Alcatel-BE3/1.0 UP/4.0.6c', null, 't',null,null);
  select wap_user_agent__new(null, 'AUR PALM WAPPER', null, 't',null,null);
  select wap_user_agent__new(null, 'Device V1.12', null, 't',null,null);
  select wap_user_agent__new(null, 'EricssonR320/R1A', null, 't',null,null);
  select wap_user_agent__new(null, 'fetchpage.cgi/0.53', null, 't',null,null);
  select wap_user_agent__new(null, 'Java1.1.8', null, 't',null,null);
  select wap_user_agent__new(null, 'm-crawler/1.0 WAP', null, 't',null,null);
  select wap_user_agent__new(null, 'Materna-WAPPreview/1.1.3', null, 't',null,null);
  select wap_user_agent__new(null, 'MC218 2.0 WAP1.1', null, 't',null,null);
  select wap_user_agent__new(null, 'Mitsu/1.1.A', null, 't',null,null);
  select wap_user_agent__new(null, 'MOT-CB/0.0.19 UP/4.0.5j', null, 't',null,null);
  select wap_user_agent__new(null, 'MOT-CB/0.0.21 UP/4.0.5m', null, 't',null,null);
  select wap_user_agent__new(null, 'Nokia-WAP-Toolkit/1.2', null, 't',null,null);
  select wap_user_agent__new(null, 'Nokia-WAP-Toolkit/1.3beta', null, 't',null,null);
  select wap_user_agent__new(null, 'Nokia7110/1.0', null, 't',null,null);
  select wap_user_agent__new(null, 'Nokia7110/1.0 ()', null, 't',null,null);
  select wap_user_agent__new(null, 'Nokia7110/1.0 (04.67)', null, 't',null,null);
  select wap_user_agent__new(null, 'Nokia7110/1.0 (04.69)', null, 't',null,null);
  select wap_user_agent__new(null, 'Nokia7110/1.0 (04.70)', null, 't',null,null);
  select wap_user_agent__new(null, 'Nokia7110/1.0 (04.71)', null, 't',null,null);
  select wap_user_agent__new(null, 'Nokia7110/1.0 (04.73)', null, 't',null,null);
  select wap_user_agent__new(null, 'Nokia7110/1.0 (04.74)', null, 't',null,null);
  select wap_user_agent__new(null, 'Nokia7110/1.0 (04.76)', null, 't',null,null);
  select wap_user_agent__new(null, 'Nokia7110/1.0 (04.77)', null, 't',null,null);
  select wap_user_agent__new(null, 'Nokia7110/1.0 (04.80)', null, 't',null,null);
  select wap_user_agent__new(null, 'Nokia7110/1.0 (30.05)', null, 't',null,null);
  select wap_user_agent__new(null, 'PLM''s WapBrowser', null, 't',null,null);
  select wap_user_agent__new(null, 'QWAPPER/1.0', null, 't',null,null);
  select wap_user_agent__new(null, 'R380 2.0 WAP1.1', null, 't',null,null);
  select wap_user_agent__new(null, 'SIE-IC35/1.0', null, 't',null,null);
  select wap_user_agent__new(null, 'SIE-P35/1.0 UP/4.1.2a', null, 't',null,null);
  select wap_user_agent__new(null, 'UP.Browser/3.01-IG01', null, 't',null,null);
  select wap_user_agent__new(null, 'UP.Browser/3.01-QC31', null, 't',null,null);
  select wap_user_agent__new(null, 'UP.Browser/3.02-MC01', null, 't',null,null);
  select wap_user_agent__new(null, 'UP.Browser/3.02-SY01', null, 't',null,null);
  select wap_user_agent__new(null, 'UP.Browser/3.1-UPG1', null, 't',null,null);
  select wap_user_agent__new(null, 'UP.Browser/4.1.2a-XXXX', null, 't',null,null);
  select wap_user_agent__new(null, 'UPG1 UP/4.0.7', null, 't',null,null);
  select wap_user_agent__new(null, 'Wapalizer/1.0', null, 't',null,null);
  select wap_user_agent__new(null, 'Wapalizer/1.1', null, 't',null,null);
  select wap_user_agent__new(null, 'WapIDE-SDK/2.0; (R320s (Arial))', null, 't',null,null);
  select wap_user_agent__new(null, 'WAPJAG Virtual WAP', null, 't',null,null);
  select wap_user_agent__new(null, 'WAPman Version 1.1', null, 't',null,null);
  select wap_user_agent__new(null, 'WAPman Version 1.1 beta:Build W2000020401', null, 't',null,null);
  select wap_user_agent__new(null, 'Waptor 1.0', null, 't',null,null);
  select wap_user_agent__new(null, 'WapView 0.00', null, 't',null,null);
  select wap_user_agent__new(null, 'WapView 0.20371', null, 't',null,null);
  select wap_user_agent__new(null, 'WapView 0.28', null, 't',null,null);
  select wap_user_agent__new(null, 'WapView 0.37', null, 't',null,null);
  select wap_user_agent__new(null, 'WapView 0.46', null, 't',null,null);
  select wap_user_agent__new(null, 'WapView 0.47', null, 't',null,null);
  select wap_user_agent__new(null, 'WinWAP 2.2 WML 1.1', null, 't',null,null);
  select wap_user_agent__new(null, 'wmlb', null, 't',null,null);
  select wap_user_agent__new(null, 'YourWap/0.91', null, 't',null,null);
  select wap_user_agent__new(null, 'YourWap/1.16', null, 't',null,null);
  select wap_user_agent__new(null, 'Zetor', null, 't',null,null);






