| 1 | package UR::Object::View::Aspect; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | require UR; | ||
| 6 | |||
| 7 | our $VERSION = "0.46"; # UR $VERSION;; | ||
| 8 | |||
| 9 | class UR::Object::View::Aspect { | ||
| 10 | id_by => [ | ||
| 11 | parent_view => { is => 'UR::Object::View', id_by => 'parent_view_id', | ||
| 12 | doc => "the id of the view object this is an aspect-of" }, | ||
| 13 | |||
| 14 | number => { is => 'Integer', | ||
| 15 | doc => "aspects of a view are numbered" }, | ||
| 16 | ], | ||
| 17 | has => [ | ||
| 18 | name => { is => 'Text', | ||
| 19 | is_mutable => 0, | ||
| 20 | doc => 'the name of the property/method on the subject which returns the value to be viewed' }, | ||
| 21 | ], | ||
| 22 | has_optional => [ | ||
| 23 | label => { is => 'Text', | ||
| 24 | doc => 'display name for this aspect' }, | ||
| 25 | |||
| 26 | position => { is => 'Scalar', | ||
| 27 | doc => 'position of this aspect within the parent view (meaning is view and toolkit dependent)' }, | ||
| 28 | |||
| 29 | delegate_view => { is => 'UR::Object::View', id_by => 'delegate_view_id', | ||
| 30 | doc => "This aspect gets rendered via another view" }, | ||
| 31 | ], | ||
| 32 | }; | ||
| 33 | |||
| 34 | sub create { | ||
| 35 | my $class = shift; | ||
| 36 | my($bx,%extra) = $class->define_boolexpr(@_); | ||
| 37 | |||
| 38 | # TODO: it would be nice to have this in the class definition: | ||
| 39 | # increment_for => 'parent_view' | ||
| 40 | unless ($bx->value_for('number')) { | ||
| 41 | if (my $parent_view_id = $bx->value_for('parent_view_id')) { | ||
| 42 | my $parent_view = UR::Object::View->get($parent_view_id); | ||
| 43 | my @previous_aspects = $parent_view->aspects; | ||
| 44 | $bx = $bx->add_filter(number => scalar(@previous_aspects)+1); | ||
| 45 | } | ||
| 46 | } | ||
| 47 | unless ($bx->value_for('label')) { | ||
| 48 | if (my $label = $bx->value_for('name')) { | ||
| 49 | $label =~ s/_/ /g; | ||
| 50 | $bx = $bx->add_filter(label => $label); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | if (keys %extra) { | ||
| 55 | # This is a sub-view | ||
| 56 | my $delegate_subject_class_name; | ||
| 57 | if (exists $extra{'subject_class_name'}) { | ||
| 58 | $delegate_subject_class_name = $extra{'subject_class_name'}; | ||
| 59 | } else { | ||
| 60 | # FIXME This duplicates functionality below in generate_delegate_view, but generate_delegate_view() | ||
| 61 | # doesn't take any args to tweak the properties of that delegated view :( | ||
| 62 | # Try to figure it out based on the name of the aspect... | ||
| 63 | my $parent_view; | ||
| 64 | if (my $view_id = $bx->value_for('parent_view_id')) { | ||
| 65 | $parent_view = UR::Object::View->get($view_id); | ||
| 66 | } elsif ($bx->specifies_value_for('parent_view')) { | ||
| 67 | $parent_view = $bx->value_for('parent_view'); | ||
| 68 | } | ||
| 69 | unless ($parent_view) { | ||
| 70 | Carp::croak("Can't determine parent view from keys/values: ",join(', ', map { sprintf("%s => '%s'", $_, $extra{$_}) } keys %extra)); | ||
| 71 | } | ||
| 72 | |||
| 73 | my $class_meta = $parent_view->subject_class_name->__meta__; | ||
| 74 | unless ($class_meta) { | ||
| 75 | Carp::croak("No class metadata for class " | ||
| 76 | . $parent_view->subject_class_meta | ||
| 77 | . ". Can't create delegate view on aspect named " | ||
| 78 | . $bx->value_for('name') ); | ||
| 79 | } | ||
| 80 | |||
| 81 | my $property_meta = $class_meta->property_meta_for_name($bx->value_for('name')); | ||
| 82 | unless ($property_meta) { | ||
| 83 | Carp::croak("No property metadata for class " . $class_meta->class_name | ||
| 84 | . " property " . $bx->value_for('name') | ||
| 85 | . ". Can't create delegate view on aspect named " . $bx->value_for('name')); | ||
| 86 | } | ||
| 87 | |||
| 88 | unless ($property_meta->data_type) { | ||
| 89 | Carp::croak("Property metadata for class " . $class_meta->class_name | ||
| 90 | . " property " . $property_meta->property_name | ||
| 91 | . " has no data_type. Can't create delegate view on aspect named " . $bx->value_for('name')); | ||
| 92 | } | ||
| 93 | |||
| 94 | $delegate_subject_class_name = $property_meta->data_type; | ||
| 95 | } | ||
| 96 | unless ($delegate_subject_class_name) { | ||
| 97 | Carp::croak("Can't determine subject_class_name for delegate view on aspect named " . $bx->value_for('name')); | ||
| 98 | } | ||
| 99 | |||
| 100 | my $delegate_view = $delegate_subject_class_name->create_view( | ||
| 101 | perspective => $bx->value_for('perspective'), | ||
| 102 | toolkit => $bx->value_for('toolkit'), | ||
| 103 | %extra | ||
| 104 | ); | ||
| 105 | unless ($delegate_view) { | ||
| 106 | Carp::croak("Can't create delegate view for aspect named " . $bx->value_for('name') . ": ".$delegate_subject_class_name->error_message); | ||
| 107 | } | ||
| 108 | #$bx->add_filter(delegate_view_id => $delegate_view->id); | ||
| 109 | $bx = $bx->add_filter(delegate_view => $delegate_view); | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | my $self = $class->SUPER::create($bx); | ||
| 114 | return unless $self; | ||
| 115 | |||
| 116 | my $name = $self->name; | ||
| 117 | unless ($name) { | ||
| 118 | $self->error_message("No name specified for aspect!"); | ||
| 119 | $self->delete; | ||
| 120 | return; | ||
| 121 | } | ||
| 122 | |||
| 123 | return $self; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub _look_for_recursion { | ||
| 127 | my $self = shift; | ||
| 128 | |||
| 129 | my $parent_view = $self->parent_view; | ||
| 130 | my $subject = $parent_view->subject; | ||
| 131 | |||
| 132 | $parent_view = $parent_view->parent_view; | ||
| 133 | while($parent_view) { | ||
| 134 | return 1 if ($parent_view->subject eq $subject); | ||
| 135 | $parent_view = $parent_view->parent_view; | ||
| 136 | } | ||
| 137 | return 0; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub generate_delegate_view { | ||
| 141 | no warnings; | ||
| 142 | my $self = shift; | ||
| 143 | |||
| 144 | my $parent_view = $self->parent_view; | ||
| 145 | |||
| 146 | my $name = $self->name; | ||
| 147 | my $subject_class_name = $parent_view->subject_class_name; | ||
| 148 | |||
| 149 | my $retval; | ||
| 150 | |||
| 151 | my $property_meta = $subject_class_name->__meta__->property($name); | ||
| 152 | my $aspect_type; | ||
| 153 | if ($property_meta) { | ||
| 154 | $aspect_type = $property_meta->_data_type_as_class_name; | ||
| 155 | unless ($aspect_type) { | ||
| 156 | Carp::confess("Undefined aspect type. Set 'is' for $name in class " . $property_meta->class_name); | ||
| 157 | } | ||
| 158 | |||
| 159 | unless ($aspect_type) { | ||
| 160 | if (my $delegated_to_meta = $property_meta->final_property_meta) { | ||
| 161 | $aspect_type = $delegated_to_meta->data_type; | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | unless ($aspect_type) { | ||
| 166 | Carp::confess("Property meta for class ".$property_meta->class_name." property ".$property_meta->property_name." has no data_type"); | ||
| 167 | } | ||
| 168 | |||
| 169 | unless ($aspect_type->can("__meta__")) { | ||
| 170 | Carp::croak("$aspect_type has no meta data? cannot generate a view for $subject_class_name $name!"); | ||
| 171 | } | ||
| 172 | } | ||
| 173 | else { | ||
| 174 | unless ($subject_class_name->can($name)) { | ||
| 175 | $self->error_message("No property/method $name found on $subject_class_name! Invalid aspect!"); | ||
| 176 | $self->delete; | ||
| 177 | Carp::croak($self->error_message); | ||
| 178 | } | ||
| 179 | $aspect_type = 'UR::Value::Text' | ||
| 180 | } | ||
| 181 | |||
| 182 | my $aspect_meta = $aspect_type->__meta__; | ||
| 183 | |||
| 184 | my $delegate_view; | ||
| 185 | eval { | ||
| 186 | $delegate_view = $aspect_type->create_view( | ||
| 187 | subject_class_name => $aspect_type, | ||
| 188 | perspective => $parent_view->perspective, | ||
| 189 | toolkit => $parent_view->toolkit, | ||
| 190 | parent_view => $parent_view, | ||
| 191 | aspects => [], | ||
| 192 | ); | ||
| 193 | }; | ||
| 194 | |||
| 195 | unless ($delegate_view) { | ||
| 196 | # try again using the "default" perspective | ||
| 197 | my $err1 = $@; | ||
| 198 | eval { | ||
| 199 | $delegate_view = $aspect_type->create_view( | ||
| 200 | subject_class_name => $aspect_type, | ||
| 201 | perspective => 'default', | ||
| 202 | toolkit => $parent_view->toolkit, | ||
| 203 | parent_view => $parent_view, | ||
| 204 | aspects => [], | ||
| 205 | ); | ||
| 206 | }; | ||
| 207 | my $err2 = $@; | ||
| 208 | |||
| 209 | unless ($delegate_view) { | ||
| 210 | $self->error_message( | ||
| 211 | "Error creating delegate view for $name ($aspect_type)! $err1\n" | ||
| 212 | . "Also failed to fall back to the default perspective for $name ($aspect_type)! $err2" | ||
| 213 | ); | ||
| 214 | return; | ||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | my @default_aspects_params = $delegate_view->_resolve_default_aspects(); | ||
| 219 | |||
| 220 | # add aspects which do not "go backward" | ||
| 221 | # no one wants to see an order, with a list of line items, which re-reprsent thier order on each | ||
| 222 | for my $aspect_params (@default_aspects_params) { | ||
| 223 | my $aspect_param_name = (ref($aspect_params) ? $aspect_params->{name} : $aspect_params); | ||
| 224 | my $aspect_property_meta = $aspect_meta->property($aspect_param_name); | ||
| 225 | no strict; no warnings; | ||
| 226 | next if (!$aspect_property_meta or !$property_meta); | ||
| 227 | if ($aspect_property_meta->reverse_as() eq $name) { | ||
| 228 | |||
| 229 | } | ||
| 230 | elsif ($property_meta->reverse_as eq $aspect_param_name) { | ||
| 231 | } | ||
| 232 | else { | ||
| 233 | $delegate_view->add_aspect(ref($aspect_params) ? %$aspect_params : $aspect_params); | ||
| 234 | } | ||
| 235 | } | ||
| 236 | $self->delegate_view($delegate_view); | ||
| 237 | $retval = $delegate_view; | ||
| 238 | |||
| 239 | return $retval; | ||
| 240 | } | ||
| 241 | |||
| 242 | 1; | ||
| 243 | |||
| 244 | =pod | ||
| 245 | |||
| 246 | =head1 NAME | ||
| 247 | |||
| 248 | UR::Object::View::Aspect - a specification for one aspect of a view | ||
| 249 | |||
| 250 | =head1 SYNOPSIS | ||
| 251 | |||
| 252 | my $v = $o->create_view( | ||
| 253 | perspective => 'default', | ||
| 254 | toolkit => 'xml', | ||
| 255 | aspects => [ | ||
| 256 | 'id', | ||
| 257 | 'name', | ||
| 258 | 'title', | ||
| 259 | { | ||
| 260 | name => 'department', | ||
| 261 | perspective => 'logo' | ||
| 262 | }, | ||
| 263 | { | ||
| 264 | name => 'boss', | ||
| 265 | label => 'Supervisor', | ||
| 266 | aspects => [ | ||
| 267 | 'name', | ||
| 268 | 'title', | ||
| 269 | { | ||
| 270 | name => 'subordinates', | ||
| 271 | perspective => 'graph by title' | ||
| 272 | } | ||
| 273 | ] | ||
| 274 | } | ||
| 275 | ] | ||
| 276 | ); | ||
| 277 | |||
| 278 | =cut | ||
| 279 | |||
| 280 |