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 = {}
    @notes = {}
    @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

#import_coworkerObject

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



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

def import_coworker
  @import_coworker
end

#notesObject

Returns the value of attribute notes.



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

def notes
  @notes
end

#organizationsObject

Returns the value of attribute organizations.



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

def organizations
  @organizations
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)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/go_import/model/rootmodel.rb', line 69

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) != 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)


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

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) != 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



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

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

    return @documents.add_file(file)
end


200
201
202
203
204
# File 'lib/go_import/model/rootmodel.rb', line 200

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

    return @documents.add_link(link)
end

#add_note(note) ⇒ Object

Adds the specifed note object to the model.

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

Examples:

Add a note from a new note

note = GoImport::Note.new
note.integration_id = "123"
note.text = "This is a note"
rootmodel.add_note(note)


173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/go_import/model/rootmodel.rb', line 173

def add_note(note)
    if note.nil?
        return nil
    end

    if !note.is_a?(Note)
        raise ArgumentError.new("Expected a note")
    end

    if note.integration_id.nil? || note.integration_id.length == 0
        note.integration_id = @notes.length.to_s
    end
    
    if find_note_by_integration_id(note.integration_id) != nil
        raise AlreadyAddedError, "Already added a note with integration_id #{note.integration_id}"
    end

    if note.created_by.nil?
        note.created_by = @import_coworker
    end
    
    @notes[note.integration_id] = note
    note.set_is_immutable

    return note
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)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/go_import/model/rootmodel.rb', line 98

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) != 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



154
155
156
157
158
159
160
161
# File 'lib/go_import/model/rootmodel.rb', line 154

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
end

#create_zip(filename, xml, files) ⇒ Object



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/go_import/model/rootmodel.rb', line 399

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_by_integration_id(integration_id) ⇒ Object



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

def find_coworker_by_integration_id(integration_id)
    if @coworkers.has_key?(integration_id)
        return @coworkers[integration_id]
    else
        return nil
    end
end

#find_deal_by_integration_id(integration_id) ⇒ Object



256
257
258
259
260
261
262
# File 'lib/go_import/model/rootmodel.rb', line 256

def find_deal_by_integration_id(integration_id)
    if @deals.has_key?(integration_id)
        return @deals[integration_id]
    else
        return nil
    end
end

#find_deals_for_organization(organization) ⇒ Object

find deals for organization using Organization#integration_id



246
247
248
249
250
251
252
253
254
# File 'lib/go_import/model/rootmodel.rb', line 246

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_note_by_integration_id(integration_id) ⇒ Object



237
238
239
240
241
242
243
# File 'lib/go_import/model/rootmodel.rb', line 237

def find_note_by_integration_id(integration_id)
    if @notes.has_key?(integration_id)
        return @notes[integration_id]
    else
        return nil
    end
end

#find_organization_by_integration_id(integration_id) ⇒ Object



220
221
222
223
224
225
226
227
# File 'lib/go_import/model/rootmodel.rb', line 220

def find_organization_by_integration_id(integration_id)
    if @organizations.has_key?(integration_id)
        return @organizations[integration_id]
    else
        return nil
    end

end

#find_person_by_integration_id(integration_id) ⇒ Object



229
230
231
232
233
234
235
# File 'lib/go_import/model/rootmodel.rb', line 229

def find_person_by_integration_id(integration_id)
    return nil if @organizations.nil?
    @organizations.each do |key, organization|
        person = organization.find_employee_by_integration_id(integration_id)
        return person if person
    end
end

#sanity_checkObject

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



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/go_import/model/rootmodel.rb', line 266

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

#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 => :notes, :type => :notes},
     {:id => :documents, :type => :documents},
    ]
end

#validate(ignore_invalid_files = false, max_file_size) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/go_import/model/rootmodel.rb', line 303

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|
    #@deals.each do |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

    #@notes.each do |note|
    @notes.each do |key, note|
        validation_message = note.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