| 1 | package WWW::SFDC::Role::CRUD; | ||
| 2 | # ABSTRACT: Shared methods between partner and tooling APIs | ||
| 3 | |||
| 4 | use 5.12.0; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use List::NSect 'spart'; | ||
| 9 | use Log::Log4perl ':easy'; | ||
| 10 | use Moo::Role; | ||
| 11 | use Scalar::Util 'blessed'; | ||
| 12 | use SOAP::Lite; | ||
| 13 | |||
| 14 | requires qw''; | ||
| 15 | |||
| 16 | |||
| 17 | sub _query { | ||
| 18 | my ($self, $query) = @_; | ||
| 19 | return $self->_call( | ||
| 20 | 'query', | ||
| 21 | SOAP::Data->name(queryString => $query), | ||
| 22 | ); | ||
| 23 | } | ||
| 24 | |||
| 25 | sub _queryAll { | ||
| 26 | my ($self, $query) = @_; | ||
| 27 | return $self->_call( | ||
| 28 | 'queryAll', | ||
| 29 | SOAP::Data->name(queryString => $query), | ||
| 30 | ); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub _queryMore { | ||
| 34 | my ($self, $locator) = @_; | ||
| 35 | return $self->_call( | ||
| 36 | 'queryMore', | ||
| 37 | SOAP::Data->name(queryLocator => $locator), | ||
| 38 | ); | ||
| 39 | } | ||
| 40 | |||
| 41 | # Extract the results from a $request. This handles the case | ||
| 42 | # where there is only one result, as well as 0 or more than 1. | ||
| 43 | # They require different handling because in the 1 case, you | ||
| 44 | # can't handle it as an array | ||
| 45 | sub _getQueryResults { | ||
| 46 | my ($self, $request) = @_; | ||
| 47 | return ref $request->{records} eq 'ARRAY' | ||
| 48 | ? map {$self->_cleanUpSObject($_)} @{$request->{records}} | ||
| 49 | : ( $self->_cleanUpSObject($request->{records}) ); | ||
| 50 | } | ||
| 51 | |||
| 52 | # Unbless an SObject, and de-duplicate the ID field - SFDC | ||
| 53 | # duplicates the ID, which is interpreted as an arrayref! | ||
| 54 | sub _cleanUpSObject { | ||
| 55 | my ($self, $obj) = @_; | ||
| 56 | return () unless $obj; | ||
| 57 | my %copy = %$obj; # strip the class from $obj | ||
| 58 | $copy{Id} = $copy{Id}->[0] if ref $copy{Id} eq "ARRAY"; | ||
| 59 | for my $key (keys %copy) { | ||
| 60 | if (blessed ($copy{$key}) and blessed ($copy{$key}) eq 'sObject') { | ||
| 61 | $copy{$key} = $self->_cleanUpSObject($copy{$key}); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | return \%copy; | ||
| 65 | } | ||
| 66 | |||
| 67 | # Given the output of _query() or _queryAll(), chain | ||
| 68 | # together calls to _queryMore() and aggregate the results. | ||
| 69 | sub _completeQuery { | ||
| 70 | my ($self, $request) = @_; | ||
| 71 | my @results = $self->_getQueryResults($request); | ||
| 72 | |||
| 73 | until ($request->{done} eq 'true') { | ||
| 74 | $self->_sleep(); | ||
| 75 | $request = $self->_queryMore($request->{queryLocator}); | ||
| 76 | push @results, $self->_getQueryResults($request); | ||
| 77 | } | ||
| 78 | |||
| 79 | return @results; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub query { | ||
| 83 | my ($self, $query) = @_; | ||
| 84 | INFO "Executing SOQL query: ".$query; | ||
| 85 | return $self->_completeQuery( | ||
| 86 | $self->_query($query) | ||
| 87 | ); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub queryAll { | ||
| 91 | my ($self, $query) = @_; | ||
| 92 | return $self->_completeQuery( | ||
| 93 | $self->_queryAll($query) | ||
| 94 | ); | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | sub create { | ||
| 99 | my $self = shift; | ||
| 100 | |||
| 101 | return map { | ||
| 102 | $self->_call( | ||
| 103 | 'create', | ||
| 104 | $self->_prepareSObjects(@$_) | ||
| 105 | ); | ||
| 106 | } spart 200, @_; | ||
| 107 | } | ||
| 108 | |||
| 109 | |||
| 110 | sub update { | ||
| 111 | my $self = shift; | ||
| 112 | |||
| 113 | TRACE "Objects for update" => \@_; | ||
| 114 | INFO "Updating objects"; | ||
| 115 | |||
| 116 | return $self->_call( | ||
| 117 | 'update', | ||
| 118 | $self->_prepareSObjects(@_) | ||
| 119 | ); | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | sub delete { | ||
| 124 | my $self = shift; | ||
| 125 | |||
| 126 | DEBUG "IDs for deletion" => \@_; | ||
| 127 | INFO "Deleting objects"; | ||
| 128 | |||
| 129 | return $self->_call( | ||
| 130 | 'delete', | ||
| 131 | map {SOAP::Data->name('ids' => $_)} @_ | ||
| 132 | ); | ||
| 133 | } | ||
| 134 | |||
| 135 | |||
| 136 | sub undelete { | ||
| 137 | my $self = shift; | ||
| 138 | |||
| 139 | DEBUG "IDs for undelete" => \@_; | ||
| 140 | INFO "Deleting objects"; | ||
| 141 | |||
| 142 | return $self->_call( | ||
| 143 | 'undelete', | ||
| 144 | map {SOAP::Data->name('ids' => $_)} @_ | ||
| 145 | ); | ||
| 146 | } | ||
| 147 | |||
| 148 | |||
| 149 | sub retrieve { | ||
| 150 | my $self = shift; | ||
| 151 | |||
| 152 | DEBUG "IDs for retrieve" => \@_; | ||
| 153 | INFO "Retrieving objects"; | ||
| 154 | |||
| 155 | return $self->_call( | ||
| 156 | 'retrieve', | ||
| 157 | map {SOAP::Data->name('ids' => $_)} @_ | ||
| 158 | ); | ||
| 159 | } | ||
| 160 | |||
| 161 | 1; | ||
| 162 | |||
| 163 | __END__ | ||
| 164 | |||
| 165 | =pod | ||
| 166 | |||
| 167 | =head1 NAME | ||
| 168 | |||
| 169 | WWW::SFDC::Role::CRUD - Shared methods between partner and tooling APIs | ||
| 170 | |||
| 171 | =head1 VERSION | ||
| 172 | |||
| 173 | version 0.22 | ||
| 174 | |||
| 175 | =head1 METHODS | ||
| 176 | |||
| 177 | =head2 query | ||
| 178 | |||
| 179 | say $_->{Id} for WWW::SFDC::Partner->instance()->query($queryString); | ||
| 180 | |||
| 181 | If the query() API call is incomplete and returns a queryLocator, this | ||
| 182 | library will continue calling queryMore() until there are no more records to | ||
| 183 | recieve, at which point it will return the entire list. | ||
| 184 | |||
| 185 | As it stands, if you want the native API behaviour, you will need to use the | ||
| 186 | internal methods _query and _queryMore. | ||
| 187 | |||
| 188 | =head2 queryAll | ||
| 189 | |||
| 190 | This has the same additional behaviour as query(). | ||
| 191 | |||
| 192 | =head2 create | ||
| 193 | |||
| 194 | say "$$_{id}:\t$$_{success}" for WWW::SFDC::Partner->instance()->create( | ||
| 195 | {type => 'thing', Id => 'foo', Field__c => 'bar', Name => 'baz'} | ||
| 196 | {type => 'otherthing', Id => 'bam', Field__c => 'bas', Name => 'bat'} | ||
| 197 | ); | ||
| 198 | |||
| 199 | Create chunks your SObjects into 200s before calling create(). This means that if | ||
| 200 | you have more than 200 objects, you will incur multiple API calls. | ||
| 201 | |||
| 202 | =head2 update | ||
| 203 | |||
| 204 | say "$$_{id}:\t$$_{success}" for WWW::SFDC::Partner->instance()->update( | ||
| 205 | {type => 'thing', Id => 'foo', Field__c => 'bar', Name => 'baz'} | ||
| 206 | {type => 'otherthing', Id => 'bam', Field__c => 'bas', Name => 'bat'} | ||
| 207 | ); | ||
| 208 | |||
| 209 | Returns an array that looks like [{success => 1, id => 'id'}, {}...] with LOWERCASE keys. | ||
| 210 | |||
| 211 | =head2 delete | ||
| 212 | |||
| 213 | say "$$_{id}:\t$$_{success}" for WWW::SFDC::Partner->instance()->delete(@ids); | ||
| 214 | |||
| 215 | Returns an array that looks like [{success => 1, id => 'id'}, {}...] with LOWERCASE keys. | ||
| 216 | |||
| 217 | =head2 undelete | ||
| 218 | |||
| 219 | say "$$_{id}:\t$$_{success}" for WWW::SFDC::Partner->instance()->undelete(@ids); | ||
| 220 | |||
| 221 | Returns an array that looks like [{success => 1, id => 'id'}, {}...] with LOWERCASE keys. | ||
| 222 | |||
| 223 | =head2 retrieve | ||
| 224 | |||
| 225 | Retrieves SObjects by ID. Not to be confused with the metadata retrieve method. | ||
| 226 | |||
| 227 | =head1 BUGS | ||
| 228 | |||
| 229 | Please report any bugs or feature requests at L<https://github.com/alexander-brett/WWW-SFDC/issues>. | ||
| 230 | |||
| 231 | =head1 SUPPORT | ||
| 232 | |||
| 233 | You can find documentation for this module with the perldoc command. | ||
| 234 | |||
| 235 | perldoc WWW::SFDC::Role::CRUD | ||
| 236 | |||
| 237 | You can also look for information at L<https://github.com/alexander-brett/WWW-SFDC> | ||
| 238 | |||
| 239 | =head1 AUTHOR | ||
| 240 | |||
| 241 | Alexander Brett <alexander.brett@sophos.com> | ||
| 242 | |||
| 243 | =head1 COPYRIGHT AND LICENSE | ||
| 244 | |||
| 245 | This software is Copyright (c) 2015 by Sophos Limited. | ||
| 246 | |||
| 247 | This is free software, licensed under: | ||
| 248 | |||
| 249 | The MIT (X11) License | ||
| 250 | |||
| 251 | The full text of the license can be found in the | ||
| 252 | F<LICENSE> file included with this distribution. | ||
| 253 | |||
| 254 | =cut |