Index: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases.pm
===================================================================
--- trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases.pm	(revision 2351)
+++ trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases.pm	(revision 2352)
@@ -433,11 +433,6 @@
         return;
     }
-    foreach my $cfield (keys %cdata) {
-        $self->check_allowed_values($otype, $cfield, $cdata{$cfield}) or do {
-            my $last = LATMOS::Accounts::Log::lastmessage(LA_ERR);
-            $self->log(LA_ERR, "Cannot create $otype, wrong value%s", ($last ? ": $last" : ''));
-            return;
-        };
-    }
+
+    my %data;
 
     # populating default value
@@ -445,9 +440,32 @@
         my %default = $self->compute_default($otype, $id, %cdata);
         foreach my $k (keys %default) {
-            $cdata{$k} = $default{$k} unless(exists($cdata{$k}));
-        }
-    }
-
-    my %data;
+            $data{$k} = $default{$k};
+        }
+    }
+
+    if ( $self->is_supported_object('templates') ) {
+        if ( $cdata{template} ) {
+            my $template = $self->get_object('templates', $cdata{template} ) or do {
+                $self->log(LA_ERR, "Cannot load template $cdata{template}");
+                return;
+            };
+
+            my $TOType = $template->_get_attributes('objecttype');
+            if ( $TOType ne $otype ) {
+                $self->log(LA_ERR, "Template $cdata{template} is for object type $TOType, not $otype");
+                return;
+            }
+
+            my $text = join('\n', $template->_get_attributes('data'));
+            my %tdata = LATMOS::Accounts::Utils::parse_obj_text($text || '');
+
+            foreach my $k ( keys %tdata ) {
+                $data{$k} = $tdata{$k};
+            }
+
+            delete( $cdata{template} );
+        }
+    }
+
     my $sub;
     foreach my $cfield (keys %cdata) {
@@ -462,4 +480,12 @@
             $data{$cfield} = $cdata{$cfield};
         }
+    }
+
+    foreach my $cfield (keys %data) {
+        $self->check_allowed_values($otype, $cfield, $data{$cfield}) or do {
+            my $last = LATMOS::Accounts::Log::lastmessage(LA_ERR);
+            $self->log(LA_ERR, "Cannot create $otype, wrong value%s", ($last ? ": $last" : ''));
+            return;
+        };
     }
 
Index: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql.pm
===================================================================
--- trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql.pm	(revision 2351)
+++ trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql.pm	(revision 2352)
@@ -18,5 +18,5 @@
 our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
 
-sub SCHEMA_VERSION { 36 };
+sub SCHEMA_VERSION { 37 };
 
 =head1 NAME
Index: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Templates.pm
===================================================================
--- trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Templates.pm	(revision 2352)
+++ trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Templates.pm	(revision 2352)
@@ -0,0 +1,96 @@
+package LATMOS::Accounts::Bases::Sql::Templates;
+
+use 5.010000;
+use strict;
+use warnings;
+
+use base qw(LATMOS::Accounts::Bases::Sql::objects);
+use LATMOS::Accounts::Log;
+use LATMOS::Accounts::I18N;
+
+our $VERSION = (q$Rev: 341 $ =~ /^Rev: (\d+) /)[0];
+
+=head1 NAME
+
+LATMOS::Ad - Perl extension for blah blah blah
+
+=head1 SYNOPSIS
+
+  use LATMOS::Accounts::Bases;
+  my $base = LATMOS::Accounts::Bases->new('sql');
+  ...
+
+=head1 DESCRIPTION
+
+Account base access over standard unix file format.
+
+=head1 FUNCTIONS
+
+=cut
+
+sub _object_table { 'templates' }
+
+sub _key_field { 'name' }
+
+sub _has_extended_attributes { 0 }
+
+sub _get_attr_schema {
+    my ($class, $base) = @_;
+
+    $class->SUPER::_get_attr_schema($base,
+        {
+            objectType => {
+                inline => 1,
+                ro => 1,
+                can_values => sub {
+                    $base->list_supported_objects;
+                },
+            },
+            data => {
+                inline => 1,
+                formtype => 'TEXTAREA',
+                multiple => 1,
+                set => sub {
+                    my ($self, $data) = @_;
+                    my @datas = $data
+                        ? (ref $data
+                            ? @$data
+                            : ($data))
+                        : ();
+                    my $sql = @datas
+                        ? '{' . join(',', map { $_ =~ s/\\/\\\\/g; '"' .$_.'"' } @datas) . '}'
+                        : undef;
+                    $base->db->do(
+                        q{UPDATE templates SET data = }
+                        . $base->db->quote($sql) . q{ where name = } .
+                        $base->db->quote($self->object->id)
+                    );
+                    return 1;
+                },
+            },
+        }
+    )
+}
+
+1;
+
+__END__
+
+=head1 SEE ALSO
+
+L<LATMOS::Accounts::Bases::Sql::Aliases>, L<LATMOS::Accounts::Bases::Sql> 
+
+=head1 AUTHOR
+
+Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself, either Perl version 5.10.0 or,
+at your option, any later version of Perl 5 you may have available.
+
+
+=cut
Index: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/objects.pm
===================================================================
--- trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/objects.pm	(revision 2351)
+++ trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/objects.pm	(revision 2352)
@@ -262,4 +262,12 @@
                 ) ];
             },
+        },
+        template => {
+            reference => 'templates',
+            can_values => sub {
+                $base->search_objects('templates', 'objectType=' . $class->type, 'exported=1')
+            },
+            ro => sub { ref $_[0] ? 1 : 0 },
+            set => sub {},
         },
     );
Index: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/upgrade.pm
===================================================================
--- trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/upgrade.pm	(revision 2351)
+++ trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/upgrade.pm	(revision 2352)
@@ -1318,5 +1318,39 @@
             ],
         },
+        {
+            ver => 37,
+            sql => [
+                qq{
+                CREATE TABLE public."templates"
+                (
+                  objectType text not NULL,
+                  data text[],
+                  CONSTRAINT template_pkey PRIMARY KEY (name)
+                )
+                INHERITS (public.objects)
+                WITH (
+                  OIDS=FALSE
+                );
+
+                CREATE INDEX templates_rev_idx
+                  ON public."templates"
+                  USING btree
+                  (rev);
+
+                CREATE INDEX templates_exported_idx
+                  ON public."templates"
+                  USING btree
+                  (exported);
+
+                CREATE TRIGGER templates_rev_tg
+                  BEFORE UPDATE OR DELETE
+                  ON public."templates"
+                  FOR EACH ROW
+                  EXECUTE PROCEDURE public.rev_tg_f();
+                }
+            ],
+        },
     );
+
 
     my $dbi = $self->{_db};
Index: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Utils.pm
===================================================================
--- trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Utils.pm	(revision 2351)
+++ trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Utils.pm	(revision 2352)
@@ -106,4 +106,29 @@
 }
 
+sub _parse_line {
+    my ( $line, $attributes ) = @_;
+
+    $line =~ /^#/ and return;
+    # Empty line are skipped (or space only)
+    $line =~ /^\s*$/ and return;
+    chomp($line);
+    my ($attr, $value) = $line =~ /^\s*(\S+):\s*(.*)\s*$/ or
+        die "Malformed input file\n";
+    $value =~ s/\s*$//;
+    $value =~ s/\\n/\n/g;
+    if ($attributes->{$attr}) {
+        if (ref $attributes->{$attr}) {
+            push(@{ $attributes->{$attr} }, $value);
+        } else {
+            my $temp = $attributes->{$attr};
+            $attributes->{$attr} = [ $temp, $value ];
+        }
+    } else {
+        $attributes->{$attr} = $value eq '' ? undef : $value;
+        # Don't remember why this is here
+        #$attr eq 'exported' && !defined $attributes->{$attr} and $attributes->{$attr} = 1;
+    }
+}
+
 =head2 parse_obj_file($handle)
 
@@ -115,28 +140,25 @@
     my ($handle) = @_;
 
-    my %attributes;
+    my $attributes = {};
     while (my $line = <$handle>) {
-        $line =~ /^#/ and next;
-        # Empty line are skipped (or space only)
-        $line =~ /^\s*$/ and next;
-        chomp($line);
-        my ($attr, $value) = $line =~ /^\s*(\S+):\s*(.*)\s*$/ or
-            die "Malformed input file\n";
-        $value =~ s/\s*$//;
-        $value =~ s/\\n/\n/g;
-        if ($attributes{$attr}) {
-            if (ref $attributes{$attr}) {
-                push(@{ $attributes{$attr} }, $value);
-            } else {
-                my $temp = $attributes{$attr};
-                $attributes{$attr} = [ $temp, $value ];
-            }
-        } else {
-            $attributes{$attr} = $value eq '' ? undef : $value;
-            # Don't remember why this is here
-            #$attr eq 'exported' && !defined $attributes{$attr} and $attributes{$attr} = 1;
-        }
-    }
-    %attributes
+        _parse_line( $line, $attributes );
+    }
+    %$attributes
+}
+
+=head2 parse_obj_text($text)
+
+=cut
+
+sub parse_obj_text {
+    my ( $Text ) = @_;
+
+    my $attributes = {};
+
+    foreach (split(/\n/, $Text)) {
+        _parse_line( $_, $attributes );
+    }
+
+    %$attributes
 }
 
