Class: OTRS::ConfigItem

Inherits:
OTRS
  • Object
show all
Defined in:
lib/otrs_connector/otrs/config_item.rb

Direct Known Subclasses

Definition

Defined Under Namespace

Classes: Definition, DeploymentState, IncidentState

Constant Summary collapse

@@builtin_fields =
[:Name,:DeplStateID,:InciStateID,:DefinitionID,
:CreateTime,:ChangeBy,:ChangeTime,:Class,:ClassID,:ConfigItemID,:CreateBy,:CreateTime,
:CurDeplState,:CurDeplStateID,:CurDeplStateType,:CurInciState,:CurInciStateID,:CurInciStateType,
:DeplState,:DeplStateType,:InciState,:InciStateType,:LastVersionID,:Number,:VersionID,:OrderBy,:Limit,:ClassIDs, :InciStateIDs, :DeplStateIDs]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OTRS

api_url, api_url=, #connect, connect, password, password=, user, user=

Constructor Details

#initialize(attributes = {}) ⇒ ConfigItem

Returns a new instance of ConfigItem.



21
22
23
24
25
26
27
28
29
# File 'lib/otrs_connector/otrs/config_item.rb', line 21

def initialize(attributes = {})
  attributes.each do |name, value|
    # cannot have numbers at beginning of field name
    unless name =~ /^\d+/ or name =~ / / or name =~ /-/
      self.class.set_accessor(name)
      send("#{name.to_sym}=", value)
    end
  end
end

Class Method Details

.find(id) ⇒ Object



81
82
83
84
85
# File 'lib/otrs_connector/otrs/config_item.rb', line 81

def self.find(id)
  data = { 'ConfigItemID' => id, 'XMLDataGet' => 1 }
  params = { :object => 'ConfigItemObjectCustom', :method => 'VersionGet', :data => data }
  self.object_preprocessor (connect(params).first)
end

.find_version(id) ⇒ Object



87
88
89
90
91
# File 'lib/otrs_connector/otrs/config_item.rb', line 87

def self.find_version(id)
  data = { 'VersionID' => id, 'XMLDataGet' => 1 }
  params = { :object => 'ConfigItemObject', :method => 'VersionGet', :data => data }
  return self.object_preprocessor (connect(params).first)
end

.from_otrs_xml(xml) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/otrs_connector/otrs/config_item.rb', line 195

def self.from_otrs_xml(xml)
  xml = xml[1].flatten[1][1].except("TagKey")
  data = {}
  xml.each do |key,value|
    xml[key].delete(xml[key][0])
    count = xml[key].count
    if count == 1
      data[key] = value[count - 1]["Content"]
      count2 = value[count -1].except("Content","TagKey").count
      if count2 >= 1
        value[count - 1].except("Content","TagKey").each do |key2,value2|
          value2.delete(value2[0])
          data["#{key}__#{key2}"] = value2[0]["Content"]
        end
      end
    else
      while count != 0
        data["#{key}__0#{count - 1}"] = value[count - 1]["Content"]
        count3 = value[count - 1].except("TagKey").count
        if count3 > 1
          value[count - 1].except("Content","TagKey").each do |key3,value3|
            value3.delete(value3[0])
            count4 = value3.count
            if count4 > 1
              while count4 != 0
                unless value3[count4 - 1]["Content"].nil?
                  data["#{key}__0#{count - 1}__#{key3}__0#{count4 - 1}"] = value3[count4 - 1]["Content"]
                end
                count4 = count4 - 1
              end
              
            else
              data["#{key}__0#{count - 1}__#{key3}"] = value3[0]["Content"]
            end
          end
        end
        count = count - 1
      end
    end
  end
  data
end

.object_preprocessor(object) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/otrs_connector/otrs/config_item.rb', line 72

def self.object_preprocessor(object)
  unless object.nil? or object.empty?
    xml = self.from_otrs_xml(object['XMLData'])
    self.new(object.except('XMLData', 'XMLDefinition').merge(xml))
  else
    nil
  end
end

.set_accessor(key) ⇒ Object



13
14
15
# File 'lib/otrs_connector/otrs/config_item.rb', line 13

def self.set_accessor(key)
  attr_accessor key.to_sym
end

.to_otrs_xml(attributes) ⇒ Object



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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/otrs_connector/otrs/config_item.rb', line 124

def self.to_otrs_xml(attributes)
  xml = attributes.except(:Name,:DeplStateID,:InciStateID,:DefinitionID,
    :CreateTime,:ChangeBy,:ChangeTime,:Class,:ClassID,:ConfigItemID,:CreateBy,:CreateTime,
    :CurDeplState,:CurDeplStateID,:CurDeplStateType,:CurInciState,:CurInciStateID,:CurInciStateType,
    :DeplState,:DeplStateType,:InciState,:InciStateType,:LastVersionID,:Number,:VersionID, :service, :Service)
  xml_hash = {}
  xml_data = [nil, { 'Version' => xml_hash }]
  tmp = []
  xml.each do |key,value|
    key = key.to_s
    tmp << key
  end
  # Order keys properly so they are parsed in the correct order
  tmp.sort! { |x,y| x <=> y }
  tmp.each do |key|
    keys = key.split(/__/)
    xml_key = keys[0]
    unless keys[1].nil? then tag_key = keys[1].gsub(/^0/,'').to_i + 1 end
    xml_subkey = keys[2]
    case key
    when /^[aA-zZ]+__0\d+__[aA-zZ]+__0\d+$/
      if xml_hash[xml_key][tag_key][xml_subkey].nil?
        xml_hash[xml_key][tag_key][xml_subkey] = [nil, { "Content" => xml[key.to_sym] }]
      else
        xml_hash[xml_key][tag_key][xml_subkey] << { "Content" => xml[key.to_sym] }
      end
    when /^[aA-zZ]+__0\d+__[aA-zZ]$/
      xml_hash[xml_key][tag_key][xml_subkey] = xml[key.to_sym]
    when /^[aA-zZ]+__0\d+$/
      if xml_hash[xml_key].nil?
        xml_hash[xml_key] = [nil] 
      end
      xml_hash[xml_key] << { "Content" => xml[key.to_sym] }
    when /^[aA-zZ]+__[aA-zZ]$/
      xml_hash[xml_key][1][xml_subkey] = xml[key.to_sym]
    when /^[aA-zZ]+$/
      xml_hash[xml_key] = [ nil, { "Content" => xml[key.to_sym] }]
    end
  end
  xml_data
end

.where(attributes) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/otrs_connector/otrs/config_item.rb', line 93

def self.where(attributes)
  tmp = {}
  tmp['What'] = self.xml_search(attributes)
  attributes.each do |key,value|
    if @@builtin_fields.include? key
      tmp[key.to_s.camelize.to_sym] = value
    end
  end
  data = tmp
  params = { :object => 'ConfigItemObjectCustom', :method => 'ConfigItemSearchExtended', :data => data }
  a = connect(params)
  results = self.superclass::Relation.new
  a.each do |b|
    b.each do |c|
      results << self.object_preprocessor(c)
    end
  end
  results
end

.xml_search(attributes) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/otrs_connector/otrs/config_item.rb', line 62

def self.xml_search(attributes)
  what = []
  attributes.each do |key,value|
    unless @@builtin_fields.include? key
      what << {"[%]{'Version'}[%]{'#{key}'}[%]{'Content'}" => value }
    end
  end
  what
end

Instance Method Details

#attributesObject



31
32
33
34
35
36
37
# File 'lib/otrs_connector/otrs/config_item.rb', line 31

def attributes
  attributes = {}
  self.instance_variables.each do |v|
    attributes[v.to_s.gsub('@','').to_sym] = self.instance_variable_get(v)
  end
  attributes
end

#createObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/otrs_connector/otrs/config_item.rb', line 43

def create
  attributes = self.attributes
  data = { 'ClassID' => self.ClassID, 'UserID' => 1 }
  params = { :object => 'ConfigItemObject', :method => 'ConfigItemAdd', :data => data }
  a = self.class.connect(params)
  attributes[:ConfigItemID] = a.first
  attributes[:XMLData] = self.class.to_otrs_xml(attributes)
  data2 = attributes
  params2 = { :object => 'ConfigItemObject', :method => 'VersionAdd', :data => data2 }
  b = self.class.connect(params2)
  new_version_id = b.first
  config_item = self.class.find(attributes[:ConfigItemID])
  attributes = config_item.attributes
  attributes.each do |key,value|
    instance_variable_set "@#{key.to_s}", value
  end
  config_item
end

#get_historyObject



113
114
115
116
117
118
119
120
121
122
# File 'lib/otrs_connector/otrs/config_item.rb', line 113

def get_history
  data = { :ConfigItemID => self.id, 'XMLDataGet' => 1 }
  params = { :object => 'ConfigItemObjectCustom', :method => 'VersionList', :data => data }
  a = OTRS.connect(params).flatten
  b = self.class.superclass::Relation.new
  a.each do |c|
    b << self.class.object_preprocessor(c)
  end
  return b
end

#persisted?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/otrs_connector/otrs/config_item.rb', line 17

def persisted?
  false
end

#saveObject



39
40
41
# File 'lib/otrs_connector/otrs/config_item.rb', line 39

def save
  self.create
end

#update_attributes(updated_attributes) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/otrs_connector/otrs/config_item.rb', line 166

def update_attributes(updated_attributes)
  self.attributes.each do |key,value|
    if updated_attributes[key].nil?
      updated_attributes[key] = value
    end
  end
  updated_attributes[:XMLData] = self.class.to_otrs_xml(updated_attributes)
  xml_attributes = self.attributes.except(:Name,:DeplStateID,:InciStateID,:DefinitionID,
    :CreateTime,:ChangeBy,:ChangeTime,:Class,:ClassID,:ConfigItemID,:CreateBy,:CreateTime,
    :CurDeplState,:CurDeplStateID,:CurDeplStateType,:CurInciState,:CurInciStateID,:CurInciStateType,
    :DeplState,:DeplStateType,:InciState,:InciStateType,:LastVersionID,:Number,:VersionID)
  xml_attributes.each do |key,value|
    updated_attributes = updated_attributes.except(key)
  end
  data = updated_attributes
  params = { :object => 'ConfigItemObject', :method => 'VersionAdd', :data => data }
  a = self.class.connect(params)
  new_version_id = a.first
  data2 = { 'VersionID' => new_version_id }
  params2 = { :object => 'ConfigItemObject', :method => 'VersionConfigItemIDGet', :data => data2 }
  b = self.class.connect(params2)
  config_item = self.class.find(b.first)
  attributes = config_item.attributes
  attributes.each do |key,value|
    instance_variable_set "@#{key.to_s}", value
  end
  config_item
end