Refactor directory structrue

This commit is contained in:
jvelilla
2014-11-13 11:57:36 -03:00
parent 8ab4343d5b
commit d963fd218b
154 changed files with 12 additions and 11 deletions

View File

@@ -0,0 +1,272 @@
note
description: "Summary description for {CMS_STORAGE_MYSQL}."
date: "$Date$"
revision: "$Revision$"
class
CMS_STORAGE_SQLITE
inherit
CMS_STORAGE
REFACTORING_HELPER
create
make
feature {NONE} -- Initialization
make (a_connection: DATABASE_CONNECTION)
--
require
is_connected: a_connection.is_connected
do
log.write_information (generator+".make_with_database is database connected? "+ a_connection.is_connected.out )
create node_provider.make (a_connection)
create user_provider.make (a_connection)
post_node_provider_execution
post_user_provider_execution
end
feature -- Access: user
has_user: BOOLEAN
-- Has any user?
do
Result := user_provider.has_user
post_user_provider_execution
end
all_users: LIST [CMS_USER]
do
to_implement("Not implemented!!!")
create {ARRAYED_LIST[CMS_USER]} Result.make (0)
end
user_by_id (a_id: like {CMS_USER}.id): detachable CMS_USER
do
Result := user_provider.user (a_id)
post_user_provider_execution
end
user_by_name (a_name: like {CMS_USER}.name): detachable CMS_USER
do
Result := user_provider.user_by_name (a_name)
post_user_provider_execution
end
user_by_email (a_email: like {CMS_USER}.email): detachable CMS_USER
do
Result := user_provider.user_by_email (a_email)
post_user_provider_execution
end
is_valid_credential (l_auth_login, l_auth_password: READABLE_STRING_32): BOOLEAN
local
l_security: SECURITY_PROVIDER
do
if attached user_provider.user_salt (l_auth_login) as l_hash then
if attached user_provider.user_by_name (l_auth_login) as l_user then
create l_security
if
attached l_user.password as l_password and then
l_security.password_hash (l_auth_password, l_hash).is_case_insensitive_equal (l_password)
then
Result := True
else
log.write_information (generator + ".login_valid User: wrong username or password" )
end
else
log.write_information (generator + ".login_valid User:" + l_auth_login + "does not exist" )
end
end
post_user_provider_execution
end
feature -- Change: user
save_user (a_user: CMS_USER)
-- Add a new user `a_user'.
do
if
attached a_user.password as l_password and then
attached a_user.email as l_email
then
user_provider.new_user (a_user.name, l_password, l_email)
else
-- set error
end
end
feature -- User Nodes
user_collaborator_nodes (a_id: like {CMS_USER}.id): LIST[CMS_NODE]
-- Possible list of nodes where the user identified by `a_id', is a collaborator.
do
create {ARRAYED_LIST[CMS_NODE]} Result.make (0)
to_implement ("Not implemented")
end
user_author_nodes (a_id: like {CMS_USER}.id): LIST[CMS_NODE]
-- Possible list of nodes where the user identified by `a_id', is the author.
do
create {ARRAYED_LIST[CMS_NODE]} Result.make (0)
to_implement ("Not implemented")
end
feature -- Users roles and permissions
user_role_by_id (a_id: like {CMS_USER_ROLE}.id): detachable CMS_USER_ROLE
-- User role by id `a_id', if any.
do
to_implement ("Not implemented")
end
user_roles: LIST [CMS_USER_ROLE]
-- Possible list of user roles.
do
create {ARRAYED_LIST[CMS_USER_ROLE]} Result.make (0)
to_implement ("Not implemented")
end
feature -- Change: roles and permissions
save_user_role (a_user_role: CMS_USER_ROLE)
-- Save user role `a_user_role'
do
to_implement ("Not implemented")
end
feature -- Access: node
nodes: LIST[CMS_NODE]
-- List of nodes.
do
create {ARRAYED_LIST[CMS_NODE]} Result.make (0)
across node_provider.nodes as c loop
Result.force (c.item)
end
post_node_provider_execution
end
recent_nodes (a_lower: INTEGER; a_count: INTEGER): LIST [CMS_NODE]
-- List of the `a_count' most recent nodes, starting from `a_lower'.
do
create {ARRAYED_LIST[CMS_NODE]} Result.make (0)
across node_provider.recent_nodes (a_lower,a_count) as c loop
Result.force (c.item)
end
post_node_provider_execution
end
node (a_id: INTEGER_64): detachable CMS_NODE
--
do
Result := node_provider.node (a_id)
post_node_provider_execution
end
feature -- Node
save_node (a_node: CMS_NODE)
-- Add a new node
do
node_provider.new_node (a_node)
post_node_provider_execution
end
delete_node (a_id: INTEGER_64)
do
node_provider.delete_node (a_id)
post_node_provider_execution
end
update_node (a_id: like {CMS_USER}.id; a_node: CMS_NODE)
do
node_provider.update_node (a_node)
post_node_provider_execution
end
update_node_title (a_id: like {CMS_USER}.id; a_node_id: like {CMS_NODE}.id; a_title: READABLE_STRING_32)
do
node_provider.update_node_title (a_id, a_title)
post_node_provider_execution
end
update_node_summary (a_id: like {CMS_USER}.id; a_node_id: like {CMS_NODE}.id; a_summary: READABLE_STRING_32)
do
node_provider.update_node_summary (a_id, a_summary)
post_node_provider_execution
end
update_node_content (a_id: like {CMS_USER}.id; a_node_id: like {CMS_NODE}.id; a_content: READABLE_STRING_32)
do
node_provider.update_node_content (a_id, a_content)
post_node_provider_execution
end
node_author (a_id: like {CMS_NODE}.id): detachable CMS_USER
-- Node's author. if any.
do
to_implement ("Not implemented")
end
node_collaborators (a_id: like {CMS_NODE}.id): LIST [CMS_USER]
-- Possible list of node's collaborator.
do
create {ARRAYED_LIST[CMS_USER]} Result.make (0)
to_implement ("Not implemented")
end
feature -- User
new_user (a_user: CMS_USER)
-- Add a new user `a_user'.
do
if
attached a_user.password as l_password and then
attached a_user.email as l_email
then
user_provider.new_user (a_user.name, l_password, l_email)
else
-- set error
end
end
feature {NONE} -- Post process
post_node_provider_execution
do
if node_provider.successful then
set_successful
else
if attached node_provider.last_error then
set_last_error_from_handler (node_provider.last_error)
end
end
end
post_user_provider_execution
do
if user_provider.successful then
set_successful
else
if attached user_provider.last_error then
set_last_error_from_handler (user_provider.last_error)
end
end
end
node_provider: NODE_DATA_PROVIDER
-- Node Data provider.
user_provider: USER_DATA_PROVIDER
-- User Data provider.
end

View File

@@ -0,0 +1,145 @@
note
description: "Object that handle a database connection for ODBC"
date: "$Date: 2014-08-20 15:21:15 -0300 (mi., 20 ago. 2014) $"
revision: "$Revision: 95678 $"
class
DATABASE_CONNECTION_MYSQL
inherit
DATABASE_CONNECTION
redefine
db_application
end
create
make, make_common, make_basic, login_with_connection_string, login_with_schema
feature -- Initialization
make_common
-- Create a database handler for MYSQL with common settings.
local
l_retried: BOOLEAN
do
create db_application.login (username, password)
if not l_retried then
db_application.set_hostname (hostname)
db_application.set_data_source (database_name)
db_application.set_base
create db_control.make
keep_connection := is_keep_connection
if keep_connection then
connect
end
set_successful
else
create db_control.make
end
rescue
create db_control.make
set_last_error_from_exception ("Connection execution")
log.write_critical (generator + ".make_common:" + last_error_message)
if is_connected then
disconnect
end
l_retried := True
retry
end
make_basic (a_database_name: STRING)
-- Create a database handler and
-- set database_name to `a_database_name'.
local
l_retried: BOOLEAN
do
create db_application.login (username, password)
if not l_retried then
db_application.set_hostname (hostname)
db_application.set_data_source (a_database_name)
db_application.set_base
create db_control.make
keep_connection := is_keep_connection
if keep_connection then
connect
end
set_successful
else
create db_control.make
end
rescue
create db_control.make
set_last_error_from_exception ("Connection execution")
log.write_critical (generator + ".make_common:" + last_error_message)
if is_connected then
disconnect
end
l_retried := True
retry
end
make (a_username: STRING; a_password: STRING; a_hostname: STRING; a_database_name: STRING; connection: BOOLEAN)
-- Create a database handler for ODBC and set `username' to `a_username',
-- `password' to `a_password'
-- `database_name' to `a_database_name'
-- `connection' to `a_connection'
do
create db_application.login (a_username, a_password)
db_application.set_hostname (a_hostname)
db_application.set_data_source (a_database_name)
db_application.set_base
create db_control.make
keep_connection := connection
if keep_connection then
connect
end
end
login_with_connection_string (a_string: STRING)
-- Login with `a_connection_string'and immediately connect to database.
local
l_string: LIST[STRING]
l_server: STRING
l_port: STRING
l_schema: STRING
l_user: STRING
l_password: STRING
do
l_string := a_string.split (';')
l_server := l_string.at (2).split ('=').at (2)
l_port := l_string.at (3).split ('=').at (2)
l_schema := l_string.at (4).split ('=').at (2)
l_user := l_string.at (5).split ('=').at (2)
l_password := l_string.at (6).split ('=').at (2)
create db_application
db_application.set_application (l_schema)
db_application.set_hostname (l_server + ":" + l_port)
db_application.login_and_connect (l_user, l_password)
db_application.set_base
create db_control.make
keep_connection := is_keep_connection
end
login_with_schema (a_schema: STRING; a_username: STRING; a_password: STRING)
-- Login with `a_connection_string'and immediately connect to database.
do
create db_application
db_application.set_application (a_schema)
db_application.login_and_connect (a_username, a_password)
db_application.set_base
create db_control.make
keep_connection := is_keep_connection
end
feature -- Databse Connection
db_application: DATABASE_APPL [MYSQL]
-- Database application.
end

View File

@@ -0,0 +1,289 @@
note
description: "Database access for node uses cases."
date: "$Date: 2014-08-20 15:21:15 -0300 (mi., 20 ago. 2014) $"
revision: "$Revision: 95678 $"
class
NODE_DATA_PROVIDER
inherit
PARAMETER_NAME_HELPER
SHARED_ERROR
REFACTORING_HELPER
create
make
feature -- Initialization
make (a_connection: DATABASE_CONNECTION)
-- Create a data provider.
do
create {DATABASE_HANDLER_IMPL} db_handler.make (a_connection)
post_execution
end
db_handler: DATABASE_HANDLER
-- Db handler.
feature -- Status Report
is_successful: BOOLEAN
-- Is the last execution sucessful?
do
Result := db_handler.successful
end
feature -- Access
nodes: DATABASE_ITERATION_CURSOR [CMS_NODE]
-- List of nodes.
local
l_parameters: STRING_TABLE [ANY]
do
log.write_information (generator + ".nodes")
create l_parameters.make (0)
db_handler.set_query (create {DATABASE_QUERY}.data_reader (Select_nodes, l_parameters))
db_handler.execute_query
create Result.make (db_handler, agent fetch_node)
post_execution
end
recent_nodes (a_lower, a_rows: INTEGER): DATABASE_ITERATION_CURSOR [CMS_NODE]
-- The most recent `a_rows'.
local
l_parameters: STRING_TABLE [ANY]
l_query: STRING
do
log.write_information (generator + ".recent_nodes")
create l_parameters.make (2)
l_parameters.put (a_rows, "rows")
create l_query.make_from_string (select_recent_nodes)
l_query.replace_substring_all ("$offset", a_lower.out)
db_handler.set_query (create {DATABASE_QUERY}.data_reader (l_query, l_parameters))
db_handler.execute_query
create Result.make (db_handler, agent fetch_node)
post_execution
end
node (a_id: INTEGER_64): detachable CMS_NODE
-- Node for the given id `a_id', if any.
local
l_parameters: STRING_TABLE [ANY]
do
log.write_information (generator + ".node")
create l_parameters.make (1)
l_parameters.put (a_id,"id")
db_handler.set_query (create {DATABASE_QUERY}.data_reader (select_node_by_id, l_parameters))
db_handler.execute_query
if db_handler.count = 1 then
Result := fetch_node
end
post_execution
end
count: INTEGER
-- Number of items nodes.
local
l_parameters: STRING_TABLE [ANY]
do
log.write_information (generator + ".count")
create l_parameters.make (0)
db_handler.set_query (create {DATABASE_QUERY}.data_reader (select_count, l_parameters))
db_handler.execute_query
if db_handler.count = 1 then
Result := db_handler.read_integer_32 (1)
end
post_execution
end
feature -- Basic operations
new_node (a_node: CMS_NODE)
-- Create a new node.
local
l_parameters: STRING_TABLE [ANY]
do
log.write_information (generator + ".new_node")
create l_parameters.make (6)
l_parameters.put (a_node.title, "title")
l_parameters.put (a_node.summary, "summary")
l_parameters.put (a_node.content, "content")
l_parameters.put (a_node.publication_date, "publication_date")
l_parameters.put (a_node.creation_date, "creation_date")
l_parameters.put (a_node.modification_date, "modification_date")
db_handler.set_query (create {DATABASE_QUERY}.data_reader (sql_insert_node, l_parameters))
db_handler.execute_change
post_execution
end
update_node_title (a_id: INTEGER_64; a_title: READABLE_STRING_32)
-- Update node title for the corresponding the report with id `a_id'.
local
l_parameters: STRING_TABLE [ANY]
do
log.write_information (generator + ".update_node_title")
create l_parameters.make (3)
l_parameters.put (a_title, "title")
l_parameters.put (create {DATE_TIME}.make_now_utc, "modification_date")
l_parameters.put (a_id, "id")
db_handler.set_query (create {DATABASE_QUERY}.data_reader (sql_update_node_title, l_parameters))
db_handler.execute_change
post_execution
end
update_node_summary (a_id: INTEGER_64; a_summary: READABLE_STRING_32)
-- Update node summary for the corresponding the report with id `a_id'.
local
l_parameters: STRING_TABLE [ANY]
do
log.write_information (generator + ".update_node_summary")
create l_parameters.make (3)
l_parameters.put (a_summary, "summary")
l_parameters.put (create {DATE_TIME}.make_now_utc, "modification_date")
l_parameters.put (a_id, "id")
db_handler.set_query (create {DATABASE_QUERY}.data_reader (sql_update_node_summary, l_parameters))
db_handler.execute_change
post_execution
end
update_node_content (a_id: INTEGER_64; a_content: READABLE_STRING_32)
-- Update node content for the corresponding the report with id `a_id'.
local
l_parameters: STRING_TABLE [ANY]
do
log.write_information (generator + ".update_node_content")
create l_parameters.make (3)
l_parameters.put (a_content, "content")
l_parameters.put (create {DATE_TIME}.make_now_utc, "modification_date")
l_parameters.put (a_id, "id")
db_handler.set_query (create {DATABASE_QUERY}.data_reader (sql_update_node_content, l_parameters))
db_handler.execute_change
post_execution
end
update_node (a_node: CMS_NODE)
-- Update node.
local
l_parameters: STRING_TABLE [ANY]
do
log.write_information (generator + ".update_node")
create l_parameters.make (7)
l_parameters.put (a_node.title, "title")
l_parameters.put (a_node.summary, "summary")
l_parameters.put (a_node.content, "content")
l_parameters.put (a_node.publication_date, "publication_date")
l_parameters.put (a_node.creation_date, "creation_date")
l_parameters.put (create {DATE_TIME}.make_now_utc, "modification_date")
l_parameters.put (a_node.id, "id")
db_handler.set_query (create {DATABASE_QUERY}.data_reader (sql_update_node, l_parameters))
db_handler.execute_change
post_execution
end
delete_node (a_id: INTEGER_64;)
-- Delete node with id `a_id'.
local
l_parameters: STRING_TABLE [ANY]
do
log.write_information (generator + ".delete_node")
create l_parameters.make (1)
l_parameters.put (a_id, "id")
db_handler.set_query (create {DATABASE_QUERY}.data_reader (sql_delete_node, l_parameters))
db_handler.execute_change
post_execution
end
feature -- Connection
connect
-- Connect to the database.
do
if not db_handler.is_connected then
db_handler.connect
end
end
disconnect
-- Disconnect to the database.
do
if db_handler.is_connected then
db_handler.disconnect
end
end
feature {NONE} -- Queries
Select_count: STRING = "select count(*) from Nodes;"
Select_nodes: STRING = "select * from Nodes;"
-- SQL Query to retrieve all nodes.
Select_node_by_id: STRING = "select * from Nodes where id =:id order by id desc, publication_date desc;"
Select_recent_nodes: STRING = "select * from Nodes order by id desc, publication_date desc Limit $offset , :rows "
SQL_Insert_node: STRING = "insert into nodes (title, summary, content, publication_date, creation_date, modification_date) values (:title, :summary, :content, :publication_date, :creation_date, :modification_date);"
-- SQL Insert to add a new node.
SQL_Update_node_title: STRING ="update nodes SET title=:title, modification_date=:modification_date where id=:id;"
-- SQL update node title.
SQL_Update_node_summary: STRING ="update nodes SET summary=:summary, modification_date=:modification_date where id=:id;"
-- SQL update node summary.
SQL_Update_node_content: STRING ="update nodes SET content=:content, modification_date=:modification_date where id=:id;"
-- SQL node content.
SQL_Update_node : STRING = "update nodes SET title=:title, summary=:summary, content=:content, publication_date=:publication_date, creation_date=:creation_date, modification_date=:modification_date where id=:id;"
-- SQL node.
SQL_Delete_node: STRING = "delete from nodes where id=:id;"
feature -- New Object
fetch_node: CMS_NODE
do
create Result.make ("", "", "")
if attached db_handler.read_integer_32 (1) as l_id then
Result.set_id (l_id)
end
if attached db_handler.read_date_time (2) as l_pd then
Result.set_publication_date (l_pd)
end
if attached db_handler.read_date_time (3) as l_cd then
Result.set_creation_date (l_cd)
end
if attached db_handler.read_date_time (4) as l_md then
Result.set_modification_date (l_md)
end
if attached db_handler.read_string (5) as l_t then
Result.set_title (l_t)
end
if attached db_handler.read_string (6) as l_s then
Result.set_summary (l_s)
end
if attached db_handler.read_string (7) as l_c then
Result.set_content (l_c)
end
end
feature {NONE} -- Implementation
post_execution
-- Post database execution.
do
if db_handler.successful then
set_successful
else
if attached db_handler.last_error then
set_last_error_from_handler (db_handler.last_error)
end
end
end
end

View File

@@ -0,0 +1,206 @@
note
description: "Summary description for {USER_DATA_PROVIDER}."
date: "$Date$"
revision: "$Revision$"
class
USER_DATA_PROVIDER
inherit
PARAMETER_NAME_HELPER
SHARED_ERROR
REFACTORING_HELPER
create
make
feature -- Initialization
make (a_connection: DATABASE_CONNECTION)
-- Create a data provider.
do
create {DATABASE_HANDLER_IMPL} db_handler.make (a_connection)
post_execution
end
db_handler: DATABASE_HANDLER
-- Db handler.
feature -- Status Report
is_successful: BOOLEAN
-- Is the last execution sucessful?
do
Result := db_handler.successful
end
has_user: BOOLEAN
-- Has any user?
do
Result := count > 0
end
feature -- Basic Operations
new_user (a_user_name: READABLE_STRING_32; a_password: READABLE_STRING_32; a_email: READABLE_STRING_32)
-- Create a new node.
local
l_parameters: STRING_TABLE [detachable ANY]
l_password_salt, l_password_hash: STRING
l_security: SECURITY_PROVIDER
do
create l_security
l_password_salt := l_security.salt
l_password_hash := l_security.password_hash (a_password, l_password_salt)
log.write_information (generator + ".new_user")
create l_parameters.make (4)
l_parameters.put (a_user_name,"username")
l_parameters.put (l_password_hash,"password")
l_parameters.put (l_password_salt,"salt")
l_parameters.put (a_email,"email")
db_handler.set_query (create {DATABASE_QUERY}.data_reader (sql_insert_user, l_parameters))
db_handler.execute_change
post_execution
end
user (a_id: INTEGER_64): detachable CMS_USER
-- User for the given id `a_id', if any.
local
l_parameters: STRING_TABLE [ANY]
do
log.write_information (generator + ".user")
create l_parameters.make (1)
l_parameters.put (a_id,"id")
db_handler.set_query (create {DATABASE_QUERY}.data_reader (select_user_by_id, l_parameters))
db_handler.execute_query
if db_handler.count = 1 then
Result := fetch_user
end
post_execution
end
user_by_name (a_name: READABLE_STRING_32): detachable CMS_USER
-- User for the given name `a_name', if any.
local
l_parameters: STRING_TABLE [ANY]
do
log.write_information (generator + ".user_by_name")
create l_parameters.make (1)
l_parameters.put (a_name,"name")
db_handler.set_query (create {DATABASE_QUERY}.data_reader (select_user_by_name, l_parameters))
db_handler.execute_query
if db_handler.count = 1 then
Result := fetch_user
end
post_execution
end
user_by_email (a_email: detachable READABLE_STRING_32): detachable CMS_USER
-- User for the given email `a_email', if any.
local
l_parameters: STRING_TABLE [detachable ANY]
do
log.write_information (generator + ".user_by_email")
create l_parameters.make (1)
l_parameters.put (a_email,"email")
db_handler.set_query (create {DATABASE_QUERY}.data_reader (select_user_by_email, l_parameters))
db_handler.execute_query
if db_handler.count = 1 then
Result := fetch_user
end
post_execution
end
user_salt (a_username: READABLE_STRING_32): detachable READABLE_STRING_32
-- User salt for the given user `a_username', if any.
local
l_parameters: STRING_TABLE [ANY]
do
log.write_information (generator + ".user_salt")
create l_parameters.make (1)
l_parameters.put (a_username,"name")
db_handler.set_query (create {DATABASE_QUERY}.data_reader (select_salt_by_username, l_parameters))
db_handler.execute_query
if db_handler.count = 1 then
if attached db_handler.read_string (1) as l_salt then
Result := l_salt.as_string_32
end
end
post_execution
end
count: INTEGER
-- Number of items users.
local
l_parameters: STRING_TABLE [ANY]
do
log.write_information (generator + ".count")
create l_parameters.make (0)
db_handler.set_query (create {DATABASE_QUERY}.data_reader (select_count, l_parameters))
db_handler.execute_query
if db_handler.count = 1 then
Result := db_handler.read_integer_32 (1)
end
post_execution
end
feature -- New Object
fetch_user: CMS_USER
do
create Result.make ("")
if attached db_handler.read_integer_32 (1) as l_id then
Result.set_id (l_id)
end
if attached db_handler.read_string (2) as l_u then
Result.set_name (l_u)
end
if attached db_handler.read_string (3) as l_p then
Result.set_password (l_p)
end
if attached db_handler.read_string (5) as l_e then
Result.set_email (l_e)
end
end
feature -- Sql Queries
Select_count: STRING = "select count(*) from Users;"
-- Number of users.
Select_user_by_id: STRING = "select * from Users where id =:id;"
-- Retrieve user by id if exists.
Select_user_by_name: STRING = "select * from Users where username =:name;"
-- Retrieve user by name if exists.
Select_user_by_email: STRING = "select * from Users where email =:email;"
-- Retrieve user by email if exists.
Select_salt_by_username: STRING = "select salt from Users where username =:name;"
-- Retrieve salt by username if exists.
SQL_Insert_user: STRING = "insert into users (username, password, salt, email) values (:username, :password, :salt, :email);"
-- SQL Insert to add a new node.
feature {NONE} -- Implementation
post_execution
-- Post database execution.
do
if db_handler.successful then
set_successful
else
if attached db_handler.last_error then
set_last_error_from_handler (db_handler.last_error)
end
end
end
end