From d7da0f1ec4e21a0e4abc6db8a530670a2caffc3b Mon Sep 17 00:00:00 2001 From: Anandakurniawan Date: Tue, 2 Dec 2014 04:12:32 +0700 Subject: [PATCH 1/3] membuat objek model --- src/java/model/Comment.java | 76 +++++++++++++++++++++++++++++++++++++ src/java/model/Post.java | 71 ++++++++++++++++++++++++++++++++++ src/java/model/User.java | 67 ++++++++++++++++++++++++++++++++ 3 files changed, 214 insertions(+) create mode 100644 src/java/model/Comment.java create mode 100644 src/java/model/Post.java create mode 100644 src/java/model/User.java diff --git a/src/java/model/Comment.java b/src/java/model/Comment.java new file mode 100644 index 00000000..9e412bba --- /dev/null +++ b/src/java/model/Comment.java @@ -0,0 +1,76 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package model; + +public class Comment { + + private int id; + private int post_id; + private String name; + private String email; + private long date; + private String comment; + + /** + * Creates a new instance of Comment + */ + public Comment() { + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getPost_id() { + return post_id; + } + + public void setPost_id(int post_id) { + this.post_id = post_id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public long getDate() { + return date; + } + + public void setDate(long date) { + this.date = date; + } + + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + + @Override + public String toString() { + return String.valueOf(id) + " " + String.valueOf(post_id) + " " + name + " " + email + " " + comment; + } +} diff --git a/src/java/model/Post.java b/src/java/model/Post.java new file mode 100644 index 00000000..410297be --- /dev/null +++ b/src/java/model/Post.java @@ -0,0 +1,71 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package model; + +public class Post { + + private int id; + private String title; + private long date; + private String content; + private boolean status; + + /** + * Creates a new instance of Post + */ + public Post() { + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public long getDate() { + return date; + } + + public void setDate(long date) { + this.date = date; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public boolean isStatus() { + return status; + } + + public void setStatus(boolean status) { + this.status = status; + } + + @Override + public String toString() { + if (status) { + return String.valueOf(id) + " " + title + " " + date + " " + content + " true"; + } else { + return String.valueOf(id) + " " + title + " " + date + " " + content + " false"; + } + } +} diff --git a/src/java/model/User.java b/src/java/model/User.java new file mode 100644 index 00000000..15cb9e8c --- /dev/null +++ b/src/java/model/User.java @@ -0,0 +1,67 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package model; + +public class User { + + private int id; + private String email; + private String username; + private String password; + private String role; + + /** + * Creates a new instance of User + */ + public User() { + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getRole() { + return role; + } + + public void setRole(String role) { + this.role = role; + } + + @Override + public String toString() { + return String.valueOf(id) + " " + email + " " + username + " " + password + " " + role; + } +} From a6ff84bc72a44c3b5049fcbcdfab157a5fc40907 Mon Sep 17 00:00:00 2001 From: Anandakurniawan Date: Tue, 2 Dec 2014 04:13:33 +0700 Subject: [PATCH 2/3] Controller dan database --- src/java/controller/CommentsController.java | 44 ++ src/java/controller/PostsController.java | 145 ++++++ src/java/controller/UsersController.java | 250 ++++++++++ src/java/model/database/MySQL.java | 480 ++++++++++++++++++++ 4 files changed, 919 insertions(+) create mode 100644 src/java/controller/CommentsController.java create mode 100644 src/java/controller/PostsController.java create mode 100644 src/java/controller/UsersController.java create mode 100644 src/java/model/database/MySQL.java diff --git a/src/java/controller/CommentsController.java b/src/java/controller/CommentsController.java new file mode 100644 index 00000000..eae10e41 --- /dev/null +++ b/src/java/controller/CommentsController.java @@ -0,0 +1,44 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package controller; + +import java.io.Serializable; +import java.util.List; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.SessionScoped; +import model.Comment; +import model.database.MySQL; + +@ManagedBean +@SessionScoped +public class CommentsController implements Serializable { + + /** + * Creates a new instance of CommentsController + */ + public CommentsController() { + } + + public void createComment(int post_id, String name, String email, String comment) { + MySQL mysql = new MySQL(); + + mysql.createComment(post_id, name, email, comment); + } + + public List getAllComments(int post_id) { + MySQL mysql = new MySQL(); + + return mysql.getAllComments(post_id); + } + + public boolean deleteComment(int id) { + MySQL mysql = new MySQL(); + + return mysql.deleteComment(id); + } + +} diff --git a/src/java/controller/PostsController.java b/src/java/controller/PostsController.java new file mode 100644 index 00000000..d9c63512 --- /dev/null +++ b/src/java/controller/PostsController.java @@ -0,0 +1,145 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package controller; + +import java.io.IOException; +import java.io.Serializable; +import java.util.List; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.SessionScoped; +import javax.faces.context.FacesContext; +import model.database.MySQL; +import model.Post; + +@ManagedBean(name = "postsController", eager = true) +@SessionScoped +public class PostsController implements Serializable { + + private int active_id; + private String active_title; + private long active_date; + private String active_content; + private boolean active_status; + + /** + * Creates a new instance of PostsController + */ + public PostsController() { + } + + public int getActive_id() { + return active_id; + } + + public void setActive_id(int active_id) { + this.active_id = active_id; + } + + public String getActive_title() { + return active_title; + } + + public void setActive_title(String active_title) { + this.active_title = active_title; + } + + public long getActive_date() { + return active_date; + } + + public void setActive_date(long active_date) { + this.active_date = active_date; + } + + public String getActive_content() { + return active_content; + } + + public void setActive_content(String active_content) { + this.active_content = active_content; + } + + public boolean isActive_status() { + return active_status; + } + + public void setActive_status(boolean active_status) { + this.active_status = active_status; + } + + public void createPost(String title, long date, String content) throws IOException { + MySQL mysql = new MySQL(); + + mysql.createPost(title, date, content); + FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml"); + } + + public List getAllPosts() { + MySQL mysql = new MySQL(); + + return mysql.getAllPosts(); + } + + public List getAllPublishedPosts() { + MySQL mysql = new MySQL(); + + return mysql.getAllPublishedPosts(); + } + + public List getAllUnpublishedPosts() { + MySQL mysql = new MySQL(); + + return mysql.getAllUnpublishedPosts(); + } + + public List getAllDeletedPosts() { + MySQL mysql = new MySQL(); + + return mysql.getAllDeletedPosts(); + } + + public Post getPost(int id) { + MySQL mysql = new MySQL(); + + return mysql.getPost(id); + } + + public void updatePost(int id, String title, long date, String content, boolean status) throws IOException { + MySQL mysql = new MySQL(); + + mysql.updatePost(id, title, date, content, status); + FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml"); + } + + public void deletePost(int id) { + MySQL mysql = new MySQL(); + + mysql.deletePost(id); + } + + public void viewPost(int id) throws IOException { + active_id = id; + FacesContext.getCurrentInstance().getExternalContext().redirect("view_post.xhtml"); + } + + public void editPost(int id, String title, long date, String content, boolean status) throws IOException { + active_id = id; + active_title = title; + active_date = date; + active_content = content; + active_status = status; + FacesContext.getCurrentInstance().getExternalContext().redirect("edit_post.xhtml"); + } + + public void restorePost(int id) throws IOException { + MySQL mysql = new MySQL(); + + mysql.restorePost(id); + FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml"); + } + +} diff --git a/src/java/controller/UsersController.java b/src/java/controller/UsersController.java new file mode 100644 index 00000000..97347386 --- /dev/null +++ b/src/java/controller/UsersController.java @@ -0,0 +1,250 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package controller; + +import java.io.IOException; +import java.io.Serializable; +import java.util.List; +import javax.faces.application.FacesMessage; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.SessionScoped; +import javax.faces.context.FacesContext; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import model.User; +import model.database.MySQL; + +@ManagedBean(name = "usersController", eager = true) +@SessionScoped +public class UsersController implements Serializable { + + private int saved_id; + private String saved_email; + private String saved_username; + private String saved_password; + private String saved_role; + + /** + * Creates a new instance of UsersController + */ + public UsersController() { + } + + public int getSaved_id() { + return saved_id; + } + + public void setSaved_id(int saved_id) { + this.saved_id = saved_id; + } + + public String getSaved_email() { + return saved_email; + } + + public void setSaved_email(String saved_email) { + this.saved_email = saved_email; + } + + public String getSaved_username() { + return saved_username; + } + + public void setSaved_username(String saved_username) { + this.saved_username = saved_username; + } + + public String getSaved_password() { + return saved_password; + } + + public void setSaved_password(String saved_password) { + this.saved_password = saved_password; + } + + public String getSaved_role() { + return saved_role; + } + + public void setSaved_role(String saved_role) { + this.saved_role = saved_role; + } + + public String getLoginEmail() { + String emailCookie = getCookieValue("email"); + if (emailCookie != null) { + return emailCookie; + } + + return ""; + } + + public String getLoginUsername() { + String userCookie = getCookieValue("username"); + if (userCookie != null) { + return userCookie; + } + + return "guest"; + } + + public boolean isAdmin() { + String roleCookie = getCookieValue("role"); + if (roleCookie != null) { + return roleCookie.equals("admin"); + } + + return false; + } + + public boolean isEditor() { + String roleCookie = getCookieValue("role"); + if (roleCookie != null) { + return roleCookie.equals("editor"); + } + + return false; + } + + public boolean isOwner() { + String roleCookie = getCookieValue("role"); + if (roleCookie != null) { + return roleCookie.equals("owner"); + } + + return false; + } + + public boolean isLogin() { + String roleCookie = getCookieValue("role"); + return roleCookie != null; + } + + public void createUser(String email, String username, String password, String role) throws IOException { + MySQL mysql = new MySQL(); + + if (mysql.createUser(email, username, password, role)) { + FacesContext.getCurrentInstance().getExternalContext().redirect("view_user.xhtml"); + } else { + + } + } + + public User getUser(int id) { + MySQL mysql = new MySQL(); + + return mysql.getUser(id); + } + + public List getAllUsers() { + MySQL mysql = new MySQL(); + + return mysql.getAllUsers(); + } + + public void updateUser(int id, String email, String username, String password, String role) throws IOException { + MySQL mysql = new MySQL(); + + mysql.updateUser(id, email, username, password, role); + FacesContext.getCurrentInstance().getExternalContext().redirect("view_user.xhtml"); + } + + public void deleteUser(int id) { + MySQL mysql = new MySQL(); + + mysql.deleteUser(id); + } + + public void viewUser(int id) throws IOException { + saved_id = id; + FacesContext.getCurrentInstance().getExternalContext().redirect("view_user.xhtml"); + } + + public void editUser(int id, String email, String username, String password, String role) throws IOException { + saved_id = id; + saved_email = email; + saved_username = username; + saved_password = password; + saved_role = role; + FacesContext.getCurrentInstance().getExternalContext().redirect("edit_user.xhtml"); + } + + public void login(String username, String password) throws IOException { + MySQL mysql = new MySQL(); + List users = mysql.getAllUsers(); + User user = null; + + for (User u : users) { + if (u.getUsername().equals(username) && u.getPassword().equals(password)) { + user = u; + break; + } + } + + if (user != null) { //ada user di database, mulai + Cookie email_cookie = new Cookie("email", user.getEmail()); + Cookie username_cookie = new Cookie("username", user.getUsername()); + Cookie role_cookie = new Cookie("role",user.getRole()); + email_cookie.setMaxAge(30*60); + email_cookie.setPath("/"); + username_cookie.setMaxAge(30*60); + username_cookie.setPath("/"); + role_cookie.setMaxAge(30*60); + role_cookie.setPath("/"); + FacesContext context = FacesContext.getCurrentInstance(); + HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); + response.addCookie(email_cookie); + response.addCookie(username_cookie); + response.addCookie(role_cookie); + response.sendRedirect("index.xhtml"); + } else { + FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Username or password is invalid")); + } + } + + public void logout() throws IOException { + FacesContext facesContext = FacesContext.getCurrentInstance(); + HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest(); + Cookie email_cookie = new Cookie("email",""); + Cookie username_cookie = new Cookie("username",""); + Cookie role_cookie = new Cookie("role",""); + email_cookie.setMaxAge(0); + email_cookie.setPath("/"); + username_cookie.setMaxAge(0); + username_cookie.setPath("/"); + role_cookie.setMaxAge(0); + role_cookie.setPath("/"); + HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse(); + response.setContentType("text/html"); + response.addCookie(email_cookie); + response.addCookie(username_cookie); + response.addCookie(role_cookie); + + FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); + FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml"); + } + + public String getCookieValue(String name) { + FacesContext facesContext = FacesContext.getCurrentInstance(); + HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest(); + Cookie cookie = null; + + Cookie[] userCookies = request.getCookies(); + if (userCookies != null && userCookies.length > 0 ) { + for (int i = 0; i < userCookies.length; i++) { + if (userCookies[i].getName().equals(name)) { + cookie = userCookies[i]; + return cookie.getValue(); + } + } + } + + return null; + } + +} diff --git a/src/java/model/database/MySQL.java b/src/java/model/database/MySQL.java new file mode 100644 index 00000000..07374ff8 --- /dev/null +++ b/src/java/model/database/MySQL.java @@ -0,0 +1,480 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package model.database; + +import java.sql.Connection; +import java.sql.Date; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; +import model.Comment; +import model.Post; +import model.User; + +public class MySQL { + + private static final String DATABASE_NAME = "simple_blog_jsf"; + + private static final String TABLE_USER = "user"; + private static final String TABLE_POST = "post"; + private static final String TABLE_COMMENT = "comment"; + + private Connection connect; + private Statement statement; + private PreparedStatement preparedStatement; + private ResultSet resultSet; + + public MySQL() { + open(); + } + + public Connection getConnect() { + return connect; + } + + public Statement getStatement() { + return statement; + } + + public PreparedStatement getPreparedStatement() { + return preparedStatement; + } + + public ResultSet getResultSet() { + return resultSet; + } + + public void setConnect(Connection connect) { + this.connect = connect; + } + + public void setStatement(Statement statement) { + this.statement = statement; + } + + public void setPreparedStatement(PreparedStatement preparedStatement) { + this.preparedStatement = preparedStatement; + } + + public void setResultSet(ResultSet resultSet) { + this.resultSet = resultSet; + } + + private void open() { + try { + // this will load the MySQL driver, each DB has its own driver + Class.forName("com.mysql.jdbc.Driver"); + + // setup the connection with the DB. + connect = DriverManager.getConnection( + "jdbc:mysql://localhost/simple_blog_jsf", "root", ""); + + // statements allow to issue SQL queries to the database + statement = connect.createStatement(); + + // preparedStatements can use variables and are more efficient + //preparedStatement = connect.prepareStatement(); + // resultSet gets the result of the SQL query + //resultSet = preparedStatement.executeQuery(); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + private void close() { + try { + if (resultSet != null) { + resultSet.close(); + } + if (statement != null) { + statement.close(); + } + if (preparedStatement != null) { + preparedStatement.close(); + } + if (connect != null) { + connect.close(); + } + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + // User + public boolean createUser(String email, String username, String password, String role) { + // kalo user di delete terus di create ulang masih bisa, harusnya ga bisa + try { + List users = getAllUsers(); + for (User user : users) { + if (user.getUsername().equalsIgnoreCase(username)) { + return false; + } + } + + preparedStatement = connect.prepareStatement("INSERT INTO " + TABLE_USER + "(email, username, password, role, created_at, updated_at) VALUES(?, ?, ?, ?, ?, ?)"); + preparedStatement.setString(1, email); + preparedStatement.setString(2, username); + preparedStatement.setString(3, password); + preparedStatement.setString(4, role); + preparedStatement.setTimestamp(5, new Timestamp(System.currentTimeMillis()), Calendar.getInstance()); + preparedStatement.setTimestamp(6, new Timestamp(System.currentTimeMillis()), Calendar.getInstance()); + preparedStatement.executeUpdate(); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return true; + } + + public List getAllUsers() { + List users; + users = new ArrayList<>(); + + try { + preparedStatement = connect.prepareStatement("SELECT id, email, username, password, role, deleted_at FROM " + TABLE_USER + " ORDER BY username"); + resultSet = preparedStatement.executeQuery(); + + while (resultSet.next()) { + System.out.println(resultSet.getString("deleted_at")); + if (resultSet.getString("deleted_at") == null) { + User user = new User(); + + user.setId(resultSet.getInt("id")); + user.setEmail(resultSet.getString("email")); + user.setUsername(resultSet.getString("username")); + user.setPassword(resultSet.getString("password")); + user.setRole(resultSet.getString("role")); + users.add(user); + } + } + } catch (SQLException e) { + System.out.println("error"); + e.printStackTrace(); + } + + return users; + } + + public User getUser(int id) { + User user = new User(); + try { + preparedStatement = connect.prepareStatement("SELECT email, username, password, role, deleted_at FROM " + TABLE_USER + " WHERE id = " + id); + resultSet = preparedStatement.executeQuery(); + + while (resultSet.next()) { + if (resultSet.getString("deleted_at") == null) { + user.setId(id); + user.setEmail(resultSet.getString("email")); + user.setUsername(resultSet.getString("username")); + user.setPassword(resultSet.getString("password")); + user.setRole(resultSet.getString("role")); + } + } + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return user; + } + + public boolean updateUser(int id, String email, String username, String password, String role) { + try { + preparedStatement = connect.prepareStatement("UPDATE " + TABLE_USER + " SET email = ?, username = ?, password = ?, role = ?, updated_at = ? WHERE id = " + id); + preparedStatement.setString(1, email); + preparedStatement.setString(2, username); + preparedStatement.setString(3, password); + preparedStatement.setString(4, role); + preparedStatement.setTimestamp(5, new Timestamp(System.currentTimeMillis()), Calendar.getInstance()); + preparedStatement.executeUpdate(); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return true; + } + + public boolean deleteUser(int id) { + try { + preparedStatement = connect.prepareStatement("UPDATE " + TABLE_USER + " SET deleted_at = ? WHERE id = " + id); + preparedStatement.setTimestamp(1, new Timestamp(System.currentTimeMillis()), Calendar.getInstance()); + preparedStatement.executeUpdate(); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return true; + } + + // Post + public boolean createPost(String title, long date, String content) { + try { + preparedStatement = connect.prepareStatement("INSERT INTO " + TABLE_POST + "(title, date, content, status, created_at, updated_at) VALUES(?, ?, ?, ?, ?, ?)"); + preparedStatement.setString(1, title); + preparedStatement.setDate(2, new Date(date)); + preparedStatement.setString(3, content); + preparedStatement.setBoolean(4, false); + preparedStatement.setTimestamp(5, new Timestamp(System.currentTimeMillis()), Calendar.getInstance()); + preparedStatement.setTimestamp(6, new Timestamp(System.currentTimeMillis()), Calendar.getInstance()); + preparedStatement.executeUpdate(); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return true; + } + + public List getAllPosts() { + List posts = new ArrayList<>(); + + try { + preparedStatement = connect.prepareStatement("SELECT id, title, date, content, status, deleted_at FROM " + TABLE_POST + " ORDER BY updated_at DESC"); + resultSet = preparedStatement.executeQuery(); + + while (resultSet.next()) { + if (resultSet.getString("deleted_at") == null) { + Post post = new Post(); + + post.setId(resultSet.getInt("id")); + post.setTitle(resultSet.getString("title")); + post.setDate(resultSet.getDate("date").getTime()); + post.setContent(resultSet.getString("content")); + post.setStatus(resultSet.getBoolean("status")); + posts.add(post); + } + } + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return posts; + } + + public List getAllPublishedPosts() { + List posts = new ArrayList<>(); + + try { + preparedStatement = connect.prepareStatement("SELECT id, title, date, content, deleted_at FROM " + TABLE_POST + " WHERE status = 1 ORDER BY updated_at DESC"); + resultSet = preparedStatement.executeQuery(); + + while (resultSet.next()) { + if (resultSet.getString("deleted_at") == null) { + Post post = new Post(); + + post.setId(resultSet.getInt("id")); + post.setTitle(resultSet.getString("title")); + post.setDate(resultSet.getDate("date").getTime()); + post.setContent(resultSet.getString("content")); + post.setStatus(true); + posts.add(post); + } + } + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return posts; + } + + public List getAllUnpublishedPosts() { + List posts = new ArrayList<>(); + + try { + preparedStatement = connect.prepareStatement("SELECT id, title, date, content, deleted_at FROM " + TABLE_POST + " WHERE status = 0 ORDER BY updated_at DESC"); + resultSet = preparedStatement.executeQuery(); + + while (resultSet.next()) { + if (resultSet.getString("deleted_at") == null) { + Post post = new Post(); + + post.setId(resultSet.getInt("id")); + post.setTitle(resultSet.getString("title")); + post.setDate(resultSet.getDate("date").getTime()); + post.setContent(resultSet.getString("content")); + post.setStatus(false); + posts.add(post); + } + } + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return posts; + } + + public List getAllDeletedPosts() { + List posts = new ArrayList<>(); + + try { + preparedStatement = connect.prepareStatement("SELECT id, title, date, content, status FROM " + TABLE_POST + " WHERE deleted_at IS NOT NULL ORDER BY updated_at DESC"); + resultSet = preparedStatement.executeQuery(); + System.out.println("woi"); + while (resultSet.next()) { + Post post = new Post(); + + post.setId(resultSet.getInt("id")); + post.setTitle(resultSet.getString("title")); + post.setDate(resultSet.getDate("date").getTime()); + post.setContent(resultSet.getString("content")); + post.setStatus(resultSet.getBoolean("status")); + posts.add(post); + System.out.println(post.toString()); + } + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return posts; + } + + public Post getPost(int id) { + Post post = new Post(); + try { + preparedStatement = connect.prepareStatement("SELECT title, date, content, status, deleted_at FROM " + TABLE_POST + " WHERE id = " + id); + resultSet = preparedStatement.executeQuery(); + + while (resultSet.next()) { + if (resultSet.getString("deleted_at") == null) { + post.setId(id); + post.setTitle(resultSet.getString("title")); + post.setDate(resultSet.getDate("date").getTime()); + post.setContent(resultSet.getString("content")); + post.setStatus(resultSet.getBoolean("status")); + } + } + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return post; + } + + public boolean updatePost(int id, String title, long date, String content, boolean status) { + try { + preparedStatement = connect.prepareStatement("UPDATE " + TABLE_POST + " SET title = ?, date = ?, content = ?, status = ?, updated_at = ? WHERE id = " + id); + preparedStatement.setString(1, title); + preparedStatement.setDate(2, new Date(date)); + preparedStatement.setString(3, content); + preparedStatement.setBoolean(4, status); + preparedStatement.setTimestamp(5, new Timestamp(System.currentTimeMillis()), Calendar.getInstance()); + preparedStatement.executeUpdate(); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return true; + } + + public boolean deletePost(int id) { + try { + preparedStatement = connect.prepareStatement("UPDATE " + TABLE_POST + " SET deleted_at = ? WHERE id = " + id); + preparedStatement.setTimestamp(1, new Timestamp(System.currentTimeMillis()), Calendar.getInstance()); + preparedStatement.executeUpdate(); + + deleteComment(id); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return true; + } + + public boolean restorePost(int id) { + try { + preparedStatement = connect.prepareStatement("UPDATE " + TABLE_POST + " SET deleted_at = ?, updated_at = ? WHERE id = " + id); + preparedStatement.setTimestamp(1, null); + preparedStatement.setTimestamp(2, new Timestamp(System.currentTimeMillis()), Calendar.getInstance()); + preparedStatement.executeUpdate(); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return true; + } + + // Comment + public boolean createComment(int post_id, String name, String email, String comment) { + try { + preparedStatement = connect.prepareStatement("INSERT INTO " + TABLE_COMMENT + "(post_id, name, email, comment, created_at) VALUES(?, ?, ?, ?, ?)"); + preparedStatement.setInt(1, post_id); + preparedStatement.setString(2, name); + preparedStatement.setString(3, email); + preparedStatement.setString(4, comment); + preparedStatement.setTimestamp(5, new Timestamp(System.currentTimeMillis()), Calendar.getInstance()); + preparedStatement.executeUpdate(); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return true; + } + + public List getAllComments(int post_id) { + List comments = new ArrayList<>(); + + try { + preparedStatement = connect.prepareStatement("SELECT id, name, email, comment, deleted_at, created_at FROM " + TABLE_COMMENT + " WHERE post_id = " + post_id + " ORDER BY created_at DESC"); + resultSet = preparedStatement.executeQuery(); + + while (resultSet.next()) { + if (resultSet.getString("deleted_at") == null) { + Comment comment = new Comment(); + + comment.setId(resultSet.getInt("id")); + comment.setPost_id(post_id); + comment.setName(resultSet.getString("name")); + comment.setEmail(resultSet.getString("email")); + comment.setComment(resultSet.getString("comment")); + comment.setDate(resultSet.getTimestamp("created_at").getTime()); + comments.add(comment); + } + } + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return comments; + } + + public boolean deleteComment(int id) { + try { + preparedStatement = connect.prepareStatement("UPDATE " + TABLE_COMMENT + " SET deleted_at = ? WHERE id = " + id); + preparedStatement.setTimestamp(1, new Timestamp(System.currentTimeMillis()), Calendar.getInstance()); + preparedStatement.executeUpdate(); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return true; + } + +} From aa9c504900e5da47dfc69da21995cbdffed4590b Mon Sep 17 00:00:00 2001 From: Anandakurniawan Date: Tue, 2 Dec 2014 04:14:28 +0700 Subject: [PATCH 3/3] halaman web --- web/META-INF/context.xml | 2 + web/WEB-INF/web.xml | 24 ++++++++ web/create_post.xhtml | 52 +++++++++++++++++ web/create_user.xhtml | 73 +++++++++++++++++++++++ web/edit_post.xhtml | 69 ++++++++++++++++++++++ web/edit_user.xhtml | 72 +++++++++++++++++++++++ web/index.xhtml | 74 ++++++++++++++++++++++++ web/login_user.xhtml | 39 +++++++++++++ web/publish_post.xhtml | 60 +++++++++++++++++++ web/restore_post.xhtml | 58 +++++++++++++++++++ web/view_post.xhtml | 122 +++++++++++++++++++++++++++++++++++++++ web/view_user.xhtml | 61 ++++++++++++++++++++ 12 files changed, 706 insertions(+) create mode 100644 web/META-INF/context.xml create mode 100644 web/WEB-INF/web.xml create mode 100644 web/create_post.xhtml create mode 100644 web/create_user.xhtml create mode 100644 web/edit_post.xhtml create mode 100644 web/edit_user.xhtml create mode 100644 web/index.xhtml create mode 100644 web/login_user.xhtml create mode 100644 web/publish_post.xhtml create mode 100644 web/restore_post.xhtml create mode 100644 web/view_post.xhtml create mode 100644 web/view_user.xhtml diff --git a/web/META-INF/context.xml b/web/META-INF/context.xml new file mode 100644 index 00000000..153c8c42 --- /dev/null +++ b/web/META-INF/context.xml @@ -0,0 +1,2 @@ + + diff --git a/web/WEB-INF/web.xml b/web/WEB-INF/web.xml new file mode 100644 index 00000000..a6c450c7 --- /dev/null +++ b/web/WEB-INF/web.xml @@ -0,0 +1,24 @@ + + + + javax.faces.PROJECT_STAGE + Development + + + Faces Servlet + javax.faces.webapp.FacesServlet + 1 + + + Faces Servlet + /faces/* + + + + 30 + + + + faces/index.xhtml + + diff --git a/web/create_post.xhtml b/web/create_post.xhtml new file mode 100644 index 00000000..54cf5704 --- /dev/null +++ b/web/create_post.xhtml @@ -0,0 +1,52 @@ + + + + + Create Page + + + + + + + +
+
+
+ + SIMPLE-BLOG + + +
+
+
+ +
+
+
+ + + Judul
+

+ Tanggal
+ +

+ Content
+

+ + +
+
+
+
+ +
+
+ \ No newline at end of file diff --git a/web/create_user.xhtml b/web/create_user.xhtml new file mode 100644 index 00000000..adc4d2f5 --- /dev/null +++ b/web/create_user.xhtml @@ -0,0 +1,73 @@ + + + + + Create Page + + + + +
+
+
+ + SIMPLE-BLOG + + +
+
+
+ +
+ + +
+ Create User +
+
+
+ email : +

+ username : +

+ password : +

+ role : + + + + + + +

+ +
+
+
+
+ +
+ + +
+ diff --git a/web/edit_post.xhtml b/web/edit_post.xhtml new file mode 100644 index 00000000..eee657ab --- /dev/null +++ b/web/edit_post.xhtml @@ -0,0 +1,69 @@ + + + + + Edit Page + + + + + +
+
+
+ + SIMPLE-BLOG + + +
+
+
+ +
+
+
+ + + + Judul

+

+ Tanggal

+ + + + + + + +

+ Content

+

+ + Status +

+
+ +
+
+
+
+ +
+ +
+ diff --git a/web/edit_user.xhtml b/web/edit_user.xhtml new file mode 100644 index 00000000..bc4b020e --- /dev/null +++ b/web/edit_user.xhtml @@ -0,0 +1,72 @@ + + + + + Edit Page + + + + +
+
+
+ + SIMPLE-BLOG + + +
+
+
+ +
+ + + +
+ Edit User +

+
+ email : +

+ username : +

+ password : +

+ role: + + + + + +

+ +
+
+
+
+
+ + +
+ diff --git a/web/index.xhtml b/web/index.xhtml new file mode 100644 index 00000000..329a41eb --- /dev/null +++ b/web/index.xhtml @@ -0,0 +1,74 @@ + + + + + + Index Page + + + + +
+
+
+ + SIMPLE-BLOG + + +
+
+
+
+ + + + + + +
+
+ + +
+
+
+ + + +
+ +
+
+ +

+ + + + | + + + +
+ … +
+
+
+
+
+ +
+ +
+ diff --git a/web/login_user.xhtml b/web/login_user.xhtml new file mode 100644 index 00000000..9a91f6bb --- /dev/null +++ b/web/login_user.xhtml @@ -0,0 +1,39 @@ + + + + + Login Page + + + + +
+
+
+ + SIMPLE-BLOG + +
+
+
+
+ + +
+ LOGIN +

+
+ username : +

+ password : +

+ +
+
+
+
+ +
+
+ diff --git a/web/publish_post.xhtml b/web/publish_post.xhtml new file mode 100644 index 00000000..d8539382 --- /dev/null +++ b/web/publish_post.xhtml @@ -0,0 +1,60 @@ + + + + + Publish Page + + + + +
+
+
+ + SIMPLE-BLOG + + +
+
+
+ +
+ + + +
+
+
+ +
+ +
+
+ +

+ + + | + + +
+
+
+
+
+ +
+
+ diff --git a/web/restore_post.xhtml b/web/restore_post.xhtml new file mode 100644 index 00000000..a5bb1b83 --- /dev/null +++ b/web/restore_post.xhtml @@ -0,0 +1,58 @@ + + + + + Restore Page + + + + +
+
+
+ + SIMPLE-BLOG + + +
+
+
+ +
+ + + +
+
+
+ +
+ +
+
+ +

+ + + +
+
+
+
+
+ +
+
+ diff --git a/web/view_post.xhtml b/web/view_post.xhtml new file mode 100644 index 00000000..9739294c --- /dev/null +++ b/web/view_post.xhtml @@ -0,0 +1,122 @@ + + + + + View Page + + + +
+
+
+ + SIMPLE-BLOG + + +
+
+
+ +
+ + +
+

+
+
+ + + +
+
+

+

+
+ +
+ COMMENTS +
+ +
+ +
+ + +
+ +

+ +
+ +

+
+ +
+ +

+ + + + + + + + + + +
+
+
+
+ + +
+ +
+
+ +
+
+ + + +
+
+ +
+ +
+
+
+
+
+
+
+
+ +
+ + +
+ diff --git a/web/view_user.xhtml b/web/view_user.xhtml new file mode 100644 index 00000000..505684b4 --- /dev/null +++ b/web/view_user.xhtml @@ -0,0 +1,61 @@ + + + + + User Page + + + + +
+
+
+ + SIMPLE-BLOG + + +
+
+
+
+ + +
+
+
    + + + + + + + +   + + +   + + +   + + + + +
+
+
+
+
+
+
+