4 require "bundler/setup"
5 require 'active_support/all'
15 class LsfProxySoapServer < SimpleWS
17 DB_FILE = 'notifications.db'
18 DB_TABLE_NAME = 'notifications'
21 doc = YAML::load_stream(File.open(File.expand_path("../config.yml", __FILE__)))
22 @config_soap_server = doc[0]
23 url= URI::parse(@config_soap_server['soap']['url'])
24 super(@config_soap_server['soap']['name'],
25 @config_soap_server['soap']['info'],
29 logfilepath= @config_soap_server['logger']['filename']
30 directory_check(File.dirname(logfilepath),"Logfile", false)
31 if not absolute_path?(logfilepath)
32 logfilepath= File.expand_path("../"+logfilepath, __FILE__)
34 logfile = File.open(logfilepath, File::WRONLY|File::APPEND|File::CREAT, 0600)
36 @log = Logger.new(logfile,5,100.kilobytes)
37 @log.formatter = proc do |severity, datetime, progname, msg|
38 "[%s%6s] %s\n" % [datetime.to_s(:db), severity, msg.dump]
40 @log.level = eval @config_soap_server['logger']['level']
42 # notifications database
43 if File.exist? DB_FILE
44 @db = SQLite3::Database.open DB_FILE
46 @db = SQLite3::Database.new DB_FILE
48 @db.execute "CREATE TABLE IF NOT EXISTS #{DB_TABLE_NAME}(Id INTEGER PRIMARY KEY,
49 obj_name TEXT, obj_id TEXT, proto_id TEXT)"
52 @log.info("Stopping server..")
56 self.serve :lsfPushData, %w(objectName objectId protocolId),
57 :objectName => :string,
59 :protocolId => :string,
61 self.serve :listNotifications
62 self.serve :fetchNotification
63 self.serve :deleteNotification
66 self.serve :getDataXML, %w(xmlParams), :xmlParams => :string, :return => :string
68 wsdl_filename_path= @config_soap_server['soap']['wsdl_filename']
69 directory_check(File.dirname(wsdl_filename_path),"WSDL-File")
70 if not absolute_path?(wsdl_filename_path)
71 wsdl_filename_path= File.expand_path("../" + wsdl_filename_path, __FILE__)
73 @log.info "WSDL path: " + wsdl_filename_path
74 self.wsdl wsdl_filename_path
76 File.open("server.pid", "w") do |f|
77 f.puts Process.pid.to_s
79 @log.info "LSF-SOAP-Server started (PID: #{Process.pid})"
82 def lsfPushData(obj_name, obj_id, proto_id)
83 puts "#{obj_name}, #{obj_id}. #{proto_id}"
84 @db.execute "INSERT INTO #{DB_TABLE_NAME}(obj_name,obj_id,proto_id) VALUES ('#{obj_name}', '#{obj_id}', '#{proto_id}')"
85 @log.info "new notification: object_name:#{obj_name}, object_id:#{obj_id}, protocol_id:#{proto_id}"
89 def getDataXML(xml_params)
90 xml_doc= Nokogiri::XML(xml_params)
91 case xml_doc.css('SOAPDataService > general > object').text
92 when @config_soap_server['lsf']['db_interface']['lecture']
93 lsf_mock_lecture(xml_doc.css('SOAPDataService > condition > LectureID').text)
94 when @config_soap_server['lsf']['db_interface']['enrollments']
95 lsf_mock_enrollments(xml_doc.css('SOAPDataService > condition > LectureID').text)
96 when @config_soap_server['lsf']['db_interface']['course_catalog']
97 lsf_mock_course_catalog(xml_doc.css('SOAPDataService > condition > TermID').text)
98 #when @config_soap_server['lsf']['db_interface']['institutions']
102 def listNotifications
103 rows= @db.execute("select * from notifications order by id ASC")
108 puts row.values_at(1,2,3).join(",")
113 def fetchNotification
114 row= @db.execute("select * from notifications order by id ASC limit 1")[0]
119 @db.execute("delete from notifications where id = #{id}")
120 row.values_at(1,2,3).join(",")
124 def deleteNotification
125 row= @db.execute("select * from notifications order by id ASC limit 1")[0]
129 @db.execute("delete from notifications where id = #{row[0]}")
143 # Test if pathname represent an absolute path or not.
144 def absolute_path?(pathname)
145 (pathname.slice(0,1)== "/") ? true : false
148 # Raise exeption when directory is not accessable/available
149 def directory_check(pathname, context=nil, logging=true)
150 if not File.directory?(pathname)
152 err_text= "#{context}: Directory #{pathname} not accessable/available."
154 err_text= "Directory #{pathname} not accessable/available."
156 @log.fatal(err_text) if logging
157 raise Exception, err_text
162 def lsf_mock_lecture(lecture_id)
165 IO.read('test_lsf_xml_files/lectures_132742.xml')
167 IO.read('test_lsf_xml_files/lectures_132743.xml')
169 IO.read('test_lsf_xml_files/lectures_132634_multiple_dates_and_groups.xml')
171 IO.read('test_lsf_xml_files/lectures_132635_multiple_dates_no_groups.xml')
177 def lsf_mock_enrollments(lecture_id)
180 IO.read('test_lsf_xml_files/enrollments_132742.xml')
182 IO.read('test_lsf_xml_files/enrollments_132743.xml')
184 IO.read('test_lsf_xml_files/enrollments_132634.xml')
186 IO.read('test_lsf_xml_files/enrollments_132634.xml')
192 def lsf_mock_course_catalog(term_id)
195 IO.read('test_lsf_xml_files/course_catalog_20131.xml')
197 IO.read('test_lsf_xml_files/course_catalog_20132.xml')
199 IO.read('test_lsf_xml_files/course_catalog_20131.xml')
208 LsfProxySoapServer.new.start
209 rescue SQLite3::Exception => e
210 puts "Error in accessing database file @{NOTIFICATIONS_DB}"
212 rescue Exception => e