annotate Lib/IMPL/DOM/Transform/ObjectToDOM.pm @ 257:299af584c05f

collapse empty array elements in POSTed data
author sergey
date Fri, 14 Dec 2012 03:07:41 +0400
parents 129e48bb5afb
children 0f59b2de72af
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
236
2904da230022 DOM refactoring
sergey
parents:
diff changeset
1 package IMPL::DOM::Transform::ObjectToDOM;
2904da230022 DOM refactoring
sergey
parents:
diff changeset
2 use strict;
2904da230022 DOM refactoring
sergey
parents:
diff changeset
3
2904da230022 DOM refactoring
sergey
parents:
diff changeset
4 use IMPL::Const qw(:prop :access);
2904da230022 DOM refactoring
sergey
parents:
diff changeset
5 use IMPL::declare {
2904da230022 DOM refactoring
sergey
parents:
diff changeset
6 require => {
2904da230022 DOM refactoring
sergey
parents:
diff changeset
7 PropertyInfo => 'IMPL::Class::PropertyInfo',
2904da230022 DOM refactoring
sergey
parents:
diff changeset
8 Builder => 'IMPL::DOM::Navigator::Builder',
2904da230022 DOM refactoring
sergey
parents:
diff changeset
9 Exception => 'IMPL::Exception',
2904da230022 DOM refactoring
sergey
parents:
diff changeset
10 ArgumentException => '-IMPL::InvalidArgumentException',
2904da230022 DOM refactoring
sergey
parents:
diff changeset
11 OperationException => '-IMPL::InvalidOperationException'
2904da230022 DOM refactoring
sergey
parents:
diff changeset
12 },
2904da230022 DOM refactoring
sergey
parents:
diff changeset
13 base => [
2904da230022 DOM refactoring
sergey
parents:
diff changeset
14 'IMPL::Transform' => sub {
250
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
15 -plain => 'TransformPlain',
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
16 HASH => 'TransformHash',
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
17 -default => 'TransformDefault'
236
2904da230022 DOM refactoring
sergey
parents:
diff changeset
18 }
2904da230022 DOM refactoring
sergey
parents:
diff changeset
19 ],
2904da230022 DOM refactoring
sergey
parents:
diff changeset
20 props => [
2904da230022 DOM refactoring
sergey
parents:
diff changeset
21 documentSchema => PROP_RO,
2904da230022 DOM refactoring
sergey
parents:
diff changeset
22 _schema => PROP_RW,
2904da230022 DOM refactoring
sergey
parents:
diff changeset
23 _navi => PROP_RW
2904da230022 DOM refactoring
sergey
parents:
diff changeset
24 ]
2904da230022 DOM refactoring
sergey
parents:
diff changeset
25 };
2904da230022 DOM refactoring
sergey
parents:
diff changeset
26
246
2746a8e5a6c4 Fixed regressions in DOM due previous refactorings
sergey
parents: 237
diff changeset
27 use constant {
250
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
28 SchemaNode => 'IMPL::DOM::Schema::Node',
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
29 ComplexNode => 'IMPL::DOM::Schema::ComplexNode'
246
2746a8e5a6c4 Fixed regressions in DOM due previous refactorings
sergey
parents: 237
diff changeset
30 };
2746a8e5a6c4 Fixed regressions in DOM due previous refactorings
sergey
parents: 237
diff changeset
31
236
2904da230022 DOM refactoring
sergey
parents:
diff changeset
32 sub CTOR {
237
61db68166c37 refactoring QueryToDOM
sergey
parents: 236
diff changeset
33 my ($this,$docName,$docSchema,$transforms) = @_;
236
2904da230022 DOM refactoring
sergey
parents:
diff changeset
34
246
2746a8e5a6c4 Fixed regressions in DOM due previous refactorings
sergey
parents: 237
diff changeset
35 my $docNodeSchema = $docSchema->selectSingleNode(sub { $_->isa(SchemaNode) and $_->name eq $docName } )
236
2904da230022 DOM refactoring
sergey
parents:
diff changeset
36 or die OperationException->new("Can't find a node schema for the document '$docName'");
2904da230022 DOM refactoring
sergey
parents:
diff changeset
37
2904da230022 DOM refactoring
sergey
parents:
diff changeset
38 my $docClass = ($docNodeSchema->can('nativeType') ? $docNodeSchema->nativeType : undef) || 'IMPL::DOM::Document';
2904da230022 DOM refactoring
sergey
parents:
diff changeset
39
2904da230022 DOM refactoring
sergey
parents:
diff changeset
40 $this->documentSchema($docNodeSchema);
2904da230022 DOM refactoring
sergey
parents:
diff changeset
41
2904da230022 DOM refactoring
sergey
parents:
diff changeset
42 $this->_navi(
2904da230022 DOM refactoring
sergey
parents:
diff changeset
43 Builder->new(
2904da230022 DOM refactoring
sergey
parents:
diff changeset
44 $docClass,
2904da230022 DOM refactoring
sergey
parents:
diff changeset
45 $docSchema,
2904da230022 DOM refactoring
sergey
parents:
diff changeset
46 ignoreUndefined => 1
2904da230022 DOM refactoring
sergey
parents:
diff changeset
47 )
2904da230022 DOM refactoring
sergey
parents:
diff changeset
48 );
2904da230022 DOM refactoring
sergey
parents:
diff changeset
49 $this->_schema($docSchema);
2904da230022 DOM refactoring
sergey
parents:
diff changeset
50
2904da230022 DOM refactoring
sergey
parents:
diff changeset
51 $this->_navi->NavigateCreate($docName);
2904da230022 DOM refactoring
sergey
parents:
diff changeset
52 }
2904da230022 DOM refactoring
sergey
parents:
diff changeset
53
2904da230022 DOM refactoring
sergey
parents:
diff changeset
54 sub TransformPlain {
2904da230022 DOM refactoring
sergey
parents:
diff changeset
55 my ($this,$data) = @_;
2904da230022 DOM refactoring
sergey
parents:
diff changeset
56
250
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
57 $this->_navi->Current->nodeValue( $data );
236
2904da230022 DOM refactoring
sergey
parents:
diff changeset
58 return $this->_navi->Current;
2904da230022 DOM refactoring
sergey
parents:
diff changeset
59 }
2904da230022 DOM refactoring
sergey
parents:
diff changeset
60
250
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
61 sub currentNode {
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
62 shift->_navi->Current;
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
63 }
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
64
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
65 sub inflateNodeValue {
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
66 shift->_navi->inflateValue(shift);
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
67 }
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
68
236
2904da230022 DOM refactoring
sergey
parents:
diff changeset
69 sub TransformHash {
2904da230022 DOM refactoring
sergey
parents:
diff changeset
70 my ($this,$data) = @_;
2904da230022 DOM refactoring
sergey
parents:
diff changeset
71
2904da230022 DOM refactoring
sergey
parents:
diff changeset
72 die ArgumentException->new(data => 'A HASH reference is required')
2904da230022 DOM refactoring
sergey
parents:
diff changeset
73 unless ref $data eq 'HASH';
250
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
74
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
75 return $this->StoreObject($this->currentNode,$data)
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
76 if !$this->currentNode->schema->isa(ComplexNode);
236
2904da230022 DOM refactoring
sergey
parents:
diff changeset
77
2904da230022 DOM refactoring
sergey
parents:
diff changeset
78 KEYLOOP: foreach my $key (keys %$data) {
2904da230022 DOM refactoring
sergey
parents:
diff changeset
79 my $value = $data->{$key};
2904da230022 DOM refactoring
sergey
parents:
diff changeset
80
2904da230022 DOM refactoring
sergey
parents:
diff changeset
81 if (ref $value eq 'ARRAY') {
257
299af584c05f collapse empty array elements in POSTed data
sergey
parents: 250
diff changeset
82 #TODO: collapse empty values only if needed
299af584c05f collapse empty array elements in POSTed data
sergey
parents: 250
diff changeset
83 foreach my $subval (grep $_, @$value) {
236
2904da230022 DOM refactoring
sergey
parents:
diff changeset
84
250
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
85 $this->_navi->saveState();
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
86
236
2904da230022 DOM refactoring
sergey
parents:
diff changeset
87 my $node = $this->_navi->NavigateCreate($key);
2904da230022 DOM refactoring
sergey
parents:
diff changeset
88
2904da230022 DOM refactoring
sergey
parents:
diff changeset
89 unless(defined $node) {
250
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
90 #$this->_navi->Back();
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
91 $this->_navi->restoreState();
236
2904da230022 DOM refactoring
sergey
parents:
diff changeset
92 next KEYLOOP;
2904da230022 DOM refactoring
sergey
parents:
diff changeset
93 }
2904da230022 DOM refactoring
sergey
parents:
diff changeset
94
250
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
95 $this->_navi->applyState();
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
96
236
2904da230022 DOM refactoring
sergey
parents:
diff changeset
97 $this->Transform($subval);
2904da230022 DOM refactoring
sergey
parents:
diff changeset
98
2904da230022 DOM refactoring
sergey
parents:
diff changeset
99 $this->_navi->Back();
2904da230022 DOM refactoring
sergey
parents:
diff changeset
100 }
2904da230022 DOM refactoring
sergey
parents:
diff changeset
101 } else {
250
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
102 $this->_navi->saveState();
236
2904da230022 DOM refactoring
sergey
parents:
diff changeset
103 my $node = $this->_navi->NavigateCreate($key);
250
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
104
236
2904da230022 DOM refactoring
sergey
parents:
diff changeset
105 unless(defined $node) {
250
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
106 #$this->_navi->Back();
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
107 $this->_navi->restoreState();
236
2904da230022 DOM refactoring
sergey
parents:
diff changeset
108 next KEYLOOP;
2904da230022 DOM refactoring
sergey
parents:
diff changeset
109 }
2904da230022 DOM refactoring
sergey
parents:
diff changeset
110
250
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
111 $this->_navi->applyState();
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
112
236
2904da230022 DOM refactoring
sergey
parents:
diff changeset
113 $this->Transform($value);
2904da230022 DOM refactoring
sergey
parents:
diff changeset
114
2904da230022 DOM refactoring
sergey
parents:
diff changeset
115 $this->_navi->Back();
2904da230022 DOM refactoring
sergey
parents:
diff changeset
116 }
2904da230022 DOM refactoring
sergey
parents:
diff changeset
117 }
2904da230022 DOM refactoring
sergey
parents:
diff changeset
118 return $this->_navi->Current;
2904da230022 DOM refactoring
sergey
parents:
diff changeset
119 }
2904da230022 DOM refactoring
sergey
parents:
diff changeset
120
250
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
121 # this method handles situatuions when a complex object must be stored in a
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
122 # simple node.
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
123 sub StoreObject {
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
124 my ($this,$node,$data) = @_;
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
125
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
126 $node->nodeValue($data);
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
127
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
128 return $node;
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
129 }
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
130
236
2904da230022 DOM refactoring
sergey
parents:
diff changeset
131 sub TransformDefault {
2904da230022 DOM refactoring
sergey
parents:
diff changeset
132 my ($this,$data) = @_;
2904da230022 DOM refactoring
sergey
parents:
diff changeset
133
250
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
134 return $this->StoreObject($this->currentNode,$data)
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
135 if !$this->currentNode->schema->isa(ComplexNode);
129e48bb5afb DOM refactoring
sergey
parents: 246
diff changeset
136
236
2904da230022 DOM refactoring
sergey
parents:
diff changeset
137 if ( ref $data and eval { $data->can('GetMeta') } ) {
2904da230022 DOM refactoring
sergey
parents:
diff changeset
138 my %props = map {
2904da230022 DOM refactoring
sergey
parents:
diff changeset
139 $_->name, 1
2904da230022 DOM refactoring
sergey
parents:
diff changeset
140 } $data->GetMeta(PropertyInfo, sub { $_->access == ACCESS_PUBLIC }, 1 );
2904da230022 DOM refactoring
sergey
parents:
diff changeset
141
2904da230022 DOM refactoring
sergey
parents:
diff changeset
142 my %values = map {
2904da230022 DOM refactoring
sergey
parents:
diff changeset
143 $_,
2904da230022 DOM refactoring
sergey
parents:
diff changeset
144 $data->$_();
2904da230022 DOM refactoring
sergey
parents:
diff changeset
145 } keys %props;
2904da230022 DOM refactoring
sergey
parents:
diff changeset
146
2904da230022 DOM refactoring
sergey
parents:
diff changeset
147 return $this->Transform(\%values);
2904da230022 DOM refactoring
sergey
parents:
diff changeset
148 } else {
2904da230022 DOM refactoring
sergey
parents:
diff changeset
149 die OperationException->new("Don't know how to transform $data");
2904da230022 DOM refactoring
sergey
parents:
diff changeset
150 }
2904da230022 DOM refactoring
sergey
parents:
diff changeset
151
2904da230022 DOM refactoring
sergey
parents:
diff changeset
152 return $this->_navi->Current;
2904da230022 DOM refactoring
sergey
parents:
diff changeset
153 }
2904da230022 DOM refactoring
sergey
parents:
diff changeset
154
2904da230022 DOM refactoring
sergey
parents:
diff changeset
155 sub buildErrors {
2904da230022 DOM refactoring
sergey
parents:
diff changeset
156 my ($this) = @_;
2904da230022 DOM refactoring
sergey
parents:
diff changeset
157
2904da230022 DOM refactoring
sergey
parents:
diff changeset
158 return $this->_navi->buildErrors;
2904da230022 DOM refactoring
sergey
parents:
diff changeset
159 }
2904da230022 DOM refactoring
sergey
parents:
diff changeset
160
2904da230022 DOM refactoring
sergey
parents:
diff changeset
161 1;
2904da230022 DOM refactoring
sergey
parents:
diff changeset
162
2904da230022 DOM refactoring
sergey
parents:
diff changeset
163 __END__
2904da230022 DOM refactoring
sergey
parents:
diff changeset
164
2904da230022 DOM refactoring
sergey
parents:
diff changeset
165 =pod
2904da230022 DOM refactoring
sergey
parents:
diff changeset
166
2904da230022 DOM refactoring
sergey
parents:
diff changeset
167 =head1 NAME
2904da230022 DOM refactoring
sergey
parents:
diff changeset
168
2904da230022 DOM refactoring
sergey
parents:
diff changeset
169 C<IMPL::DOM::Transform::ObjectToDOM> -преобразование объекта
2904da230022 DOM refactoring
sergey
parents:
diff changeset
170
2904da230022 DOM refactoring
sergey
parents:
diff changeset
171 =head1 SYNOPSIS
2904da230022 DOM refactoring
sergey
parents:
diff changeset
172
2904da230022 DOM refactoring
sergey
parents:
diff changeset
173 =cut