| 1 | package UR::Object::Command::FetchAndDo; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use UR; | ||
| 7 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 8 | use Data::Dumper; | ||
| 9 | |||
| 10 | class UR::Object::Command::FetchAndDo { | ||
| 11 | is => 'Command', | ||
| 12 | is_abstract => 1, | ||
| 13 | has => [ | ||
| 14 | subject_class => { | ||
| 15 | is => 'UR::Object::Type', | ||
| 16 | id_by => 'subject_class_name', | ||
| 17 | }, | ||
| 18 | filter => { | ||
| 19 | is => 'Text', | ||
| 20 | is_optional => 1, | ||
| 21 | doc => 'Filter results based on the parameters. See below for how to.' | ||
| 22 | }, | ||
| 23 | _fields => { | ||
| 24 | is_many => 1, | ||
| 25 | is_optional => 1, | ||
| 26 | doc => 'Methods which the caller intends to use on the fetched objects. May lead to pre-fetching the data.' | ||
| 27 | }, | ||
| 28 | ], | ||
| 29 | }; | ||
| 30 | |||
| 31 | |||
| 32 | ######################################################################## | ||
| 33 | |||
| 34 | sub help_detail { | ||
| 35 | my $class = shift; | ||
| 36 | |||
| 37 | return $class->_filter_doc; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub _filter_doc { | ||
| 41 | my $class = shift; | ||
| 42 | |||
| 43 | my $doc = <<EOS; | ||
| 44 | Filtering: | ||
| 45 | ---------- | ||
| 46 | Create filter equations by combining filterable properties with operators and | ||
| 47 | values. | ||
| 48 | Combine and separate these 'equations' by commas. | ||
| 49 | Use single quotes (') to contain values with spaces: name='genome institute' | ||
| 50 | Use percent signs (%) as wild cards in like (~). | ||
| 51 | Use backslash or single quotes to escape characters which have special meaning | ||
| 52 | to the shell such as < > and & | ||
| 53 | |||
| 54 | Operators: | ||
| 55 | ---------- | ||
| 56 | = (exactly equal to) | ||
| 57 | ~ (like the value) | ||
| 58 | : (in the list of several values, slash "/" separated) | ||
| 59 | (or between two values, dash "-" separated) | ||
| 60 | > (greater than) | ||
| 61 | >= (greater than or equal to) | ||
| 62 | < (less than) | ||
| 63 | <= (less than or equal to) | ||
| 64 | |||
| 65 | Examples: | ||
| 66 | --------- | ||
| 67 | EOS | ||
| 68 | if (my $help_synopsis = $class->help_synopsis) { | ||
| 69 | $doc .= " $help_synopsis\n"; | ||
| 70 | } else { | ||
| 71 | $doc .= <<EOS | ||
| 72 | lister-command --filter name=Bob --show id,name,address | ||
| 73 | lister-command --filter name='something with space',employees\>200,job~%manager | ||
| 74 | lister-command --filter cost:20000-90000 | ||
| 75 | lister-command --filter answer:yes/maybe | ||
| 76 | EOS | ||
| 77 | } | ||
| 78 | |||
| 79 | $doc .= <<EOS; | ||
| 80 | |||
| 81 | Filterable Properties: | ||
| 82 | ---------------------- | ||
| 83 | EOS | ||
| 84 | |||
| 85 | # Try to get the subject class name | ||
| 86 | my $self = $class->create; | ||
| 87 | if ( not $self->subject_class_name | ||
| 88 | and my $subject_class_name = $self->_resolved_params_from_get_options->{subject_class_name} ) { | ||
| 89 | $self = $class->create(subject_class_name => $subject_class_name); | ||
| 90 | } | ||
| 91 | |||
| 92 | if ( $self->subject_class_name ) { | ||
| 93 | if ( my @properties = $self->_subject_class_filterable_properties ) { | ||
| 94 | my $longest_name = 0; | ||
| 95 | foreach my $property ( @properties ) { | ||
| 96 | my $name_len = length($property->property_name); | ||
| 97 | $longest_name = $name_len if ($name_len > $longest_name); | ||
| 98 | } | ||
| 99 | |||
| 100 | for my $property ( @properties ) { | ||
| 101 | my $property_doc = $property->doc; | ||
| 102 | unless ($property_doc) { | ||
| 103 | eval { | ||
| 104 | foreach my $ancestor_class_meta ( $property->class_meta->ancestry_class_metas ) { | ||
| 105 | my $ancestor_property_meta = $ancestor_class_meta->property_meta_for_name($property->property_name); | ||
| 106 | if ($ancestor_property_meta and $ancestor_property_meta->doc) { | ||
| 107 | $property_doc = $ancestor_property_meta->doc; | ||
| 108 | last; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | }; | ||
| 112 | } | ||
| 113 | $property_doc ||= ' (undocumented)'; | ||
| 114 | $property_doc =~ s/\n//gs; # Get rid of embeded newlines | ||
| 115 | |||
| 116 | my $data_type = $property->data_type || ''; | ||
| 117 | $data_type = ucfirst(lc $data_type); | ||
| 118 | |||
| 119 | $doc .= sprintf(" %${longest_name}s ($data_type): $property_doc\n", | ||
| 120 | $property->property_name); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | else { | ||
| 124 | $doc .= sprintf(" %s\n", $self->error_message); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | else { | ||
| 128 | $doc .= " Can't determine the list of filterable properties without a subject_class_name"; | ||
| 129 | } | ||
| 130 | |||
| 131 | return $doc; | ||
| 132 | } | ||
| 133 | |||
| 134 | ######################################################################## | ||
| 135 | |||
| 136 | sub execute { | ||
| 137 | my $self = shift; | ||
| 138 | |||
| 139 | $self->_validate_subject_class | ||
| 140 | or return; | ||
| 141 | |||
| 142 | my $iterator = $self->_fetch | ||
| 143 | or return; | ||
| 144 | |||
| 145 | return $self->_do($iterator); | ||
| 146 | } | ||
| 147 | |||
| 148 | sub _validate_subject_class { | ||
| 149 | my $self = shift; | ||
| 150 | |||
| 151 | my $subject_class_name = $self->subject_class_name; | ||
| 152 | $self->error_message("No subject_class_name indicated.") | ||
| 153 | and return unless $subject_class_name; | ||
| 154 | |||
| 155 | $self->error_message( | ||
| 156 | sprintf( | ||
| 157 | 'This command is not designed to work on a base UR class (%s).', | ||
| 158 | $subject_class_name, | ||
| 159 | ) | ||
| 160 | ) | ||
| 161 | and return if $subject_class_name =~ /^UR::/; | ||
| 162 | |||
| 163 | UR::Object::Type->use_module_with_namespace_constraints($subject_class_name); | ||
| 164 | |||
| 165 | my $subject_class = $self->subject_class; | ||
| 166 | $self->error_message( | ||
| 167 | sprintf( | ||
| 168 | 'Can\'t get class meta object for class (%s). Is this class a properly declared UR::Object?', | ||
| 169 | $subject_class_name, | ||
| 170 | ) | ||
| 171 | ) | ||
| 172 | and return unless $subject_class; | ||
| 173 | |||
| 174 | $self->error_message( | ||
| 175 | sprintf( | ||
| 176 | 'Can\'t find method (all_property_metas) in %s. Is this a properly declared UR::Object class?', | ||
| 177 | $subject_class_name, | ||
| 178 | ) | ||
| 179 | ) | ||
| 180 | and return unless $subject_class->can('all_property_metas'); | ||
| 181 | |||
| 182 | return 1; | ||
| 183 | } | ||
| 184 | |||
| 185 | sub _subject_class_filterable_properties { | ||
| 186 | my $self = shift; | ||
| 187 | |||
| 188 | $self->_validate_subject_class | ||
| 189 | or return; | ||
| 190 | |||
| 191 | my %props = map { $_->property_name => $_ } | ||
| 192 | $self->subject_class->property_metas; | ||
| 193 | |||
| 194 | return map { $_->[1] } # These maps are to get around a bug in perl 5.8 | ||
| 195 | sort { $a->[0] cmp $b->[0] } # sort involving methdo calls inside the sort sub that | ||
| 196 | map { [ $_->property_name, $_ ] } # might do sorts of their own | ||
| 197 | grep { substr($_->property_name, 0, 1) ne '_' } # Skip 'private' properties starting with '_' | ||
| 198 | grep { ! $_->data_type or index($_->data_type, '::') == -1 } # Can't filter object-type properties from a lister, right? | ||
| 199 | values %props; | ||
| 200 | } | ||
| 201 | |||
| 202 | sub _hint_string { | ||
| 203 | return; | ||
| 204 | } | ||
| 205 | |||
| 206 | sub _base_filter { | ||
| 207 | return; | ||
| 208 | } | ||
| 209 | |||
| 210 | sub _complete_filter { | ||
| 211 | my $self = shift; | ||
| 212 | return join(',', grep { defined $_ } $self->_base_filter,$self->filter); | ||
| 213 | } | ||
| 214 | |||
| 215 | sub _fetch | ||
| 216 | { | ||
| 217 | my $self = shift; | ||
| 218 | my ($bool_expr, %extra) = UR::BoolExpr->resolve_for_string( | ||
| 219 | $self->subject_class_name, | ||
| 220 | $self->_complete_filter, | ||
| 221 | $self->_hint_string | ||
| 222 | ); | ||
| 223 | |||
| 224 | $self->error_message( sprintf('Unrecognized field(s): %s', join(', ', keys %extra)) ) | ||
| 225 | and return if %extra; | ||
| 226 | |||
| 227 | if (my $i = $self->subject_class_name->create_iterator($bool_expr)) { | ||
| 228 | return $i; | ||
| 229 | } | ||
| 230 | else { | ||
| 231 | $self->error_message($self->subject_class_name->error_message); | ||
| 232 | return; | ||
| 233 | } | ||
| 234 | } | ||
| 235 | |||
| 236 | sub _do | ||
| 237 | { | ||
| 238 | shift->error_message("Abstract class. Please implement a '_do' method in your subclass."); | ||
| 239 | return; | ||
| 240 | } | ||
| 241 | |||
| 242 | 1; | ||
| 243 | |||
| 244 | =pod | ||
| 245 | |||
| 246 | =head1 NAME | ||
| 247 | |||
| 248 | UR::Object::Command::FetchAndDo - Base class for fetching objects and then performing a function on/with them. | ||
| 249 | |||
| 250 | =head1 SYNOPSIS | ||
| 251 | |||
| 252 | package MyFecthAndDo; | ||
| 253 | |||
| 254 | use strict; | ||
| 255 | use warnings; | ||
| 256 | |||
| 257 | use above "UR"; | ||
| 258 | |||
| 259 | class MyFecthAndDo { | ||
| 260 | is => 'UR::Object::Command::FetchAndDo', | ||
| 261 | has => [ | ||
| 262 | # other properties... | ||
| 263 | ], | ||
| 264 | }; | ||
| 265 | |||
| 266 | sub _do { # required | ||
| 267 | my ($self, $iterator) = @_; | ||
| 268 | |||
| 269 | while (my $obj = $iterator->next) { | ||
| 270 | ... | ||
| 271 | } | ||
| 272 | |||
| 273 | return 1; | ||
| 274 | } | ||
| 275 | |||
| 276 | 1; | ||
| 277 | |||
| 278 | =head1 Provided by the Developer | ||
| 279 | |||
| 280 | =head2 _do (required) | ||
| 281 | |||
| 282 | Implement this method to 'do' unto the iterator. Return true for success, false for failure. | ||
| 283 | |||
| 284 | sub _do { | ||
| 285 | my ($self, $iterator) = @_; | ||
| 286 | |||
| 287 | while (my $obj = $iterator->next) { | ||
| 288 | ... | ||
| 289 | } | ||
| 290 | |||
| 291 | return 1; | ||
| 292 | } | ||
| 293 | |||
| 294 | =head2 subject_class_name (optional) | ||
| 295 | |||
| 296 | The subject_class_name is the class for which the objects will be fetched. It can be specified one of two main ways: | ||
| 297 | |||
| 298 | =over | ||
| 299 | |||
| 300 | =item I<by_the_end_user_on_the_command_line> | ||
| 301 | |||
| 302 | For this do nothing, the end user will have to provide it when the command is run. | ||
| 303 | |||
| 304 | =item I<by_the_developer_in the_class_declartion> | ||
| 305 | |||
| 306 | For this, in the class declaration, add a has key w/ arrayref of hashrefs. One of the hashrefs needs to be subject_class_name. Give it this declaration: | ||
| 307 | |||
| 308 | class MyFetchAndDo { | ||
| 309 | is => 'UR::Object::Command::FetchAndDo', | ||
| 310 | has => [ | ||
| 311 | subject_class_name => { | ||
| 312 | value => <CLASS NAME>, | ||
| 313 | is_constant => 1, | ||
| 314 | }, | ||
| 315 | ], | ||
| 316 | }; | ||
| 317 | |||
| 318 | =back | ||
| 319 | |||
| 320 | =head2 helps (optional) | ||
| 321 | |||
| 322 | Overwrite the help_brief, help_synopsis and help_detail methods to provide specific help. If overwiting the help_detail method, use call '_filter_doc' to get the filter documentation and usage to combine with your specific help. | ||
| 323 | |||
| 324 | =cut | ||
| 325 | |||
| 326 | #$HeadURL: svn+ssh://svn/srv/svn/gscpan/distro/ur-bundle/trunk/lib/UR/Object/Command/FetchAndDo.pm $ | ||
| 327 | #$Id: FetchAndDo.pm 47408 2009-06-01 03:53:45Z ssmith $# |