was.rb 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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. doc = Nokogiri::XML(@p[0].value)
  206. doc.xpath(".//item").each do |node|
  207. $db.execute("INSERT INTO replacements (station, patternID, abbreviation, url) VALUES (?,?,?,?)", [@r[0], @r[1], node.xpath(".//abbreviation").text, node.xpath(".//url").text])
  208. end
  209. end
  210. end
  211. class GetReplacement < Riddl::Implementation
  212. def response
  213. result = $db.execute "SELECT * FROM replacements WHERE station = #{@r[0]} and patternID = #{@r[1]}"
  214. builder = Nokogiri::XML::Builder.new do |xml|
  215. xml.replacement(){
  216. result.each do |row|
  217. xml.item(){
  218. xml.abbreviation row[2]
  219. xml.url row[3]
  220. }
  221. end
  222. }
  223. end
  224. Riddl::Parameter::Complex.new('replacement','application/xml',builder.to_xml)
  225. end
  226. end
  227. class GetImages < Riddl::Implementation
  228. def response
  229. formatted_no_decl = Nokogiri::XML::Node::SaveOptions::FORMAT +
  230. Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
  231. result = $db.execute "SELECT DISTINCT station, patternID, imageID FROM image WHERE station = #{@r[0]} and patternID = #{@r[1]}"
  232. builder = Nokogiri::XML::Builder.new do |xml|
  233. xml.images(){
  234. result.each do |row|
  235. xml<< GetImage::prepare(@r[0], @r[1], row[2]).to_xml( save_with:formatted_no_decl )
  236. end
  237. }
  238. end
  239. Riddl::Parameter::Complex.new('images','application/xml',builder.to_xml)
  240. end
  241. end
  242. class UploadImage < Riddl::Implementation
  243. def response
  244. lang = @p[@p.length-1].value
  245. result = $db.execute "SELECT MAX(imageID) FROM image WHERE station = #{@r[0]} and patternID = #{@r[1]} and language = '#{lang}'"
  246. if(result[0][0] == nil)
  247. id = 0
  248. else
  249. id = result[0][0] +1
  250. end
  251. #puts JSON.pretty_generate(@p)
  252. i = 0
  253. while i < @p.length-1 do
  254. item = @p[i]
  255. if(item != nil && item.name == "files[]")
  256. path = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], id.to_s)
  257. FileUtils.mkdir_p(path)
  258. #juergen nach alternative fragen
  259. readFile = File.read(item.value.inspect.to_s[/Tempfile:(.*?)>/m, 1])
  260. File.open(File.join(path, lang + ".svg"), 'wb') do |file|
  261. file.write(readFile.to_s)
  262. end
  263. $db.execute("INSERT INTO image (station, patternID, imageID, language, label) VALUES (?,?,?,?,?)", [@r[0], @r[1], id, lang, "Label"])
  264. id += 1
  265. end
  266. i +=1
  267. end
  268. end
  269. end
  270. class AddExternalImage < Riddl::Implementation
  271. def response
  272. doc = Nokogiri::XML(@p[0].value)
  273. url = doc.xpath("/externalImage/url").text
  274. lang = doc.xpath("/externalImage/lang").text
  275. result = $db.execute "SELECT MAX(imageID) FROM image WHERE station = #{@r[0]} and patternID = #{@r[1]} and language = '#{lang}'"
  276. if(result[0][0] == nil)
  277. id = 0
  278. else
  279. id = result[0][0] +1
  280. end
  281. #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
  282. 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%"/>
  283. </svg>'
  284. path = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], id.to_s)
  285. File.open(File.join(path, lang + ".svg"), 'wb') do |file|
  286. file.write(image)
  287. end
  288. #Create DB entry as usual
  289. $db.execute("INSERT INTO image (station, patternID, imageID, language, label) VALUES (?,?,?,?,?)", [@r[0], @r[1], id, lang, url])
  290. end
  291. end
  292. class ReorderImages < Riddl::Implementation
  293. def response
  294. #remove brackets
  295. iter = @p[0].value.read.chop
  296. iter[0] =""
  297. iter = iter.split(",").map(&:to_i)
  298. i = 0
  299. while i < iter.length do
  300. if(i != iter[i])
  301. #swap
  302. path = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], i.to_s)
  303. path2 = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], iter[i].to_s)
  304. tmp = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1])
  305. FileUtils.mv(path + "/" + @r[4] + ".svg", tmp)
  306. FileUtils.mv(path2 + "/" + @r[4] + ".svg", path)
  307. FileUtils.mv(tmp + "/" + @r[4] + ".svg", path2)
  308. #DB Swap
  309. result = $db.execute "SELECT MAX(imageID) FROM image"
  310. if(result[0][0] == nil)
  311. maxImgId = 0
  312. else
  313. maxImgId = result[0][0] +1
  314. end
  315. #Wegen 999999 fragen
  316. #nutze max ID+1 zum tauschen
  317. $db.execute("UPDATE image SET imageID = ? WHERE station = ? AND patternID = ? AND imageID = ? AND language = ?", [maxImgId, @r[0], @r[1], i, @r[4]])
  318. $db.execute("UPDATE image SET imageID = ? WHERE station = ? AND patternID = ? AND imageID = ? AND language = ?", [i, @r[0], @r[1], iter[i], @r[4]])
  319. $db.execute("UPDATE image SET imageID = ? WHERE station = ? AND patternID = ? AND imageID = ? AND language = ?", [iter[i], @r[0], @r[1], maxImgId, @r[4]])
  320. iter.map! do |item|
  321. if(item == i)
  322. iter[i]
  323. else
  324. item
  325. end
  326. end
  327. end
  328. i +=1
  329. end
  330. end
  331. end
  332. class GetImage < Riddl::Implementation
  333. def self::prepare(station, pattern, imageID)
  334. result = $db.execute "SELECT * FROM image WHERE station = #{station} and patternID = #{pattern} and imageID = #{imageID}"
  335. Nokogiri::XML::Builder.new do |xml|
  336. xml.image(:id => imageID){
  337. result.each do |row|
  338. xml.variant(:lang => row[3], :label => row[4]){
  339. xml.text("https://centurio.work/customers/evva/was/images/uploads/#{station}/#{pattern}/#{imageID}/#{row[3]}.svg")
  340. }
  341. end
  342. }
  343. end
  344. end
  345. def response
  346. builder = GetImage::prepare(@r[0], @r[1], @r[3])
  347. Riddl::Parameter::Complex.new('image','application/xml',builder.to_xml)
  348. end
  349. end
  350. class UpdateImageLabel < Riddl::Implementation
  351. def response
  352. $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]])
  353. end
  354. end
  355. class GetRealImage < Riddl::Implementation
  356. def response
  357. #split on "." and tacke [0] to allow for e.g. de-at.svg
  358. #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
  359. #https://centurio.work/customers/evva/was/server/0/0/images/3/de-at?video=xyz
  360. img = File.read(File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], @r[3], @r[4].split(".")[0] + ".svg"))
  361. if false #Currently replacements are done on the client side
  362. if(@p[0].nil?)
  363. puts "Undefined p"
  364. else
  365. if(@p[0].name == "video" && @p[0].value)
  366. xml = Nokogiri.parse img
  367. puts "Width " + xml.xpath("string(//xmlns:image/@width)")
  368. puts "Height " + xml.xpath("string(//xmlns:image/@height)")
  369. #puts "Posi " + xml.xpath("string(//xmlns:clipPath/path/@height)")
  370. puts "Posi " + xml.xpath("string(//xmlns:image/following-sibling::clipPath/@id)")
  371. puts xml.xpath("string(//xmlns:text[starts-with(text(), 'url')])").sub("url=", "")
  372. # text
  373. img = img.sub! "</svg>", '
  374. <g><g transform="translate(562,288),scale(1.35185 1.35185)">
  375. <foreignObject width="480" height="270">
  376. <video width="480" height="270" controls="" autoplay="autoplay" muted="muted" xmlns="http://www.w3.org/1999/xhtml" >
  377. <source src="' + xml.xpath("string(//xmlns:text[starts-with(text(), 'url')])").sub("url=", "") +'" type="video/mp4"/>
  378. </video>
  379. </foreignObject>
  380. </g></g></svg>'
  381. else
  382. @p.each do |item|
  383. unless item.name.nil? || item.value.nil?
  384. img.sub! item.name, item.value
  385. end
  386. end
  387. end
  388. end
  389. end
  390. Riddl::Parameter::Complex.new('theRealImage','image/svg+xml',img)
  391. end
  392. end
  393. class DeleteImage < Riddl::Implementation
  394. def response
  395. #puts "Delete Image"
  396. #puts File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], @r[3] , @r[4] + ".svg")
  397. 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"))
  398. $db.execute("DELETE FROM image WHERE station = ? AND patternID = ? AND imageID = ? AND language = ?", [@r[0], @r[1], @r[3], @r[4]])
  399. result = $db.execute "SELECT MAX(imageID) FROM image WHERE station = #{@r[0]} and patternID = #{@r[1]} and language = '#{@r[4]}'"
  400. if(result[0][0] == nil)
  401. id = 0
  402. else
  403. id = result[0][0] +1
  404. end
  405. #reorder if !end gets deleted
  406. if(id == @r[3])
  407. return
  408. else
  409. cur = @r[3].to_i + 1
  410. prev = @r[3].to_i
  411. while cur < id.to_i do
  412. $db.execute("UPDATE image SET imageID = ? WHERE station = ? AND patternID = ? AND imageID = ? AND language = ?", [prev, @r[0], @r[1], cur, @r[4]])
  413. src = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], cur.to_s, @r[4] + ".svg")
  414. target = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], prev.to_s, @r[4] + ".svg")
  415. FileUtils.mv(src, target)
  416. cur+=1
  417. prev += 1
  418. end
  419. end
  420. end
  421. end
  422. class ListSearch < Riddl::Implementation
  423. def response
  424. ret = {
  425. :patternID => "/patternID",
  426. :multi => "/multi",
  427. :juergen => "/juergen"
  428. }
  429. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  430. end
  431. end
  432. class SearchPattern < Riddl::Implementation
  433. def response
  434. iter = @p[0].value.split(".")
  435. searchstring = ""
  436. count = 0
  437. iter.each do |item|
  438. if !item.nil?
  439. if item.to_s != "*"
  440. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  441. end
  442. count += 1
  443. end
  444. end
  445. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  446. if result.length > 0
  447. ret = {
  448. :amount => result.length.to_s,
  449. :ids => result
  450. }
  451. end
  452. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  453. #puts the first found string
  454. #result = $db.execute "SELECT P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10 FROM station WHERE station = 0" + searchstring
  455. #puts "found" + result.length.to_s
  456. #result[0].each do |item|
  457. # if !item.nil?
  458. # puts item + "."
  459. # end
  460. #end
  461. end
  462. end
  463. class SearchImages < Riddl::Implementation
  464. def response
  465. iter = @p[0].value.split(".")
  466. searchstring = ""
  467. count = 0
  468. #search = K.*.9.*
  469. #match: = K.5.9.7
  470. #match: = K.5.9.*
  471. #match: = K.*.*.*
  472. #no match: = K.*.8.*
  473. iter.each do |item|
  474. if !item.nil?
  475. if item.to_s != "*"
  476. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  477. else
  478. searchstring = searchstring + " and (P" + count.to_s + "!=''" + ")"
  479. end
  480. count += 1
  481. end
  482. end
  483. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  484. ret = []
  485. if result.length > 0
  486. result.each do |item|
  487. result2 = $db.execute "SELECT DISTINCT station, patternID, imageID FROM image WHERE station = #{@r[0]} and patternID = #{item[0]}"
  488. result2.each do |item2|
  489. ret << item2[0].to_s + "/" + item2[1].to_s + "/" + item2[2].to_s
  490. end
  491. end
  492. end
  493. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  494. end
  495. end
  496. class SearchImagesSingle < Riddl::Implementation
  497. def response
  498. iter = @p[0].value.split(".")
  499. searchstring = ""
  500. count = 0
  501. iter.each do |item|
  502. if !item.nil?
  503. if item.to_s != "*"
  504. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  505. else
  506. searchstring = searchstring + " and (P" + count.to_s + "!=''" + ")"
  507. end
  508. count += 1
  509. end
  510. end
  511. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  512. count = 0
  513. pattern = 0
  514. image = 0
  515. if result.length > 0
  516. result.each do |item|
  517. result2 = $db.execute "SELECT DISTINCT station, patternID, imageID FROM image WHERE station = #{@r[0]} and patternID = #{item[0]}"
  518. result2.each do |item2|
  519. if(count == @r[3].to_i)
  520. puts "Found "
  521. pattern = item2[1]
  522. image = item2[2]
  523. end
  524. count += 1
  525. end
  526. end
  527. end
  528. img = File.read(File.join(File.dirname(__dir__),'images/uploads', @r[0].to_s, pattern.to_s, image.to_s, "de-at.svg"))
  529. Riddl::Parameter::Complex.new('theRealImage','image/svg+xml',img)
  530. end
  531. end
  532. class SearchImages2 < Riddl::Implementation
  533. def response
  534. iter = @p[0].value.split(".")
  535. searchstring = ""
  536. count = 0
  537. #search = K.*.9.*
  538. #match: = K.5.9.7
  539. #match: = K.5.9.*
  540. #match: = K.*.*.*
  541. #no match: = K.*.8.*
  542. iter.each do |item|
  543. if !item.nil?
  544. if item.to_s != "*"
  545. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  546. else
  547. searchstring = searchstring + " and (P" + count.to_s + "!=''" + ")"
  548. end
  549. count += 1
  550. end
  551. end
  552. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  553. ret = []
  554. if result.length > 0
  555. result.each do |item|
  556. result2 = $db.execute "SELECT DISTINCT station, patternID, imageID FROM image WHERE station = #{@r[0]} and patternID = #{item[0]}"
  557. result2.each do |item2|
  558. ret << item2[0].to_s + "/" + item2[1].to_s + "/" + item2[2].to_s
  559. end
  560. end
  561. end
  562. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  563. end
  564. end
  565. class SearchImages2Single < Riddl::Implementation
  566. def response
  567. iter = @p[0].value.split(".")
  568. searchstring = ""
  569. count = 0
  570. iter.each do |item|
  571. if !item.nil?
  572. if item.to_s != "*"
  573. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  574. else
  575. searchstring = searchstring + " and (P" + count.to_s + "!=''" + ")"
  576. end
  577. count += 1
  578. end
  579. end
  580. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  581. count = 0
  582. pattern = 0
  583. image = 0
  584. if result.length > 0
  585. result.each do |item|
  586. result2 = $db.execute "SELECT DISTINCT station, patternID, imageID FROM image WHERE station = #{@r[0]} and patternID = #{item[0]}"
  587. result2.each do |item2|
  588. if(count == @r[3].to_i)
  589. puts "Found "
  590. pattern = item2[1]
  591. image = item2[2]
  592. end
  593. count += 1
  594. end
  595. end
  596. end
  597. #https://centurio.work/customers/evva/was/ui/imageReplacement.php?___image___=8/23/0/de-at.svg
  598. img = File.read(File.join(File.dirname(__dir__),'images/uploads', @r[0].to_s, pattern.to_s, image.to_s, "de-at.svg"))
  599. uri = "https://centurio.work/customers/evva/was/ui/imageReplacement.php?___image___=" + @r[0].to_s + "/" + pattern.to_s + "/" + image.to_s + "/de-at.svg"
  600. string = '<html><body style="margin: 0"><iframe style="position: absolute; height: 100%; width: 100%; border: none" src="' + uri +'" title="description"></body></html>'
  601. Riddl::Parameter::Complex.new('html','text/html',string)
  602. end
  603. end
  604. class SearchImages3Single < Riddl::Implementation
  605. def response
  606. iter = @p[0].value.split(".")
  607. searchstring = ""
  608. count = 0
  609. iter.each do |item|
  610. if !item.nil?
  611. if item.to_s != "*"
  612. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  613. else
  614. searchstring = searchstring + " and (P" + count.to_s + "!=''" + ")"
  615. end
  616. count += 1
  617. end
  618. end
  619. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  620. count = 0
  621. pattern = 0
  622. image = 0
  623. builder = Nokogiri::XML::Builder.new do |xml|
  624. xml.image(:id => @r[3].to_s){
  625. if result.length > 0
  626. result.each do |item|
  627. result2 = $db.execute "SELECT DISTINCT station, patternID, imageID FROM image WHERE station = #{@r[0]} and patternID = #{item[0]}"
  628. result2.each do |item2|
  629. if(count == @r[3].to_i)
  630. result3 = $db.execute "SELECT DISTINCT station, patternID, imageID, language, label FROM image WHERE station = #{@r[0]} and patternID = #{item[0]} and imageID =#{item2[2]}"
  631. result3.each do |item3|
  632. 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"
  633. @p.each_with_index do |item, index|
  634. if index != 0
  635. uri += "&" + item.name.to_s + "=" + item.value.to_s
  636. end
  637. end
  638. xml.variant(:lang => item3[3].to_s, :label => item3[4].to_s){
  639. xml.text(uri)
  640. }
  641. end
  642. end
  643. count += 1
  644. end
  645. end
  646. end
  647. }
  648. end
  649. #https://centurio.work/customers/evva/was/ui/imageReplacement.php?___image___=8/23/0/de-at.svg
  650. #puts builder.to_xml
  651. Riddl::Parameter::Complex.new('image','application/xml',builder.to_xml)
  652. end
  653. end
  654. class SearchErrors < Riddl::Implementation
  655. def response
  656. iter = @p[0].value.split(".")
  657. searchstring = ""
  658. count = 0
  659. #search = K.*.9.*
  660. #match: = K.5.9.7
  661. #match: = K.5.9.*
  662. #match: = K.*.*.*
  663. #no match: = K.*.8.*
  664. iter.each do |item|
  665. if !item.nil?
  666. if item.to_s != "*"
  667. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  668. else
  669. searchstring = searchstring + " and (P" + count.to_s + "!=''" + ")"
  670. end
  671. count += 1
  672. end
  673. end
  674. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  675. ret = []
  676. if result.length > 0
  677. result.each do |item|
  678. result2 = $db.execute "SELECT DISTINCT error FROM error WHERE station = #{@r[0]} and patternID = #{item[0]}"
  679. result2.each do |item2|
  680. ret << item2[0].to_s
  681. end
  682. end
  683. end
  684. ret = ret.uniq
  685. builder = Nokogiri::XML::Builder.new do |xml|
  686. xml.reason{
  687. if result.length > 0
  688. ret.each do |item|
  689. xml.reason(:lang => "de-at"){
  690. xml.text(item)
  691. }
  692. end
  693. end
  694. }
  695. end
  696. Riddl::Parameter::Complex.new('errors','application/xml',builder.to_xml)
  697. end
  698. end
  699. class SearchImagesReverse < Riddl::Implementation
  700. def response
  701. iter = @p[0].value.split(".")
  702. searchstring = ""
  703. count = 0
  704. #search = K.*.9.*
  705. #match: = K.5.9.7
  706. #match: = K.5.9.*
  707. #match: = K.*.*.*
  708. #no match: = K.*.8.*
  709. iter.each do |item|
  710. if !item.nil?
  711. if item.to_s != "*"
  712. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  713. else
  714. searchstring = searchstring + " and (P" + count.to_s + "!=''" + ")"
  715. end
  716. count += 1
  717. end
  718. end
  719. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  720. ret = []
  721. if result.length > 0
  722. result.each do |item|
  723. result2 = $db.execute "SELECT DISTINCT station, patternID, imageID FROM image WHERE station = #{@r[0]} and patternID = #{item[0]}"
  724. result2.each do |item2|
  725. ret << item2[0].to_s + "/" + item2[1].to_s + "/" + item2[2].to_s
  726. end
  727. end
  728. end
  729. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  730. end
  731. end
  732. def createDB(opts)
  733. $db.execute("PRAGMA foreign_keys=ON");
  734. $db.execute "CREATE TABLE IF NOT EXISTS stations(station INT, name VARCHAR(20), PRIMARY KEY(station))"
  735. opts[:appconf]["stations"].each { |n|
  736. CreateStation::createDB(n)
  737. }
  738. # Unique pattern
  739. # $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))"
  740. $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))"
  741. $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)"
  742. $db.execute "CREATE TABLE IF NOT EXISTS replacements(station INT, patternID INT, abbreviation TEXT, url TEXT, FOREIGN KEY(station, patternID) REFERENCES station(station, patternID) ON DELETE CASCADE)"
  743. $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))"
  744. # $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)"
  745. # $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))"
  746. end
  747. server = Riddl::Server.new(File.join(__dir__,'/was.xml'), :host => 'localhost') do |opts|
  748. accessible_description true
  749. cross_site_xhr true
  750. createDB(opts)
  751. # opts[:appconf]
  752. on resource do
  753. run GetStations if get
  754. run CreateStation if post
  755. on resource '\d+' do
  756. run GetStation if get
  757. run CreatePattern if post 'pattern'
  758. on resource '\d+' do
  759. run GetPattern if get
  760. run UpdatePattern if put 'pattern'
  761. run DeletePattern if delete
  762. run DuplicatePattern if put 'patternID'
  763. on resource 'error' do
  764. run SaveError if put 'error'
  765. run GetError if get
  766. #should add deleteerror
  767. end
  768. on resource 'replacement' do
  769. run SaveReplacement if put 'replacement'
  770. run GetReplacement if get
  771. #should add deleteReplacement
  772. end
  773. on resource 'images' do
  774. run GetImages if get
  775. #run CreateImage if post #'image'
  776. run AddExternalImage if post 'externalImage'
  777. run UploadImage if post
  778. on resource 'reorder' do
  779. on resource do
  780. run ReorderImages if put 'orderlist'
  781. end
  782. end
  783. on resource '\d+' do
  784. run GetImage if get
  785. on resource do
  786. run UpdateImageLabel if put 'label'
  787. run GetRealImage if get
  788. run DeleteImage if delete
  789. end
  790. end
  791. end
  792. end
  793. on resource 'search' do
  794. run ListSearch if get
  795. on resource 'patternID' do
  796. run SearchPattern if get
  797. end
  798. on resource 'images' do
  799. run SearchImages if get
  800. on resource '\d+' do
  801. run SearchImagesSingle if get
  802. end
  803. end
  804. on resource 'imagesServerSide' do
  805. run SearchImages2 if get
  806. on resource '\d+' do
  807. run SearchImages2Single if get
  808. end
  809. end
  810. on resource 'imagesWIZ' do
  811. run SearchImages2 if get
  812. on resource '\d+' do
  813. run SearchImages3Single if get
  814. end
  815. end
  816. on resource 'errorsWIZ' do
  817. run SearchErrors if get
  818. end
  819. on resource 'imagesReverse' do
  820. run SearchImagesReverse if get
  821. end
  822. end
  823. end
  824. end
  825. end.loop!