annotate Lib/IMPL/DOM/Transform/QueryToDOM.pm @ 323:b56b1ec33b59

minor changes to support JSON in transformation from a query to an object
author sergey
date Thu, 23 May 2013 18:40:26 +0400
parents c9c2ec29793f
children 34a110d1f06c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
237
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
1 package IMPL::DOM::Transform::QueryToDOM;
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
2 use strict;
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
3
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
4 use IMPL::Const qw(:prop);
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
5 use IMPL::declare {
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
6 base => [
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
7 'IMPL::DOM::Transform::ObjectToDOM' => '@_'
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
8 ],
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
9 props => [
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
10 prefix => PROP_RO,
238
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
11 delimiter => PROP_RO
237
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
12 ]
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
13 };
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
14
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
15 sub CTOR {
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
16 my ($this) = @_;
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
17
264
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
18 $this->templates->{'CGI'} = 'TransformCGI';
323
b56b1ec33b59 minor changes to support JSON in transformation from a query to an object
sergey
parents: 264
diff changeset
19 $this->templates->{'IMPL::Web::Application::Action'} = 'TransformAction';
244
a02b110da931 refactoring
sergey
parents: 238
diff changeset
20
238
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
21 $this->delimiter('[.]');
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
22 $this->prefix('');
237
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
23 }
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
24
250
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
25 # inflate simple properties
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
26 sub TransformPlain {
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
27 my ($this,$data) = @_;
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
28
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
29 $this->currentNode->nodeProperty( rawValue => $data );
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
30 $this->currentNode->nodeValue( $this->inflateNodeValue($data) );
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
31 return $this->currentNode;
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
32 }
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
33
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
34 # do not store complex data as node values
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
35 sub StoreObject {
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
36 my ($this,$node,$data) = @_;
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
37
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
38 return $node;
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
39 }
129e48bb5afb DOM refactoring
sergey
parents: 244
diff changeset
40
264
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
41 #TODO: support a.b[0][1].c[1]
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
42
237
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
43 sub TransformCGI {
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
44 my ($this,$query) = @_;
323
b56b1ec33b59 minor changes to support JSON in transformation from a query to an object
sergey
parents: 264
diff changeset
45
237
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
46 my $data={};
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
47
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
48 my $prefix = $this->prefix;
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
49 my $delim = $this->delimiter;
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
50
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
51 foreach my $param (grep index($_,$prefix) >= 0 , $query->param()) {
244
a02b110da931 refactoring
sergey
parents: 238
diff changeset
52
a02b110da931 refactoring
sergey
parents: 238
diff changeset
53 my @value = grep length($_), $query->param($param) or next;
237
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
54
238
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
55 my @parts = split /$delim/,$param;
237
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
56
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
57 my $node = $data;
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
58 while ( my $part = shift @parts ) {
238
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
59 if (my ($name,$index) = ($part =~ m/^(\w+)(?:\[(\d+)\])?$/) ) {
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
60 if (@parts) {
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
61 if(defined $index) {
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
62 $node = ($node->{$name}[$index] ||= {});
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
63 } else {
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
64 $node = ($node->{$name} ||= {});
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
65 }
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
66 } else {
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
67 if(defined $index) {
244
a02b110da931 refactoring
sergey
parents: 238
diff changeset
68 $node->{$name}[$index] = (@value == 1 ? $value[0] : \@value);
238
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
69 } else {
244
a02b110da931 refactoring
sergey
parents: 238
diff changeset
70 $node->{$name} = (@value == 1 ? $value[0] : \@value);
238
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
71 }
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
72 }
237
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
73 }
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
74 }
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
75 }
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
76
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
77 return $this->Transform($data);
238
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
78 }
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
79
323
b56b1ec33b59 minor changes to support JSON in transformation from a query to an object
sergey
parents: 264
diff changeset
80 sub TransformAction {
b56b1ec33b59 minor changes to support JSON in transformation from a query to an object
sergey
parents: 264
diff changeset
81 my ($this,$action) = @_;
b56b1ec33b59 minor changes to support JSON in transformation from a query to an object
sergey
parents: 264
diff changeset
82
b56b1ec33b59 minor changes to support JSON in transformation from a query to an object
sergey
parents: 264
diff changeset
83 return $this->Transform($action->isJson ? $action->jsonData : $action->query);
b56b1ec33b59 minor changes to support JSON in transformation from a query to an object
sergey
parents: 264
diff changeset
84 }
b56b1ec33b59 minor changes to support JSON in transformation from a query to an object
sergey
parents: 264
diff changeset
85
264
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
86 1;
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
87
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
88 __END__
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
89
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
90 =pod
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
91
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
92 =head1 NAME
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
93
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
94 C<IMPL::DOM::Transform::QueryToDOM> - преобразование CGI запроса в DOM документ.
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
95
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
96 =head1 SYNOPSIS
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
97
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
98 =begin code
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
99
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
100 use CGI();
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
101 use IMPL::require {
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
102 Schema => 'IMPL::DOM::Schema',
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
103 Config => 'IMPL::Config',
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
104 QueryToDOM => 'IMPL::DOM::Transform::QueryToDOM'
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
105 }
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
106
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
107 my $q = CGI->new();
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
108
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
109 my $schema = Schema->LoadSchema(Config->AppBase('schemas','person.xml'));
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
110 my $transorm = QueryToDOM->new('edit', $schema);
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
111
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
112 my $form = $transform->Transform($q);
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
113
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
114 my @errors;
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
115
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
116 push @errors, $transform->buildErrors;
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
117 push @errors, $schema->Validate($doc);
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
118
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
119
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
120 =end code
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
121
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
122 =head1 DESCRIPTION
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
123
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
124 Наследует C<IMPL::DOM::Transform::ObjectToDOM>. Добавляет метод
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
125 C<TransformCGI> который применятеся к объектам типа C<CGI> (и производных).
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
126
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
127 Запрос C<CGI> сначала приводится к хешу, затем полученный хеш преобразуется
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
128 в DOM документ при помощи вызова метода C<Transform>.
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
129
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
130 Для этого выбираются параметры запроса, затем, имя каждого параметра
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
131 рассматривается в виде пути к свойству, создается структура из хешей и массивов
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
132 в которую по указанному пути кладется значение.
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
133
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
134 Если параметр имеет несколько значений, значит свойство является массивом.
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
135
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
136 Также изменено поведение некоторых методов преобразования.
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
137
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
138 =over
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
139
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
140 =item * C<TransformPlain($value)>
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
141
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
142 Преобразование для простого значения свойства. Посокльку в запросе передаются
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
143 строковые значения, а схема документа может предполпгать другие типы, при
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
144 преобразовании значения параметра из запроса к значению узла используется
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
145 метод C<< $this->inflateNodeValue($value) >>, также помимо значения
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
146 C<< $this->currentNode->nodeValue >> задается атрибут
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
147 C<< $this->currentNode->nodeProperty( rawValue => $value) >>, для того, чтобы
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
148 была возможность получить оригинальное значение параметра запроса (например,
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
149 в случае когда его формат был не верным и C<nodeValue> будет C<undef>).
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
150
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
151 =item * C<StoreObject($node,$object)>
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
152
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
153 Данный метод вызывается если текущий узел (переданный в параметре C<$node>)
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
154 предполагает простое значение, однако в запросе для него было передано сложное
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
155 содержимое. Данная реализация просто игнорирует переданный объект C<$object>
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
156 и возвращает C<$node> без изменений.
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
157
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
158 =back
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
159
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
160 =head1 MEMBERS
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
161
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
162 =head2 C<[get]delimiter>
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
163
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
164 REGEX. Разделитель свойств в имени параметра, по-умолчанию C<'[.]'>
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
165
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
166 =head2 C<[get]prefix>
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
167
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
168 Строка, префикс имен параметров, которые участвуют в формировании документа.
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
169 По-умолчанию пусто.
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
170
c9c2ec29793f *IMPL::DOM::Transform: updated documentation
sergey
parents: 250
diff changeset
171 =cut