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_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



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

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 history from a new history

history = GoImport::History.new
history.integration_id = "123"
history.text = "This is a history"
rootmodel.add_history(history)


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/go_import/model/rootmodel.rb', line 184

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


211
212
213
214
215
# File 'lib/go_import/model/rootmodel.rb', line 211

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

#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



517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# File 'lib/go_import/model/rootmodel.rb', line 517

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]" }


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

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



223
224
225
226
227
228
229
230
# File 'lib/go_import/model/rootmodel.rb', line 223

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 }


326
327
328
329
330
# File 'lib/go_import/model/rootmodel.rb', line 326

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



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

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



262
263
264
265
266
267
268
269
# File 'lib/go_import/model/rootmodel.rb', line 262

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" }


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

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!" }


367
368
369
370
371
# File 'lib/go_import/model/rootmodel.rb', line 367

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



252
253
254
255
256
257
258
259
# File 'lib/go_import/model/rootmodel.rb', line 252

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" }


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

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



232
233
234
235
236
237
238
239
240
# File 'lib/go_import/model/rootmodel.rb', line 232

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" }


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

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



242
243
244
245
246
247
248
249
250
# File 'lib/go_import/model/rootmodel.rb', line 242

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



557
558
559
560
561
562
563
564
565
566
# File 'lib/go_import/model/rootmodel.rb', line 557

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" \
  " Histories:     #{@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.



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/go_import/model/rootmodel.rb', line 386

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]" }


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

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" }


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

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" }


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

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" }


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

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



423
424
425
426
427
428
429
430
431
432
433
434
435
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
# File 'lib/go_import/model/rootmodel.rb', line 423

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