Mockups for testing.
[ecp_lsfp_soaps.git] / server.rb
blobd077ac73c7c999691303d18ad017e862f4d4949f
1 #!/usr/bin/env ruby
3 require 'rubygems'
4 require "bundler/setup"
5 require 'active_support/all'
6 require 'sqlite3'
8 require 'yaml'
9 require 'simplews'
10 require 'pp'
11 require 'uri'
13 require 'nokogiri'
15 class LsfProxySoapServer < SimpleWS
17   DB_FILE = 'notifications.db'
18   DB_TABLE_NAME = 'notifications'
20   def initialize
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'],
26           url.host, url.port)
28     # initializing logger
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__)
33     end
34     logfile = File.open(logfilepath, File::WRONLY|File::APPEND|File::CREAT, 0600)
35     logfile.sync= true
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]
39     end
40     @log.level = eval @config_soap_server['logger']['level']
41     
42     # notifications database
43     if File.exist? DB_FILE
44       @db = SQLite3::Database.open DB_FILE
45     else
46       @db = SQLite3::Database.new DB_FILE
47     end
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)"
51     trap('INT'){
52       @log.info("Stopping server..")
53       self.shutdown
54     }
55   
56     self.serve :lsfPushData, %w(objectName objectId protocolId), 
57           :objectName => :string,
58           :objectId => :string,
59           :protocolId => :string,
60           :return => :string
61     self.serve :listNotifications
62     self.serve :fetchNotification
63     self.serve :deleteNotification
64     self.serve :getTime
65     self.serve :ping
66     self.serve :getDataXML, %w(xmlParams), :xmlParams => :string, :return => :string
67   
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__)
72     end
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
78     end
79     @log.info "LSF-SOAP-Server started (PID: #{Process.pid})"
80   end
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}"
86   end
88   # LSF Mockup
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']
99     end
100   end 
102   def listNotifications
103     rows= @db.execute("select * from notifications order by id ASC")
104     if rows.blank?
105       nil
106     else
107       rows.each do |row|
108         puts row.values_at(1,2,3).join(",")
109       end
110     end
111   end
113   def fetchNotification
114     row= @db.execute("select * from notifications order by id ASC limit 1")[0]
115     if row.blank?
116       nil
117     else
118       id= row[0]
119       @db.execute("delete from notifications where id = #{id}")
120       row.values_at(1,2,3).join(",")
121     end
122   end
124   def deleteNotification
125     row= @db.execute("select * from notifications order by id ASC limit 1")[0]
126     if row.blank?
127       nil
128     else
129       @db.execute("delete from notifications where id = #{row[0]}")
130     end
131   end
133   def getTime
134     Time.now
135   end
137   def ping
138     return true
139   end
141 private
143   # Test if pathname represent an absolute path or not.
144   def absolute_path?(pathname)
145     (pathname.slice(0,1)== "/") ? true : false
146   end
148   # Raise exeption when directory is not accessable/available
149   def directory_check(pathname, context=nil, logging=true)
150     if not File.directory?(pathname)
151       if context
152         err_text= "#{context}: Directory #{pathname} not accessable/available."
153       else
154         err_text= "Directory #{pathname} not accessable/available."
155       end
156       @log.fatal(err_text) if logging
157       raise Exception, err_text
158     end
159     true
160   end
162   def lsf_mock_lecture(lecture_id)
163     case lecture_id
164       when "132742"
165         IO.read('test_lsf_xml_files/lectures_132742.xml')
166       when "132743"
167         IO.read('test_lsf_xml_files/lectures_132743.xml')
168       when "132634"
169         IO.read('test_lsf_xml_files/lectures_132634_multiple_dates_and_groups.xml')
170       when "132635"
171         IO.read('test_lsf_xml_files/lectures_132635_multiple_dates_no_groups.xml')
172       else
173         "not available"
174     end
175   end
177   def lsf_mock_enrollments(lecture_id)
178     case lecture_id
179       when "132742"
180         IO.read('test_lsf_xml_files/enrollments_132742.xml')
181       when "132743"
182         IO.read('test_lsf_xml_files/enrollments_132743.xml')
183       when "132634"
184         IO.read('test_lsf_xml_files/enrollments_132634.xml')
185       when "132635"
186         IO.read('test_lsf_xml_files/enrollments_132634.xml')
187       else
188         "not available"
189     end
190   end
192   def lsf_mock_course_catalog(term_id)
193     case term_id
194       when "20131"
195         IO.read('test_lsf_xml_files/course_catalog_20131.xml')
196       when "20132"
197         IO.read('test_lsf_xml_files/course_catalog_20132.xml')
198       when "-1"
199         IO.read('test_lsf_xml_files/course_catalog_20131.xml')
200       else
201         "not available"
202     end
203   end
207 begin
208   LsfProxySoapServer.new.start
209 rescue SQLite3::Exception => e 
210   puts "Error in accessing database file @{NOTIFICATIONS_DB}"
211   puts e
212 rescue Exception => e
213   puts e
214   puts e.backtrace
215 ensure
216   db.close if $db