Advertisement
rodrigoAIS

Untitled

Feb 10th, 2017
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 11.39 KB | None | 0 0
  1. class CloneDatabaseService
  2.   attr_accessor :equal
  3.   attr_accessor :all_records
  4.  
  5.   def self.default_models_for_sinchronization
  6.     @all_records.push('CloneDatabase'.constantize)
  7.     @all_records.push('AttachedFile'.constantize)
  8.   end
  9.  
  10.   def self.validate_table_withoud_records
  11.     clone_database = CloneDatabase.last
  12.     if clone_database != nil
  13.       start = clone_database.start
  14.       check_extranet = clone_database.check_extranet
  15.       finish = clone_database.finish
  16.     end
  17.  
  18.     if Rails.application.config.intranet_site
  19.       if clone_database == nil
  20.         CloneDatabase.create(
  21.             start: '2012-10-26 13:29:52',
  22.             check_extranet: nil, finish: nil,
  23.             migration_version: ActiveRecord::Migrator.current_version.to_s
  24.         )
  25.       elsif start != nil && check_extranet != nil && finish != nil
  26.         CloneDatabase.create(
  27.             start: Time.now,
  28.             check_extranet: nil,
  29.             finish: nil,
  30.             migration_version: ActiveRecord::Migrator.current_version.to_s
  31.         )
  32.       end
  33.     else
  34.       if clone_database != nil
  35.         if start != nil && check_extranet == nil && finish == nil
  36.           clone_database.check_extranet = Time.now
  37.           clone_database.save
  38.         end
  39.       end
  40.     end
  41.   end
  42.  
  43.   def self.is_restore_database(params)
  44.     if params[:model].class == String
  45.       @equal = true
  46.       array_with_tables_to_remove_from_restoration_download = [
  47.           'SinergiaCache5252Report',
  48.           'SinergiaCacheResultReport',
  49.           'SinergiaParam',
  50.           'SinergiaValue',
  51.           'CacheSinergiaValue',
  52.           'CkeditorAsset',
  53.           'SchemaMigration',
  54.           'KeyRestoreDatabase'
  55.       ]
  56.  
  57.       parse_model = JSON.parse(params[:model])
  58.       array_with_tables_to_remove_from_restoration_download.each do |table|
  59.         parse_model.delete(table)
  60.       end
  61.       params[:model] = parse_model
  62.     else
  63.       @equal = false
  64.     end
  65.   end
  66.  
  67.   def self.validate_auto_reference_association_belongs_to(association_belongs_to)
  68.     copy_association_belongs_to = association_belongs_to
  69.     copy_association_belongs_to.each do |association|
  70.       if association.name == :parent || association.name == :friend
  71.         association_belongs_to.delete(association)
  72.         break
  73.       end
  74.     end
  75.     return association_belongs_to
  76.   end
  77.  
  78.   def self.validate_auto_reference_association_has_many(association_has_many)
  79.     copy_association_has_many = association_has_many
  80.     copy_association_has_many.each do |association|
  81.       if association.name == :parent || association.name == :friend
  82.         association_has_many.delete(association)
  83.         break
  84.       end
  85.     end
  86.     return association_has_many
  87.   end
  88.  
  89.   def self.populate_model_collection(model)
  90.     association_belongs_to = model.reflect_on_all_associations(:belongs_to)
  91.     association_has_many = model.reflect_on_all_associations(:has_many)
  92.  
  93.     association_belongs_to = validate_auto_reference_association_belongs_to(association_belongs_to)
  94.     association_has_many = validate_auto_reference_association_has_many(association_has_many)
  95.  
  96.     association_belongs_to.each do |association|
  97.       unless @all_records.include?(association.name.to_s.singularize.camelize.constantize)
  98.         populate_model_collection(association.name.to_s.camelize.constantize)
  99.       end
  100.     end
  101.  
  102.     # name.to_s.singularize.capitalize.constantize
  103.     unless @all_records.include?(model.name.to_s.singularize.camelize.constantize)
  104.       @all_records << model.name.to_s.singularize.camelize.constantize
  105.     end
  106.  
  107.     association_has_many.each do |association|
  108.       unless @all_records.include?(association.name.to_s.singularize.camelize.constantize)
  109.         populate_model_collection(association.name.to_s.singularize.camelize.constantize)
  110.       end
  111.     end
  112.   end
  113.  
  114.   def self.build_collection_of_models(models_for_mapping)
  115.     @all_records = []
  116.     models_for_mapping.each do |model|
  117.       populate_model_collection(model.constantize)
  118.     end
  119.   end
  120.  
  121.   def self.find_vinculate_record(attached_file)
  122.     file_write = File.new("#{Rails.root.join('uploads/cache').to_s}/#{attached_file.attached_file_id}.json", 'w+')
  123.     value = Base64.strict_encode64(
  124.         File.read(Rails.root.join("uploads/store/#{attached_file.attached_file_id}").to_s)
  125.     )
  126.     file_write.puts value
  127.     file_write.close
  128.     name = {name: attached_file.attached_file_id}
  129.     name
  130.   end
  131.  
  132.   def self.check_if_exists_attached_file(model, files)
  133.     AttachedFile.all.each do |attached_file|
  134.       if attached_file.model_type == model.to_s &&
  135.           model.exists?(attached_file.model_id) &&
  136.           File.exist?(Rails.root.join("uploads/store/#{attached_file.attached_file_id}").to_s)
  137.         files << find_vinculate_record(attached_file)
  138.       end
  139.     end
  140.     files
  141.   end
  142.  
  143.   def self.build_object(model, download)
  144.     name_file_and_value = {}
  145.     if @equal
  146.       if model.column_names.include?('intranet_only') && Rails.application.config.intranet_site
  147.         name_file_and_value[:value] = model.all.where('intranet_only = ?', false)
  148.       else
  149.         name_file_and_value[:value] = model.all
  150.       end
  151.     else
  152.       if Rails.application.config.intranet_site
  153.         if model.column_names.include?('intranet_only')
  154.           name_file_and_value[:value] = model.all.where('updated_at > ? AND intranet_only = ?', download.start, false)
  155.         else
  156.           name_file_and_value[:value] = model.all.where('updated_at > ?', download.start)
  157.         end
  158.       else
  159.         name_file_and_value[:value] = model.all.where('updated_at > ?', download.start)
  160.       end
  161.     end
  162.  
  163.     name_file_and_value[:name] = model.to_s
  164.     create_file(name_file_and_value[:name], name_file_and_value[:value])
  165.     name = {name: model.to_s}
  166.     name
  167.   end
  168.  
  169.  
  170.   def self.build_files(all_files, records)
  171.     all_files.each do |file|
  172.       if File.exist?(Rails.root.join("uploads/store/#{file}").to_s)
  173.         file_write = Base64.strict_encode64(File.read(Rails.root.join("uploads/store/#{file}").to_s))
  174.         name = {name: file}
  175.         create_file(file, file_write)
  176.         records << name
  177.       end
  178.     end
  179.     records
  180.   end
  181.  
  182.   def self.find_records(model, download)
  183.     all_models = []
  184.     if @equal
  185.       all_models = model.all
  186.     else
  187.       all_models = model.all.where('updated_at > ?', download.start)
  188.     end
  189.     all_models
  190.   end
  191.  
  192.   def self.get_files(model, records, download)
  193.     all_records = find_records(model, download)
  194.  
  195.     if all_records.count != 0
  196.       all_files = []
  197.       all_records.each do |record|
  198.         if record.title_image_id != nil
  199.           all_files << record.title_image_id
  200.         end
  201.       end
  202.       return build_files(all_files, records)
  203.     else
  204.       return records
  205.     end
  206.   end
  207.  
  208.   def self.create_file(name, files)
  209.     file_write = File.new("#{Rails.root.join('uploads/cache').to_s}/#{name}.json", 'w+')
  210.     count_comma = 0
  211.     max_regs_fsync = 1000
  212.     if files.class != String
  213.       file_write.print '['
  214.       files.each do |file|
  215.         if count_comma > 0
  216.           file_write.print ','
  217.         end
  218.         #TODO: OTIMIZAR USO DE MEMORIA, GASTO ATUAL: 3878699 BYTES
  219.         file_write.print file.to_json
  220.  
  221.         # XXX: grava em disco
  222.         if (count_comma > 0 && count_comma % max_regs_fsync == 0)
  223.           file_write.fsync
  224.         end
  225.  
  226.         count_comma += 1
  227.       end
  228.       file_write.print ']'
  229.     else
  230.       file_write.print files
  231.  
  232.       # XXX: grava em disco
  233.       file_write.fsync
  234.     end
  235.  
  236.     file_write.close
  237.     name
  238.   end
  239.  
  240.   def self.build_videos(all_videos, records)
  241.     all_videos.each do |video|
  242.       if File.exist?(Rails.root.join("uploads/store/#{video}").to_s)
  243.         file = Base64.strict_encode64(File.read(Rails.root.join("uploads/store/#{video}").to_s))
  244.         name = {name: video}
  245.         create_file(video, file)
  246.         records << name
  247.         file.close
  248.       end
  249.     end
  250.     records
  251.   end
  252.  
  253.   def self.get_videos(model, records, download)
  254.     all_records = find_records(model, download)
  255.  
  256.     if all_records.count != 0
  257.       all_videos = []
  258.       all_records.each do |record|
  259.         if record.video_file_id != nil
  260.           all_videos << record.video_file_id
  261.         end
  262.       end
  263.       return build_videos(all_videos, records)
  264.     else
  265.       return records
  266.     end
  267.   end
  268.  
  269.   def self.all_objects(params)
  270.     validate_table_withoud_records
  271.     is_restore_database(params)
  272.     build_collection_of_models(params[:model])
  273.     files = []
  274.     download = CloneDatabase.last
  275.  
  276.     unless download.nil?
  277.       default_models_for_sinchronization
  278.       @all_records.each do |model|
  279.         files = check_if_exists_attached_file(model, files)
  280.         if model.column_names.include?('title_image_id')
  281.           files << build_object(model, download)
  282.           files = get_files(model, files, download)
  283.         elsif model.name == 'Video'
  284.           files << build_object(model, download)
  285.           files = get_videos(model, files, download)
  286.         else
  287.           files << build_object(model, download)
  288.         end
  289.       end
  290.     end
  291.  
  292.     files.compact!
  293.     files
  294.   end
  295.  
  296.   def self.remove_ghost_columns_when_convert_to_json(parse_hash, model)
  297.     columns_name = model.constantize
  298.     columns_name = columns_name.column_names
  299.  
  300.     new_array_hash = []
  301.     #TODO: OTIMIZAR USO DE MEMORIA, GASTO ATUAL: 6440688 BYTES
  302.     parse_hash.each do |value_hash|
  303.       new_hash = {}
  304.       columns_name.each do |column_name|
  305.         new_hash[column_name] = value_hash[column_name]
  306.       end
  307.       new_array_hash << new_hash
  308.     end
  309.     new_array_hash
  310.   end
  311.  
  312.   def self.remove_files(zip_information)
  313.     zip_information.each do |file_name|
  314.       File.delete("#{Rails.root.join('uploads/cache').to_s}/#{file_name[:name]}.json")
  315.     end
  316.   end
  317.  
  318.   def self.build_zip(zip_information, database_tables)
  319.     zip_tempfile = Tempfile.new(['', '.zip'])
  320.     begin
  321.       Zip::OutputStream.open(zip_tempfile.path) do |zip|
  322.         zip_information.each do |file|
  323.           zip.put_next_entry("#{file[:name]}.json")
  324.           array = database_tables
  325.           #TODO pesquisar metodo para leitura gradual
  326.           #TODO: OTIMIZAR USO DE MEMORIA, GARANTIR QUE ESTE FILE.OPEN SERA FECHADO
  327.           File.open("#{Rails.root.join('uploads/cache').to_s}/#{file[:name]}.json", 'r') do |file_read|
  328.             if array.include?(file[:name])
  329.               #TODO: OTIMIZAR USO DE MEMORIA, GASTO ATUAL: 3275653 BYTES
  330.               string_file = file_read.gets
  331.               parse_for_hash = JSON.parse(string_file)
  332.               # this validation is necessary why when hash is converted to json with the function 'to_json'
  333.               # generate keys inexistents 'ghost'
  334.               # That when go atualization the ghost keys not found in database
  335.               new_hash = remove_ghost_columns_when_convert_to_json(parse_for_hash, file[:name])
  336.               zip.print new_hash.to_json
  337.             else
  338.               zip.print file_read.gets
  339.             end
  340.             file_read.close
  341.           end
  342.         end
  343.  
  344.         # XXX: fecha zip stream
  345.         zip.close
  346.       end
  347.  
  348.         # XXX: Nao mais necessario
  349.         # zip_data = File.read(zip_tempfile.path)
  350.     ensure
  351.       # XXX: Nao pode apagar o arquivo temporario
  352.       # zip_tempfile.close
  353.       # apaga os arquivos JSON gerados
  354.       # remove_files(zip_information)
  355.     end
  356.     # XXX: Retorna o arquivo zip temporario
  357.     # zip_data
  358.     return zip_tempfile
  359.   end
  360.  
  361. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement