| Line | |
|---|
| 1 | require 'model_loader' |
|---|
| 2 | require 'model_writer' |
|---|
| 3 | require 'ostruct' |
|---|
| 4 | |
|---|
| 5 | class RepositoryController < ApplicationController |
|---|
| 6 | layout 'admin' |
|---|
| 7 | |
|---|
| 8 | def select_repo |
|---|
| 9 | if @request.post? |
|---|
| 10 | unless @params['repo']['repository'].blank? |
|---|
| 11 | reset_session |
|---|
| 12 | cookies.delete "breadcrumb" |
|---|
| 13 | @session['repo'] = @params['repo'] |
|---|
| 14 | begin |
|---|
| 15 | set_repo |
|---|
| 16 | redirect_to :controller => 'nav_class', :action => 'list' and return |
|---|
| 17 | rescue => ex |
|---|
| 18 | flash['notice'] = 'Invalid username/password/repository combination.' |
|---|
| 19 | end |
|---|
| 20 | end |
|---|
| 21 | end |
|---|
| 22 | @repos = SemanticRecord::Base::connection.list_repositories |
|---|
| 23 | end |
|---|
| 24 | |
|---|
| 25 | def import |
|---|
| 26 | @db = OpenStruct.new(@params['db']) |
|---|
| 27 | if @request.post? |
|---|
| 28 | begin |
|---|
| 29 | if @db.script.blank? |
|---|
| 30 | modelo = YAML.load(@db.file) |
|---|
| 31 | else |
|---|
| 32 | modelo = YAML.load(@db.script) |
|---|
| 33 | end |
|---|
| 34 | rescue => ex |
|---|
| 35 | flash['notice'] = "Invalid database script, not valid YAML: #{ex}" |
|---|
| 36 | return |
|---|
| 37 | end |
|---|
| 38 | |
|---|
| 39 | begin |
|---|
| 40 | ModelObserver.suspend |
|---|
| 41 | SemanticRecord::Base.connection.clear_repository if @db.clear == '1' |
|---|
| 42 | ModelLoader.new(modelo).load |
|---|
| 43 | ModelObserver.resume |
|---|
| 44 | rescue => ex |
|---|
| 45 | ModelObserver.resume |
|---|
| 46 | flash['notice'] = "Error while importing script: #{ex}. |
|---|
| 47 | This might have left the repository in an inconsistent state." |
|---|
| 48 | return |
|---|
| 49 | end |
|---|
| 50 | |
|---|
| 51 | flash['notice'] = "Script imported succesfully!" |
|---|
| 52 | end |
|---|
| 53 | end |
|---|
| 54 | |
|---|
| 55 | def export |
|---|
| 56 | |
|---|
| 57 | if request.post? |
|---|
| 58 | begin |
|---|
| 59 | @file_name = "#{SemanticRecord::Base.repository['repository']}-#{Time.now.strftime("%Y-%m-%dT%H%M%S")}.tmp.yml" |
|---|
| 60 | path = "#{RAILS_ROOT}/public/#{@file_name}" |
|---|
| 61 | ModelWriter.new(path).write |
|---|
| 62 | rescue => ex |
|---|
| 63 | flash['notice'] = "Error while exporting repository: #{ex}." |
|---|
| 64 | return |
|---|
| 65 | end |
|---|
| 66 | |
|---|
| 67 | flash['notice'] = "Repository exported succesfully. Please download the YAML script below." |
|---|
| 68 | end |
|---|
| 69 | |
|---|
| 70 | end |
|---|
| 71 | end |
|---|