was.rb 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  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. require 'uri'
  27. $db = SQLite3::Database.open 'database/stations.db'
  28. class GetJsonFiles < Riddl::Implementation
  29. def response
  30. Dir.chdir( __dir__ + '/json')
  31. ret = [];
  32. Dir.glob('*').select{ |e|
  33. ret.append(e)
  34. }
  35. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  36. end
  37. end
  38. class GetJson < Riddl::Implementation
  39. def response
  40. Dir.chdir( __dir__ + '/json')
  41. Riddl::Parameter::Complex.new('list','application/json',File.open(@r[1]).read) rescue Riddl::Parameter::Complex.new('list','application/json', '{"error":"No such file"}')
  42. end
  43. end
  44. class SaveJson < Riddl::Implementation
  45. def response
  46. Dir.chdir( __dir__ + '/json')
  47. File.write(@r.last,(@p[0].value.read))
  48. end
  49. end
  50. class GetStations < Riddl::Implementation
  51. def response
  52. result = $db.execute "SELECT * FROM stations ORDER BY station ASC"
  53. builder = Nokogiri::XML::Builder.new do |xml|
  54. xml.stations {
  55. result.each do |row|
  56. xml.station(:id => row[0]){
  57. resultstation = $db.execute "SELECT * FROM stationpattern WHERE station = #{row[0]}"
  58. resultstation.each do |row|
  59. xml.pattern(:id => row[1], :description => row[2], :changed => row[3]){
  60. xml.parts{
  61. resultpattern = $db.execute "SELECT * FROM pattern WHERE internalID = #{row[1]}"
  62. resultpattern.each do |row2|
  63. xml.send(row2[1], row2[2])
  64. end
  65. }
  66. }
  67. end
  68. }
  69. end
  70. }
  71. end
  72. #puts builder.to_xml
  73. Riddl::Parameter::Complex.new('stations','application/xml',builder.to_xml)
  74. end
  75. end
  76. class CreateStation < Riddl::Implementation
  77. def self::createDB(n)
  78. #result = $db.execute "SELECT MAX(station) FROM stations"
  79. #if(result[0][0] == nil)
  80. # $db.execute "INSERT INTO stations (station) VALUES (?)", 0
  81. #else
  82. # $db.execute "INSERT INTO stations (station) VALUES (?)", result[0][0]+1
  83. #end
  84. #$db.execute ("INSERT INTO stations (station, name) VALUES (?,?)", [n,n])
  85. $db.execute("INSERT OR IGNORE INTO stations (station, name) VALUES (?,?)", [n,n])
  86. end
  87. def response
  88. CreateStation::createDB(7)
  89. end
  90. end
  91. class GetStation < Riddl::Implementation
  92. def self::prepare(id)
  93. resultstation = $db.execute "SELECT * FROM stationpattern WHERE station = #{id}"
  94. Nokogiri::XML::Builder.new do |xml|
  95. xml.station(:id => id){
  96. resultstation.each do |row|
  97. xml.pattern(:id => row[1], :description => row[2], :changed => row[3]){
  98. xml.parts{
  99. resultpattern = $db.execute "SELECT * FROM pattern WHERE internalID = #{row[1]}"
  100. resultpattern.each do |row2|
  101. xml.send(row2[1], row2[2])
  102. end
  103. }
  104. }
  105. end
  106. }
  107. end
  108. end
  109. def response
  110. builder = GetStation::prepare(@r.last)
  111. Riddl::Parameter::Complex.new('station','application/xml',builder.to_xml)
  112. end
  113. end
  114. class CreatePattern < Riddl::Implementation
  115. def response
  116. doc = Nokogiri::XML(@p[0].value)
  117. result = $db.execute "SELECT MAX(internalID) FROM stationpattern"
  118. if(result[0][0] == nil)
  119. id = 0
  120. else
  121. id = result[0][0] +1
  122. end
  123. $db.execute("INSERT INTO stationpattern (station, internalID, description, date) VALUES (?,?,?,?)", [@r.last, id, doc.xpath("/*/@description")[0].value, doc.xpath("/*/@changed")[0].value])
  124. doc.xpath(".//parts/*").each do |node|
  125. #if(node.text != "")
  126. $db.execute("INSERT INTO pattern (internalID, type, value) VALUES (?,?,?)", [id, node.name, node.text])
  127. #end
  128. end
  129. ret = {:id => id}
  130. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  131. end
  132. end
  133. class GetPattern < Riddl::Implementation
  134. def response
  135. resultstation = $db.execute "SELECT * FROM stationpattern WHERE station = #{@r[0]} AND internalID = #{@r[1]}"
  136. builder = Nokogiri::XML::Builder.new do |xml|
  137. resultstation.each do |row|
  138. xml.pattern(:id => row[1], :description => row[2], :changed => row[3]){
  139. xml.parts{
  140. resultpattern = $db.execute "SELECT * FROM pattern WHERE internalID = #{row[1]}"
  141. resultpattern.each do |row2|
  142. xml.send(row2[1], row2[2])
  143. end
  144. }
  145. }
  146. end
  147. end
  148. Riddl::Parameter::Complex.new('pattern','application/xml',builder.to_xml)
  149. end
  150. end
  151. class UpdatePattern < Riddl::Implementation
  152. def response
  153. doc = Nokogiri::XML(@p[0].value)
  154. $db.execute("Update stationpattern SET description = ?, date = ? WHERE internalID = ?", [doc.xpath("/*/@description")[0].value, doc.xpath("/*/@changed")[0].value, @r[1]])
  155. $db.execute("DELETE FROM pattern WHERE internalID = ?", [@r[1]])
  156. doc.xpath(".//parts/*").each do |node|
  157. #if(node.text != "")
  158. $db.execute("INSERT INTO pattern (internalID, type, value) VALUES (?,?,?)", [@r[1], node.name, node.text])
  159. #end
  160. end
  161. end
  162. end
  163. class DeletePattern < Riddl::Implementation
  164. def response
  165. $db.execute("DELETE FROM stationpattern WHERE internalID = ?", [@r[1]])
  166. end
  167. end
  168. class DuplicatePattern < Riddl::Implementation
  169. def response
  170. #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.
  171. result = $db.execute "SELECT MAX(internalID) FROM stationpattern"
  172. if(result[0][0] == nil)
  173. id = 0
  174. else
  175. id = result[0][0] +1
  176. end
  177. result = $db.execute "SELECT * FROM stationpattern WHERE internalID = #{@r[1]}"
  178. result[0][1] = id;
  179. $db.execute( "INSERT INTO stationpattern values (?,?,?,?)", result[0])
  180. result = $db.execute "SELECT * FROM pattern WHERE internalID = #{@r[1]}"
  181. result.each do |row|
  182. row[0] = id;
  183. $db.execute( "INSERT INTO pattern values (?,?,?)", row)
  184. end
  185. #duplicate error
  186. #$db.execute "CREATE TABLE IF NOT EXISTS error(internalID INT, error TEXT, FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  187. result = $db.execute "SELECT * FROM error WHERE internalID = #{@r[1]}"
  188. if(result[0] != nil)
  189. result.each do |row|
  190. row[0] = id;
  191. questionmarks = ""
  192. row.length().times{questionmarks = questionmarks + ",?"}
  193. questionmarks[0] = ""
  194. $db.execute( "INSERT INTO error values (#{questionmarks})", row)
  195. end
  196. end
  197. #duplicate image db
  198. #$db.execute "CREATE TABLE IF NOT EXISTS image(internalID INT, imageID INT, language CHARACTER(20), label TEXT, FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  199. result = $db.execute "SELECT * FROM image WHERE internalID = #{@r[1]}"
  200. if(result[0] != nil)
  201. result.each do |row|
  202. row[0] = id;
  203. questionmarks = ""
  204. row.length().times{questionmarks = questionmarks + ",?"}
  205. questionmarks[0] = ""
  206. $db.execute( "INSERT INTO image values (#{questionmarks})", row)
  207. end
  208. end
  209. #duplicate images
  210. pathorigin = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1])
  211. pathdestination = File.join(File.dirname(__dir__),'images/uploads', @r[0], id.to_s)
  212. if (Dir.exist?(pathorigin))
  213. FileUtils.mkdir_p(pathdestination)
  214. FileUtils.copy_entry pathorigin, pathdestination
  215. end
  216. #duplicate Replacement db
  217. #$db.execute "CREATE TABLE IF NOT EXISTS replacements(internalID INT, abbreviation TEXT, url TEXT, ordering INT, FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  218. result = $db.execute "SELECT * FROM replacements WHERE internalID = #{@r[1]}"
  219. if(result[0] != nil)
  220. result.each do |row|
  221. row[0] = id;
  222. questionmarks = ""
  223. row.length().times{questionmarks = questionmarks + ",?"}
  224. questionmarks[0] = ""
  225. $db.execute( "INSERT INTO replacements values (#{questionmarks})", row)
  226. end
  227. end
  228. ret = {:id => id}
  229. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  230. end
  231. end
  232. class SaveError < Riddl::Implementation
  233. def response
  234. $db.execute "CREATE TABLE IF NOT EXISTS error(internalID INT, error TEXT, FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  235. #Delete all, afterwards insert
  236. $db.execute("DELETE FROM error WHERE internalID = ?", [@r[1]])
  237. doc = Nokogiri::XML(@p[0].value)
  238. doc.xpath(".//reason").each do |node|
  239. $db.execute("INSERT INTO error (internalID, error) VALUES (?,?)", [@r[1], node.text])
  240. end
  241. end
  242. end
  243. class GetError < Riddl::Implementation
  244. def response
  245. result = $db.execute "SELECT * FROM error WHERE internalID = #{@r[1]}"
  246. builder = Nokogiri::XML::Builder.new do |xml|
  247. xml.error(){
  248. result.each do |row|
  249. xml.reason row[1]
  250. end
  251. }
  252. end
  253. Riddl::Parameter::Complex.new('error','application/xml',builder.to_xml)
  254. end
  255. end
  256. class SaveReplacement < Riddl::Implementation
  257. def response
  258. # $db.execute "CREATE TABLE IF NOT EXISTS replacements(internalID INT, abbreviation TEXT, url TEXT, ordering INT, FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  259. #Delete all, afterwards insert
  260. $db.execute("DELETE FROM replacements WHERE internalID = ?", [@r[1]])
  261. order = 0;
  262. doc = Nokogiri::XML(@p[0].value)
  263. doc.xpath(".//item").each do |node|
  264. $db.execute("INSERT INTO replacements (internalID, abbreviation, url, ordering) VALUES (?,?,?,?)", [@r[1], node.xpath(".//abbreviation").text, node.xpath(".//url").text, order])
  265. order += 1
  266. end
  267. end
  268. end
  269. class GetReplacement < Riddl::Implementation
  270. def response
  271. result = $db.execute "SELECT * FROM replacements WHERE internalID = #{@r[1]} ORDER BY ordering ASC"
  272. builder = Nokogiri::XML::Builder.new do |xml|
  273. xml.replacement(){
  274. result.each do |row|
  275. xml.item(){
  276. xml.abbreviation row[1]
  277. xml.url row[2]
  278. }
  279. end
  280. }
  281. end
  282. Riddl::Parameter::Complex.new('replacement','application/xml',builder.to_xml)
  283. end
  284. end
  285. class GetImages < Riddl::Implementation
  286. def response
  287. formatted_no_decl = Nokogiri::XML::Node::SaveOptions::FORMAT +
  288. Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
  289. #$db.execute "CREATE TABLE IF NOT EXISTS image(internalID INT, imageID INT, language CHARACTER(20), label TEXT, FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  290. result = $db.execute "SELECT DISTINCT imageID FROM image WHERE internalID = #{@r[1]} ORDER BY imageID ASC"
  291. builder = Nokogiri::XML::Builder.new do |xml|
  292. xml.images(){
  293. result.each do |row|
  294. xml<< GetImage::prepare(@r[0], @r[1], row[0], @a[0][:appconf]["wasurl"]).to_xml( save_with:formatted_no_decl )
  295. end
  296. }
  297. end
  298. Riddl::Parameter::Complex.new('images','application/xml',builder.to_xml)
  299. end
  300. end
  301. class UploadImage < Riddl::Implementation
  302. def response
  303. lang = @p[@p.length-1].value
  304. #$db.execute "CREATE TABLE IF NOT EXISTS image(internalID INT, imageID INT, language CHARACTER(20), label TEXT, FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  305. result = $db.execute "SELECT MAX(imageID) FROM image WHERE internalID = #{@r[1]} and language = '#{lang}'"
  306. if(result[0][0] == nil)
  307. id = 0
  308. else
  309. id = result[0][0] +1
  310. end
  311. #puts JSON.pretty_generate(@p)
  312. i = 0
  313. while i < @p.length-1 do
  314. item = @p[i]
  315. if(item != nil && item.name == "files[]")
  316. path = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], id.to_s)
  317. FileUtils.mkdir_p(path)
  318. #juergen nach alternative fragen
  319. readFile = File.read(item.value.inspect.to_s[/Tempfile:(.*?)>/m, 1])
  320. File.open(File.join(path, lang + ".svg"), 'wb') do |file|
  321. file.write(readFile.to_s)
  322. end
  323. $db.execute("INSERT INTO image (internalID, imageID, language, label) VALUES (?,?,?,?)", [@r[1], id, lang, "Label"])
  324. id += 1
  325. end
  326. i +=1
  327. end
  328. end
  329. end
  330. class AddExternalImage < Riddl::Implementation
  331. def response
  332. doc = Nokogiri::XML(@p[0].value)
  333. url = doc.xpath("/externalImage/url").text
  334. lang = doc.xpath("/externalImage/lang").text
  335. result = $db.execute "SELECT MAX(imageID) FROM image WHERE internalID = #{@r[1]} and language = '#{lang}'"
  336. if(result[0][0] == nil)
  337. id = 0
  338. else
  339. id = result[0][0] +1
  340. end
  341. #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
  342. 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%"/>
  343. </svg>'
  344. path = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], id.to_s)
  345. FileUtils.mkdir_p(path)
  346. File.open(File.join(path, lang + ".svg"), 'wb') do |file|
  347. file.write(image)
  348. end
  349. #Create DB entry as usual
  350. $db.execute("INSERT INTO image (internalID, imageID, language, label) VALUES (?,?,?,?)", [@r[1], id, lang, url])
  351. end
  352. end
  353. class ReorderImages < Riddl::Implementation
  354. def response
  355. #remove brackets
  356. iter = @p[0].value.read.chop
  357. iter[0] =""
  358. iter = iter.split(",").map(&:to_i)
  359. i = 0
  360. while i < iter.length do
  361. if(i != iter[i])
  362. #swap
  363. path = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], i.to_s)
  364. path2 = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], iter[i].to_s)
  365. tmp = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1])
  366. FileUtils.mv(path + "/" + @r[4] + ".svg", tmp)
  367. FileUtils.mv(path2 + "/" + @r[4] + ".svg", path)
  368. FileUtils.mv(tmp + "/" + @r[4] + ".svg", path2)
  369. #DB Swap
  370. result = $db.execute "SELECT MAX(imageID) FROM image"
  371. if(result[0][0] == nil)
  372. maxImgId = 0
  373. else
  374. maxImgId = result[0][0] +1
  375. end
  376. #Wegen 999999 fragen
  377. #nutze max ID+1 zum tauschen
  378. $db.execute("UPDATE image SET imageID = ? WHERE internalID = ? AND imageID = ? AND language = ?", [maxImgId, @r[1], i, @r[4]])
  379. $db.execute("UPDATE image SET imageID = ? WHERE internalID = ? AND imageID = ? AND language = ?", [i, @r[1], iter[i], @r[4]])
  380. $db.execute("UPDATE image SET imageID = ? WHERE internalID = ? AND imageID = ? AND language = ?", [iter[i], @r[1], maxImgId, @r[4]])
  381. iter.map! do |item|
  382. if(item == i)
  383. iter[i]
  384. else
  385. item
  386. end
  387. end
  388. end
  389. i +=1
  390. end
  391. end
  392. end
  393. class GetImage < Riddl::Implementation
  394. def self::prepare(station, pattern, imageID, wasurl)
  395. result = $db.execute "SELECT * FROM image WHERE internalID = #{pattern} and imageID = #{imageID}"
  396. Nokogiri::XML::Builder.new do |xml|
  397. xml.image(:id => imageID){
  398. result.each do |row|
  399. xml.variant(:lang => row[2], :label => row[3]){
  400. xml.text(wasurl + "images/uploads/#{station}/#{pattern}/#{imageID}/#{row[3]}.svg")
  401. }
  402. end
  403. }
  404. end
  405. end
  406. def response
  407. builder = GetImage::prepare(@r[0], @r[1], @r[3], @a[0][:appconf]["wasurl"])
  408. Riddl::Parameter::Complex.new('image','application/xml',builder.to_xml)
  409. end
  410. end
  411. class UpdateImageLabel < Riddl::Implementation
  412. def response
  413. $db.execute("UPDATE image SET label = ? WHERE internalID = ? AND imageID = ? AND language = ?", [@p[0].value, @r[1], @r[3], @r[4]])
  414. end
  415. end
  416. #Not used, prototype for replacing image serverside
  417. class GetRealImage < Riddl::Implementation
  418. def response
  419. #split on "." and tacke [0] to allow for e.g. de-at.svg
  420. #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
  421. #https://centurio.work/customers/evva/was/server/0/0/images/3/de-at?video=xyz
  422. img = File.read(File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], @r[3], @r[4].split(".")[0] + ".svg"))
  423. if false #Currently replacements are done on the client side
  424. if(@p[0].nil?)
  425. #puts "Undefined p"
  426. else
  427. if(@p[0].name == "video" && @p[0].value)
  428. xml = Nokogiri.parse img
  429. #puts "Width " + xml.xpath("string(//xmlns:image/@width)")
  430. #puts "Height " + xml.xpath("string(//xmlns:image/@height)")
  431. #puts "Posi " + xml.xpath("string(//xmlns:clipPath/path/@height)")
  432. #puts "Posi " + xml.xpath("string(//xmlns:image/following-sibling::clipPath/@id)")
  433. #puts xml.xpath("string(//xmlns:text[starts-with(text(), 'url')])").sub("url=", "")
  434. # text
  435. img = img.sub! "</svg>", '
  436. <g><g transform="translate(562,288),scale(1.35185 1.35185)">
  437. <foreignObject width="480" height="270">
  438. <video width="480" height="270" controls="" autoplay="autoplay" muted="muted" xmlns="http://www.w3.org/1999/xhtml" >
  439. <source src="' + xml.xpath("string(//xmlns:text[starts-with(text(), 'url')])").sub("url=", "") +'" type="video/mp4"/>
  440. </video>
  441. </foreignObject>
  442. </g></g></svg>'
  443. else
  444. @p.each do |item|
  445. unless item.name.nil? || item.value.nil?
  446. img.sub! item.name, item.value
  447. end
  448. end
  449. end
  450. end
  451. end
  452. Riddl::Parameter::Complex.new('theRealImage','image/svg+xml',img)
  453. end
  454. end
  455. class DeleteImage < Riddl::Implementation
  456. def response
  457. 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"))
  458. $db.execute("DELETE FROM image WHERE internalID = ? AND imageID = ? AND language = ?", [@r[1], @r[3], @r[4]])
  459. result = $db.execute "SELECT MAX(imageID) FROM image WHERE internalID = #{@r[1]} and language = '#{@r[4]}'"
  460. if(result[0][0] == nil)
  461. id = 0
  462. else
  463. id = result[0][0] +1
  464. end
  465. #reorder if !end gets deleted
  466. if(id == @r[3])
  467. return
  468. else
  469. cur = @r[3].to_i + 1
  470. prev = @r[3].to_i
  471. while cur < id.to_i do
  472. $db.execute("UPDATE image SET imageID = ? WHERE internalID = ? AND imageID = ? AND language = ?", [prev, @r[1], cur, @r[4]])
  473. src = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], cur.to_s, @r[4] + ".svg")
  474. target = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], prev.to_s, @r[4] + ".svg")
  475. FileUtils.mv(src, target)
  476. cur+=1
  477. prev += 1
  478. end
  479. end
  480. end
  481. end
  482. class ListSearch < Riddl::Implementation
  483. def response
  484. ret = {
  485. :imagesWIZAll => "/imagesWIZ?pattern=...",
  486. :imagesWIZSingle => "/imagesWIZ/0..*/?pattern=...",
  487. :errorsWIZ => "/errorsWIZ/"
  488. }
  489. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  490. end
  491. end
  492. class SearchImages < Riddl::Implementation
  493. def response
  494. #https://centurio.work/customers/evva/was/ui/TransformProductCode.php?ProductCode=E.A.PZ.DZ-S.31.31.MP.SEP
  495. pattern = JSON.parse(Net::HTTP.get URI(@a[0][:appconf]["wasurl"] + "ui/TransformProductCode.php?ProductCode=" + @p[0].value))
  496. #Get first key dynamicly and then build sql query
  497. searchstring = "SELECT " + pattern.keys[0] + ".internalID AS ID"
  498. searchstring = searchstring + " FROM " + pattern.map{ |key, val| "pattern " + key}.join(', ')
  499. searchstring = searchstring + " WHERE " + pattern.map{ |key, val| pattern.keys[0] + ".internalID == " + key + ".internalID"}.join(' AND ')
  500. searchstring = searchstring + " AND " + pattern.map{ |key, val| key + ".type == '" + key + "' AND (" + key + ".value == '"+ val +"' or " + key + ".value == '*')"}.join(' AND ') + " ORDER BY ID ASC"
  501. result = $db.execute searchstring;
  502. #puts searchstring
  503. #Search for images with one or multiple found patternIDs
  504. ret = []
  505. if result.length > 0
  506. result.each do |item|
  507. result2 = $db.execute "SELECT DISTINCT internalID, imageID FROM image WHERE internalID = #{item[0]} ORDER BY imageID ASC"
  508. result2.each do |item2|
  509. result3 = $db.execute "SELECT DISTINCT station FROM stationpattern WHERE internalID = #{item2[0]} AND station = #{@r[0]}"
  510. if(result3[0] != nil)
  511. ret << @r[0] + "/" + item2[0].to_s + "/" + item2[1].to_s
  512. end
  513. end
  514. end
  515. end
  516. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  517. end
  518. end
  519. class SearchImagesSingle < Riddl::Implementation
  520. def response
  521. pattern = JSON.parse(Net::HTTP.get URI(@a[0][:appconf]["wasurl"] + "ui/TransformProductCode.php?ProductCode=" + @p[0].value))
  522. searchstring = "SELECT " + pattern.keys[0] + ".internalID AS ID"
  523. searchstring = searchstring + " FROM " + pattern.map{ |key, val| "pattern " + key}.join(', ')
  524. searchstring = searchstring + " WHERE " + pattern.map{ |key, val| pattern.keys[0] + ".internalID == " + key + ".internalID"}.join(' AND ')
  525. searchstring = searchstring + " AND " + pattern.map{ |key, val| key + ".type == '" + key + "' AND (" + key + ".value == '"+ val +"' or " + key + ".value == '*')"}.join(' AND ') + " ORDER BY ID ASC"
  526. result = $db.execute searchstring;
  527. count = 0
  528. pattern = 0
  529. image = 0
  530. builder = Nokogiri::XML::Builder.new do |xml|
  531. xml.image(:id => @r[3].to_s){
  532. if result.length > 0
  533. result.each do |item|
  534. result2 = $db.execute "SELECT DISTINCT internalID, imageID FROM image WHERE internalID = #{item[0]} ORDER BY imageID ASC"
  535. result2.each do |item2|
  536. result3 = $db.execute "SELECT DISTINCT internalID, imageID, language, label FROM image WHERE internalID = #{item[0]} and imageID =#{item2[1]}"
  537. result3.each do |item3|
  538. result4 = $db.execute "SELECT DISTINCT station FROM stationpattern WHERE internalID = #{item3[0]} AND station = #{@r[0]}"
  539. if(result4[0] != nil)
  540. if(count == @r[3].to_i)
  541. #ret << @r[0] + "/" + item2[0].to_s + "/" + item2[1].to_s
  542. uri = @a[0][:appconf]["wasurl"] + "ui/imageReplacement.php?___image___=" + @r[0].to_s + "/" + item3[0].to_s + "/" + item3[1].to_s + "/" + item3[2].to_s + ".svg"
  543. @p.each_with_index do |item, index|
  544. if index != 0
  545. uri += "&" + item.name.to_s + "=" + item.value.to_s
  546. end
  547. end
  548. xml.variant(:lang => item3[2].to_s, :label => item3[3].to_s){
  549. xml.text(uri)
  550. }
  551. end
  552. count += 1
  553. end
  554. end
  555. end
  556. end
  557. end
  558. }
  559. end
  560. #https://centurio.work/customers/evva/was/ui/imageReplacement.php?___image___=8/23/0/de-at.svg
  561. #puts builder.to_xml
  562. Riddl::Parameter::Complex.new('image','application/xml',builder.to_xml)
  563. end
  564. end
  565. class SearchErrors < Riddl::Implementation
  566. def response
  567. pattern = JSON.parse(Net::HTTP.get URI(@a[0][:appconf]["wasurl"] + "ui/TransformProductCode.php?ProductCode=" + @p[0].value))
  568. searchstring = "SELECT " + pattern.keys[0] + ".internalID AS ID"
  569. searchstring = searchstring + " FROM " + pattern.map{ |key, val| "pattern " + key}.join(', ')
  570. searchstring = searchstring + " WHERE " + pattern.map{ |key, val| pattern.keys[0] + ".internalID == " + key + ".internalID"}.join(' AND ')
  571. searchstring = searchstring + " AND " + pattern.map{ |key, val| key + ".type == '" + key + "' AND (" + key + ".value == '"+ val +"' or " + key + ".value == '*')"}.join(' AND ') + " ORDER BY ID ASC"
  572. result = $db.execute searchstring;
  573. ret = []
  574. if result.length > 0
  575. result.each do |item|
  576. result2 = $db.execute "SELECT DISTINCT error FROM error WHERE internalID = #{item[0]} ORDER BY internalID ASC"
  577. result2.each do |item2|
  578. ret << item2[0].to_s
  579. end
  580. end
  581. end
  582. ret = ret.uniq
  583. builder = Nokogiri::XML::Builder.new do |xml|
  584. xml.reason{
  585. if result.length > 0
  586. ret.each do |item|
  587. xml.reason(:lang => "de-at"){
  588. xml.text(item)
  589. }
  590. end
  591. end
  592. }
  593. end
  594. Riddl::Parameter::Complex.new('errors','application/xml',builder.to_xml)
  595. end
  596. end
  597. class GetAssignments < Riddl::Implementation
  598. def response
  599. ret = {}
  600. resultstation = $db.execute "SELECT * FROM assignments"
  601. resultstation.each do |row|
  602. ret[row[0]] = row[1]
  603. end
  604. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  605. end
  606. end
  607. class SaveNewAssignment < Riddl::Implementation
  608. def response
  609. form = JSON.parse(@p[0].value.read);
  610. result = $db.execute "SELECT MAX(ID) FROM assignments"
  611. if(result[0][0] == nil)
  612. id = 0
  613. else
  614. id = result[0][0] +1
  615. end
  616. #bring given pattern into correct string (initially pattern could be in different order than required)
  617. pattern = [];
  618. patternsymlink = [];
  619. fullcode = JSON.parse( File.read(File.join(File.dirname(__dir__),"server/json/ProductCode.json")))
  620. fullcode["pattern"].each do |child|
  621. pattern.push(form["parts"][child["abk"]])
  622. patternsymlink.push(child["abk"])
  623. end
  624. #Create Assignments Symlink
  625. #Station3_Ba;DZ-S_Of;MP_ID;12.jpg
  626. #img url
  627. #form["img"]
  628. #img name
  629. #pp form
  630. #pp pattern
  631. #pp patternsymlink
  632. link = "Station" + form["station"]
  633. pattern.each_with_index {|val, index|
  634. if(val != "")
  635. link = link + "_" + patternsymlink[index].to_s + ";" + val.to_s
  636. end
  637. }
  638. if(form["assignmentText"] != "")
  639. link = link + "_ID;" + form["assignmentText"].to_s
  640. end
  641. link = link + ".jpg"
  642. #pp link
  643. #pp @a[0][:appconf]["universalstorageeinzelbilder"]
  644. #File.write(File.join(@a[0][:appconf]["universalstorageeinzelbilder"]),File.open("defaultContent/js/ui.js").read)
  645. #File.symlink File.join("test.jpg"), File.join(@a[0][:appconf]["universalstorageeinzelbilder"])
  646. imgfile = File.join(form["img"])
  647. symfile = File.join(@a[0][:appconf]["universalstorageeinzelbilder"], "Symlinks", link)
  648. #pp imgfile;
  649. #pp symfile;
  650. if File.file?(imgfile) #file exists
  651. if !File.file?(symfile) #symlink should not exist
  652. nw = "" + form["img"]
  653. nw.slice! @a[0][:appconf]["universalstorageeinzelbilder"].to_s
  654. File.symlink "../" + nw, symfile
  655. end
  656. end
  657. #Create Assignemtns DB
  658. checkimage = $db.execute "SELECT * FROM assignments WHERE image = '#{form["img"]}'"
  659. #if image not stored create new assignment with pattern, otherwise only add pattern
  660. if(checkimage[0] == nil)
  661. #check if assignment would overlap
  662. if (!ListAssignmentSearch::prepare(@a[0][:appconf]["wasurl"], pattern.join('.'), form["station"], form["assignmentText"]).any?)
  663. $db.execute("INSERT INTO assignments (ID, image) VALUES (?,?)", [id, form["img"]])
  664. form["parts"].each{ |key,val|
  665. $db.execute("INSERT INTO patternassignments (ID, AssID, type, value) VALUES (?,?,?,?)", [id, 0, key, val])
  666. }
  667. $db.execute("INSERT INTO patternassignments (ID, AssID, type, value) VALUES (?,?,?,?)", [id, 0, "STATION", form["station"]])
  668. $db.execute("INSERT INTO patternassignments (ID, AssID, type, value) VALUES (?,?,?,?)", [id, 0, "TEXT", form["assignmentText"]])
  669. ret = {:id => id}
  670. end
  671. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  672. else
  673. #pp "Im Else"
  674. maxID = $db.execute "SELECT MAX(AssID) FROM patternassignments WHERE ID ='#{checkimage[0][0]}'"
  675. if(maxID[0][0] == nil)
  676. maxID = 0
  677. else
  678. maxID = maxID[0][0] +1
  679. end
  680. #Search 4 assignment if none found add assignment
  681. #pp ListAssignmentSearch::prepare(@a[0][:appconf]["wasurl"], pattern.join('.'), form["station"], form["assignmentText"])
  682. if (!ListAssignmentSearch::prepare(@a[0][:appconf]["wasurl"], pattern.join('.'), form["station"], form["assignmentText"]).any?)
  683. form["parts"].each{ |key,val|
  684. $db.execute("INSERT INTO patternassignments (ID, AssID, type, value) VALUES (?,?,?,?)", [checkimage[0][0], maxID, key, val])
  685. }
  686. $db.execute("INSERT INTO patternassignments (ID, AssID, type, value) VALUES (?,?,?,?)", [checkimage[0][0], maxID, "STATION", form["station"]])
  687. $db.execute("INSERT INTO patternassignments (ID, AssID, type, value) VALUES (?,?,?,?)", [checkimage[0][0], maxID, "TEXT", form["assignmentText"]])
  688. #pp "Added Assignment"
  689. ret = {:id => checkimage[0][0]}
  690. end
  691. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  692. end
  693. end
  694. end
  695. class CreateSymlinks < Riddl::Implementation
  696. def response
  697. pp "Creating Symlinks"
  698. #GETTING ALL Assignments
  699. ret = {}
  700. ret2 = {}
  701. resultstation = $db.execute "SELECT * FROM assignments"
  702. resultstation.each do |row|
  703. #row[0] pattern ID
  704. #row[1] pattern image
  705. #if limiting to a specific assignment ID
  706. #if row[0] != 627 #164 = battery
  707. # next
  708. #end
  709. #Getting Assignments for ID:
  710. ret2 = {}
  711. allAssIds = $db.execute "SELECT DISTINCT AssID FROM patternassignments WHERE ID = #{row[0]}"
  712. allAssIds.each do |row2|
  713. station = $db.execute "SELECT value FROM patternassignments WHERE ID = #{row[0]} AND AssID = #{row2[0]} AND type == 'STATION'"
  714. stationvalue = Integer(station[0][0])
  715. singleItem = $db.execute "SELECT type, value FROM patternassignments WHERE ID = #{row[0]} AND AssID = #{row2[0]} AND type != 'STATION'"
  716. if(ret2[stationvalue] == nil)
  717. ret2[stationvalue] = Hash.new()
  718. end
  719. ret2[stationvalue][row2[0]] = Hash.new()
  720. singleItem.each do |it|
  721. ret2[stationvalue][row2[0]][it[0]] = it[1]
  722. end
  723. link = "Station" + stationvalue.to_s
  724. ret2[stationvalue][row2[0]].each do |key, value|
  725. if(value != "")
  726. if(key != "TEXT")
  727. link = link + "_" + key.to_s + ";" + value.to_s
  728. else
  729. link = link + "_" + "ID" + ";" + value.to_s
  730. end
  731. end
  732. end
  733. link = link + ".jpg"
  734. #pp link
  735. imgfile = File.join(row[1])
  736. symfile = File.join(@a[0][:appconf]["universalstorageeinzelbilder"], "Symlinks", link)
  737. #pp "imgfile " + imgfile
  738. #pp "symfile " + symfile
  739. #Create Symlinks for each Assignment
  740. if File.file?(imgfile) #file exists
  741. #pp "File exists"
  742. if !File.symlink?(symfile) #symlink should not exist
  743. pp "Creating Symlink"
  744. nw = "" + row[1]
  745. nw.slice! @a[0][:appconf]["universalstorageeinzelbilder"].to_s
  746. #pp "../" + nw
  747. #pp symfile
  748. #pp "actual link creation";
  749. # nw = actual image, symfile = symlink location
  750. File.symlink "../" + nw, symfile
  751. else
  752. #pp "Symlink exists"
  753. end
  754. end
  755. end
  756. end
  757. #Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret2))
  758. end
  759. end
  760. class GetAssignment < Riddl::Implementation
  761. def response
  762. ret = {}
  763. allAssIds = $db.execute "SELECT DISTINCT AssID FROM patternassignments WHERE ID = #{@r[1]}"
  764. allAssIds.each do |row|
  765. station = $db.execute "SELECT value FROM patternassignments WHERE ID = #{@r[1]} AND AssID = #{row[0]} AND type == 'STATION'"
  766. stationvalue = Integer(station[0][0])
  767. #old where pattern is stored as string
  768. #singleItem = $db.execute "SELECT value FROM patternassignments WHERE ID = #{@r[1]} AND AssID = #{row[0]} AND type != 'STATION'"
  769. #if(ret[stationvalue] == nil)
  770. # ret[stationvalue] = Hash.new()
  771. #end
  772. #ret[stationvalue][row[0]] = singleItem.join('.')
  773. singleItem = $db.execute "SELECT type, value FROM patternassignments WHERE ID = #{@r[1]} AND AssID = #{row[0]} AND type != 'STATION'"
  774. if(ret[stationvalue] == nil)
  775. ret[stationvalue] = Hash.new()
  776. end
  777. ret[stationvalue][row[0]] = Hash.new()
  778. singleItem.each do |it|
  779. ret[stationvalue][row[0]][it[0]] = it[1]
  780. end
  781. end
  782. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  783. end
  784. end
  785. class RenameAssignmentImagePath < Riddl::Implementation
  786. def response
  787. $db.execute("UPDATE assignments SET image = '#{JSON.parse(@p[0].value.read)["url"]}' WHERE ID = #{@r[1]}")
  788. end
  789. end
  790. class DeleteAssignment < Riddl::Implementation
  791. def response
  792. $db.execute("DELETE FROM assignments WHERE ID = ?", [@r[1]])
  793. end
  794. end
  795. class DeleteSingleAssignment < Riddl::Implementation
  796. def response
  797. $db.execute("DELETE FROM patternassignments WHERE AssID = ? AND ID = ?", [@r[2], @r[1]])
  798. end
  799. end
  800. class ListAssignmentSearch < Riddl::Implementation
  801. def self::prepare(appconf, pat, pat_station, pat_value)
  802. #https://centurio.work/customers/evva/was/ui/TransformProductCode.php?ProductCode=E.A.PZ.DZ-S.31.31.MP.SEP
  803. pattern = JSON.parse(Net::HTTP.get URI(appconf + "ui/TransformProductCode.php?ProductCode=" + pat))
  804. #remove empty entries from pattern
  805. pattern.each do |key, value|
  806. if(value == "")
  807. if(key == "Zi")
  808. elsif (key == "Kf")
  809. else
  810. pattern.delete(key)
  811. end
  812. end
  813. end
  814. emptypatternparts = []
  815. #fullcode = JSON.parse( File.read(File.join("json" , "ProductCode.json")))
  816. #puts File.join(File.dirname(__dir__),"server/json/ProductCode.json")
  817. fullcode = JSON.parse( File.read(File.join(File.dirname(__dir__),"server/json/ProductCode.json")))
  818. fullcode["pattern"].each do |key|
  819. if(!pattern.has_key?(key["abk"]) )
  820. emptypatternparts.push(key["abk"])
  821. end
  822. end
  823. #$db.execute "CREATE TABLE IF NOT EXISTS patternassignments(ID INT, AssID INT, type CHARACTER(20), value CHARACTER(20), FOREIGN KEY(ID) REFERENCES assignments(ID) ON DELETE CASCADE)"
  824. #Get first key dynamicly and then build sql query
  825. if(pattern.keys[0] != nil)
  826. searchstring = "SELECT " + pattern.keys[0] + ".ID AS ID"
  827. searchstring = searchstring + " FROM " + pattern.map{ |key, val| "patternassignments " + key}.join(', ')
  828. searchstring = searchstring + ", " + emptypatternparts.map{ |key| "patternassignments " + key}.join(', ')
  829. searchstring = searchstring + " , patternassignments STATION , patternassignments ValText"
  830. searchstring = searchstring + " WHERE " + pattern.map{ |key, val| pattern.keys[0] + ".AssID == " + key + ".AssID"}.join(' AND ');
  831. searchstring = searchstring + " AND " + pattern.map{ |key, val| pattern.keys[0] + ".ID == " + key + ".ID"}.join(' AND ')
  832. searchstring = searchstring + " AND " + emptypatternparts.map{ |key| pattern.keys[0] + ".AssID == " + key + ".AssID"}.join(' AND ')
  833. searchstring = searchstring + " AND " + emptypatternparts.map{ |key| pattern.keys[0] + ".ID == " + key + ".ID"}.join(' AND ')
  834. searchstring = searchstring + " AND " + emptypatternparts.map{ |key| key + ".type == '" + key + "' AND " + key + ".value == ''" }.join(' AND ')
  835. searchstring = searchstring + " AND " + pattern.keys[0] + ".AssID == STATION.AssID"
  836. searchstring = searchstring + " AND " + pattern.keys[0] + ".AssID == ValText.AssID"
  837. searchstring = searchstring + " AND " + pattern.keys[0] + ".ID == STATION.ID"
  838. searchstring = searchstring + " AND " + pattern.keys[0] + ".ID == ValText.ID"
  839. searchstring = searchstring + " AND STATION.type == 'STATION'" + " AND STATION.value == '" + pat_station + "'"
  840. searchstring = searchstring + " AND ValText.type == 'TEXT'" + " AND ValText.value == '" + pat_value + "'"
  841. searchstring = searchstring + " AND " + pattern.map{ |key, val|
  842. #In case a value has the option "KeineAngabe" empty values are allowed
  843. if(val.start_with?("KeineAngabe"))
  844. key + ".type == '" + key + "' AND (" + key + ".value == '"+ val +"' or " + key + ".value == '*' or " + key + ".value == '')"
  845. else
  846. key + ".type == '" + key + "' AND (" + key + ".value == '"+ val +"' or " + key + ".value == '*')"
  847. end
  848. }.join(' AND ') + " ORDER BY ID ASC"
  849. #puts searchstring;
  850. else
  851. searchstring = "SELECT * FROM "
  852. searchstring = searchstring + "patternassignments STATION , patternassignments ValText"
  853. searchstring = searchstring + " WHERE"
  854. searchstring = searchstring + " STATION.type == 'STATION'" + " AND STATION.value == '" + pat_station + "'"
  855. searchstring = searchstring + " AND ValText.type == 'TEXT'" + " AND ValText.value == '" + pat_value + "'"
  856. searchstring = searchstring + " AND ValText.AssID == STATION.AssID"
  857. searchstring = searchstring + " ORDER BY ID ASC"
  858. end
  859. #pp searchstring
  860. result = $db.execute searchstring;
  861. #count entries
  862. #result2 = $db.execute "SELECT COUNT(*) FROM patternassignments";
  863. #pp result2;
  864. #Search for images with one or multiple found patternIDs
  865. ret = []
  866. if result.length > 0
  867. result.each do |item|
  868. result2 = $db.execute "SELECT DISTINCT image FROM assignments WHERE ID = #{item[0]}"
  869. result2.each do |item2|
  870. ret << item2[0]
  871. end
  872. end
  873. end
  874. return ret
  875. end
  876. def response
  877. ret = ListAssignmentSearch::prepare(@a[0][:appconf]["wasurl"], @p[0].value, @p[1].value, @p[2].value)
  878. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  879. end
  880. end
  881. class ListAssignmentSearchMultiple < Riddl::Implementation
  882. def response
  883. #pattern station text
  884. pattern = JSON.parse(@p[0].value)
  885. station = JSON.parse(@p[1].value)
  886. text = JSON.parse(@p[2].value)
  887. ret = []
  888. pattern.each_with_index {|val, index|
  889. #puts "#{val} => #{index} : #{station[index]} : #{text[index]}"
  890. ret = ret.push(ListAssignmentSearch::prepare(@a[0][:appconf]["wasurl"], val, station[index], text[index]))
  891. }
  892. #ret = ListAssignmentSearch::prepare(@a[0][:appconf]["wasurl"], @p[0].value, @p[1].value, @p[2].value)
  893. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  894. end
  895. end
  896. def createDB(opts)
  897. $db.execute("PRAGMA foreign_keys=ON");
  898. $db.execute "CREATE TABLE IF NOT EXISTS stations(station INT, name VARCHAR(20), PRIMARY KEY(station))"
  899. opts[:appconf]["stations"].each { |n|
  900. CreateStation::createDB(n)
  901. }
  902. #vor DB struktur umstellung
  903. #$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))"
  904. #$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)"
  905. #$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)"
  906. #$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))"
  907. $db.execute "CREATE TABLE IF NOT EXISTS stationpattern(station INT, internalID INT, description TEXT, date TEXT, PRIMARY KEY(internalID), UNIQUE(internalID))"
  908. $db.execute "CREATE TABLE IF NOT EXISTS pattern(internalID INT, type CHARACTER(20), value CHARACTER(20), FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  909. $db.execute "CREATE TABLE IF NOT EXISTS error(internalID INT, error TEXT, FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  910. $db.execute "CREATE TABLE IF NOT EXISTS replacements(internalID INT, abbreviation TEXT, url TEXT, ordering INT, FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  911. $db.execute "CREATE TABLE IF NOT EXISTS image(internalID INT, imageID INT, language CHARACTER(20), label TEXT, FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  912. $db.execute "CREATE TABLE IF NOT EXISTS assignments(ID INT, image TEXT, PRIMARY KEY(ID) UNIQUE (ID))"
  913. $db.execute "CREATE TABLE IF NOT EXISTS patternassignments(ID INT, AssID INT, type CHARACTER(20), value CHARACTER(20), FOREIGN KEY(ID) REFERENCES assignments(ID) ON DELETE CASCADE)"
  914. end
  915. server = Riddl::Server.new(File.join(__dir__,'/was.xml'), :host => 'localhost') do |opts|
  916. accessible_description true
  917. cross_site_xhr true
  918. createDB(opts)
  919. # opts[:appconf]
  920. on resource do
  921. run GetStations if get
  922. run CreateStation if post
  923. on resource 'cutimage' do
  924. run CutImage if get
  925. #should add deleteReplacement
  926. end
  927. on resource 'json' do
  928. run GetJsonFiles if get
  929. on resource '.*' do
  930. run SaveJson if put 'list'
  931. run GetJson if get
  932. end
  933. #should add deleteReplacement
  934. end
  935. on resource '\d+' do
  936. run GetStation if get
  937. run CreatePattern if post 'pattern'
  938. on resource '\d+' do
  939. run GetPattern if get
  940. run UpdatePattern if put 'pattern'
  941. run DeletePattern if delete
  942. run DuplicatePattern if put 'patternID'
  943. on resource 'error' do
  944. run SaveError if put 'error'
  945. run GetError if get
  946. #should add deleteerror
  947. end
  948. on resource 'replacement' do
  949. run SaveReplacement if put 'replacement'
  950. run GetReplacement if get
  951. #should add deleteReplacement
  952. end
  953. on resource 'images' do
  954. run GetImages, opts if get
  955. #run CreateImage if post #'image'
  956. run AddExternalImage if post 'externalImage'
  957. run UploadImage if post
  958. on resource 'reorder' do
  959. on resource do
  960. run ReorderImages if put 'orderlist'
  961. end
  962. end
  963. on resource '\d+' do
  964. run GetImage, opts if get
  965. on resource do
  966. run UpdateImageLabel if put 'label'
  967. run GetRealImage if get
  968. run DeleteImage if delete
  969. end
  970. end
  971. end
  972. end
  973. on resource 'search' do
  974. run ListSearch if get
  975. on resource 'imagesWIZ' do
  976. run SearchImages, opts if get
  977. on resource '\d+' do
  978. run SearchImagesSingle, opts if get
  979. end
  980. end
  981. on resource 'errorsWIZ' do
  982. run SearchErrors, opts if get
  983. end
  984. end
  985. end
  986. on resource 'assignments' do
  987. run GetAssignments if get
  988. run SaveNewAssignment, opts if post 'list'
  989. on resource 'search' do
  990. run ListAssignmentSearch, opts if get
  991. end
  992. on resource 'searchMultiple' do
  993. run ListAssignmentSearchMultiple, opts if post
  994. end
  995. on resource 'createSymlinks' do
  996. run CreateSymlinks, opts if get
  997. end
  998. on resource '\d+' do
  999. run GetAssignment if get
  1000. run RenameAssignmentImagePath if put 'list'
  1001. run DeleteAssignment if delete
  1002. on resource '\d+' do
  1003. run DeleteSingleAssignment if delete
  1004. end
  1005. end
  1006. end
  1007. end
  1008. end.loop!