sportgenic-activerecord-activesalesforce-adapter 2.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/README +51 -0
  2. data/lib/active_record/connection_adapters/activesalesforce.rb +36 -0
  3. data/lib/active_record/connection_adapters/activesalesforce_adapter.rb +823 -0
  4. data/lib/active_record/connection_adapters/asf_active_record.rb +40 -0
  5. data/lib/active_record/connection_adapters/boxcar_command.rb +66 -0
  6. data/lib/active_record/connection_adapters/column_definition.rb +95 -0
  7. data/lib/active_record/connection_adapters/entity_definition.rb +59 -0
  8. data/lib/active_record/connection_adapters/id_resolver.rb +84 -0
  9. data/lib/active_record/connection_adapters/recording_binding.rb +90 -0
  10. data/lib/active_record/connection_adapters/relationship_definition.rb +81 -0
  11. data/lib/active_record/connection_adapters/result_array.rb +31 -0
  12. data/lib/active_record/connection_adapters/sid_authentication_filter.rb +57 -0
  13. data/lib/rforce/binding.rb +202 -0
  14. data/lib/rforce/method_keys.rb +14 -0
  15. data/lib/rforce/soap_pullable.rb +90 -0
  16. data/lib/rforce/soap_response_expat.rb +35 -0
  17. data/lib/rforce/soap_response_hpricot.rb +70 -0
  18. data/lib/rforce/soap_response_rexml.rb +34 -0
  19. data/lib/rforce/version.rb +3 -0
  20. data/lib/rforce.rb +116 -0
  21. data/test/unit/basic_test.rb +203 -0
  22. data/test/unit/config.yml +5 -0
  23. data/test/unit/recorded_results/AsfUnitTestsBasicTest.test_add_notes_to_contact.recording +1966 -0
  24. data/test/unit/recorded_results/AsfUnitTestsBasicTest.test_assignment_rule_id.recording +1621 -0
  25. data/test/unit/recorded_results/AsfUnitTestsBasicTest.test_batch_insert.recording +1611 -0
  26. data/test/unit/recorded_results/AsfUnitTestsBasicTest.test_client_id.recording +1618 -0
  27. data/test/unit/recorded_results/AsfUnitTestsBasicTest.test_count_contacts.recording +1620 -0
  28. data/test/unit/recorded_results/AsfUnitTestsBasicTest.test_create_a_contact.recording +1611 -0
  29. data/test/unit/recorded_results/AsfUnitTestsBasicTest.test_find_a_contact.recording +1611 -0
  30. data/test/unit/recorded_results/AsfUnitTestsBasicTest.test_find_a_contact_by_first_name.recording +3468 -0
  31. data/test/unit/recorded_results/AsfUnitTestsBasicTest.test_find_a_contact_by_id.recording +1664 -0
  32. data/test/unit/recorded_results/AsfUnitTestsBasicTest.test_find_addresses.recording +1635 -0
  33. data/test/unit/recorded_results/AsfUnitTestsBasicTest.test_get_created_by_from_contact.recording +4307 -0
  34. data/test/unit/recorded_results/AsfUnitTestsBasicTest.test_master_detail.recording +1951 -0
  35. data/test/unit/recorded_results/AsfUnitTestsBasicTest.test_read_all_content_columns.recording +1611 -0
  36. data/test/unit/recorded_results/AsfUnitTestsBasicTest.test_save_a_contact.recording +1611 -0
  37. data/test/unit/recorded_results/AsfUnitTestsBasicTest.test_use_default_rule.recording +1618 -0
  38. data/test/unit/recorded_results/AsfUnitTestsBasicTest.test_use_update_mru.recording +1618 -0
  39. data/test/unit/recorded_test_case.rb +83 -0
  40. metadata +160 -0
@@ -0,0 +1,823 @@
1
+ =begin
2
+ ActiveSalesforce
3
+ Copyright 2006 Doug Chasman
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+ =end
17
+
18
+ require 'thread'
19
+ require 'benchmark'
20
+
21
+ require 'active_record'
22
+ require 'active_record/connection_adapters/abstract_adapter'
23
+
24
+ require File.dirname(__FILE__) + '/../../rforce'
25
+ require File.dirname(__FILE__) + '/column_definition'
26
+ require File.dirname(__FILE__) + '/relationship_definition'
27
+ require File.dirname(__FILE__) + '/boxcar_command'
28
+ require File.dirname(__FILE__) + '/entity_definition'
29
+ require File.dirname(__FILE__) + '/asf_active_record'
30
+ require File.dirname(__FILE__) + '/id_resolver'
31
+ require File.dirname(__FILE__) + '/sid_authentication_filter'
32
+ require File.dirname(__FILE__) + '/recording_binding'
33
+ require File.dirname(__FILE__) + '/result_array'
34
+
35
+
36
+ module ActiveRecord
37
+ class Base
38
+ @@cache = {}
39
+
40
+ def self.debug(msg)
41
+ logger.debug(msg) if logger
42
+ end
43
+
44
+ def self.flush_connections()
45
+ @@cache = {}
46
+ end
47
+
48
+ # Establishes a connection to the database that's used by all Active Record objects.
49
+ def self.activesalesforce_connection(config) # :nodoc:
50
+ debug("\nUsing ActiveSalesforce connection\n")
51
+
52
+ # Default to production system using 11.0 API
53
+ url = config[:url]
54
+ url = "https://www.salesforce.com" unless url
55
+
56
+ uri = URI.parse(url)
57
+ uri.path = "/services/Soap/u/11.0"
58
+ url = uri.to_s
59
+
60
+ sid = config[:sid]
61
+ client_id = config[:client_id]
62
+ username = config[:username].to_s
63
+ password = config[:password].to_s
64
+
65
+ # Recording/playback support
66
+ recording_source = config[:recording_source]
67
+ recording = config[:recording]
68
+
69
+ if recording_source
70
+ recording_source = File.open(recording_source, recording ? "w" : "r")
71
+ binding = ActiveSalesforce::RecordingBinding.new(url, nil, recording != nil, recording_source, logger)
72
+ binding.client_id = client_id if client_id
73
+ binding.login(username, password) unless sid
74
+ end
75
+
76
+ # Check to insure that the second to last path component is a 'u' for Partner API
77
+ raise ActiveSalesforce::ASFError.new(logger, "Invalid salesforce server url '#{url}', must be a valid Parter API URL") unless url.match(/\/u\//mi)
78
+
79
+ if sid
80
+ binding = @@cache["sid=#{sid}"] unless binding
81
+
82
+ unless binding
83
+ debug("Establishing new connection for [sid='#{sid}']")
84
+
85
+ binding = RForce::Binding.new(url, sid)
86
+ @@cache["sid=#{sid}"] = binding
87
+
88
+ debug("Created new connection for [sid='#{sid}']")
89
+ else
90
+ debug("Reused existing connection for [sid='#{sid}']")
91
+ end
92
+ else
93
+ binding = @@cache["#{url}.#{username}.#{password}.#{client_id}"] unless binding
94
+
95
+ unless binding
96
+ debug("Establishing new connection for ['#{url}', '#{username}, '#{client_id}'")
97
+
98
+ seconds = Benchmark.realtime {
99
+ binding = RForce::Binding.new(url, sid)
100
+ binding.login(username, password)
101
+
102
+ @@cache["#{url}.#{username}.#{password}.#{client_id}"] = binding
103
+ }
104
+
105
+ debug("Created new connection for ['#{url}', '#{username}', '#{client_id}'] in #{seconds} seconds")
106
+ end
107
+ end
108
+
109
+ ConnectionAdapters::SalesforceAdapter.new(binding, logger, config)
110
+
111
+ end
112
+ end
113
+
114
+
115
+ module ConnectionAdapters
116
+
117
+ class SalesforceAdapter < AbstractAdapter
118
+ include StringHelper
119
+
120
+ MAX_BOXCAR_SIZE = 200
121
+
122
+ attr_accessor :batch_size
123
+ attr_reader :entity_def_map, :keyprefix_to_entity_def_map, :config, :class_to_entity_map
124
+
125
+ def initialize(connection, logger, config)
126
+ super(connection, logger)
127
+
128
+ @connection_options = nil
129
+ @config = config
130
+
131
+ @entity_def_map = {}
132
+ @keyprefix_to_entity_def_map = {}
133
+
134
+ @command_boxcar = nil
135
+ @class_to_entity_map = {}
136
+ end
137
+
138
+
139
+ def set_class_for_entity(klass, entity_name)
140
+ debug("Setting @class_to_entity_map['#{entity_name.upcase}'] = #{klass} for connection #{self}")
141
+ @class_to_entity_map[entity_name.upcase] = klass
142
+ end
143
+
144
+
145
+ def binding
146
+ @connection
147
+ end
148
+
149
+
150
+ def adapter_name #:nodoc:
151
+ 'ActiveSalesforce'
152
+ end
153
+
154
+
155
+ def supports_migrations? #:nodoc:
156
+ false
157
+ end
158
+
159
+ def table_exists?(table_name)
160
+ true
161
+ end
162
+
163
+ # QUOTING ==================================================
164
+
165
+ def quote(value, column = nil)
166
+ case value
167
+ when NilClass then quoted_value = "NULL"
168
+ when TrueClass then quoted_value = "TRUE"
169
+ when FalseClass then quoted_value = "FALSE"
170
+ when Float, Fixnum, Bignum then quoted_value = "'#{value.to_s}'"
171
+ else quoted_value = super(value, column)
172
+ end
173
+
174
+ quoted_value
175
+ end
176
+
177
+ # CONNECTION MANAGEMENT ====================================
178
+
179
+ def active?
180
+ true
181
+ end
182
+
183
+
184
+ def reconnect!
185
+ connect
186
+ end
187
+
188
+
189
+ # TRANSACTIOn SUPPORT (Boxcarring really because the salesforce.com api does not support transactions)
190
+
191
+ # Override AbstractAdapter's transaction method to implement
192
+ # per-connection support for nested transactions that do not commit until
193
+ # the outermost transaction is finished. ActiveRecord provides support
194
+ # for this, but does not distinguish between database connections, which
195
+ # prevents opening transactions to two different databases at the same
196
+ # time.
197
+ def transaction_with_nesting_support(*args, &block)
198
+ open = Thread.current["open_transactions_for_#{self.class.name.underscore}"] ||= 0
199
+ Thread.current["open_transactions_for_#{self.class.name.underscore}"] = open + 1
200
+
201
+ begin
202
+ transaction_without_nesting_support(open.zero?, &block)
203
+ ensure
204
+ Thread.current["open_transactions_for_#{self.class.name.underscore}"] -= 1
205
+ end
206
+ end
207
+ alias_method_chain :transaction, :nesting_support
208
+
209
+ # Begins the transaction (and turns off auto-committing).
210
+ def begin_db_transaction
211
+ log('Opening boxcar', 'begin_db_transaction()')
212
+ @command_boxcar = []
213
+ end
214
+
215
+
216
+ def send_commands(commands)
217
+ # Send the boxcar'ed command set
218
+ verb = commands[0].verb
219
+
220
+ args = []
221
+ commands.each do |command|
222
+ command.args.each { |arg| args << arg }
223
+ end
224
+
225
+ response = @connection.send(verb, args)
226
+
227
+ result = get_result(response, verb)
228
+
229
+ result = [ result ] unless result.is_a?(Array)
230
+
231
+ errors = []
232
+ result.each_with_index do |r, n|
233
+ success = r[:success] == "true"
234
+
235
+ # Give each command a chance to process its own result
236
+ command = commands[n]
237
+ command.after_execute(r)
238
+
239
+ # Handle the set of failures
240
+ errors << r[:errors] unless r[:success] == "true"
241
+ end
242
+
243
+ unless errors.empty?
244
+ message = errors.join("\n")
245
+ fault = (errors.map { |error| error[:message] }).join("\n")
246
+ raise ActiveSalesforce::ASFError.new(@logger, message, fault)
247
+ end
248
+
249
+ result
250
+ end
251
+
252
+
253
+ # Commits the transaction (and turns on auto-committing).
254
+ def commit_db_transaction()
255
+ log("Committing boxcar with #{@command_boxcar.length} commands", 'commit_db_transaction()')
256
+
257
+ previous_command = nil
258
+ commands = []
259
+
260
+ @command_boxcar.each do |command|
261
+ if commands.length >= MAX_BOXCAR_SIZE or (previous_command and (command.verb != previous_command.verb))
262
+ send_commands(commands)
263
+
264
+ commands = []
265
+ previous_command = nil
266
+ else
267
+ commands << command
268
+ previous_command = command
269
+ end
270
+ end
271
+
272
+ # Discard the command boxcar
273
+ @command_boxcar = nil
274
+
275
+ # Finish off the partial boxcar
276
+ send_commands(commands) unless commands.empty?
277
+
278
+ end
279
+
280
+ # Rolls back the transaction (and turns on auto-committing). Must be
281
+ # done if the transaction block raises an exception or returns false.
282
+ def rollback_db_transaction()
283
+ log('Rolling back boxcar', 'rollback_db_transaction()')
284
+ @command_boxcar = nil
285
+ end
286
+
287
+
288
+ # DATABASE STATEMENTS ======================================
289
+
290
+ def select_all(sql, name = nil) #:nodoc:
291
+ raw_table_name = sql.match(/FROM (\w+)/mi)[1]
292
+ table_name, columns, entity_def = lookup(raw_table_name)
293
+
294
+ column_names = columns.map { |column| column.api_name }
295
+
296
+ # Check for SELECT COUNT(*) FROM query
297
+
298
+ # Rails 1.1
299
+ selectCountMatch = sql.match(/SELECT\s+COUNT\(\*\)\s+AS\s+count_all\s+FROM/mi)
300
+
301
+ # Rails 1.0
302
+ selectCountMatch = sql.match(/SELECT\s+COUNT\(\*\)\s+FROM/mi) unless selectCountMatch
303
+
304
+ if selectCountMatch
305
+ soql = "SELECT COUNT() FROM#{selectCountMatch.post_match}"
306
+ else
307
+ if sql.match(/SELECT\s+\*\s+FROM/mi)
308
+ # Always convert SELECT * to select all columns (required for the AR attributes mechanism to work correctly)
309
+ soql = sql.sub(/SELECT .+ FROM/mi, "SELECT #{column_names.join(', ')} FROM")
310
+ else
311
+ soql = sql
312
+ end
313
+ end
314
+
315
+ soql.sub!(/\s+FROM\s+\w+/mi, " FROM #{entity_def.api_name}")
316
+
317
+ if selectCountMatch
318
+ query_result = get_result(@connection.query(:queryString => soql), :query)
319
+ return [{ :count => query_result[:size] }]
320
+ end
321
+
322
+ # Look for a LIMIT clause
323
+ limit = extract_sql_modifier(soql, "LIMIT")
324
+ limit = MAX_BOXCAR_SIZE unless limit
325
+
326
+ # Look for an OFFSET clause
327
+ offset = extract_sql_modifier(soql, "OFFSET")
328
+
329
+ # Fixup column references to use api names
330
+ columns = entity_def.column_name_to_column
331
+ soql.gsub!(/((?:\w+\.)?\w+)(?=\s*(?:=|!=|<|>|<=|>=|like)\s*(?:'[^']*'|NULL|TRUE|FALSE))/mi) do |column_name|
332
+ # strip away any table alias
333
+ column_name.sub!(/\w+\./, '')
334
+
335
+ column = columns[column_name]
336
+ raise ActiveSalesforce::ASFError.new(@logger, "Column not found for #{column_name}!") unless column
337
+
338
+ column.api_name
339
+ end
340
+
341
+ # Update table name references
342
+ soql.sub!(/#{raw_table_name}\./mi, "#{entity_def.api_name}.")
343
+
344
+ @connection.batch_size = @batch_size if @batch_size
345
+ @batch_size = nil
346
+
347
+ query_result = get_result(@connection.query(:queryString => soql), :query)
348
+ result = ActiveSalesforce::ResultArray.new(query_result[:size].to_i)
349
+ return result unless query_result[:records]
350
+
351
+ add_rows(entity_def, query_result, result, limit)
352
+
353
+ while ((query_result[:done].casecmp("true") != 0) and (result.size < limit or limit == 0))
354
+ # Now queryMore
355
+ locator = query_result[:queryLocator];
356
+ query_result = get_result(@connection.queryMore(:queryLocator => locator), :queryMore)
357
+
358
+ add_rows(entity_def, query_result, result, limit)
359
+ end
360
+
361
+ result
362
+ end
363
+
364
+ def add_rows(entity_def, query_result, result, limit)
365
+ records = query_result[:records]
366
+ records = [ records ] unless records.is_a?(Array)
367
+
368
+ records.each do |record|
369
+ row = {}
370
+
371
+ record.each do |name, value|
372
+ if name != :type
373
+ # Ids may be returned in an array with 2 duplicate entries...
374
+ value = value[0] if name == :Id && value.is_a?(Array)
375
+
376
+ column = entity_def.api_name_to_column[name.to_s]
377
+ attribute_name = column.name
378
+
379
+ if column.type == :boolean
380
+ row[attribute_name] = (value.casecmp("true") == 0)
381
+ else
382
+ row[attribute_name] = value
383
+ end
384
+ end
385
+ end
386
+
387
+ result << row
388
+
389
+ break if result.size >= limit and limit != 0
390
+ end
391
+ end
392
+
393
+ def select_one(sql, name = nil) #:nodoc:
394
+ self.batch_size = 1
395
+
396
+ result = select_all(sql, name)
397
+
398
+ result.nil? ? nil : result.first
399
+ end
400
+
401
+
402
+ def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
403
+ log(sql, name) {
404
+ # Convert sql to sobject
405
+ table_name, columns, entity_def = lookup(sql.match(/INSERT\s+INTO\s+(\w+)\s+/mi)[1])
406
+ columns = entity_def.column_name_to_column
407
+
408
+ # Extract array of column names
409
+ names = sql.match(/\((.+)\)\s+VALUES/mi)[1].scan(/\w+/mi)
410
+
411
+ # Extract arrays of values
412
+ values = sql.match(/VALUES\s*\((.+)\)/mi)[1]
413
+ values = values.scan(/(NULL|TRUE|FALSE|'(?:(?:[^']|'')*)'),*/mi).flatten
414
+ values.map! { |v| v.first == "'" ? v.slice(1, v.length - 2) : v == "NULL" ? nil : v }
415
+
416
+ fields = get_fields(columns, names, values, :createable)
417
+
418
+ sobject = create_sobject(entity_def.api_name, nil, fields)
419
+
420
+ # Track the id to be able to update it when the create() is actually executed
421
+ id = String.new
422
+ queue_command ActiveSalesforce::BoxcarCommand::Insert.new(self, sobject, id)
423
+
424
+ id
425
+ }
426
+ end
427
+
428
+
429
+ def update(sql, name = nil) #:nodoc:
430
+ #log(sql, name) {
431
+ # Convert sql to sobject
432
+ table_name, columns, entity_def = lookup(sql.match(/UPDATE\s+(\w+)\s+/mi)[1])
433
+ columns = entity_def.column_name_to_column
434
+
435
+ match = sql.match(/SET\s+(.+)\s+WHERE/mi)[1]
436
+ names = match.scan(/(\w+)\s*=\s*(?:'|NULL|TRUE|FALSE)/mi).flatten
437
+
438
+ values = match.scan(/=\s*(NULL|TRUE|FALSE|'(?:(?:[^']|'')*)'),*/mi).flatten
439
+ values.map! { |v| v.first == "'" ? v.slice(1, v.length - 2) : v == "NULL" ? nil : v }
440
+
441
+ fields = get_fields(columns, names, values, :updateable)
442
+ null_fields = get_null_fields(columns, names, values, :updateable)
443
+
444
+ ids = sql.match(/WHERE\s+id\s*=\s*'(\w+)'/mi)
445
+ return if ids.nil?
446
+ id = ids[1]
447
+
448
+ sobject = create_sobject(entity_def.api_name, id, fields, null_fields)
449
+
450
+ queue_command ActiveSalesforce::BoxcarCommand::Update.new(self, sobject)
451
+ #}
452
+ end
453
+
454
+
455
+ def delete(sql, name = nil)
456
+ log(sql, name) {
457
+ # Extract the id
458
+ match = sql.match(/WHERE\s+id\s*=\s*'(\w+)'/mi)
459
+
460
+ if match
461
+ ids = [ match[1] ]
462
+ else
463
+ # Check for the form (id IN ('x', 'y'))
464
+ match = sql.match(/WHERE\s+\(\s*id\s+IN\s*\((.+)\)\)/mi)[1]
465
+ ids = match.scan(/\w+/)
466
+ end
467
+
468
+ ids_element = []
469
+ ids.each { |id| ids_element << :ids << id }
470
+
471
+ queue_command ActiveSalesforce::BoxcarCommand::Delete.new(self, ids_element)
472
+ }
473
+ end
474
+
475
+
476
+ def get_updated(object_type, start_date, end_date, name = nil)
477
+ msg = "get_updated(#{object_type}, #{start_date}, #{end_date})"
478
+ log(msg, name) {
479
+ get_updated_element = []
480
+ get_updated_element << 'type { :xmlns => "urn:sobject.partner.soap.sforce.com" }' << object_type
481
+ get_updated_element << :startDate << start_date
482
+ get_updated_element << :endDate << end_date
483
+
484
+ result = get_result(@connection.getUpdated(get_updated_element), :getUpdated)
485
+
486
+ result[:ids]
487
+ }
488
+ end
489
+
490
+
491
+ def get_deleted(object_type, start_date, end_date, name = nil)
492
+ msg = "get_deleted(#{object_type}, #{start_date}, #{end_date})"
493
+ log(msg, name) {
494
+ get_deleted_element = []
495
+ get_deleted_element << 'type { :xmlns => "urn:sobject.partner.soap.sforce.com" }' << object_type
496
+ get_deleted_element << :startDate << start_date
497
+ get_deleted_element << :endDate << end_date
498
+
499
+ result = get_result(@connection.getDeleted(get_deleted_element), :getDeleted)
500
+
501
+ ids = []
502
+ result[:deletedRecords].each do |v|
503
+ ids << v[:id]
504
+ end
505
+
506
+ ids
507
+ }
508
+ end
509
+
510
+
511
+ def get_user_info(name = nil)
512
+ msg = "get_user_info()"
513
+ log(msg, name) {
514
+ get_result(@connection.getUserInfo([]), :getUserInfo)
515
+ }
516
+ end
517
+
518
+
519
+ def retrieve_field_values(object_type, fields, ids, name = nil)
520
+ msg = "retrieve(#{object_type}, [#{ids.to_a.join(', ')}])"
521
+ log(msg, name) {
522
+ retrieve_element = []
523
+ retrieve_element << :fieldList << fields.to_a.join(", ")
524
+ retrieve_element << 'type { :xmlns => "urn:sobject.partner.soap.sforce.com" }' << object_type
525
+ ids.to_a.each { |id| retrieve_element << :ids << id }
526
+
527
+ result = get_result(@connection.retrieve(retrieve_element), :retrieve)
528
+
529
+ result = [ result ] unless result.is_a?(Array)
530
+
531
+ # Remove unwanted :type and normalize :Id if required
532
+ field_values = []
533
+ result.each do |v|
534
+ v = v.dup
535
+ v.delete(:type)
536
+ v[:Id] = v[:Id][0] if v[:Id].is_a? Array
537
+
538
+ field_values << v
539
+ end
540
+
541
+ field_values
542
+ }
543
+ end
544
+
545
+
546
+ def get_fields(columns, names, values, access_check)
547
+ fields = {}
548
+ names.each_with_index do | name, n |
549
+ value = values[n]
550
+
551
+ if value
552
+ column = columns[name]
553
+
554
+ raise ActiveSalesforce::ASFError.new(@logger, "Column not found for #{name}!") unless column
555
+
556
+ value.gsub!(/''/, "'") if value.is_a? String
557
+
558
+ include_field = ((not value.empty?) and column.send(access_check))
559
+
560
+ if (include_field)
561
+ case column.type
562
+ when :date
563
+ value = Time.parse(value + "Z").utc.strftime("%Y-%m-%d")
564
+ when :datetime
565
+ value = Time.parse(value + "Z").utc.strftime("%Y-%m-%dT%H:%M:%SZ")
566
+ end
567
+
568
+ fields[column.api_name] = value
569
+ end
570
+ end
571
+ end
572
+
573
+ fields
574
+ end
575
+
576
+ def get_null_fields(columns, names, values, access_check)
577
+ fields = {}
578
+ names.each_with_index do | name, n |
579
+ value = values[n]
580
+
581
+ if !value
582
+ column = columns[name]
583
+ fields[column.api_name] = nil if column.send(access_check) && column.api_name.casecmp("ownerid") != 0
584
+ end
585
+ end
586
+
587
+ fields
588
+ end
589
+
590
+ def extract_sql_modifier(soql, modifier)
591
+ value = soql.match(/\s+#{modifier}\s+(\d+)/mi)
592
+ if value
593
+ value = value[1].to_i
594
+ soql.sub!(/\s+#{modifier}\s+\d+/mi, "")
595
+ end
596
+
597
+ value
598
+ end
599
+
600
+
601
+ def get_result(response, method)
602
+ responseName = (method.to_s + "Response").to_sym
603
+ finalResponse = response[responseName]
604
+
605
+ raise ActiveSalesforce::ASFError.new(@logger, response[:Fault][:faultstring], response.fault) unless finalResponse
606
+
607
+ result = finalResponse[:result]
608
+ end
609
+
610
+
611
+ def check_result(result)
612
+ result = [ result ] unless result.is_a?(Array)
613
+
614
+ result.each do |r|
615
+ raise ActiveSalesforce::ASFError.new(@logger, r[:errors], r[:errors][:message]) unless r[:success] == "true"
616
+ end
617
+
618
+ result
619
+ end
620
+
621
+
622
+ def get_entity_def(entity_name)
623
+ cached_entity_def = @entity_def_map[entity_name]
624
+
625
+ if cached_entity_def
626
+ # Check for the loss of asf AR setup
627
+ entity_klass = class_from_entity_name(entity_name)
628
+
629
+ configure_active_record(cached_entity_def) unless entity_klass.respond_to?(:asf_augmented?)
630
+
631
+ return cached_entity_def
632
+ end
633
+
634
+ cached_columns = []
635
+ cached_relationships = []
636
+
637
+ begin
638
+ metadata = get_result(@connection.describeSObject(:sObjectType => entity_name), :describeSObject)
639
+ custom = false
640
+ rescue ActiveSalesforce::ASFError
641
+ # Fallback and see if we can find a custom object with this name
642
+ debug(" Unable to find medata for '#{entity_name}', falling back to custom object name #{entity_name + "__c"}")
643
+
644
+ metadata = get_result(@connection.describeSObject(:sObjectType => entity_name + "__c"), :describeSObject)
645
+ custom = true
646
+ end
647
+
648
+ metadata[:fields].each do |field|
649
+ column = SalesforceColumn.new(field)
650
+ cached_columns << column
651
+
652
+ cached_relationships << SalesforceRelationship.new(field, column) if field[:type] =~ /reference/mi
653
+ end
654
+
655
+ relationships = metadata[:childRelationships]
656
+ if relationships
657
+ relationships = [ relationships ] unless relationships.is_a? Array
658
+
659
+ relationships.each do |relationship|
660
+ if relationship[:cascadeDelete] == "true"
661
+ r = SalesforceRelationship.new(relationship)
662
+ cached_relationships << r
663
+ end
664
+ end
665
+ end
666
+
667
+ key_prefix = metadata[:keyPrefix]
668
+
669
+ entity_def = ActiveSalesforce::EntityDefinition.new(self, entity_name, entity_klass,
670
+ cached_columns, cached_relationships, custom, key_prefix)
671
+
672
+ @entity_def_map[entity_name] = entity_def
673
+ @keyprefix_to_entity_def_map[key_prefix] = entity_def
674
+
675
+ configure_active_record(entity_def)
676
+
677
+ entity_def
678
+ end
679
+
680
+
681
+ def configure_active_record(entity_def)
682
+ entity_name = entity_def.name
683
+ klass = class_from_entity_name(entity_name)
684
+
685
+ class << klass
686
+ def asf_augmented?
687
+ true
688
+ end
689
+ end
690
+
691
+ # Add support for SID-based authentication
692
+ ActiveSalesforce::SessionIDAuthenticationFilter.register(klass)
693
+
694
+ klass.set_inheritance_column nil unless entity_def.custom?
695
+ klass.set_primary_key "id"
696
+
697
+ # Create relationships for any reference field
698
+ entity_def.relationships.each do |relationship|
699
+ referenceName = relationship.name
700
+ unless self.respond_to? referenceName.to_sym or relationship.reference_to == "Profile"
701
+ reference_to = relationship.reference_to
702
+ one_to_many = relationship.one_to_many
703
+ foreign_key = relationship.foreign_key
704
+
705
+ # DCHASMAN TODO Figure out how to handle polymorphic refs (e.g. Note.parent can refer to
706
+ # Account, Contact, Opportunity, Contract, Asset, Product2, <CustomObject1> ... <CustomObject(n)>
707
+ if reference_to.is_a? Array
708
+ debug(" Skipping unsupported polymophic one-to-#{one_to_many ? 'many' : 'one' } relationship '#{referenceName}' from #{klass} to [#{relationship.reference_to.join(', ')}] using #{foreign_key}")
709
+ next
710
+ end
711
+
712
+ # Handle references to custom objects
713
+ reference_to = reference_to.chomp("__c").camelize if reference_to.match(/__c$/)
714
+
715
+ begin
716
+ referenced_klass = class_from_entity_name(reference_to)
717
+ rescue NameError => e
718
+ # Automatically create a least a stub for the referenced entity
719
+ debug(" Creating ActiveRecord stub for the referenced entity '#{reference_to}'")
720
+
721
+ referenced_klass = klass.class_eval("::#{reference_to} = Class.new(ActiveRecord::Base)")
722
+
723
+ # Automatically inherit the connection from the referencee
724
+ def referenced_klass.connection
725
+ klass.connection
726
+ end
727
+ end
728
+
729
+ if referenced_klass
730
+ if one_to_many
731
+ assoc_name = reference_to.underscore.pluralize.to_sym
732
+ klass.has_many assoc_name, :class_name => referenced_klass.name, :foreign_key => foreign_key
733
+ else
734
+ assoc_name = reference_to.underscore.singularize.to_sym
735
+ klass.belongs_to assoc_name, :class_name => referenced_klass.name, :foreign_key => foreign_key
736
+ end
737
+
738
+ debug(" Created one-to-#{one_to_many ? 'many' : 'one' } relationship '#{referenceName}' from #{klass} to #{referenced_klass} using #{foreign_key}")
739
+ end
740
+ end
741
+ end
742
+
743
+ end
744
+
745
+
746
+ def columns(table_name, name = nil)
747
+ table_name, columns, entity_def = lookup(table_name)
748
+ entity_def.columns
749
+ end
750
+
751
+
752
+ def class_from_entity_name(entity_name)
753
+ entity_klass = @class_to_entity_map[entity_name.upcase]
754
+ debug("Found matching class '#{entity_klass}' for entity '#{entity_name}'") if entity_klass
755
+
756
+ # Constantize entities under the Salesforce namespace.
757
+ entity_klass = ("Salesforce::" + entity_name).constantize unless entity_klass
758
+
759
+ entity_klass
760
+ end
761
+
762
+
763
+ def create_sobject(entity_name, id, fields, null_fields = [])
764
+ sobj = []
765
+
766
+ sobj << 'type { :xmlns => "urn:sobject.partner.soap.sforce.com" }' << entity_name
767
+ sobj << 'Id { :xmlns => "urn:sobject.partner.soap.sforce.com" }' << id if id
768
+
769
+ # add any changed fields
770
+ fields.each do | name, value |
771
+ sobj << name.to_sym << value if value
772
+ end
773
+
774
+ # add null fields
775
+ null_fields.each do | name, value |
776
+ sobj << 'fieldsToNull { :xmlns => "urn:sobject.partner.soap.sforce.com" }' << name
777
+ end
778
+
779
+ [ :sObjects, sobj ]
780
+ end
781
+
782
+
783
+ def column_names(table_name)
784
+ columns(table_name).map { |column| column.name }
785
+ end
786
+
787
+
788
+ def lookup(raw_table_name)
789
+ table_name = raw_table_name.singularize
790
+
791
+ # See if a table name to AR class mapping was registered
792
+ klass = @class_to_entity_map[table_name.upcase]
793
+
794
+ entity_name = klass ? raw_table_name : table_name.camelize
795
+ entity_def = get_entity_def(entity_name)
796
+
797
+ [table_name, entity_def.columns, entity_def]
798
+ end
799
+
800
+
801
+ def debug(msg)
802
+ @logger.debug(msg) if @logger
803
+ end
804
+
805
+ protected
806
+
807
+ def queue_command(command)
808
+ # If @command_boxcar is not nil, then this is a transaction
809
+ # and commands should be queued in the boxcar
810
+ if @command_boxcar
811
+ @command_boxcar << command
812
+
813
+ # If a command is not executed within a transaction, it should
814
+ # be executed immediately
815
+ else
816
+ send_commands([command])
817
+ end
818
+ end
819
+
820
+ end
821
+
822
+ end
823
+ end