Skip to content

Extend 'Client' class with 'create_multiple' and 'bulk_delete' methods #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
bc8d1ba
Improve define_region method
Feb 11, 2016
2c73b31
Create specs
Feb 11, 2016
ea4a242
Update version
Feb 11, 2016
cc4b4fe
- Extended 'Client' class with 'create_multiple' method, for bulk upl…
karolkulesza Feb 17, 2016
9f7b98b
- Extended ‘client.rb’ in forked ‘dynamics_crm' gem with the ‘bulk_de…
karolkulesza Feb 18, 2016
bad527f
- Fixed Travis CI issue: "undefined method `spec' for nil:NilClass” b…
karolkulesza Feb 19, 2016
8500100
- Extended ‘bulk_delete’ method in ‘client.rb’ with the option to wai…
karolkulesza Feb 22, 2016
a71ef9c
Widen curb and mimemagic dependencies.
jheth Feb 23, 2016
4aba80f
Merge pull request #37 from lucaskds/improve-region-determination
jheth Feb 24, 2016
d3f03ad
- Trivial refactoring in 'bulk_delete' method, in 'client.rb';
karolkulesza Feb 24, 2016
27d9322
Switch region hash to string keys.
jheth Feb 24, 2016
8ab364a
Updated rspec syntax using transpec.
jheth Feb 24, 2016
1a36004
Widen development dependencies and update ruby versions.
jheth Feb 24, 2016
97312c7
Fix be_truthy error.
jheth Feb 24, 2016
1c777b3
Remove ruby 2.2 for now.
jheth Feb 24, 2016
dea77f6
Update to version 0.7.0 with CHANGELOG.
jheth Mar 4, 2016
9c0904d
Add max dependency
jheth Mar 4, 2016
e43c926
Avoid escaping nil values.
jheth Mar 8, 2016
0a8ad2e
Support ArrayOfEntity so one can create bound entities like ActivityP…
Mar 10, 2016
d98a567
Test added
Mar 11, 2016
f9a9a4d
Merge pull request #41 from mbhnyc/master
jheth Apr 14, 2016
81cbb31
Add special case for nil value
stephanvd Apr 20, 2016
7faad5f
Merge pull request #43 from stephanvd/feature/support-nil
jheth May 16, 2016
5f6f6ef
- Exposed 'security_token0', 'security_token1' and 'key_identifier' a…
karolkulesza Jul 25, 2016
5609ad9
- Extended 'Fault' support to handle different format (as in case of …
karolkulesza Jul 26, 2016
dc1b0a0
- Extended 'Client' class with 'create_multiple' method, for bulk upl…
karolkulesza Feb 17, 2016
46635e8
- Extended ‘client.rb’ in forked ‘dynamics_crm' gem with the ‘bulk_de…
karolkulesza Feb 18, 2016
936bef6
- Fixed Travis CI issue: "undefined method `spec' for nil:NilClass” b…
karolkulesza Feb 19, 2016
4a17009
- Extended ‘bulk_delete’ method in ‘client.rb’ with the option to wai…
karolkulesza Feb 22, 2016
8122975
- Trivial refactoring in 'bulk_delete' method, in 'client.rb';
karolkulesza Feb 24, 2016
d13e1db
- Exposed 'security_token0', 'security_token1' and 'key_identifier' a…
karolkulesza Jul 25, 2016
ca71d21
- Extended 'Fault' support to handle different format (as in case of …
karolkulesza Jul 26, 2016
b50884b
Merge branch 'master' of https://github.com/karolkulesza/dynamics_crm
karolkulesza Jul 27, 2016
89622e5
- Fixed the issue: "undefined method `blank?' for "S:Sender":REXML::T…
karolkulesza Jul 27, 2016
ca70a08
- Extended 'Client' class with 'update_multiple' method, for bulk upl…
karolkulesza Jul 27, 2016
d2ce044
- 'MessageBuilder' refactoring: Introduced 'execute_multiple_request'…
karolkulesza Jul 27, 2016
d98e0ec
- Extended 'Client' class with 'delete_multiple' method, for bulk del…
karolkulesza Jul 28, 2016
2ac9f77
- Fixed the issue: "Required member 'LogicalName' missing for field '…
karolkulesza Jul 28, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.bundle
.config
.yardoc
.idea
Gemfile.lock
InstalledFiles
_yardoc
Expand All @@ -16,3 +17,4 @@ test/tmp
test/version_tmp
tmp
vendor

5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
language: ruby
rvm:
- 1.9.3
- 2.0.0
- 2.1.0
notifications:
email:
recipients:
- [email protected]
on_success: never
on_failure: always
before_install:
- gem install bundler
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this necessary?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build on Travis CI did not succeed, due to:

"In short, the bundler version that Travis uses is very outdated. Running a gem update and install for bundler as a prestep for the Travis build fixes it usually."

Please refer to the issues described here:

10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 0.7.0 (March 4, 2016)
* Add EntityCollection to xml attributes #24
* Fix illegal XML characters #25
* Dynamics optimize metadata queries #26
* Add Money attribute #31
* Use `request.bytesize` instead of `request.length` #32
* Rexml sanitization on username and password for ocp request builder #33
* Adding new regions to determine region set #34
* Improve region determination #37

## 0.6.0 (December 30, 2014)

* Adds support for Relationship Metadata.
Expand Down
11 changes: 6 additions & 5 deletions dynamics_crm.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_runtime_dependency 'curb', '~> 0.8', '>= 0.8.5'
spec.add_runtime_dependency 'mimemagic', '~> 0.2', '>= 0.2.1'
spec.add_runtime_dependency 'curb', '>= 0.8', '< 1.0.0'
spec.add_runtime_dependency 'mimemagic', '>= 0.2', '< 4.0.0'
spec.add_runtime_dependency 'builder', '>= 3.0.0', '< 4.0.0'

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "bundler", ">= 1.3", "< 2.0"
spec.add_development_dependency 'rake', '~> 10.1'
spec.add_development_dependency 'rspec', '~> 2.14'
spec.add_development_dependency 'simplecov', '~> 0.7'
spec.add_development_dependency 'rspec', '>= 2.14', "< 4.0"
spec.add_development_dependency 'simplecov', '>= 0.7', "< 1.0"
spec.add_development_dependency 'pry', '~> 0.10.3'
end
1 change: 1 addition & 0 deletions lib/dynamics_crm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
require "dynamics_crm/response/retrieve_multiple_result"
require "dynamics_crm/response/create_result"
require "dynamics_crm/response/execute_result"
require "dynamics_crm/response/execute_multiple_result"
# Metadata
require "dynamics_crm/metadata/xml_document"
require "dynamics_crm/metadata/one_to_many_relationship"
Expand Down
112 changes: 92 additions & 20 deletions lib/dynamics_crm/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,21 @@ class Client
extend Forwardable
include XML::MessageBuilder

attr_accessor :logger, :caller_id, :timeout
attr_accessor :logger, :caller_id, :timeout, :security_token0, :security_token1, :key_identifier
attr_reader :hostname, :region, :organization_endpoint, :login_url

OCP_LOGIN_URL = 'https://login.microsoftonline.com/RST2.srf'

REGION = {
"crm9.dynamics.com" => "urn:crmgcc:dynamics.com",
"crm7.dynamics.com" => "urn:crmjpn:dynamics.com",
"crm6.dynamics.com" => "urn:crmoce:dynamics.com",
"crm5.dynamics.com" => "urn:crmapac:dynamics.com",
"crm4.dynamics.com" => "urn:crmemea:dynamics.com",
"crm2.dynamics.com" => "urn:crmsam:dynamics.com",
"crm.dynamics.com" => "urn:crmna:dynamics.com",
}

# Initializes Client instance.
# Requires: organization_name
# Optional: hostname
Expand All @@ -32,6 +42,7 @@ def initialize(config={organization_name: nil, hostname: nil, caller_id: nil, lo
@organization_name = config[:organization_name]
@hostname = config[:hostname] || "#{@organization_name}.api.crm.dynamics.com"
@organization_endpoint = "https://#{@hostname}/XRMServices/2011/Organization.svc"
REGION.default = @organization_endpoint
@caller_id = config[:caller_id]
@timeout = config[:timeout] || 120

Expand Down Expand Up @@ -83,7 +94,7 @@ def authenticate(username, password)
@timestamp = '<u:Timestamp xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" u:Id="_0"><u:Created>' + @header_current_time + '</u:Created><u:Expires>' + @header_expires_time + '</u:Expires></u:Timestamp>'
@digest_value = Digest::SHA1.base64digest @timestamp
@signature = '<SignedInfo xmlns="http://www.w3.org/2000/09/xmldsig#"><CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"></CanonicalizationMethod><SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#hmac-sha1"></SignatureMethod><Reference URI="#_0"><Transforms><Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"></Transform></Transforms><DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"></DigestMethod><DigestValue>' + @digest_value + '</DigestValue></Reference></SignedInfo>'
@signature_value = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), Base64.decode64(@server_secret), @signature)).chomp
@signature_value = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha1'), Base64.decode64(@server_secret), @signature)).chomp
else
cipher_values = document.get_elements("//CipherValue")

Expand Down Expand Up @@ -112,6 +123,20 @@ def create(entity_name, attributes)
return Response::CreateResult.new(xml_response)
end

def create_multiple(entity_name, attributesArray)

entities = []
attributesArray.each do |attributes|
entity = XML::Entity.new(entity_name)
entity.attributes = XML::Attributes.new(attributes)

entities << entity
end
xml_response = post(organization_endpoint, create_multiple_request(entities))

return Response::ExecuteMultipleResult.new(xml_response)
end

# http://crmtroubleshoot.blogspot.com.au/2013/07/dynamics-crm-2011-php-and-soap-calls.html
def retrieve(entity_name, guid, columns=[])

Expand Down Expand Up @@ -160,13 +185,69 @@ def update(entity_name, guid, attributes)
return Response::UpdateResponse.new(xml_response)
end

def update_multiple(entity_name, guids, attributesArray)
raise "Number of 'guids' to be updated should be equal to attributes array size" if guids.count != attributesArray.count

entities = []
attributesArray.each_with_index do |attributes, index|
entity = XML::Entity.new(entity_name)
entity.id = guids[index]
entity.attributes = XML::Attributes.new(attributes)

entities << entity
end
xml_response = post(organization_endpoint, update_multiple_request(entities))

return Response::ExecuteMultipleResult.new(xml_response)
end

def delete(entity_name, guid)
request = delete_request(entity_name, guid)

xml_response = post(organization_endpoint, request)
return Response::DeleteResponse.new(xml_response)
end

def delete_multiple(entity_name, guids)
entities = []
guids.each do |guid|
entity = XML::EntityReference.new(entity_name, guid)
entity.logical_name = entity_name

entities << entity
end
request = delete_multiple_request(entities)
xml_response = post(organization_endpoint, request)

return Response::ExecuteMultipleResult.new(xml_response)
end

def bulk_delete(entity_name, emailToRecipientGuid, criteria=[], wait=false)

query = XML::Query.new(entity_name)
query.criteria = XML::Criteria.new(criteria)

request = bulk_delete_request(query, emailToRecipientGuid)
xml_response = post(organization_endpoint, request)

response = Response::ExecuteResult.new(xml_response)
jobId = response['JobId']
if wait && !jobId.nil?
while true
jobStatus = check_job_status(jobId)
if jobStatus != "Waiting For Resources" && jobStatus != "Waiting" && jobStatus != "In Progress"
break
end
sleep(2) # wait x seconds and check again
end

if jobStatus != 'Succeeded'
response['errorMessage'] = "The job has not completed with success. The response returned a status of #{jobStatus}. Please check your query/parameters and try again."
end
end
response
end

def execute(action, parameters={}, response_class=nil)
request = execute_request(action, parameters)
xml_response = post(organization_endpoint, request)
Expand Down Expand Up @@ -304,24 +385,8 @@ def login_url
end

def determine_region
case hostname
when /crm9\.dynamics\.com/
"urn:crmgcc:dynamics.com"
when /crm7\.dynamics\.com/
"urn:crmjpn:dynamics.com"
when /crm6\.dynamics\.com/
"urn:crmoce:dynamics.com"
when /crm5\.dynamics\.com/
"urn:crmapac:dynamics.com"
when /crm4\.dynamics\.com/
"urn:crmemea:dynamics.com"
when /crm2\.dynamics\.com/
"urn:crmsam:dynamics.com"
when /\.dynamics\.com/
"urn:crmna:dynamics.com"
else
organization_endpoint
end
hostname.match(/(crm\d?\.dynamics.com)/)
REGION[$1]
end

def post(url, request)
Expand Down Expand Up @@ -369,6 +434,13 @@ def formatter
@formatter
end

def check_job_status(job_id)
jobStatusResult = self.retrieve_multiple('asyncoperation', [["asyncoperationid", "Equal", job_id]], ["statuscode"])
jobStatus = jobStatusResult.entities[0].formatted_values['statuscode'] if jobStatusResult.entities.count > 0

jobStatus
end

end

end
35 changes: 35 additions & 0 deletions lib/dynamics_crm/response/execute_multiple_result.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module DynamicsCRM
module Response
class ExecuteMultipleResult < Result

protected

# Returns base element of the response document to parse.
def response_element
class_name = 'ExecuteResult' if self.is_a?(ExecuteMultipleResult)
end

# Invoked by Result constructor
def parse_result_response(result)
h = {}
result.elements["b:Results"].each_element do |key_value_pair|
if key_value_pair.elements["c:key"].text == "Responses"
value_element = key_value_pair.elements["c:value"]
h[:entities] = []
value_element.each_element do |response_item|
itemResults = response_item.elements["d:Response"].elements["b:Results"]
if !itemResults.nil?
h[:entities] << XML::MessageParser.parse_key_value_pairs(itemResults)
end
end
break
end
end

h
end

end

end
end
4 changes: 3 additions & 1 deletion lib/dynamics_crm/response/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ def initialize(xml)
@document = REXML::Document.new(xml)

fault_xml = @document.get_elements("//[local-name() = 'Fault']")
raise XML::Fault.new(fault_xml) if fault_xml.any?
code = fault_xml.first.get_elements("//[local-name() = 'Code']") if fault_xml.any?
code = fault_xml.first.get_elements("//[local-name() = 'ErrorCode']") if fault_xml.any? && code.blank?
raise XML::Fault.new(fault_xml) if fault_xml.any? && !code.empty?

@result_response = @document.get_elements("//#{response_element}").first

Expand Down
2 changes: 1 addition & 1 deletion lib/dynamics_crm/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module DynamicsCRM
VERSION = "0.6.0"
VERSION = "0.7.1"
end
31 changes: 22 additions & 9 deletions lib/dynamics_crm/xml/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def initialize(attrs)
def get_type(key, value)
type = "string"
case value
when ::Array
type = "ArrayOfEntity"
when ::Fixnum
type = "int"
when ::BigDecimal, ::Float
Expand Down Expand Up @@ -77,7 +79,7 @@ def to_xml
end

# escape strings to avoid xml parsing errors
value = CGI.escapeHTML(value) if type == "string"
value = CGI.escapeHTML(value) if value.is_a?(String)

xml << build_xml(key, value, type)
end
Expand Down Expand Up @@ -111,6 +113,13 @@ def build_xml(key, value, type)
def render_value_xml(type, value)
xml = ""
case type
when "ArrayOfEntity"
raise "We can only serialize Entities inside of ArrayOfEntity" unless value.all?{|a| a.is_a?(DynamicsCRM::XML::Entity)}
xml << %Q{
<c:value i:type="a:ArrayOfEntity">
#{value.map(&->(_) { _.to_xml({in_array: true}) }).join}
</c:value>
}
when "EntityReference"
xml << %Q{
<c:value i:type="a:EntityReference">
Expand All @@ -131,7 +140,11 @@ def render_value_xml(type, value)
s_namespace = "http://schemas.microsoft.com/xrm/2011/Metadata"
end

if type == "guid"
if value.nil?
xml << %Q{
<c:value i:nil="true"></c:value>
}
elsif type == "guid"
xml << %Q{
<c:value xmlns:d="http://schemas.microsoft.com/2003/10/Serialization/" i:type="d:guid">#{value}</c:value>
}
Expand All @@ -154,14 +167,14 @@ def render_value_xml(type, value)
end

def render_object_xml(type, value)
case type
when "EntityQueryExpression"
xml = %Q{<c:value i:type="d:#{type}" xmlns:d="http://schemas.microsoft.com/xrm/2011/Metadata/Query">} << value.to_xml({namespace: 'd'}) << "</c:value>"
else
xml = %Q{<c:value i:type="a:#{type}">} << value.to_xml({exclude_root: true, namespace: 'a'}) << "</c:value>"
end
case type
when "EntityQueryExpression"
xml = %Q{<c:value i:type="d:#{type}" xmlns:d="http://schemas.microsoft.com/xrm/2011/Metadata/Query">} << value.to_xml({namespace: 'd'}) << "</c:value>"
else
xml = %Q{<c:value i:type="a:#{type}">} << value.to_xml({exclude_root: true, namespace: 'a'}) << "</c:value>"
end

return xml
xml
end

def class_name
Expand Down
18 changes: 13 additions & 5 deletions lib/dynamics_crm/xml/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@ def to_xml(options={})

return inner_xml if options[:exclude_root]

%Q{
<entity xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
#{inner_xml}
</entity>
}
if options[:in_array]
%Q{
<a:Entity>
#{inner_xml}
</a:Entity>
}
else
%Q{
<entity xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
#{inner_xml}
</entity>
}
end
end

def to_hash
Expand Down
2 changes: 2 additions & 0 deletions lib/dynamics_crm/xml/fault.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ def initialize(fault_xml)
end
# REXL::Element
@code = fault_xml.get_text("//[local-name() = 'Code']/[local-name() = 'Value']")
@code = fault_xml.get_text("//[local-name() = 'ErrorCode']") if @code.nil? || @code.empty?
@subcode = fault_xml.get_text("//[local-name() = 'Code']/[local-name() = 'Subcode']/[local-name() = 'Value']")
@reason = fault_xml.get_text("//[local-name() = 'Reason']/[local-name() = 'Text']")
@reason = fault_xml.get_text("//[local-name() = 'Message']") if @reason.nil? || @reason.empty?

@detail = {}
detail_fragment = fault_xml.get_elements("//[local-name() = 'Detail']").first
Expand Down
Loading