otrs.rb 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. module Import
  2. end
  3. module Import::OTRS
  4. def self.request(part)
  5. url = Setting.get('import_otrs_endpoint') + '/' + part + ';Key=' + Setting.get('import_otrs_endpoint_key')
  6. puts 'GET: ' + url
  7. response = UserAgent.request(
  8. url,
  9. {
  10. :user => Setting.get('import_otrs_user'),
  11. :password => Setting.get('import_otrs_password'),
  12. },
  13. )
  14. if !response.success?
  15. puts "ERROR: #{response.error}"
  16. return
  17. end
  18. return response
  19. end
  20. def self.post(base, data)
  21. url = Setting.get('import_otrs_endpoint') + '/' + base
  22. data['Key'] = Setting.get('import_otrs_endpoint_key')
  23. puts 'POST: ' + url
  24. response = UserAgent.request(
  25. url,
  26. {
  27. :method => 'post',
  28. :user => Setting.get('import_otrs_user'),
  29. :password => Setting.get('import_otrs_password'),
  30. },
  31. )
  32. if !response.success?
  33. puts "ERROR: #{response.error}"
  34. return
  35. end
  36. return response
  37. end
  38. def self.json(response)
  39. data = Encode.conv( 'utf8', response.body.to_s )
  40. JSON.parse( data )
  41. end
  42. def self.auth(username, password)
  43. response = post( "public.pl", { :Action => 'Export', :Type => 'Auth', :User => username, :Pw => password } )
  44. return if !response
  45. return if !response.success?
  46. result = json(response)
  47. return result
  48. end
  49. def self.session(session_id)
  50. response = post( "public.pl", { :Action => 'Export', :Type => 'SessionCheck', :SessionID => session_id } )
  51. return if !response
  52. return if !response.success?
  53. result = json(response)
  54. return result
  55. end
  56. def self.start
  57. puts 'Start import...'
  58. # # set system in import mode
  59. # Setting.set('import_mode', true)
  60. # check if system is in import mode
  61. if !Setting.get('import_mode')
  62. raise "System is not in import mode!"
  63. end
  64. response = request("public.pl?Action=Export")
  65. return if !response
  66. return if !response.success?
  67. #self.ticket('156115')
  68. #return
  69. # create states
  70. ticket_state
  71. # create priorities
  72. ticket_priority
  73. # create groups
  74. ticket_group
  75. # create agents
  76. user
  77. # create customers
  78. # customer
  79. result = JSON.parse( response.body )
  80. result = result.reverse
  81. Thread.abort_on_exception = true
  82. thread_count = 4
  83. threads = {}
  84. (1..thread_count).each {|thread|
  85. threads[thread] = Thread.new {
  86. sleep thread * 3
  87. puts "Started import thread# #{thread} ..."
  88. run = true
  89. while run
  90. ticket_ids = result.pop(20)
  91. if !ticket_ids.empty?
  92. self.ticket(ticket_ids)
  93. else
  94. puts "... thread# #{thread}, no more work."
  95. run = false
  96. end
  97. end
  98. }
  99. }
  100. (1..thread_count).each {|thread|
  101. threads[thread].join
  102. }
  103. return
  104. end
  105. def self.diff_worker
  106. return if !Setting.get('import_mode')
  107. return if Setting.get('import_otrs_endpoint') == 'http://otrs_host/otrs'
  108. self.diff
  109. end
  110. def self.diff
  111. puts 'Start diff...'
  112. # check if system is in import mode
  113. if !Setting.get('import_mode')
  114. raise "System is not in import mode!"
  115. end
  116. # create states
  117. ticket_state
  118. # create priorities
  119. ticket_priority
  120. # create groups
  121. ticket_group
  122. # create agents
  123. user
  124. self.ticket_diff()
  125. return
  126. end
  127. def self.ticket_diff()
  128. url = "public.pl?Action=Export;Type=TicketDiff;Limit=30"
  129. response = request( url )
  130. return if !response
  131. return if !response.success?
  132. result = json(response)
  133. self._ticket_result(result)
  134. end
  135. def self.ticket(ticket_ids)
  136. url = "public.pl?Action=Export;Type=Ticket;"
  137. ticket_ids.each {|ticket_id|
  138. url = url + "TicketID=#{CGI::escape ticket_id};"
  139. }
  140. response = request( url )
  141. return if !response
  142. return if !response.success?
  143. result = json(response)
  144. self._ticket_result(result)
  145. end
  146. def self._ticket_result(result)
  147. # puts result.inspect
  148. map = {
  149. :Ticket => {
  150. :Changed => :updated_at,
  151. :Created => :created_at,
  152. :CreateBy => :created_by_id,
  153. :TicketNumber => :number,
  154. :QueueID => :group_id,
  155. :StateID => :ticket_state_id,
  156. :PriorityID => :ticket_priority_id,
  157. :Owner => :owner,
  158. :CustomerUserID => :customer,
  159. :Title => :title,
  160. :TicketID => :id,
  161. :FirstResponse => :first_response,
  162. # :FirstResponseTimeDestinationDate => :first_response_escal_date,
  163. # :FirstResponseInMin => :first_response_in_min,
  164. # :FirstResponseDiffInMin => :first_response_diff_in_min,
  165. :Closed => :close_time,
  166. # :SoltutionTimeDestinationDate => :close_time_escal_date,
  167. # :CloseTimeInMin => :close_time_in_min,
  168. # :CloseTimeDiffInMin => :close_time_diff_in_min,
  169. },
  170. :Article => {
  171. :SenderType => :ticket_article_sender,
  172. :ArticleType => :ticket_article_type,
  173. :TicketID => :ticket_id,
  174. :ArticleID => :id,
  175. :Body => :body,
  176. :From => :from,
  177. :To => :to,
  178. :Cc => :cc,
  179. :Subject => :subject,
  180. :InReplyTo => :in_reply_to,
  181. :MessageID => :message_id,
  182. # :ReplyTo => :reply_to,
  183. :References => :references,
  184. :Changed => :updated_at,
  185. :Created => :created_at,
  186. :ChangedBy => :updated_by_id,
  187. :CreatedBy => :created_by_id,
  188. },
  189. }
  190. result.each {|record|
  191. # use transaction
  192. ActiveRecord::Base.transaction do
  193. ticket_new = {
  194. :title => '',
  195. :created_by_id => 1,
  196. :updated_by_id => 1,
  197. }
  198. map[:Ticket].each { |key,value|
  199. if record['Ticket'][key.to_s] && record['Ticket'][key.to_s].class == String
  200. ticket_new[value] = Encode.conv( 'utf8', record['Ticket'][key.to_s] )
  201. else
  202. ticket_new[value] = record['Ticket'][key.to_s]
  203. end
  204. }
  205. # puts key.to_s
  206. # puts value.to_s
  207. #puts 'new ticket data ' + ticket_new.inspect
  208. # check if state already exists
  209. ticket_old = Ticket.where( :id => ticket_new[:id] ).first
  210. #puts 'TICKET OLD ' + ticket_old.inspect
  211. # find user
  212. if ticket_new[:owner]
  213. user = User.lookup( :login => ticket_new[:owner] )
  214. if user
  215. ticket_new[:owner_id] = user.id
  216. else
  217. ticket_new[:owner_id] = 1
  218. end
  219. ticket_new.delete(:owner)
  220. end
  221. if ticket_new[:customer]
  222. user = User.lookup( :login => ticket_new[:customer] )
  223. if user
  224. ticket_new[:customer_id] = user.id
  225. else
  226. ticket_new[:customer_id] = 1
  227. end
  228. ticket_new.delete(:customer)
  229. else
  230. ticket_new[:customer_id] = 1
  231. end
  232. # puts 'ttt' + ticket_new.inspect
  233. # set state types
  234. if ticket_old
  235. puts "update Ticket.find(#{ticket_new[:id]})"
  236. ticket_old.update_attributes(ticket_new)
  237. else
  238. puts "add Ticket.find(#{ticket_new[:id]})"
  239. ticket = Ticket.new(ticket_new)
  240. ticket.id = ticket_new[:id]
  241. ticket.save
  242. end
  243. record['Articles'].each { |article|
  244. # get article values
  245. article_new = {
  246. :created_by_id => 1,
  247. :updated_by_id => 1,
  248. }
  249. map[:Article].each { |key,value|
  250. if article[key.to_s]
  251. article_new[value] = Encode.conv( 'utf8', article[key.to_s] )
  252. end
  253. }
  254. # create customer/sender if needed
  255. if article_new[:ticket_article_sender] == 'customer' && article_new[:created_by_id].to_i == 1 && !article_new[:from].empty?
  256. # set extra headers
  257. begin
  258. email = Mail::Address.new( article_new[:from] ).address
  259. rescue
  260. email = article_new[:from]
  261. if article_new[:from] =~ /<(.+?)>/
  262. email = $1
  263. end
  264. end
  265. user = User.where( :email => email ).first
  266. if !user
  267. user = User.where( :login => email ).first
  268. end
  269. if !user
  270. begin
  271. display_name = Mail::Address.new( article_new[:from] ).display_name ||
  272. ( Mail::Address.new( article_new[:from] ).comments && Mail::Address.new( article_new[:from] ).comments[0] )
  273. rescue
  274. display_name = article_new[:from]
  275. end
  276. # do extra decoding because we needed to use field.value
  277. display_name = Mail::Field.new( 'X-From', display_name ).to_s
  278. roles = Role.lookup( :name => 'Customer' )
  279. user = User.create(
  280. :login => email,
  281. :firstname => display_name,
  282. :lastname => '',
  283. :email => email,
  284. :password => '',
  285. :active => true,
  286. :role_ids => [roles.id],
  287. :updated_by_id => 1,
  288. :created_by_id => 1,
  289. )
  290. end
  291. article_new[:created_by_id] = user.id
  292. end
  293. if article_new[:ticket_article_sender] == 'customer'
  294. article_new[:ticket_article_sender_id] = Ticket::Article::Sender.lookup( :name => 'Customer' ).id
  295. article_new.delete( :ticket_article_sender )
  296. end
  297. if article_new[:ticket_article_sender] == 'agent'
  298. article_new[:ticket_article_sender_id] = Ticket::Article::Sender.lookup( :name => 'Agent' ).id
  299. article_new.delete( :ticket_article_sender )
  300. end
  301. if article_new[:ticket_article_sender] == 'system'
  302. article_new[:ticket_article_sender_id] = Ticket::Article::Sender.lookup( :name => 'System' ).id
  303. article_new.delete( :ticket_article_sender )
  304. end
  305. if article_new[:ticket_article_type] == 'email-external'
  306. article_new[:ticket_article_type_id] = Ticket::Article::Type.lookup( :name => 'email' ).id
  307. article_new[:internal] = false
  308. elsif article_new[:ticket_article_type] == 'email-internal'
  309. article_new[:ticket_article_type_id] = Ticket::Article::Type.lookup( :name => 'email' ).id
  310. article_new[:internal] = true
  311. elsif article_new[:ticket_article_type] == 'note-external'
  312. article_new[:ticket_article_type_id] = Ticket::Article::Type.lookup( :name => 'note' ).id
  313. article_new[:internal] = false
  314. elsif article_new[:ticket_article_type] == 'note-internal'
  315. article_new[:ticket_article_type_id] = Ticket::Article::Type.lookup( :name => 'note' ).id
  316. article_new[:internal] = true
  317. elsif article_new[:ticket_article_type] == 'phone'
  318. article_new[:ticket_article_type_id] = Ticket::Article::Type.lookup( :name => 'phone' ).id
  319. article_new[:internal] = false
  320. elsif article_new[:ticket_article_type] == 'webrequest'
  321. article_new[:ticket_article_type_id] = Ticket::Article::Type.lookup( :name => 'web' ).id
  322. article_new[:internal] = false
  323. else
  324. article_new[:ticket_article_type_id] = 9
  325. end
  326. article_new.delete( :ticket_article_type )
  327. article_old = Ticket::Article.where( :id => article_new[:id] ).first
  328. #puts 'ARTICLE OLD ' + article_old.inspect
  329. # set state types
  330. if article_old
  331. puts "update Ticket::Article.find(#{article_new[:id]})"
  332. # puts article_new.inspect
  333. article_old.update_attributes(article_new)
  334. else
  335. puts "add Ticket::Article.find(#{article_new[:id]})"
  336. article = Ticket::Article.new(article_new)
  337. article.id = article_new[:id]
  338. article.save
  339. end
  340. }
  341. record['History'].each { |history|
  342. # puts '-------'
  343. # puts history.inspect
  344. if history['HistoryType'] == 'NewTicket'
  345. History.add(
  346. :id => history['HistoryID'],
  347. :o_id => history['TicketID'],
  348. :history_type => 'created',
  349. :history_object => 'Ticket',
  350. :created_at => history['CreateTime'],
  351. :created_by_id => history['CreateBy']
  352. )
  353. end
  354. if history['HistoryType'] == 'StateUpdate'
  355. data = history['Name']
  356. # "%%new%%open%%"
  357. from = nil
  358. to = nil
  359. if data =~ /%%(.+?)%%(.+?)%%/
  360. from = $1
  361. to = $2
  362. state_from = Ticket::State.lookup( :name => from )
  363. state_to = Ticket::State.lookup( :name => to )
  364. if state_from
  365. from_id = state_from.id
  366. end
  367. if state_to
  368. to_id = state_to.id
  369. end
  370. end
  371. # puts "STATE UPDATE (#{history['HistoryID']}): -> #{from}->#{to}"
  372. History.add(
  373. :id => history['HistoryID'],
  374. :o_id => history['TicketID'],
  375. :history_type => 'updated',
  376. :history_object => 'Ticket',
  377. :history_attribute => 'ticket_state',
  378. :value_from => from,
  379. :id_from => from_id,
  380. :value_to => to,
  381. :id_to => to_id,
  382. :created_at => history['CreateTime'],
  383. :created_by_id => history['CreateBy']
  384. )
  385. end
  386. if history['HistoryType'] == 'Move'
  387. data = history['Name']
  388. # "%%Queue1%%5%%Postmaster%%1"
  389. from = nil
  390. to = nil
  391. if data =~ /%%(.+?)%%(.+?)%%(.+?)%%(.+?)$/
  392. from = $1
  393. from_id = $2
  394. to = $3
  395. to_id = $4
  396. end
  397. History.add(
  398. :id => history['HistoryID'],
  399. :o_id => history['TicketID'],
  400. :history_type => 'updated',
  401. :history_object => 'Ticket',
  402. :history_attribute => 'group',
  403. :value_from => from,
  404. :value_to => to,
  405. :id_from => from_id,
  406. :id_to => to_id,
  407. :created_at => history['CreateTime'],
  408. :created_by_id => history['CreateBy']
  409. )
  410. end
  411. if history['HistoryType'] == 'PriorityUpdate'
  412. data = history['Name']
  413. # "%%3 normal%%3%%5 very high%%5"
  414. from = nil
  415. to = nil
  416. if data =~ /%%(.+?)%%(.+?)%%(.+?)%%(.+?)$/
  417. from = $1
  418. from_id = $2
  419. to = $3
  420. to_id = $4
  421. end
  422. History.add(
  423. :id => history['HistoryID'],
  424. :o_id => history['TicketID'],
  425. :history_type => 'updated',
  426. :history_object => 'Ticket',
  427. :history_attribute => 'ticket_priority',
  428. :value_from => from,
  429. :value_to => to,
  430. :id_from => from_id,
  431. :id_to => to_id,
  432. :created_at => history['CreateTime'],
  433. :created_by_id => history['CreateBy']
  434. )
  435. end
  436. if history['ArticleID'] && history['ArticleID'] != 0
  437. History.add(
  438. :id => history['HistoryID'],
  439. :o_id => history['ArticleID'],
  440. :history_type => 'created',
  441. :history_object => 'Ticket::Article',
  442. :related_o_id => history['TicketID'],
  443. :related_history_object => 'Ticket',
  444. :created_at => history['CreateTime'],
  445. :created_by_id => history['CreateBy']
  446. )
  447. end
  448. }
  449. end
  450. }
  451. end
  452. def self.ticket_state
  453. response = request( "public.pl?Action=Export;Type=State" )
  454. return if !response
  455. return if !response.success?
  456. result = json(response)
  457. # puts result.inspect
  458. map = {
  459. :ChangeTime => :updated_at,
  460. :CreateTime => :created_at,
  461. :CreateBy => :created_by_id,
  462. :ChangeBy => :updated_by_id,
  463. :Name => :name,
  464. :ID => :id,
  465. :ValidID => :active,
  466. :Comment => :note,
  467. };
  468. Ticket::State.all.each {|state|
  469. state.name = state.name + '_tmp'
  470. state.save
  471. }
  472. result.each { |state|
  473. _set_valid(state)
  474. # get new attributes
  475. state_new = {
  476. :created_by_id => 1,
  477. :updated_by_id => 1,
  478. }
  479. map.each { |key,value|
  480. if state[key.to_s]
  481. state_new[value] = state[key.to_s]
  482. end
  483. }
  484. # check if state already exists
  485. state_old = Ticket::State.where( :id => state_new[:id] ).first
  486. # puts 'st: ' + state['TypeName']
  487. # set state types
  488. if state['TypeName'] == 'pending auto'
  489. state['TypeName'] = 'pending action'
  490. end
  491. ticket_state_type = Ticket::StateType.where( :name => state['TypeName'] ).first
  492. state_new[:state_type_id] = ticket_state_type.id
  493. if state_old
  494. # puts 'TS: ' + state_new.inspect
  495. state_old.update_attributes(state_new)
  496. else
  497. state = Ticket::State.new(state_new)
  498. state.id = state_new[:id]
  499. state.save
  500. end
  501. }
  502. end
  503. def self.ticket_priority
  504. response = request( "public.pl?Action=Export;Type=Priority" )
  505. return if !response
  506. return if !response.success?
  507. result = json(response)
  508. map = {
  509. :ChangeTime => :updated_at,
  510. :CreateTime => :created_at,
  511. :CreateBy => :created_by_id,
  512. :ChangeBy => :updated_by_id,
  513. :Name => :name,
  514. :ID => :id,
  515. :ValidID => :active,
  516. :Comment => :note,
  517. };
  518. result.each { |priority|
  519. _set_valid(priority)
  520. # get new attributes
  521. priority_new = {
  522. :created_by_id => 1,
  523. :updated_by_id => 1,
  524. }
  525. map.each { |key,value|
  526. if priority[key.to_s]
  527. priority_new[value] = priority[key.to_s]
  528. end
  529. }
  530. # check if state already exists
  531. priority_old = Ticket::Priority.where( :id => priority_new[:id] ).first
  532. # set state types
  533. if priority_old
  534. priority_old.update_attributes(priority_new)
  535. else
  536. priority = Ticket::Priority.new(priority_new)
  537. priority.id = priority_new[:id]
  538. priority.save
  539. end
  540. }
  541. end
  542. def self.ticket_group
  543. response = request( "public.pl?Action=Export;Type=Queue" )
  544. return if !response
  545. return if !response.success?
  546. result = json(response)
  547. map = {
  548. :ChangeTime => :updated_at,
  549. :CreateTime => :created_at,
  550. :CreateBy => :created_by_id,
  551. :ChangeBy => :updated_by_id,
  552. :Name => :name,
  553. :QueueID => :id,
  554. :ValidID => :active,
  555. :Comment => :note,
  556. };
  557. result.each { |group|
  558. _set_valid(group)
  559. # get new attributes
  560. group_new = {
  561. :created_by_id => 1,
  562. :updated_by_id => 1,
  563. }
  564. map.each { |key,value|
  565. if group[key.to_s]
  566. group_new[value] = group[key.to_s]
  567. end
  568. }
  569. # check if state already exists
  570. group_old = Group.where( :id => group_new[:id] ).first
  571. # set state types
  572. if group_old
  573. group_old.update_attributes(group_new)
  574. else
  575. group = Group.new(group_new)
  576. group.id = group_new[:id]
  577. group.save
  578. end
  579. }
  580. end
  581. def self.user
  582. response = request( "public.pl?Action=Export;Type=User" )
  583. return if !response
  584. return if !response.success?
  585. result = json(response)
  586. map = {
  587. :ChangeTime => :updated_at,
  588. :CreateTime => :created_at,
  589. :CreateBy => :created_by_id,
  590. :ChangeBy => :updated_by_id,
  591. :UserID => :id,
  592. :ValidID => :active,
  593. :Comment => :note,
  594. :UserEmail => :email,
  595. :UserFirstname => :firstname,
  596. :UserLastname => :lastname,
  597. # :UserTitle =>
  598. :UserLogin => :login,
  599. :UserPw => :password,
  600. };
  601. result.each { |user|
  602. # puts 'USER: ' + user.inspect
  603. _set_valid(user)
  604. role = Role.lookup( :name => 'Agent' )
  605. # get new attributes
  606. user_new = {
  607. :created_by_id => 1,
  608. :updated_by_id => 1,
  609. :source => 'OTRS Import',
  610. :role_ids => [ role.id ],
  611. }
  612. map.each { |key,value|
  613. if user[key.to_s]
  614. user_new[value] = user[key.to_s]
  615. end
  616. }
  617. # check if state already exists
  618. # user_old = User.where( :login => user_new[:login] ).first
  619. user_old = User.where( :id => user_new[:id] ).first
  620. # set state types
  621. if user_old
  622. puts "update User.find(#{user_new[:id]})"
  623. # puts 'Update User' + user_new.inspect
  624. user_new.delete( :role_ids )
  625. user_old.update_attributes(user_new)
  626. else
  627. puts "add User.find(#{user_new[:id]})"
  628. # puts 'Add User' + user_new.inspect
  629. user = User.new(user_new)
  630. user.id = user_new[:id]
  631. user.save
  632. end
  633. # end
  634. }
  635. end
  636. def self.customer
  637. done = false
  638. count = 0
  639. while done == false
  640. sleep 2
  641. puts "Count=#{count};Offset=#{count}"
  642. response = request( "public.pl?Action=Export;Type=Customer;Count=100;Offset=#{count}" )
  643. return if !response
  644. count = count + 3000
  645. return if !response.success?
  646. result = json(response)
  647. map = {
  648. :ChangeTime => :updated_at,
  649. :CreateTime => :created_at,
  650. :CreateBy => :created_by_id,
  651. :ChangeBy => :updated_by_id,
  652. :ValidID => :active,
  653. :UserComment => :note,
  654. :UserEmail => :email,
  655. :UserFirstname => :firstname,
  656. :UserLastname => :lastname,
  657. # :UserTitle =>
  658. :UserLogin => :login,
  659. :UserPassword => :password,
  660. :UserPhone => :phone,
  661. :UserFax => :fax,
  662. :UserMobile => :mobile,
  663. :UserStreet => :street,
  664. :UserZip => :zip,
  665. :UserCity => :city,
  666. :UserCountry => :country,
  667. };
  668. done = true
  669. result.each { |user|
  670. done = false
  671. _set_valid(user)
  672. role = Role.lookup( :name => 'Customer' )
  673. # get new attributes
  674. user_new = {
  675. :created_by_id => 1,
  676. :updated_by_id => 1,
  677. :source => 'OTRS Import',
  678. :role_ids => [role.id],
  679. }
  680. map.each { |key,value|
  681. if user[key.to_s]
  682. user_new[value] = user[key.to_s]
  683. end
  684. }
  685. # check if state already exists
  686. # user_old = User.where( :login => user_new[:login] ).first
  687. user_old = User.where( :login => user_new[:login] ).first
  688. # set state types
  689. if user_old
  690. puts "update User.find(#{user_new[:id]})"
  691. # puts 'Update User' + user_new.inspect
  692. user_old.update_attributes(user_new)
  693. else
  694. # puts 'Add User' + user_new.inspect
  695. puts "add User.find(#{user_new[:id]})"
  696. user = User.new(user_new)
  697. user.save
  698. end
  699. }
  700. end
  701. end
  702. def self._set_valid(record)
  703. # map
  704. if record['ValidID'] == '3'
  705. record['ValidID'] = '2'
  706. end
  707. if record['ValidID'] == '2'
  708. record['ValidID'] = false
  709. end
  710. if record['ValidID'] == '1'
  711. record['ValidID'] = true
  712. end
  713. if record['ValidID'] == '0'
  714. record['ValidID'] = false
  715. end
  716. end
  717. end