Class: GoImport::RootModel

Inherits:
Object
  • Object
show all
Includes:
SerializeHelper
Defined in:
lib/go_import/model/rootmodel.rb

Overview

The root model for Go import. This class is the container for everything else.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SerializeHelper

#get_import_rows, #serialize, #serialize_to_file

Constructor Details

#initializeRootModel

Returns a new instance of RootModel.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/go_import/model/rootmodel.rb', line 44

def initialize()
    @settings = Settings.new
    @organizations = {}
    @coworkers = {}
    @import_coworker = Coworker.new
    @import_coworker.integration_id = "import"
    @import_coworker.first_name = "Import"
    @coworkers[@import_coworker.integration_id] = @import_coworker
    @deals = {}
    @histories = {}
    @documents = Documents.new
    @configuration = {}

    configure
end

Instance Attribute Details

#configurationObject

The configuration is used to set run-time properties for go-import. This should not be confused with the model’s settings. Sets the following properties:

ALLOW_DEALS_WITHOUT_RESPONSIBLE - if set to true, deals without a responsible will NOT have the import user set as default.



23
24
25
# File 'lib/go_import/model/rootmodel.rb', line 23

def configuration
  @configuration
end

#coworkersObject

Returns the value of attribute coworkers.



14
15
16
# File 'lib/go_import/model/rootmodel.rb', line 14

def coworkers
  @coworkers
end

#dealsObject

Returns the value of attribute deals.



14
15
16
# File 'lib/go_import/model/rootmodel.rb', line 14

def deals
  @deals
end

#documentsObject (readonly)

Returns the value of attribute documents.



25
26
27
# File 'lib/go_import/model/rootmodel.rb', line 25

def documents
  @documents
end

#historiesObject

Returns the value of attribute histories.



14
15
16
# File 'lib/go_import/model/rootmodel.rb', line 14

def histories
  @histories
end

#import_coworkerObject

the import_coworker is a special coworker that is set as responsible for objects that requires a coworker, eg a history.



12
13
14
# File 'lib/go_import/model/rootmodel.rb', line 12

def import_coworker
  @import_coworker
end

#organizationsObject

Returns the value of attribute organizations.



14
15
16
# File 'lib/go_import/model/rootmodel.rb', line 14

def organizations
  @organizations
end

#personsObject (readonly)

Returns the value of attribute persons.



25
26
27
# File 'lib/go_import/model/rootmodel.rb', line 25

def persons
  @persons
end

#settingsObject

Returns the value of attribute settings.



14
15
16
# File 'lib/go_import/model/rootmodel.rb', line 14

def settings
  @settings
end

Instance Method Details

#add_client_visit(client_visit) ⇒ Object



214
215
216
217
218
219
220
221
222
# File 'lib/go_import/model/rootmodel.rb', line 214

def add_client_visit(client_visit)
    if client_visit.nil?
        return nil
    end
    if !client_visit.is_a?(ClientVisit)
        raise ArgumentError.new("Expected a ClientVisit")
    end
    add_history(client_visit)
end

#add_comment(comment) ⇒ Object



184
185
186
187
188
189
190
191
192
# File 'lib/go_import/model/rootmodel.rb', line 184

def add_comment(comment)
    if comment.nil?
        return nil
    end
    if !comment.is_a?(Comment)
        raise ArgumentError.new("Expected a Comment")
    end
    add_history(comment)
end

#add_coworker(coworker) ⇒ Object

Examples:

Add a coworker from a new coworker

coworker = GoImport::Coworker.new
coworker.integration_id = "123"
coworker.first_name="Kalle"
coworker.last_name="Anka"
coworker.email = "[email protected]"
rootmodel.add_coworker(coworker)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/go_import/model/rootmodel.rb', line 73

def add_coworker(coworker)
    if coworker.nil?
        return nil
    end

    if !coworker.is_a?(Coworker)
        raise ArgumentError.new("Expected a coworker")
    end

    if coworker.integration_id.nil? || coworker.integration_id.length == 0
        raise IntegrationIdIsRequiredError, "An integration id is required for a coworker."
    end

    if find_coworker_by_integration_id(coworker.integration_id, false) != nil
        raise AlreadyAddedError, "Already added a coworker with integration_id #{coworker.integration_id}"
    end

    @coworkers[coworker.integration_id] = coworker
    coworker.set_is_immutable

    return coworker
end

#add_deal(deal) ⇒ Object

Adds the specifed deal object to the model.

Examples:

Add a deal from a new deal

deal = GoImport::Deal.new
deal.integration_id = "123"
deal.name = "Big deal"
rootmodel.add_deal(deal)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/go_import/model/rootmodel.rb', line 131

def add_deal(deal)
    if deal.nil?
        return nil
    end

    if !deal.is_a?(Deal)
        raise ArgumentError.new("Expected a deal")
    end

    if deal.integration_id.nil? || deal.integration_id.length == 0
        raise IntegrationIdIsRequiredError, "An integration id is required for a deal."
    end

    if find_deal_by_integration_id(deal.integration_id, false) != nil
        raise AlreadyAddedError, "Already added a deal with integration_id #{deal.integration_id}"
    end

    if !configuration[:allow_deals_without_responsible] && deal.responsible_coworker.nil?
        deal.responsible_coworker = @import_coworker
    end

    @deals[deal.integration_id] = deal
    deal.set_is_immutable

    return deal
end

#add_file(file) ⇒ Object



267
268
269
270
271
# File 'lib/go_import/model/rootmodel.rb', line 267

def add_file(file)
    @documents = Documents.new if @documents == nil

    return @documents.add_file(file)
end

#add_history(history) ⇒ Object

Adds the specifed history object to the model.

If no integration_id has been specifed go-import generate one.

Examples:

Add a comment from a new comment

comment = GoImport::Comment.new
comment.integration_id = "123"
comment.text = "This is a history"
rootmodel.add_comment(comment)


234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/go_import/model/rootmodel.rb', line 234

def add_history(history)
    if history.nil?
        return nil
    end

    if !history.is_a?(History)
        raise ArgumentError.new("Expected a history")
    end

    if history.integration_id.nil? || history.integration_id.length == 0
        history.integration_id = @histories.length.to_s
    end

    if find_history_by_integration_id(history.integration_id, false) != nil
        raise AlreadyAddedError, "Already added a history with integration_id #{history.integration_id}"
    end

    if history.created_by.nil?
        history.created_by = @import_coworker
    end

    @histories[history.integration_id] = history
    history.set_is_immutable

    return history
end


261
262
263
264
265
# File 'lib/go_import/model/rootmodel.rb', line 261

def add_link(link)
    @documents = Documents.new if @documents == nil

    return @documents.add_link(link)
end

#add_organization(organization) ⇒ Object

Adds the specifed organization object to the model.

Examples:

Add an organization from a new organization

organization = GoImport::Organization.new
organization.integration_id = "123"
organization.name = "Beagle Boys"
rootmodel.add_organization(organization)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/go_import/model/rootmodel.rb', line 102

def add_organization(organization)
    if organization.nil?
        return nil
    end

    if !organization.is_a?(Organization)
        raise ArgumentError.new("Expected an organization")
    end

    if organization.integration_id.nil? || organization.integration_id.length == 0
        raise IntegrationIdIsRequiredError, "An integration id is required for an organization."
    end

    if find_organization_by_integration_id(organization.integration_id, false) != nil
        raise AlreadyAddedError, "Already added an organization with integration_id #{organization.integration_id}"
    end

    @organizations[organization.integration_id] = organization
    organization.set_is_immutable

    return organization
end

#add_sales_call(sales_call) ⇒ Object



174
175
176
177
178
179
180
181
182
# File 'lib/go_import/model/rootmodel.rb', line 174

def add_sales_call(sales_call)
    if sales_call.nil?
        return nil
    end
    if !sales_call.is_a?(SalesCall)
        raise ArgumentError.new("Expected a SalesCall")
    end
    add_history(sales_call)
end

#add_talked_to(talked_to) ⇒ Object



194
195
196
197
198
199
200
201
202
# File 'lib/go_import/model/rootmodel.rb', line 194

def add_talked_to(talked_to)
    if talked_to.nil?
        return nil
    end
    if !talked_to.is_a?(TalkedTo)
        raise ArgumentError.new("Expected a TalkedTo")
    end
    add_history(talked_to)
end

#add_tried_to_reach(tried_to_reach) ⇒ Object



204
205
206
207
208
209
210
211
212
# File 'lib/go_import/model/rootmodel.rb', line 204

def add_tried_to_reach(tried_to_reach)
    if tried_to_reach.nil?
        return nil
    end
    if !tried_to_reach.is_a?(TriedToReach)
        raise ArgumentError.new("Expected a TriedToReach")
    end
    add_history(tried_to_reach)
end

#configureObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/go_import/model/rootmodel.rb', line 158

def configure()
    if defined?(ALLOW_DEALS_WITHOUT_RESPONSIBLE)
        config_value = ALLOW_DEALS_WITHOUT_RESPONSIBLE.to_s

        configuration[:allow_deals_without_responsible] =
            config_value.downcase == "true" || config_value == "1"
    end

    if defined?(REPORT_RESULT)
        config_value = REPORT_RESULT.to_s

        configuration[:report_result] =
            config_value.downcase == "true" || config_value == "1"
    end
end

#create_zip(filename, xml, files) ⇒ Object



567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/go_import/model/rootmodel.rb', line 567

def create_zip(filename, xml, files)
    Zip::File.open("#{Dir.pwd}/#{filename}", Zip::File::CREATE) do |zip_file|
        puts "Add go.xml file to zip '#{filename}'..."
        zip_file.add('go.xml', xml)

        if files.length > 0
            if defined?(FILES_FOLDER) && !FILES_FOLDER.empty?()
                puts "Files with relative path are imported from '#{FILES_FOLDER}'."
                root_folder = FILES_FOLDER
            else
                puts "Files with relative path are imported from the current folder (#{Dir.pwd})."
                root_folder = Dir.pwd
            end

            # If a file's path is absolute, then we probably dont
            # have the files in the same location here. For
            # example, the customer might have stored their files
            # at f:\lime-easy\documents. We must replace this part
            # of each file with the root_folder from above.
            if defined?(FILES_FOLDER_AT_CUSTOMER) && !FILES_FOLDER_AT_CUSTOMER.empty?()
                files_folder_at_customer = FILES_FOLDER_AT_CUSTOMER
                puts "Files with absolute paths will have the part '#{files_folder_at_customer}' replaced with '#{root_folder}'."
            else
                files_folder_at_customer = ""
                puts "Files with absolute paths will be imported from their origial location."
            end

            # 1) files/ - a folder with all files referenced from
            # the source.
            files.with_progress(" - Trying to add files to zip...").each do |file|
                # we dont need to check that the file exists since
                # we assume that rootmodel.validate has been
                # called before save_to_zip.
                file.add_to_zip_file(zip_file)
            end
        end
        puts "Compressing zip file ... "
    end
end

#find_coworker(report_result = !!configuration[:report_result],, &block) ⇒ Object

Finds a coworker based on one of its property. Returns the first found matching coworker

Examples:

Finds a coworker on its name

rm.find_coworker {|coworker| coworker.email == "[email protected]" }


396
397
398
399
400
# File 'lib/go_import/model/rootmodel.rb', line 396

def find_coworker(report_result=!!configuration[:report_result], &block)
  result = find(@coworkers.values.flatten, &block)
  report_failed_to_find_object("coworker") if result.nil? and report_result
  return result
end

#find_coworker_by_integration_id(integration_id, report_result = !!configuration[:report_result])) ⇒ Object



273
274
275
276
277
278
279
280
# File 'lib/go_import/model/rootmodel.rb', line 273

def find_coworker_by_integration_id(integration_id, report_result=!!configuration[:report_result])
    if @coworkers.has_key?(integration_id)
        return @coworkers[integration_id]
    else
        report_failed_to_find_object("coworker", ":#{integration_id}") if report_result
        return nil
    end
end

#find_deal(report_result = !!configuration[:report_result],, &block) ⇒ Object

Finds a deal based on one of its property. Returns the first found matching deal Method is much slower then using find_deal_by_integration_id

Examples:

Finds a deal on its name

rm.find_deal {|deal| deal.value == 120000 }


376
377
378
379
380
# File 'lib/go_import/model/rootmodel.rb', line 376

def find_deal(report_result=!!configuration[:report_result], &block)
  result = find(@deals.values.flatten, &block)
  report_failed_to_find_object("person") if result.nil? and report_result
  return result
end

#find_deal_by_integration_id(integration_id, report_result = !!configuration[:report_result])) ⇒ Object



321
322
323
324
325
326
327
328
# File 'lib/go_import/model/rootmodel.rb', line 321

def find_deal_by_integration_id(integration_id, report_result=!!configuration[:report_result])
    if @deals.has_key?(integration_id)
        return @deals[integration_id]
    else
        report_failed_to_find_object("deal", ":#{integration_id}") if report_result
        return nil
    end
end

#find_deals_for_organization(organization) ⇒ Object

find deals for organization using Organization#integration_id



312
313
314
315
316
317
318
319
# File 'lib/go_import/model/rootmodel.rb', line 312

def find_deals_for_organization(organization)
    deals = []

    deals = @deals.values.select do |deal|
        !deal.customer.nil? && deal.customer.integration_id == organization.integration_id
    end
    return deals
end

#find_document(type, report_result = !!configuration[:report_result],, &block) ⇒ Object

Finds a document based on one of its property. Returns the first found matching document

Examples:

Finds a document on its name

rm.find_document(:file) {|document| document.name == "Important Tender" }


427
428
429
430
431
432
# File 'lib/go_import/model/rootmodel.rb', line 427

def find_document(type, report_result=!!configuration[:report_result], &block)
  result = find(@documents.files, &block) if type == :file
  result = find(@documents.links, &block) if type == :link
  report_failed_to_find_object("document") if result.nil? and report_result
  return result
end

#find_history(report_result = !!configuration[:report_result],, &block) ⇒ Object

Finds a history based on one of its property. Returns the first found matching history

Examples:

Finds a history on its name

rm.find_history {|history| history.text == "hello!" }


417
418
419
420
421
# File 'lib/go_import/model/rootmodel.rb', line 417

def find_history(report_result=!!configuration[:report_result], &block)
  result = find(@histories.values.flatten, &block)
  report_failed_to_find_object("history") if result.nil? and report_result
  return result
end

#find_history_by_integration_id(integration_id, report_result = !!configuration[:report_result])) ⇒ Object



302
303
304
305
306
307
308
309
# File 'lib/go_import/model/rootmodel.rb', line 302

def find_history_by_integration_id(integration_id, report_result=!!configuration[:report_result])
    if @histories.has_key?(integration_id)
        return @histories[integration_id]
    else
        report_failed_to_find_object("history", ":#{integration_id}") if report_result
        return nil
    end
end

#find_organization(report_result = !!configuration[:report_result],, &block) ⇒ Object

Finds a organization based on one of its property. Returns the first found matching organization Method is much slower then using find_organization_by_integration_id

Examples:

Finds a organization on its name

rm.find_organization {|org| org.name == "Lundalogik" }


335
336
337
338
339
# File 'lib/go_import/model/rootmodel.rb', line 335

def find_organization(report_result=!!configuration[:report_result], &block)
  result = find(@organizations.values.flatten, &block)
  report_failed_to_find_object("organization") if result.nil? and report_result
  return result
end

#find_organization_by_integration_id(integration_id, report_result = !!configuration[:report_result])) ⇒ Object



282
283
284
285
286
287
288
289
290
# File 'lib/go_import/model/rootmodel.rb', line 282

def find_organization_by_integration_id(integration_id, report_result=!!configuration[:report_result])
    if @organizations.has_key?(integration_id)
        return @organizations[integration_id]
    else
        report_failed_to_find_object("organization", ":#{integration_id}") if report_result
        return nil
    end

end

#find_person(report_result = !!configuration[:report_result],, &block) ⇒ Object

Finds a person based on one of its property. Returns the first found matching person

Examples:

Finds a person on its name

rm.find_person {|person| person.first_name == "Kalle" }


355
356
357
358
359
# File 'lib/go_import/model/rootmodel.rb', line 355

def find_person(report_result=!!configuration[:report_result], &block)
  result = find(persons, &block)
  report_failed_to_find_object("person") if result.nil? and report_result
  return result
end

#find_person_by_integration_id(integration_id, report_result = !!configuration[:report_result])) ⇒ Object



292
293
294
295
296
297
298
299
300
# File 'lib/go_import/model/rootmodel.rb', line 292

def find_person_by_integration_id(integration_id, report_result=!!configuration[:report_result])
    return nil if @organizations.nil?
    @organizations.each do |key, organization|
        person = organization.find_employee_by_integration_id(integration_id)
        return person if person
    end
    report_failed_to_find_object("person", ":#{integration_id}") if report_result
    return nil
end

#report_rootmodel_statusObject



607
608
609
610
611
612
613
614
615
616
# File 'lib/go_import/model/rootmodel.rb', line 607

def report_rootmodel_status
  #nbr_of_persons = @organizations.collect{|k, o| o.employees}.flatten.compact.length
  nbr_of_documents = @documents.files.length + @documents.links.length
  puts "Rootmodel contains:\n" \
  " Organizations: #{@organizations.length}\n" \
  " Persons:       #{persons.length}\n" \
  " Deals:         #{@deals.length}\n" \
  " History logs:  #{@histories.length}\n" \
  " Documents:     #{nbr_of_documents}"
end

#sanity_checkObject

Returns a string describing problems with the data. For instance if integration_id for any entity is not unique.



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/go_import/model/rootmodel.rb', line 436

def sanity_check
    error = String.new

    # dups = get_integration_id_duplicates(with_non_empty_integration_id(@coworkers))
    # dups_error_items = (dups.collect{|coworker| coworker.integration_id}).compact
    # if dups.length > 0
    #     error = "#{error}\nDuplicate coworker integration_id: #{dups_error_items.join(", ")}."
    # end

    # dups = get_integration_id_duplicates(with_non_empty_integration_id(@organizations))
    # dups_error_items = (dups.collect{|org| org.integration_id}).compact
    # if dups.length > 0
    #     error = "#{error}\nDuplicate organization integration_id: #{dups_error_items.join(", ")}."
    # end

    # dups = get_integration_id_duplicates(with_non_empty_integration_id(@deals))
    # dups_error_items = (dups.collect{|deal| deal.integration_id}).compact
    # if dups_error_items.length > 0
    #     error = "#{error}\nDuplicate deal integration_id: #{dups_error_items.join(", ")}."
    # end

    persons = @organizations.collect{|k, o| o.employees}.flatten.compact
    dups = get_integration_id_duplicates(with_non_empty_integration_id(persons))
    dups_error_items = (dups.collect{|person| person.integration_id}).compact
    if dups_error_items.length > 0
        error = "#{error}\nDuplicate person integration_id: #{dups_error_items.join(", ")}."
    end

    dups = get_integration_id_duplicates(with_non_empty_integration_id(@documents.links))
    dups_error_items = (dups.collect{|l| l.integration_id}).compact
    if dups_error_items.length > 0
        error = "#{error}\nDuplicate link integration_id: #{dups_error_items.join(", ")}."
    end

    return error.strip
end

#select_coworkers(report_result = !!configuration[:report_result],, &block) ⇒ Object

Finds coworkers based on one of their property. Returns all matching coworkers

Examples:

Selects coworkers on their names

rm.select_coworkers {|coworker| coworker.email == "[email protected]" }


406
407
408
409
410
# File 'lib/go_import/model/rootmodel.rb', line 406

def select_coworkers(report_result=!!configuration[:report_result], &block)
  result = select(@coworkers, &block)
  report_failed_to_find_object("coworker") if result.empty? and report_result
  return result
end

#select_deals(report_result = !!configuration[:report_result],, &block) ⇒ Object

Finds deals based on one of their property. Returns all matching deals

Examples:

Selects deals on their names

rm.select_deals {|deal| deal.name == "Big Deal" }


386
387
388
389
390
# File 'lib/go_import/model/rootmodel.rb', line 386

def select_deals(report_result=!!configuration[:report_result], &block)
  result = select(@deals.values.flatten, &block)
  report_failed_to_find_object("deal") if result.empty? and report_result
  return result
end

#select_organizations(report_result = !!configuration[:report_result],, &block) ⇒ Object

Finds organizations based on one of their property. Returns all matching organizations

Examples:

Selects organizations on their names

rm.select_organizations {|org| org.name == "Lundalogik" }


345
346
347
348
349
# File 'lib/go_import/model/rootmodel.rb', line 345

def select_organizations(report_result=!!configuration[:report_result], &block)
  result = select(@organizations.values.flatten, &block)
  report_failed_to_find_object("organization") if result.empty? and report_result
  return result
end

#select_persons(report_result = !!configuration[:report_result],, &block) ⇒ Object

Finds persons based on one of their property. Returns all matching persons

Examples:

Selects persons on their names

rm.select_person {|p| p.first_name == "Kalle" }


365
366
367
368
369
# File 'lib/go_import/model/rootmodel.rb', line 365

def select_persons(report_result=!!configuration[:report_result], &block)
  result = select(persons, &block)
  report_failed_to_find_object("person") if result.empty? and report_result
  return result
end

#serialize_nameObject



38
39
40
# File 'lib/go_import/model/rootmodel.rb', line 38

def serialize_name
    "GoImport"
end

#serialize_variablesObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/go_import/model/rootmodel.rb', line 27

def serialize_variables
    [
     {:id => :settings, :type => :settings},
     {:id => :coworkers, :type => :coworkers},
     {:id => :organizations, :type => :organizations},
     {:id => :deals, :type => :deals},
     {:id => :histories, :type => :histories},
     {:id => :documents, :type => :documents},
    ]
end

#validate(ignore_invalid_files = false, max_file_size) ⇒ Object



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/go_import/model/rootmodel.rb', line 473

def validate(ignore_invalid_files = false, max_file_size)
    errors = String.new
    warnings = String.new

    @organizations.each do |k, o|
        validation_message = o.validate()

        if !validation_message.empty?
            errors = "#{errors}\n#{validation_message}"
        end
    end

    converter_deal_statuses = @settings.deal.statuses.map {|status| status.label} if @settings.deal != nil
    @deals.each do |key, deal|
        error, warning = deal.validate converter_deal_statuses

        if !error.empty?
            errors = "#{errors}\n#{error}"
        end
        if !warning.empty?
            warnings = "#{warnings}\n#{warning}"
        end
    end

    @histories.each do |key, history|
        validation_message = history.validate

        if !validation_message.empty?
            errors = "#{errors}\n#{validation_message}"
        end
    end

    @documents.links.each do |link|
        validation_message = link.validate
        if !validation_message.empty?
            errors = "#{errors}\n#{validation_message}"
        end
    end

    @documents.files.each do |file|
        validation_message = file.validate(ignore_invalid_files, max_file_size)
        if !validation_message.empty?
            errors = "#{errors}\n#{validation_message}"
        end
    end

    return [errors.strip, warnings.strip]
end