fruit_to_lime 2.5.5 → 2.5.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/lib/fruit_to_lime.rb +17 -17
  2. data/lib/fruit_to_lime/csv_helper.rb +47 -47
  3. data/lib/fruit_to_lime/email_helper.rb +10 -10
  4. data/lib/fruit_to_lime/errors.rb +16 -16
  5. data/lib/fruit_to_lime/excel_helper.rb +10 -10
  6. data/lib/fruit_to_lime/global_phone.json +6571 -6571
  7. data/lib/fruit_to_lime/model/address.rb +60 -60
  8. data/lib/fruit_to_lime/model/class_settings.rb +50 -50
  9. data/lib/fruit_to_lime/model/coworker.rb +76 -76
  10. data/lib/fruit_to_lime/model/coworker_reference.rb +33 -33
  11. data/lib/fruit_to_lime/model/customfield.rb +87 -87
  12. data/lib/fruit_to_lime/model/deal.rb +141 -141
  13. data/lib/fruit_to_lime/model/deal_status.rb +12 -12
  14. data/lib/fruit_to_lime/model/note.rb +80 -79
  15. data/lib/fruit_to_lime/model/organization.rb +203 -203
  16. data/lib/fruit_to_lime/model/person.rb +151 -151
  17. data/lib/fruit_to_lime/model/referencetosource.rb +45 -45
  18. data/lib/fruit_to_lime/model/relation.rb +23 -23
  19. data/lib/fruit_to_lime/model/rootmodel.rb +342 -338
  20. data/lib/fruit_to_lime/model/settings.rb +60 -60
  21. data/lib/fruit_to_lime/model/tag.rb +35 -35
  22. data/lib/fruit_to_lime/model_helpers.rb +54 -54
  23. data/lib/fruit_to_lime/phone_helper.rb +74 -74
  24. data/lib/fruit_to_lime/roo_helper.rb +72 -72
  25. data/lib/fruit_to_lime/serialize_helper.rb +186 -186
  26. data/lib/fruit_to_lime/templating.rb +52 -52
  27. data/spec/address_spec.rb +48 -48
  28. data/spec/class_settings_spec.rb +37 -37
  29. data/spec/coworker_spec.rb +94 -94
  30. data/spec/custom_field_spec.rb +22 -22
  31. data/spec/deal_spec.rb +101 -101
  32. data/spec/helpers/csv_helper_spec.rb +29 -29
  33. data/spec/helpers/email_helper_spec.rb +32 -32
  34. data/spec/helpers/phone_helper_spec.rb +97 -97
  35. data/spec/helpers/serialize_helper_spec.rb +249 -249
  36. data/spec/helpers/xsd_validate_spec.rb +58 -58
  37. data/spec/note_spec.rb +98 -98
  38. data/spec/organization_spec.rb +103 -103
  39. data/spec/person_spec.rb +134 -134
  40. data/spec/rootmodel_spec.rb +306 -277
  41. data/spec/templating_spec.rb +11 -11
  42. data/templates/csv/lib/tomodel.rb +230 -230
  43. data/templates/csv/spec/exporter_spec.rb +17 -17
  44. data/templates/csv/spec/sample_data/coworkers.csv +2 -2
  45. data/templates/csv/spec/sample_data/deals.csv +2 -2
  46. data/templates/csv/spec/sample_data/organizations.csv +2 -2
  47. data/templates/csv/spec/sample_data/persons.csv +2 -2
  48. data/templates/easy/Gemfile +5 -5
  49. data/templates/easy/Rakefile.rb +7 -7
  50. data/templates/easy/convert.rb +2 -2
  51. data/templates/easy/spec/exporter_spec.rb +10 -10
  52. data/templates/easy/spec/spec_helper.rb +24 -24
  53. data/templates/excel/lib/tomodel.rb +207 -207
  54. data/templates/sqlserver/lib/tomodel.rb +79 -79
  55. metadata +3 -3
@@ -1,46 +1,46 @@
1
- module FruitToLime
2
- class ReferenceToSource
3
- include SerializeHelper
4
- attr_accessor :name, :id, :format
5
-
6
- def serialize_variables
7
- [:name, :format, :id].map { |prop| { :id => prop, :type => :string } }
8
- end
9
-
10
- def serialize_name
11
- "ReferenceToSource"
12
- end
13
-
14
- def get_import_rows
15
- (serialize_variables + [{ :id => :value, :type => :string }]).map do |p|
16
- map_to_row p
17
- end
18
- end
19
-
20
- def initialize(opt = nil)
21
- if opt != nil
22
- serialize_variables.each do |myattr|
23
- val = opt[myattr[:id]]
24
- instance_variable_set("@" + myattr[:id].to_s,val) if val != nil
25
- end
26
- end
27
- end
28
-
29
- def to_s
30
- return "#{@name}_#{@format}_#{@id}"
31
- end
32
-
33
- def ==(other)
34
- if other==nil
35
- return false
36
- end
37
- return @name == other.name && @id == other.id && @format== other.format
38
- end
39
- # Sets the id of this instance to the parameter supplied. Will also set {#name} and {#format} so that this reference is identified as a PAR identifier by Go.
40
- def par_se(id)
41
- @name = 'pase'
42
- @format = 'External'
43
- @id = id
44
- end
45
- end
1
+ module FruitToLime
2
+ class ReferenceToSource
3
+ include SerializeHelper
4
+ attr_accessor :name, :id, :format
5
+
6
+ def serialize_variables
7
+ [:name, :format, :id].map { |prop| { :id => prop, :type => :string } }
8
+ end
9
+
10
+ def serialize_name
11
+ "ReferenceToSource"
12
+ end
13
+
14
+ def get_import_rows
15
+ (serialize_variables + [{ :id => :value, :type => :string }]).map do |p|
16
+ map_to_row p
17
+ end
18
+ end
19
+
20
+ def initialize(opt = nil)
21
+ if opt != nil
22
+ serialize_variables.each do |myattr|
23
+ val = opt[myattr[:id]]
24
+ instance_variable_set("@" + myattr[:id].to_s,val) if val != nil
25
+ end
26
+ end
27
+ end
28
+
29
+ def to_s
30
+ return "#{@name}_#{@format}_#{@id}"
31
+ end
32
+
33
+ def ==(other)
34
+ if other==nil
35
+ return false
36
+ end
37
+ return @name == other.name && @id == other.id && @format== other.format
38
+ end
39
+ # Sets the id of this instance to the parameter supplied. Will also set {#name} and {#format} so that this reference is identified as a PAR identifier by Go.
40
+ def par_se(id)
41
+ @name = 'pase'
42
+ @format = 'External'
43
+ @id = id
44
+ end
45
+ end
46
46
  end
@@ -1,23 +1,23 @@
1
- module FruitToLime
2
- module Relation
3
- # This is the default, we have not been in contact with this
4
- # organization in any way.
5
- NoRelation = 0
6
-
7
- # Something is happening with this organization, we might have
8
- # booked a meeting with them or created a deal, etc.
9
- WorkingOnIt = 1
10
-
11
- # We have made a deal with this organization.
12
- IsACustomer = 2
13
-
14
- # We have made a deal with this organization but it was some
15
- # time ago and we don't consider them a customer any more.
16
- WasACustomer = 3
17
-
18
- # We had something going with this organization but we
19
- # couldn't close the deal and we don't think they will be a
20
- # customer to us in the foreseeable future.
21
- BeenInTouch = 4
22
- end
23
- end
1
+ module FruitToLime
2
+ module Relation
3
+ # This is the default, we have not been in contact with this
4
+ # organization in any way.
5
+ NoRelation = 0
6
+
7
+ # Something is happening with this organization, we might have
8
+ # booked a meeting with them or created a deal, etc.
9
+ WorkingOnIt = 1
10
+
11
+ # We have made a deal with this organization.
12
+ IsACustomer = 2
13
+
14
+ # We have made a deal with this organization but it was some
15
+ # time ago and we don't consider them a customer any more.
16
+ WasACustomer = 3
17
+
18
+ # We had something going with this organization but we
19
+ # couldn't close the deal and we don't think they will be a
20
+ # customer to us in the foreseeable future.
21
+ BeenInTouch = 4
22
+ end
23
+ end
@@ -1,338 +1,342 @@
1
- # encoding: utf-8
2
- module FruitToLime
3
- # The root model for Go import. This class is the container for everything else.
4
- class RootModel
5
- # the import_coworker is a special coworker that is set as
6
- # responsible for objects that requires a coworker, eg a note.
7
- attr_accessor :import_coworker
8
-
9
- attr_accessor :settings, :organizations, :coworkers, :deals, :notes
10
- def serialize_variables
11
- [
12
- {:id => :settings, :type => :settings},
13
- {:id => :coworkers, :type => :coworkers},
14
- {:id => :organizations, :type => :organizations},
15
- {:id => :deals, :type => :deals},
16
- {:id => :notes, :type => :notes},
17
- ]
18
- end
19
-
20
- def serialize_name
21
- "GoImport"
22
- end
23
-
24
- include SerializeHelper
25
-
26
- def initialize()
27
- @settings = Settings.new
28
- @organizations = []
29
- @coworkers = []
30
- @import_coworker = Coworker.new
31
- @import_coworker.integration_id = "import"
32
- @import_coworker.first_name = "Import"
33
- @coworkers.push @import_coworker
34
- @deals = []
35
- @notes = []
36
- end
37
-
38
- # Adds the specifed coworker object to the model.
39
- # @example Add a coworker from a hash
40
- # rootmodel.add_coworker({
41
- # :integration_id=>"123",
42
- # :first_name=>"Kalle",
43
- # :last_name=>"Anka",
44
- # :email=>"kalle.anka@vonanka.com"
45
- # })
46
- #
47
- # @example Add a coworker from a new coworker
48
- # coworker = FruitToLime::Coworker.new
49
- # coworker.integration_id = "123"
50
- # coworker.first_name="Kalle"
51
- # coworker.last_name="Anka"
52
- # coworker.email = "kalle.anka@vonanka.com"
53
- # rootmodel.add_coworker(coworker)
54
- #
55
- # @example If you want to keep adding coworkers and dont care about duplicates not being added
56
- # begin
57
- # rootmodel.add_coworker(coworker)
58
- # rescue FruitToLime::AlreadyAddedError
59
- # puts "Warning: already added coworker"
60
- # end
61
- # @see Coworker
62
- def add_coworker(coworker)
63
- @coworkers = [] if @coworkers == nil
64
-
65
- if coworker == nil
66
- raise "Missing coworker to add!"
67
- end
68
-
69
- coworker = Coworker.new(coworker) if !coworker.is_a?(Coworker)
70
-
71
- if find_coworker_by_integration_id(coworker.integration_id) != nil
72
- raise AlreadyAddedError, "Already added a coworker with integration_id #{coworker.integration_id}"
73
- end
74
-
75
- @coworkers.push(coworker)
76
-
77
- return coworker
78
- end
79
-
80
- # Adds the specifed organization object to the model.
81
- # @example Add an organization from a hash
82
- # rootmodel.add_organization({
83
- # :integration_id => "123",
84
- # :name => "Beagle Boys",
85
- # })
86
- #
87
- # @example Add an organization from a new organization
88
- # organization = FruitToLime::Organization.new
89
- # organization.integration_id = "123"
90
- # organization.name = "Beagle Boys"
91
- # rootmodel.add_organization(organization)
92
- #
93
- # @example If you want to keep adding organizations and dont
94
- # care about duplicates not being added. Your model might not
95
- # be saved due to duplicate integration_ids.
96
- # begin
97
- # rootmodel.add_organization(organization)
98
- # rescue FruitToLime::AlreadyAddedError
99
- # puts "Warning: already added organization"
100
- # end
101
- # @see Coworker
102
- def add_organization(organization)
103
- @organizations = [] if @organizations.nil?
104
-
105
- if organization.nil?
106
- raise "Missing organization to add"
107
- end
108
-
109
- organization = Organization.new(organization) if !organization.is_a?(Organization)
110
-
111
- if find_organization_by_integration_id(organization.integration_id) != nil
112
- raise AlreadyAddedError, "Already added an organization with integration_id #(organization.integration_id)"
113
- end
114
-
115
- @organizations.push(organization)
116
-
117
- return organization
118
- end
119
-
120
- # Adds the specifed deal object to the model.
121
- # @example Add an deal from a hash
122
- # rootmodel.add_deal({
123
- # :integration_id => "123",
124
- # :name => "Big deal",
125
- # })
126
- #
127
- # @example Add a deal from a new deal
128
- # deal = FruitToLime::Deal.new
129
- # deal.integration_id = "123"
130
- # deal.name = "Big deal"
131
- # rootmodel.add_deal(deal)
132
- #
133
- # @example If you want to keep adding deals and dont
134
- # care about duplicates not being added. Your model might not
135
- # be saved due to duplicate integration_ids.
136
- # begin
137
- # rootmodel.add_deal(deal)
138
- # rescue FruitToLime::AlreadyAddedError
139
- # puts "Warning: already added deal"
140
- # end
141
- # @see Coworker
142
- def add_deal(deal)
143
- @deals = [] if @deals.nil?
144
-
145
- if deal.nil?
146
- raise "Missing deal to add"
147
- end
148
-
149
- deal = Deal.new(deal) if !deal.is_a?(Deal)
150
-
151
- if find_deal_by_integration_id(deal.integration_id) != nil
152
- raise AlreadyAddedError, "Already added a deal with integration_id #{deal.integration_id}"
153
- end
154
-
155
- @deals.push(deal)
156
-
157
- return deal
158
- end
159
-
160
- # Adds the specifed note object to the model.
161
- # @example Add an deal from a hash
162
- # rootmodel.add_note({
163
- # :integration_id => "123",
164
- # :text => "This is a note",
165
- # })
166
- #
167
- # @example Add a note from a new note
168
- # note = FruitToLime::Note.new
169
- # note.integration_id = "123"
170
- # note.text = "Big deal"
171
- # rootmodel.add_note(note)
172
- #
173
- # @example If you want to keep adding deals and dont
174
- # care about duplicates not being added. Your model might not
175
- # be saved due to duplicate integration_ids.
176
- # begin
177
- # rootmodel.add_deal(deal)
178
- # rescue FruitToLime::AlreadyAddedError
179
- # puts "Warning: already added deal"
180
- # end
181
- # @see Coworker
182
- def add_note(note)
183
- @notes = [] if @notes == nil
184
-
185
- if note.nil?
186
- raise "Missing note to add"
187
- end
188
-
189
- note = Note.new(note) if !note.is_a?(Note)
190
-
191
- if (!note.integration_id.nil? && note.integration_id.length > 0) &&
192
- find_note_by_integration_id(note.integration_id) != nil
193
- raise AlreadyAddedError, "Already added a note with integration_id #{note.integration_id}"
194
- end
195
-
196
- @notes.push(note)
197
-
198
- return note
199
- end
200
-
201
- def with_new_note
202
- @notes = [] if @notes == nil
203
-
204
- note = Note.new
205
- @notes.push note
206
- yield note
207
- end
208
-
209
- def find_coworker_by_integration_id(integration_id)
210
- return @coworkers.find do |coworker|
211
- coworker.integration_id == integration_id
212
- end
213
- end
214
-
215
- def find_organization_by_integration_id(integration_id)
216
- return @organizations.find do |organization|
217
- organization.integration_id == integration_id
218
- end
219
- end
220
-
221
- def find_person_by_integration_id(integration_id)
222
- return nil if @organizations.nil?
223
- @organizations.each do |organization|
224
- person = organization.find_employee_by_integration_id(integration_id)
225
- return person if person
226
- end
227
- end
228
-
229
- def find_note_by_integration_id(integration_id)
230
- return @notes.find do |note|
231
- note.integration_id == integration_id
232
- end
233
- end
234
-
235
- # find deals for organization using {Organization#integration_id}
236
- def find_deals_for_organization(organization)
237
- deals = []
238
-
239
- deals = @deals.select do |deal|
240
- !deal.customer.nil? && deal.customer.integration_id == organization.integration_id
241
- end
242
-
243
- return deals
244
- end
245
-
246
- def find_deal_by_integration_id(integration_id)
247
- return @deals.find do |deal|
248
- deal.integration_id == integration_id
249
- end
250
- end
251
-
252
- # Returns a string describing problems with the data. For instance if integration_id for any entity is not unique.
253
- def sanity_check
254
- error = String.new
255
-
256
- dups = get_integration_id_duplicates(with_non_empty_integration_id(@coworkers))
257
- dups_error_items = (dups.collect{|coworker| coworker.integration_id}).compact
258
- if dups.length > 0
259
- error = "#{error}\nDuplicate coworker integration_id: #{dups_error_items.join(", ")}."
260
- end
261
-
262
- dups = get_integration_id_duplicates(with_non_empty_integration_id(@organizations))
263
- dups_error_items = (dups.collect{|org| org.integration_id}).compact
264
- if dups.length > 0
265
- error = "#{error}\nDuplicate organization integration_id: #{dups_error_items.join(", ")}."
266
- end
267
-
268
- dups = get_integration_id_duplicates(with_non_empty_integration_id(@deals))
269
- dups_error_items = (dups.collect{|deal| deal.integration_id}).compact
270
- if dups_error_items.length > 0
271
- error = "#{error}\nDuplicate deal integration_id: #{dups_error_items.join(", ")}."
272
- end
273
-
274
- persons = @organizations.collect{|o| o.employees}.flatten.compact
275
- dups = get_integration_id_duplicates(with_non_empty_integration_id(persons))
276
- dups_error_items = (dups.collect{|person| person.integration_id}).compact
277
- if dups_error_items.length > 0
278
- error = "#{error}\nDuplicate person integration_id: #{dups_error_items.join(", ")}."
279
- end
280
-
281
- return error.strip
282
- end
283
-
284
- def validate()
285
- error = String.new
286
-
287
- @organizations.each do |o|
288
- validation_message = o.validate()
289
-
290
- if !validation_message.empty?
291
- error = "#{error}\n#{validation_message}"
292
- end
293
- end
294
-
295
- @deals.each do |deal|
296
- validation_message = deal.validate
297
-
298
- if !validation_message.empty?
299
- error = "#{error}\n#{validation_message}"
300
- end
301
- end
302
-
303
- @notes.each do |note|
304
- validation_message = note.validate
305
-
306
- if !validation_message.empty?
307
- error = "#{error}\n#{validation_message}"
308
- end
309
- end
310
-
311
- return error.strip
312
- end
313
-
314
- # @!visibility private
315
- def to_rexml(doc)
316
- element_name = serialize_name
317
- elem = doc.add_element(element_name,{"Version"=>"v2_0"})
318
- SerializeHelper::serialize_variables_rexml(elem, self)
319
- end
320
-
321
- private
322
- # returns all items from the object array with duplicate integration ids.
323
- # To get all organizations with the same integration_id use
324
- # @example Get all the organization duplicates with the same integration id
325
- # rm.get_integration_id_duplicates(rm.organizations)
326
- def get_integration_id_duplicates(objects)
327
- uniq_items = objects.uniq {|item| item.integration_id}.compact
328
-
329
- return (objects - uniq_items).compact
330
- end
331
-
332
- def with_non_empty_integration_id(objects)
333
- return objects.select do |obj|
334
- obj.integration_id!=nil && !obj.integration_id.empty?
335
- end
336
- end
337
- end
338
- end
1
+ # encoding: utf-8
2
+ module FruitToLime
3
+ # The root model for Go import. This class is the container for everything else.
4
+ class RootModel
5
+ # the import_coworker is a special coworker that is set as
6
+ # responsible for objects that requires a coworker, eg a note.
7
+ attr_accessor :import_coworker
8
+
9
+ attr_accessor :settings, :organizations, :coworkers, :deals, :notes
10
+ def serialize_variables
11
+ [
12
+ {:id => :settings, :type => :settings},
13
+ {:id => :coworkers, :type => :coworkers},
14
+ {:id => :organizations, :type => :organizations},
15
+ {:id => :deals, :type => :deals},
16
+ {:id => :notes, :type => :notes},
17
+ ]
18
+ end
19
+
20
+ def serialize_name
21
+ "GoImport"
22
+ end
23
+
24
+ include SerializeHelper
25
+
26
+ def initialize()
27
+ @settings = Settings.new
28
+ @organizations = []
29
+ @coworkers = []
30
+ @import_coworker = Coworker.new
31
+ @import_coworker.integration_id = "import"
32
+ @import_coworker.first_name = "Import"
33
+ @coworkers.push @import_coworker
34
+ @deals = []
35
+ @notes = []
36
+ end
37
+
38
+ # Adds the specifed coworker object to the model.
39
+ # @example Add a coworker from a hash
40
+ # rootmodel.add_coworker({
41
+ # :integration_id=>"123",
42
+ # :first_name=>"Kalle",
43
+ # :last_name=>"Anka",
44
+ # :email=>"kalle.anka@vonanka.com"
45
+ # })
46
+ #
47
+ # @example Add a coworker from a new coworker
48
+ # coworker = FruitToLime::Coworker.new
49
+ # coworker.integration_id = "123"
50
+ # coworker.first_name="Kalle"
51
+ # coworker.last_name="Anka"
52
+ # coworker.email = "kalle.anka@vonanka.com"
53
+ # rootmodel.add_coworker(coworker)
54
+ #
55
+ # @example If you want to keep adding coworkers and dont care about duplicates not being added
56
+ # begin
57
+ # rootmodel.add_coworker(coworker)
58
+ # rescue FruitToLime::AlreadyAddedError
59
+ # puts "Warning: already added coworker"
60
+ # end
61
+ # @see Coworker
62
+ def add_coworker(coworker)
63
+ @coworkers = [] if @coworkers == nil
64
+
65
+ if coworker == nil
66
+ raise "Missing coworker to add!"
67
+ end
68
+
69
+ coworker = Coworker.new(coworker) if !coworker.is_a?(Coworker)
70
+
71
+ if find_coworker_by_integration_id(coworker.integration_id) != nil
72
+ raise AlreadyAddedError, "Already added a coworker with integration_id #{coworker.integration_id}"
73
+ end
74
+
75
+ @coworkers.push(coworker)
76
+
77
+ return coworker
78
+ end
79
+
80
+ # Adds the specifed organization object to the model.
81
+ # @example Add an organization from a hash
82
+ # rootmodel.add_organization({
83
+ # :integration_id => "123",
84
+ # :name => "Beagle Boys",
85
+ # })
86
+ #
87
+ # @example Add an organization from a new organization
88
+ # organization = FruitToLime::Organization.new
89
+ # organization.integration_id = "123"
90
+ # organization.name = "Beagle Boys"
91
+ # rootmodel.add_organization(organization)
92
+ #
93
+ # @example If you want to keep adding organizations and dont
94
+ # care about duplicates not being added. Your model might not
95
+ # be saved due to duplicate integration_ids.
96
+ # begin
97
+ # rootmodel.add_organization(organization)
98
+ # rescue FruitToLime::AlreadyAddedError
99
+ # puts "Warning: already added organization"
100
+ # end
101
+ # @see Coworker
102
+ def add_organization(organization)
103
+ @organizations = [] if @organizations.nil?
104
+
105
+ if organization.nil?
106
+ raise "Missing organization to add"
107
+ end
108
+
109
+ organization = Organization.new(organization) if !organization.is_a?(Organization)
110
+
111
+ if find_organization_by_integration_id(organization.integration_id) != nil
112
+ raise AlreadyAddedError, "Already added an organization with integration_id #(organization.integration_id)"
113
+ end
114
+
115
+ @organizations.push(organization)
116
+
117
+ return organization
118
+ end
119
+
120
+ # Adds the specifed deal object to the model.
121
+ # @example Add an deal from a hash
122
+ # rootmodel.add_deal({
123
+ # :integration_id => "123",
124
+ # :name => "Big deal",
125
+ # })
126
+ #
127
+ # @example Add a deal from a new deal
128
+ # deal = FruitToLime::Deal.new
129
+ # deal.integration_id = "123"
130
+ # deal.name = "Big deal"
131
+ # rootmodel.add_deal(deal)
132
+ #
133
+ # @example If you want to keep adding deals and dont
134
+ # care about duplicates not being added. Your model might not
135
+ # be saved due to duplicate integration_ids.
136
+ # begin
137
+ # rootmodel.add_deal(deal)
138
+ # rescue FruitToLime::AlreadyAddedError
139
+ # puts "Warning: already added deal"
140
+ # end
141
+ # @see Coworker
142
+ def add_deal(deal)
143
+ @deals = [] if @deals.nil?
144
+
145
+ if deal.nil?
146
+ raise "Missing deal to add"
147
+ end
148
+
149
+ deal = Deal.new(deal) if !deal.is_a?(Deal)
150
+
151
+ if find_deal_by_integration_id(deal.integration_id) != nil
152
+ raise AlreadyAddedError, "Already added a deal with integration_id #{deal.integration_id}"
153
+ end
154
+
155
+ if deal.responsible_coworker.nil?
156
+ deal.responsible_coworker = @import_coworker
157
+ end
158
+
159
+ @deals.push(deal)
160
+
161
+ return deal
162
+ end
163
+
164
+ # Adds the specifed note object to the model.
165
+ # @example Add an deal from a hash
166
+ # rootmodel.add_note({
167
+ # :integration_id => "123",
168
+ # :text => "This is a note",
169
+ # })
170
+ #
171
+ # @example Add a note from a new note
172
+ # note = FruitToLime::Note.new
173
+ # note.integration_id = "123"
174
+ # note.text = "Big deal"
175
+ # rootmodel.add_note(note)
176
+ #
177
+ # @example If you want to keep adding deals and dont
178
+ # care about duplicates not being added. Your model might not
179
+ # be saved due to duplicate integration_ids.
180
+ # begin
181
+ # rootmodel.add_deal(deal)
182
+ # rescue FruitToLime::AlreadyAddedError
183
+ # puts "Warning: already added deal"
184
+ # end
185
+ # @see Coworker
186
+ def add_note(note)
187
+ @notes = [] if @notes == nil
188
+
189
+ if note.nil?
190
+ raise "Missing note to add"
191
+ end
192
+
193
+ note = Note.new(note) if !note.is_a?(Note)
194
+
195
+ if (!note.integration_id.nil? && note.integration_id.length > 0) &&
196
+ find_note_by_integration_id(note.integration_id) != nil
197
+ raise AlreadyAddedError, "Already added a note with integration_id #{note.integration_id}"
198
+ end
199
+
200
+ @notes.push(note)
201
+
202
+ return note
203
+ end
204
+
205
+ def with_new_note
206
+ @notes = [] if @notes == nil
207
+
208
+ note = Note.new
209
+ @notes.push note
210
+ yield note
211
+ end
212
+
213
+ def find_coworker_by_integration_id(integration_id)
214
+ return @coworkers.find do |coworker|
215
+ coworker.integration_id == integration_id
216
+ end
217
+ end
218
+
219
+ def find_organization_by_integration_id(integration_id)
220
+ return @organizations.find do |organization|
221
+ organization.integration_id == integration_id
222
+ end
223
+ end
224
+
225
+ def find_person_by_integration_id(integration_id)
226
+ return nil if @organizations.nil?
227
+ @organizations.each do |organization|
228
+ person = organization.find_employee_by_integration_id(integration_id)
229
+ return person if person
230
+ end
231
+ end
232
+
233
+ def find_note_by_integration_id(integration_id)
234
+ return @notes.find do |note|
235
+ note.integration_id == integration_id
236
+ end
237
+ end
238
+
239
+ # find deals for organization using {Organization#integration_id}
240
+ def find_deals_for_organization(organization)
241
+ deals = []
242
+
243
+ deals = @deals.select do |deal|
244
+ !deal.customer.nil? && deal.customer.integration_id == organization.integration_id
245
+ end
246
+
247
+ return deals
248
+ end
249
+
250
+ def find_deal_by_integration_id(integration_id)
251
+ return @deals.find do |deal|
252
+ deal.integration_id == integration_id
253
+ end
254
+ end
255
+
256
+ # Returns a string describing problems with the data. For instance if integration_id for any entity is not unique.
257
+ def sanity_check
258
+ error = String.new
259
+
260
+ dups = get_integration_id_duplicates(with_non_empty_integration_id(@coworkers))
261
+ dups_error_items = (dups.collect{|coworker| coworker.integration_id}).compact
262
+ if dups.length > 0
263
+ error = "#{error}\nDuplicate coworker integration_id: #{dups_error_items.join(", ")}."
264
+ end
265
+
266
+ dups = get_integration_id_duplicates(with_non_empty_integration_id(@organizations))
267
+ dups_error_items = (dups.collect{|org| org.integration_id}).compact
268
+ if dups.length > 0
269
+ error = "#{error}\nDuplicate organization integration_id: #{dups_error_items.join(", ")}."
270
+ end
271
+
272
+ dups = get_integration_id_duplicates(with_non_empty_integration_id(@deals))
273
+ dups_error_items = (dups.collect{|deal| deal.integration_id}).compact
274
+ if dups_error_items.length > 0
275
+ error = "#{error}\nDuplicate deal integration_id: #{dups_error_items.join(", ")}."
276
+ end
277
+
278
+ persons = @organizations.collect{|o| o.employees}.flatten.compact
279
+ dups = get_integration_id_duplicates(with_non_empty_integration_id(persons))
280
+ dups_error_items = (dups.collect{|person| person.integration_id}).compact
281
+ if dups_error_items.length > 0
282
+ error = "#{error}\nDuplicate person integration_id: #{dups_error_items.join(", ")}."
283
+ end
284
+
285
+ return error.strip
286
+ end
287
+
288
+ def validate()
289
+ error = String.new
290
+
291
+ @organizations.each do |o|
292
+ validation_message = o.validate()
293
+
294
+ if !validation_message.empty?
295
+ error = "#{error}\n#{validation_message}"
296
+ end
297
+ end
298
+
299
+ @deals.each do |deal|
300
+ validation_message = deal.validate
301
+
302
+ if !validation_message.empty?
303
+ error = "#{error}\n#{validation_message}"
304
+ end
305
+ end
306
+
307
+ @notes.each do |note|
308
+ validation_message = note.validate
309
+
310
+ if !validation_message.empty?
311
+ error = "#{error}\n#{validation_message}"
312
+ end
313
+ end
314
+
315
+ return error.strip
316
+ end
317
+
318
+ # @!visibility private
319
+ def to_rexml(doc)
320
+ element_name = serialize_name
321
+ elem = doc.add_element(element_name,{"Version"=>"v2_0"})
322
+ SerializeHelper::serialize_variables_rexml(elem, self)
323
+ end
324
+
325
+ private
326
+ # returns all items from the object array with duplicate integration ids.
327
+ # To get all organizations with the same integration_id use
328
+ # @example Get all the organization duplicates with the same integration id
329
+ # rm.get_integration_id_duplicates(rm.organizations)
330
+ def get_integration_id_duplicates(objects)
331
+ uniq_items = objects.uniq {|item| item.integration_id}.compact
332
+
333
+ return (objects - uniq_items).compact
334
+ end
335
+
336
+ def with_non_empty_integration_id(objects)
337
+ return objects.select do |obj|
338
+ obj.integration_id!=nil && !obj.integration_id.empty?
339
+ end
340
+ end
341
+ end
342
+ end