diff --git a/build.xml b/build.xml new file mode 100644 index 00000000..f75e456c --- /dev/null +++ b/build.xml @@ -0,0 +1,71 @@ + + + + + + + + + + + Builds, tests, and runs the project Simple-Blog3. + + + diff --git a/index.html b/index.html deleted file mode 100644 index 07cff0ba..00000000 --- a/index.html +++ /dev/null @@ -1,114 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Simple Blog - - - - - -
- - - -
-
- -
-
- - - -
- - - - - - - - \ No newline at end of file diff --git a/nbproject/ant-deploy.xml b/nbproject/ant-deploy.xml new file mode 100644 index 00000000..9bc5adf7 --- /dev/null +++ b/nbproject/ant-deploy.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml new file mode 100644 index 00000000..b7fa386c --- /dev/null +++ b/nbproject/build-impl.xml @@ -0,0 +1,1446 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set build.web.dir + Must set build.generated.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.war + + + + + + + + + +The Java EE server classpath is not correctly set up - server home directory is missing. +Either open the project in the IDE and assign the server or setup the server classpath manually. +For example like this: + ant -Dj2ee.server.home=<app_server_installation_directory> + + +The Java EE server classpath is not correctly set up. Your active server type is ${j2ee.server.type}. +Either open the project in the IDE and assign the server or setup the server classpath manually. +For example like this: + ant -Duser.properties.file=<path_to_property_file> (where you put the property "j2ee.platform.classpath" in a .properties file) +or ant -Dj2ee.platform.classpath=<server_classpath> (where no properties file is used) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +The libs.CopyLibs.classpath property is not set up. +This property must point to +org-netbeans-modules-java-j2seproject-copylibstask.jar file which is part +of NetBeans IDE installation and is usually located at +<netbeans_installation>/java<version>/ant/extra folder. +Either open the project in the IDE and make sure CopyLibs library +exists or setup the property manually. For example like this: + ant -Dlibs.CopyLibs.classpath=a/path/to/org-netbeans-modules-java-j2seproject-copylibstask.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.jsp.includes + + + + + + + + + + + + + + + + + + + + + + + + + + Must select a file in the IDE or set jsp.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Browser not found, cannot launch the deployed application. Try to set the BROWSER environment variable. + + + Launching ${browse.url} + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nbproject/faces-config.NavData b/nbproject/faces-config.NavData new file mode 100644 index 00000000..298bfc50 --- /dev/null +++ b/nbproject/faces-config.NavData @@ -0,0 +1,6 @@ + + + + + + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties new file mode 100644 index 00000000..59903876 --- /dev/null +++ b/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=3133e380 +build.xml.script.CRC32=d07eaf0e +build.xml.stylesheet.CRC32=651128d4@1.67.1.1 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=3133e380 +nbproject/build-impl.xml.script.CRC32=c4625538 +nbproject/build-impl.xml.stylesheet.CRC32=99ea4b56@1.67.1.1 diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 00000000..b7520ca5 --- /dev/null +++ b/nbproject/project.properties @@ -0,0 +1,98 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=true +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +auxiliary.org-netbeans-modules-css-prep.less_2e_compiler_2e_options= +auxiliary.org-netbeans-modules-css-prep.less_2e_enabled=false +auxiliary.org-netbeans-modules-css-prep.less_2e_mappings=/less:/css +auxiliary.org-netbeans-modules-css-prep.sass_2e_compiler_2e_options= +auxiliary.org-netbeans-modules-css-prep.sass_2e_enabled=false +auxiliary.org-netbeans-modules-css-prep.sass_2e_mappings=/scss:/css +auxiliary.org-netbeans-modules-projectapi.jsf_2e_language=JSP +auxiliary.org-netbeans-modules-web-clientproject-api.js_2e_libs_2e_folder=assets/js +build.classes.dir=${build.web.dir}/WEB-INF/classes +build.classes.excludes=**/*.java,**/*.form +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +build.web.dir=${build.dir}/web +build.web.excludes=${build.classes.excludes} +client.urlPart= +compile.jsps=false +conf.dir=${source.root}/conf +debug.classpath=${build.classes.dir}:${javac.classpath} +debug.test.classpath=\ + ${run.test.classpath} +display.browser=true +# Files to be excluded from distribution war +dist.archive.excludes= +dist.dir=dist +dist.ear.war=${dist.dir}/${war.ear.name} +dist.javadoc.dir=${dist.dir}/javadoc +dist.war=${dist.dir}/${war.name} +endorsed.classpath=\ + ${libs.javaee-endorsed-api-6.0.classpath} +excludes= +file.reference.mysql-connector-java-3.1.14-bin.jar=../../DeveloperTool/mysql-connector-java-3.1.14/mysql-connector-java-3.1.14-bin.jar +file.reference.mysql-connector-java-5.1.18-bin.jar=E:/ITB 2012/TEKNIK INFORMATIKA 2012 NIM 13512015/Semester 5/Pengembangan Aplikasi Berbasis Web/Tugas/Tubes 2/mysql-connector-java-5.1.18-bin.jar +file.reference.mysql-connector-java-5.1.34-bin.jar=C:/Program Files (x86)/MySQL/MySQL Connector J/mysql-connector-java-5.1.34-bin.jar +includes=** +j2ee.compile.on.save=true +j2ee.copy.static.files.on.save=true +j2ee.deploy.on.save=true +j2ee.platform=1.6-web +j2ee.platform.classpath=${j2ee.server.home}/lib/annotations-api.jar:${j2ee.server.home}/lib/catalina-ant.jar:${j2ee.server.home}/lib/catalina-ha.jar:${j2ee.server.home}/lib/catalina-tribes.jar:${j2ee.server.home}/lib/catalina.jar:${j2ee.server.home}/lib/ecj-4.2.1.jar:${j2ee.server.home}/lib/el-api.jar:${j2ee.server.home}/lib/jasper-el.jar:${j2ee.server.home}/lib/jasper.jar:${j2ee.server.home}/lib/jsp-api.jar:${j2ee.server.home}/lib/servlet-api.jar:${j2ee.server.home}/lib/tomcat-api.jar:${j2ee.server.home}/lib/tomcat-coyote.jar:${j2ee.server.home}/lib/tomcat-dbcp.jar:${j2ee.server.home}/lib/tomcat-i18n-es.jar:${j2ee.server.home}/lib/tomcat-i18n-fr.jar:${j2ee.server.home}/lib/tomcat-i18n-ja.jar:${j2ee.server.home}/lib/tomcat-jdbc.jar:${j2ee.server.home}/lib/tomcat-util.jar +j2ee.server.type=Tomcat +jar.compress=false +javac.classpath=\ + ${libs.jsf20.classpath}:\ + ${file.reference.mysql-connector-java-5.1.18-bin.jar}:\ + ${file.reference.mysql-connector-java-5.1.34-bin.jar}:\ + ${file.reference.mysql-connector-java-3.1.14-bin.jar} +# Space-separated list of extra javac options +javac.compilerargs= +javac.debug=true +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.6 +javac.target=1.6 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.preview=true +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +lib.dir=${web.docbase.dir}/WEB-INF/lib +persistence.xml.dir=${conf.dir} +platform.active=default_platform +resource.dir=setup +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +# Space-separated list of JVM arguments used when running a class with a main method or a unit test +# (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value): +runmain.jvmargs= +source.encoding=UTF-8 +source.root=src +src.dir=${source.root}/java +test.src.dir=test +war.content.additional= +war.ear.name=${war.name} +war.name=Simple-Blog3.war +web.docbase.dir=web +webinf.dir=web/WEB-INF diff --git a/nbproject/project.xml b/nbproject/project.xml new file mode 100644 index 00000000..536c458b --- /dev/null +++ b/nbproject/project.xml @@ -0,0 +1,33 @@ + + + org.netbeans.modules.web.project + + + Simple-Blog3 + 1.6.5 + + + ${libs.jsf20.classpath} + WEB-INF/lib + + + ${file.reference.mysql-connector-java-5.1.18-bin.jar} + + + ${file.reference.mysql-connector-java-5.1.34-bin.jar} + + + ${file.reference.mysql-connector-java-3.1.14-bin.jar} + WEB-INF/lib + + + + + + + + + + + + diff --git a/new_post.html b/new_post.html deleted file mode 100644 index fc4e18ee..00000000 --- a/new_post.html +++ /dev/null @@ -1,111 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Simple Blog | Tambah Post - - - - - -
- - - -
- - -

-

- -
-
-

Tambah Post

- -
-
- - - - - - -
- - - -
-
-
-
- -
- - - -
- - - - - - - - \ No newline at end of file diff --git a/post.html b/post.html deleted file mode 100644 index c0b6f9e6..00000000 --- a/post.html +++ /dev/null @@ -1,140 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Simple Blog | Apa itu Simple Blog? - - - - - -
- - - -
- -
-
- -

Apa itu Simple Blog?

-

-
-
- -
-
-
-

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Blanditiis aliquam minus consequuntur amet nulla eius, neque beatae, nostrum possimus, officiis eaque consectetur. Sequi sunt maiores dolore, illum quidem eos explicabo! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Magnam consequuntur consequatur molestiae saepe sed, incidunt sunt inventore minima voluptatum adipisci hic, est ipsa iste. Nobis, aperiam provident quae. Reprehenderit, iste.

-

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Asperiores animi tenetur nam delectus eveniet iste non culpa laborum provident minima numquam excepturi rem commodi, officia accusamus eos voluptates obcaecati. Possimus?

- -
- -

Komentar

- -
-
- - - - - - -
- - - -
-
- -
    -
  • -
    -

    Jems

    -
    2 menit lalu
    -
    -

    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perferendis repudiandae quae natus quos alias eos repellendus a obcaecati cupiditate similique quibusdam, atque omnis illum, minus ex dolorem facilis tempora deserunt! …

    -
  • - -
  • -
    -

    Kave

    -
    1 jam lalu
    -
    -

    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perferendis repudiandae quae natus quos alias eos repellendus a obcaecati cupiditate similique quibusdam, atque omnis illum, minus ex dolorem facilis tempora deserunt! …

    -
  • -
-
-
- -
- - - -
- - - - - - - - \ No newline at end of file diff --git a/simple_blog.sql b/simple_blog.sql new file mode 100644 index 00000000..e5116b5e --- /dev/null +++ b/simple_blog.sql @@ -0,0 +1,70 @@ +-- phpMyAdmin SQL Dump +-- version 4.1.12 +-- http://www.phpmyadmin.net +-- +-- Host: 127.0.0.1 +-- Generation Time: Nov 23, 2014 at 09:26 AM +-- Server version: 5.6.16 +-- PHP Version: 5.5.11 + +SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; +SET time_zone = "+00:00"; + + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8 */; + +-- +-- Database: `simple_blog` +-- + +-- -------------------------------------------------------- + +-- +-- Table structure for table `komentar` +-- + +CREATE TABLE IF NOT EXISTS `komentar` ( + `Id_Komentar` int(10) NOT NULL AUTO_INCREMENT, + `Nama` varchar(40) NOT NULL, + `Tanggal` date NOT NULL, + `Komentar` text NOT NULL, + `Id_Post` int(10) NOT NULL, + `Id_User` int(10) NOT NULL, + PRIMARY KEY (`Id_Komentar`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `post` +-- + +CREATE TABLE IF NOT EXISTS `post` ( + `Id_Post` int(10) NOT NULL AUTO_INCREMENT, + `Judul` varchar(100) NOT NULL, + `Tanggal` date NOT NULL, + `Konten` text NOT NULL, + `Id_User` int(10) NOT NULL, + PRIMARY KEY (`Id_Post`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `user` +-- + +CREATE TABLE IF NOT EXISTS `user` ( + `Id_User` int(10) NOT NULL AUTO_INCREMENT, + `Username` varchar(40) NOT NULL, + `Password` varchar(20) NOT NULL, + `Role` varchar(10) NOT NULL, + PRIMARY KEY (`Id_User`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; + +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; diff --git a/src/conf/MANIFEST.MF b/src/conf/MANIFEST.MF new file mode 100644 index 00000000..59499bce --- /dev/null +++ b/src/conf/MANIFEST.MF @@ -0,0 +1,2 @@ +Manifest-Version: 1.0 + diff --git a/src/java/Bean/AddCommentBean.java b/src/java/Bean/AddCommentBean.java new file mode 100644 index 00000000..306673c2 --- /dev/null +++ b/src/java/Bean/AddCommentBean.java @@ -0,0 +1,153 @@ +/* + * 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 Bean; + +import Database.DatabaseAccess; +import Model.Comment; +import Model.Comments; +import Model.User; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.Date; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.ManagedProperty; +import javax.faces.bean.RequestScoped; + +/** + * + * @author kevinyu + */ +@ManagedBean +@RequestScoped +public class AddCommentBean { + + @ManagedProperty(value="#{param.postId}") + private String postId; + + @ManagedProperty(value="#{user}") + private User user; + + private ArrayList comments; + + private String name; + private String email; + private String text; + + public AddCommentBean() { + + } + + public String getName() { + if (name==null) { + if (!user.getRole().equals("guest")) { + name = user.getUsername(); + } + } + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + if (email==null) { + if (!user.getRole().equals("guest")) { + email = user.getEmail(); + } + } + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public String getPostId() { + return postId; + } + + public void setPostId(String postId) { + this.postId = postId; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public ArrayList getComments() { + if (comments==null) { + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + + try { + dbManager.openConnection(); + } catch (SQLException ex) { + Logger.getLogger(AddCommentBean.class.getName()).log(Level.SEVERE, null, ex); + } + + comments = Comments.getInstance().getCommentByPostId(Integer.parseInt(postId)); + + try { + dbManager.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(AddCommentBean.class.getName()).log(Level.SEVERE, null, ex); + } + } + return comments; + } + + public void setComments(ArrayList comments) { + this.comments = comments; + } + + public void addComment() { + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + + try { + dbManager.openConnection(); + } catch (SQLException ex) { + Logger.getLogger(AddCommentBean.class.getName()).log(Level.SEVERE, null, ex); + } + + Comment comment = new Comment(); + comment.setName(name); + comment.setEmail(email); + comment.setText(text); + comment.setCreatorId(user.getId()); + comment.setGuest(user.getRole().equals("guest")); + Date date = new Date(); + Timestamp ts = new Timestamp(date.getTime()); + comment.setTimestamp(ts); + comment.setPostId(Integer.parseInt(postId)); + + Comments.getInstance().addComment(comment); + + comments.add(comment); + + try { + dbManager.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(AddCommentBean.class.getName()).log(Level.SEVERE, null, ex); + } + + } +} diff --git a/src/java/Bean/AddPostBean.java b/src/java/Bean/AddPostBean.java new file mode 100644 index 00000000..a0421021 --- /dev/null +++ b/src/java/Bean/AddPostBean.java @@ -0,0 +1,69 @@ +/* + * 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 Bean; + +import Database.DatabaseAccess; +import Model.Post; +import Model.Posts; +import Model.User; +import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.ManagedProperty; +import javax.faces.bean.RequestScoped; + +/** + * + * @author kevinyu + */ +@ManagedBean +@RequestScoped +public class AddPostBean { + + @ManagedProperty(value="#{user}") + User user; + + Post post; + + public AddPostBean() { + post = new Post(); + } + + public void setPost(Post post) { + this.post = post; + } + + public void setUser(User newUser){ + this.user = newUser; + } + + public Post getPost() { + return post; + } + + public User getUser(){ + return user; + } + + public String addPost(){ + post.setCreatorId(user.getId()); + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + try { + dbManager.openConnection(); + } catch (SQLException ex) { + Logger.getLogger(AddPostBean.class.getName()).log(Level.SEVERE, null, ex); + } + Posts.getInstance().insertPostDB(post); + try { + dbManager.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(AddPostBean.class.getName()).log(Level.SEVERE, null, ex); + } + return "new_post?faces-redirect=true"; + } +} diff --git a/src/java/Bean/CookieHelper.java b/src/java/Bean/CookieHelper.java new file mode 100644 index 00000000..c749495b --- /dev/null +++ b/src/java/Bean/CookieHelper.java @@ -0,0 +1,69 @@ +package Bean; + + +import javax.faces.context.FacesContext; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/* + * 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. + */ + +/** + * + * @author kevinyu + */ +public class CookieHelper { + +public static void setCookie(String name, String value,int expiry) { + + 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]; + break; + } + } + } + + if (cookie != null) { + cookie.setValue(value); + } else { + cookie = new Cookie(name, value); + cookie.setPath(request.getContextPath()); + } + + cookie.setMaxAge(expiry); + + HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse(); + response.addCookie(cookie); +} + +public static Cookie getCookie(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; + } + } + } + return null; +} +} \ No newline at end of file diff --git a/src/java/Bean/CreateUserBean.java b/src/java/Bean/CreateUserBean.java new file mode 100644 index 00000000..76ed995a --- /dev/null +++ b/src/java/Bean/CreateUserBean.java @@ -0,0 +1,55 @@ +/* + * 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 Bean; + +import Database.DatabaseAccess; +import Model.User; +import Model.Users; +import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.RequestScoped; + +/** + * + * @author kevinyu + */ +@ManagedBean +@RequestScoped +public class CreateUserBean { + private User user; + + public CreateUserBean() { + user = new User(); + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public String createUser() { + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + try { + dbManager.openConnection(); + } catch (SQLException ex) { + Logger.getLogger(AddPostBean.class.getName()).log(Level.SEVERE, null, ex); + } + Users.getInstance().addUser(user); + try { + dbManager.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(AddPostBean.class.getName()).log(Level.SEVERE, null, ex); + } + return "new_user?faces-redirect=true"; + } + +} diff --git a/src/java/Bean/DeletePostBean.java b/src/java/Bean/DeletePostBean.java new file mode 100644 index 00000000..b1c8c746 --- /dev/null +++ b/src/java/Bean/DeletePostBean.java @@ -0,0 +1,103 @@ +/* + * 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 Bean; + +import Database.DatabaseAccess; +import Model.Comments; +import Model.Post; +import Model.Posts; +import Test.UsersTest; +import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.ManagedProperty; +import javax.faces.bean.RequestScoped; + +/** + * + * @author kevinyu + */ +@ManagedBean +@RequestScoped +public class DeletePostBean { + + @ManagedProperty(value="#{param.postId}") + private String postId; + + public String getPostId() { + return postId; + } + + public void setPostId(String postId) { + this.postId = postId; + } + + public String softDeletePost() { + + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + try { + dbManager.openConnection(); + } catch (SQLException ex) { + Logger.getLogger(UsersTest.class.getName()).log(Level.SEVERE, null, ex); + } + + Post post = Posts.getInstance().findPost(Integer.parseInt(postId)); + post.setDeleted(true); + Posts.getInstance().updatePostDB(post); + try { + dbManager.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(UsersTest.class.getName()).log(Level.SEVERE, null, ex); + } + + return "user_management.jsf?faces-redirect=true"; + } + + public String deletePost() { + + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + try { + dbManager.openConnection(); + } catch (SQLException ex) { + Logger.getLogger(UsersTest.class.getName()).log(Level.SEVERE, null, ex); + } + + Posts.getInstance().removePost(Integer.parseInt(postId)); + Comments.getInstance().deleteCommentByPostId(Integer.parseInt(postId)); + try { + dbManager.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(UsersTest.class.getName()).log(Level.SEVERE, null, ex); + } + + return "deleted_post.jsf?faces-redirect=true"; + } + + public String undeletePost() { + + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + try { + dbManager.openConnection(); + } catch (SQLException ex) { + Logger.getLogger(UsersTest.class.getName()).log(Level.SEVERE, null, ex); + } + + Post post = Posts.getInstance().findPost(Integer.parseInt(postId)); + post.setDeleted(false); + Posts.getInstance().updatePostDB(post); + + try { + dbManager.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(UsersTest.class.getName()).log(Level.SEVERE, null, ex); + } + + return "deleted_post.jsf?faces-redirect=true"; + } + +} diff --git a/src/java/Bean/DeleteUserBean.java b/src/java/Bean/DeleteUserBean.java new file mode 100644 index 00000000..5e35d0aa --- /dev/null +++ b/src/java/Bean/DeleteUserBean.java @@ -0,0 +1,75 @@ +/* + * 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 Bean; + +import Database.DatabaseAccess; +import Model.User; +import Model.Users; +import Test.UsersTest; +import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.ManagedProperty; +import javax.faces.bean.RequestScoped; + +/** + * + * @author kevinyu + */ +@ManagedBean +@RequestScoped +public class DeleteUserBean { + + private User user; + + @ManagedProperty(value="#{param.userId}") + private String userId; + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public DeleteUserBean() { + this.user = new User(); + } + + public void setUser(User user) { + this.user = user; + } + + public User getUser() { + return user; + } + + public String deleteUser() { + + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + try { + dbManager.openConnection(); + } catch (SQLException ex) { + Logger.getLogger(UsersTest.class.getName()).log(Level.SEVERE, null, ex); + } + + user.setId(Integer.parseInt(userId)); + + Users.getInstance().deleteUserByUserId(user); + + try { + dbManager.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(DeleteUserBean.class.getName()).log(Level.SEVERE, null, ex); + } + + return "read_user?faces-redirect=true"; + } + +} diff --git a/src/java/Bean/DeletedPostBean.java b/src/java/Bean/DeletedPostBean.java new file mode 100644 index 00000000..02f37255 --- /dev/null +++ b/src/java/Bean/DeletedPostBean.java @@ -0,0 +1,50 @@ +/* + * 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 Bean; + +import Database.DatabaseAccess; +import Model.Post; +import Model.Posts; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.RequestScoped; + +/** + * + * @author kevinyu + */ +@ManagedBean +@RequestScoped +public class DeletedPostBean { + + private ArrayList deletedPosts; + + public ArrayList getDeletedPosts() { + return deletedPosts; + } + + public void setDeletedPost(ArrayList deletedPost) { + this.deletedPosts = deletedPost; + } + + public DeletedPostBean() { + try { + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + dbManager.openConnection(); + + deletedPosts = Posts.getInstance().getDeletedPost(true); + + dbManager.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(PublishPostBean.class.getName()).log(Level.SEVERE, null, ex); + } + } + +} diff --git a/src/java/Bean/EditPostBean.java b/src/java/Bean/EditPostBean.java new file mode 100644 index 00000000..e68c6309 --- /dev/null +++ b/src/java/Bean/EditPostBean.java @@ -0,0 +1,95 @@ +/* + * 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 Bean; + +import Database.DatabaseAccess; +import Model.Post; +import Model.Posts; +import Test.UsersTest; +import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.ManagedProperty; +import javax.faces.bean.RequestScoped; + +/** + * + * @author kevinyu + */ +@ManagedBean +@RequestScoped +public class EditPostBean { + + @ManagedProperty(value="#{param.postId}") + private String postId; + + private Post post; + + public String getPostId() { + return postId; + } + + public void setPostId(String postId) { + this.postId = postId; + } + + public Post getPost() { + if (post==null) { + if (postId!=null){ + getPostFromPostId(); + } + else { + post = new Post(); + } + } + return post; + } + + private void getPostFromPostId() { + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + try { + dbManager.openConnection(); + } catch (SQLException ex) { + Logger.getLogger(UsersTest.class.getName()).log(Level.SEVERE, null, ex); + } + + post = Posts.getInstance().findPost(Integer.parseInt(postId)); + + try { + dbManager.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(EditPostBean.class.getName()).log(Level.SEVERE, null, ex); + } + } + + public void setPost(Post post) { + this.post = post; + } + + public String editPost() { + + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + try { + dbManager.openConnection(); + } catch (SQLException ex) { + Logger.getLogger(UsersTest.class.getName()).log(Level.SEVERE, null, ex); + } + + post.setId(Integer.parseInt(postId)); + Posts.getInstance().updatePostDB(post); + + try { + dbManager.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(EditPostBean.class.getName()).log(Level.SEVERE, null, ex); + } + return "user_management.jsf"; + + } + +} diff --git a/src/java/Bean/IndexBean.java b/src/java/Bean/IndexBean.java new file mode 100644 index 00000000..ec4b04c2 --- /dev/null +++ b/src/java/Bean/IndexBean.java @@ -0,0 +1,55 @@ +/* + * 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 Bean; + +import Database.DatabaseAccess; +import Model.Post; +import Model.Posts; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.RequestScoped; + +/** + * + * @author kevinyu + */ +@ManagedBean +@RequestScoped +public class IndexBean { + + private ArrayList posts; + + public ArrayList getPosts() { + return posts; + } + + public void setPosts(ArrayList posts) { + this.posts = posts; + } + + public IndexBean() { + try { + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + + dbManager.openConnection(); + posts = Posts.getInstance().getPublishedPost(); + Collections.reverse(posts); + dbManager.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(IndexBean.class.getName()).log(Level.SEVERE, null, ex); + } + } + + + public String goToEditPost(){ + return "admin_edit_post?faces-redirect=true"; + } +} diff --git a/src/java/Bean/LoginBean.java b/src/java/Bean/LoginBean.java new file mode 100644 index 00000000..c60b1f1b --- /dev/null +++ b/src/java/Bean/LoginBean.java @@ -0,0 +1,115 @@ +package Bean; + + +import Database.DatabaseAccess; +import Model.User; +import Model.Users; +import java.io.IOException; +import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.ManagedProperty; +import javax.faces.bean.RequestScoped; +import javax.faces.context.FacesContext; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; + +/* + * 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. + */ + +/** + * + * @author kevinyu + */ +@ManagedBean +@RequestScoped +public class LoginBean { + private String username; + private String password; + + @ManagedProperty(value="#{user}") + private User user; + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public String getPassword() { + return password; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public void setPassword(String password) { + this.password = password; + } + + public String doNavigation() { + boolean isExist = true; + User new_user; + new_user = Users.getInstance().validateUser(username,password); + isExist = new_user!=null; + + if (isExist){ + user.setId(new_user.getId()); + user.setPassword(new_user.getPassword()); + user.setRole(new_user.getRole()); + user.setUsername(new_user.getUsername()); + user.setEmail(new_user.getEmail()); + HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest(); + CookieHelper.setCookie("userID", Integer.toString(user.getId()), 100); + return "user_management?faces-redirect=true"; + } + else { + return "login?faces-redirect=true"; + } + } + + public void check() { + Cookie cookie = CookieHelper.getCookie("userID"); + User new_user=null; + if (cookie != null){ + + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + + try { + dbManager.openConnection(); + } catch (SQLException ex) { + Logger.getLogger(LoginBean.class.getName()).log(Level.SEVERE, null, ex); + } + new_user = Users.getInstance().findUser(Integer.parseInt(cookie.getValue())); + try { + dbManager.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(LoginBean.class.getName()).log(Level.SEVERE, null, ex); + } + if (new_user!=null) { + user.setId(new_user.getId()); + user.setPassword(new_user.getPassword()); + user.setRole(new_user.getRole()); + user.setUsername(new_user.getUsername()); + user.setEmail(new_user.getEmail()); + try { + FacesContext.getCurrentInstance().getExternalContext().redirect("user_management.jsf"); + } catch (IOException ex) { + Logger.getLogger(LoginBean.class.getName()).log(Level.SEVERE, null, ex); + } + } + } + } +} + diff --git a/src/java/Bean/LogoutBean.java b/src/java/Bean/LogoutBean.java new file mode 100644 index 00000000..29995a2f --- /dev/null +++ b/src/java/Bean/LogoutBean.java @@ -0,0 +1,26 @@ +/* + * 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 Bean; + +import javax.faces.bean.ManagedBean; +import javax.faces.bean.RequestScoped; +import javax.faces.context.FacesContext; + +/** + * + * @author wira gotama + */ +@ManagedBean +@RequestScoped +public class LogoutBean { + + public String logout(){ + CookieHelper.setCookie("userID", "-1", 100); + FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); + return "login.jsf?faces-redirect=true"; + } +} diff --git a/src/java/Bean/PublishPostBean.java b/src/java/Bean/PublishPostBean.java new file mode 100644 index 00000000..939f1e55 --- /dev/null +++ b/src/java/Bean/PublishPostBean.java @@ -0,0 +1,59 @@ +/* + * 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 Bean; + +import Database.DatabaseAccess; +import Model.Post; +import Model.Posts; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.ManagedProperty; +import javax.faces.bean.RequestScoped; + +/** + * + * @author kevinyu + */ +@ManagedBean +@RequestScoped +public class PublishPostBean { + + @ManagedProperty(value="#{param.postId}") + private String postId; + + public String getPostId() { + return postId; + } + + public void setPostId(String postId) { + this.postId = postId; + } + + public String publishPost() { + + + try { + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + dbManager.openConnection(); + + Post post = Posts.getInstance().findPost(Integer.parseInt(postId)); + post.setPublished(true); + + Posts.getInstance().updatePostDB(post); + + dbManager.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(PublishPostBean.class.getName()).log(Level.SEVERE, null, ex); + } + + return "list_post?faces-redirect=true"; + } + +} diff --git a/src/java/Bean/ReadUserBean.java b/src/java/Bean/ReadUserBean.java new file mode 100644 index 00000000..2b0aa4a1 --- /dev/null +++ b/src/java/Bean/ReadUserBean.java @@ -0,0 +1,48 @@ +package Bean; + +import Database.DatabaseAccess; +import Model.User; +import Model.Users; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.RequestScoped; + +/* + * 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. + */ + +/** + * + * @author kevinyu + */ +@ManagedBean +@RequestScoped +public class ReadUserBean { + + private ArrayList userList; + + public ReadUserBean() { + try { + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + dbManager.openConnection(); + userList = Users.getInstance().getAllUser(); + dbManager.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(ReadUserBean.class.getName()).log(Level.SEVERE, null, ex); + } + } + + public ArrayList getUserList() { + return this.userList; + } + + public void setUserList(ArrayList userList) { + this.userList = userList; + } + +} diff --git a/src/java/Bean/RoleList.java b/src/java/Bean/RoleList.java new file mode 100644 index 00000000..f28cc8c0 --- /dev/null +++ b/src/java/Bean/RoleList.java @@ -0,0 +1,28 @@ +/* + * 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 Bean; + +import javax.faces.bean.ApplicationScoped; +import javax.faces.bean.ManagedBean; + +/** + * + * @author kevinyu + */ +@ManagedBean +@ApplicationScoped +public class RoleList { + private String[] roles = {"owner","editor","admin"}; + + public String[] getRoles() { + return roles; + } + + public void setRoles(String[] roles) { + this.roles = roles; + } +} diff --git a/src/java/Bean/UnpublishedPost.java b/src/java/Bean/UnpublishedPost.java new file mode 100644 index 00000000..fd7369e7 --- /dev/null +++ b/src/java/Bean/UnpublishedPost.java @@ -0,0 +1,49 @@ +/* + * 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 Bean; + +import Database.DatabaseAccess; +import Model.Post; +import Model.Posts; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.RequestScoped; + +/** + * + * @author kevinyu + */ +@ManagedBean +@RequestScoped +public class UnpublishedPost { + + private ArrayList unpublishedPosts; + + public ArrayList getUnpublishedPosts() { + return unpublishedPosts; + } + + public void setUnpublishedPosts(ArrayList unpublishedPosts) { + this.unpublishedPosts = unpublishedPosts; + } + + public UnpublishedPost() { + try { + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + dbManager.openConnection(); + + unpublishedPosts = Posts.getInstance().getUnpublishedPost(); + + dbManager.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(PublishPostBean.class.getName()).log(Level.SEVERE, null, ex); + } + } +} diff --git a/src/java/Bean/UpdateUserBean.java b/src/java/Bean/UpdateUserBean.java new file mode 100644 index 00000000..cf060a35 --- /dev/null +++ b/src/java/Bean/UpdateUserBean.java @@ -0,0 +1,92 @@ +/* + * 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 Bean; + +import Database.DatabaseAccess; +import Model.User; +import Model.Users; +import java.sql.SQLException; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.ManagedProperty; +import javax.faces.bean.RequestScoped; +import javax.faces.context.FacesContext; + +/** + * + * @author kevinyu + */ +@ManagedBean +@RequestScoped +public class UpdateUserBean { + + @ManagedProperty(value="#{param.userId}") + private String userId; + + private User user; + + public User getUser() { + if (user==null){ + if (userId!=null) { + getUserFromUserId(); + } + else{ + user = new User(); + } + } + return user; + } + + private void getUserFromUserId(){ + try { + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + + dbManager.openConnection(); + user = Users.getInstance().findUser(Integer.parseInt(userId)); + dbManager.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(UpdateUserBean.class.getName()).log(Level.SEVERE, null, ex); + } + } + + public void setUser(User user) { + this.user = user; + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public String updateUser() { + DatabaseAccess databaseAccess = DatabaseAccess.getInstance(); + try { + databaseAccess.openConnection(); + System.out.println("Berhasil membuka koneksi"); + } catch (SQLException e) { + System.out.println("Gagal membuka koneksi"); + System.out.println(e); + } + Map params = + FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap(); + user.setId(Integer.parseInt(params.get("userId"))); + Users.getInstance().updateUser(this.user); + + try { + databaseAccess.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(UpdateUserBean.class.getName()).log(Level.SEVERE, null, ex); + } + + return "read_user?faces-redirect=true"; + } +} diff --git a/src/java/Bean/ViewPostBean.java b/src/java/Bean/ViewPostBean.java new file mode 100644 index 00000000..7a1d736f --- /dev/null +++ b/src/java/Bean/ViewPostBean.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 Bean; + +import Database.DatabaseAccess; +import Model.Post; +import Model.Posts; +import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.ManagedProperty; +import javax.faces.bean.RequestScoped; + +/** + * + * @author kevinyu + */ +@ManagedBean +@RequestScoped +public class ViewPostBean { + + @ManagedProperty(value="#{param.postId}") + private String postId; + + private Post post; + + public String getPostId() { + return postId; + } + + public void setPostId(String postId) { + this.postId = postId; + } + + public Post getPost() { + if (post==null){ + if (postId!=null){ + getPostFromPostId(); + } + else{ + post = new Post(); + } + } + return post; + } + + private void getPostFromPostId() { + try { + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + dbManager.openConnection(); + post = Posts.getInstance().findPost(Integer.parseInt(postId)); + dbManager.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(ViewPostBean.class.getName()).log(Level.SEVERE, null, ex); + } + } + + public void setPost(Post post) { + this.post = post; + } + +} diff --git a/src/java/Database/DBConnectionDemo.java b/src/java/Database/DBConnectionDemo.java new file mode 100644 index 00000000..7268d0b3 --- /dev/null +++ b/src/java/Database/DBConnectionDemo.java @@ -0,0 +1,116 @@ +package Database; + +import Model.Post; +import java.sql.SQLException; +import java.sql.Timestamp; + +public class DBConnectionDemo { + + public static void main(String args[]) { + // Mengakses instans + DatabaseAccess databaseAccess = DatabaseAccess.getInstance(); + + // Set konfigurasi + databaseAccess.setDatabase("simple_blog"); + databaseAccess.setUsername("root"); // secara default root + databaseAccess.setPassword(""); // secara default string kosong + + // Membuka koneksi + try { + databaseAccess.openConnection(); + System.out.println("Berhasil membuka koneksi"); + } catch (SQLException e) { + System.out.println("Gagal membuka koneksi"); + System.out.println(e); + } + + // -------------------INSERT---------------------- + // Melakukan INSERT INTO Employee (id,age,first,last) VALUES + // (200,21,'Kevin','Yudi + // Utama'); + Post p = new Post(); + p.setTitle("Post Testing"); + p.setText("Ini adalah post testing"); + p.setTimeString("2012-08-07"); + Timestamp ts = p.getTimestamp(); + try { + databaseAccess.executeUpdateQuery("INSERT INTO post (Judul,Id_User,Konten,Tanggal) VALUES" + + "('"+p.getTitle()+"',1,'"+p.getText()+"','"+ + p.getTimeString()+"');"); + System.out.println("Berhasil memasukkan records"); + } catch (Exception e1) { + System.out.println("Gagal memasukkan records"); + System.out.println("Error : " + e1); + } + // Alternatif databaseAccess.executeUpdateQuery("INSERT INTO Employee + // (age,first,last) VALUES (21,'Kevin','Yudi Utama');");" + + // -----------------DELETE------------------------ + // Melakukan DELETE FROM Employee WHERE first='Kevin' AND last='Yudi'; + + // null jika tidak ada kondisi (menghapus seluruh records pada tabel); + /*String condition = "first='Kevin' AND last='Yudi'"; + + try { + databaseAccess.deleteRecords("Employee", condition); + System.out.println("Berhasil menghapus records"); + } catch (SQLException e1) { + System.out.println("Gagal menghapus records"); + e1.printStackTrace(); + } + // Alternatif databaseAccess.executeUpdateQuery("DELETE FROM Employee + // WHERE + // first='Kevin' AND last='Yudi';"); + + // -------------------UPDATE---------------------------- + // melakukan UPDATE Employee SET first='Wira',last='Gotama' WHERE Id = + // 103; + ArrayList columns = new ArrayList(Arrays.asList("first", "last")); + ArrayList values = new ArrayList(Arrays.asList("'Wira'", "'Gotama'")); + condition = "Id = 103"; + try { + databaseAccess + .updateRecords("Employee", columns, values, condition); + System.out.println("Berhasil melakukan update terhadap database"); + } catch (SQLException e2) { + System.out.println("Gagal melakukan update terhadap database"); + System.out.println(e2); + } + + // -----------------SELECT-------------------------- + // melakukan SELECT id,first,last FROM Employee; + columns = new ArrayList(Arrays.asList("id", "first", "last")); + condition = null; // jika ada kondisi dapat dimasukkan seperti pada + // INSERT dan DELETE; + ResultSet result = null; + try { + result = databaseAccess.selectRecords("Employee", columns, + condition); + System.out.println("Berhasil melakukan seleksi pada database"); + } catch (SQLException e1) { + System.out.println("Gagal melkaukan seleksi pada database"); + System.out.println(e1); + } + + // Mengakses hasil seleksi + try { + while (result.next()) { + System.out.println("Id = " + result.getInt("id")); + System.out.println("First Name = " + result.getString("first")); + System.out.println("Last Name = " + result.getString("last")); + System.out.println("---------------------------------------"); + } + } catch (SQLException e1) { + e1.printStackTrace(); + }*/ + + // Menutup koneksi + try { + databaseAccess.closeConnection(); + System.out.println("Berhasil menutup koneksi"); + } catch (SQLException e) { + e.printStackTrace(); + } + + } +} diff --git a/src/java/Database/DatabaseAccess.java b/src/java/Database/DatabaseAccess.java new file mode 100644 index 00000000..8faedf7f --- /dev/null +++ b/src/java/Database/DatabaseAccess.java @@ -0,0 +1,183 @@ +package Database; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; + +import javax.servlet.annotation.WebServlet; + +public class DatabaseAccess { + + private static DatabaseAccess instance; + private Connection conn; + private Statement statement; + // JDBC driver name and database URL + final String jdbcDriver; + String DB_URL; + + // Database credentials + String USER; + String PASS; + + private DatabaseAccess() { + + this.jdbcDriver = "com.mysql.jdbc.Driver"; + this.DB_URL = "jdbc:mysql://localhost/simple_blog"; + + this.USER = "root"; + this.PASS = ""; + + } + + public void setUsername(String username) { + this.USER = username; + } + + public void setPassword(String password) { + this.PASS = password; + } + + public void setDatabase(String databaseName) { + this.DB_URL = "jdbc:mysql://localhost/" + databaseName; + } + + public static DatabaseAccess getInstance() { + if (instance == null) { + instance = new DatabaseAccess(); + } + return instance; + } + + public void openConnection() throws SQLException { + try { + // Register JDBC driver + Class.forName("com.mysql.jdbc.Driver"); + + // Open a connection + this.conn = DriverManager.getConnection(this.DB_URL, this.USER, + this.PASS); + } catch (SQLException se) { + System.out.println("Cannot open connection"); + throw se; + } catch (ClassNotFoundException e) { + System.out.println("JDBC not found"); + } + this.statement = this.conn.createStatement(); + } + + public void closeConnection() throws SQLException { + try { + this.conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + throw e; + } + } + + public ResultSet executeFetchQuery(String query) { + ResultSet resultSet = null; + try { + resultSet = this.statement.executeQuery(query); + } catch (SQLException e) { + System.out.println(e); + } + return resultSet; + } + + public void executeUpdateQuery(String query) { + System.out.println(query); + ResultSet resultSet = null; + try { + this.statement.executeUpdate(query); + } catch (SQLException e) { + System.out.println(e); + } + } + + public void insertRecords(String tableName, ArrayList columns, + ArrayList values) throws SQLException { + StringBuilder query = new StringBuilder("INSERT INTO " + tableName + + " ("); + for (int i = 0; i < columns.size(); i++) { + query.append(columns.get(i)); + if (i != columns.size() - 1) { + query.append(","); + } else { + query.append(")"); + } + } + query.append(" VALUES ("); + for (int i = 0; i < values.size(); i++) { + query.append(values.get(i)); + if (i != values.size() - 1) { + query.append(","); + } else { + query.append(")"); + } + } + query.append(";"); + System.out.println(query); + this.statement.executeUpdate(query.toString()); + } + + public void deleteRecords(String tableName, String condition) + throws SQLException { + String query = "DELETE FROM " + tableName; + if (condition != null) { + query += " WHERE " + condition; + } + query += ";"; + System.out.println(query); + this.statement.executeUpdate(query); + } + + public ResultSet selectRecords(String tableName, ArrayList columns, + String condition) throws SQLException { + StringBuilder query = new StringBuilder("SELECT "); + for (int i = 0; i < columns.size(); i++) { + query.append(columns.get(i)); + if (i != columns.size() - 1) { + query.append(","); + } + } + query.append(" FROM " + tableName); + if (condition != null) { + query.append(" WHERE " + condition); + } + query.append(";"); + ResultSet resultSet = this.statement.executeQuery(query.toString()); + return resultSet; + } + + public ResultSet selectAllRecords(String tableName, String condition) + throws SQLException { + StringBuilder query = new StringBuilder("SELECT * FROM " + tableName); + if (condition != null) { + query.append(" WHERE " + condition); + } + query.append(";"); + System.out.println(query); + ResultSet resultSet = this.statement.executeQuery(query.toString()); + return resultSet; + } + + public void updateRecords(String tableName, ArrayList column, + ArrayList value, String condition) throws SQLException { + StringBuilder query = new StringBuilder("UPDATE " + tableName + " SET "); + for (int i = 0; i < column.size(); i++) { + query.append(column.get(i) + "=" + value.get(i)); + if (i != column.size() - 1) { + query.append(","); + } + } + if (condition!=null){ + query.append(" WHERE " + condition); + } + query.append(";"); + System.out.println(query); + this.statement.executeUpdate(query.toString()); + } +} \ No newline at end of file diff --git a/src/java/Filter/FilterAdmin.java b/src/java/Filter/FilterAdmin.java new file mode 100644 index 00000000..9d1edc71 --- /dev/null +++ b/src/java/Filter/FilterAdmin.java @@ -0,0 +1,51 @@ +/* + * 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 Filter; + +import Model.User; +import java.io.IOException; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +/** + * + * @author wira gotama + */ +public class FilterAdmin implements Filter { + @Override + public void init(FilterConfig filterConfig) throws ServletException { + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + HttpServletRequest req = (HttpServletRequest) request; + HttpSession session = req.getSession(); + User user = (session != null) ? (User) session.getAttribute("user") : null; + if (user==null){ + HttpServletResponse res = (HttpServletResponse) response; + res.sendRedirect("login.jsf"); + } + else if (user.getRole().equals("admin")) { + chain.doFilter(request, response); + } + else { + HttpServletResponse res = (HttpServletResponse) response; + res.sendRedirect("login.jsf"); + } + } + + @Override + public void destroy() { + } +} diff --git a/src/java/Filter/FilterAdminEditor.java b/src/java/Filter/FilterAdminEditor.java new file mode 100644 index 00000000..473f6128 --- /dev/null +++ b/src/java/Filter/FilterAdminEditor.java @@ -0,0 +1,51 @@ +/* + * 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 Filter; + +import Model.User; +import java.io.IOException; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +/** + * + * @author wira gotama + */ +public class FilterAdminEditor implements Filter { + @Override + public void init(FilterConfig filterConfig) throws ServletException { + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + HttpServletRequest req = (HttpServletRequest) request; + HttpSession session = req.getSession(); + User user = (session != null) ? (User) session.getAttribute("user") : null; + if (user==null){ + HttpServletResponse res = (HttpServletResponse) response; + res.sendRedirect("login.jsf"); + } + else if (user.getRole().equals("editor") || user.getRole().equals("admin")) { + chain.doFilter(request, response); + } + else { + HttpServletResponse res = (HttpServletResponse) response; + res.sendRedirect("login.jsf"); + } + } + + @Override + public void destroy() { + } +} diff --git a/src/java/Filter/FilterAdminOwner.java b/src/java/Filter/FilterAdminOwner.java new file mode 100644 index 00000000..28eaf72f --- /dev/null +++ b/src/java/Filter/FilterAdminOwner.java @@ -0,0 +1,51 @@ +/* + * 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 Filter; + +import Model.User; +import java.io.IOException; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +/** + * + * @author wira gotama + */ +public class FilterAdminOwner implements Filter { + @Override + public void init(FilterConfig filterConfig) throws ServletException { + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + HttpServletRequest req = (HttpServletRequest) request; + HttpSession session = req.getSession(); + User user = (session != null) ? (User) session.getAttribute("user") : null; + if (user==null){ + HttpServletResponse res = (HttpServletResponse) response; + res.sendRedirect("login.jsf"); + } + else if (user.getRole().equals("owner") || user.getRole().equals("admin")) { + chain.doFilter(request, response); + } + else { + HttpServletResponse res = (HttpServletResponse) response; + res.sendRedirect("login.jsf"); + } + } + + @Override + public void destroy() { + } +} diff --git a/src/java/Filter/FilterGuest.java b/src/java/Filter/FilterGuest.java new file mode 100644 index 00000000..3ccd17d5 --- /dev/null +++ b/src/java/Filter/FilterGuest.java @@ -0,0 +1,48 @@ +/* + * 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 Filter; + +import Model.User; +import java.io.IOException; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +/** + * + * @author wira gotama + */ +public class FilterGuest implements Filter { + @Override + public void init(FilterConfig filterConfig) throws ServletException { + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + HttpServletRequest req = (HttpServletRequest) request; + HttpSession session = req.getSession(); + User user = (session != null) ? (User) session.getAttribute("user") : null; + String isLogin = (String) req.getSession().getAttribute("isLogin"); + if (user!=null && (isLogin!=null) && (isLogin.equals("yes"))) { + HttpServletResponse res = (HttpServletResponse) response; + res.sendRedirect("login.jsf"); + } + else { + chain.doFilter(request, response); + } + } + + @Override + public void destroy() { + } +} diff --git a/src/java/Filter/FilterLogged.java b/src/java/Filter/FilterLogged.java new file mode 100644 index 00000000..f1b50676 --- /dev/null +++ b/src/java/Filter/FilterLogged.java @@ -0,0 +1,51 @@ +/* + * 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 Filter; + +import Model.User; +import java.io.IOException; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +/** + * + * @author wira gotama + */ +public class FilterLogged implements Filter { + @Override + public void init(FilterConfig filterConfig) throws ServletException { + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + HttpServletRequest req = (HttpServletRequest) request; + HttpSession session = req.getSession(); + User user = (session != null) ? (User) session.getAttribute("user") : null; + if (user==null){ + HttpServletResponse res = (HttpServletResponse) response; + res.sendRedirect("login.jsf"); + } + else if (user.getRole().equals("guest")) { + HttpServletResponse res = (HttpServletResponse) response; + res.sendRedirect("login.jsf"); + } + else if (user.getRole().equals("editor") || user.getRole().equals("owner") || user.getRole().equals("admin")) { + chain.doFilter(request, response); + } + } + + @Override + public void destroy() { + } +} diff --git a/src/java/Filter/FilterOwner.java b/src/java/Filter/FilterOwner.java new file mode 100644 index 00000000..2a0b890a --- /dev/null +++ b/src/java/Filter/FilterOwner.java @@ -0,0 +1,51 @@ +/* + * 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 Filter; + +import Model.User; +import java.io.IOException; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +/** + * + * @author wira gotama + */ +public class FilterOwner implements Filter { + @Override + public void init(FilterConfig filterConfig) throws ServletException { + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + HttpServletRequest req = (HttpServletRequest) request; + HttpSession session = req.getSession(); + User user = (session != null) ? (User) session.getAttribute("user") : null; + if (user==null){ + HttpServletResponse res = (HttpServletResponse) response; + res.sendRedirect("login.jsf"); + } + else if (user.getRole().equals("owner") || user.getRole().equals("admin")) { + chain.doFilter(request, response); + } + else { + HttpServletResponse res = (HttpServletResponse) response; + res.sendRedirect("login.jsf"); + } + } + + @Override + public void destroy() { + } +} diff --git a/src/java/Filter/LoginFilter.java b/src/java/Filter/LoginFilter.java new file mode 100644 index 00000000..325d1e5d --- /dev/null +++ b/src/java/Filter/LoginFilter.java @@ -0,0 +1,50 @@ +package Filter; + + +import Model.User; +import java.io.IOException; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +/* + * 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. + */ + +/** + * + * @author kevinyu + */ + +public class LoginFilter implements Filter{ + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + HttpServletRequest req = (HttpServletRequest) request; + HttpSession session = req.getSession(); + User user = (session != null) ? (User) session.getAttribute("user") : null; + if (user!=null && (user.getRole().equals("editor") || user.getRole().equals("owner") || user.getRole().equals("admin"))) { + chain.doFilter(request,response); + } + else { + HttpServletResponse res = (HttpServletResponse)response; + res.sendRedirect("login.jsf"); + } + } + + @Override + public void destroy() { + } + +} diff --git a/src/java/Filter/LoginRedirect.java b/src/java/Filter/LoginRedirect.java new file mode 100644 index 00000000..368a2912 --- /dev/null +++ b/src/java/Filter/LoginRedirect.java @@ -0,0 +1,49 @@ +package Filter; + +import java.io.IOException; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/* + * 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. + */ + +/** + * + * @author kevinyu + */ +public class LoginRedirect implements Filter{ + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + HttpServletRequest req = (HttpServletRequest) request; + String isLogin = (String) req.getSession().getAttribute("isLogin"); + if (isLogin==null){ + chain.doFilter(request,response); + } + else if ((isLogin.equals("yes"))) { + HttpServletResponse res = (HttpServletResponse) response; + res.sendRedirect("user_management.jsf"); + } + else { + chain.doFilter(request,response); + } + } + + @Override + public void destroy() { + } + +} diff --git a/src/java/Model/Comment.java b/src/java/Model/Comment.java new file mode 100644 index 00000000..fde7a02a --- /dev/null +++ b/src/java/Model/Comment.java @@ -0,0 +1,142 @@ +/* + * 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; +import Database.*; +import java.util.Date; +import java.lang.Object; +import java.sql.Timestamp; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.logging.Level; +import java.util.logging.Logger; +/** + * + * @author wira gotama + */ +public class Comment { + private int id; + private int post_id; + private int creatorId; + private String email; + private String text; + private String name; + private boolean guest; + + public int getPost_id() { + return post_id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public boolean isGuest() { + return guest; + } + + public void setGuest(boolean guest) { + this.guest = guest; + } + private Timestamp timestamp; + + public Comment() { + guest = true; + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + Date parsedDate=null; + try { + parsedDate = dateFormat.parse("2012-01-01"); + } catch (ParseException ex) { + Logger.getLogger(Comment.class.getName()).log(Level.SEVERE, null, ex); + } + this.timestamp = new java.sql.Timestamp(parsedDate.getTime()); + } + + public Comment(int id, int post_id, int creator, String email, String text, Timestamp timestamp,String nama,boolean guest) { + this.id = id; + this.post_id = post_id; + this.creatorId = creator; + this.email = email; + this.text = text; + this.timestamp = timestamp; + this.name = nama; + this.guest = guest; + this.timestamp = timestamp; + } + + /* Setter */ + public void setId(int id) { + this.id = id; + } + + public void setPostId(int post_id) { + this.post_id = post_id; + } + + public void setCreatorId(int creator) { + this.creatorId = creator; + } + + public void setEmail(String email) { + this.email = email; + } + + public void setText(String text) { + this.text = text; + } + + public void setTimestamp(Timestamp timestamp) { + this.timestamp = timestamp; + } + + /* Getter */ + public int getId() { + return id; + } + + public int getPostId() { + return post_id; + } + + public int getCreatorId() { + return creatorId; + } + + public String getEmail() { + return email; + } + + public String getText() { + return text; + } + + public Timestamp getTimestamp() { + return timestamp; + } + + public String getTimeString() { + if (timestamp==null){ + return ""; + } + String timeString = new SimpleDateFormat("yyyy-MM-dd").format(timestamp); + return timeString; + } + + public void setTimeString(String newTime) { + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + try { + Date parsedDate = dateFormat.parse(newTime); + timestamp = new java.sql.Timestamp(parsedDate.getTime()); + } catch (ParseException ex) { + Logger.getLogger(Post.class.getName()).log(Level.SEVERE, null, ex); + } + + } +} diff --git a/src/java/Model/Comments.java b/src/java/Model/Comments.java new file mode 100644 index 00000000..5eb42aec --- /dev/null +++ b/src/java/Model/Comments.java @@ -0,0 +1,122 @@ +/* + * 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; + +import Database.DatabaseAccess; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author kevinyu + */ +public class Comments { + private static Comments instance; + + private Comments() { + + } + + public static Comments getInstance() { + if (instance==null){ + instance = new Comments(); + } + return instance; + } + + public void addComment(Comment comment) { + DatabaseAccess databaseAccess = DatabaseAccess.getInstance(); + String idUser = "" + comment.getCreatorId() +""; + String idPost = "" + comment.getPostId() +""; + String isGuest = comment.isGuest() ? "TRUE" : "FALSE" ; + ArrayList columns = null; + ArrayList values = null; + if (comment.isGuest()) { + columns = new ArrayList(Arrays.asList("Nama","Tanggal","Komentar","Id_Post","Id_User","Email","Guest")); + values = new ArrayList(Arrays.asList("'"+comment.getName()+"'", + "'"+comment.getTimeString()+"'","'"+comment.getText()+"'",idPost,idUser,"'"+ + comment.getEmail()+"'",isGuest)); + } + else { + columns = new ArrayList(Arrays.asList("Nama","Tanggal","Komentar","Id_Post","Id_User","Email","Guest")); + values = new ArrayList(Arrays.asList("''","'"+comment.getTimeString()+"'","'"+comment.getText()+"'", + idPost,idUser,"''",isGuest)); + } + + try { + databaseAccess.insertRecords("komentar", columns, values); + } catch (Exception ex) { + Logger.getLogger(Post.class.getName()).log(Level.SEVERE, null, ex); + } + } + + public ArrayList getCommentByPostId(int postId) { + ArrayList comments = new ArrayList(); + DatabaseAccess databaseAccess = DatabaseAccess.getInstance(); + String condition = "Id_Post="+postId; + ResultSet resultSet = null; + + try { + resultSet = databaseAccess.selectAllRecords("komentar", condition); + } + catch (SQLException e1) { + System.out.println(e1); + } + if (resultSet!=null){ + try + { + while (resultSet.next()){ + Comment comment; + boolean isGuest = resultSet.getBoolean("Guest"); + int commentId = resultSet.getInt("Id_Komentar"); + Timestamp date = resultSet.getTimestamp("Tanggal"); + String text = resultSet.getString("Komentar"); + int postingId = resultSet.getInt("Id_Post"); + int userId = resultSet.getInt("Id_User"); + String nama,email; + nama = resultSet.getString("Nama"); + email = resultSet.getString("Email"); + System.out.println("Nama :"+nama); + System.out.println("Email :"+email); + comments.add(new Comment(commentId,postingId,userId,email,text,date,nama,isGuest)); + } + } + + catch (SQLException e1) { + e1.printStackTrace(); + } + } + for (Comment com:comments) { + + if (!com.isGuest()) { + User user = Users.getInstance().findUser(com.getCreatorId()); + String nama = user.getUsername(); + String email = user.getEmail(); + com.setName(nama); + com.setEmail(email); + } + } + return comments; + } + + public void deleteCommentByPostId(int postId) { + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + String tableName = "komentar"; + String condition = "Id_Post="+postId; + try { + dbManager.deleteRecords("komentar", condition); + } catch (SQLException ex) { + Logger.getLogger(Comments.class.getName()).log(Level.SEVERE, null, ex); + } + } + +} diff --git a/src/java/Model/Post.java b/src/java/Model/Post.java new file mode 100644 index 00000000..4c5833be --- /dev/null +++ b/src/java/Model/Post.java @@ -0,0 +1,136 @@ +package Model; + +import Database.*; +import java.sql.Timestamp; +import java.util.Vector; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.RequestScoped; +/** + * + * @author wira gotama + */ + +@ManagedBean +@RequestScoped +public class Post { + private int id; + private String title; + private int creatorId; + private String text; + private Timestamp timestamp; + private boolean isPublished; + private boolean isDeleted; + + public Post() { + title = ""; + text = ""; + isPublished = false; + isDeleted = false; + } + + public Post(int id, String title, int creatorId, String text, Timestamp timestamp,boolean isPublished,boolean isDeleted) { + this.id = id; + this.title = title; + this.creatorId = creatorId; + this.text = text; + this.timestamp = timestamp; + this.isPublished = isPublished; + this.isDeleted = isDeleted; + } + + public boolean isPublished() { + return isPublished; + } + + public void setPublished(boolean isPublished) { + this.isPublished = isPublished; + } + + public boolean isDeleted() { + return isDeleted; + } + + public void setDeleted(boolean isDeleted) { + this.isDeleted = isDeleted; + } + + /* Setter */ + public void setId(int id) { + this.id = id; + } + + public void setTitle(String title) { + this.title = title; + } + + public void setCreatorId(int creatorId) { + this.creatorId = creatorId; + } + + public void setText(String text) { + this.text = text; + } + + public void setTimestamp(Timestamp timestamp) { + this.timestamp = timestamp; + } + + /* Getter */ + public int getId() { + return this.id; + } + + public String getTitle() { + return this.title; + } + + public int getCreatorId() { + return this.creatorId; + } + + public String getText() { + return this.text; + } + + + public String getTrimText() { + if (text.length()>101){ + return text.substring(0, 100) +"..."; + } + else{ + return text; + } + } + + public Timestamp getTimestamp() { + return this.timestamp; + } + + public String getTimeString() { + if (timestamp==null){ + return ""; + } + String timeString = new SimpleDateFormat("yyyy-MM-dd").format(timestamp); + return timeString; + } + + public void setTimeString(String newTime) { + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + try { + Date parsedDate = dateFormat.parse(newTime); + timestamp = new java.sql.Timestamp(parsedDate.getTime()); + } catch (ParseException ex) { + Logger.getLogger(Post.class.getName()).log(Level.SEVERE, null, ex); + } + + } +} diff --git a/src/java/Model/Posts.java b/src/java/Model/Posts.java new file mode 100644 index 00000000..c1fd1e45 --- /dev/null +++ b/src/java/Model/Posts.java @@ -0,0 +1,240 @@ +/* + * 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; + +import Database.*; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Vector; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author user + */ +public class Posts { + private Vector posts; + + private static Posts instance; + + private Posts() { + posts = new Vector(); + } + + public static Posts getInstance() { + if (instance==null){ + instance = new Posts(); + } + return instance; + } + + public Post findPost(int post_id) + { + + ResultSet result=null; + DatabaseAccess databaseAccess = DatabaseAccess.getInstance(); + String condition = "Id_Post="+post_id; + try { + result = databaseAccess.selectAllRecords("post",condition); + } + catch (SQLException e1) { + System.out.println(e1); + } + + + //fetch the result + Post p = null; + if (result!=null) { + try + { + while (result.next()) { + + p = new Post(result.getInt("Id_Post"), result.getString("Judul") ,result.getInt("Id_User") , + result.getString("Konten"), result.getTimestamp("Tanggal"),result.getBoolean("Published") + ,result.getBoolean("Deleted")); + posts.add(p); + } + } + catch (SQLException e1) { + e1.printStackTrace(); + } + } + return p; + } + + public void removePost(int post_id) { + + DatabaseAccess databaseAccess = DatabaseAccess.getInstance(); + String condition = "Id_Post="+post_id; + try { + databaseAccess.deleteRecords("post", condition); + } catch (SQLException e1) { + e1.printStackTrace(); + } + } + + public void updatePostDB(Post p) { + DatabaseAccess databaseAccess = DatabaseAccess.getInstance(); + String isPublished = p.isPublished() ? "TRUE" : "FALSE"; + String isDeleted = p.isDeleted() ? "TRUE" : "FALSE"; + ArrayList columns = new ArrayList(Arrays.asList("Judul","Konten", "Tanggal","Published","Deleted")); + ArrayListvalues = new ArrayList(Arrays.asList("'"+p.getTitle()+"'", + "'"+p.getText()+"'","'"+ p.getTimestamp()+"'",isPublished,isDeleted)); + String condition = "Id_Post="+p.getId(); + try { + databaseAccess.updateRecords("post", columns, values, condition); + + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public void insertPostDB(Post p) { + DatabaseAccess databaseAccess = DatabaseAccess.getInstance(); + String idUser = "" + p.getCreatorId() +""; + ArrayList columns = new ArrayList(Arrays.asList("Judul","Tanggal","Konten","Id_User")); + ArrayList values = new ArrayList(Arrays.asList("'"+p.getTitle()+"'","'"+p.getTimeString()+"'", + "'"+p.getText()+"'",idUser)); + try { + databaseAccess.insertRecords("post", columns, values); + } catch (Exception ex) { + Logger.getLogger(Post.class.getName()).log(Level.SEVERE, null, ex); + } + } + + public Vector filter(int user_id){ + Vector posts = new Vector(); + DatabaseAccess databaseAccess = DatabaseAccess.getInstance(); + String condition = "user_id="+user_id; + ResultSet resultSet = null; + + try { + resultSet = databaseAccess.selectAllRecords("post", condition); + } + catch (SQLException e1) { + System.out.println(e1); + } + + try + { + while (resultSet.next()){ + + posts.add(new Post(resultSet.getInt("Id_Post"), resultSet.getString("Judul"), resultSet.getInt("Id_User"), + resultSet.getString("Konten"), resultSet.getTimestamp("Tanggal"),resultSet.getBoolean("Published"), + resultSet.getBoolean("Deleted"))); + } + + } + catch (SQLException e1) { + e1.printStackTrace(); + } + return posts; + } + + public ArrayList getPublishedPost() { + ArrayList posts=new ArrayList(); + + DatabaseAccess databaseAccess = DatabaseAccess.getInstance(); + ResultSet resultSet = null; + + try { + resultSet = databaseAccess.selectAllRecords("post", "Published=TRUE AND Deleted=FALSE"); + } + catch (SQLException e1) { + System.out.println(e1); + } + + if (resultSet!=null){ + try + { + while (resultSet.next()){ + posts.add(new Post(resultSet.getInt("Id_Post"), resultSet.getString("Judul"), resultSet.getInt("Id_User"), + resultSet.getString("Konten"), resultSet.getTimestamp("Tanggal"), + resultSet.getBoolean("Published"),resultSet.getBoolean("Deleted"))); + } + + } + catch (SQLException e1) { + e1.printStackTrace(); + } + } + return posts; + } + + public ArrayList getUnpublishedPost() { + ArrayList posts=new ArrayList(); + + DatabaseAccess databaseAccess = DatabaseAccess.getInstance(); + ResultSet resultSet = null; + + try { + resultSet = databaseAccess.selectAllRecords("post", "Published=FALSE"); + } + catch (SQLException e1) { + System.out.println(e1); + } + + if (resultSet!=null){ + try + { + while (resultSet.next()){ + posts.add(new Post(resultSet.getInt("Id_Post"), resultSet.getString("Judul"), resultSet.getInt("Id_User"), + resultSet.getString("Konten"), resultSet.getTimestamp("Tanggal"), + resultSet.getBoolean("Published"),resultSet.getBoolean("Deleted"))); + } + + } + catch (SQLException e1) { + e1.printStackTrace(); + } + } + return posts; + } + + public ArrayList getDeletedPost(boolean deleted) { + ArrayList posts=new ArrayList(); + + DatabaseAccess databaseAccess = DatabaseAccess.getInstance(); + ResultSet resultSet = null; + + try { + if (deleted){ + resultSet = databaseAccess.selectAllRecords("post", "Deleted=TRUE"); + } + else { + resultSet = databaseAccess.selectAllRecords("post", "Deleted=FALSE"); + } + } + catch (SQLException e1) { + System.out.println(e1); + } + + if (resultSet!=null){ + try + { + while (resultSet.next()){ + posts.add(new Post(resultSet.getInt("Id_Post"), resultSet.getString("Judul"), resultSet.getInt("Id_User"), + resultSet.getString("Konten"), resultSet.getTimestamp("Tanggal"), + resultSet.getBoolean("Published"),resultSet.getBoolean("Deleted"))); + } + + } + catch (SQLException e1) { + e1.printStackTrace(); + } + } + return posts; + } +} diff --git a/src/java/Model/User.java b/src/java/Model/User.java new file mode 100644 index 00000000..f65fbe40 --- /dev/null +++ b/src/java/Model/User.java @@ -0,0 +1,87 @@ +/* + * 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; + +import Database.DatabaseAccess; +import java.util.ArrayList; +import java.util.Arrays; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.SessionScoped; + +/** + * + * @author user + */ + +@ManagedBean +@SessionScoped +public class User { + private int id; + private String username; + private String pass; + private String email; + private String role; + + public User () { + this.username = ""; + this.pass = ""; + this.email = ""; + this.role = "guest"; + } + + public User(int id, String username, String pass, String email) + { + this.id = id; + this.username = username; + this.pass = pass; + this.email = email; + } + + /* Setter */ + public void setId(int id) { + this.id = id; + } + + public void setUsername(String username){ + this.username = username; + } + + public void setPassword(String pass) { + this.pass = pass; + } + + public void setEmail(String email) { + this.email = email; + } + + public void setRole(String newRole) { + role = newRole; + } + + /* Getter */ + public int getId(){ + return this.id; + } + + public String getUsername(){ + return username; + } + + public String getPassword(){ + return this.pass; + } + + public String getEmail(){ + return this.email; + } + + public String getRole() { + return role; + } +} + + diff --git a/src/java/Model/Users.java b/src/java/Model/Users.java new file mode 100644 index 00000000..d0de255e --- /dev/null +++ b/src/java/Model/Users.java @@ -0,0 +1,173 @@ +/* + * 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; + +import Database.DatabaseAccess; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author kevinyu + */ +public class Users { + private static Users instance; + + private Users() { + + } + + public static Users getInstance() { + if (instance==null){ + instance = new Users(); + } + return instance; + } + + public ArrayList getAllUser() { + DatabaseAccess databaseAccess = DatabaseAccess.getInstance(); + + ResultSet result = null; + try { + result = databaseAccess.selectAllRecords("user", null); + } catch (SQLException e1) { + System.out.println(e1); + e1.printStackTrace(); + } + + ArrayList userList = new ArrayList(); + try{ + while (result.next()) { + int idUser = result.getInt("Id_User"); + String username = result.getString("Username"); + String password = result.getString("Password"); + String email = result.getString("Email"); + String role = result.getString("Role"); + User user = new User(idUser,username,password,email); + user.setRole(role); + userList.add(user); + } + } + catch(SQLException e2) { + e2.printStackTrace(); + } + + return userList; + } + + public User findUser(int userId) { + DatabaseAccess databaseAccess = DatabaseAccess.getInstance(); + + String condition = "Id_User="+userId; + ResultSet result = null; + try { + result = databaseAccess.selectAllRecords("user", condition); + } catch (SQLException e1) { + System.out.println(e1); + e1.printStackTrace(); + } + if (result!=null){ + try{ + if (result.next()) { + int id = result.getInt("Id_User"); + String username = result.getString("Username"); + String password = result.getString("Password"); + String email = result.getString("Email"); + User user = new User(id,username,password,email); + user.setRole(result.getString("Role")); + return user; + } + } + catch(SQLException e2) { + e2.printStackTrace(); + } + } + return null; + } + + public User validateUser(String username,String password) { + DatabaseAccess databaseAccess = DatabaseAccess.getInstance(); + try { + databaseAccess.openConnection(); + System.out.println("Berhasil membuka koneksi"); + } catch (SQLException e) { + System.out.println("Gagal membuka koneksi"); + System.out.println(e); + } + + String condition = "Username='"+username+"' AND Password='"+password+"'"; + ResultSet result = null; + try { + result = databaseAccess.selectAllRecords("user", condition); + System.out.println("Berhasil melakukan seleksi pada database"); + } catch (SQLException e1) { + System.out.println("Gagal melkaukan seleksi pada database"); + System.out.println(e1); + e1.printStackTrace(); + } + try{ + if (result.next()) { + int idUser = result.getInt("Id_User"); + String email = result.getString("Email"); + User user = new User(idUser,username,password,email); + user.setRole(result.getString("Role")); + databaseAccess.closeConnection(); + return user; + } + } + catch(SQLException e2) { + e2.printStackTrace(); + } + + return null; + + } + + public void addUser(User user) { + ArrayList columns = new ArrayList(Arrays.asList("Username","Password","Role","Email")); + ArrayList values = new ArrayList(Arrays.asList("'"+user.getUsername()+ + "'","'"+user.getPassword()+"'","'"+user.getRole()+"'","'"+user.getEmail()+"'")); + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + try { + dbManager.insertRecords("user",columns,values); + } catch (SQLException ex) { + Logger.getLogger(Users.class.getName()).log(Level.SEVERE, null, ex); + } + } + + public void updateUser(User user) { + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + + String table = "user"; + ArrayList columns = new ArrayList(Arrays.asList("Username","Password","Role","Email")); + ArrayList values = new ArrayList(Arrays.asList("'"+user.getUsername()+"'", + "'"+user.getPassword()+"'","'"+user.getRole()+"'","'"+user.getEmail()+"'")); + String condition = "Id_User="+user.getId(); + try { + dbManager.updateRecords(table,columns,values,condition); + } catch (SQLException ex) { + Logger.getLogger(Users.class.getName()).log(Level.SEVERE, null, ex); + } + + } + + public void deleteUserByUserId(User user) { + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + String condition = "Id_User="+user.getId(); + String tableName = "user"; + try { + dbManager.deleteRecords(tableName,condition); + } + catch(SQLException e){ + e.printStackTrace(); + } + } +} diff --git a/src/java/Test/CommentTest.java b/src/java/Test/CommentTest.java new file mode 100644 index 00000000..3bae8412 --- /dev/null +++ b/src/java/Test/CommentTest.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 Test; + +import Database.DatabaseAccess; +import Model.Comment; +import Model.Comments; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author kevinyu + */ +public class CommentTest { + public static void main(String[] args){ + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + try { + dbManager.openConnection(); + } catch (SQLException ex) { + Logger.getLogger(UsersTest.class.getName()).log(Level.SEVERE, null, ex); + } + + + /*Comment comment1 = new Comment(); + comment1.setName("Kevin Yudi Utama"); + comment1.setCreatorId(0); + comment1.setEmail("kevin.kayu@gmail.com"); + comment1.setGuest(true); + comment1.setPostId(1); + comment1.setTimeString("2016-01-04"); + comment1.setText("Ini adalah komentar pertama"); + Comments.getInstance().addComment(comment1); + + comment1.setName("Kevin Yudi Utama"); + comment1.setCreatorId(1); + comment1.setEmail("kevin.kayu@gmail.com"); + comment1.setGuest(false); + comment1.setPostId(1); + comment1.setTimeString("2016-01-07"); + comment1.setText("Ini adalah komentar kedua"); + Comments.getInstance().addComment(comment1);*/ + + ArrayList comments = Comments.getInstance().getCommentByPostId(18); + + for (Comment comment:comments) { + System.out.println(comment.getName()); + System.out.println(""+comment.getCreatorId()+""); + System.out.println(comment.getEmail()); + System.out.println(comment.getEmail()); + System.out.println(comment.isGuest()?"TRUE":"FALSE"); + System.out.println(comment.getPostId()); + System.out.println(comment.getTimeString()); + System.out.println(comment.getText()); + System.out.println("--------------------------------"); + } + + + try { + dbManager.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(UsersTest.class.getName()).log(Level.SEVERE, null, ex); + } + } +} diff --git a/src/java/Test/PostsTest.java b/src/java/Test/PostsTest.java new file mode 100644 index 00000000..66553bce --- /dev/null +++ b/src/java/Test/PostsTest.java @@ -0,0 +1,52 @@ +/* + * 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 Test; + +import Database.DatabaseAccess; +import Model.Post; +import Model.Posts; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author kevinyu + */ +public class PostsTest { + + public static void main(String[] args){ + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + try { + dbManager.openConnection(); + } catch (SQLException ex) { + Logger.getLogger(UsersTest.class.getName()).log(Level.SEVERE, null, ex); + } + + + System.out.println("PublishedPost"); + ArrayList posts = Posts.getInstance().getPublishedPost(); + for (Post post:posts){ + System.out.println(post.getText()); + } + + System.out.println("\nUnpublishedPost"); + ArrayList posts1 = Posts.getInstance().getUnpublishedPost(); + for (Post post:posts1){ + System.out.println(post.getText()); + } + + //Posts.getInstance().removePost(2); + + try { + dbManager.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(UsersTest.class.getName()).log(Level.SEVERE, null, ex); + } + } +} diff --git a/src/java/Test/UsersTest.java b/src/java/Test/UsersTest.java new file mode 100644 index 00000000..4ce0fba6 --- /dev/null +++ b/src/java/Test/UsersTest.java @@ -0,0 +1,60 @@ +/* + * 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 Test; + +import Database.DatabaseAccess; +import Model.User; +import Model.Users; +import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author kevinyu + */ +public class UsersTest { + + public static void main(String[] args) { + User user = new User(); + user.setUsername("KevinYudi"); + user.setPassword("Password"); + user.setRole("owner"); + user.setEmail("kevin.kayu@gmail.com"); + DatabaseAccess dbManager = DatabaseAccess.getInstance(); + try { + dbManager.openConnection(); + } catch (SQLException ex) { + Logger.getLogger(UsersTest.class.getName()).log(Level.SEVERE, null, ex); + } + + + Users.getInstance().addUser(user); + + User user1 = new User(); + user1.setUsername("winson"); + //Users.getInstance().deleteUserByUsername(user1); + + //User user2 = Users.getInstance().findUser(5); + //System.out.println("Nama : "+user2.getUsername()); + + /*User user3 = new User(); + user3.setId(5); + user3.setUsername("Eric"); + user3.setPassword("teric"); + Users.getInstance().updateUser(user3);*/ + + User user4 = new User(); + user4.setId(3); + Users.getInstance().deleteUserByUserId(user4); + try { + dbManager.closeConnection(); + } catch (SQLException ex) { + Logger.getLogger(UsersTest.class.getName()).log(Level.SEVERE, null, ex); + } + } +} diff --git a/web/META-INF/context.xml b/web/META-INF/context.xml new file mode 100644 index 00000000..9a366063 --- /dev/null +++ b/web/META-INF/context.xml @@ -0,0 +1,2 @@ + + diff --git a/web/WEB-INF/faces-config.xml b/web/WEB-INF/faces-config.xml new file mode 100644 index 00000000..0393e360 --- /dev/null +++ b/web/WEB-INF/faces-config.xml @@ -0,0 +1,7 @@ + + + + diff --git a/web/WEB-INF/web.xml b/web/WEB-INF/web.xml new file mode 100644 index 00000000..134f9b88 --- /dev/null +++ b/web/WEB-INF/web.xml @@ -0,0 +1,160 @@ + + + + + + + loginRedirect + + Filter.LoginRedirect + + + test-param + Initialization Paramter + + + + + adminRedirect + + Filter.FilterAdmin + + + test-param + Initialization Paramter + + + + + adminownerRedirect + + Filter.FilterAdminOwner + + + test-param + Initialization Paramter + + + + + guestRedirect + + Filter.FilterGuest + + + test-param + Initialization Paramter + + + + + loggedRedirect + + Filter.FilterLogged + + + test-param + Initialization Paramter + + + + + admineditorRedirect + + Filter.FilterAdminEditor + + + test-param + Initialization Paramter + + + + + + + loginRedirect + /login.jsf + + + + adminRedirect + /new_user.jsf + /read_user.jsf + /deleted_post.jsf + + + + adminownerRedirect + /new_post.jsf + + + + guestRedirect + /index.jsf + + + + loggedRedirect + /user_management.jsf + + + + admineditorRedirect + /list_post.jsf + + + + + + loginRedirect + LoginRedirect + + + + adminRedirect + adminRedirect + + + + adminownerRedirect + adminownerRedirect + + + + guestRedirect + guestRedirect + + + + loggedRedirect + loggedRedirect + + + + admineditorRedirect + admineditorRedirect + + + + javax.faces.PROJECT_STAGE + Development + + + Faces Servlet + javax.faces.webapp.FacesServlet + 1 + + + Faces Servlet + *.jsf + + + + 30 + + + + login.jsf + + + diff --git a/web/assets/css/button.css b/web/assets/css/button.css new file mode 100644 index 00000000..e69de29b diff --git a/assets/css/screen.css b/web/assets/css/screen.css similarity index 82% rename from assets/css/screen.css rename to web/assets/css/screen.css index 1d8fdf3b..2f6d4cf1 100644 --- a/assets/css/screen.css +++ b/web/assets/css/screen.css @@ -106,15 +106,11 @@ header#teaser h1 { /* Links ================================================== */ a { - color: #F40034; text-decoration: none; -webkit-transition: color .2s ease-in-out; -moz-transition: color .2s ease-in-out; transition: color .2s ease-in-out; } -a:hover { - color: #F40034; -} /* Layout ================================================== */ @@ -233,7 +229,7 @@ div.cover > img { } /* Contains the time, title and subtitle for an article */ .art-header-inner { - position: fixed; + position: absolute; top: 300px; left: 50%; margin-left: -490px; @@ -843,7 +839,7 @@ hr.featured-article:after { height: 90px; } -#contact-area textarea:focus, #contact-area input:focus { +#contact-area textarea:focus, #contact-area input:focus, #contact-area select:focus { border: 2px solid #900; } @@ -852,10 +848,200 @@ hr.featured-article:after { } +#contact-area select { + width : 100px; + padding : 5px; + border: 2px solid #ccc; +} + label { float: left; text-align: right; margin-right: 15px; width: 100px; padding-top: 5px; +} + +.loginContainer { + height : 500px; +} + +.login{ + position : absolute; + top : calc(40% - 75px); + left : calc(50% - 50px); + height : 150px; + width : 350px; + padding : 10px; + z-index : 2; +} + +.login input[type=text]{ + width : 250px; + height : 30px; + background : #ffffff; + border : 1px solid rgba(62,62,62,0.6); + border-radius : 2px; + color : #000000; + font-family : 'Exo', sans-serif; + font-size : 16px; + font-weight : 400; + padding : 4px; +} + +.login input[type=password]{ + width : 250px; + height : 30px; + background : #ffffff; + border : 1px solid rgba(62,62,62,0.6); + border-radius : 2px; + color : #000000; + font-family : 'Exo', sans-serif; + font-size : 16px; + font-weight : 400; + padding : 4px; + margin-top : 10px; +} + +.login input[type=submit]{ + width : 260px; + height : 35px; + background : #3E3E33; + border : 1px solid #fff; + cursor : pointer; + border-radius : 2px; + color : #ffffff; + font-family : 'Exo', sans-serif; + font-size : 16px; + font-weight : 400; + padding : 6px; + margin-top : 10px; +} + +.login input[type=submit]:hover{ + opacity : 0.8; +} + +.login input[type=submit]:active{ + opacity : 0.6; +} + +.login input[type=text]:focus{ + outline : none; + border : 1px solid rgba(255,153,51,0.9); +} + +.login input[type=password]:focus{ + outline : none; + border : 1px solid rgba(255,153,51,0.9); +} + +.login input[type=submit]:focus{ + outline: none; +} + +.login input[type=checkbox] { + margin-left : 120px; +} + +.blogAbout{ + font-family: "proxima-nova", "helvetica neue", "helvetica", sans-serif; + font-weight: 300; + font-size: 8px; + line-height: 30px; + color: #000; + position : absolute; + top : calc(38% - 35px); + left: calc(45% - 255px); + z-index : 2; +} + +.blogAbout div{ + float : left; + color : #3E3E33; + font-size : 35px; + font-weight : 200; +} + +.blogAbout div span{ + text-transform: uppercase; + color : #F40034; +} + +.blogAbout div div{ + font-family: "proxima-nova", "helvetica neue", "helvetica", sans-serif; + font-size : 15px; + color : #3E3E33; +} + +.crudContaier { + height : 500px; +} + +.navManagement { + font-family: "proxima-nova", "helvetica neue", "helvetica", sans-serif; + margin-left : 0px; + position : absolute; + top : calc(16%); + left: calc(13%); + z-index : 2; +} +.navManagement ul +{ + list-style-type : none; + margin : 0; + padding-left : 0; +} + +.navManagement li +{ + display : inline; + float : left; + padding : 8px; + padding-left : 20px; +} + +.navManagement a, +.navManagement a:focus, +.navManagement a:visited, +.navManagement a:hover, +.navManagement a:active { + color : #000000; + text-decoration : none; +} + +.navManagement a:hover { + color : #FF9933; +} + +.form { + margin-top : 200px; + margin-left : 300px; + margin-bottom : 40px; +} + +.userdata { + margin-bottom : 40px; +} + +.userTable{ + border-collapse:collapse; + border:1px solid #000000; +} + +.userTableHeader{ + text-align:center; + background:none repeat scroll 0 0 #B5B5B5; + border-bottom:1px solid #000000; + padding:2px; +} + +.userTableOddRow{ + text-align:center; + background:none repeat scroll 0 0 #FFFFFFF; +} + +.userTableEvenRow{ + text-align:center; + background:none repeat scroll 0 0 #D3D3D3; } \ No newline at end of file diff --git a/assets/img/favicon.ico b/web/assets/img/favicon.ico similarity index 100% rename from assets/img/favicon.ico rename to web/assets/img/favicon.ico diff --git a/assets/img/pre.png b/web/assets/img/pre.png similarity index 100% rename from assets/img/pre.png rename to web/assets/img/pre.png diff --git a/web/assets/js/validate.js b/web/assets/js/validate.js new file mode 100644 index 00000000..9eff85e2 --- /dev/null +++ b/web/assets/js/validate.js @@ -0,0 +1,8 @@ +function validateDeletion() { +/* Post deletion confirmation */ + var answer = confirm ("Are you sure want to delete this user?"); + if (answer) { + return true; + } + else return false; +} \ No newline at end of file diff --git a/web/assets/template/template.xhtml b/web/assets/template/template.xhtml new file mode 100644 index 00000000..e0240af9 --- /dev/null +++ b/web/assets/template/template.xhtml @@ -0,0 +1,209 @@ + + + + + Penguasa Dunia + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + +Default content + + + +
+
+ + + diff --git a/web/deleted_post.xhtml b/web/deleted_post.xhtml new file mode 100644 index 00000000..cbfe2075 --- /dev/null +++ b/web/deleted_post.xhtml @@ -0,0 +1,41 @@ + + + + +
+
+ +
+
+
+
diff --git a/web/edit_post.xhtml b/web/edit_post.xhtml new file mode 100644 index 00000000..27f427bc --- /dev/null +++ b/web/edit_post.xhtml @@ -0,0 +1,28 @@ + + + + + + + + Judul: + + + Tanggal: + + + Konten:
+ + + + + +
+
+
+
+
\ No newline at end of file diff --git a/web/index.xhtml b/web/index.xhtml new file mode 100644 index 00000000..c361e363 --- /dev/null +++ b/web/index.xhtml @@ -0,0 +1,88 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Simple Blog + + + + + +
+ + + +
+
+ +
+
+ + + +
+ +
+ \ No newline at end of file diff --git a/web/list_post.xhtml b/web/list_post.xhtml new file mode 100644 index 00000000..51daf839 --- /dev/null +++ b/web/list_post.xhtml @@ -0,0 +1,41 @@ + + + + +
+
+ +
+
+
+
\ No newline at end of file diff --git a/web/login.xhtml b/web/login.xhtml new file mode 100644 index 00000000..e911eac5 --- /dev/null +++ b/web/login.xhtml @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Simple Blog + + + + + + + + +
+ + + +
+
+
+
Welcome
---Sang Penguasa Dunia---
+
+ +
+
+ + + +
+ +
+ diff --git a/web/new_post.xhtml b/web/new_post.xhtml new file mode 100644 index 00000000..9dab311b --- /dev/null +++ b/web/new_post.xhtml @@ -0,0 +1,26 @@ + + + + + + + + Judul: + + + Tanggal: + + + Konten:
+ + + +
+
+
+
+
\ No newline at end of file diff --git a/web/new_user.xhtml b/web/new_user.xhtml new file mode 100644 index 00000000..28f6ee6f --- /dev/null +++ b/web/new_user.xhtml @@ -0,0 +1,29 @@ + + + + +
+
+ + + + + + + + + + + +

+ + +
+
+
+
+
\ No newline at end of file diff --git a/web/post.xhtml b/web/post.xhtml new file mode 100644 index 00000000..d7ab0a35 --- /dev/null +++ b/web/post.xhtml @@ -0,0 +1,164 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Simple Blog | #{viewPostBean.post.title} + + + + + +
+ + + +
+ +
+
+ +

#{viewPostBean.post.title}

+

+
+
+ +
+
+
+

#{viewPostBean.post.text}

+ +
+ +

Komentar

+ +
+ + + + + + +
+ + + + + + + +
+ +
    + + +
  • +
    +

    #{komen.name}

    +
    #{komen.email}
    +
    #{komen.timeString}
    +
    +

    #{komen.text}

    +
  • +
    +
    +
+
+
+
+ +
+ + + +
+ +
+ + \ No newline at end of file diff --git a/web/read_user.xhtml b/web/read_user.xhtml new file mode 100644 index 00000000..a35e253a --- /dev/null +++ b/web/read_user.xhtml @@ -0,0 +1,79 @@ + + + + + +
+
+
+ + + + + + User ID + + #{usr.id} + + + + Username + #{usr.username} + + + + Email + #{usr.email} + + + + Role + #{usr.role} + + + + Command + + + | + + + + + + + +
+
+
+
+
+
+
\ No newline at end of file diff --git a/readme.md b/web/readme.md similarity index 100% rename from readme.md rename to web/readme.md diff --git a/web/test.xhtml b/web/test.xhtml new file mode 100644 index 00000000..c3c92b21 --- /dev/null +++ b/web/test.xhtml @@ -0,0 +1,23 @@ + + + +Test Tags + + + +

Test Tags

+
+Test that JSF Tags Render Properly + +Verify that you see a textfield and button below.

+Textfield: + +
+Button: + +
+
+
\ No newline at end of file diff --git a/web/update_user.xhtml b/web/update_user.xhtml new file mode 100644 index 00000000..83f3b3de --- /dev/null +++ b/web/update_user.xhtml @@ -0,0 +1,33 @@ + + + + +
+
+ + + + + + + + + + + + + +

+ + + + +
+
+
+
+
\ No newline at end of file diff --git a/web/user_management.xhtml b/web/user_management.xhtml new file mode 100644 index 00000000..b7fe68a2 --- /dev/null +++ b/web/user_management.xhtml @@ -0,0 +1,43 @@ + + + +
+
+
+ +
+
+
+
\ No newline at end of file