Class: OTRS::ConfigItem
- Defined in:
- lib/otrs_connector/otrs/config_item.rb
Constant Summary collapse
- @@builtin_fields =
Field Names that are part of all ConfigItem objects, not stored in XMLData table
[: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
-
.find(id) ⇒ Object
Find by ConfigItemID.
-
.find_version(id) ⇒ Object
Find by Version ID.
-
.from_otrs_xml(xml) ⇒ Object
Convert OTRS XMLData structure to our object structure.
-
.object_preprocessor(object) ⇒ Object
Custom object processor because of XMLData.
- .set_accessor(key) ⇒ Object
-
.to_otrs_xml(attributes) ⇒ Object
Convert non-builtin fields to OTRS’s XMLData structure.
- .where(attributes) ⇒ Object
-
.xml_search(attributes) ⇒ Object
Converts search hash for search itmes that ar enot the in @@builtin_fields to OTRS XMLData searches.
Instance Method Summary collapse
- #create ⇒ Object
-
#get_history ⇒ Object
Get history of CI object, returns as CI’s…
-
#initialize(attributes = {}) ⇒ ConfigItem
constructor
A new instance of ConfigItem.
- #persisted? ⇒ Boolean
- #save ⇒ Object
- #update_attributes(updated_attributes) ⇒ Object
Methods inherited from OTRS
api_url, api_url=, #attributes, #connect, connect, get_from_remote, password, password=, process_response, setup_connection_params, user, user=
Constructor Details
#initialize(attributes = {}) ⇒ ConfigItem
Returns a new instance of ConfigItem.
23 24 25 26 27 28 29 30 31 |
# File 'lib/otrs_connector/otrs/config_item.rb', line 23 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
Find by ConfigItemID
78 79 80 81 82 |
# File 'lib/otrs_connector/otrs/config_item.rb', line 78 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
Find by Version ID
85 86 87 88 89 |
# File 'lib/otrs_connector/otrs/config_item.rb', line 85 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
Convert OTRS XMLData structure to our object structure
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 237 238 239 240 241 |
# File 'lib/otrs_connector/otrs/config_item.rb', line 199 def self.from_otrs_xml(xml) # OTRS Allows multiples of the same field name. To handle this, and to make sure we pull all the fields these fields are being handled specially. Fields with __keyname__count are these fields 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
Custom object processor because of XMLData
68 69 70 71 72 73 74 75 |
# File 'lib/otrs_connector/otrs/config_item.rb', line 68 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
15 16 17 |
# File 'lib/otrs_connector/otrs/config_item.rb', line 15 def self.set_accessor(key) attr_accessor key.to_sym end |
.to_otrs_xml(attributes) ⇒ Object
Convert non-builtin fields to OTRS’s XMLData structure
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 165 166 |
# File 'lib/otrs_connector/otrs/config_item.rb', line 125 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| # In some cases we created special field names because there were multiple fields with the same name. Fields with the "__" are these special fields and need to be handled specially 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
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/otrs_connector/otrs/config_item.rb', line 91 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
Converts search hash for search itmes that ar enot the in @@builtin_fields to OTRS XMLData searches
57 58 59 60 61 62 63 64 65 |
# File 'lib/otrs_connector/otrs/config_item.rb', line 57 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
#create ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/otrs_connector/otrs/config_item.rb', line 37 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_history ⇒ Object
Get history of CI object, returns as CI’s… may want to create a new class, subclassed from this one called ConfigItemHistoryEntry, or some such, but this works for now.
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/otrs_connector/otrs/config_item.rb', line 112 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
19 20 21 |
# File 'lib/otrs_connector/otrs/config_item.rb', line 19 def persisted? false end |
#save ⇒ Object
33 34 35 |
# File 'lib/otrs_connector/otrs/config_item.rb', line 33 def save self.create end |
#update_attributes(updated_attributes) ⇒ Object
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 194 195 |
# File 'lib/otrs_connector/otrs/config_item.rb', line 168 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 |