From 9dc3dbafd802e3c4e4173e3d3d483e4ad0950617 Mon Sep 17 00:00:00 2001 From: josteitv Date: Wed, 20 May 2015 11:33:55 +0200 Subject: [PATCH] Added TODO-list --- .gitignore | 4 + pom.xml | 6 + src/assembly/assembly.xml | 0 src/files/start.bat | 0 src/main/java/FileServlet.java | 0 src/main/java/Html.java | 0 src/main/java/LoginService.java | 0 src/main/java/LoginServlet.java | 0 src/main/java/LogoutServlet.java | 0 src/main/java/Main.java | 1 + src/main/java/MoneyTransferServlet.java | 0 src/main/java/Repository.java | 139 +++++++++++++++++- src/main/java/SearchServlet.java | 0 src/main/java/SecureServlet.java | 1 + src/main/java/TodoItem.java | 30 ++++ src/main/java/TodoServlet.java | 89 +++++++++++ src/main/resources/logback.xml | 0 src/main/resources/web/css/bootstrap.min.css | 0 .../resources/web/css/starter-template.css | 0 src/main/resources/web/index.html | 0 20 files changed, 266 insertions(+), 4 deletions(-) create mode 100644 .gitignore mode change 100755 => 100644 pom.xml mode change 100755 => 100644 src/assembly/assembly.xml mode change 100755 => 100644 src/files/start.bat mode change 100755 => 100644 src/main/java/FileServlet.java mode change 100755 => 100644 src/main/java/Html.java mode change 100755 => 100644 src/main/java/LoginService.java mode change 100755 => 100644 src/main/java/LoginServlet.java mode change 100755 => 100644 src/main/java/LogoutServlet.java mode change 100755 => 100644 src/main/java/Main.java mode change 100755 => 100644 src/main/java/MoneyTransferServlet.java mode change 100755 => 100644 src/main/java/Repository.java mode change 100755 => 100644 src/main/java/SearchServlet.java mode change 100755 => 100644 src/main/java/SecureServlet.java create mode 100644 src/main/java/TodoItem.java create mode 100644 src/main/java/TodoServlet.java mode change 100755 => 100644 src/main/resources/logback.xml mode change 100755 => 100644 src/main/resources/web/css/bootstrap.min.css mode change 100755 => 100644 src/main/resources/web/css/starter-template.css mode change 100755 => 100644 src/main/resources/web/index.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4e247ee --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/.settings +/target +/.classpath +/.project diff --git a/pom.xml b/pom.xml old mode 100755 new mode 100644 index 2f7b834..8515836 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,12 @@ 1.4.187 + + org.apache.commons + commons-lang3 + 3.4 + + diff --git a/src/assembly/assembly.xml b/src/assembly/assembly.xml old mode 100755 new mode 100644 diff --git a/src/files/start.bat b/src/files/start.bat old mode 100755 new mode 100644 diff --git a/src/main/java/FileServlet.java b/src/main/java/FileServlet.java old mode 100755 new mode 100644 diff --git a/src/main/java/Html.java b/src/main/java/Html.java old mode 100755 new mode 100644 diff --git a/src/main/java/LoginService.java b/src/main/java/LoginService.java old mode 100755 new mode 100644 diff --git a/src/main/java/LoginServlet.java b/src/main/java/LoginServlet.java old mode 100755 new mode 100644 diff --git a/src/main/java/LogoutServlet.java b/src/main/java/LogoutServlet.java old mode 100755 new mode 100644 diff --git a/src/main/java/Main.java b/src/main/java/Main.java old mode 100755 new mode 100644 index 65f7568..fb1e1d6 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -44,6 +44,7 @@ private static ServletContextHandler createServletContextHandler() { servletHandler.addServlet(SecureServlet.class, "/secure/*"); servletHandler.addServlet(MoneyTransferServlet.class, "/secure/transfer.html"); + servletHandler.addServlet(TodoServlet.class, "/secure/todo.html"); servletHandler.addServlet(LoginServlet.class, "/login"); servletHandler.addServlet(LogoutServlet.class, "/logout"); servletHandler.addServlet(SearchServlet.class, "/search.html"); diff --git a/src/main/java/MoneyTransferServlet.java b/src/main/java/MoneyTransferServlet.java old mode 100755 new mode 100644 diff --git a/src/main/java/Repository.java b/src/main/java/Repository.java old mode 100755 new mode 100644 index cb7d877..890dd4c --- a/src/main/java/Repository.java +++ b/src/main/java/Repository.java @@ -4,6 +4,8 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,6 +28,8 @@ public static void createDatabase() { private static void createTables() { executeSql("CREATE TABLE IF NOT EXISTS LOGIN(id int primary key, username varchar(255), password varchar(255))"); executeSql("CREATE TABLE IF NOT EXISTS ACCOUNT(id int primary key, username varchar(255), amount int)"); + executeSql("CREATE TABLE IF NOT EXISTS TODO_ITEM(id int primary key, user varchar(255), todo varchar(255))"); + executeSql("CREATE SEQUENCE IF NOT EXISTS TODO_ITEM_SEQ"); } private static void insertUsers() { @@ -42,7 +46,7 @@ private static void insertAccountData() { "MERGE INTO ACCOUNT(id, username, amount) VALUES(2, 'pål', 20)", "MERGE INTO ACCOUNT(id, username, amount) VALUES(3, 'espen', 100)"); } - + private static void executeSql(String... sqls) { Connection connection = getDBConnection(); Statement stmt = null; @@ -117,9 +121,9 @@ public static String getAmount(String username) { Connection connection = getDBConnection(); try { - PreparedStatement ps = connection.prepareStatement(sql); - ps.setString(1, username); - ResultSet rs = ps.executeQuery(); + PreparedStatement ps = connection.prepareStatement(sql); + ps.setString(1, username); + ResultSet rs = ps.executeQuery(); if (rs.next()) { return rs.getString(1); @@ -139,4 +143,131 @@ public static String getAmount(String username) { } } + public static TodoItem getTodoItem(String id) { + String sql = "select id, user, todo from TODO_ITEM where id = ?"; + + Connection connection = getDBConnection(); + try { + PreparedStatement ps = connection.prepareStatement(sql); + ps.setString(1, id); + ResultSet rs = ps.executeQuery(); + + if (rs.next()) { + String dbId = rs.getString(1); + String dbUser = rs.getString(2); + String dbTodo = rs.getString(3); + return new TodoItem(dbId, dbUser, dbTodo); + } else { + return null; + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + if (connection != null) { + connection.close(); + } + } catch (SQLException e) { + // ignore + } + } + } + + public static List getTodoItems(String user) { + String sql = "select id, user, todo from TODO_ITEM where user = ?"; + + Connection connection = getDBConnection(); + try { + PreparedStatement ps = connection.prepareStatement(sql); + ps.setString(1, user); + ResultSet rs = ps.executeQuery(); + + List todoItems = new ArrayList<>(); + while (rs.next()) { + String dbId = rs.getString(1); + String dbUser = rs.getString(2); + String dbTodo = rs.getString(3); + todoItems.add(new TodoItem(dbId, dbUser, dbTodo)); + } + return todoItems; + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + if (connection != null) { + connection.close(); + } + } catch (SQLException e) { + // ignore + } + } + } + + public static void saveTodoItem(TodoItem todoItem) { + String sql = "insert into TODO_ITEM(id, user, todo) VALUES(TODO_ITEM_SEQ.nextval,?,?)"; + + Connection connection = getDBConnection(); + try { + PreparedStatement ps = connection.prepareStatement(sql); + ps.setString(1, todoItem.getUser()); + ps.setString(2, todoItem.getTodo()); + ps.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + if (connection != null) { + connection.close(); + } + } catch (SQLException e) { + // ignore + } + } + } + + public static void updateTodoItem(TodoItem todoItem) { + String sql = "update TODO_ITEM set todo = ? where id = ? and user = ?"; + + Connection connection = getDBConnection(); + try { + PreparedStatement ps = connection.prepareStatement(sql); + ps.setString(1, todoItem.getTodo()); + ps.setString(2, todoItem.getId()); + ps.setString(3, todoItem.getUser()); + ps.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + if (connection != null) { + connection.close(); + } + } catch (SQLException e) { + // ignore + } + } + + } + + public static void deleteTodoItem(String id) { + String sql = "delete from TODO_ITEM where id = ?"; + + Connection connection = getDBConnection(); + try { + PreparedStatement ps = connection.prepareStatement(sql); + ps.setString(1, id); + ps.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + if (connection != null) { + connection.close(); + } + } catch (SQLException e) { + // ignore + } + } + } + } diff --git a/src/main/java/SearchServlet.java b/src/main/java/SearchServlet.java old mode 100755 new mode 100644 diff --git a/src/main/java/SecureServlet.java b/src/main/java/SecureServlet.java old mode 100755 new mode 100644 index 1effe5d..b2d62b3 --- a/src/main/java/SecureServlet.java +++ b/src/main/java/SecureServlet.java @@ -13,6 +13,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t response.setStatus(HttpServletResponse.SC_OK); String content = "

You are logged in to the secure area as '" + request.getUserPrincipal().getName() + "'.

" + + "

View TODO list

" + "

Log out of secure area

"; response.getWriter().println(Html.html(content)); diff --git a/src/main/java/TodoItem.java b/src/main/java/TodoItem.java new file mode 100644 index 0000000..04f9cbd --- /dev/null +++ b/src/main/java/TodoItem.java @@ -0,0 +1,30 @@ + +public class TodoItem { + + private final String id; + private final String user; + private final String todo; + + public TodoItem(String id, String user, String todo) { + this.id = id; + this.user = user; + this.todo = todo; + } + + public TodoItem(String user, String todo) { + this(null, user, todo); + } + + public String getId() { + return id; + } + + public String getUser() { + return user; + } + + public String getTodo() { + return todo; + } + +} diff --git a/src/main/java/TodoServlet.java b/src/main/java/TodoServlet.java new file mode 100644 index 0000000..bbd55ea --- /dev/null +++ b/src/main/java/TodoServlet.java @@ -0,0 +1,89 @@ +import static org.apache.commons.lang3.StringEscapeUtils.escapeHtml4; + +import java.io.IOException; +import java.util.List; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class TodoServlet extends HttpServlet { + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.setStatus(HttpServletResponse.SC_OK); + + String user = request.getUserPrincipal().getName(); + + String id = request.getParameter("id"); + String todo = request.getParameter("todo"); + String delete = request.getParameter("delete"); + + String html = "

TODO

"; + + String todoText = ""; + String idText = ""; + + if (!isNullOrEmpty(id) && !isNullOrEmpty(delete)) { + // Delete todo item + Repository.deleteTodoItem(id); + + } else if (!isNullOrEmpty(id) && isNullOrEmpty(todo)) { + // View todo item + TodoItem todoItem = Repository.getTodoItem(id); + todoText = escapeHtml4(todoItem.getTodo()); + idText = id; + + } else if (isNullOrEmpty(id) && !isNullOrEmpty(todo)) { + // Create new todo item + Repository.saveTodoItem(new TodoItem(user, todo)); + + } else if (!isNullOrEmpty(id) && !isNullOrEmpty(todo)) { + // Update todo item + Repository.updateTodoItem(new TodoItem(id, user, todo)); + todoText = escapeHtml4(todo); + idText = id; + } + + html += "
" + + "
" + + "
" + + " " + + "
" + + " " + + "
" + + "
" + + "" + + "
" + + "
"; + + html += "
" + + "" + + "
"; + + List todoItems = Repository.getTodoItems(user); + for (TodoItem todoItem : todoItems) { + html += ""; + } + + response.getWriter().println(Html.html(html)); + } + + private boolean isNullOrEmpty(String id) { + return id == null || id.isEmpty(); + } + +} diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml old mode 100755 new mode 100644 diff --git a/src/main/resources/web/css/bootstrap.min.css b/src/main/resources/web/css/bootstrap.min.css old mode 100755 new mode 100644 diff --git a/src/main/resources/web/css/starter-template.css b/src/main/resources/web/css/starter-template.css old mode 100755 new mode 100644 diff --git a/src/main/resources/web/index.html b/src/main/resources/web/index.html old mode 100755 new mode 100644