summaryrefslogtreecommitdiffstats
path: root/did-model.lua
diff options
context:
space:
mode:
authorTed Trask <ttrask01@yahoo.com>2010-04-06 15:15:15 +0000
committerTed Trask <ttrask01@yahoo.com>2010-04-06 15:15:15 +0000
commit9d6dc0dc33e4c7c5e9aaf096bb6b3bef9a0ae8cc (patch)
tree67358688e89d3e8ee5a989ea99662d15100a2b82 /did-model.lua
parentdfaaa00e238905388d9cbe6ef630ec15a1e17347 (diff)
downloadacf-did-9d6dc0dc33e4c7c5e9aaf096bb6b3bef9a0ae8cc.tar.bz2
acf-did-9d6dc0dc33e4c7c5e9aaf096bb6b3bef9a0ae8cc.tar.xz
Added test button to editdefinition.
Test and Cancel are now handled in customized handler in controller. Added some rule testing code to sort them into priority order.
Diffstat (limited to 'did-model.lua')
-rw-r--r--did-model.lua82
1 files changed, 70 insertions, 12 deletions
diff --git a/did-model.lua b/did-model.lua
index 03f4e98..0bb88c2 100644
--- a/did-model.lua
+++ b/did-model.lua
@@ -358,6 +358,54 @@ local updaterules = function(did, rules)
return res
end
+local testrules = function(rules)
+ local success = true
+ local errtxt
+ local entries = {}
+ local res, err = pcall(function()
+ local sql = "CREATE TEMP TABLE testing (did character varying(40) NOT NULL, extension character varying(40) NOT NULL, starttime time without time zone, endtime time without time zone, startdate date, enddate date, dayofweek bit(7))"
+ assert (con:execute(sql))
+ for i,rule in ipairs(rules) do
+ sql = {}
+ sql[1] = "INSERT INTO testing ("
+ sql[3] = ") VALUES ("
+ sql[5] = ")"
+ names = {}
+ vals = {}
+ for name,val in pairs(rule) do
+ if val and val ~= "" then
+ names[#names+1] = escape(name)
+ vals[#vals+1] = "'"..escape(val).."'"
+ end
+ end
+ sql[2] = table.concat(names, ", ")
+ sql[4] = table.concat(vals, ", ")
+ sql = table.concat(sql, "")
+ res = assert (con:execute(sql))
+ end
+
+ sql = "SELECT * FROM testing ORDER BY did, startdate ASC NULLS FIRST, enddate DESC NULLS FIRST, dayofweek ASC, starttime ASC NULLS FIRST, endtime DESC NULLS FIRST"
+ local cur = assert (con:execute(sql))
+ local row = cur:fetch ({}, "a")
+ while row do
+ entries[#entries+1] = {did=row.did, extension=row.extension, starttime=row.starttime, endtime=row.endtime, startdate=row.startdate, enddate=row.enddate, dayofweek=row.dayofweek}
+ row = cur:fetch (row, "a")
+ end
+ -- close everything
+ cur:close()
+ end)
+ if not res then
+ success = false
+ entries = rules
+ errtxt = string.gsub(err or "", "\n.*", "")
+ end
+ local res, err = pcall(function()
+ local sql = "DROP TABLE testing"
+ assert (con:execute(sql))
+ end)
+ return success, errtxt, entries
+end
+
-- Put the given rules into pubdid
local publishrules = function(did, rules)
-- mark all rules for this did as stale, add in new ones, and delete the stale ones
@@ -513,30 +561,30 @@ local function validaterules(rules)
local connected = databaseconnect(DatabaseUser)
for i,rule in ipairs(rules) do
if not validator.is_integer(rule.did) then
- errtxt[#errtxt+1] = "Rule #"..i..": DID is not a valid number"
+ errtxt[#errtxt+1] = "Rule "..i..": DID is not a valid number"
end
if rule.extension == "" or string.find(rule.extension, "[^%d#%*]") then
- errtxt[#errtxt+1] = "Rule #"..i..": Extension is not a valid number"
+ errtxt[#errtxt+1] = "Rule "..i..": Extension is not a valid number"
end
local res,err
if rule.starttime ~= "" then
res,err,rule.starttime = convertdatabaseentry("TIME", rule.starttime)
- if not res then errtxt[#errtxt+1] = "Rule #"..i..": StartTime "..err end
+ if not res then errtxt[#errtxt+1] = "Rule "..i..": StartTime "..err end
end
if rule.endtime ~= "" then
res,err,rule.endtime = convertdatabaseentry("TIME", rule.endtime)
- if not res then errtxt[#errtxt+1] = "Rule #"..i..": EndTime "..err end
+ if not res then errtxt[#errtxt+1] = "Rule "..i..": EndTime "..err end
end
if rule.startdate ~= "" then
res,err,rule.startdate = convertdatabaseentry("DATE", rule.startdate)
- if not res then errtxt[#errtxt+1] = "Rule #"..i..": StartDate "..err end
+ if not res then errtxt[#errtxt+1] = "Rule "..i..": StartDate "..err end
end
if rule.enddate ~= "" then
res,err,rule.enddate = convertdatabaseentry("DATE", rule.enddate)
- if not res then errtxt[#errtxt+1] = "Rule #"..i..": EndDate "..err end
+ if not res then errtxt[#errtxt+1] = "Rule "..i..": EndDate "..err end
end
if #rule.dayofweek ~= 7 or string.find(rule.dayofweek, "[^01]") then
- errtxt[#errtxt+1] = "Rule #"..i..": DayOfWeek invalid entry"
+ errtxt[#errtxt+1] = "Rule "..i..": DayOfWeek invalid entry"
end
end
if connected then databasedisconnect() end
@@ -751,7 +799,7 @@ function getdefinition(did)
end
-- If exists true, then make sure exists first, if false or undefined, make sure doesn't exist
-function savedefinition(defin, exists)
+function savedefinition(defin, test, exists)
-- remove blank entries, if present
defin.value.rules.value = string.gsub("\n"..format.dostounix(defin.value.rules.value), "\n%s*,%s*,%s*,%s*,%s*,%s*0000000", "")
defin.value.rules.value = string.gsub(defin.value.rules.value, "$\n", "")
@@ -763,7 +811,11 @@ function savedefinition(defin, exists)
defin.value.rules.value = formatrules(rules)
defin.value.rules.errtxt = errtxt
success = validatedefinition(defin) and success
- defin.errtxt = "Failed to save definition"
+ if test then
+ defin.errtxt = "Failed test for DID"
+ else
+ defin.errtxt = "Failed to save DID"
+ end
if success then
local definition = {}
for name,val in pairs(defin.value) do
@@ -772,7 +824,7 @@ function savedefinition(defin, exists)
local res, err = pcall(function()
local connected
- if not exists then
+ if not test and not exists then
databasedisconnect()
local pw = format.parse_ini_file(fs.read_file(configfile) or "", "", "password") or ""
connected = databaseconnect(DatabaseOwner, pw)
@@ -784,6 +836,12 @@ function savedefinition(defin, exists)
defin.value.did.errtxt = "DID Number already exists"
elseif #def == 0 and exists then
defin.value.did.errtxt = "DID Number does not exist"
+ elseif test then
+ success, defin.value.rules.errtxt, rules = testrules(rules)
+ defin.value.rules.value = formatrules(rules)
+ if success then
+ defin.errtxt = "Passed test for DID"
+ end
else
local descr = {}
descr[#descr+1] = describechange(def[1], definition)
@@ -817,8 +875,8 @@ function savedefinition(defin, exists)
return defin
end
-function updatedefinition(defin)
- return savedefinition(defin, true)
+function updatedefinition(defin, test)
+ return savedefinition(defin, test, true)
end
function deletedefinition(did)