was.rb 35 KB

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