diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..a0bdcdd8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +/SimpleBlog/nbproject/private/ +/SimpleBlog/build/ +/SimpleBlog/dist/ +/JsfJpaCrud/nbproject/private/ +/JsfJpaCrud/build/ +/SimpleBlogJsfJpa/nbproject/private/ +/SimpleBlogJsfJpa/build/ \ No newline at end of file diff --git a/SimpleBlog/build.xml b/SimpleBlog/build.xml new file mode 100644 index 00000000..b949aae0 --- /dev/null +++ b/SimpleBlog/build.xml @@ -0,0 +1,71 @@ + + + + + + + + + + + Builds, tests, and runs the project SimpleBlog. + + + diff --git a/SimpleBlog/nbproject/ant-deploy.xml b/SimpleBlog/nbproject/ant-deploy.xml new file mode 100644 index 00000000..2d5f8778 --- /dev/null +++ b/SimpleBlog/nbproject/ant-deploy.xml @@ -0,0 +1,111 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SimpleBlog/nbproject/build-impl.xml b/SimpleBlog/nbproject/build-impl.xml new file mode 100644 index 00000000..ec443653 --- /dev/null +++ b/SimpleBlog/nbproject/build-impl.xml @@ -0,0 +1,1444 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/SimpleBlog/nbproject/faces-config.NavData b/SimpleBlog/nbproject/faces-config.NavData new file mode 100644 index 00000000..298bfc50 --- /dev/null +++ b/SimpleBlog/nbproject/faces-config.NavData @@ -0,0 +1,6 @@ + + + + + + diff --git a/SimpleBlog/nbproject/genfiles.properties b/SimpleBlog/nbproject/genfiles.properties new file mode 100644 index 00000000..b23e99c3 --- /dev/null +++ b/SimpleBlog/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=5ee20916 +build.xml.script.CRC32=bedf25f1 +build.xml.stylesheet.CRC32=651128d4@1.68.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=5ee20916 +nbproject/build-impl.xml.script.CRC32=e646747f +nbproject/build-impl.xml.stylesheet.CRC32=99ea4b56@1.68.1.1 diff --git a/SimpleBlog/nbproject/project.properties b/SimpleBlog/nbproject/project.properties new file mode 100644 index 00000000..5f66438f --- /dev/null +++ b/SimpleBlog/nbproject/project.properties @@ -0,0 +1,94 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=true +annotation.processing.processor.options=-Aeclipselink.canonicalmodel.use_static_factory=false +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-projectapi.jsf_2e_language=Facelets +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= +includes=** +j2ee.compile.on.save=true +j2ee.copy.static.files.on.save=true +j2ee.deploy.on.save=true +j2ee.platform=1.7-web +j2ee.platform.classpath=${j2ee.server.home}/modules/endorsed/javax.annotation-api.jar:${j2ee.server.home}/modules/endorsed/jaxb-api.jar:${j2ee.server.home}/modules/endorsed/webservices-api-osgi.jar:${j2ee.server.home}/modules/bean-validator.jar:${j2ee.server.home}/modules/javax.batch-api.jar:${j2ee.server.home}/modules/javax.ejb-api.jar:${j2ee.server.home}/modules/javax.el.jar:${j2ee.server.home}/modules/javax.enterprise.concurrent-api.jar:${j2ee.server.home}/modules/javax.enterprise.concurrent.jar:${j2ee.server.home}/modules/javax.enterprise.deploy-api.jar:${j2ee.server.home}/modules/javax.faces.jar:${j2ee.server.home}/modules/javax.inject.jar:${j2ee.server.home}/modules/javax.interceptor-api.jar:${j2ee.server.home}/modules/javax.jms-api.jar:${j2ee.server.home}/modules/javax.json.jar:${j2ee.server.home}/modules/javax.mail.jar:${j2ee.server.home}/modules/javax.management.j2ee-api.jar:${j2ee.server.home}/modules/javax.persistence.jar:${j2ee.server.home}/modules/javax.resource-api.jar:${j2ee.server.home}/modules/javax.security.auth.message-api.jar:${j2ee.server.home}/modules/javax.security.jacc-api.jar:${j2ee.server.home}/modules/javax.servlet-api.jar:${j2ee.server.home}/modules/javax.servlet.jsp-api.jar:${j2ee.server.home}/modules/javax.servlet.jsp.jar:${j2ee.server.home}/modules/javax.servlet.jsp.jstl-api.jar:${j2ee.server.home}/modules/javax.servlet.jsp.jstl.jar:${j2ee.server.home}/modules/javax.transaction-api.jar:${j2ee.server.home}/modules/javax.websocket-api.jar:${j2ee.server.home}/modules/javax.ws.rs-api.jar:${j2ee.server.home}/modules/javax.xml.registry-api.jar:${j2ee.server.home}/modules/javax.xml.rpc-api.jar:${j2ee.server.home}/modules/jaxb-osgi.jar:${j2ee.server.home}/modules/webservices-osgi.jar:${j2ee.server.home}/modules/weld-osgi-bundle.jar:${j2ee.server.middleware}/mq/lib/jaxm-api.jar +j2ee.platform.embeddableejb.classpath=${j2ee.server.home}/lib/embedded/glassfish-embedded-static-shell.jar +j2ee.platform.wscompile.classpath=${j2ee.server.home}/modules/webservices-osgi.jar +j2ee.platform.wsgen.classpath=${j2ee.server.home}/modules/webservices-osgi.jar:${j2ee.server.home}/modules/endorsed/webservices-api-osgi.jar:${j2ee.server.home}/modules/jaxb-osgi.jar:${j2ee.server.home}/modules/endorsed/jaxb-api.jar +j2ee.platform.wsimport.classpath=${j2ee.server.home}/modules/webservices-osgi.jar:${j2ee.server.home}/modules/endorsed/webservices-api-osgi.jar:${j2ee.server.home}/modules/jaxb-osgi.jar:${j2ee.server.home}/modules/endorsed/jaxb-api.jar +j2ee.platform.wsit.classpath= +j2ee.server.type=gfv3ee6 +jar.compress=false +javac.classpath=\ + ${libs.primefaces.classpath} +# Space-separated list of extra javac options +javac.compilerargs= +javac.debug=true +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath}:\ + ${libs.eclipselink.classpath}:\ + ${libs.eclipselinkmodelgen.classpath} +javac.source=1.8 +javac.target=1.8 +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 +no.dependencies=false +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=SimpleBlog.war +web.docbase.dir=web +webinf.dir=web/WEB-INF diff --git a/SimpleBlog/nbproject/project.xml b/SimpleBlog/nbproject/project.xml new file mode 100644 index 00000000..a2343094 --- /dev/null +++ b/SimpleBlog/nbproject/project.xml @@ -0,0 +1,23 @@ + + + org.netbeans.modules.web.project + + + SimpleBlog + 1.6.5 + + + ${libs.primefaces.classpath} + WEB-INF/lib + + + + + + + + + + + + diff --git a/SimpleBlog/src/conf/MANIFEST.MF b/SimpleBlog/src/conf/MANIFEST.MF new file mode 100644 index 00000000..59499bce --- /dev/null +++ b/SimpleBlog/src/conf/MANIFEST.MF @@ -0,0 +1,2 @@ +Manifest-Version: 1.0 + diff --git a/SimpleBlog/src/conf/persistence.xml b/SimpleBlog/src/conf/persistence.xml new file mode 100644 index 00000000..12554f06 --- /dev/null +++ b/SimpleBlog/src/conf/persistence.xml @@ -0,0 +1,9 @@ + + + + jdbc/__default + false + + + + diff --git a/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jpa/controller/CommentJpaController.java b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jpa/controller/CommentJpaController.java new file mode 100644 index 00000000..fb814edd --- /dev/null +++ b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jpa/controller/CommentJpaController.java @@ -0,0 +1,75 @@ +package id.ac.itb.informatika.wbd.jpa.controller; + +import id.ac.itb.informatika.wbd.jpa.entities.Comment; +import id.ac.itb.informatika.wbd.jpa.entities.Post; +import id.ac.itb.informatika.wbd.jsf.JsfUtil; +import java.util.ArrayList; +import java.util.List; +import javax.annotation.Resource; +import javax.faces.bean.ManagedBean; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityNotFoundException; +import javax.persistence.PersistenceUnit; +import javax.persistence.Query; +import javax.transaction.UserTransaction; + +@ManagedBean + +public class CommentJpaController extends JpaController { + + public void create(Comment comment) throws Exception { + EntityManager em = null; + + //comment = new Comment(); + try { + utx.begin(); + em = getEntityManager(); + em.persist(comment); + utx.commit(); + } catch (Exception ex) { + utx.rollback(); + } finally { + if (em != null) { + em.close(); + } + } + } + + public List getAllComments() { + EntityManager em = getEntityManager(); + try { + Query q = em.createQuery("select object(o) from Comment as o order by o.date desc"); + List entries = q.getResultList(); + if (entries == null) { + entries = new ArrayList(); + } + return entries; + } finally { + em.close(); + } + } + + public List getPostComments(Long id) { + EntityManager em = getEntityManager(); + try { + Query q = em.createQuery("select object(o) from Comment as o where o.post = :id order by o.date asc"); + List entries = q.setParameter("id", id).getResultList();; + if (entries == null) { + entries = new ArrayList(); + } + return entries; + } finally { + em.close(); + } + } + + public Comment findComment(Long id) { + EntityManager em = getEntityManager(); + try { + return em.find(Comment.class, id); + } finally { + em.close(); + } + } +} diff --git a/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jpa/controller/JpaController.java b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jpa/controller/JpaController.java new file mode 100644 index 00000000..eef4fdf3 --- /dev/null +++ b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jpa/controller/JpaController.java @@ -0,0 +1,47 @@ +package id.ac.itb.informatika.wbd.jpa.controller; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import javax.annotation.Resource; +import javax.faces.bean.ManagedBean; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceUnit; +import javax.transaction.UserTransaction; + +@ManagedBean +public class JpaController { + @Resource + protected UserTransaction utx = null; + + @PersistenceUnit(unitName = "SimpleBlogPU") + protected EntityManagerFactory emf = null; + + protected Connection con = null; + + public JpaController() { + } + + public Connection getConnection() throws SQLException{ + + //if (con == null) { + String url = "jdbc:mysql://localhost:3306/simple_blog"; + String user = "root"; + String password = ""; + try { + con = DriverManager.getConnection(url, user, password); + //System.out.println("Connection completed."); + } catch (SQLException ex) { + //System.out.println(ex.getMessage()); + } + finally{ + } + //} + return con; + } + + public EntityManager getEntityManager() { + return emf.createEntityManager(); + } +} diff --git a/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jpa/controller/PostJpaController.java b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jpa/controller/PostJpaController.java new file mode 100644 index 00000000..03d596f3 --- /dev/null +++ b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jpa/controller/PostJpaController.java @@ -0,0 +1,157 @@ +package id.ac.itb.informatika.wbd.jpa.controller; + +import id.ac.itb.informatika.wbd.jpa.entities.Post; +import id.ac.itb.informatika.wbd.jsf.JsfUtil; +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; +import javax.annotation.Resource; +import javax.faces.bean.ManagedBean; +import javax.faces.context.FacesContext; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityNotFoundException; +import javax.persistence.PersistenceUnit; +import javax.persistence.Query; +import javax.transaction.UserTransaction; + +@ManagedBean +public class PostJpaController extends JpaController { + + public void create(Post post){ + try{ + Connection con = getConnection(); + String query = "INSERT INTO Post (title,content,date,published,deleted,featured) " + + "VALUES (?, ?, ?, ?, ?, ?)"; + PreparedStatement ps = con.prepareStatement(query); + ps.setString(1, post.getTitle()); + ps.setString(2, post.getContent()); + ps.setDate(3, post.getDate()); + ps.setBoolean(4, false); + ps.setBoolean(5, false); + ps.setBoolean(6, false); + ps.executeUpdate(); + //FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml"); + con.close(); + } catch (SQLException ex) { + System.out.println(ex.getMessage()); + } + } + + public void create(Post post, Boolean alternative) throws Exception { + + EntityManager em = null; + try { + utx.begin(); + em = getEntityManager(); + em.persist(post); + utx.commit(); + } catch (Exception ex) { + utx.rollback(); + } finally { + if (em != null) { + em.close(); + } + } + } + + public void edit(Post post) throws Exception { + EntityManager em = getEntityManager(); + + try { + utx.begin(); + //Post persistentPost = em.find(Post.class, post.getId()); + em.joinTransaction(); + post = em.merge(post); + utx.commit(); + } catch (Exception ex) { + utx.rollback(); + } finally { + if (em != null) { + em.close(); + } + } + } + + public void destroy(Long id) throws Exception { + EntityManager em = null; + try { + utx.begin(); + em = getEntityManager(); + //em.joinTransaction(); + Post post = null; + try { + post = em.getReference(Post.class, id); + post.getId(); + } catch (EntityNotFoundException e) { + JsfUtil.ensureAddErrorMessage(e, "A persistence error occurred."); + } + + //post = em.find(Post.class, id); + em.remove(post); + //Query query = em.createQuery("DELETE FROM posts c WHERE c.id = :p"); + //int deletedCount = query.setParameter("p", id).executeUpdate(); + utx.commit(); + } catch (Exception ex) { + utx.rollback(); + //throw ex; + } finally { + if (em != null) { + em.close(); + } + } + } + + public List getAllPosts(Boolean alternative) { + EntityManager em = getEntityManager(); + try { + Query q = em.createQuery("select object(o) from Post as o order by o.date desc"); + List entries = q.getResultList(); + if (entries == null) { + entries = new ArrayList(); + } + return entries; + } finally { + em.close(); + } + } + + public List getAllPosts(){ + List list = new ArrayList(); + + try{ + con = getConnection(); + + Statement sm = con.createStatement(); + ResultSet res = sm.executeQuery("SELECT object(o) FROM Post as o ORDER BY o.date DESC"); + + while(res.next()){ + Post pos = new Post(); + pos.setId(res.getLong("id")); + pos.setTitle(res.getString("title")); + pos.setContent(res.getString("contenct")); + pos.setDate(res.getDate("date")); + } + con.close(); + } catch (SQLException ex) { + System.out.println(ex.getMessage()); + } + + return list; + } + + public Post findPost(Long id) { + EntityManager em = getEntityManager(); + try { + return em.find(Post.class, id); + } finally { + em.close(); + } + } +} diff --git a/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jpa/controller/UserJpaController.java b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jpa/controller/UserJpaController.java new file mode 100644 index 00000000..95039bee --- /dev/null +++ b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jpa/controller/UserJpaController.java @@ -0,0 +1,114 @@ +package id.ac.itb.informatika.wbd.jpa.controller; + +import id.ac.itb.informatika.wbd.jpa.entities.Post; +import id.ac.itb.informatika.wbd.jsf.JsfUtil; +import java.util.ArrayList; +import java.util.List; +import javax.annotation.Resource; +import javax.faces.bean.ManagedBean; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityNotFoundException; +import javax.persistence.PersistenceUnit; +import javax.persistence.Query; +import javax.transaction.UserTransaction; + +@ManagedBean +public class UserJpaController { + @Resource + private UserTransaction utx = null; + + @PersistenceUnit(unitName = "SimpleBlogPU") + private EntityManagerFactory emf = null; + + public EntityManager getEntityManager() { + return emf.createEntityManager(); + } + + public void create(Post post) throws Exception { + EntityManager em = null; + + try { + utx.begin(); + em = getEntityManager(); + em.persist(post); + utx.commit(); + } catch (Exception ex) { + utx.rollback(); + } finally { + if (em != null) { + em.close(); + } + } + } + + public void edit(Post post) throws Exception { + EntityManager em = getEntityManager(); + + try { + utx.begin(); + //Post persistentPost = em.find(Post.class, post.getId()); + em.joinTransaction(); + post = em.merge(post); + utx.commit(); + } catch (Exception ex) { + utx.rollback(); + } finally { + if (em != null) { + em.close(); + } + } + } + + public void destroy(Long id) throws Exception { + EntityManager em = null; + try { + utx.begin(); + em = getEntityManager(); + //em.joinTransaction(); + Post post = null; + try { + post = em.getReference(Post.class, id); + post.getId(); + } catch (EntityNotFoundException e) { + JsfUtil.ensureAddErrorMessage(e, "A persistence error occurred."); + } + + //post = em.find(Post.class, id); + em.remove(post); + //Query query = em.createQuery("DELETE FROM posts c WHERE c.id = :p"); + //int deletedCount = query.setParameter("p", id).executeUpdate(); + utx.commit(); + } catch (Exception ex) { + utx.rollback(); + //throw ex; + } finally { + if (em != null) { + em.close(); + } + } + } + + public List getAllPosts() { + EntityManager em = getEntityManager(); + try { + Query q = em.createQuery("select object(o) from Post as o order by o.date desc"); + List entries = q.getResultList(); + if (entries == null) { + entries = new ArrayList(); + } + return entries; + } finally { + em.close(); + } + } + + public Post findPost(Long id) { + EntityManager em = getEntityManager(); + try { + return em.find(Post.class, id); + } finally { + em.close(); + } + } +} diff --git a/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jpa/entities/Comment.java b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jpa/entities/Comment.java new file mode 100644 index 00000000..719e444c --- /dev/null +++ b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jpa/entities/Comment.java @@ -0,0 +1,162 @@ +package id.ac.itb.informatika.wbd.jpa.entities; + +import java.io.Serializable; +import java.util.Date; +import javax.persistence.Basic; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; + +@Entity +@Table(name = "comments") +@NamedQueries({@NamedQuery(name = "Comment.findAll", query = "SELECT c FROM Comment c"), + @NamedQuery(name = "Comment.findById", query = "SELECT c FROM Comment c WHERE c.id = :id"), + @NamedQuery(name = "Comment.findByPost", query = "SELECT c FROM Comment c WHERE c.post = :post")}) +public class Comment implements Serializable { + private static final long serialVersionUID = 1L; + + @Id + @Basic(optional = false) + @Column(name = "comment_id") + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + // @JoinColumn(name = "post_id") + // @ManyToOne + // private Post post; + + @Column(name = "post_id") + private Long post; + + @Column(name = "comment_author") + private String name; + + @Column(name = "comment_author_email") + private String email; + + @Column(name = "comment_author_ip") + private String ip; + + @Column(name = "comment_date") + @Temporal(TemporalType.TIMESTAMP) + private Date date; + + @Column(name = "comment_content") + private String content; + + @Column(name = "post_featured") + private Boolean featured; + + public Comment() { + } + + public Comment(Long id) { + this.id = id; + } + + public Comment(Long id, Date date, String content) { + this.id = id; + this.date = date; + this.content = content; + this.featured = false; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public Boolean getFeatured() { + return featured; + } + + public void setFeatured(Boolean featured) { + this.featured = featured; + } + + public Long getPost() { + return post; + } + + public void setPost(Long post) { + this.post = post; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + @Override + public int hashCode() { + int hash = 0; + hash += (id != null ? id.hashCode() : 0); + return hash; + } + + @Override + public boolean equals(Object object) { + // TODO: Warning - this method won't work in the case the id fields are not set + if (!(object instanceof Comment)) { + return false; + } + Comment other = (Comment) object; + if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { + return false; + } + return true; + } + + @Override + public String toString() { + return "id.ac.itb.informatika.wbd.jpa.entities.Post[ id=" + id + " ]"; + } + +} diff --git a/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jpa/entities/Post.java b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jpa/entities/Post.java new file mode 100644 index 00000000..d672cf69 --- /dev/null +++ b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jpa/entities/Post.java @@ -0,0 +1,168 @@ +package id.ac.itb.informatika.wbd.jpa.entities; + +import java.io.Serializable; +import java.sql.Date; +import java.util.Set; +import javax.persistence.Basic; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.persistence.OneToMany; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; + +@Entity +@Table(name = "posts") +@NamedQueries({@NamedQuery(name = "Post.findAll", query = "SELECT c FROM Post c"), + @NamedQuery(name = "Post.findById", query = "SELECT c FROM Post c WHERE c.id = :id"), + @NamedQuery(name = "Post.findByTitle", query = "SELECT c FROM Post c WHERE c.title = :title")}) +public class Post implements Serializable { + private static final long serialVersionUID = 1L; + + @Id + @Basic(optional = false) + @Column(name = "post_id") + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @Column(name = "post_title") + private String title; + + @Column(name = "post_date") + // @Temporal(TemporalType.TIMESTAMP) + private Date date; + + @Column(name = "post_content") + private String content; + + @Column(name = "post_featured") + private Boolean featured; + + @Column(name = "post_published") + private Boolean published; + + @Column(name = "post_deleted") + private Boolean deleted; + + @Column(name = "post_modified") + //@Temporal(TemporalType.TIMESTAMP) + private Date modified; + + // @OneToMany(mappedBy = "post") // mappedBy indicates that this side is the + // inverse side, and that the mapping is defined by the attribute parentOrder + // at the other side of the association. + // private Set comments; + + public Post() { + } + + public Post(Long id) { + this.id = id; + } + + public Post(Long id, String title, Date date, String content) { + this.id = id; + this.title = title; + this.date = date; + this.content = content; + this.featured = false; + this.published = false; + this.modified = date; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public Boolean getFeatured() { + return featured; + } + + public void setFeatured(Boolean featured) { + this.featured = featured; + } + + public Boolean getPublished() { + return published; + } + + public void setPublished(Boolean published) { + this.published = published; + } + + public Date getModified() { + return modified; + } + + public void setModified(Date modified) { + this.modified = modified; + } + + public Boolean getDeleted() { + return deleted; + } + + public void setDeleted(Boolean deleted) { + this.deleted = deleted; + } + + + @Override + public int hashCode() { + int hash = 0; + hash += (id != null ? id.hashCode() : 0); + return hash; + } + + @Override + public boolean equals(Object object) { + // TODO: Warning - this method won't work in the case the id fields are not set + if (!(object instanceof Post)) { + return false; + } + Post other = (Post) object; + if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { + return false; + } + return true; + } + + @Override + public String toString() { + return "id.ac.itb.informatika.wbd.jpa.entities.Post[ id=" + id + " ]"; + } + +} diff --git a/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jpa/entities/User.java b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jpa/entities/User.java new file mode 100644 index 00000000..d1b7cc26 --- /dev/null +++ b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jpa/entities/User.java @@ -0,0 +1,86 @@ +package id.ac.itb.informatika.wbd.jpa.entities; + +import javax.persistence.Basic; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.persistence.Table; + +@Entity +@Table(name = "users") +@NamedQueries({@NamedQuery(name = "User.findAll", query = "SELECT c FROM User c"), + @NamedQuery(name = "User.findById", query = "SELECT c FROM User c WHERE c.id = :id"), + @NamedQuery(name = "User.findByRole", query = "SELECT c FROM User c WHERE c.role = :role")}) + +public class User { + + @Id + @Basic(optional = false) + @Column(name = "user_id") + private Long id; + + @Column(name = "user_email") + private String email; + + @Column(name = "user_name") + private String name; + + @Column(name = "user_username") + private String username; + + @Column(name = "user_password") + private String password; + + @Column(name = "user_role") + private String role; + + public Long getId(){ + return id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getEmail(){ + return email; + } + + public String getName(){ + return name; + } + + public String getPassword(){ + return password; + } + + public String getRole(){ + return role; + } + + public void setId(Long id){ + this.id = id; + } + + public void setEmail(String email){ + this.email = email; + } + + public void setName(String name){ + this.name = name; + } + + public void setPassword(String password){ + this.password = password; + } + + public void setRole(String role){ + this.role = role; + } +} diff --git a/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jsf/CommentController.java b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jsf/CommentController.java new file mode 100644 index 00000000..456fa25f --- /dev/null +++ b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jsf/CommentController.java @@ -0,0 +1,69 @@ +package id.ac.itb.informatika.wbd.jsf; + +import id.ac.itb.informatika.wbd.jpa.controller.CommentJpaController; +import id.ac.itb.informatika.wbd.jpa.entities.Comment; +import id.ac.itb.informatika.wbd.jpa.entities.Post; +import java.util.ArrayList; +import java.util.List; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.ManagedProperty; +import javax.faces.bean.RequestScoped; +import javax.faces.bean.ViewScoped; +import javax.faces.context.FacesContext; + +@ManagedBean +@ViewScoped +public class CommentController { + + public CommentController() { + FacesContext facesContext = FacesContext.getCurrentInstance(); + jpaController = (CommentJpaController) facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, "commentJpaControl"); + } + + private Comment commentCurrent = null; + private List commentList = null; + private CommentJpaController jpaController = null; + + public Comment getCommentCurrent() { + if (commentCurrent == null) { + commentCurrent = new Comment(); + } + return commentCurrent; + } + + public void setCommentCurrent(Comment commentCurrent) { + this.commentCurrent = commentCurrent; + } + + public List getCommentList() { + if (commentList == null) { + //JsfUtil.addSuccessMessage(Long.toString(postController.getPostCurrent().getId())); + //commentList = jpaController.getPostComments(id); + //loadComment(); + commentList = new ArrayList(); + } + return commentList; + } + + public int getCommentCount() { + return commentList.size(); + } + + public String loadComment(Long id) { + //commentList = jpaController.getPostComments(postController.getPostCurrent().getId()); + commentList = jpaController.getPostComments(id); + return null; + } + + public String newComment(Long id) { + try { + commentCurrent.setPost(id); + jpaController.create(commentCurrent); + JsfUtil.addSuccessMessage("Comment sudah berhasil dibuat"); + } catch (Exception e) { + JsfUtil.ensureAddErrorMessage(e, "A persistence error occurred."); + return null; + } + return null; + } +} diff --git a/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jsf/JsfCrudELResolver.java b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jsf/JsfCrudELResolver.java new file mode 100644 index 00000000..73f51910 --- /dev/null +++ b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jsf/JsfCrudELResolver.java @@ -0,0 +1,608 @@ +package id.ac.itb.informatika.wbd.jsf; + +import java.beans.BeanInfo; +import java.beans.FeatureDescriptor; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import javax.el.ELContext; +import javax.el.ELException; +import javax.el.ELResolver; + +public class JsfCrudELResolver extends ELResolver { + public static final String JSFCRUD_CLASS = "jsfcrud_class"; + public static final String JSFCRUD_METHOD = "jsfcrud_method"; + public static final String JSFCRUD_PARAMS = "jsfcrud_params"; + public static final String JSFCRUD_INVOKE = "jsfcrud_invoke"; + public static final String JSFCRUD_TRANSFORM = "jsfcrud_transform"; + public static final String JSFCRUD_NULL = "jsfcrud_null"; + + /** + * {@inheritDoc} + */ + public Object getValue(ELContext context, Object base, Object property) { + if(context == null) { + throw new NullPointerException(); + } + + String propertyName = null; + if (property != null) { + propertyName = property.toString(); + } + + if (JSFCRUD_NULL.equals(propertyName) && + ! (base instanceof JsfCrudMethod) && + ! (base instanceof JsfCrudParameterizedMethod) && + ! (base instanceof JsfCrudTransform) ) { + throw new ELException(JSFCRUD_NULL + " expects a base of type JsfCrudMethod, JsfCrudParameterizedMethod, or JsfCrudTransform; received " + base); + } + + Object result = null; + + if (JSFCRUD_CLASS.equals(propertyName)) { + if (base != null) { + throw new ELException(JSFCRUD_CLASS + " expects a null base; received " + base); + } + result = new JsfCrudClass(); + context.setPropertyResolved(true); + return result; + } else if (JSFCRUD_METHOD.equals(propertyName)) { + if (base == null) { + throw new ELException(JSFCRUD_METHOD + " expects a non-null (possibly JsfCrudClass) base; received null"); + } + result = new JsfCrudMethod(base); + context.setPropertyResolved(true); + return result; + } else if (JSFCRUD_PARAMS.equals(propertyName)) { + if (! (base instanceof JsfCrudMethod)) { + throw new ELException(JSFCRUD_PARAMS + " expects a base of type JsfCrudMethod; received " + base); + } + result = new JsfCrudParameterizedMethod((JsfCrudMethod)base); + context.setPropertyResolved(true); + return result; + } else if (JSFCRUD_INVOKE.equals(propertyName)) { + JsfCrudParameterizedMethod parameterizedMethod = getParameterizedMethodToInvoke(base); + result = parameterizedMethod.invoke(); + context.setPropertyResolved(true); + return result; + } else if (JSFCRUD_TRANSFORM.equals(propertyName)) { + if (base == null) { + throw new ELException(JSFCRUD_TRANSFORM + " expects a non-null base; received null"); + } + result = new JsfCrudTransform(base); + context.setPropertyResolved(true); + return result; + } else if (base instanceof JsfCrudClass) { + ((JsfCrudClass)base).setType(propertyName); + result = base; + context.setPropertyResolved(true); + return result; + } else if (base instanceof JsfCrudMethod) { + JsfCrudMethod baseAsMethod = (JsfCrudMethod)base; + if (baseAsMethod.getMethodName() == null) { + baseAsMethod.setMethodName(propertyName); + result = base; + context.setPropertyResolved(true); + return result; + } + else { + //already have a method name. start adding parameters. + JsfCrudParameterizedMethod pMethod = new JsfCrudParameterizedMethod(baseAsMethod); + pMethod.addParameter(property); + result = pMethod; + context.setPropertyResolved(true); + return result; + } + } else if (base instanceof JsfCrudParameterizedMethod) { + ((JsfCrudParameterizedMethod)base).addParameter(property); + result = base; + context.setPropertyResolved(true); + return result; + } else if (base instanceof JsfCrudTransform) { + if (JSFCRUD_NULL.equals(propertyName)) { + ((JsfCrudTransform)base).addNullMethod(); + result = base; + context.setPropertyResolved(true); + return result; + } else if (property instanceof JsfCrudMethod) { + ((JsfCrudTransform)base).addMethod((JsfCrudMethod)property); + result = base; + context.setPropertyResolved(true); + return result; + } + else { + result = ((JsfCrudTransform)base).getProperty(propertyName); + context.setPropertyResolved(true); + return result; + } + } + + return null; + } + + private JsfCrudParameterizedMethod getParameterizedMethodToInvoke(Object base) { + JsfCrudParameterizedMethod parameterizedMethod = null; + if (base instanceof JsfCrudParameterizedMethod) { + parameterizedMethod = (JsfCrudParameterizedMethod)base; + } else if (base instanceof JsfCrudMethod) { + parameterizedMethod = new JsfCrudParameterizedMethod((JsfCrudMethod)base); + } + if (parameterizedMethod == null) { + throw new ELException(JSFCRUD_INVOKE + " expects a base of type JsfCrudParameterizedMethod or JsfCrudMethod; received " + base); + } + return parameterizedMethod; + } + + /** + * {@inheritDoc} + */ + public void setValue(ELContext context, Object base, Object property, Object value) { + if(context == null) { + throw new NullPointerException(); + } + + String propertyName = null; + if (property != null) { + propertyName = property.toString(); + } + + if ( base instanceof JsfCrudClass || + base instanceof JsfCrudMethod || + base instanceof JsfCrudParameterizedMethod || + (base instanceof JsfCrudTransform && property instanceof JsfCrudMethod) || + JSFCRUD_CLASS.equals(propertyName) || + JSFCRUD_METHOD.equals(propertyName) || + JSFCRUD_PARAMS.equals(propertyName) || + JSFCRUD_INVOKE.equals(propertyName) || + JSFCRUD_TRANSFORM.equals(propertyName) || + JSFCRUD_NULL.equals(propertyName) || + property == null ) { + throw new ELException("setValue was called with base " + base + ", property " + property + ", and value " + value + "; expected a base of type JsfCrudTransform and a valid property of the JsfCrudTransform's own base"); + } + + if (base instanceof JsfCrudTransform) { + ((JsfCrudTransform)base).setProperty(propertyName, value); + context.setPropertyResolved(true); + } + } + + /** + * {@inheritDoc} + */ + public boolean isReadOnly(ELContext context, Object base, Object property) { + if(context == null) { + throw new NullPointerException(); + } + + String propertyName = null; + if (property != null) { + propertyName = property.toString(); + } + + if (JSFCRUD_CLASS.equals(propertyName) && base == null) { + context.setPropertyResolved(true); + } else if (JSFCRUD_METHOD.equals(propertyName) && base != null) { + context.setPropertyResolved(true); + } else if (JSFCRUD_PARAMS.equals(propertyName) && (base instanceof JsfCrudMethod)) { + context.setPropertyResolved(true); + } else if (JSFCRUD_INVOKE.equals(propertyName) && (base instanceof JsfCrudParameterizedMethod || base instanceof JsfCrudMethod)) { + context.setPropertyResolved(true); + } else if (JSFCRUD_TRANSFORM.equals(propertyName) && base != null) { + context.setPropertyResolved(true); + } else if (base instanceof JsfCrudClass) { + context.setPropertyResolved(true); + } else if (base instanceof JsfCrudMethod) { + context.setPropertyResolved(true); + } else if (base instanceof JsfCrudParameterizedMethod) { + context.setPropertyResolved(true); + } else if (base instanceof JsfCrudTransform) { + if (property instanceof JsfCrudMethod) { + context.setPropertyResolved(true); + } + else { + context.setPropertyResolved(true); + return false; + } + } else { + return false; + } + + return true; + } + + /** + * {@inheritDoc} + */ + public Class getType(ELContext context, Object base, Object property) { + if(context == null) { + throw new NullPointerException(); + } + + String propertyName = null; + if (property != null) { + propertyName = property.toString(); + } + + if (JSFCRUD_CLASS.equals(propertyName) && base == null) { + context.setPropertyResolved(true); + return JsfCrudClass.class; + } else if (JSFCRUD_METHOD.equals(propertyName) && base != null) { + context.setPropertyResolved(true); + return JsfCrudMethod.class; + } else if (JSFCRUD_PARAMS.equals(propertyName) && (base instanceof JsfCrudMethod)) { + context.setPropertyResolved(true); + return JsfCrudParameterizedMethod.class; + } else if (JSFCRUD_INVOKE.equals(propertyName) && (base instanceof JsfCrudParameterizedMethod || base instanceof JsfCrudMethod)) { + JsfCrudParameterizedMethod parameterizedMethod = getParameterizedMethodToInvoke(base); + Class result = parameterizedMethod.getReturnType(); + context.setPropertyResolved(true); + return result; + } else if (JSFCRUD_TRANSFORM.equals(propertyName) && base != null) { + context.setPropertyResolved(true); + return JsfCrudTransform.class; + } else if (base instanceof JsfCrudClass) { + context.setPropertyResolved(true); + return JsfCrudClass.class; + } else if (base instanceof JsfCrudMethod) { + JsfCrudMethod baseAsMethod = (JsfCrudMethod)base; + if (baseAsMethod.getMethodName() == null) { + context.setPropertyResolved(true); + return JsfCrudMethod.class; + } + else { + context.setPropertyResolved(true); + return JsfCrudParameterizedMethod.class; + } + } else if (base instanceof JsfCrudParameterizedMethod) { + context.setPropertyResolved(true); + return JsfCrudParameterizedMethod.class; + } else if (base instanceof JsfCrudTransform) { + if (property instanceof JsfCrudMethod) { + context.setPropertyResolved(true); + return JsfCrudTransform.class; + } + else { + Class result = ((JsfCrudTransform)base).getPropertyType(propertyName); + context.setPropertyResolved(true); + return result; + } + } + + return null; + } + + /** + * {@inheritDoc} + */ + public Iterator getFeatureDescriptors(ELContext context, Object base) { + //todo: implement + return null; + } + + /** + * {@inheritDoc} + */ + public Class getCommonPropertyType(ELContext context, + Object base) { + if (context == null) { + throw new NullPointerException(); + } + + if (base == null || base instanceof JsfCrudClass) { + return String.class; + } else if (base instanceof JsfCrudMethod) { + JsfCrudMethod baseAsMethod = (JsfCrudMethod)base; + if (baseAsMethod.getMethodName() == null) { + return String.class; + } + else { + return Object.class; //could be a param Object, JSFCRUD_NULL, or JSFCRUD_INVOKE + } + } else if (base instanceof JsfCrudParameterizedMethod) { + return Object.class; //could be a param Object, JSFCRUD_NULL, or JSFCRUD_INVOKE + } else if (base instanceof JsfCrudTransform) { + JsfCrudTransform baseAsTransform = (JsfCrudTransform)base; + boolean[] tma = baseAsTransform.transformMethodsAssigned; + if (!tma[0] || !tma[1]) { + return Object.class; //could be a JsfCrudMethod or JSFCRUD_NULL + } + else { + return String.class; //the tailing propertyName + } + } else if (base != null) { + return String.class; //a catch-all + } + + return null; + } + + private class JsfCrudClass { + private Class type; + public Class getType() { + return type; + } + public void setType(String typeName) { + try { + type = Class.forName(typeName); + } catch (ClassNotFoundException e){ + throw new ELException(e); + } + } + @Override + public String toString() { + return "JsfCrudClass[" + type + "]"; + } + } + + private class JsfCrudMethod { + private Object base; //can be an JsfCrudClass instance, or an arbitrary Object + private String methodName; + public JsfCrudMethod(Object base) { + this.base = base; + } + public Object getBase() { + return base; + } + public String getMethodName() { + return methodName; + } + public void setMethodName(String methodName) { + this.methodName = methodName; + } + @Override + public String toString() { + return "JsfCrudMethod[base=" + base + ",methodName=" + methodName + "]"; + } + } + + private class JsfCrudParameterizedMethod { + private JsfCrudMethod method; + private List actualParams; + private Method methodToInvoke; + + public JsfCrudParameterizedMethod(JsfCrudMethod method) { + this.method = method; + actualParams = new ArrayList(); + } + public JsfCrudMethod getMethod() { + return method; + } + public void addParameter(Object param) { + if (JsfCrudELResolver.JSFCRUD_NULL.equals(param)) { + param = null; + } + actualParams.add(param); + } + @Override + public String toString() { + StringBuffer sb = new StringBuffer("JsfCrudParameterizedMethod[method="); + sb.append(method); + sb.append(",params=List["); + int i = 0; + for (Object param : actualParams) { + if (i > 0) { + sb.append(","); + } + sb.append(param); + i++; + } + sb.append("]]"); + return sb.toString(); + } + public Object invoke() { + findMethodToInvoke(); + Object methodBase = method.getBase(); + Object instance = methodBase instanceof JsfCrudClass ? null : methodBase; + Object[] paramArray = actualParams.toArray(); + try { + return methodToInvoke.invoke(instance, paramArray); + } catch (IllegalAccessException e) { + throw new ELException(e); + } catch (InvocationTargetException e) { + throw new ELException(e); + } + } + + public Class getReturnType() { + findMethodToInvoke(); + return methodToInvoke.getReturnType(); + } + + private void findMethodToInvoke() { + if (methodToInvoke != null) { + return; + } + + Object methodBase = method.getBase(); + JsfCrudClass staticMethodBase = null; + if (methodBase instanceof JsfCrudClass) { + staticMethodBase = (JsfCrudClass)methodBase; + } + Class type; + Method[] methodsOfType; + if (staticMethodBase == null) { + type = methodBase.getClass(); + methodsOfType = type.getMethods(); + } + else { + type = staticMethodBase.getType(); + methodsOfType = type.getDeclaredMethods(); + } + methodsOfTypeLoop: + for (Method methodOfType : methodsOfType) { + String methodName = method.getMethodName(); + String methodOfTypeName = methodOfType.getName(); + if (!methodName.equals(methodOfTypeName)) { + continue; + } + if (staticMethodBase == null) { + if (Modifier.isStatic(methodOfType.getModifiers())) { + continue; + } + } + else { + if (!Modifier.isStatic(methodOfType.getModifiers())) { + continue; + } + } + Class[] methodOfTypeFormalParams = methodOfType.getParameterTypes(); + if (methodOfTypeFormalParams.length != actualParams.size()) { + continue; + } + for (int i = 0; i < methodOfTypeFormalParams.length; i++) { + Object param = actualParams.get(i); + if (param == null) { + if (methodOfTypeFormalParams[i].isPrimitive()) { + continue methodsOfTypeLoop; + } + } else { + Class paramType = param.getClass(); + if (!methodOfTypeFormalParams[i].isAssignableFrom(paramType)) { + continue methodsOfTypeLoop; + } + } + } + methodToInvoke = methodOfType; + break; + } + if (methodToInvoke == null) { + throw new ELException("could not find method to invoke; no appropriate method found in type " + type + ". JsfCrudParameterizedMethod was " + this); + } + } + } + + private class JsfCrudTransform { + private Object base; + private JsfCrudMethod[] transformMethods; + private boolean[] transformMethodsAssigned; + public JsfCrudTransform(Object base) { + this.base = base; + transformMethods = new JsfCrudMethod[2]; + transformMethodsAssigned = new boolean[2]; + } + @Override + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append("JsfCrudTransform=[base="); + sb.append(base); + sb.append(",transformMethod0="); + sb.append(transformMethods[0]); + sb.append(",transformMethod1="); + sb.append(transformMethods[1]); + sb.append(",transformMethodsAssigned0="); + sb.append(transformMethodsAssigned[0]); + sb.append(",transformMethodsAssigned1="); + sb.append(transformMethodsAssigned[1]); + sb.append("]"); + return sb.toString(); + } + public Object getBase() { + return base; + } + public void addMethod(JsfCrudMethod method) { + if (!transformMethodsAssigned[0]) { + transformMethods[0] = method; + transformMethodsAssigned[0] = true; + } + else { + if (transformMethodsAssigned[1]) { + throw new ELException("attempt to add more than two methods to a JsfCrudTransform; additional JsfCrudMethod was " + method); + } + transformMethods[1] = method; + transformMethodsAssigned[1] = true; + } + } + public void addNullMethod() { + if (!transformMethodsAssigned[0]) { + transformMethodsAssigned[0] = true; + } + else { + if (transformMethodsAssigned[1]) { + throw new ELException("attempt to add more than two methods to a JsfCrudTransform; additional JsfCrudMethod was null"); + } + transformMethodsAssigned[1] = true; + } + } + public Class getPropertyType(String propertyName) { + if (transformMethods[0] == null) { + PropertyDescriptor pd = getPropertyDescriptor(propertyName); + return pd.getPropertyType(); + } + JsfCrudParameterizedMethod parameterizedMethod = getParameterizedTransformationMethod(propertyName); + return parameterizedMethod.getReturnType(); + } + private Object getUntransformedProperty(String propertyName) { + PropertyDescriptor pd = getPropertyDescriptor(propertyName); + if (pd == null) { + throw new ELException("could not get untransformed property " + propertyName + " of base object " + base + ": base object has no such property"); + } + Method readMethod = pd.getReadMethod(); + Object rawResult; + try { + rawResult = readMethod.invoke(base); + } catch (IllegalAccessException e) { + throw new ELException(e); + } catch (InvocationTargetException e) { + throw new ELException(e); + } + return rawResult; + } + private JsfCrudParameterizedMethod getParameterizedTransformationMethod(String propertyName) { + Object rawResult = getUntransformedProperty(propertyName); + JsfCrudParameterizedMethod parameterizedMethod = new JsfCrudParameterizedMethod(transformMethods[0]); + parameterizedMethod.addParameter(rawResult); + return parameterizedMethod; + } + public Object getProperty(String propertyName) { + if (transformMethods[0] == null) { + return getUntransformedProperty(propertyName); + } + JsfCrudParameterizedMethod parameterizedMethod = getParameterizedTransformationMethod(propertyName); + return parameterizedMethod.invoke(); + } + public void setProperty(String propertyName, Object value) { + PropertyDescriptor pd = getPropertyDescriptor(propertyName); + if (pd == null) { + throw new ELException("could not set property " + propertyName + " of base object " + base + " with raw value " + value + ": base object has no such property"); + } + Object transformedOrUntransformedValue = null; + if (transformMethods[1] == null) { + transformedOrUntransformedValue = value; + } + else { + JsfCrudParameterizedMethod parameterizedMethod = new JsfCrudParameterizedMethod(transformMethods[1]); + parameterizedMethod.addParameter(value); + transformedOrUntransformedValue = parameterizedMethod.invoke(); + } + Method writeMethod = pd.getWriteMethod(); + try { + writeMethod.invoke(base, transformedOrUntransformedValue); + } catch (IllegalAccessException e) { + throw new ELException(e); + } catch (InvocationTargetException e) { + throw new ELException(e); + } + } + private PropertyDescriptor getPropertyDescriptor(String propertyName) { + Class baseType = base.getClass(); + BeanInfo info; + try { + info = Introspector.getBeanInfo(baseType); + } catch (IntrospectionException ie) { + throw new ELException(ie); + } + for (PropertyDescriptor pd : info.getPropertyDescriptors()) { + if (propertyName.equals(pd.getName())) { + return pd; + } + } + return null; + } + } +} diff --git a/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jsf/JsfUtil.java b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jsf/JsfUtil.java new file mode 100644 index 00000000..03d9df44 --- /dev/null +++ b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jsf/JsfUtil.java @@ -0,0 +1,101 @@ +package id.ac.itb.informatika.wbd.jsf; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import javax.faces.application.FacesMessage; +import javax.faces.component.UIComponent; +import javax.faces.context.FacesContext; +import javax.faces.convert.Converter; +import javax.faces.model.SelectItem; + + +public class JsfUtil { + + public static SelectItem[] getSelectItems(List entities, boolean selectOne) { + int size = selectOne ? entities.size() + 1 : entities.size(); + SelectItem[] items = new SelectItem[size]; + int i = 0; + if (selectOne) { + items[0] = new SelectItem("", "---"); + i++; + } + for (Object x : entities) { + items[i++] = new SelectItem(x, x.toString()); + } + return items; + } + + public static void ensureAddErrorMessage(Exception ex, String defaultMsg) { + String msg = ex.getLocalizedMessage(); + if (msg != null && msg.length() > 0) { + addErrorMessage(msg); + } else { + addErrorMessage(defaultMsg); + } + } + + public static void addErrorMessages(List messages) { + for (String message : messages) { + addErrorMessage(message); + } + } + + public static void addErrorMessage(String msg) { + FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg); + FacesContext.getCurrentInstance().addMessage(null, facesMsg); + } + + public static void addSuccessMessage(String msg) { + FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg); + FacesContext.getCurrentInstance().addMessage("successInfo", facesMsg); + } + + public static String getRequestParameter(String key) { + return FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(key); + } + + public static Object getObjectFromRequestParameter(String requestParameterName, Converter converter, UIComponent component) { + String theId = JsfUtil.getRequestParameter(requestParameterName); + return converter.getAsObject(FacesContext.getCurrentInstance(), component, theId); + } + + public static Collection arrayToCollection(T[] arr) { + if (arr == null) { + return new ArrayList(); + } + return Arrays.asList(arr); + } + + public static Object[] collectionToArray(Collection c) { + if (c == null) { + return new Object[0]; + } + return c.toArray(); + } + + public static String getAsConvertedString(Object object, Converter converter) { + return converter.getAsString(FacesContext.getCurrentInstance(), null, object); + } + + public static String getAsString(Object object) { + if (object instanceof Collection) { + Collection collection = (Collection)object; + if (collection.size() == 0) { + return "(No Items)"; + } + StringBuffer sb = new StringBuffer(); + int i = 0; + for (Object item : collection) { + if (i > 0) { + sb.append("
"); + } + sb.append(item); + i++; + } + return sb.toString(); + } + return String.valueOf(object); + } +} diff --git a/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jsf/PostController.java b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jsf/PostController.java new file mode 100644 index 00000000..af1b09fc --- /dev/null +++ b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jsf/PostController.java @@ -0,0 +1,93 @@ +package id.ac.itb.informatika.wbd.jsf; + +import id.ac.itb.informatika.wbd.jpa.controller.PostJpaController; +import id.ac.itb.informatika.wbd.jpa.entities.Post; +import java.util.List; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.RequestScoped; +import javax.faces.context.FacesContext; +import javax.faces.convert.Converter; + +@ManagedBean +@RequestScoped +public class PostController { + + public PostController() { + FacesContext facesContext = FacesContext.getCurrentInstance(); + jpaController = (PostJpaController) facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, "postJpaController"); + converter = new PostConverter(); + } + + private Post postCurrent = null; + private List postList = null; + private PostJpaController jpaController = null; + private PostConverter converter = null; + + + public void setPostCurrent(Post postCurrent) { + this.postCurrent = postCurrent; + } + + public Post getPostCurrent() { + if (postCurrent == null) { + postCurrent = (Post) JsfUtil.getObjectFromRequestParameter("jsfcrud.currentPost", converter, null); + } + if (postCurrent == null) { + postCurrent = new Post(); + } + return postCurrent; + } + + public List getPostList() { + if (postList == null) { + postList = jpaController.getAllPosts(); + } + return postList; + } + + public int getPostCount() { + return postList.size(); + } + + public Converter getConverter() { + return converter; + } + + public String newPost() { + String postString = converter.getAsString(FacesContext.getCurrentInstance(), null, postCurrent); + String currentPostString = JsfUtil.getRequestParameter("jsfcrud.currentPost"); + + try { + if (postCurrent.getId() == null) { + jpaController.create(postCurrent); + JsfUtil.addSuccessMessage("Post sudah berhasil dibuat"); + } else { + jpaController.edit(postCurrent); + JsfUtil.addSuccessMessage("Post sudah berhasil diedit "); + } + } catch (Exception e) { + JsfUtil.ensureAddErrorMessage(e, "A persistence error occurred."); + return null; + } + postList = jpaController.getAllPosts(); + return "post_list"; + + } + + public String deletePost(Post post) { + String idAsString = JsfUtil.getRequestParameter("jsfcrud.currentPost"); + Long id = new Long(idAsString); + + try { + jpaController.destroy(post.getId()); + JsfUtil.addSuccessMessage("Post berhasil dihapus"); + } catch (Exception e) { + JsfUtil.ensureAddErrorMessage(e, "A persistence error occurred."); + return null; + } + postList = jpaController.getAllPosts(); + return "post_list"; + } + + +} diff --git a/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jsf/PostConverter.java b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jsf/PostConverter.java new file mode 100644 index 00000000..c8eafb41 --- /dev/null +++ b/SimpleBlog/src/java/id/ac/itb/informatika/wbd/jsf/PostConverter.java @@ -0,0 +1,33 @@ +package id.ac.itb.informatika.wbd.jsf; + +import id.ac.itb.informatika.wbd.jpa.controller.PostJpaController; +import id.ac.itb.informatika.wbd.jpa.entities.Post; +import javax.faces.component.UIComponent; +import javax.faces.context.FacesContext; +import javax.faces.convert.Converter; + +public class PostConverter implements Converter { + + public Object getAsObject(FacesContext facesContext, UIComponent component, String string) { + if (string == null || string.length() == 0) { + return null; + } + Long id = new Long(string); + PostJpaController controller = (PostJpaController) facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, "postJpaController"); + return controller.findPost(id); + } + + public String getAsString(FacesContext facesContext, UIComponent component, Object object) { + if (object == null) { + return null; + } + if (object instanceof Post) { + Post o = (Post) object; + return o.getId() == null ? "" : o.getId().toString(); + } else { + throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: jpa.entities.Customer"); + } + } + +} + diff --git a/SimpleBlog/web/WEB-INF/faces-config.xml b/SimpleBlog/web/WEB-INF/faces-config.xml new file mode 100644 index 00000000..ca59c0f5 --- /dev/null +++ b/SimpleBlog/web/WEB-INF/faces-config.xml @@ -0,0 +1,29 @@ + + + + + id.ac.itb.informatika.wbd.jsf.JsfCrudELResolver + + + + + post_list + postList.xhtml + + + + + post_new + postNew.xhtml + + + + + post_edit + postNew.xhtml + + + diff --git a/SimpleBlog/web/WEB-INF/templates/foot.xhtml b/SimpleBlog/web/WEB-INF/templates/foot.xhtml new file mode 100644 index 00000000..49b15950 --- /dev/null +++ b/SimpleBlog/web/WEB-INF/templates/foot.xhtml @@ -0,0 +1,6 @@ + + + + diff --git a/SimpleBlog/web/WEB-INF/templates/footer.xhtml b/SimpleBlog/web/WEB-INF/templates/footer.xhtml new file mode 100644 index 00000000..eb369e37 --- /dev/null +++ b/SimpleBlog/web/WEB-INF/templates/footer.xhtml @@ -0,0 +1,12 @@ + + + + diff --git a/SimpleBlog/web/WEB-INF/templates/head.xhtml b/SimpleBlog/web/WEB-INF/templates/head.xhtml new file mode 100644 index 00000000..b02f814b --- /dev/null +++ b/SimpleBlog/web/WEB-INF/templates/head.xhtml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SimpleBlog/web/WEB-INF/templates/header.xhtml b/SimpleBlog/web/WEB-INF/templates/header.xhtml new file mode 100644 index 00000000..3f477b64 --- /dev/null +++ b/SimpleBlog/web/WEB-INF/templates/header.xhtml @@ -0,0 +1,11 @@ + + + + diff --git a/SimpleBlog/web/WEB-INF/templates/template.xhtml b/SimpleBlog/web/WEB-INF/templates/template.xhtml new file mode 100644 index 00000000..c937d48b --- /dev/null +++ b/SimpleBlog/web/WEB-INF/templates/template.xhtml @@ -0,0 +1,23 @@ + + + + + + + <ui:insert name="title">SimpleBlog</ui:insert> + + + +
+ + + Content + + +
+ +
+ + diff --git a/SimpleBlog/web/WEB-INF/web.xml b/SimpleBlog/web/WEB-INF/web.xml new file mode 100644 index 00000000..c0e3fd4f --- /dev/null +++ b/SimpleBlog/web/WEB-INF/web.xml @@ -0,0 +1,24 @@ + + + + javax.faces.PROJECT_STAGE + Development + + + Faces Servlet + javax.faces.webapp.FacesServlet + 1 + + + Faces Servlet + /faces/* + + + + 30 + + + + faces/postList.xhtml + + diff --git a/SimpleBlog/web/postDetail.xhtml b/SimpleBlog/web/postDetail.xhtml new file mode 100644 index 00000000..6628380b --- /dev/null +++ b/SimpleBlog/web/postDetail.xhtml @@ -0,0 +1,76 @@ + + + + Title + + + + +
+ +
+
+ +

#{postController.postCurrent.title}

+

+
+
+ +
+
+
+

#{postController.postCurrent.content}

+
+ +

Komentar

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

    +
    +
    +

    +
  • +
    +
+ +
+
+ +
+
+ +
+ +
+ +
diff --git a/SimpleBlog/web/postList.xhtml b/SimpleBlog/web/postList.xhtml new file mode 100644 index 00000000..10579d36 --- /dev/null +++ b/SimpleBlog/web/postList.xhtml @@ -0,0 +1,60 @@ + + + + Simple Blog + + + +
+
+ +
+
+ +
+ +
\ No newline at end of file diff --git a/SimpleBlog/web/postNew.xhtml b/SimpleBlog/web/postNew.xhtml new file mode 100644 index 00000000..75fc1f86 --- /dev/null +++ b/SimpleBlog/web/postNew.xhtml @@ -0,0 +1,51 @@ + + + + New/Edit Post + + + +
+ +

+ +
+
+

Post

+ +
+ + + + + + + + + + + + + + + + + + + + + + +
+
+
+ +
+
+ +
diff --git a/SimpleBlog/web/resources/css/custom.css b/SimpleBlog/web/resources/css/custom.css new file mode 100644 index 00000000..cf6377c2 --- /dev/null +++ b/SimpleBlog/web/resources/css/custom.css @@ -0,0 +1,15 @@ +.art-title { + margin-bottom:40px +} + +.art-header { + height: 500px; +} + +.art-header-inner { + position: relative; +} + +.art { + margin-top: 0px; +} \ No newline at end of file diff --git a/assets/css/screen.css b/SimpleBlog/web/resources/css/screen.css similarity index 100% rename from assets/css/screen.css rename to SimpleBlog/web/resources/css/screen.css diff --git a/assets/img/favicon.ico b/SimpleBlog/web/resources/img/favicon.ico similarity index 100% rename from assets/img/favicon.ico rename to SimpleBlog/web/resources/img/favicon.ico diff --git a/assets/img/pre.png b/SimpleBlog/web/resources/img/pre.png similarity index 100% rename from assets/img/pre.png rename to SimpleBlog/web/resources/img/pre.png diff --git a/SimpleBlog/web/resources/js/simpleblog.js b/SimpleBlog/web/resources/js/simpleblog.js new file mode 100644 index 00000000..8a4b319b --- /dev/null +++ b/SimpleBlog/web/resources/js/simpleblog.js @@ -0,0 +1,114 @@ +function addComment(url, comment, postId) { + if (!validateComment(comment)) { + return false; + } + + var xmlhttp= window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); + + xmlhttp.onreadystatechange = function() { + if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { + var loadUrl = url.replace("new", "list") + "/" + postId; + loadComment(loadUrl); + } + } + + var parameters = "postid=" + postId + "&name=" + comment.Nama.value + "&email=" + comment.Email.value + "&content=" + comment.Komentar.value; + xmlhttp.open("POST", url, true); + xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); + xmlhttp.send(parameters); +} + +function loadComment(url) { + var xmlhttp= window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); + + xmlhttp.onreadystatechange = function() { + if (xmlhttp.readyState==4 && xmlhttp.status==200) { + document.getElementById("comments").innerHTML = xmlhttp.responseText; + document.getElementById("Nama").value = ''; + document.getElementById("Email").value = ''; + document.getElementById("Komentar").value = ''; + } + } + + xmlhttp.open("GET", url, true); + xmlhttp.send(); +} + +function validatePost() { + console.log("validatePost is called"); + judul = document.getElementById("NewPost:Judul"); + tanggal = document.getElementById("NewPost:Tanggal"); + konten = document.getElementById("NewPost:Konten"); + + if (isBlank(judul.value)) { + alert("Judul Tidak Boleh Kosong"); + return false; + } + + if (isBlank(tanggal.value)) { + alert("Tanggal Tidak Boleh Kosong"); + return false; + } + + if (isBlank(konten.value)) { + alert("Konten Tidak Boleh Kosong"); + return false; + } + + var regex = /^(\d{1,2})-(\d{1,2})-(\d{4})$/ + if (!regex.test(tanggal.value)) { + alert("Format Tanggal Harus dd-mm-yyyy"); + return false; + } + + var dd = tanggal.value.split("-")[0]; + var mm = tanggal.value.split("-")[1]; + var yy = tanggal.value.split("-")[2]; + var date = new Date(yy, mm - 1, dd); + if ((date.getDate() != dd) || (date.getMonth() + 1 != mm) || (date.getFullYear() != yy)) { + alert("Angka Yang Dimasukkan Bukanlah Tanggal"); + return false; + } + + var today = new Date(); + if (today > date) { + alert("Tanggal Harus Lebih Dari Hari Ini"); + return false; + } + + return true; +} + +function validateComment() { + console.log("validateComment is called"); + nama = document.getElementById("NewComment:Nama"); + tanggal = document.getElementById("NewComment:Email"); + konten = document.getElementById("NewComment:Komentar"); + + if (isBlank(nama.value)) { + alert("Nama Tidak Boleh Kosong"); + return false; + } + + if (isBlank(email.value)) { + alert("Nama Tidak Boleh Kosong"); + return false; + } + + if (isBlank(komentar.value)) { + alert("Komentar Tidak Boleh Kosong"); + return false; + } + + var regex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; + if (!regex.test(email.value)) { + alert("Email Yang Dimasukkan Tidak Valid"); + return false; + } + + return true; +} + +function isBlank(str) { + return (!str || 0 === str.length || /^\s*$/.test(str)); +} \ No newline at end of file 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/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