blib/lib/Keystone/Resolver/DB/Object.pm untested

filter: Statementstmt 0.0% 0 / 219 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 27 totaltotal 0.0% 0 / 399 CC 38 SCAR 28.3
CC
38
Cov
0%
editable_fields
42.0
field
42.0
virtual_field
30.0
CRAP 17.0
1 # $Id: Object.pm,v 1.34 2008-04-29 17:05:38 mike Exp $
2  
3 package Keystone::Resolver::DB::Object;
4  
5 use strict;
6 use warnings;
7 use Carp;
8  
9  
10 sub new {
11 my $class = shift();
12 my($db) = shift();
13  
14 my @fields = $class->physical_fields();
15 my %hash = (_db => $db);
16 foreach my $i (1 .. @fields) {
17 my $key = $fields[$i-1];
18 my $value = $_[$i-1];
19 $hash{$key} = $value;
20 }
21  
22 return bless \%hash, $class;
23 }
24  
25  
26 sub class {
27 my $this = shift();
28 my $class = ref $this;
29 $class =~ s/^Keystone::Resolver::DB:://;
30 return $class;
31 }
32  
33  
34 # Accessors and delegations
35 sub db { shift()->{_db} }
36 sub log { shift()->{_db}->log(@_) }
37 sub quote { shift()->{_db}->quote(@_) }
38  
39 # Default implementations of subclass-specific virtual functions
40 # fields() must be explicitly provided for searchable classes
41 # virtual_fields() must be explicitly provided for searchable classes
42 sub mandatory_fields { qw() }
43 # search_fields() must be explicitly provided for searchable classes
44 # display_fields() must be explicitly provided for searchable classes
45 sub fulldisplay_fields { shift()->display_fields(@_) }
46 sub field_map { {} }
47  
48  
49 # Returns an empty array if it's OK to delete this object, or
50 # otherwise an array of one or more strings, each specifying a reason
51 # why not. Can be overridden by subclasses, but by default insists on
52 # no non-dependent links.
53 #
54 sub undeletable {
55 my $this = shift();
56  
57 my @reasons;
58 my %fields = $this->fields();
59 foreach my $key (sort keys %fields) {
60 my $ref = $fields{$key};
61 if (ref $ref && defined $ref->[3]) {
62 my($linkfield, $linkclass, $linkto) = @$ref;
63 ### This is wasteful: it would be better to use a method
64 # that only counts hits instead of fetching all the data
65 # and constructing all the objects, but there is as yet
66 # no such method,
67 my @hits = $this->db()->find($linkclass, undef, $linkto,
68 $this->field($linkfield));
69 my $n = @hits;
70 if ($n == 1) {
71 push @reasons, "a $linkclass depends on it";
72 } elsif ($n != 0) {
73 push @reasons, "$n $linkclass objects depend on it";
74 }
75 }
76 }
77  
78 return @reasons;
79 }
80  
81  
82 # Returns a list of all the field specified by fields(), with types
83 # drawn from fulldisplay_fields() where available and using "t" when
84 # not.
85 #
86 # Fields which are used as the link-field in a virtual-field recipe
87 # of the "dependent-link" type are omitted (e.g. service_type_id
88 # from the Service class, because it is the link-field in the
89 # service_type recipe).
90 #
91 # Virtual fields that are of not of the "dependent-link" type have a
92 # exclude-at-creation-time attribute prepended to their type, if they
93 # don't already have it.
94 #
95 sub editable_fields {
96 my $class = shift();
97  
98 my @allfields = $class->fields();
99 my %hash = @allfields;
100 my(%omitFields, %virtualFields);
101 foreach my $key (keys %hash) {
102 my $value = $hash{$key};
103 if (defined $value && ref $value) {
104 ### The correct test here might not be for @$value==3 but
105 # something like defined $value[3]. See all the virtual
106 # fields in Service.pm and think harder.
107 if (@$value == 3) {
108 $omitFields{$value->[0]} = 1;
109 } else {
110 $virtualFields{$key} = 1;
111 }
112 }
113 }
114  
115 foreach my $skip ($class->uneditable_fields()) {
116 $omitFields{$skip} = 1;
117 }
118  
119 my %fdfields = $class->fulldisplay_fields();
120 my @res;
121  
122 while (@allfields) {
123 my $name = shift @allfields;
124 my $recipe = shift @allfields;
125 if (defined $omitFields{$name}) {
126 warn "omitting '$name' from editable_field($class)\n";
127 next;
128 }
129  
130 my $display = $fdfields{$name} || "t";
131 if (defined $virtualFields{$name}) {
132 $display = "X$display" if $display !~ /X/;
133 warn "made '$name' readonly '$display' in editable_field($class)\n";
134 }
135  
136 push @res, ($name, $display);
137 }
138  
139 return @res;
140 }
141  
142  
143 # List of fields to omit from the return of editable fields (unless
144 # that method has been overridden, of course). This list is empty in
145 # general, but can be used to knock out link-fields and suchlike as
146 # required.
147 #
148 sub uneditable_fields {
149 return ();
150 }
151  
152  
153 sub physical_fields {
154 my $class = shift();
155  
156 my @allfields = $class->fields();
157 my @pfields;
158 while (@allfields) {
159 my $name = shift @allfields;
160 my $recipe = shift @allfields;
161 push @pfields, $name if !defined $recipe;
162 }
163  
164 return @pfields;
165 }
166  
167  
168 sub virtual_fields {
169 my $class = shift();
170  
171 my @allfields = $class->fields();
172 my @vfields;
173 while (@allfields) {
174 my $name = shift @allfields;
175 my $recipe = shift @allfields;
176 push @vfields, $name, $recipe if defined $recipe;
177 }
178  
179 return @vfields;
180 }
181  
182  
183 # Parses full-type strings such as those used on the RHS of
184 # display_fields() arrays, e.g. "c", "Lt", "Rn". Returns an array of
185 # four elements:
186 # 0: whether the field is a link
187 # 1: whether the field is readonly
188 # 2: the field's core type
189 # 3: whether the field should be excluded at creation time.
190 # (It would make more sense if 2 and 3 were reversed, but existing
191 # code assumes the first three elements from before the fourth was
192 # added.)
193 #
194 sub analyse_type {
195 my $_unused_this = shift();
196 my($type, $field) = @_;
197  
198 return (undef, undef, $type) if ref $type;
199 my $link = ($type =~ s/L//);
200 my $readonly = ($type =~ s/R//);
201 my $exclude = ($type =~ s/X//);
202 # Special-case the fields that we know may never change
203 $readonly = 1 if grep { $field eq $_ } qw(id tag);
204  
205 return ($link, $readonly, $type, $exclude);
206 }
207  
208  
209 # Returns name of CSS class to be used for displaying fields of the
210 # specified type. ### Knows about what's in "style.css"
211 #
212 sub type2class {
213 my $this = shift();
214 my($type) = @_;
215  
216 return "enum" if ref($type) eq "ARRAY";
217 return $type if grep { $type eq $_ } qw(t c n b);
218 return "error";
219 }
220  
221  
222 sub create {
223 my $class = shift();
224 my($db, %maybe_data) = @_;
225  
226 my %data;
227 foreach my $key (keys %maybe_data) {
228 $data{$key} = $maybe_data{$key}
229 if $maybe_data{$key} ne "" &&
230 grep { $_ eq $key } $class->physical_fields();
231 }
232  
233 my $table = $class->table();
234 my $sql = "INSERT INTO " . $db->quote($table) .
235 " (" . join(", ", map { $db->quote($_) } sort keys %data) . ") VALUES" .
236 " (" . join(", ", map { sql_quote($data{$_}) } sort keys %data) . ")";
237 $db->do($sql);
238 my $id = $db->last_insert_id($table);
239 die "can't get new record's ID" if !defined $id;
240  
241 return $db->find1($class, id => $id);
242 }
243  
244  
245 sub sql_quote {
246 my($text) = @_;
247 my $sq = "'";
248  
249 $text =~ s/$sq/''/g;
250 return "'$text'";
251 }
252  
253  
254 # Returns a label to be used on-screen for the specified field
255 sub label {
256 my $this = shift();
257 my($field, $label) = @_;
258  
259 return $label if defined $label;
260 my $map = $this->field_map();
261 $label = $map->{$field};
262 return $label if defined $label;
263  
264 # No explicit label passed, and none in config: use default rules
265 $label = $field;
266 $label =~ s/_/ /g;
267 return ucfirst($label);
268 }
269  
270  
271 # Return the components needed to identify a linked-to object
272 sub link {
273 my $this = shift();
274 my($field) = @_;
275  
276 my %virtual = $this->virtual_fields();
277 my $ref = $virtual{$field};
278 return undef if !defined $ref;
279 my($linkfield, $linkclass, $linkto) = @$ref;
280 my $linkid = $this->field($linkfield);
281  
282 return ($linkclass, $linkto, $linkid, $linkfield);
283 }
284  
285  
286 # Returns the number of fields modified, dies on error
287 sub update {
288 my $this = shift();
289 my(%maybe_data) = @_;
290  
291 my %data;
292 foreach my $key (keys %maybe_data) {
293 $data{$key} = $maybe_data{$key}
294 if (!defined $this->field($key) ||
295 $maybe_data{$key} ne $this->field($key));
296 }
297  
298 return 0 if !%data; # nothing to do
299 my $sql = "UPDATE " . $this->quote($this->table()) . " SET " .
300 join(", ", map { $this->quote($_) . " = " . sql_quote($data{$_}) } sort keys %data) .
301 " WHERE " . $this->quote("id") . " = " . $this->id();
302  
303 $this->db()->do($sql);
304 foreach my $key (keys %data) {
305 $this->field($key, $data{$key});
306 }
307  
308 return scalar keys %data;
309 }
310  
311  
312 sub delete {
313 my $this = shift();
314  
315 my $sql = "DELETE FROM " . $this->quote($this->table()) .
316 " WHERE " . $this->quote("id") . " = " . $this->id();
317  
318 $this->db()->do($sql);
319 # Wow, that embarrasingly easy
320 }
321  
322  
323 sub field {
324 my $this = shift();
325 my($fieldname, $value) = @_;
326  
327 die "$this: request for system-function field '$fieldname'"
328 if grep { $_ eq $fieldname } qw(table fields mandatory_fields
329 physical_fields
330 virtual_fields search_fields
331 sort_fields display_fields
332 fulldisplay_fields field_map
333 field);
334  
335 if (grep { $_ eq $fieldname } $this->physical_fields()) {
336 $this->{$fieldname} = $value if defined $value;
337 return $this->{$fieldname};
338 }
339  
340 my %virtual;
341 eval { %virtual = $this->virtual_fields() };
342 if (!defined $virtual{$fieldname}) {
343 confess "$this: field `$fieldname' not defined";
344 } elsif (defined $value) {
345 die "can't set virtual field '$fieldname'='$value'";
346 } else {
347 return $this->virtual_field($fieldname);
348 }
349 }
350  
351  
352 sub virtual_field {
353 my $this = shift();
354 my($fieldname) = @_;
355  
356 my %virtual = $this->virtual_fields();
357 my $ref = $virtual{$fieldname};
358 my($linkfield, $class, $linkto, $sortby, $valfield) = @$ref;
359  
360 my $value = $this->field($linkfield);
361 return undef if !defined $value; # e.g. link-field in new record
362  
363 if (defined $sortby) {
364 # Link is to multiple records
365 my @obj = $this->db()->find($class, $sortby, $linkto, $value);
366 #warn "$this->virtual_fields($fieldname) -> @obj";
367 return [ @obj ];
368 }
369  
370 # Link is to a single "parent" record
371 my $obj = $this->db()->find1($class, $linkto, $value);
372 if (!defined $obj) {
373 # The link is broken! The Dark Lord's reign begins!
374 return "[$class:$linkto:$value]";
375 }
376  
377 if (defined $valfield) {
378 return $obj->field($valfield);
379 } else {
380 return $obj->render_name();
381 }
382 }
383  
384  
385 sub AUTOLOAD {
386 my $this = shift();
387  
388 my $class = ref $this || $this;
389 use vars qw($AUTOLOAD);
390 (my $fieldname = $AUTOLOAD) =~ s/.*:://;
391 die "$class: request for field '$fieldname' on undefined object"
392 if !defined $this;
393  
394 return $this->field($fieldname, @_);
395 }
396  
397  
398 sub DESTROY {} # Avoid warning from AUTOLOAD()
399  
400  
401 sub render {
402 my $this = shift();
403 my $class = ref($this);
404  
405 my $name;
406 eval {
407 $name = $this->tag();
408 }; if ($@ || !$name) {
409 undef $@; ### should this really be necessary?
410 eval {
411 $name = $this->name();
412 }; if ($@ || !$name) {
413 undef $@; ### should this really be necessary?
414 $name = undef;
415 }
416 }
417  
418 my $text = "$class " . $this->id();
419 $text .= " ($name)" if defined $name;
420 return $text;
421 }
422  
423  
424 sub render_name {
425 my $this = shift();
426  
427 my $res;
428 eval { $res = $this->name() };
429 if (!$@ && defined $res) {
430 #warn "returning name()='$res'";
431 return $res;
432 }
433 eval { $res = $this->tag() };
434 if (!$@ && defined $res) {
435 #warn "returning tag()='$res'";
436 return $res;
437 }
438  
439 my $id = $this->id();
440 if (defined $id) {
441 #warn "returning id '$id'";
442 return ref($this) . " " . $id;
443 }
444  
445 #warn "returning new";
446 return "[NEW]";
447 }
448  
449  
450 1;