was.rb 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. #!/usr/bin/ruby
  2. #
  3. # This file is part of centurio.work/ing/commands.
  4. #
  5. # centurio.work/ing/commands is free software: you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or (at your
  8. # option) any later version.
  9. #
  10. # centurio.work/ing/commands is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  13. # Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along with
  16. # centurio.work/ing/commands (file COPYING in the main directory). If not, see
  17. # <http://www.gnu.org/licenses/>.
  18. require 'rubygems'
  19. require 'json'
  20. require 'riddl/server'
  21. require 'riddl/client'
  22. require 'fileutils'
  23. require 'nokogiri'
  24. require 'sqlite3'
  25. require 'net/http'
  26. $db = SQLite3::Database.open 'database/stations.db'
  27. class GetStations < Riddl::Implementation
  28. def response
  29. result = $db.execute "SELECT * FROM stations"
  30. builder = Nokogiri::XML::Builder.new do |xml|
  31. xml.stations {
  32. result.each do |row|
  33. xml.station(:id => row[0]){
  34. resultstation = $db.execute "SELECT * FROM station WHERE station = #{row[0]}"
  35. resultstation.each do |row|
  36. xml.pattern(:id => row[1], :value => row[2], :description => row[3], :changed => row[4])
  37. end
  38. }
  39. end
  40. }
  41. end
  42. #puts builder.to_xml
  43. Riddl::Parameter::Complex.new('stations','application/xml',builder.to_xml)
  44. end
  45. end
  46. class CreateStation < Riddl::Implementation
  47. def self::createDB(n)
  48. #result = $db.execute "SELECT MAX(station) FROM stations"
  49. #if(result[0][0] == nil)
  50. # $db.execute "INSERT INTO stations (station) VALUES (?)", 0
  51. #else
  52. # $db.execute "INSERT INTO stations (station) VALUES (?)", result[0][0]+1
  53. #end
  54. #$db.execute ("INSERT INTO stations (station, name) VALUES (?,?)", [n,n])
  55. $db.execute("INSERT OR IGNORE INTO stations (station, name) VALUES (?,?)", [n,n])
  56. end
  57. def response
  58. GetStation::createDB
  59. end
  60. end
  61. class GetStation < Riddl::Implementation
  62. def self::prepare(id)
  63. result = $db.execute "SELECT * FROM station WHERE station = #{id}"
  64. Nokogiri::XML::Builder.new do |xml|
  65. xml.station(:id => id){
  66. result.each do |row|
  67. xml.pattern(:id => row[1], :value => row[2], :description => row[3], :changed => row[4])
  68. end
  69. }
  70. end
  71. end
  72. def response
  73. builder = GetStation::prepare(@r.last)
  74. Riddl::Parameter::Complex.new('station','application/xml',builder.to_xml)
  75. end
  76. end
  77. class CreatePattern < Riddl::Implementation
  78. def response
  79. doc = Nokogiri::XML(@p[0].value)
  80. result = $db.execute "SELECT MAX(patternID) FROM station"
  81. if(result[0][0] == nil)
  82. id = 0
  83. else
  84. id = result[0][0] +1
  85. end
  86. values = doc.xpath("/*/@value")[0].value.split(".")
  87. $db.execute("INSERT INTO station (station, patternID, pattern, description, date, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", [@r.last, id, doc.xpath("/*/@value")[0].value, doc.xpath("/*/@description")[0].value, doc.xpath("/*/@changed")[0].value, values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7], values[8], values[9], values[10]])
  88. ret = {:id => id}
  89. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  90. end
  91. end
  92. class GetPattern < Riddl::Implementation
  93. def response
  94. result = $db.execute "SELECT * FROM station WHERE station = #{@r[0]} and patternID = #{@r[1]}"
  95. builder = Nokogiri::XML::Builder.new do |xml|
  96. xml.pattern(:id => result[0][1], :value => result[0][2], :description => result[0][3], :changed => result[0][4])
  97. end
  98. Riddl::Parameter::Complex.new('pattern','application/xml',builder.to_xml)
  99. end
  100. end
  101. class UpdatePattern < Riddl::Implementation
  102. def response
  103. doc = Nokogiri::XML(@p[0].value)
  104. #puts "UPdate Pattern" + @r[0] + @r[1] + " value " + doc.xpath("/*/@value")[0].value
  105. #$db.execute("DELETE FROM station WHERE station = ? AND patternID = ?", [@r[0], @r[1]])
  106. values = doc.xpath("/*/@value")[0].value.split(".")
  107. #$db.execute("INSERT INTO station (station, patternID, pattern, description, date, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", [@r[0], @r[1], doc.xpath("/*/@value")[0].value, doc.xpath("/*/@description")[0].value, doc.xpath("/*/@changed")[0].value, values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7], values[8], values[9], values[10]])
  108. #$db.execute("UPDATE image SET imageID = ? WHERE station = ? AND patternID = ? AND imageID = ? AND language = ?", [999999, @r[0], @r[1], i, @r[4]])
  109. $db.execute("Update station SET pattern = ?, description = ?, date = ?, P0 = ?, P1 = ?, P2 = ?, P3 = ?, P4 = ?, P5 = ?, P6 = ?, P7 = ?, P8 = ?, P9 = ?, P10 = ? WHERE station = ? AND patternID = ?", [doc.xpath("/*/@value")[0].value, doc.xpath("/*/@description")[0].value, doc.xpath("/*/@changed")[0].value, values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7], values[8], values[9], values[10], @r[0], @r[1]])
  110. #$db.execute("INSERT INTO station (station, patternID, pattern, description, date) VALUES (?,?,?,?,?)", [@r[0], @r[1]], doc.xpath("/*/@value")[0].value, doc.xpath("/*/@description")[0].value, doc.xpath("/*/@changed")[0].value)
  111. #$db.execute("UPDATE station SET pattern = ? and description = ? and date = ? WHERE station = ? and patternID = ?", [doc.xpath("/*/@value")[0].value, doc.xpath("/*/@description")[0].value, doc.xpath("/*/@changed")[0].value, @r[0], @r[1]])
  112. end
  113. end
  114. class DeletePattern < Riddl::Implementation
  115. def response
  116. $db.execute("DELETE FROM station WHERE station = ? AND patternID = ?", [@r[0], @r[1]])
  117. end
  118. end
  119. class DuplicatePattern < Riddl::Implementation
  120. def response
  121. #duplicate db etwas mühsam da der eintrag kopiert werden muss und dabei die unique ID verändert werden muss daher geht insert into select nicht so ganz.
  122. result = $db.execute "SELECT MAX(patternID) FROM station"
  123. if(result[0][0] == nil)
  124. id = 0
  125. else
  126. id = result[0][0] +1
  127. end
  128. result = $db.execute "SELECT * FROM station WHERE station = #{@r[0]} and patternID = #{@r[1]}"
  129. result[0][1] = id;
  130. questionmarks = ""
  131. result[0].length().times{questionmarks = questionmarks + ",?"}
  132. questionmarks[0] = ""
  133. $db.execute( "INSERT INTO station values (#{questionmarks})", result[0])
  134. #duplicate error
  135. result = $db.execute "SELECT * FROM error WHERE station = #{@r[0]} and patternID = #{@r[1]}"
  136. if(result[0] != nil)
  137. result.each do |row|
  138. row[1] = id;
  139. questionmarks = ""
  140. row.length().times{questionmarks = questionmarks + ",?"}
  141. questionmarks[0] = ""
  142. $db.execute( "INSERT INTO error values (#{questionmarks})", row)
  143. end
  144. end
  145. #duplicate image db
  146. result = $db.execute "SELECT * FROM image WHERE station = #{@r[0]} and patternID = #{@r[1]}"
  147. if(result[0] != nil)
  148. result.each do |row|
  149. row[1] = id;
  150. questionmarks = ""
  151. row.length().times{questionmarks = questionmarks + ",?"}
  152. questionmarks[0] = ""
  153. $db.execute( "INSERT INTO image values (#{questionmarks})", row)
  154. end
  155. end
  156. #duplicate images
  157. pathorigin = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1])
  158. pathdestination = File.join(File.dirname(__dir__),'images/uploads', @r[0], id.to_s)
  159. FileUtils.mkdir_p(pathdestination)
  160. FileUtils.copy_entry pathorigin, pathdestination
  161. #duplicate Replacement db
  162. result = $db.execute "SELECT * FROM replacements WHERE station = #{@r[0]} and patternID = #{@r[1]}"
  163. if(result[0] != nil)
  164. result.each do |row|
  165. row[1] = id;
  166. questionmarks = ""
  167. row.length().times{questionmarks = questionmarks + ",?"}
  168. questionmarks[0] = ""
  169. $db.execute( "INSERT INTO replacements values (#{questionmarks})", row)
  170. end
  171. end
  172. ret = {:id => id}
  173. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  174. end
  175. end
  176. class SaveError < Riddl::Implementation
  177. def response
  178. $db.execute "CREATE TABLE IF NOT EXISTS error(station INT, patternID INT, error TEXT, FOREIGN KEY(station) REFERENCES station(station), FOREIGN KEY(patternID) REFERENCES station(patternID))"
  179. #Delete all, afterwards insert
  180. $db.execute("DELETE FROM error WHERE station = ? AND patternID = ?", [@r[0], @r[1]])
  181. doc = Nokogiri::XML(@p[0].value)
  182. doc.xpath(".//reason").each do |node|
  183. $db.execute("INSERT INTO error (station, patternID, error) VALUES (?,?,?)", [@r[0], @r[1], node.text])
  184. end
  185. end
  186. end
  187. class GetError < Riddl::Implementation
  188. def response
  189. $db.execute "CREATE TABLE IF NOT EXISTS error(station INT, patternID INT, error TEXT, FOREIGN KEY(station) REFERENCES station(station), FOREIGN KEY(patternID) REFERENCES station(patternID))"
  190. result = $db.execute "SELECT * FROM error WHERE station = #{@r[0]} and patternID = #{@r[1]}"
  191. builder = Nokogiri::XML::Builder.new do |xml|
  192. xml.error(){
  193. result.each do |row|
  194. xml.reason row[2]
  195. end
  196. }
  197. end
  198. Riddl::Parameter::Complex.new('error','application/xml',builder.to_xml)
  199. end
  200. end
  201. class SaveReplacement < Riddl::Implementation
  202. def response
  203. #Delete all, afterwards insert
  204. $db.execute("DELETE FROM replacements WHERE station = ? AND patternID = ?", [@r[0], @r[1]])
  205. order = 0;
  206. doc = Nokogiri::XML(@p[0].value)
  207. doc.xpath(".//item").each do |node|
  208. $db.execute("INSERT INTO replacements (station, patternID, abbreviation, url, ordering) VALUES (?,?,?,?,?)", [@r[0], @r[1], node.xpath(".//abbreviation").text, node.xpath(".//url").text, order])
  209. order += 1
  210. end
  211. end
  212. end
  213. class GetReplacement < Riddl::Implementation
  214. def response
  215. result = $db.execute "SELECT * FROM replacements WHERE station = #{@r[0]} and patternID = #{@r[1]} ORDER BY ordering ASC"
  216. builder = Nokogiri::XML::Builder.new do |xml|
  217. xml.replacement(){
  218. result.each do |row|
  219. xml.item(){
  220. xml.abbreviation row[2]
  221. xml.url row[3]
  222. }
  223. end
  224. }
  225. end
  226. Riddl::Parameter::Complex.new('replacement','application/xml',builder.to_xml)
  227. end
  228. end
  229. class GetImages < Riddl::Implementation
  230. def response
  231. formatted_no_decl = Nokogiri::XML::Node::SaveOptions::FORMAT +
  232. Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
  233. result = $db.execute "SELECT DISTINCT station, patternID, imageID FROM image WHERE station = #{@r[0]} and patternID = #{@r[1]}"
  234. builder = Nokogiri::XML::Builder.new do |xml|
  235. xml.images(){
  236. result.each do |row|
  237. xml<< GetImage::prepare(@r[0], @r[1], row[2]).to_xml( save_with:formatted_no_decl )
  238. end
  239. }
  240. end
  241. Riddl::Parameter::Complex.new('images','application/xml',builder.to_xml)
  242. end
  243. end
  244. class UploadImage < Riddl::Implementation
  245. def response
  246. lang = @p[@p.length-1].value
  247. result = $db.execute "SELECT MAX(imageID) FROM image WHERE station = #{@r[0]} and patternID = #{@r[1]} and language = '#{lang}'"
  248. if(result[0][0] == nil)
  249. id = 0
  250. else
  251. id = result[0][0] +1
  252. end
  253. #puts JSON.pretty_generate(@p)
  254. i = 0
  255. while i < @p.length-1 do
  256. item = @p[i]
  257. if(item != nil && item.name == "files[]")
  258. path = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], id.to_s)
  259. FileUtils.mkdir_p(path)
  260. #juergen nach alternative fragen
  261. readFile = File.read(item.value.inspect.to_s[/Tempfile:(.*?)>/m, 1])
  262. File.open(File.join(path, lang + ".svg"), 'wb') do |file|
  263. file.write(readFile.to_s)
  264. end
  265. $db.execute("INSERT INTO image (station, patternID, imageID, language, label) VALUES (?,?,?,?,?)", [@r[0], @r[1], id, lang, "Label"])
  266. id += 1
  267. end
  268. i +=1
  269. end
  270. end
  271. end
  272. class AddExternalImage < Riddl::Implementation
  273. def response
  274. doc = Nokogiri::XML(@p[0].value)
  275. url = doc.xpath("/externalImage/url").text
  276. lang = doc.xpath("/externalImage/lang").text
  277. result = $db.execute "SELECT MAX(imageID) FROM image WHERE station = #{@r[0]} and patternID = #{@r[1]} and language = '#{lang}'"
  278. if(result[0][0] == nil)
  279. id = 0
  280. else
  281. id = result[0][0] +1
  282. end
  283. #Create link file (we create a file so moving/deleting works always the same and the database does not have to be changed for external files) only thing that had to be changed was imgReplacement.php
  284. image = '<svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" overflow="hidden"> <image href="' + url +'" height="100%" width="100%"/>
  285. </svg>'
  286. path = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], id.to_s)
  287. File.open(File.join(path, lang + ".svg"), 'wb') do |file|
  288. file.write(image)
  289. end
  290. #Create DB entry as usual
  291. $db.execute("INSERT INTO image (station, patternID, imageID, language, label) VALUES (?,?,?,?,?)", [@r[0], @r[1], id, lang, url])
  292. end
  293. end
  294. class ReorderImages < Riddl::Implementation
  295. def response
  296. #remove brackets
  297. iter = @p[0].value.read.chop
  298. iter[0] =""
  299. iter = iter.split(",").map(&:to_i)
  300. i = 0
  301. while i < iter.length do
  302. if(i != iter[i])
  303. #swap
  304. path = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], i.to_s)
  305. path2 = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], iter[i].to_s)
  306. tmp = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1])
  307. FileUtils.mv(path + "/" + @r[4] + ".svg", tmp)
  308. FileUtils.mv(path2 + "/" + @r[4] + ".svg", path)
  309. FileUtils.mv(tmp + "/" + @r[4] + ".svg", path2)
  310. #DB Swap
  311. result = $db.execute "SELECT MAX(imageID) FROM image"
  312. if(result[0][0] == nil)
  313. maxImgId = 0
  314. else
  315. maxImgId = result[0][0] +1
  316. end
  317. #Wegen 999999 fragen
  318. #nutze max ID+1 zum tauschen
  319. $db.execute("UPDATE image SET imageID = ? WHERE station = ? AND patternID = ? AND imageID = ? AND language = ?", [maxImgId, @r[0], @r[1], i, @r[4]])
  320. $db.execute("UPDATE image SET imageID = ? WHERE station = ? AND patternID = ? AND imageID = ? AND language = ?", [i, @r[0], @r[1], iter[i], @r[4]])
  321. $db.execute("UPDATE image SET imageID = ? WHERE station = ? AND patternID = ? AND imageID = ? AND language = ?", [iter[i], @r[0], @r[1], maxImgId, @r[4]])
  322. iter.map! do |item|
  323. if(item == i)
  324. iter[i]
  325. else
  326. item
  327. end
  328. end
  329. end
  330. i +=1
  331. end
  332. end
  333. end
  334. class GetImage < Riddl::Implementation
  335. def self::prepare(station, pattern, imageID)
  336. result = $db.execute "SELECT * FROM image WHERE station = #{station} and patternID = #{pattern} and imageID = #{imageID}"
  337. Nokogiri::XML::Builder.new do |xml|
  338. xml.image(:id => imageID){
  339. result.each do |row|
  340. xml.variant(:lang => row[3], :label => row[4]){
  341. xml.text("https://centurio.work/customers/evva/was/images/uploads/#{station}/#{pattern}/#{imageID}/#{row[3]}.svg")
  342. }
  343. end
  344. }
  345. end
  346. end
  347. def response
  348. builder = GetImage::prepare(@r[0], @r[1], @r[3])
  349. Riddl::Parameter::Complex.new('image','application/xml',builder.to_xml)
  350. end
  351. end
  352. class UpdateImageLabel < Riddl::Implementation
  353. def response
  354. $db.execute("UPDATE image SET label = ? WHERE station = ? AND patternID = ? AND imageID = ? AND language = ?", [@p[0].value, @r[0], @r[1], @r[3], @r[4]])
  355. end
  356. end
  357. class GetRealImage < Riddl::Implementation
  358. def response
  359. #split on "." and tacke [0] to allow for e.g. de-at.svg
  360. #would lead to error on de.at.svg //should this be fixed? would also lead to error if everything after last "." would be removed in case of only having de.at
  361. #https://centurio.work/customers/evva/was/server/0/0/images/3/de-at?video=xyz
  362. img = File.read(File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], @r[3], @r[4].split(".")[0] + ".svg"))
  363. if false #Currently replacements are done on the client side
  364. if(@p[0].nil?)
  365. puts "Undefined p"
  366. else
  367. if(@p[0].name == "video" && @p[0].value)
  368. xml = Nokogiri.parse img
  369. puts "Width " + xml.xpath("string(//xmlns:image/@width)")
  370. puts "Height " + xml.xpath("string(//xmlns:image/@height)")
  371. #puts "Posi " + xml.xpath("string(//xmlns:clipPath/path/@height)")
  372. puts "Posi " + xml.xpath("string(//xmlns:image/following-sibling::clipPath/@id)")
  373. puts xml.xpath("string(//xmlns:text[starts-with(text(), 'url')])").sub("url=", "")
  374. # text
  375. img = img.sub! "</svg>", '
  376. <g><g transform="translate(562,288),scale(1.35185 1.35185)">
  377. <foreignObject width="480" height="270">
  378. <video width="480" height="270" controls="" autoplay="autoplay" muted="muted" xmlns="http://www.w3.org/1999/xhtml" >
  379. <source src="' + xml.xpath("string(//xmlns:text[starts-with(text(), 'url')])").sub("url=", "") +'" type="video/mp4"/>
  380. </video>
  381. </foreignObject>
  382. </g></g></svg>'
  383. else
  384. @p.each do |item|
  385. unless item.name.nil? || item.value.nil?
  386. img.sub! item.name, item.value
  387. end
  388. end
  389. end
  390. end
  391. end
  392. Riddl::Parameter::Complex.new('theRealImage','image/svg+xml',img)
  393. end
  394. end
  395. class DeleteImage < Riddl::Implementation
  396. def response
  397. #puts "Delete Image"
  398. #puts File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], @r[3] , @r[4] + ".svg")
  399. File.delete(File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], @r[3] , @r[4] + ".svg")) if File.exist?(File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], @r[3] , @r[4] + ".svg"))
  400. $db.execute("DELETE FROM image WHERE station = ? AND patternID = ? AND imageID = ? AND language = ?", [@r[0], @r[1], @r[3], @r[4]])
  401. result = $db.execute "SELECT MAX(imageID) FROM image WHERE station = #{@r[0]} and patternID = #{@r[1]} and language = '#{@r[4]}'"
  402. if(result[0][0] == nil)
  403. id = 0
  404. else
  405. id = result[0][0] +1
  406. end
  407. #reorder if !end gets deleted
  408. if(id == @r[3])
  409. return
  410. else
  411. cur = @r[3].to_i + 1
  412. prev = @r[3].to_i
  413. while cur < id.to_i do
  414. $db.execute("UPDATE image SET imageID = ? WHERE station = ? AND patternID = ? AND imageID = ? AND language = ?", [prev, @r[0], @r[1], cur, @r[4]])
  415. src = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], cur.to_s, @r[4] + ".svg")
  416. target = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], prev.to_s, @r[4] + ".svg")
  417. FileUtils.mv(src, target)
  418. cur+=1
  419. prev += 1
  420. end
  421. end
  422. end
  423. end
  424. class ListSearch < Riddl::Implementation
  425. def response
  426. ret = {
  427. :patternID => "/patternID",
  428. :multi => "/multi",
  429. :juergen => "/juergen"
  430. }
  431. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  432. end
  433. end
  434. class SearchPattern < Riddl::Implementation
  435. def response
  436. iter = @p[0].value.split(".")
  437. searchstring = ""
  438. count = 0
  439. iter.each do |item|
  440. if !item.nil?
  441. if item.to_s != "*"
  442. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  443. end
  444. count += 1
  445. end
  446. end
  447. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  448. if result.length > 0
  449. ret = {
  450. :amount => result.length.to_s,
  451. :ids => result
  452. }
  453. end
  454. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  455. #puts the first found string
  456. #result = $db.execute "SELECT P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10 FROM station WHERE station = 0" + searchstring
  457. #puts "found" + result.length.to_s
  458. #result[0].each do |item|
  459. # if !item.nil?
  460. # puts item + "."
  461. # end
  462. #end
  463. end
  464. end
  465. class SearchImages < Riddl::Implementation
  466. def response
  467. iter = @p[0].value.split(".")
  468. searchstring = ""
  469. count = 0
  470. #search = K.*.9.*
  471. #match: = K.5.9.7
  472. #match: = K.5.9.*
  473. #match: = K.*.*.*
  474. #no match: = K.*.8.*
  475. iter.each do |item|
  476. if !item.nil?
  477. if item.to_s != "*"
  478. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  479. else
  480. searchstring = searchstring + " and (P" + count.to_s + "!=''" + ")"
  481. end
  482. count += 1
  483. end
  484. end
  485. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  486. ret = []
  487. if result.length > 0
  488. result.each do |item|
  489. result2 = $db.execute "SELECT DISTINCT station, patternID, imageID FROM image WHERE station = #{@r[0]} and patternID = #{item[0]}"
  490. result2.each do |item2|
  491. ret << item2[0].to_s + "/" + item2[1].to_s + "/" + item2[2].to_s
  492. end
  493. end
  494. end
  495. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  496. end
  497. end
  498. class SearchImagesSingle < Riddl::Implementation
  499. def response
  500. iter = @p[0].value.split(".")
  501. searchstring = ""
  502. count = 0
  503. iter.each do |item|
  504. if !item.nil?
  505. if item.to_s != "*"
  506. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  507. else
  508. searchstring = searchstring + " and (P" + count.to_s + "!=''" + ")"
  509. end
  510. count += 1
  511. end
  512. end
  513. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  514. count = 0
  515. pattern = 0
  516. image = 0
  517. if result.length > 0
  518. result.each do |item|
  519. result2 = $db.execute "SELECT DISTINCT station, patternID, imageID FROM image WHERE station = #{@r[0]} and patternID = #{item[0]}"
  520. result2.each do |item2|
  521. if(count == @r[3].to_i)
  522. puts "Found "
  523. pattern = item2[1]
  524. image = item2[2]
  525. end
  526. count += 1
  527. end
  528. end
  529. end
  530. img = File.read(File.join(File.dirname(__dir__),'images/uploads', @r[0].to_s, pattern.to_s, image.to_s, "de-at.svg"))
  531. Riddl::Parameter::Complex.new('theRealImage','image/svg+xml',img)
  532. end
  533. end
  534. class SearchImages2 < Riddl::Implementation
  535. def response
  536. iter = @p[0].value.split(".")
  537. searchstring = ""
  538. count = 0
  539. #search = K.*.9.*
  540. #match: = K.5.9.7
  541. #match: = K.5.9.*
  542. #match: = K.*.*.*
  543. #no match: = K.*.8.*
  544. iter.each do |item|
  545. if !item.nil?
  546. if item.to_s != "*"
  547. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  548. else
  549. searchstring = searchstring + " and (P" + count.to_s + "!=''" + ")"
  550. end
  551. count += 1
  552. end
  553. end
  554. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  555. ret = []
  556. if result.length > 0
  557. result.each do |item|
  558. result2 = $db.execute "SELECT DISTINCT station, patternID, imageID FROM image WHERE station = #{@r[0]} and patternID = #{item[0]}"
  559. result2.each do |item2|
  560. ret << item2[0].to_s + "/" + item2[1].to_s + "/" + item2[2].to_s
  561. end
  562. end
  563. end
  564. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  565. end
  566. end
  567. class SearchImages2Single < Riddl::Implementation
  568. def response
  569. iter = @p[0].value.split(".")
  570. searchstring = ""
  571. count = 0
  572. iter.each do |item|
  573. if !item.nil?
  574. if item.to_s != "*"
  575. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  576. else
  577. searchstring = searchstring + " and (P" + count.to_s + "!=''" + ")"
  578. end
  579. count += 1
  580. end
  581. end
  582. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  583. count = 0
  584. pattern = 0
  585. image = 0
  586. if result.length > 0
  587. result.each do |item|
  588. result2 = $db.execute "SELECT DISTINCT station, patternID, imageID FROM image WHERE station = #{@r[0]} and patternID = #{item[0]}"
  589. result2.each do |item2|
  590. if(count == @r[3].to_i)
  591. puts "Found "
  592. pattern = item2[1]
  593. image = item2[2]
  594. end
  595. count += 1
  596. end
  597. end
  598. end
  599. #https://centurio.work/customers/evva/was/ui/imageReplacement.php?___image___=8/23/0/de-at.svg
  600. img = File.read(File.join(File.dirname(__dir__),'images/uploads', @r[0].to_s, pattern.to_s, image.to_s, "de-at.svg"))
  601. uri = "https://centurio.work/customers/evva/was/ui/imageReplacement.php?___image___=" + @r[0].to_s + "/" + pattern.to_s + "/" + image.to_s + "/de-at.svg"
  602. string = '<html><body style="margin: 0"><iframe style="position: absolute; height: 100%; width: 100%; border: none" src="' + uri +'" title="description"></body></html>'
  603. Riddl::Parameter::Complex.new('html','text/html',string)
  604. end
  605. end
  606. class SearchImages3Single < Riddl::Implementation
  607. def response
  608. iter = @p[0].value.split(".")
  609. searchstring = ""
  610. count = 0
  611. iter.each do |item|
  612. if !item.nil?
  613. if item.to_s != "*"
  614. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  615. else
  616. searchstring = searchstring + " and (P" + count.to_s + "!=''" + ")"
  617. end
  618. count += 1
  619. end
  620. end
  621. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  622. count = 0
  623. pattern = 0
  624. image = 0
  625. builder = Nokogiri::XML::Builder.new do |xml|
  626. xml.image(:id => @r[3].to_s){
  627. if result.length > 0
  628. result.each do |item|
  629. result2 = $db.execute "SELECT DISTINCT station, patternID, imageID FROM image WHERE station = #{@r[0]} and patternID = #{item[0]}"
  630. result2.each do |item2|
  631. if(count == @r[3].to_i)
  632. result3 = $db.execute "SELECT DISTINCT station, patternID, imageID, language, label FROM image WHERE station = #{@r[0]} and patternID = #{item[0]} and imageID =#{item2[2]}"
  633. result3.each do |item3|
  634. uri = "https://centurio.work/customers/evva/was/ui/imageReplacement.php?___image___=" + @r[0].to_s + "/" + item3[1].to_s + "/" + item3[2].to_s + "/" + item3[3].to_s + ".svg"
  635. @p.each_with_index do |item, index|
  636. if index != 0
  637. uri += "&" + item.name.to_s + "=" + item.value.to_s
  638. end
  639. end
  640. xml.variant(:lang => item3[3].to_s, :label => item3[4].to_s){
  641. xml.text(uri)
  642. }
  643. end
  644. end
  645. count += 1
  646. end
  647. end
  648. end
  649. }
  650. end
  651. #https://centurio.work/customers/evva/was/ui/imageReplacement.php?___image___=8/23/0/de-at.svg
  652. #puts builder.to_xml
  653. Riddl::Parameter::Complex.new('image','application/xml',builder.to_xml)
  654. end
  655. end
  656. class SearchErrors < Riddl::Implementation
  657. def response
  658. iter = @p[0].value.split(".")
  659. searchstring = ""
  660. count = 0
  661. #search = K.*.9.*
  662. #match: = K.5.9.7
  663. #match: = K.5.9.*
  664. #match: = K.*.*.*
  665. #no match: = K.*.8.*
  666. iter.each do |item|
  667. if !item.nil?
  668. if item.to_s != "*"
  669. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  670. else
  671. searchstring = searchstring + " and (P" + count.to_s + "!=''" + ")"
  672. end
  673. count += 1
  674. end
  675. end
  676. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  677. ret = []
  678. if result.length > 0
  679. result.each do |item|
  680. result2 = $db.execute "SELECT DISTINCT error FROM error WHERE station = #{@r[0]} and patternID = #{item[0]}"
  681. result2.each do |item2|
  682. ret << item2[0].to_s
  683. end
  684. end
  685. end
  686. ret = ret.uniq
  687. builder = Nokogiri::XML::Builder.new do |xml|
  688. xml.reason{
  689. if result.length > 0
  690. ret.each do |item|
  691. xml.reason(:lang => "de-at"){
  692. xml.text(item)
  693. }
  694. end
  695. end
  696. }
  697. end
  698. Riddl::Parameter::Complex.new('errors','application/xml',builder.to_xml)
  699. end
  700. end
  701. class SearchImagesReverse < Riddl::Implementation
  702. def response
  703. iter = @p[0].value.split(".")
  704. searchstring = ""
  705. count = 0
  706. #search = K.*.9.*
  707. #match: = K.5.9.7
  708. #match: = K.5.9.*
  709. #match: = K.*.*.*
  710. #no match: = K.*.8.*
  711. iter.each do |item|
  712. if !item.nil?
  713. if item.to_s != "*"
  714. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  715. else
  716. searchstring = searchstring + " and (P" + count.to_s + "!=''" + ")"
  717. end
  718. count += 1
  719. end
  720. end
  721. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  722. ret = []
  723. if result.length > 0
  724. result.each do |item|
  725. result2 = $db.execute "SELECT DISTINCT station, patternID, imageID FROM image WHERE station = #{@r[0]} and patternID = #{item[0]}"
  726. result2.each do |item2|
  727. ret << item2[0].to_s + "/" + item2[1].to_s + "/" + item2[2].to_s
  728. end
  729. end
  730. end
  731. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  732. end
  733. end
  734. def createDB(opts)
  735. $db.execute("PRAGMA foreign_keys=ON");
  736. $db.execute "CREATE TABLE IF NOT EXISTS stations(station INT, name VARCHAR(20), PRIMARY KEY(station))"
  737. opts[:appconf]["stations"].each { |n|
  738. CreateStation::createDB(n)
  739. }
  740. # Unique pattern
  741. # $db.execute "CREATE TABLE IF NOT EXISTS station(station INT, patternID INT, pattern CHARACTER(20), description TEXT, date TEXT, P0 VARCHAR(2), P1 VARCHAR(2), P2 VARCHAR(2), P3 VARCHAR(2), P4 VARCHAR(2), P5 VARCHAR(2), P6 VARCHAR(2), P7 VARCHAR(2), P8 VARCHAR(2), P9 VARCHAR(2), P10 VARCHAR(2), PRIMARY KEY(station,patternID), UNIQUE(station, pattern))"
  742. $db.execute "CREATE TABLE IF NOT EXISTS station(station INT, patternID INT, pattern CHARACTER(256), description TEXT, date TEXT, P0 VARCHAR(10), P1 VARCHAR(10), P2 VARCHAR(10), P3 VARCHAR(10), P4 VARCHAR(10), P5 VARCHAR(10), P6 VARCHAR(10), P7 VARCHAR(10), P8 VARCHAR(10), P9 VARCHAR(10), P10 VARCHAR(10), PRIMARY KEY(station,patternID), UNIQUE(station, patternID))"
  743. $db.execute "CREATE TABLE IF NOT EXISTS error(station INT, patternID INT, error TEXT, FOREIGN KEY(station, patternID) REFERENCES station(station, patternID) ON DELETE CASCADE)"
  744. $db.execute "CREATE TABLE IF NOT EXISTS replacements(station INT, patternID INT, abbreviation TEXT, url TEXT, ordering INT, FOREIGN KEY(station, patternID) REFERENCES station(station, patternID) ON DELETE CASCADE)"
  745. $db.execute "CREATE TABLE IF NOT EXISTS image(station INT, patternID INT, imageID INT, language CHARACTER(20), label TEXT, FOREIGN KEY(station, patternID) REFERENCES station(station, patternID) ON DELETE CASCADE,PRIMARY KEY(station,patternID,imageID,language))"
  746. # $db.execute "CREATE TABLE IF NOT EXISTS error(station INT, patternID INT, error TEXT, FOREIGN KEY(station) REFERENCES station(station), FOREIGN KEY(patternID) REFERENCES station(patternID) ON DELETE CASCADE)"
  747. # $db.execute "CREATE TABLE IF NOT EXISTS image(station INT, patternID INT, imageID INT, language CHARACTER(20), label TEXT, FOREIGN KEY(station) REFERENCES station(station), FOREIGN KEY(patternID) REFERENCES station(patternID) ON DELETE CASCADE,PRIMARY KEY(station,patternID,imageID,language))"
  748. end
  749. server = Riddl::Server.new(File.join(__dir__,'/was.xml'), :host => 'localhost') do |opts|
  750. accessible_description true
  751. cross_site_xhr true
  752. createDB(opts)
  753. # opts[:appconf]
  754. on resource do
  755. run GetStations if get
  756. run CreateStation if post
  757. on resource '\d+' do
  758. run GetStation if get
  759. run CreatePattern if post 'pattern'
  760. on resource '\d+' do
  761. run GetPattern if get
  762. run UpdatePattern if put 'pattern'
  763. run DeletePattern if delete
  764. run DuplicatePattern if put 'patternID'
  765. on resource 'error' do
  766. run SaveError if put 'error'
  767. run GetError if get
  768. #should add deleteerror
  769. end
  770. on resource 'replacement' do
  771. run SaveReplacement if put 'replacement'
  772. run GetReplacement if get
  773. #should add deleteReplacement
  774. end
  775. on resource 'images' do
  776. run GetImages if get
  777. #run CreateImage if post #'image'
  778. run AddExternalImage if post 'externalImage'
  779. run UploadImage if post
  780. on resource 'reorder' do
  781. on resource do
  782. run ReorderImages if put 'orderlist'
  783. end
  784. end
  785. on resource '\d+' do
  786. run GetImage if get
  787. on resource do
  788. run UpdateImageLabel if put 'label'
  789. run GetRealImage if get
  790. run DeleteImage if delete
  791. end
  792. end
  793. end
  794. end
  795. on resource 'search' do
  796. run ListSearch if get
  797. on resource 'patternID' do
  798. run SearchPattern if get
  799. end
  800. on resource 'images' do
  801. run SearchImages if get
  802. on resource '\d+' do
  803. run SearchImagesSingle if get
  804. end
  805. end
  806. on resource 'imagesServerSide' do
  807. run SearchImages2 if get
  808. on resource '\d+' do
  809. run SearchImages2Single if get
  810. end
  811. end
  812. on resource 'imagesWIZ' do
  813. run SearchImages2 if get
  814. on resource '\d+' do
  815. run SearchImages3Single if get
  816. end
  817. end
  818. on resource 'errorsWIZ' do
  819. run SearchErrors if get
  820. end
  821. on resource 'imagesReverse' do
  822. run SearchImagesReverse if get
  823. end
  824. end
  825. end
  826. end
  827. end.loop!