was.rb 35 KB

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