Skip to content

Commit

Permalink
Added TODO-list
Browse files Browse the repository at this point in the history
  • Loading branch information
josteitv committed May 20, 2015
1 parent fbae195 commit 9dc3dba
Show file tree
Hide file tree
Showing 20 changed files with 266 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.settings
/target
/.classpath
/.project
6 changes: 6 additions & 0 deletions pom.xml
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
<version>1.4.187</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>

</dependencies>

<build>
Expand Down
Empty file modified src/assembly/assembly.xml
100755 → 100644
Empty file.
Empty file modified src/files/start.bat
100755 → 100644
Empty file.
Empty file modified src/main/java/FileServlet.java
100755 → 100644
Empty file.
Empty file modified src/main/java/Html.java
100755 → 100644
Empty file.
Empty file modified src/main/java/LoginService.java
100755 → 100644
Empty file.
Empty file modified src/main/java/LoginServlet.java
100755 → 100644
Empty file.
Empty file modified src/main/java/LogoutServlet.java
100755 → 100644
Empty file.
1 change: 1 addition & 0 deletions src/main/java/Main.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Empty file modified src/main/java/MoneyTransferServlet.java
100755 → 100644
Empty file.
139 changes: 135 additions & 4 deletions src/main/java/Repository.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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<TodoItem> 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<TodoItem> 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
}
}
}

}
Empty file modified src/main/java/SearchServlet.java
100755 → 100644
Empty file.
1 change: 1 addition & 0 deletions src/main/java/SecureServlet.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
response.setStatus(HttpServletResponse.SC_OK);

String content = "<h1>You are logged in to the secure area as '" + request.getUserPrincipal().getName() + "'.</h1>"
+ "<h3><a href='/secure/todo.html'>View TODO list</a></h3>"
+ "<h3><a href='/logout'>Log out of secure area</a></h3>";

response.getWriter().println(Html.html(content));
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/TodoItem.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
89 changes: 89 additions & 0 deletions src/main/java/TodoServlet.java
Original file line number Diff line number Diff line change
@@ -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 = "<h2>TODO</h2>";

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 += "<div class='container'>"
+ "<form method='GET' action='/secure/todo.html' "
+ "class='form-horizontal '>"
+ "<div class='form-group'>"
+ " <input type='hidden' name='id' value='" + idText + "' />"
+ " <div class='col-lg-10'>"
+ " <textarea class='form-control' rows='5' name='todo'>"
+ todoText
+ " </textarea>"
+ " </div>"
+ "</div>"
+ "<button type='submit' class='btn btn-primary pull-right'>Save</button>"
+ "</form>"
+ "</div>";

html += "<div class='container'>"
+ "<div class='col-lg-10'>"
+ "<a href='/secure/todo.html'>"
+ "&lt;create new item&gt;"
+ "</a>"
+ "</div>"
+ "</div>";

List<TodoItem> todoItems = Repository.getTodoItems(user);
for (TodoItem todoItem : todoItems) {
html += "<div class='col-lg-10'>"
+ "<a href='?id=" + todoItem.getId() + "'>"
+ escapeHtml4(todoItem.getTodo())
+ "</a>"
+ "<a href='?id=" + todoItem.getId() + "&delete=true'> [x]</a>"
+ "</div>";
}

response.getWriter().println(Html.html(html));
}

private boolean isNullOrEmpty(String id) {
return id == null || id.isEmpty();
}

}
Empty file modified src/main/resources/logback.xml
100755 → 100644
Empty file.
Empty file modified src/main/resources/web/css/bootstrap.min.css
100755 → 100644
Empty file.
Empty file modified src/main/resources/web/css/starter-template.css
100755 → 100644
Empty file.
Empty file modified src/main/resources/web/index.html
100755 → 100644
Empty file.

0 comments on commit 9dc3dba

Please sign in to comment.