From dd8a53ced39101e11a2260eaf2996041f33d0bca Mon Sep 17 00:00:00 2001
From: Kaveri1010 <49593343+Kaveri1010@users.noreply.github.com>
Date: Mon, 15 Apr 2019 00:21:11 +0100
Subject: [PATCH] Added my files
---
blog-web/pom.xml | 10 +++
.../pierceecom/blog/JAXRSConfiguration.java | 14 +++
.../main/java/com/pierceecom/dao/PostDAO.java | 86 +++++++++++++++++++
.../pierceecom/dao/SessionFactoryHelper.java | 40 +++++++++
.../DataNotFoundExceptionMapper.java | 22 +++++
.../GenericExceptionMapper.java | 22 +++++
.../IllegalArgumentExceptionMapper.java | 27 ++++++
.../NotAllowedExceptionMapper.java | 25 ++++++
.../OptimisticLockExceptionMapper.java | 21 +++++
.../exceptions/DataNotFoundException.java | 14 +++
.../com/pierceecom/model/ErrorMessage.java | 46 ++++++++++
.../main/java/com/pierceecom/model/Post.java | 51 +++++++++++
.../pierceecom/resource/BlogPostResource.java | 68 +++++++++++++++
.../com/pierceecom/service/PostService.java | 66 ++++++++++++++
14 files changed, 512 insertions(+)
create mode 100644 blog-web/src/main/java/com/pierceecom/dao/PostDAO.java
create mode 100644 blog-web/src/main/java/com/pierceecom/dao/SessionFactoryHelper.java
create mode 100644 blog-web/src/main/java/com/pierceecom/exceptionmappers/DataNotFoundExceptionMapper.java
create mode 100644 blog-web/src/main/java/com/pierceecom/exceptionmappers/GenericExceptionMapper.java
create mode 100644 blog-web/src/main/java/com/pierceecom/exceptionmappers/IllegalArgumentExceptionMapper.java
create mode 100644 blog-web/src/main/java/com/pierceecom/exceptionmappers/NotAllowedExceptionMapper.java
create mode 100644 blog-web/src/main/java/com/pierceecom/exceptionmappers/OptimisticLockExceptionMapper.java
create mode 100644 blog-web/src/main/java/com/pierceecom/exceptions/DataNotFoundException.java
create mode 100644 blog-web/src/main/java/com/pierceecom/model/ErrorMessage.java
create mode 100644 blog-web/src/main/java/com/pierceecom/model/Post.java
create mode 100644 blog-web/src/main/java/com/pierceecom/resource/BlogPostResource.java
create mode 100644 blog-web/src/main/java/com/pierceecom/service/PostService.java
diff --git a/blog-web/pom.xml b/blog-web/pom.xml
index 8ec9481..ddff25f 100644
--- a/blog-web/pom.xml
+++ b/blog-web/pom.xml
@@ -35,6 +35,16 @@
2.17
test
+
+ org.hibernate
+ hibernate-core
+ 5.4.2.Final
+
+
+ com.oracle
+ ojdbc6
+ 12.6
+
diff --git a/blog-web/src/main/java/com/pierceecom/blog/JAXRSConfiguration.java b/blog-web/src/main/java/com/pierceecom/blog/JAXRSConfiguration.java
index 96a0744..a421fb5 100644
--- a/blog-web/src/main/java/com/pierceecom/blog/JAXRSConfiguration.java
+++ b/blog-web/src/main/java/com/pierceecom/blog/JAXRSConfiguration.java
@@ -2,15 +2,29 @@
import java.util.HashSet;
import java.util.Set;
+
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
+
+
@ApplicationPath("/")
public class JAXRSConfiguration extends Application {
@Override
public Set> getClasses() {
HashSet> classes = new HashSet<>();
classes.add(HelloPierceResource.class);
+ classes.add(com.pierceecom.resource.BlogPostResource.class);
+ classes.add(com.pierceecom.exceptionmappers.IllegalArgumentExceptionMapper.class);
+ classes.add(com.pierceecom.exceptionmappers.DataNotFoundExceptionMapper.class);
+ classes.add(com.pierceecom.exceptionmappers.OptimisticLockExceptionMapper.class);
+ classes.add(com.pierceecom.exceptionmappers.NotAllowedExceptionMapper.class);
+ classes.add(com.pierceecom.exceptionmappers.GenericExceptionMapper.class);
+
+
+
+
+
return classes;
}
}
diff --git a/blog-web/src/main/java/com/pierceecom/dao/PostDAO.java b/blog-web/src/main/java/com/pierceecom/dao/PostDAO.java
new file mode 100644
index 0000000..5f0c0f6
--- /dev/null
+++ b/blog-web/src/main/java/com/pierceecom/dao/PostDAO.java
@@ -0,0 +1,86 @@
+package com.pierceecom.dao;
+import java.util.List;
+
+
+
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import com.pierceecom.model.Post;
+
+public class PostDAO {
+
+ private Session currentSession;
+
+ private Transaction currentTransaction;
+ public PostDAO() {
+
+ }
+
+
+ public Session openCurrentSession() {
+ currentSession = SessionFactoryHelper.getSessionFactory().openSession();
+ return currentSession;
+ }
+
+ public Session openCurrentSessionwithTransaction() {
+ currentSession = SessionFactoryHelper.getSessionFactory().openSession();
+ currentTransaction = currentSession.beginTransaction();
+ return currentSession;
+ }
+
+ public void closeCurrentSession() {
+ currentSession.close();
+ }
+
+ public void closeCurrentSessionwithTransaction() {
+ currentTransaction.commit();
+ currentSession.close();
+ }
+
+
+ public Session getCurrentSession() {
+ return currentSession;
+ }
+
+ public void setCurrentSession(Session currentSession) {
+ this.currentSession = currentSession;
+ }
+
+ public Transaction getCurrentTransaction() {
+ return currentTransaction;
+ }
+
+ public void setCurrentTransaction(Transaction currentTransaction) {
+ this.currentTransaction = currentTransaction;
+ }
+
+ public void persist(Post entity) {
+ getCurrentSession().save(entity);
+ }
+
+ public void update(Post entity) {
+ getCurrentSession().update(entity);
+ }
+
+ public Post findById(String id) {
+ Post post = (Post) getCurrentSession().get(Post.class, id);
+ return post;
+ }
+
+ public void delete(Post entity) {
+ getCurrentSession().delete(entity);
+ }
+
+ @SuppressWarnings("unchecked")
+ public List findAll() {
+ List posts = (List) getCurrentSession().createQuery("from Post").list();
+ return posts;
+ }
+
+ public void deleteAll() {
+ List entityList = findAll();
+ for (Post entity : entityList) {
+ delete(entity);
+ }
+ }
+}
diff --git a/blog-web/src/main/java/com/pierceecom/dao/SessionFactoryHelper.java b/blog-web/src/main/java/com/pierceecom/dao/SessionFactoryHelper.java
new file mode 100644
index 0000000..ccb33e4
--- /dev/null
+++ b/blog-web/src/main/java/com/pierceecom/dao/SessionFactoryHelper.java
@@ -0,0 +1,40 @@
+package com.pierceecom.dao;
+
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.hibernate.cfg.Configuration;
+
+public class SessionFactoryHelper {
+ private static final SessionFactory sessionFactory;
+
+ static {
+ try {
+ // Build a SessionFactory object from session-factory config
+ // defined in the hibernate.cfg.xml file. In this file we
+ // register the JDBC connection information, connection pool,
+ // the hibernate dialect that we used and the mapping to our
+ // hbm.xml file for each pojo (plain old java object).
+ Configuration config = new Configuration();
+ sessionFactory = config.configure().buildSessionFactory();
+ } catch (Throwable e) {
+ System.err.println("Error in creating SessionFactory object."
+ + e.getMessage());
+ throw new ExceptionInInitializerError(e);
+ }
+ }
+
+ public static void main(String[] args) {
+ Session session = SessionFactoryHelper.getSessionFactory()
+ .getCurrentSession();
+
+ System.out.println("session = " + session);
+ }
+
+ /**
+ * A static method for other application to get SessionFactory object
+ * initialized in this helper class.
+ */
+ public static SessionFactory getSessionFactory() {
+ return sessionFactory;
+ }
+}
\ No newline at end of file
diff --git a/blog-web/src/main/java/com/pierceecom/exceptionmappers/DataNotFoundExceptionMapper.java b/blog-web/src/main/java/com/pierceecom/exceptionmappers/DataNotFoundExceptionMapper.java
new file mode 100644
index 0000000..360f99c
--- /dev/null
+++ b/blog-web/src/main/java/com/pierceecom/exceptionmappers/DataNotFoundExceptionMapper.java
@@ -0,0 +1,22 @@
+package com.pierceecom.exceptionmappers;
+
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+
+import com.pierceecom.exceptions.DataNotFoundException;
+import com.pierceecom.model.ErrorMessage;
+
+@Provider
+public class DataNotFoundExceptionMapper implements ExceptionMapper {
+ @Override
+ public Response toResponse(DataNotFoundException ex){
+
+ ErrorMessage errorMessage =new ErrorMessage(ex.getMessage(),204,"");
+ return Response.status(Status.NOT_FOUND).entity(errorMessage).build();
+
+ }
+
+
+}
diff --git a/blog-web/src/main/java/com/pierceecom/exceptionmappers/GenericExceptionMapper.java b/blog-web/src/main/java/com/pierceecom/exceptionmappers/GenericExceptionMapper.java
new file mode 100644
index 0000000..8337a2e
--- /dev/null
+++ b/blog-web/src/main/java/com/pierceecom/exceptionmappers/GenericExceptionMapper.java
@@ -0,0 +1,22 @@
+package com.pierceecom.exceptionmappers;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+
+import com.pierceecom.model.ErrorMessage;
+
+@Provider
+public class GenericExceptionMapper implements ExceptionMapper {
+ @Override
+ public Response toResponse(Throwable ex){
+
+ ErrorMessage errorMessage =new ErrorMessage("Internal Server Error", 500, "#/definitions/Post");
+ return Response.status(Status.INTERNAL_SERVER_ERROR).entity(errorMessage).type(MediaType.APPLICATION_JSON).build();
+
+}
+
+}
+
diff --git a/blog-web/src/main/java/com/pierceecom/exceptionmappers/IllegalArgumentExceptionMapper.java b/blog-web/src/main/java/com/pierceecom/exceptionmappers/IllegalArgumentExceptionMapper.java
new file mode 100644
index 0000000..77be92d
--- /dev/null
+++ b/blog-web/src/main/java/com/pierceecom/exceptionmappers/IllegalArgumentExceptionMapper.java
@@ -0,0 +1,27 @@
+package com.pierceecom.exceptionmappers;
+
+import java.lang.IllegalArgumentException;
+import java.lang.Override;
+
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+
+
+
+import com.pierceecom.model.ErrorMessage;
+
+
+@Provider
+public class IllegalArgumentExceptionMapper implements ExceptionMapper {
+ @Override
+ public Response toResponse(IllegalArgumentException ex){
+
+ ErrorMessage errorMessage =new ErrorMessage("Post not found", 404, "");
+ return Response.status(Status.NOT_FOUND).entity(errorMessage).build();
+
+ }
+
+
+}
diff --git a/blog-web/src/main/java/com/pierceecom/exceptionmappers/NotAllowedExceptionMapper.java b/blog-web/src/main/java/com/pierceecom/exceptionmappers/NotAllowedExceptionMapper.java
new file mode 100644
index 0000000..5963385
--- /dev/null
+++ b/blog-web/src/main/java/com/pierceecom/exceptionmappers/NotAllowedExceptionMapper.java
@@ -0,0 +1,25 @@
+package com.pierceecom.exceptionmappers;
+
+
+import javax.ws.rs.NotAllowedException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+
+import com.pierceecom.model.ErrorMessage;
+
+@Provider
+
+public class NotAllowedExceptionMapper implements ExceptionMapper {
+ @Override
+ public Response toResponse(NotAllowedException ex){
+
+ ErrorMessage errorMessage =new ErrorMessage("Invalid input", 404, "#/definitions/Post");
+ return Response.status(Status.METHOD_NOT_ALLOWED).entity(errorMessage).type(MediaType.APPLICATION_JSON).build();
+
+ }
+
+}
+
diff --git a/blog-web/src/main/java/com/pierceecom/exceptionmappers/OptimisticLockExceptionMapper.java b/blog-web/src/main/java/com/pierceecom/exceptionmappers/OptimisticLockExceptionMapper.java
new file mode 100644
index 0000000..4d29fae
--- /dev/null
+++ b/blog-web/src/main/java/com/pierceecom/exceptionmappers/OptimisticLockExceptionMapper.java
@@ -0,0 +1,21 @@
+package com.pierceecom.exceptionmappers;
+
+import javax.persistence.OptimisticLockException;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+
+import com.pierceecom.model.ErrorMessage;
+
+@Provider
+public class OptimisticLockExceptionMapper implements ExceptionMapper {
+ @Override
+ public Response toResponse(OptimisticLockException ex){
+
+ ErrorMessage errorMessage =new ErrorMessage("Post not found", 404, "");
+ return Response.status(Status.NOT_FOUND).entity(errorMessage).build();
+
+ }
+
+}
diff --git a/blog-web/src/main/java/com/pierceecom/exceptions/DataNotFoundException.java b/blog-web/src/main/java/com/pierceecom/exceptions/DataNotFoundException.java
new file mode 100644
index 0000000..db69743
--- /dev/null
+++ b/blog-web/src/main/java/com/pierceecom/exceptions/DataNotFoundException.java
@@ -0,0 +1,14 @@
+package com.pierceecom.exceptions;
+
+public class DataNotFoundException extends RuntimeException {
+
+ private static final long serialVersionUID = 4645536692857767394L;
+
+
+public DataNotFoundException(String message){
+ super(message);
+
+}
+
+}
+
\ No newline at end of file
diff --git a/blog-web/src/main/java/com/pierceecom/model/ErrorMessage.java b/blog-web/src/main/java/com/pierceecom/model/ErrorMessage.java
new file mode 100644
index 0000000..93e45f5
--- /dev/null
+++ b/blog-web/src/main/java/com/pierceecom/model/ErrorMessage.java
@@ -0,0 +1,46 @@
+package com.pierceecom.model;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement
+public class ErrorMessage {
+ private String errorMessage;
+ private int errorCode;
+ private String documentation;
+
+ public ErrorMessage() {
+ }
+
+ public ErrorMessage(String errorMessage, int errorCode, String documentation) {
+ super();
+ this.errorMessage = errorMessage;
+ this.errorCode = errorCode;
+ this.documentation = documentation;
+ }
+
+ public String getErrorMessage() {
+ return errorMessage;
+ }
+
+ public void setErrorMessage(String errorMessage) {
+ this.errorMessage = errorMessage;
+ }
+
+ public int getErrorCode() {
+ return errorCode;
+ }
+
+ public void setErrorCode(int errorCode) {
+ this.errorCode = errorCode;
+ }
+
+ public String getDocumentation() {
+ return documentation;
+ }
+
+ public void setDocumentation(String documentation) {
+ this.documentation = documentation;
+ }
+
+}
+
diff --git a/blog-web/src/main/java/com/pierceecom/model/Post.java b/blog-web/src/main/java/com/pierceecom/model/Post.java
new file mode 100644
index 0000000..92d1333
--- /dev/null
+++ b/blog-web/src/main/java/com/pierceecom/model/Post.java
@@ -0,0 +1,51 @@
+package com.pierceecom.model;
+
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.hibernate.annotations.GenericGenerator;
+
+
+@XmlRootElement
+@Entity
+public class Post {
+ @Id
+ @GeneratedValue(generator = "uuid")
+ @GenericGenerator(name = "uuid", strategy = "uuid2")
+ private String id;
+ private String title;
+ private String content;
+
+ Post(){
+
+ }
+
+ public Post(String id, String title, String content) {
+ this.id = id;
+ this.title = title;
+ this.content = content;
+ }
+ public String getId() {
+ return id;
+ }
+ public void setId(String id) {
+ this.id = id;
+ }
+ public String getTitle() {
+ return title;
+ }
+ public void setTitle(String title) {
+ this.title = title;
+ }
+ public String getContent() {
+ return content;
+ }
+ public void setContent(String content) {
+ this.content = content;
+ }
+
+
+}
diff --git a/blog-web/src/main/java/com/pierceecom/resource/BlogPostResource.java b/blog-web/src/main/java/com/pierceecom/resource/BlogPostResource.java
new file mode 100644
index 0000000..cac9773
--- /dev/null
+++ b/blog-web/src/main/java/com/pierceecom/resource/BlogPostResource.java
@@ -0,0 +1,68 @@
+package com.pierceecom.resource;
+
+import java.util.List;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+
+
+import javax.ws.rs.core.Response;
+
+import com.pierceecom.model.Post;
+import com.pierceecom.service.PostService;
+
+
+@Path("posts")
+public class BlogPostResource {
+ PostService postService = new PostService();
+ @GET
+ @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
+ public List getPosts() {
+ return postService.getAllPosts();
+ }
+ @GET
+ @Path("/{postId}")
+ @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
+ public Post getPost(@PathParam("postId") String postId){
+ return postService.getPostById(postId);
+ }
+
+ @POST
+ @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
+ @Consumes({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
+ public Response addPost(Post p){
+ return Response
+ .status(Response.Status.CREATED)
+ .entity(postService.addPost(p))
+ .build();
+ }
+
+ @PUT
+ @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
+ @Consumes({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
+ public Response updatePost(Post p){
+ return Response
+ .status(Response.Status.CREATED)
+ .entity(postService.putPost(p))
+ .build();
+ }
+
+ @DELETE
+ @Path("/{postId}")
+ @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
+ public Response deletepost(@PathParam("postId") String postId){
+ System.out.println(postId);
+ postService.deletePost(postId);
+ return Response
+ .status(Response.Status.OK)
+ .build();
+ }
+}
\ No newline at end of file
diff --git a/blog-web/src/main/java/com/pierceecom/service/PostService.java b/blog-web/src/main/java/com/pierceecom/service/PostService.java
new file mode 100644
index 0000000..949bcdf
--- /dev/null
+++ b/blog-web/src/main/java/com/pierceecom/service/PostService.java
@@ -0,0 +1,66 @@
+package com.pierceecom.service;
+
+import java.util.List;
+
+import com.pierceecom.exceptions.DataNotFoundException;
+import com.pierceecom.dao.PostDAO;
+
+import com.pierceecom.model.Post;
+
+public class PostService {
+
+ private static PostDAO postDAO;
+
+ public PostService() {
+ postDAO = new PostDAO();
+ }
+
+ public PostDAO postDAO() {
+ return postDAO;
+ }
+
+ public Post addPost(Post entity) {
+ postDAO.openCurrentSessionwithTransaction();
+ postDAO.persist(entity);
+ postDAO.closeCurrentSessionwithTransaction();
+ return (getPostById(entity.getId()));
+ }
+
+ public Post putPost(Post entity) {
+ postDAO.openCurrentSessionwithTransaction();
+ postDAO.update(entity);
+ postDAO.closeCurrentSessionwithTransaction();
+ return (getPostById(entity.getId()));
+ }
+
+ public Post getPostById(String id) {
+ postDAO.openCurrentSession();
+ Post post = postDAO.findById(id);
+ postDAO.closeCurrentSession();
+ if (post == null) {
+ throw new DataNotFoundException("No content");
+ }
+ return post;
+ }
+
+ public void deletePost(String id) {
+ postDAO.openCurrentSessionwithTransaction();
+ Post post = postDAO.findById(id);
+ postDAO.delete(post);
+ postDAO.closeCurrentSessionwithTransaction();
+ }
+
+ public List getAllPosts() {
+ postDAO.openCurrentSession();
+ List posts = postDAO.findAll();
+ postDAO.closeCurrentSession();
+ return posts;
+ }
+
+ public void deleteAllPosts() {
+ postDAO.openCurrentSessionwithTransaction();
+ postDAO.deleteAll();
+ postDAO.closeCurrentSessionwithTransaction();
+ }
+
+}