Index: openacs-4/packages/acs-kernel/sql/postgresql/apm-create.sql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/acs-kernel/sql/postgresql/apm-create.sql,v
diff -u -r1.67 -r1.68
--- openacs-4/packages/acs-kernel/sql/postgresql/apm-create.sql	25 Sep 2007 15:22:35 -0000	1.67
+++ openacs-4/packages/acs-kernel/sql/postgresql/apm-create.sql	25 Nov 2008 17:08:49 -0000	1.68
@@ -32,8 +32,18 @@
 				check (package_type in ('apm_application', 'apm_service')),
     spec_file_path		varchar(1500),
     spec_file_mtime		integer,
-    initial_install_p		boolean default 'f' not null,
-    singleton_p			boolean default 'f' not null
+    initial_install_p		boolean default 'f'
+                                constraint apm_packages_in_inst_nn
+                                not null,
+    singleton_p			boolean default 'f'
+                                constraint apm_packages_singleton_nn
+                                not null,
+    implements_subsite_p        boolean default 'f'
+                                constraint apm_packages_impl_subs_p_nn
+                                not null,
+    inherit_templates_p         boolean default 't'
+                                constraint inherit_templates_p_nn
+                                not null
 );
 
 comment on table apm_package_types is '
@@ -73,7 +83,18 @@
  restricted to the acs-admin/ subsite.
 ';
 
+comment on column apm_package_types.implements_subsite_p is '
+  If true, this package implements subsite semantics, typically by extending the
+  acs-subsite package.  Used by the admin "mount subsite" UI, the request processor (for
+  setting ad_conn''s subsite_* attributes), etc.
+';
 
+comment on column apm_package_types.inherit_templates_p is '
+  If true, inherit templates from packages this package extends.  If false, only
+  templates in this package''s www subdirectory tree will be mapped to URLs by the
+  request processor.
+';
+
 create or replace function inline_0 ()
 returns integer as '
 begin
@@ -641,9 +662,11 @@
 -- DCW - 2001-05-04, converted tarball storage to use content repository.
 create view apm_package_version_info as
     select v.package_key, t.package_uri, t.pretty_name, t.singleton_p, t.initial_install_p,
+           t.inherit_templates_p, t.implements_subsite_p,
            v.version_id, v.version_name,
            v.version_uri, v.summary, v.description_format, v.description, v.release_date,
-           v.vendor, v.vendor_uri, v.auto_mount, v.enabled_p, v.installed_p, v.tagged_p, v.imported_p, v.data_model_loaded_p,
+           v.vendor, v.vendor_uri, v.auto_mount, v.enabled_p, v.installed_p, v.tagged_p,
+           v.imported_p, v.data_model_loaded_p,
            v.activation_date, v.deactivation_date,
            coalesce(v.content_length,0) as tarball_length,
            distribution_uri, distribution_date
@@ -972,7 +995,8 @@
                        constraint apm_package_deps_version_id_nn not null,
     dependency_type    varchar(20)
                        constraint apm_package_deps_type_nn not null
-                       constraint apm_package_deps_type_ck check(dependency_type in ('provides','requires')),
+                       constraint apm_package_deps_type_ck
+                       check(dependency_type in ('extends', 'provides','requires')),
     service_uri        varchar(1500)
                        constraint apm_package_deps_uri_nn not null,
     service_version    varchar(100)
@@ -1069,7 +1093,7 @@
 
 drop function inline_7 ();
 
-create or replace function apm__register_package (varchar,varchar,varchar,varchar,varchar,boolean,boolean,varchar,integer)
+create or replace function apm__register_package (varchar,varchar,varchar,varchar,varchar,boolean,boolean,boolean,boolean,varchar,integer)
 returns integer as '
 declare
   package_key            alias for $1;  
@@ -1079,8 +1103,10 @@
   package_type           alias for $5;  
   initial_install_p      alias for $6;  -- default ''f''  
   singleton_p            alias for $7;  -- default ''f''  
-  spec_file_path         alias for $8;  -- default null
-  spec_file_mtime        alias for $9;  -- default null
+  implements_subsite_p   alias for $8;  -- default ''f''  
+  inherit_templates_p    alias for $9;  -- default ''f''  
+  spec_file_path         alias for $10;  -- default null
+  spec_file_mtime        alias for $11;  -- default null
 begin
     PERFORM apm_package_type__create_type(
     	package_key,
@@ -1090,16 +1116,17 @@
 	package_type,
 	initial_install_p,
 	singleton_p,
+        implements_subsite_p,
+        inherit_templates_p,
 	spec_file_path,
 	spec_file_mtime
     );
 
     return 0; 
 end;' language 'plpgsql';
 
-
 -- function update_package
-create or replace function apm__update_package (varchar,varchar,varchar,varchar,varchar,boolean,boolean,varchar,integer)
+create or replace function apm__update_package (varchar,varchar,varchar,varchar,varchar,boolean,boolean,boolean,boolean,varchar,integer)
 returns varchar as '
 declare
   package_key            alias for $1;  
@@ -1109,8 +1136,10 @@
   package_type           alias for $5;  -- default null  
   initial_install_p      alias for $6;  -- default null  
   singleton_p            alias for $7;  -- default null  
-  spec_file_path         alias for $8;  -- default null  
-  spec_file_mtime        alias for $9;  -- default null  
+  implements_subsite_p   alias for $8;  -- default ''f''  
+  inherit_templates_p    alias for $9;  -- default ''f''  
+  spec_file_path         alias for $10;  -- default null
+  spec_file_mtime        alias for $11;  -- default null
 begin
  
     return apm_package_type__update_type(
@@ -1121,13 +1150,13 @@
 	package_type,
 	initial_install_p,
 	singleton_p,
+        implements_subsite_p,
+        inherit_templates_p,
 	spec_file_path,
 	spec_file_mtime
     );
-   
 end;' language 'plpgsql';
 
-
 -- procedure unregister_package
 create or replace function apm__unregister_package (varchar,boolean)
 returns integer as '
@@ -1168,7 +1197,7 @@
 
 
 -- procedure register_application
-create or replace function apm__register_application (varchar,varchar,varchar,varchar,boolean,boolean,varchar,integer)
+create or replace function apm__register_application (varchar,varchar,varchar,varchar,boolean,boolean,boolean,boolean,varchar,integer)
 returns integer as '
 declare
   package_key            alias for $1;  
@@ -1177,8 +1206,10 @@
   package_uri            alias for $4;  
   initial_install_p      alias for $5;  -- default ''f'' 
   singleton_p            alias for $6;  -- default ''f'' 
-  spec_file_path         alias for $7;  -- default null
-  spec_file_mtime        alias for $8;  -- default null
+  implements_subsite_p   alias for $7;  -- default ''f''  
+  inherit_templates_p    alias for $8;  -- default ''f''  
+  spec_file_path         alias for $9;  -- default null
+  spec_file_mtime        alias for $10;  -- default null
 begin
    PERFORM apm__register_package(
 	package_key,
@@ -1188,6 +1219,8 @@
 	''apm_application'',
 	initial_install_p,
 	singleton_p,
+        implements_subsite_p,
+        inherit_templates_p,
 	spec_file_path,
 	spec_file_mtime
    ); 
@@ -1220,7 +1253,7 @@
 
 
 -- procedure register_service
-create or replace function apm__register_service (varchar,varchar,varchar,varchar,boolean,boolean,varchar,integer)
+create or replace function apm__register_service (varchar,varchar,varchar,varchar,boolean,boolean,boolean,boolean,varchar,integer)
 returns integer as '
 declare
   package_key            alias for $1;  
@@ -1229,8 +1262,10 @@
   package_uri            alias for $4;  
   initial_install_p      alias for $5;  -- default ''f''  
   singleton_p            alias for $6;  -- default ''f''  
-  spec_file_path         alias for $7;  -- default null
-  spec_file_mtime        alias for $8;  -- default null
+  implements_subsite_p   alias for $7;  -- default ''f''  
+  inherit_templates_p    alias for $8;  -- default ''f''  
+  spec_file_path         alias for $9;  -- default null
+  spec_file_mtime        alias for $10;  -- default null
 begin
    PERFORM apm__register_package(
 	package_key,
@@ -1240,6 +1275,8 @@
 	''apm_service'',
 	initial_install_p,
 	singleton_p,
+        implements_subsite_p,
+        inherit_templates_p,
 	spec_file_path,
 	spec_file_mtime
    );  
@@ -1520,6 +1557,35 @@
 
 -- create or replace package body apm_package
 -- procedure initialize_parameters
+
+create or replace function apm_package__is_child(varchar, varchar) returns boolean as '
+declare
+  parent_package_key       alias for $1;
+  child_package_key        alias for $2;
+  dependency               record;
+begin
+
+  if parent_package_key = child_package_key then
+    return ''t'';
+  end if;
+
+  for dependency in 
+    select apd.service_uri
+    from apm_package_versions apv, apm_package_dependencies apd
+    where apd.version_id = apv.version_id
+      and apv.enabled_p
+      and apd.dependency_type = ''extends''
+      and apv.package_key = child_package_key
+  loop
+    if dependency.service_uri = parent_package_key or
+      apm_package__is_child(parent_package_key, dependency.service_uri) then
+      return ''t'';
+    end if;
+  end loop;
+      
+  return ''f'';
+end;' language 'plpgsql';
+
 create or replace function apm_package__initialize_parameters (integer,varchar)
 returns integer as '
 declare
@@ -2027,13 +2093,14 @@
 
 
 -- function add_dependency
-create or replace function apm_package_version__add_dependency (integer,integer,varchar,varchar)
+create or replace function apm_package_version__add_dependency (varchar,integer,integer,varchar,varchar)
 returns integer as '
 declare
-  add_dependency__dependency_id          alias for $1;  -- default null  
-  add_dependency__version_id             alias for $2;  
-  add_dependency__dependency_uri         alias for $3;  
-  add_dependency__dependency_version     alias for $4;  
+  add_dependency__dependency_type        alias for $1;
+  add_dependency__dependency_id          alias for $2;  -- default null  
+  add_dependency__version_id             alias for $3;  
+  add_dependency__dependency_uri         alias for $4;  
+  add_dependency__dependency_version     alias for $5;  
   v_dep_id                            apm_package_dependencies.dependency_id%TYPE;
 begin
       if add_dependency__dependency_id is null then
@@ -2045,8 +2112,8 @@
       insert into apm_package_dependencies
       (dependency_id, version_id, dependency_type, service_uri, service_version)
       values
-      (v_dep_id, add_dependency__version_id, ''requires'', add_dependency__dependency_uri,
-	add_dependency__dependency_version);
+      (v_dep_id, add_dependency__version_id, add_dependency__dependency_type,
+        add_dependency__dependency_uri, add_dependency__dependency_version);
 
       return v_dep_id;
    
@@ -2252,7 +2319,7 @@
 
 -- create or replace package body apm_package_type
 -- procedure create_type
-create or replace function apm_package_type__create_type (varchar,varchar,varchar,varchar,varchar,boolean,boolean,varchar,integer)
+create or replace function apm_package_type__create_type (varchar,varchar,varchar,varchar,varchar,boolean,boolean,boolean,boolean,varchar,integer)
 returns integer as '
 declare
   create_type__package_key            alias for $1;  
@@ -2262,23 +2329,27 @@
   create_type__package_type           alias for $5;  
   create_type__initial_install_p      alias for $6;  
   create_type__singleton_p            alias for $7;  
-  create_type__spec_file_path         alias for $8;  -- default null  
-  create_type__spec_file_mtime        alias for $9;  -- default null
+  create_type__implements_subsite_p   alias for $8;
+  create_type__inherit_templates_p    alias for $9;
+  create_type__spec_file_path         alias for $10;  -- default null  
+  create_type__spec_file_mtime        alias for $11;  -- default null
 begin
    insert into apm_package_types
     (package_key, pretty_name, pretty_plural, package_uri, package_type,
-    spec_file_path, spec_file_mtime, initial_install_p, singleton_p)
+    spec_file_path, spec_file_mtime, initial_install_p, singleton_p,
+    implements_subsite_p, inherit_templates_p)
    values
     (create_type__package_key, create_type__pretty_name, create_type__pretty_plural,
      create_type__package_uri, create_type__package_type, create_type__spec_file_path, 
-     create_type__spec_file_mtime, create_type__initial_install_p, create_type__singleton_p);
+     create_type__spec_file_mtime, create_type__initial_install_p, create_type__singleton_p,
+     create_type__implements_subsite_p, create_type__inherit_templates_p);
 
    return 0; 
 end;' language 'plpgsql';
 
 
 -- function update_type
-create or replace function apm_package_type__update_type (varchar,varchar,varchar,varchar,varchar,boolean,boolean,varchar,integer)
+create or replace function apm_package_type__update_type (varchar,varchar,varchar,varchar,varchar,boolean,boolean,boolean,boolean,varchar,integer)
 returns varchar as '
 declare
   update_type__package_key            alias for $1;  
@@ -2288,8 +2359,10 @@
   update_type__package_type           alias for $5;  -- default null  
   update_type__initial_install_p      alias for $6;  -- default null  
   update_type__singleton_p            alias for $7;  -- default null  
-  update_type__spec_file_path         alias for $8;  -- default null  
-  update_type__spec_file_mtime        alias for $9;  -- default null  
+  update_type__implements_subsite_p   alias for $8;  -- default null  
+  update_type__inherit_templates_p    alias for $9;  -- default null  
+  update_type__spec_file_path         alias for $10;  -- default null  
+  update_type__spec_file_mtime        alias for $11;  -- default null  
 begin
       UPDATE apm_package_types SET
       	pretty_name = coalesce(update_type__pretty_name, pretty_name),
@@ -2299,7 +2372,9 @@
     	spec_file_path = coalesce(update_type__spec_file_path, spec_file_path),
     	spec_file_mtime = coalesce(update_type__spec_file_mtime, spec_file_mtime),
     	singleton_p = coalesce(update_type__singleton_p, singleton_p),
-    	initial_install_p = coalesce(update_type__initial_install_p, initial_install_p)
+    	initial_install_p = coalesce(update_type__initial_install_p, initial_install_p),
+    	implements_subsite_p = coalesce(update_type__implements_subsite_p, implements_subsite_p),
+    	inherit_templates_p = coalesce(update_type__inherit_templates_p, inherit_templates_p)
       where package_key = update_type__package_key;
 
       return update_type__package_key;
