Class: RxcmsDbmsPlugin::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/rxcms-dbms_plugin/classes/executor.rb

Class Method Summary collapse

Class Method Details

.execute(placeholder, attrs, exts) ⇒ Object

Tag syntax [[<<placeholder>>? &type=“stored-query” &data=“<<data-source>>” &attrs=“<<attr-data>>”…]]



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rxcms-dbms_plugin/classes/executor.rb', line 6

def self.execute(placeholder, attrs, exts)
  begin

    dbms_config = HashWithIndifferentAccess.new(YAML.load(File.read(File.expand_path("../../../../config/dbms/dbms_config.yml", __FILE__))))

    if attrs.has_key?('configs')
      if attrs['configs'].has_key?('type')
        if attrs['configs']['type'] == 'stored-query'

          if (ActiveRecord::Base.connection == Database.connection)
            if (!Metadata.first({ :conditions => ['key = ? and sites_id = ?', dbms_config[:DBMS_CURRENT_HOST], exts[:appid]] }).nil? &&
                !Metadata.first({ :conditions => ['key = ? and sites_id = ?', dbms_config[:DBMS_CURRENT_PORT], exts[:appid]] }).nil? &&
                !Metadata.first({ :conditions => ['key = ? and sites_id = ?', dbms_config[:DBMS_CURRENT_DATABASE], exts[:appid]] }).nil? &&
                !Metadata.first({ :conditions => ['key = ? and sites_id = ?', dbms_config[:DBMS_CURRENT_USER], exts[:appid]] }).nil? &&
                !Metadata.first({ :conditions => ['key = ? and sites_id = ?', dbms_config[:DBMS_CURRENT_PASSWORD], exts[:appid]] }).nil?)

              connectionResult = Database.connect_sql(Metadata.first({ :conditions => ['key = ? and sites_id = ?', dbms_config[:DBMS_CURRENT_ADAPTER], exts[:appid]] }).value,
                                                      Metadata.first({ :conditions => ['key = ? and sites_id = ?', dbms_config[:DBMS_CURRENT_HOST], exts[:appid]] }).value,
                                                      Metadata.first({ :conditions => ['key = ? and sites_id = ?', dbms_config[:DBMS_CURRENT_PORT], exts[:appid]] }).value,
                                                      Metadata.first({ :conditions => ['key = ? and sites_id = ?', dbms_config[:DBMS_CURRENT_USER], exts[:appid]] }).value,
                                                      Metadata.first({ :conditions => ['key = ? and sites_id = ?', dbms_config[:DBMS_CURRENT_PASSWORD], exts[:appid]] }).value,
                                                      Metadata.first({ :conditions => ['key = ? and sites_id = ?', dbms_config[:DBMS_CURRENT_DATABASE], exts[:appid]] }).value)
              if (!connectionResult)
                return "{{[alliance_dbms - #{placeholder.key}] Unable to establish connection to database}}"
              end
            else
              return "{{[alliance_dbms - #{placeholder.key}] Missing important keys to establish connection}}"
            end
          end

          if attrs['configs'].has_key?('data')
            tQueryItem = Metadata.all({ :conditions => ['key = ? and sites_id = ?', attrs['configs']['data'], exts[:appid]] })
            languageSwitchable = Metadata.first({ :conditions => ['key = ? and sites_id = ?', 'autoLanguageSwitch', exts[:appid]] })
            queryItem = nil

            tQueryItem.each do |query|
              tQueryItemJsonObject = ActiveSupport::JSON.decode(SymmetricEncryption.decrypt(query.value))
              tQueryItemJsonObjectLanguage = tQueryItemJsonObject['language'].scan(/^[a-z]{2}/).first

              if (!languageSwitchable.nil?)
                if (languageSwitchable.value == 'yes')
                  if (exts[:language] == tQueryItemJsonObjectLanguage)
                    queryItem = query
                    break
                  end
                elsif (languageSwitchable.value == 'no')
                  if ('no' == tQueryItemJsonObjectLanguage)
                    queryItem = query
                    break
                  end
                else
                  return "{{alliance-dbms - #{placeholder.key} the \"value\" for language switchability is invalid}}"
                end
              else
                return "{{alliance-dbms - #{placeholder.key} Language switchability is not available}}"
              end
            end

            if (!queryItem.nil?)

              queryItemJsonObject = ActiveSupport::JSON.decode(SymmetricEncryption.decrypt(queryItem.value))
              queryItemJsonData = Database.connection.select_all(queryItemJsonObject['sql'])

              if (attrs['configs'].has_key?('tple') && attrs['configs'].has_key?('tplo'))
                # if it is alternate template
                contentResult = ''

                queryItemEvenTemplate = Metadata.find_by_key(attrs['configs']['tple'])
                queryItemOddTemplate = Metadata.find_by_key(attrs['configs']['tplo'])

                if (!queryItemEvenTemplate.nil? && !queryItemOddTemplate.nil?)
                  tplCount = 0
                  queryItemJsonData.each do |q|
                    itemTemplate = ''

                    if (tplCount % 2 == 0)
                      itemTemplate = queryItemEvenTemplate.value.strip
                    else
                      itemTemplate = queryItemOddTemplate.value.strip
                    end
                    parsedObjs = itemTemplate.scan(/\[\[\$[a-zA-Z\-_]+\]\]/)

                    parsedObjs.each do |p|
                      tpObj = p.gsub(/[^a-zA-Z\-_]/, '')

                      if (!q[tpObj].nil?)
                        itemTemplate = itemTemplate.gsub(p, q[tpObj].to_s.strip)
                      else
                        itemTemplate = itemTemplate.gsub(p, '')
                      end
                    end

                    contentResult << itemTemplate
                    tplCount = tplCount + 1
                  end
                end

                # Content should be empty if even and odd template don't exist
                contentResult
              elsif (attrs['configs'].keys.grep(/^tpl[0-9]+$/).size > 0)
                # if it is multiple template
                "{{[alliance-dbms - #{placeholder.key}] multiple template is not supported}}"
              else
                # if it is single template
                contentResult = ''

                queryItemJsonData.each do |q|
                  queryItemTemplate = placeholder.value.strip
                  parsedObjs = queryItemTemplate.scan(/\[\[\$[a-zA-Z\-_]+\]\]/)

                  parsedObjs.each do |p|
                    tpObj = p.gsub(/[^a-zA-Z\-_]/, '')

                    if (!q[tpObj].nil?)
                      queryItemTemplate = queryItemTemplate.gsub(p, q[tpObj].to_s.strip)
                    else
                      queryItemTemplate = queryItemTemplate.gsub(p, '')
                    end
                  end

                  contentResult << queryItemTemplate
                end

                # Content should be empty if db returns no data
                contentResult
              end
            else
              placeholder.value.strip.html_safe
            end
          else
            placeholder.value.strip.html_safe
          end
        else
          placeholder.value.strip.html_safe
        end
      else
        placeholder.value.strip.html_safe
      end
    else
      placeholder.value.strip.html_safe
    end
  rescue Exception => ex
    "{{[alliance-dbms - #{placeholder.key}] #{ex.message}}}"
  end
end