| 1 | package UR::Object::Command::Copy; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings FATAL => 'all'; | ||
| 5 | |||
| 6 | class UR::Object::Command::Copy { | ||
| 7 | is => 'Command::V2', | ||
| 8 | is_abstract => 1, | ||
| 9 | has => { | ||
| 10 | changes => { | ||
| 11 | is => 'Text', | ||
| 12 | is_many => 1, | ||
| 13 | doc => 'A name/value comma-separated list of changes', | ||
| 14 | }, | ||
| 15 | }, | ||
| 16 | }; | ||
| 17 | |||
| 18 | sub help_detail { | ||
| 19 | <<HELP; | ||
| 20 | Any non-delegated, non-ID properties may be specified with an operator and a value. | ||
| 21 | |||
| 22 | Valid operators are '=', '+=', '-=', and '.='; function is same as in Perl. | ||
| 23 | Example: | ||
| 24 | --changes "name.=-RT101912,foo=bar" | ||
| 25 | |||
| 26 | A value of 'undef' may be used to pass a Perl undef as the value. Either `foo=` [safer] or `foo=''` can be used to set the value to an empty string. | ||
| 27 | HELP | ||
| 28 | } | ||
| 29 | |||
| 30 | sub execute { | ||
| 31 | my $self = shift; | ||
| 32 | |||
| 33 | my $tx = UR::Context::Transaction->begin; | ||
| 34 | |||
| 35 | my $copy = $self->source->copy(); | ||
| 36 | |||
| 37 | my $failure; | ||
| 38 | for my $change ($self->changes) { | ||
| 39 | my ($key, $op, $value) = $change =~ /^(.+?)(=|\+=|\-=|\.=)(.*)$/; | ||
| 40 | $failure = "Invalid change: $change" and last | ||
| 41 | unless $key and defined $op; | ||
| 42 | $failure = sprintf('Invalid property %s for %s', $key, $copy->__display_name__) and last | ||
| 43 | if !$copy->can($key); | ||
| 44 | |||
| 45 | $value = undef if $value eq ''; | ||
| 46 | |||
| 47 | if ($op eq '=') { | ||
| 48 | $copy->$key($value); | ||
| 49 | } | ||
| 50 | elsif ($op eq '+=') { | ||
| 51 | $copy->$key($copy->$key + $value); | ||
| 52 | } | ||
| 53 | elsif ($op eq '-=') { | ||
| 54 | $copy->$key($copy->$key - $value); | ||
| 55 | } | ||
| 56 | elsif ($op eq '.=') { | ||
| 57 | $copy->$key($copy->$key . $value); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | if ($failure or !$tx->commit ) { | ||
| 62 | $tx->rollback; | ||
| 63 | $self->fatal_message($failure || 'Failed to commit software transaction!'); | ||
| 64 | } | ||
| 65 | |||
| 66 | $self->status_message("NEW\t%s\t%s", $copy->class, $copy->__display_name__); | ||
| 67 | 1; | ||
| 68 | } | ||
| 69 | |||
| 70 | 1; |