summaryrefslogtreecommitdiffstats
path: root/did-model.lua
diff options
context:
space:
mode:
authorTed Trask <ttrask01@yahoo.com>2010-07-14 09:09:17 +0000
committerTed Trask <ttrask01@yahoo.com>2010-07-14 09:09:17 +0000
commit2dda99df5caf988b4abdbc3ac3963eb6af5ab45d (patch)
tree663d3092b695add67752c27a973bbcf9337ba978 /did-model.lua
parentcae845f3f047034a596ff82ef7526224821dba7d (diff)
downloadacf-did-2dda99df5caf988b4abdbc3ac3963eb6af5ab45d.tar.bz2
acf-did-2dda99df5caf988b4abdbc3ac3963eb6af5ab45d.tar.xz
Added available field to definition rather than looking at description.
Diffstat (limited to 'did-model.lua')
-rw-r--r--did-model.lua43
1 files changed, 25 insertions, 18 deletions
diff --git a/did-model.lua b/did-model.lua
index 9a03bfb..d28d775 100644
--- a/did-model.lua
+++ b/did-model.lua
@@ -29,7 +29,7 @@ local didlist
local database_creation_script = {
"CREATE AGGREGATE array_accum(anyelement) (SFUNC = array_append, STYPE = anyarray, INITCOND = '{}')",
"CREATE TABLE dbhistlog (logdatetime timestamp(3) without time zone NOT NULL, msgtext text, userid text)",
- "CREATE TABLE definition (did character varying(40) NOT NULL, identification character varying(7), department character varying(40), description character varying(255), lastchange timestamp(0) without time zone)",
+ "CREATE TABLE definition (did character varying(40) NOT NULL, identification character varying(7), department character varying(40), description character varying(255), lastchange timestamp(0) without time zone, available boolean)",
"CREATE TABLE pubdid (did character varying(40), extension character varying(40), starttime time without time zone, endtime time without time zone, stale boolean)",
"CREATE TABLE rule (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))",
"ALTER TABLE ONLY definition ADD CONSTRAINT definition_pkey PRIMARY KEY (did)",
@@ -187,7 +187,7 @@ local groomdbhistlog = function()
logme("removed " .. res .. " old dbhistlog lines")
end
-local generatewhereclause = function(did, extension, identification, description, department, allowedlist, clause)
+local generatewhereclause = function(did, extension, identification, description, department, available, allowedlist, clause)
local sql = ""
local where = {}
-- We're going to use regular expressions so can search for substrings
@@ -207,6 +207,9 @@ local generatewhereclause = function(did, extension, identification, description
if department and department ~= "" then
where[#where+1] = "department ~* '"..escaperegex(department).."'"
end
+ if (available ~= nil) then
+ where[#where+1] = "available = "..tostring(available)
+ end
--if allowedlist and #allowedlist > 0 then
if allowedlist then
where[#where+1] = "definition.did IN ('"..table.concat(allowedlist, "', '").."')"
@@ -230,7 +233,10 @@ local getdefinitionentries = function(sql)
cur = assert (con:execute(sql))
row = cur:fetch ({}, "a")
while row do
- entries[#entries+1] = {did=row.did, identification=row.identification, department=row.department, description=row.description, extension=row.extension,lastchange=row.lastchange}
+ entries[#entries+1] = {did=row.did, identification=row.identification, department=row.department, description=row.description, extension=row.extension, available=row.available, lastchange=row.lastchange}
+ if entries[#entries].available then
+ entries[#entries].available = (entries[#entries].available == 't')
+ end
row = cur:fetch (row, "a")
end
-- close everything
@@ -239,13 +245,13 @@ local getdefinitionentries = function(sql)
end
local listunuseddefinitions = function(did, identification, description, department, allowedlist, page)
- local where = generatewhereclause(did, nil, identification, description, department, allowedlist, "AND")
+ local where = generatewhereclause(did, nil, identification, description, department, nil, allowedlist, "AND")
local sql = "SELECT * FROM definition WHERE did NOT IN (SELECT did FROM rule)"..where.." ORDER BY did"..generatelimitclause(page)
return getdefinitionentries(sql)
end
local countunuseddefinitions = function(did, identification, description, department, allowedlist)
- local where = generatewhereclause(did, nil, identification, description, department, allowedlist, "AND")
+ local where = generatewhereclause(did, nil, identification, description, department, nil, allowedlist, "AND")
local sql = "SELECT count(*) FROM definition WHERE did NOT IN (SELECT did FROM rule)"..where
cur = assert (con:execute(sql))
local count = cur:fetch()
@@ -255,7 +261,7 @@ end
-- Lists only the definitions that have rules, this also allows us to select based upon extension
local listuseddefinitions = function(did, extension, identification, description, department, allowedlist, page)
- local where = string.gsub(generatewhereclause(did, extension, identification, description, department, allowedlist, "HAVING"), "extension", "array_to_string(array_accum(rule.extension), ', ')")
+ local where = string.gsub(generatewhereclause(did, extension, identification, description, department, nil, allowedlist, "HAVING"), "extension", "array_to_string(array_accum(rule.extension), ', ')")
-- Combine with rules to get extensions, this will drop all dids that don't have rules
-- Relies on this custom aggregate function being defined
-- local sql = "CREATE AGGREGATE array_accum(anyelement)(sfunc = array_append, stype = anyarray, initcond = '{}')"
@@ -266,7 +272,7 @@ end
-- Counts only the definitions that have rules, this also allows us to select based upon extension
local countuseddefinitions = function(did, extension, identification, description, department, allowedlist)
- local where = string.gsub(generatewhereclause(did, extension, identification, description, department, allowedlist, "HAVING"), "extension", "array_to_string(array_accum(rule.extension), ', ')")
+ local where = string.gsub(generatewhereclause(did, extension, identification, description, department, nil, allowedlist, "HAVING"), "extension", "array_to_string(array_accum(rule.extension), ', ')")
-- Combine with rules to get extensions, this will drop all dids that don't have rules
-- Relies on this custom aggregate function being defined
-- local sql = "CREATE AGGREGATE array_accum(anyelement)(sfunc = array_append, stype = anyarray, initcond = '{}')"
@@ -280,7 +286,7 @@ local countuseddefinitions = function(did, extension, identification, descriptio
end
local listdefinitionsandextensions = function(did, identification, description, department, allowedlist, page)
- local where = generatewhereclause(did, nil, identification, description, department, allowedlist, "HAVING")
+ local where = generatewhereclause(did, nil, identification, description, department, nil, allowedlist, "HAVING")
-- Combine with rules to get extensions, use LEFT JOIN to not drop all dids that don't have rules
-- Relies on this custom aggregate function being defined
-- local sql = "CREATE AGGREGATE array_accum(anyelement)(sfunc = array_append, stype = anyarray, initcond = '{}')"
@@ -289,13 +295,13 @@ local listdefinitionsandextensions = function(did, identification, description,
return getdefinitionentries(sql)
end
-local listdefinitions = function(did, identification, description, department, allowedlist, page)
- local sql = "SELECT * FROM definition"..generatewhereclause(did, nil, identification, description, department, allowedlist).." ORDER BY did"..generatelimitclause(page)
+local listdefinitions = function(did, identification, description, department, available, allowedlist, page)
+ local sql = "SELECT * FROM definition"..generatewhereclause(did, nil, identification, description, department, available, allowedlist).." ORDER BY did"..generatelimitclause(page)
return getdefinitionentries(sql)
end
local countdefinitions = function(did, identification, description, department, allowedlist)
- local sql = "SELECT count(*) FROM definition"..generatewhereclause(did, nil, identification, description, department, allowedlist)
+ local sql = "SELECT count(*) FROM definition"..generatewhereclause(did, nil, identification, description, department, nil, allowedlist)
cur = assert (con:execute(sql))
local count = cur:fetch()
cur:close()
@@ -303,7 +309,7 @@ local countdefinitions = function(did, identification, description, department,
end
local listdefs = function(did, identification, description, department, allowedlist)
- local sql = "SELECT did FROM definition"..generatewhereclause(did, nil, identification, description, department, allowedlist).." ORDER BY did"
+ local sql = "SELECT did FROM definition"..generatewhereclause(did, nil, identification, description, department, nil, allowedlist).." ORDER BY did"
local entries = {}
cur = assert (con:execute(sql))
row = cur:fetch ({}, "a")
@@ -345,9 +351,9 @@ local findunuseddefinition = function(exchange)
end
local updatedefinitionentry = function(definition)
- local sql = string.format("UPDATE definition SET identification='%s', department='%s', description='%s', lastchange='now' WHERE did='%s'",
- escape(definition.identification), escape(definition.department),
- escape(definition.description), escape(definition.did))
+ local sql = string.format("UPDATE definition SET identification='%s', department='%s', description='%s', available=%s, lastchange='now' WHERE did='%s'",
+ escape(definition.identification), escape(definition.department), escape(definition.description),
+ tostring(definition.available), escape(definition.did))
local res = assert (con:execute(sql))
-- logme("Updated DID "..definition.did)
return res
@@ -903,7 +909,7 @@ function getdefinition(self, userid, did)
group.did.errtxt = "DID does not exist"
local res, err = pcall(function()
local connected = databaseconnect(DatabaseUser)
- local definition = listdefinitions(stripdash(did), nil, nil, nil, allowedlist)
+ local definition = listdefinitions(stripdash(did), nil, nil, nil, nil, allowedlist)
local rules = listrules(stripdash(did))
if connected then databasedisconnect() end
if #definition == 1 then
@@ -961,7 +967,7 @@ function savedefinition(self, userid, defin, test, exists)
else
connected = databaseconnect(DatabaseUser)
end
- local def = listdefinitions(definition.did, nil, nil, nil, allowedlist)
+ local def = listdefinitions(definition.did, nil, nil, nil, nil, allowedlist)
if #def > 0 and not exists then
defin.value.did.errtxt = "DID Number already exists"
elseif #def == 0 and exists then
@@ -1360,12 +1366,13 @@ function requestdid(self, userid)
local result = ""
local res, err = pcall(function()
local connected = databaseconnect(DatabaseUser)
- local defs = listdefinitions(nil, nil, "Available")
+ local defs = listdefinitions(nil, nil, nil, nil, true)
if #defs == 0 then
errtxt = "No DIDs available"
else
adduserpermission(self, userid, defs[1].did)
defs[1].description = "Reserved for "..userid
+ defs[1].available = false
updatedefinitionentry(defs[1])
result = "Assigned new DID "..defs[1].did
end