From 97c5bdab3d8c8ae274c7433b22a0d788d81bcbdd Mon Sep 17 00:00:00 2001 From: Jonathan Ignacio Date: Fri, 24 Mar 2017 09:52:47 -0300 Subject: [PATCH 01/12] WIP - starting the refactoring --- .../com/piccritic/database/JPAConnector.java | 27 ++++- .../database/feedback/JPAVoteConnector.java | 55 +++------ .../database/license/JPALicenseConnector.java | 25 ++-- .../database/post/AlbumConnector.java | 39 +++++++ .../database/post/JPAAlbumConnector.java | 75 ++++++++++++ .../database/post/JPAPostConnector.java | 108 +++--------------- .../com/piccritic/database/post/Post.java | 2 +- .../database/post/PostConnector.java | 39 +------ 8 files changed, 185 insertions(+), 185 deletions(-) create mode 100644 database/src/main/java/com/piccritic/database/post/AlbumConnector.java create mode 100644 database/src/main/java/com/piccritic/database/post/JPAAlbumConnector.java diff --git a/database/src/main/java/com/piccritic/database/JPAConnector.java b/database/src/main/java/com/piccritic/database/JPAConnector.java index 8723509..334e384 100644 --- a/database/src/main/java/com/piccritic/database/JPAConnector.java +++ b/database/src/main/java/com/piccritic/database/JPAConnector.java @@ -2,19 +2,40 @@ import java.util.HashMap; import java.util.Map; +import java.util.Set; import javax.persistence.EntityManager; import javax.persistence.Persistence; +import javax.validation.ConstraintViolation; +import javax.validation.Validation; -public abstract class JPAConnector { +import com.piccritic.database.feedback.Vote; +import com.piccritic.database.feedback.VoteException; +import com.vaadin.addon.jpacontainer.JPAContainer; +import com.vaadin.addon.jpacontainer.JPAContainerFactory; + +public abstract class JPAConnector { protected EntityManager entity; + protected JPAContainer container; - public JPAConnector() { + public JPAConnector(Class c) { + container = JPAContainerFactory.make(c, entity); Map configOverrides = new HashMap(); configOverrides.put("hibernate.connection.url", System.getenv("JDBC_DATABASE_URL")); entity = Persistence.createEntityManagerFactory("postgres", configOverrides).createEntityManager(); } - + + /** + * Ensures that object follows constraints correctly. + * @param t the object to be checked + * @throws Exception + */ + protected void validate(T t) throws Exception { + Set> violations = Validation.buildDefaultValidatorFactory().getValidator().validate(t); + for (ConstraintViolation violation : violations) { + throw new Exception(violation.getMessage()); + } + } } diff --git a/database/src/main/java/com/piccritic/database/feedback/JPAVoteConnector.java b/database/src/main/java/com/piccritic/database/feedback/JPAVoteConnector.java index 5308ad1..b30b921 100644 --- a/database/src/main/java/com/piccritic/database/feedback/JPAVoteConnector.java +++ b/database/src/main/java/com/piccritic/database/feedback/JPAVoteConnector.java @@ -5,45 +5,30 @@ package com.piccritic.database.feedback; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.Set; -import javax.persistence.EntityManager; -import javax.persistence.Persistence; import javax.persistence.TypedQuery; -import javax.validation.ConstraintViolation; -import javax.validation.Validation; +import com.piccritic.database.JPAConnector; import com.piccritic.database.user.Critic; import com.vaadin.addon.jpacontainer.EntityItem; -import com.vaadin.addon.jpacontainer.JPAContainer; -import com.vaadin.addon.jpacontainer.JPAContainerFactory; /** * This class implements {@link VoteConnector} using Vaadin JPAContainers. * * @author Jonathan Ignacio and Frank Bosse */ -public class JPAVoteConnector implements VoteConnector{ - - private JPAContainer votes; +public class JPAVoteConnector extends JPAConnector implements VoteConnector{ public JPAVoteConnector(){ - Map configOverrides = new HashMap(); - configOverrides.put("hibernate.connection.url", System.getenv("JDBC_DATABASE_URL")); - - EntityManager entity = Persistence.createEntityManagerFactory("postgres", configOverrides).createEntityManager(); - - votes = JPAContainerFactory.make(Vote.class, entity); + super(Vote.class); } /* (non-Javadoc) * @see com.piccritic.database.feedback.VoteConnector#selectVote(java.lang.Long) */ public Vote selectVote(Long id) { - EntityItem voteItem = votes.getItem(id); + EntityItem voteItem = container.getItem(id); return (voteItem != null) ? voteItem.getEntity() : null; } @@ -55,7 +40,7 @@ public Long getVoteId(Critic critic, Comment comment) { return null; } String query = "SELECT v from Vote v WHERE v.critic = :cr AND v.comment = :co"; - TypedQuery v = votes.getEntityProvider().getEntityManager().createQuery(query, Vote.class) + TypedQuery v = container.getEntityProvider().getEntityManager().createQuery(query, Vote.class) .setParameter("cr", critic) .setParameter("co", comment); List votes = v.getResultList(); @@ -74,9 +59,9 @@ public Vote insertVote(Vote vote) throws VoteException { validate(vote); vote.setId(null); - vote.setId((Long) votes.addEntity(vote)); + vote.setId((Long) container.addEntity(vote)); - EntityItem voteItem = votes.getItem(vote.getId()); + EntityItem voteItem = container.getItem(vote.getId()); boolean rating = (boolean) voteItem.getItemProperty("rating").getValue(); Comment change = (Comment) voteItem.getItemProperty("comment").getValue(); if(rating){ @@ -98,7 +83,7 @@ public Vote insertVote(Vote vote) throws VoteException { @SuppressWarnings("unchecked") public Vote updateVote(Vote vote) throws VoteException { validate(vote); - EntityItem voteItem = votes.getItem(vote.getId()); + EntityItem voteItem = container.getItem(vote.getId()); voteItem.getItemProperty("rating").setValue(vote.getRating()); @@ -123,7 +108,7 @@ public Vote updateVote(Vote vote) throws VoteException { @SuppressWarnings("unchecked") public boolean deleteVote(Vote vote) throws VoteException { validate(vote); - EntityItem voteItem = votes.getItem(vote.getId()); + EntityItem voteItem = container.getItem(vote.getId()); voteItem.getItemProperty("rating").setValue(vote.getRating()); @@ -139,26 +124,22 @@ public boolean deleteVote(Vote vote) throws VoteException { } voteItem.commit(); - votes.removeItem(vote.getId()); - return !votes.containsId(vote.getId()); + container.removeItem(vote.getId()); + return !container.containsId(vote.getId()); } - /** - * Ensures that vote follows constraints correctly. - * @param vote the vote object to be checked - * @throws VoteException - */ - private void validate(Vote vote) throws VoteException { - Set> violations = Validation.buildDefaultValidatorFactory().getValidator().validate(vote); - for (ConstraintViolation violation : violations) { - throw new VoteException(violation.getMessage()); + protected void validate(Vote vote) throws VoteException { + try{ + super.validate(vote); + } catch(Exception e) { + throw new VoteException(e.getMessage()); } } @Override public int getScore(Comment comment) { String query = "SELECT v FROM Vote v WHERE v.comment = :comment"; - TypedQuery q = votes.getEntityProvider().getEntityManager().createQuery(query, Vote.class) + TypedQuery q = container.getEntityProvider().getEntityManager().createQuery(query, Vote.class) .setParameter("comment", comment); List voteList = q.getResultList(); @@ -176,7 +157,7 @@ public int getScore(Comment comment) { @Override public List getVotes(Comment comment) { String query = "SELECT v FROM Vote v WHERE v.comment = :comment"; - TypedQuery q = votes.getEntityProvider().getEntityManager().createQuery(query, Vote.class) + TypedQuery q = container.getEntityProvider().getEntityManager().createQuery(query, Vote.class) .setParameter("comment", comment); return q.getResultList(); } diff --git a/database/src/main/java/com/piccritic/database/license/JPALicenseConnector.java b/database/src/main/java/com/piccritic/database/license/JPALicenseConnector.java index 09926c0..cdd3908 100644 --- a/database/src/main/java/com/piccritic/database/license/JPALicenseConnector.java +++ b/database/src/main/java/com/piccritic/database/license/JPALicenseConnector.java @@ -5,42 +5,39 @@ import com.piccritic.database.JPAConnector; import com.vaadin.addon.jpacontainer.EntityItem; -import com.vaadin.addon.jpacontainer.JPAContainer; -import com.vaadin.addon.jpacontainer.JPAContainerFactory; -public class JPALicenseConnector extends JPAConnector implements LicenseConnector { +public class JPALicenseConnector extends JPAConnector implements LicenseConnector { - private static JPAContainer licenses; public JPALicenseConnector() { - licenses = JPAContainerFactory.make(License.class, entity); + super(License.class); if (!exists(License.ATTRIBUTION)) { - licenses.addEntity(new AttributionLicense()); + container.addEntity(new AttributionLicense()); } if (!exists(License.ATTRIBUTION_SHAREALIKE)) { - licenses.addEntity(new AttributionShareAlikeLicense()); + container.addEntity(new AttributionShareAlikeLicense()); } if (!exists(License.ATTRIBUTION_NO_DERIVS)) { - licenses.addEntity(new AttributionNoDerivsLicense()); + container.addEntity(new AttributionNoDerivsLicense()); } if (!exists(License.ATTRIBUTION_NON_COMMERCIAL)) { - licenses.addEntity(new AttributionNonCommercialLicense()); + container.addEntity(new AttributionNonCommercialLicense()); } if (!exists(License.ATTRIBUTION_NON_COMMERCIAL_SHAREALIKE)) { - licenses.addEntity(new AttributionNonComShareLicense()); + container.addEntity(new AttributionNonComShareLicense()); } if (!exists(License.ATTRIBUTION_NON_COMMERCIAL_NO_DERIVS)) { - licenses.addEntity(new AttributionNonComNDerivsLicense()); + container.addEntity(new AttributionNonComNDerivsLicense()); } } - private static boolean exists(String licenseType) { - EntityItem licenseItem = licenses.getItem(licenseType); + private boolean exists(String licenseType) { + EntityItem licenseItem = container.getItem(licenseType); return licenseItem != null; } public License selectLicense(String licenseType) { - EntityItem licenseItem = licenses.getItem(licenseType); + EntityItem licenseItem = container.getItem(licenseType); return (licenseItem != null) ? licenseItem.getEntity() : null; } } diff --git a/database/src/main/java/com/piccritic/database/post/AlbumConnector.java b/database/src/main/java/com/piccritic/database/post/AlbumConnector.java new file mode 100644 index 0000000..c9d1a47 --- /dev/null +++ b/database/src/main/java/com/piccritic/database/post/AlbumConnector.java @@ -0,0 +1,39 @@ +package com.piccritic.database.post; + +public interface AlbumConnector { + + /** + * Inserts the specified Album into the database. + * + * @param album the album to insert + * @return the same album, with a generated ID + * @throws AlbumException if the album validation fails + */ + public Album insertAlbum(Album album) throws AlbumException; + + /** + * Updates the specified Album in the database. + * + * @param album the album to update + * @return the same album + * @throws AlbumException if the album validation fails + */ + public Album updateAlbum(Album album) throws AlbumException; + + /** + * Gets the Album with the matching ID from the database. + * + * @param id the id to search for + * @return the album with the given ID, or null if it doesn't exist + */ + public Album selectAlbum(Long id); + + /** + * Deletes the specified Album from the database. + * + * @param album the album to delete + * @return true on successful delete + * @throws AlbumException if the album validation fails + */ + public boolean deleteAlbum(Album album) throws AlbumException; +} diff --git a/database/src/main/java/com/piccritic/database/post/JPAAlbumConnector.java b/database/src/main/java/com/piccritic/database/post/JPAAlbumConnector.java new file mode 100644 index 0000000..705b502 --- /dev/null +++ b/database/src/main/java/com/piccritic/database/post/JPAAlbumConnector.java @@ -0,0 +1,75 @@ +package com.piccritic.database.post; + +import com.piccritic.database.JPAConnector; +import com.vaadin.addon.jpacontainer.EntityItem; + +public class JPAAlbumConnector extends JPAConnector implements AlbumConnector{ + + /** + * Initializes the JPAContainers for this PostConnector. + */ + public JPAAlbumConnector() { + super(Album.class); + } + + /* (non-Javadoc) + * @see com.piccritic.database.post.PostConnector#insertAlbum(com.piccritic.database.post.Album) + */ + public Album insertAlbum(Album album) throws AlbumException { + validate(album); + album.setId(null); + album.setId((Long) container.addEntity(album)); + return selectAlbum(album.getId()); + } + + /* (non-Javadoc) + * @see com.piccritic.database.post.PostConnector#updateAlbum(com.piccritic.database.post.Album) + */ + @SuppressWarnings("unchecked") + public Album updateAlbum(Album album) throws AlbumException { + EntityItem albumItem = container.getItem(album.getId()); + + validate(album); + albumItem.getItemProperty("name").setValue(album.getName()); + albumItem.commit(); + + return selectAlbum(album.getId()); + } + + /* (non-Javadoc) + * @see com.piccritic.database.post.PostConnector#selectAlbum(Long) + */ + public Album selectAlbum(Long id) { + EntityItem albumItem = container.getItem(id); + return (albumItem != null) ? albumItem.getEntity() : null; + } + + /* (non-Javadoc) + * @see com.piccritic.database.post.PostConnector#deleteAlbum(com.piccritic.database.post.Album) + */ + public boolean deleteAlbum(Album album) throws AlbumException { + validate(album); + if (album == null) { + throw new AlbumException("Cannot delete null album"); + } + container.removeItem(album.getId()); + return !container.containsId(album.getId()); + + } + + /** + * Validates the fields and throws exceptions when the fields + * do not currently abide by the rules defined in the album class + * + * @param album + * @throws AlbumException Message for the UI portion of the code. + */ + protected void validate(Album album) throws AlbumException { + try{ + super.validate(album); + } catch (Exception e) { + throw new AlbumException(e.getMessage()); + } + } + +} \ No newline at end of file diff --git a/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java b/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java index 740759a..eb56e66 100644 --- a/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java +++ b/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java @@ -5,26 +5,13 @@ package com.piccritic.database.post; import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; import java.util.List; -import java.util.Map; -import java.util.Set; -import javax.persistence.EntityManager; -import javax.persistence.Persistence; -import javax.persistence.Query; import javax.persistence.TypedQuery; -import javax.validation.ConstraintViolation; -import javax.validation.Validation; -import com.piccritic.database.feedback.Comment; -import com.piccritic.database.feedback.Rating; -import com.piccritic.database.user.Critic; import com.piccritic.database.JPAConnector; +import com.piccritic.database.user.Critic; import com.vaadin.addon.jpacontainer.EntityItem; -import com.vaadin.addon.jpacontainer.JPAContainer; -import com.vaadin.addon.jpacontainer.JPAContainerFactory; /** * This class implements {@link PostConnector} using @@ -32,62 +19,13 @@ * * @author Ryan Lowe
Jonathan Ignacio
Damien Robichaud */ -public class JPAPostConnector extends JPAConnector implements PostConnector { - - private JPAContainer albums; - private JPAContainer posts; +public class JPAPostConnector extends JPAConnector implements PostConnector { /** * Initializes the JPAContainers for this PostConnector. */ public JPAPostConnector() { - albums = JPAContainerFactory.make(Album.class, entity); - posts = JPAContainerFactory.make(Post.class, entity); - } - - /* (non-Javadoc) - * @see com.piccritic.database.post.PostConnector#insertAlbum(com.piccritic.database.post.Album) - */ - public Album insertAlbum(Album album) throws AlbumException { - validate(album); - album.setId(null); - album.setId((Long) albums.addEntity(album)); - return selectAlbum(album.getId()); - } - - /* (non-Javadoc) - * @see com.piccritic.database.post.PostConnector#updateAlbum(com.piccritic.database.post.Album) - */ - @SuppressWarnings("unchecked") - public Album updateAlbum(Album album) throws AlbumException { - EntityItem albumItem = albums.getItem(album.getId()); - - validate(album); - albumItem.getItemProperty("name").setValue(album.getName()); - albumItem.commit(); - - return selectAlbum(album.getId()); - } - - /* (non-Javadoc) - * @see com.piccritic.database.post.PostConnector#selectAlbum(Long) - */ - public Album selectAlbum(Long id) { - EntityItem albumItem = albums.getItem(id); - return (albumItem != null) ? albumItem.getEntity() : null; - } - - /* (non-Javadoc) - * @see com.piccritic.database.post.PostConnector#deleteAlbum(com.piccritic.database.post.Album) - */ - public boolean deleteAlbum(Album album) throws AlbumException { - validate(album); - if (album == null) { - throw new AlbumException("Cannot delete null album"); - } - albums.removeItem(album.getId()); - return !albums.containsId(album.getId()); - + super(Post.class); } /* (non-Javadoc) @@ -95,7 +33,7 @@ public boolean deleteAlbum(Album album) throws AlbumException { */ public Post insertPost(Post post) throws PostException { validate(post); - posts.addEntity(post); + container.addEntity(post); return selectPost(post.getPath()); } @@ -105,7 +43,7 @@ public Post insertPost(Post post) throws PostException { @SuppressWarnings("unchecked") public Post updatePost(Post post) throws PostException { validate(post); - EntityItem postItem = posts.getItem(post.getPath()); + EntityItem postItem = container.getItem(post.getPath()); if (postItem == null) { throw new PostException("Cannot update null post"); } @@ -120,7 +58,7 @@ public Post updatePost(Post post) throws PostException { * @see com.piccritic.database.post.PostConnector#selectPost(Long) */ public Post selectPost(String path) { - EntityItem postItem = posts.getItem(path); + EntityItem postItem = container.getItem(path); return (postItem != null) ? postItem.getEntity() : null; } @@ -133,22 +71,8 @@ public boolean deletePost(Post post) throws PostException { throw new PostException("Cannot delete null post"); } post.getAlbum().getPosts().remove(post); - posts.removeItem(post.getPath()); - return !posts.containsId(post.getPath()); - } - - /** - * Validates the fields and throws exceptions when the fields - * do not currently abide by the rules defined in the album class - * - * @param album - * @throws AlbumException Message for the UI portion of the code. - */ - private void validate(Album album) throws AlbumException { - Set> violations = Validation.buildDefaultValidatorFactory().getValidator().validate(album); - for (ConstraintViolation violation : violations) { - throw new AlbumException(violation.getMessage()); - } + container.removeItem(post.getPath()); + return !container.containsId(post.getPath()); } /** @@ -158,10 +82,11 @@ private void validate(Album album) throws AlbumException { * @param post * @throws PostException Message for the UI portion of the code. */ - private void validate(Post post) throws PostException { - Set> violations = Validation.buildDefaultValidatorFactory().getValidator().validate(post); - for (ConstraintViolation violation : violations) { - throw new PostException(violation.getMessage()); + protected void validate(Post post) throws PostException { + try { + super.validate(post); + } catch(Exception e) { + throw new PostException(e.getMessage()); } } @@ -170,16 +95,15 @@ private void validate(Post post) throws PostException { * @see com.piccritic.database.post.PostConnector#getPosts(com.piccritic.database.user.Critic) */ public List getPosts(Critic critic){ - critic.getAlbums(); String query1 = "SELECT a FROM Album a WHERE a.critic = :crit ORDER BY a.creationDate"; - TypedQuery q = albums.getEntityProvider().getEntityManager().createQuery(query1, Album.class) + TypedQuery q = entity.createQuery(query1, Album.class) .setParameter("crit", critic); List postList = new ArrayList(); List albums = q.getResultList(); for(Album album: albums){ String query2 = "SELECT p FROM Post p WHERE p.album = :album"; - TypedQuery q2 = posts.getEntityProvider().getEntityManager().createQuery(query2, Post.class) + TypedQuery q2 = entity.createQuery(query2, Post.class) .setParameter("album", album); postList.addAll(q2.getResultList()); } @@ -194,7 +118,7 @@ public List getPosts(Critic critic){ */ public List getPosts(int number) throws PostException { try { - TypedQuery q = posts.getEntityProvider().getEntityManager().createQuery("SELECT c from Post c", Post.class); + TypedQuery q = entity.createQuery("SELECT c from Post c", Post.class); q.setMaxResults(number); return q.getResultList(); } catch (Exception e) { diff --git a/database/src/main/java/com/piccritic/database/post/Post.java b/database/src/main/java/com/piccritic/database/post/Post.java index efb4a7c..03947c0 100644 --- a/database/src/main/java/com/piccritic/database/post/Post.java +++ b/database/src/main/java/com/piccritic/database/post/Post.java @@ -29,7 +29,7 @@ * The primary key is the post's id (Long) defined by the database. * It is assigned to an {@link Album} object. * - * @author Ryan Lowe
Johnathan Ignacio + * @author Ryan Lowe
Jonathan Ignacio */ @Entity public class Post { diff --git a/database/src/main/java/com/piccritic/database/post/PostConnector.java b/database/src/main/java/com/piccritic/database/post/PostConnector.java index 869ab0b..b00af35 100644 --- a/database/src/main/java/com/piccritic/database/post/PostConnector.java +++ b/database/src/main/java/com/piccritic/database/post/PostConnector.java @@ -6,8 +6,6 @@ import java.util.List; -import com.piccritic.database.feedback.Comment; -import com.piccritic.database.feedback.Rating; import com.piccritic.database.user.Critic; /** @@ -19,42 +17,7 @@ * ian-dawson */ public interface PostConnector { - - /** - * Inserts the specified Album into the database. - * - * @param album the album to insert - * @return the same album, with a generated ID - * @throws AlbumException if the album validation fails - */ - public Album insertAlbum(Album album) throws AlbumException; - - /** - * Updates the specified Album in the database. - * - * @param album the album to update - * @return the same album - * @throws AlbumException if the album validation fails - */ - public Album updateAlbum(Album album) throws AlbumException; - - /** - * Gets the Album with the matching ID from the database. - * - * @param id the id to search for - * @return the album with the given ID, or null if it doesn't exist - */ - public Album selectAlbum(Long id); - - /** - * Deletes the specified Album from the database. - * - * @param album the album to delete - * @return true on successful delete - * @throws AlbumException if the album validation fails - */ - public boolean deleteAlbum(Album album) throws AlbumException; - + /** * Inserts the specified Post into the database. * From a565f2c9dff2f5d209f4769cc88466c892abfe2d Mon Sep 17 00:00:00 2001 From: jonathanignacio Date: Fri, 24 Mar 2017 15:10:52 -0300 Subject: [PATCH 02/12] Fixed warnings in DB and all connectors extend JPAConnector now. --- .../com/piccritic/database/JPAConnector.java | 28 +++++-- .../feedback/JPACommentConnector.java | 75 +++++++++-------- .../database/feedback/JPARatingConnector.java | 57 ++++++------- .../database/feedback/JPAVoteConnector.java | 63 +++++++++------ .../database/feedback/RatingConnector.java | 4 - .../database/license/JPALicenseConnector.java | 21 ++++- .../database/post/JPAAlbumConnector.java | 16 ++-- .../database/post/JPAPostConnector.java | 16 ++-- .../database/user/JPAUserConnector.java | 80 +++++++------------ .../database/user/UserConnector.java | 4 - 10 files changed, 190 insertions(+), 174 deletions(-) diff --git a/database/src/main/java/com/piccritic/database/JPAConnector.java b/database/src/main/java/com/piccritic/database/JPAConnector.java index 334e384..a69f8d8 100644 --- a/database/src/main/java/com/piccritic/database/JPAConnector.java +++ b/database/src/main/java/com/piccritic/database/JPAConnector.java @@ -9,16 +9,24 @@ import javax.validation.ConstraintViolation; import javax.validation.Validation; -import com.piccritic.database.feedback.Vote; -import com.piccritic.database.feedback.VoteException; import com.vaadin.addon.jpacontainer.JPAContainer; import com.vaadin.addon.jpacontainer.JPAContainerFactory; +/** + * Abstract class to define a JPAConnector's constructor and validate methods. Has EntityManager entity + * and JPAContainer container. + * @param Type of item to create the JPAContainer for. + * @author Damien Robichaud
Jonathan Ignacio + */ public abstract class JPAConnector { protected EntityManager entity; protected JPAContainer container; + /** + * Constructs the JPAConnector by establishing the database connection and instantiating the connector's JPA container. + * @param c - The class of the inherited JPAConnector, necessary to create a JPAContainer of a particular type. + */ public JPAConnector(Class c) { container = JPAContainerFactory.make(c, entity); Map configOverrides = new HashMap(); @@ -28,9 +36,10 @@ public JPAConnector(Class c) { } /** - * Ensures that object follows constraints correctly. - * @param t the object to be checked - * @throws Exception + * Validates the fields and throws exceptions when the fields + * do not currently abide by the rules defined in the given object's class + * @param t the object to be checked. + * @throws Exception - general exception, varies by the object's validate method. */ protected void validate(T t) throws Exception { Set> violations = Validation.buildDefaultValidatorFactory().getValidator().validate(t); @@ -39,3 +48,12 @@ protected void validate(T t) throws Exception { } } } + +//TODO Comment - Test +//TODO Rating - Test +//TODO Vote - Test +//TODO License - Test +//TODO Album - Test +//TODO Post - Test +//TODO User - Test + diff --git a/database/src/main/java/com/piccritic/database/feedback/JPACommentConnector.java b/database/src/main/java/com/piccritic/database/feedback/JPACommentConnector.java index f3cfebc..4a21517 100644 --- a/database/src/main/java/com/piccritic/database/feedback/JPACommentConnector.java +++ b/database/src/main/java/com/piccritic/database/feedback/JPACommentConnector.java @@ -4,46 +4,36 @@ */ package com.piccritic.database.feedback; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Set; -import javax.persistence.EntityManager; -import javax.persistence.Persistence; import javax.persistence.TypedQuery; -import javax.validation.ConstraintViolation; -import javax.validation.Validation; +import com.piccritic.database.JPAConnector; import com.piccritic.database.post.Post; import com.piccritic.database.user.Critic; import com.vaadin.addon.jpacontainer.EntityItem; -import com.vaadin.addon.jpacontainer.JPAContainer; -import com.vaadin.addon.jpacontainer.JPAContainerFactory; /** - * This class implements {@link CommentConnector} using Vaadin JPAContainers. - * + * This class enables a connection to the database using JPAContainers. It has a number of methods for + * performing comment-related operations on the database. Implements {@link CommentConnector}. Extends {@link JPAConnector}. * @author Ryan Lowe */ -public class JPACommentConnector implements CommentConnector { +public class JPACommentConnector extends JPAConnector implements CommentConnector { - private JPAContainer comments; - - public JPACommentConnector() { - Map configOverrides = new HashMap(); - configOverrides.put("hibernate.connection.url", System.getenv("JDBC_DATABASE_URL")); - - EntityManager entity = Persistence.createEntityManagerFactory("postgres", configOverrides).createEntityManager(); - comments = JPAContainerFactory.make(Comment.class, entity); + /** + * Initializes the JPAContainer for this CommentConnector. + */ + public JPACommentConnector() { + super(Comment.class); } /* (non-Javadoc) * @see com.piccritic.database.feedback.CommentConnector#selectComment(java.lang.Long) */ public Comment selectComment(Long id) { - EntityItem commentItem = comments.getItem(id); + EntityItem commentItem = container.getItem(id); return (commentItem != null) ? commentItem.getEntity() : null; } @@ -53,7 +43,7 @@ public Comment selectComment(Long id) { public Comment insertComment(Comment comment) throws CommentException { validate(comment); comment.setId(null); - comment.setId((Long) comments.addEntity(comment)); + comment.setId((Long) container.addEntity(comment)); return selectComment(comment.getId()); } @@ -63,7 +53,7 @@ public Comment insertComment(Comment comment) throws CommentException { @SuppressWarnings("unchecked") public Comment updateComment(Comment comment) throws CommentException { validate(comment); - EntityItem commentItem = comments.getItem(comment.getId()); + EntityItem commentItem = container.getItem(comment.getId()); commentItem.getItemProperty("content").setValue(comment.getContent()); commentItem.getItemProperty("votes").setValue(comment.getVotes()); @@ -77,7 +67,7 @@ public Comment updateComment(Comment comment) throws CommentException { */ @SuppressWarnings("unchecked") public boolean deleteComment(Comment comment) { - EntityItem commentItem = comments.getItem(comment.getId()); + EntityItem commentItem = container.getItem(comment.getId()); // Check if the comment has any replies Set replies = (Set) commentItem.getItemProperty("replies").getValue(); if (replies == null || replies.isEmpty()) { @@ -89,9 +79,9 @@ public boolean deleteComment(Comment comment) { // Remove the comment from the post comment.getPost().getComments().remove(comment); // Delete the comment - comments.removeItem(comment.getId()); + container.removeItem(comment.getId()); // Return true if comment was deleted - return !comments.containsId(comment.getId()); + return !container.containsId(comment.getId()); } // If there are replies, remove the author and the content but keep the comment Critic author = (Critic) commentItem.getItemProperty("author"); @@ -99,34 +89,51 @@ public boolean deleteComment(Comment comment) { commentItem.getItemProperty("author").setValue(null); commentItem.getItemProperty("content").setValue(""); commentItem.commit(); - return comments.getItem(comment.getId()).getItemProperty("author") == null; + return container.getItem(comment.getId()).getItemProperty("author") == null; } - private void validate(Comment comment) throws CommentException { - Set> violations = Validation.buildDefaultValidatorFactory().getValidator().validate(comment); - for (ConstraintViolation violation : violations) { - throw new CommentException(violation.getMessage()); + /* + * (non-Javadoc) + * @see com.piccritic.database.JPAConnector#validate(java.lang.Object) + */ + protected void validate(Comment comment) throws CommentException { + try { + super.validate(comment); + } catch (Exception e) { + throw new CommentException(e.getMessage()); } } - + + /* + * (non-Javadoc) + * @see com.piccritic.database.feedback.CommentConnector#getComments(com.piccritic.database.post.Post) + */ @Override public List getComments(Post post) { String query = "SELECT c FROM Comment c WHERE c.post = :path ORDER BY c.creationDate"; - TypedQuery q = comments.getEntityProvider().getEntityManager().createQuery(query, Comment.class).setParameter("path", post); + TypedQuery q = container.getEntityProvider().getEntityManager().createQuery(query, Comment.class).setParameter("path", post); return q.getResultList(); } + /* + * (non-Javadoc) + * @see com.piccritic.database.feedback.CommentConnector#getComments(com.piccritic.database.user.Critic) + */ @Override public List getComments(Critic critic) { String query = "SELECT c FROM Comment c WHERE c.critic = :crit ORDER BY c.creationDate"; - TypedQuery q = comments.getEntityProvider().getEntityManager().createQuery(query, Comment.class).setParameter("crit", critic); + TypedQuery q = container.getEntityProvider().getEntityManager().createQuery(query, Comment.class).setParameter("crit", critic); return q.getResultList(); } + /* + * (non-Javadoc) + * @see com.piccritic.database.feedback.CommentConnector#getCommentCount(com.piccritic.database.user.Critic) + */ @Override public int getCommentCount(Critic critic) { String query = "SELECT c FROM Comment c WHERE c.critic = :critic"; - TypedQuery q = comments.getEntityProvider().getEntityManager().createQuery(query, Comment.class) + TypedQuery q = container.getEntityProvider().getEntityManager().createQuery(query, Comment.class) .setParameter("critic", critic); List commentList = q.getResultList(); diff --git a/database/src/main/java/com/piccritic/database/feedback/JPARatingConnector.java b/database/src/main/java/com/piccritic/database/feedback/JPARatingConnector.java index 7301173..12b4e6a 100644 --- a/database/src/main/java/com/piccritic/database/feedback/JPARatingConnector.java +++ b/database/src/main/java/com/piccritic/database/feedback/JPARatingConnector.java @@ -4,42 +4,28 @@ */ package com.piccritic.database.feedback; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - -import javax.persistence.EntityManager; -import javax.persistence.Persistence; -import javax.validation.ConstraintViolation; -import javax.validation.Validation; - +import com.piccritic.database.JPAConnector; import com.vaadin.addon.jpacontainer.EntityItem; -import com.vaadin.addon.jpacontainer.JPAContainer; -import com.vaadin.addon.jpacontainer.JPAContainerFactory; /** - * This class implements {@link RatingConnector} using Vaadin JPAContainers. - * - * @author Frank Bosse
Jonathan Ignacio + * This class enables a connection to the database using JPAContainers. It has a number of methods for + * performing rating-related operations on the database. Implements {@link RatingConnector}. Extends {@link JPAConnector}. + * @author Frank Bosse
Jonathan Ignacio */ -public class JPARatingConnector implements RatingConnector { +public class JPARatingConnector extends JPAConnector implements RatingConnector { - private JPAContainer ratings; - + /** + * Initializes the JPAContainer for this RatingConnector. + */ public JPARatingConnector() { - Map configOverrides = new HashMap(); - configOverrides.put("hibernate.connection.url", System.getenv("JDBC_DATABASE_URL")); - - EntityManager entity = Persistence.createEntityManagerFactory("postgres", configOverrides).createEntityManager(); - - ratings = JPAContainerFactory.make(Rating.class, entity); + super(Rating.class); } /* (non-Javadoc) * @see com.piccritic.database.feedback.CommentConnector#selectComment(java.lang.Long) */ public Rating selectRating(Long id) { - EntityItem ratingItem = ratings.getItem(id); + EntityItem ratingItem = container.getItem(id); return (ratingItem != null) ? ratingItem.getEntity() : null; } @@ -49,7 +35,7 @@ public Rating selectRating(Long id) { public Rating insertRating(Rating rating) throws RatingException { validate(rating); rating.setId(null); - rating.setId((Long)ratings.addEntity(rating)); + rating.setId((Long)container.addEntity(rating)); return selectRating(rating.getId()); } @@ -58,7 +44,7 @@ public Rating insertRating(Rating rating) throws RatingException { */ public Rating updateRating(Rating rating) throws RatingException { validate(rating); - EntityItem ratingItem = ratings.getItem(rating.getId()); + EntityItem ratingItem = container.getItem(rating.getId()); ratingItem.commit(); @@ -69,14 +55,19 @@ public Rating updateRating(Rating rating) throws RatingException { * @see com.piccritic.database.feedback.CommentConnector#deleteComment(com.piccritic.database.feedback.Comment) */ public boolean deleteRating(Rating rating) { - ratings.removeItem(rating.getId()); - return !ratings.containsId(rating.getId()); + container.removeItem(rating.getId()); + return !container.containsId(rating.getId()); } - - private void validate(Rating rating) throws RatingException { - Set> violations = Validation.buildDefaultValidatorFactory().getValidator().validate(rating); - for (ConstraintViolation violation : violations) { - throw new RatingException(violation.getMessage()); + + /* + * (non-Javadoc) + * @see com.piccritic.database.JPAConnector#validate(java.lang.Object) + */ + protected void validate(Rating rating) throws RatingException { + try { + super.validate(rating); + } catch (Exception e) { + throw new RatingException(e.getMessage()); } } } diff --git a/database/src/main/java/com/piccritic/database/feedback/JPAVoteConnector.java b/database/src/main/java/com/piccritic/database/feedback/JPAVoteConnector.java index b30b921..6f82916 100644 --- a/database/src/main/java/com/piccritic/database/feedback/JPAVoteConnector.java +++ b/database/src/main/java/com/piccritic/database/feedback/JPAVoteConnector.java @@ -14,12 +14,15 @@ import com.vaadin.addon.jpacontainer.EntityItem; /** - * This class implements {@link VoteConnector} using Vaadin JPAContainers. - * - * @author Jonathan Ignacio and Frank Bosse + * This class enables a connection to the database using JPAContainers. It has a number of methods for + * performing vote-related operations on the database. Implements {@link VoteConnector}. Extends {@link JPAConnector}. + * @author Jonathan Ignacio
Frank Bosse */ public class JPAVoteConnector extends JPAConnector implements VoteConnector{ + /** + * Initializes the JPAContainer for this VoteConnector. + */ public JPAVoteConnector(){ super(Vote.class); } @@ -31,25 +34,6 @@ public Vote selectVote(Long id) { EntityItem voteItem = container.getItem(id); return (voteItem != null) ? voteItem.getEntity() : null; } - - /* (non-Javadoc) - * @see com.piccritic.database.feedback.VoteConnector#getVoteId(com.piccritic.database.user.Critic, com.piccritic.database.feedback.Comment) - */ - public Long getVoteId(Critic critic, Comment comment) { - if (critic == null || comment == null) { - return null; - } - String query = "SELECT v from Vote v WHERE v.critic = :cr AND v.comment = :co"; - TypedQuery v = container.getEntityProvider().getEntityManager().createQuery(query, Vote.class) - .setParameter("cr", critic) - .setParameter("co", comment); - List votes = v.getResultList(); - if (votes != null && votes.size() >= 1) { - return v.getResultList().get(0).getId(); - } else { - return null; - } - } /* (non-Javadoc) * @see com.piccritic.database.feedback.VoteConnector#insertVote(com.piccritic.database.feedback.Vote) @@ -128,6 +112,10 @@ public boolean deleteVote(Vote vote) throws VoteException { return !container.containsId(vote.getId()); } + /* + * (non-Javadoc) + * @see com.piccritic.database.JPAConnector#validate(java.lang.Object) + */ protected void validate(Vote vote) throws VoteException { try{ super.validate(vote); @@ -136,10 +124,33 @@ protected void validate(Vote vote) throws VoteException { } } + /* (non-Javadoc) + * @see com.piccritic.database.feedback.VoteConnector#getVoteId(com.piccritic.database.user.Critic, com.piccritic.database.feedback.Comment) + */ + public Long getVoteId(Critic critic, Comment comment) { + if (critic == null || comment == null) { + return null; + } + String query = "SELECT v from Vote v WHERE v.critic = :cr AND v.comment = :co"; + TypedQuery v = entity.createQuery(query, Vote.class) + .setParameter("cr", critic) + .setParameter("co", comment); + List votes = v.getResultList(); + if (votes != null && votes.size() >= 1) { + return v.getResultList().get(0).getId(); + } else { + return null; + } + } + + /* + * (non-Javadoc) + * @see com.piccritic.database.feedback.VoteConnector#getScore(com.piccritic.database.feedback.Comment) + */ @Override public int getScore(Comment comment) { String query = "SELECT v FROM Vote v WHERE v.comment = :comment"; - TypedQuery q = container.getEntityProvider().getEntityManager().createQuery(query, Vote.class) + TypedQuery q = entity.createQuery(query, Vote.class) .setParameter("comment", comment); List voteList = q.getResultList(); @@ -154,10 +165,14 @@ public int getScore(Comment comment) { return score; } + /* + * (non-Javadoc) + * @see com.piccritic.database.feedback.VoteConnector#getVotes(com.piccritic.database.feedback.Comment) + */ @Override public List getVotes(Comment comment) { String query = "SELECT v FROM Vote v WHERE v.comment = :comment"; - TypedQuery q = container.getEntityProvider().getEntityManager().createQuery(query, Vote.class) + TypedQuery q = entity.createQuery(query, Vote.class) .setParameter("comment", comment); return q.getResultList(); } diff --git a/database/src/main/java/com/piccritic/database/feedback/RatingConnector.java b/database/src/main/java/com/piccritic/database/feedback/RatingConnector.java index d8768ff..4198b4b 100644 --- a/database/src/main/java/com/piccritic/database/feedback/RatingConnector.java +++ b/database/src/main/java/com/piccritic/database/feedback/RatingConnector.java @@ -4,10 +4,6 @@ */ package com.piccritic.database.feedback; -import java.util.List; - -import com.piccritic.database.user.Critic; - /** * This interface provides methods that read to and write from the database * with regard to {@link Rating} objects. diff --git a/database/src/main/java/com/piccritic/database/license/JPALicenseConnector.java b/database/src/main/java/com/piccritic/database/license/JPALicenseConnector.java index cdd3908..fd54dcc 100644 --- a/database/src/main/java/com/piccritic/database/license/JPALicenseConnector.java +++ b/database/src/main/java/com/piccritic/database/license/JPALicenseConnector.java @@ -1,14 +1,22 @@ /** - * + * JPALicenseConnector.java + * Created March ??, 2017 */ package com.piccritic.database.license; import com.piccritic.database.JPAConnector; import com.vaadin.addon.jpacontainer.EntityItem; +/** + * This class enables a connection to the database using JPAContainers. It has a method for selecting a license from the database + * and a method to verify if a license exists. Implements {@link LicenseConnector}. Extends {@link JPAConnector}. + * @author Damien Robichaud + */ public class JPALicenseConnector extends JPAConnector implements LicenseConnector { - + /** + * Initializes the JPAContainer for this Connector. + */ public JPALicenseConnector() { super(License.class); if (!exists(License.ATTRIBUTION)) { @@ -31,11 +39,20 @@ public JPALicenseConnector() { } } + /** + * Checks if the given type of license exists. + * @param licenseType - The type of license. + * @return true if the license exists. False otherwise. + */ private boolean exists(String licenseType) { EntityItem licenseItem = container.getItem(licenseType); return licenseItem != null; } + /* + * (non-Javadoc) + * @see com.piccritic.database.license.LicenseConnector#selectLicense(java.lang.String) + */ public License selectLicense(String licenseType) { EntityItem licenseItem = container.getItem(licenseType); return (licenseItem != null) ? licenseItem.getEntity() : null; diff --git a/database/src/main/java/com/piccritic/database/post/JPAAlbumConnector.java b/database/src/main/java/com/piccritic/database/post/JPAAlbumConnector.java index 705b502..8f49e76 100644 --- a/database/src/main/java/com/piccritic/database/post/JPAAlbumConnector.java +++ b/database/src/main/java/com/piccritic/database/post/JPAAlbumConnector.java @@ -3,10 +3,15 @@ import com.piccritic.database.JPAConnector; import com.vaadin.addon.jpacontainer.EntityItem; +/** + * This class enables a connection to the database using JPAContainers. It has a number of methods for + * performing album-related operations on the database. Implements {@link AlbumConnector}. Extends {@link JPAConnector}. + * @author Ryan Lowe
Jonathan Ignacio
Damien Robichaud + */ public class JPAAlbumConnector extends JPAConnector implements AlbumConnector{ /** - * Initializes the JPAContainers for this PostConnector. + * Initializes the JPAContainer for this AlbumConnector. */ public JPAAlbumConnector() { super(Album.class); @@ -57,12 +62,9 @@ public boolean deleteAlbum(Album album) throws AlbumException { } - /** - * Validates the fields and throws exceptions when the fields - * do not currently abide by the rules defined in the album class - * - * @param album - * @throws AlbumException Message for the UI portion of the code. + /* + * (non-Javadoc) + * @see com.piccritic.database.JPAConnector#validate(java.lang.Object) */ protected void validate(Album album) throws AlbumException { try{ diff --git a/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java b/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java index eb56e66..4f23194 100644 --- a/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java +++ b/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java @@ -14,15 +14,14 @@ import com.vaadin.addon.jpacontainer.EntityItem; /** - * This class implements {@link PostConnector} using - * Vaadin JPAContainers. - * + * This class enables a connection to the database using JPAContainers. It has a number of methods for + * performing post-related operations on the database. Implements {@link PostConnector}. Extends {@link JPAConnector}. * @author Ryan Lowe
Jonathan Ignacio
Damien Robichaud */ public class JPAPostConnector extends JPAConnector implements PostConnector { /** - * Initializes the JPAContainers for this PostConnector. + * Initializes the JPAContainer for this PostConnector. */ public JPAPostConnector() { super(Post.class); @@ -75,12 +74,9 @@ public boolean deletePost(Post post) throws PostException { return !container.containsId(post.getPath()); } - /** - * Validates the fields and throws exceptions when the fields - * do not currently abide by the rules defined in the post class - * - * @param post - * @throws PostException Message for the UI portion of the code. + /* + * (non-Javadoc) + * @see com.piccritic.database.JPAConnector#validate(java.lang.Object) */ protected void validate(Post post) throws PostException { try { diff --git a/database/src/main/java/com/piccritic/database/user/JPAUserConnector.java b/database/src/main/java/com/piccritic/database/user/JPAUserConnector.java index 052e7e5..a7d21ef 100644 --- a/database/src/main/java/com/piccritic/database/user/JPAUserConnector.java +++ b/database/src/main/java/com/piccritic/database/user/JPAUserConnector.java @@ -4,44 +4,36 @@ */ package com.piccritic.database.user; -import java.util.Set; - -import javax.validation.ConstraintViolation; -import javax.validation.Validation; - import com.piccritic.database.JPAConnector; import com.vaadin.addon.jpacontainer.EntityItem; import com.vaadin.addon.jpacontainer.JPAContainer; import com.vaadin.addon.jpacontainer.JPAContainerFactory; /** - * This class enables connection to the database using JPAContainers. It has a - * number of methods for performing user-related operations on the database. - * - * @author Ryan Lowe
- * Damien Robichaud + * This class enables a connection to the database using JPAContainers. It has a number of methods for + * performing user-related operations on the database. Implements {@link UserConnector}. Extends {@link JPAConnector}. + * @author Ryan Lowe
Damien Robichaud */ -public class JPAUserConnector extends JPAConnector implements UserConnector { +public class JPAUserConnector extends JPAConnector implements UserConnector { - private JPAContainer critics; private JPAContainer logins; + /** + * Initializes the JPAContainers for this UserConnector. + */ public JPAUserConnector() { - critics = JPAContainerFactory.make(Critic.class, entity); + super(Critic.class); logins = JPAContainerFactory.make(UserLogin.class, entity); } /* * (non-Javadoc) - * - * @see - * com.piccritic.database.user.UserConnector#insertCritic(com.piccritic. - * database.user.Critic, java.lang.String) + * @see com.piccritic.database.user.UserConnector#insertCritic(com.piccritic.database.user.Critic, java.lang.String) */ public Critic insertCritic(Critic critic, String hash) throws UserException { validate(critic); - critics.addEntity(critic); + container.addEntity(critic); UserLogin login = new UserLogin(); login.setHandle(critic.getHandle()); login.setHash(hash); @@ -52,40 +44,32 @@ public Critic insertCritic(Critic critic, String hash) throws UserException { /* * (non-Javadoc) - * - * @see - * com.piccritic.database.user.UserConnector#deleteCritic(com.piccritic. - * database.user.Critic) + * @see com.piccritic.database.user.UserConnector#deleteCritic(com.piccritic.database.user.Critic) */ public boolean deleteCritic(Critic critic) { String handle = critic.getHandle(); - critics.removeItem(handle); + container.removeItem(handle); logins.removeItem(handle); - return !critics.containsId(handle) && !logins.containsId(handle); + return !container.containsId(handle) && !logins.containsId(handle); } /* * (non-Javadoc) - * - * @see - * com.piccritic.database.user.UserConnector#selectCritic(java.lang.String) + * @see com.piccritic.database.user.UserConnector#selectCritic(java.lang.String) */ public Critic selectCritic(String handle) { - EntityItem criticItem = critics.getItem(handle); + EntityItem criticItem = container.getItem(handle); return (criticItem != null) ? criticItem.getEntity() : null; } /* * (non-Javadoc) - * - * @see - * com.piccritic.database.user.UserConnector#updateCritic(com.piccritic. - * database.user.Critic) + * @see com.piccritic.database.user.UserConnector#updateCritic(com.piccritic.database.user.Critic) */ @SuppressWarnings("unchecked") public Critic updateCritic(Critic critic) throws UserException { - EntityItem criticItem = critics.getItem(critic.getHandle()); + EntityItem criticItem = container.getItem(critic.getHandle()); validate(critic); criticItem.getItemProperty("firstName").setValue(critic.getFirstName()); @@ -99,10 +83,7 @@ public Critic updateCritic(Critic critic) throws UserException { /* * (non-Javadoc) - * - * @see - * com.piccritic.database.user.UserConnector#updateCritic(com.piccritic. - * database.user.Critic, java.lang.String) + * @see com.piccritic.database.user.UserConnector#updateCritic(com.piccritic.database.user.Critic, java.lang.String) */ @SuppressWarnings("unchecked") public Critic updateCritic(Critic critic, String hash) throws UserException { @@ -115,9 +96,7 @@ public Critic updateCritic(Critic critic, String hash) throws UserException { /* * (non-Javadoc) - * - * @see - * com.piccritic.database.user.UserConnector#getUserHash(java.lang.String) + * @see com.piccritic.database.user.UserConnector#getUserHash(java.lang.String) */ public String getUserHash(String handle) { @@ -128,18 +107,17 @@ public String getUserHash(String handle) { return (String) login.getItemProperty("hash").getValue(); } - /** - * Validates the fields and throws exceptions when the fields - * do not currently abide by the rules defined in the critic class - * - * @param critic - * @throws UserException Message for the UI portion of the code. + /* + * (non-Javadoc) + * @see com.piccritic.database.JPAConnector#validate(java.lang.Object) */ - private void validate(Critic critic) throws UserException { - Set> violations = Validation.buildDefaultValidatorFactory().getValidator() - .validate(critic); - for (ConstraintViolation violation : violations) { - throw new UserException(violation.getPropertyPath() + " " + violation.getMessage()); + protected void validate(Critic critic) throws UserException { + // in the super validate method, violation.getPropertyPath() + " " + violation.getMessage()); + // is not present as it was in iteration 2 in this method. Leaving it out for now unless it is needed. + try{ + super.validate(critic); + } catch(Exception e) { + throw new UserException(e.getMessage()); } } diff --git a/database/src/main/java/com/piccritic/database/user/UserConnector.java b/database/src/main/java/com/piccritic/database/user/UserConnector.java index 6b2bb86..3fec3a9 100644 --- a/database/src/main/java/com/piccritic/database/user/UserConnector.java +++ b/database/src/main/java/com/piccritic/database/user/UserConnector.java @@ -4,10 +4,6 @@ */ package com.piccritic.database.user; -import java.util.List; - -import com.piccritic.database.feedback.Comment; - /** * This interface is to be used for any critic data queries. * From 50d2aa53b89bf8dc63a6dd149bac1a4f835fc64e Mon Sep 17 00:00:00 2001 From: Jonathan Ignacio Date: Fri, 24 Mar 2017 17:05:21 -0300 Subject: [PATCH 03/12] Modified database tests to reflect refactored changes. --- .../feedback/CommentConnectorTest.java | 13 +++++++++---- .../database/feedback/RatingConnectorTest.java | 12 +++++++++--- .../database/feedback/VoteConnectorTest.java | 11 +++++++---- .../database/post/PostConnectorTest.java | 18 +++++++++--------- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/database/src/test/java/com/piccritic/database/feedback/CommentConnectorTest.java b/database/src/test/java/com/piccritic/database/feedback/CommentConnectorTest.java index 6d50943..9ac8fab 100644 --- a/database/src/test/java/com/piccritic/database/feedback/CommentConnectorTest.java +++ b/database/src/test/java/com/piccritic/database/feedback/CommentConnectorTest.java @@ -1,6 +1,7 @@ package com.piccritic.database.feedback; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import java.sql.Date; import java.util.HashSet; @@ -12,8 +13,11 @@ import com.piccritic.database.license.AttributionLicense; import com.piccritic.database.license.JPALicenseConnector; +import com.piccritic.database.license.LicenseConnector; import com.piccritic.database.post.Album; +import com.piccritic.database.post.AlbumConnector; import com.piccritic.database.post.AlbumException; +import com.piccritic.database.post.JPAAlbumConnector; import com.piccritic.database.post.JPAPostConnector; import com.piccritic.database.post.Post; import com.piccritic.database.post.PostConnector; @@ -48,8 +52,9 @@ public class CommentConnectorTest { private Set ratings = new HashSet(); private AttributionLicense license = new AttributionLicense(); - JPALicenseConnector lc = new JPALicenseConnector(); + LicenseConnector lc = new JPALicenseConnector(); UserConnector uc = new JPAUserConnector(); + AlbumConnector ac = new JPAAlbumConnector(); PostConnector pc = new JPAPostConnector(); CommentConnector cc = new JPACommentConnector(); @@ -87,7 +92,7 @@ public void init() { try { critic = uc.insertCritic(critic, "hash"); album.setCritic(critic); - album = pc.insertAlbum(album); + album = ac.insertAlbum(album); post.setAlbum(album); posts.add(post); post = pc.insertPost(post); @@ -125,7 +130,7 @@ public void tearDown() { cc.deleteComment(comment); try { pc.deletePost(post); - pc.deleteAlbum(album); + ac.deleteAlbum(album); } catch (PostException | AlbumException e) { e.getLocalizedMessage(); } diff --git a/database/src/test/java/com/piccritic/database/feedback/RatingConnectorTest.java b/database/src/test/java/com/piccritic/database/feedback/RatingConnectorTest.java index 289d34d..2770c20 100644 --- a/database/src/test/java/com/piccritic/database/feedback/RatingConnectorTest.java +++ b/database/src/test/java/com/piccritic/database/feedback/RatingConnectorTest.java @@ -1,6 +1,9 @@ package com.piccritic.database.feedback; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.sql.Date; import java.util.HashSet; @@ -13,7 +16,9 @@ import com.piccritic.database.license.AttributionLicense; import com.piccritic.database.license.JPALicenseConnector; import com.piccritic.database.post.Album; +import com.piccritic.database.post.AlbumConnector; import com.piccritic.database.post.AlbumException; +import com.piccritic.database.post.JPAAlbumConnector; import com.piccritic.database.post.JPAPostConnector; import com.piccritic.database.post.Post; import com.piccritic.database.post.PostConnector; @@ -47,6 +52,7 @@ public class RatingConnectorTest { JPALicenseConnector lc = new JPALicenseConnector(); UserConnector uc = new JPAUserConnector(); PostConnector pc = new JPAPostConnector(); + AlbumConnector ac = new JPAAlbumConnector(); CommentConnector cc = new JPACommentConnector(); VoteConnector vc = new JPAVoteConnector(); RatingConnector rc = new JPARatingConnector(); @@ -85,7 +91,7 @@ public void init() { try { critic = uc.insertCritic(critic, "hash"); album.setCritic(critic); - album = pc.insertAlbum(album); + album = ac.insertAlbum(album); post.setAlbum(album); posts.add(post); @@ -138,7 +144,7 @@ public void cleanUp() { try { pc.deletePost(post); - pc.deleteAlbum(album); + ac.deleteAlbum(album); } catch (PostException | AlbumException e) { e.getLocalizedMessage(); } diff --git a/database/src/test/java/com/piccritic/database/feedback/VoteConnectorTest.java b/database/src/test/java/com/piccritic/database/feedback/VoteConnectorTest.java index ef1a29c..be4cbda 100644 --- a/database/src/test/java/com/piccritic/database/feedback/VoteConnectorTest.java +++ b/database/src/test/java/com/piccritic/database/feedback/VoteConnectorTest.java @@ -1,5 +1,6 @@ package com.piccritic.database.feedback; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import java.sql.Date; import java.util.HashSet; @@ -12,7 +13,9 @@ import com.piccritic.database.license.AttributionLicense; import com.piccritic.database.license.JPALicenseConnector; import com.piccritic.database.post.Album; +import com.piccritic.database.post.AlbumConnector; import com.piccritic.database.post.AlbumException; +import com.piccritic.database.post.JPAAlbumConnector; import com.piccritic.database.post.JPAPostConnector; import com.piccritic.database.post.Post; import com.piccritic.database.post.PostConnector; @@ -21,7 +24,6 @@ import com.piccritic.database.user.JPAUserConnector; import com.piccritic.database.user.UserConnector; import com.piccritic.database.user.UserException; -import com.piccritic.database.feedback.JPAVoteConnector; /** @@ -50,6 +52,7 @@ public class VoteConnectorTest { JPALicenseConnector lc = new JPALicenseConnector(); UserConnector uc = new JPAUserConnector(); PostConnector pc = new JPAPostConnector(); + AlbumConnector ac = new JPAAlbumConnector(); CommentConnector cc = new JPACommentConnector(); VoteConnector vc = new JPAVoteConnector(); @@ -92,7 +95,7 @@ public void init() { try { critic = uc.insertCritic(critic, "hash"); album.setCritic(critic); - album = pc.insertAlbum(album); + album = ac.insertAlbum(album); post.setAlbum(album); posts.add(post); @@ -152,7 +155,7 @@ public void tearDown() { vc.deleteVote(vote); cc.deleteComment(comment); pc.deletePost(post); - pc.deleteAlbum(album); + ac.deleteAlbum(album); } catch (PostException | AlbumException | VoteException e) { e.getLocalizedMessage(); } diff --git a/database/src/test/java/com/piccritic/database/post/PostConnectorTest.java b/database/src/test/java/com/piccritic/database/post/PostConnectorTest.java index c26ae85..95ae156 100644 --- a/database/src/test/java/com/piccritic/database/post/PostConnectorTest.java +++ b/database/src/test/java/com/piccritic/database/post/PostConnectorTest.java @@ -18,14 +18,13 @@ import org.junit.Test; import com.piccritic.database.feedback.Comment; +import com.piccritic.database.feedback.Rating; import com.piccritic.database.license.AttributionNonCommercialLicense; import com.piccritic.database.license.JPALicenseConnector; import com.piccritic.database.user.Critic; import com.piccritic.database.user.JPAUserConnector; import com.piccritic.database.user.UserConnector; import com.piccritic.database.user.UserException; -import com.piccritic.database.feedback.CommentException; -import com.piccritic.database.feedback.Rating; /** * This class uses JUnit to test the functionality @@ -64,6 +63,7 @@ public class PostConnectorTest { private Set ratingSet = new HashSet(); PostConnector pc = new JPAPostConnector(); + AlbumConnector ac = new JPAAlbumConnector(); UserConnector uc = new JPAUserConnector(); @Before @@ -111,7 +111,7 @@ public void init() { try { uc.insertCritic(critic, hash); album.setCritic(critic); - pc.insertAlbum(album); + ac.insertAlbum(album); post.setPath(path); post.setLicense(new AttributionNonCommercialLicense()); post1.setPath(path1); @@ -162,14 +162,14 @@ public void testDeletePost() { @Test public void testSelectAlbum() { - assertEquals(album, pc.selectAlbum(album.getId())); + assertEquals(album, ac.selectAlbum(album.getId())); } @Test public void testUpdateAlbum() { album.setName("new name"); try { - assertEquals(album, pc.updateAlbum(album)); + assertEquals(album, ac.updateAlbum(album)); } catch (AlbumException e) { fail(e.getMessage()); } @@ -183,9 +183,9 @@ public void testDeleteAlbum() { pc.deletePost(post2); pc.deletePost(post3); pc.deletePost(post4); - assertTrue(pc.deleteAlbum(album)); - assertNull(pc.selectAlbum(album.getId())); - pc.insertAlbum(album); + assertTrue(ac.deleteAlbum(album)); + assertNull(ac.selectAlbum(album.getId())); + ac.insertAlbum(album); pc.insertPost(post); pc.insertPost(post1); pc.insertPost(post2); @@ -228,7 +228,7 @@ public void tearDown() { pc.deletePost(post2); pc.deletePost(post3); pc.deletePost(post4); - pc.deleteAlbum(album); + ac.deleteAlbum(album); } catch (Exception e) { e.getLocalizedMessage(); } From 68810cc7435f8d9f8ca3a52dec6c052e6443e2c3 Mon Sep 17 00:00:00 2001 From: Jonathan Ignacio Date: Fri, 24 Mar 2017 17:47:50 -0300 Subject: [PATCH 04/12] Modify compute tests and classes to reflect refactored changes --- .../piccritic/compute/post/PostService.java | 5 ++- .../piccritic/compute/user/UserService.java | 26 ++++++++------- .../compute/feedback/FeedbackServiceTest.java | 24 +++++++------- .../compute/post/PostServiceTest.java | 17 +++++++--- .../com/piccritic/database/JPAConnector.java | 8 ----- .../com/piccritic/website/NavigationIT.java | 10 +----- .../com/piccritic/website/PicCriticIT.java | 12 +++---- .../piccritic/website/license/LicenseIT.java | 5 --- .../website/post/AddPostComponentsIT.java | 11 ++++--- .../piccritic/website/post/ViewPostIT.java | 9 +---- .../website/user/ViewUserPostsIT.java | 33 ++++++++++--------- 11 files changed, 75 insertions(+), 85 deletions(-) diff --git a/compute/src/main/java/com/piccritic/compute/post/PostService.java b/compute/src/main/java/com/piccritic/compute/post/PostService.java index 8919cac..b89f7ad 100644 --- a/compute/src/main/java/com/piccritic/compute/post/PostService.java +++ b/compute/src/main/java/com/piccritic/compute/post/PostService.java @@ -19,7 +19,9 @@ import com.piccritic.database.feedback.Vote; import com.piccritic.database.feedback.VoteException; import com.piccritic.database.post.Album; +import com.piccritic.database.post.AlbumConnector; import com.piccritic.database.post.AlbumException; +import com.piccritic.database.post.JPAAlbumConnector; import com.piccritic.database.post.JPAPostConnector; import com.piccritic.database.post.Post; import com.piccritic.database.post.PostConnector; @@ -38,6 +40,7 @@ public class PostService implements PostServiceInterface { public static final String USERS_DIR = "users"; static PostConnector pc = new JPAPostConnector(); + static AlbumConnector ac = new JPAAlbumConnector(); private FeedbackServiceInterface fs = FeedbackService.createService(); public File getImageFile(String handle) { @@ -113,7 +116,7 @@ public boolean deletePost(Post post) throws PostException, CommentException, Vot @Override public Album updateAlbum(Album album) throws AlbumException { - return pc.updateAlbum(album); + return ac.updateAlbum(album); } public Album getDefaultAlbum(String handle) { diff --git a/compute/src/main/java/com/piccritic/compute/user/UserService.java b/compute/src/main/java/com/piccritic/compute/user/UserService.java index e542763..81f3377 100644 --- a/compute/src/main/java/com/piccritic/compute/user/UserService.java +++ b/compute/src/main/java/com/piccritic/compute/user/UserService.java @@ -15,8 +15,9 @@ import com.piccritic.compute.hashing.Hasher; import com.piccritic.database.post.Album; +import com.piccritic.database.post.AlbumConnector; import com.piccritic.database.post.AlbumException; -import com.piccritic.database.post.JPAPostConnector; +import com.piccritic.database.post.JPAAlbumConnector; import com.piccritic.database.user.Critic; import com.piccritic.database.user.JPAUserConnector; import com.piccritic.database.user.UserConnector; @@ -28,7 +29,7 @@ */ public class UserService implements UserServiceInterface { private static UserServiceInterface instance; - private static UserConnector connector; + private static UserConnector uc; private static String createSuccess = "Profile successfully created!"; private static String updateSuccess = "Profile successfully updated!"; private static String createFailure = "Profile could not be created."; @@ -37,7 +38,8 @@ public class UserService implements UserServiceInterface { private static String handleInUse = "Handle already in use."; private UserService() { - connector = new JPAUserConnector(); + uc = new JPAUserConnector(); + } public static UserServiceInterface createService() { @@ -63,7 +65,7 @@ public String create(Critic critic, String password) throws UserException { } critic.setJoinDate(new Date(Calendar.getInstance().getTime().getTime())); - Critic selected = connector.selectCritic(critic.getHandle()); + Critic selected = uc.selectCritic(critic.getHandle()); if (selected != null) { throw new UserException(handleInUse); } @@ -71,7 +73,7 @@ public String create(Critic critic, String password) throws UserException { try { Hasher hasher = new Hasher(); String hash = hasher.generateHash(password); - Critic inserted = connector.insertCritic(critic, hash); + Critic inserted = uc.insertCritic(critic, hash); if (inserted == null) { throw new UserException(createFailure); } @@ -89,8 +91,8 @@ public String create(Critic critic, String password) throws UserException { critic.setAlbums(albums); try { - JPAPostConnector pc = new JPAPostConnector(); - pc.insertAlbum(defaultAlbum); + AlbumConnector ac = new JPAAlbumConnector(); + ac.insertAlbum(defaultAlbum); } catch (AlbumException e) { e.printStackTrace(); throw new UserException(e.getLocalizedMessage()); @@ -110,14 +112,14 @@ public String create(Critic critic, String password) throws UserException { */ @Override public String update(Critic critic, String password) throws UserException { - Critic selected = connector.selectCritic(critic.getHandle()); + Critic selected = uc.selectCritic(critic.getHandle()); if (selected == null) { throw new UserException(updateFailure); } Critic updated; if (password.isEmpty()) { - updated = connector.updateCritic(critic); + updated = uc.updateCritic(critic); if (updated == null) { throw new UserException(updateFailure); } @@ -126,7 +128,7 @@ public String update(Critic critic, String password) throws UserException { try { Hasher hasher = new Hasher(); String hash = hasher.generateHash(password); - Critic inserted = connector.insertCritic(critic, hash); + Critic inserted = uc.insertCritic(critic, hash); if (inserted == null) { throw new UserException(createFailure); } @@ -136,7 +138,7 @@ public String update(Critic critic, String password) throws UserException { } } - String hash = connector.getUserHash(critic.getHandle()); + String hash = uc.getUserHash(critic.getHandle()); if (hash == null) { throw new UserException(updateFailure); } @@ -149,6 +151,6 @@ public String update(Critic critic, String password) throws UserException { */ @Override public Critic select(String handle) { - return connector.selectCritic(handle); + return uc.selectCritic(handle); } } diff --git a/compute/src/test/java/com/piccritic/compute/feedback/FeedbackServiceTest.java b/compute/src/test/java/com/piccritic/compute/feedback/FeedbackServiceTest.java index 142d861..4bf4c09 100644 --- a/compute/src/test/java/com/piccritic/compute/feedback/FeedbackServiceTest.java +++ b/compute/src/test/java/com/piccritic/compute/feedback/FeedbackServiceTest.java @@ -17,8 +17,6 @@ import org.junit.Before; import org.junit.Test; -import com.piccritic.database.license.AttributionLicense; -import com.piccritic.database.license.JPALicenseConnector; import com.piccritic.database.feedback.Comment; import com.piccritic.database.feedback.CommentConnector; import com.piccritic.database.feedback.CommentException; @@ -31,8 +29,11 @@ import com.piccritic.database.feedback.Vote; import com.piccritic.database.feedback.VoteConnector; import com.piccritic.database.feedback.VoteException; +import com.piccritic.database.license.AttributionLicense; import com.piccritic.database.post.Album; +import com.piccritic.database.post.AlbumConnector; import com.piccritic.database.post.AlbumException; +import com.piccritic.database.post.JPAAlbumConnector; import com.piccritic.database.post.JPAPostConnector; import com.piccritic.database.post.Post; import com.piccritic.database.post.PostConnector; @@ -68,14 +69,15 @@ public class FeedbackServiceTest { private Set ratings = new HashSet(); private AttributionLicense license = new AttributionLicense(); - JPALicenseConnector lc = new JPALicenseConnector(); - UserConnector uc = new JPAUserConnector(); - PostConnector pc = new JPAPostConnector(); - CommentConnector cc = new JPACommentConnector(); - VoteConnector vc = new JPAVoteConnector(); - RatingConnector rc = new JPARatingConnector(); + //private JPALicenseConnector lc = new JPALicenseConnector(); //commented out because unused + private UserConnector uc = new JPAUserConnector(); + private PostConnector pc = new JPAPostConnector(); + private AlbumConnector ac = new JPAAlbumConnector(); + private CommentConnector cc = new JPACommentConnector(); + private VoteConnector vc = new JPAVoteConnector(); + private RatingConnector rc = new JPARatingConnector(); - FeedbackServiceInterface fs = FeedbackService.createService(); + private FeedbackServiceInterface fs = FeedbackService.createService(); @Before public void init() { @@ -139,7 +141,7 @@ public void init() { voter = uc.insertCritic(voter, "hash"); album.setCritic(critic); - album = pc.insertAlbum(album); + album = ac.insertAlbum(album); post.setAlbum(album); posts.add(post); @@ -320,7 +322,7 @@ public void tearDown() { try { cc.deleteComment(comment); pc.deletePost(post); - pc.deleteAlbum(album); + ac.deleteAlbum(album); } catch (PostException | AlbumException e) { e.getLocalizedMessage(); } diff --git a/compute/src/test/java/com/piccritic/compute/post/PostServiceTest.java b/compute/src/test/java/com/piccritic/compute/post/PostServiceTest.java index c65be53..f4d9bb5 100644 --- a/compute/src/test/java/com/piccritic/compute/post/PostServiceTest.java +++ b/compute/src/test/java/com/piccritic/compute/post/PostServiceTest.java @@ -19,12 +19,16 @@ import com.piccritic.database.feedback.VoteException; import com.piccritic.database.license.AttributionLicense; import com.piccritic.database.post.Album; +import com.piccritic.database.post.AlbumConnector; import com.piccritic.database.post.AlbumException; +import com.piccritic.database.post.JPAAlbumConnector; import com.piccritic.database.post.JPAPostConnector; import com.piccritic.database.post.Post; +import com.piccritic.database.post.PostConnector; import com.piccritic.database.post.PostException; import com.piccritic.database.user.Critic; import com.piccritic.database.user.JPAUserConnector; +import com.piccritic.database.user.UserConnector; import com.piccritic.database.user.UserException; /** @@ -41,8 +45,11 @@ public class PostServiceTest { private Comment comment; private Set criticComments = new HashSet(); private Set postComments = new HashSet(); - private JPAUserConnector uc = new JPAUserConnector(); - private JPAPostConnector pc = new JPAPostConnector(); + + private UserConnector uc = new JPAUserConnector(); + private PostConnector pc = new JPAPostConnector(); + private AlbumConnector ac = new JPAAlbumConnector(); + private PostService ps = new PostService(); private FeedbackServiceInterface fs = FeedbackService.createService(); @@ -84,7 +91,7 @@ public void setup() throws Exception{ comment.setScore(0); critic = uc.insertCritic(critic, "hash"); - pc.insertAlbum(album); + ac.insertAlbum(album); post.setPath("path"); } @@ -145,8 +152,8 @@ public void tearDown() { try { pc.deletePost(post); album.setPosts(null); - pc.updateAlbum(album); - pc.deleteAlbum(album); + ac.updateAlbum(album); + ac.deleteAlbum(album); critic.setAlbums(null); uc.updateCritic(critic); uc.deleteCritic(critic); diff --git a/database/src/main/java/com/piccritic/database/JPAConnector.java b/database/src/main/java/com/piccritic/database/JPAConnector.java index a69f8d8..2216eed 100644 --- a/database/src/main/java/com/piccritic/database/JPAConnector.java +++ b/database/src/main/java/com/piccritic/database/JPAConnector.java @@ -49,11 +49,3 @@ protected void validate(T t) throws Exception { } } -//TODO Comment - Test -//TODO Rating - Test -//TODO Vote - Test -//TODO License - Test -//TODO Album - Test -//TODO Post - Test -//TODO User - Test - diff --git a/website/src/test/java/com/piccritic/website/NavigationIT.java b/website/src/test/java/com/piccritic/website/NavigationIT.java index b6cff52..08290d0 100644 --- a/website/src/test/java/com/piccritic/website/NavigationIT.java +++ b/website/src/test/java/com/piccritic/website/NavigationIT.java @@ -1,18 +1,10 @@ package com.piccritic.website; -import static org.junit.Assert.*; +import static org.junit.Assert.assertTrue; -import org.junit.After; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.openqa.selenium.phantomjs.PhantomJSDriver; -import com.vaadin.testbench.ScreenshotOnFailureRule; -import com.vaadin.testbench.TestBenchTestCase; -import com.vaadin.testbench.elements.ComboBoxElement; -import com.vaadin.testbench.elements.LabelElement; -import com.vaadin.testbench.elements.PanelElement; import com.vaadin.testbench.elements.ButtonElement; /** diff --git a/website/src/test/java/com/piccritic/website/PicCriticIT.java b/website/src/test/java/com/piccritic/website/PicCriticIT.java index 12dde45..91784ae 100644 --- a/website/src/test/java/com/piccritic/website/PicCriticIT.java +++ b/website/src/test/java/com/piccritic/website/PicCriticIT.java @@ -4,20 +4,18 @@ import org.junit.Rule; import org.openqa.selenium.phantomjs.PhantomJSDriver; -import com.piccritic.database.license.AttributionLicense; -import com.piccritic.database.license.JPALicenseConnector; import com.piccritic.compute.user.UserService; import com.piccritic.compute.user.UserServiceInterface; -import com.piccritic.database.user.UserException; import com.piccritic.database.license.AttributionLicense; -import com.vaadin.testbench.TestBenchTestCase; +import com.piccritic.database.license.JPALicenseConnector; +import com.piccritic.database.user.UserException; import com.vaadin.testbench.ScreenshotOnFailureRule; +import com.vaadin.testbench.TestBenchTestCase; import com.vaadin.testbench.elements.ButtonElement; import com.vaadin.testbench.elements.ComboBoxElement; -import com.vaadin.testbench.elements.LabelElement; -import com.vaadin.testbench.elements.TextFieldElement; -import com.vaadin.testbench.elements.PasswordFieldElement; import com.vaadin.testbench.elements.NotificationElement; +import com.vaadin.testbench.elements.PasswordFieldElement; +import com.vaadin.testbench.elements.TextFieldElement; /** * @author Damien
Francis bosse diff --git a/website/src/test/java/com/piccritic/website/license/LicenseIT.java b/website/src/test/java/com/piccritic/website/license/LicenseIT.java index c869e3c..7fea2da 100644 --- a/website/src/test/java/com/piccritic/website/license/LicenseIT.java +++ b/website/src/test/java/com/piccritic/website/license/LicenseIT.java @@ -1,8 +1,5 @@ package com.piccritic.website.license; -import static org.junit.Assert.*; - -import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -13,8 +10,6 @@ import com.vaadin.testbench.TestBenchTestCase; import com.vaadin.testbench.elements.ComboBoxElement; import com.vaadin.testbench.elements.LabelElement; -import com.vaadin.testbench.elements.PanelElement; -import com.vaadin.testbench.elements.ButtonElement; /** * This class contains JUnit tests, which are run using Vaadin TestBench 4. diff --git a/website/src/test/java/com/piccritic/website/post/AddPostComponentsIT.java b/website/src/test/java/com/piccritic/website/post/AddPostComponentsIT.java index cafec2c..13bd8c9 100644 --- a/website/src/test/java/com/piccritic/website/post/AddPostComponentsIT.java +++ b/website/src/test/java/com/piccritic/website/post/AddPostComponentsIT.java @@ -12,15 +12,17 @@ import org.junit.Before; import org.junit.Test; -import com.piccritic.database.license.AttributionLicense; -import com.piccritic.database.license.JPALicenseConnector; import com.piccritic.database.feedback.Comment; import com.piccritic.database.feedback.CommentConnector; import com.piccritic.database.feedback.JPACommentConnector; import com.piccritic.database.feedback.Rating; import com.piccritic.database.feedback.Vote; +import com.piccritic.database.license.AttributionLicense; +import com.piccritic.database.license.JPALicenseConnector; import com.piccritic.database.post.Album; +import com.piccritic.database.post.AlbumConnector; import com.piccritic.database.post.AlbumException; +import com.piccritic.database.post.JPAAlbumConnector; import com.piccritic.database.post.JPAPostConnector; import com.piccritic.database.post.Post; import com.piccritic.database.post.PostConnector; @@ -63,6 +65,7 @@ public class AddPostComponentsIT extends PicCriticIT { JPALicenseConnector lc = new JPALicenseConnector(); PostConnector pc = new JPAPostConnector(); + AlbumConnector ac = new JPAAlbumConnector(); UserConnector uc = new JPAUserConnector(); // RatingConnector rc = new JPARatingConnector(); CommentConnector cc = new JPACommentConnector(); @@ -125,7 +128,7 @@ public void initthings() { uc.insertCritic(critic, hash); album.setCritic(critic); - pc.insertAlbum(album); + ac.insertAlbum(album); post.setPath(path); post.setAlbum(album); @@ -201,7 +204,7 @@ public void tearDown() { try { cc.deleteComment(cc.getComments(post).get(0)); pc.deletePost(post); - pc.deleteAlbum(album); + ac.deleteAlbum(album); } catch (PostException | AlbumException e) { e.getLocalizedMessage(); } diff --git a/website/src/test/java/com/piccritic/website/post/ViewPostIT.java b/website/src/test/java/com/piccritic/website/post/ViewPostIT.java index 28f1c1d..ce537ec 100644 --- a/website/src/test/java/com/piccritic/website/post/ViewPostIT.java +++ b/website/src/test/java/com/piccritic/website/post/ViewPostIT.java @@ -1,17 +1,10 @@ package com.piccritic.website.post; -import static org.junit.Assert.assertEquals; - -import org.junit.After; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.openqa.selenium.phantomjs.PhantomJSDriver; -import com.piccritic.website.PicCriticIT; import com.piccritic.database.user.UserException; -import com.vaadin.testbench.ScreenshotOnFailureRule; -import com.vaadin.testbench.TestBenchTestCase; +import com.piccritic.website.PicCriticIT; import com.vaadin.testbench.elements.ButtonElement; import com.vaadin.testbench.elements.TextAreaElement; import com.vaadin.testbench.elements.TextFieldElement; diff --git a/website/src/test/java/com/piccritic/website/user/ViewUserPostsIT.java b/website/src/test/java/com/piccritic/website/user/ViewUserPostsIT.java index 4a0fde1..6dfb774 100644 --- a/website/src/test/java/com/piccritic/website/user/ViewUserPostsIT.java +++ b/website/src/test/java/com/piccritic/website/user/ViewUserPostsIT.java @@ -1,9 +1,24 @@ package com.piccritic.website.user; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.IOException; +import java.sql.Date; +import java.util.HashSet; +import java.util.Set; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + import com.piccritic.database.license.AttributionLicense; import com.piccritic.database.license.JPALicenseConnector; import com.piccritic.database.post.Album; +import com.piccritic.database.post.AlbumConnector; import com.piccritic.database.post.AlbumException; +import com.piccritic.database.post.JPAAlbumConnector; import com.piccritic.database.post.JPAPostConnector; import com.piccritic.database.post.Post; import com.piccritic.database.post.PostConnector; @@ -15,19 +30,6 @@ import com.piccritic.website.PicCriticIT; import com.vaadin.testbench.elements.ImageElement; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.sql.Date; -import java.util.HashSet; -import java.util.Set; -import java.io.File; -import java.io.IOException; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - /** * This class contains JUnit tests, which are run using Vaadin TestBench 4. * @@ -58,6 +60,7 @@ public class ViewUserPostsIT extends PicCriticIT { JPALicenseConnector lc = new JPALicenseConnector(); PostConnector pc = new JPAPostConnector(); + AlbumConnector ac = new JPAAlbumConnector(); UserConnector uc = new JPAUserConnector(); private void openUserPage(String user) { @@ -88,7 +91,7 @@ public void initthings() { image.createNewFile(); uc.insertCritic(critic, hash); album.setCritic(critic); - pc.insertAlbum(album); + ac.insertAlbum(album); post.setPath(path); post.setAlbum(album); pc.insertPost(post); @@ -111,7 +114,7 @@ public void tearDown() { try { image.delete(); pc.deletePost(post); - pc.deleteAlbum(album); + ac.deleteAlbum(album); } catch (Exception e) { e.getLocalizedMessage(); } From 95e2b828ba87e8641334cd65c671772fd43e7acc Mon Sep 17 00:00:00 2001 From: damienr74 Date: Fri, 24 Mar 2017 18:22:25 -0300 Subject: [PATCH 05/12] fix entity manager bug Author: Damien & Jonathan --- database/src/main/java/com/piccritic/database/JPAConnector.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/src/main/java/com/piccritic/database/JPAConnector.java b/database/src/main/java/com/piccritic/database/JPAConnector.java index 2216eed..b717bec 100644 --- a/database/src/main/java/com/piccritic/database/JPAConnector.java +++ b/database/src/main/java/com/piccritic/database/JPAConnector.java @@ -28,11 +28,11 @@ public abstract class JPAConnector { * @param c - The class of the inherited JPAConnector, necessary to create a JPAContainer of a particular type. */ public JPAConnector(Class c) { - container = JPAContainerFactory.make(c, entity); Map configOverrides = new HashMap(); configOverrides.put("hibernate.connection.url", System.getenv("JDBC_DATABASE_URL")); entity = Persistence.createEntityManagerFactory("postgres", configOverrides).createEntityManager(); + container = JPAContainerFactory.make(c, entity); } /** From 5c3b4de4297f228723af854f2e632b072b68b62e Mon Sep 17 00:00:00 2001 From: ajsteadly Date: Mon, 27 Mar 2017 13:01:39 -0300 Subject: [PATCH 06/12] Added Master classes for connectors and services Refactoring to prevent needlessly creating extra instances of JPAConnectors and Services. --- .../piccritic/compute/MasterConnector.java | 37 +++++++++++++++++++ .../com/piccritic/compute/MasterService.java | 25 +++++++++++++ .../compute/feedback/FeedbackService.java | 27 ++++++++------ .../compute/license/LicenseService.java | 8 +++- .../piccritic/compute/post/PostService.java | 20 +++++++--- .../piccritic/compute/user/UserService.java | 8 +++- .../compute/feedback/FeedbackServiceTest.java | 21 ++++++++--- .../compute/post/PostServiceTest.java | 30 ++++++++------- .../database/post/JPAPostConnector.java | 2 +- .../database/post/PostConnectorTest.java | 2 +- .../com/piccritic/website/DefaultView.java | 4 +- .../java/com/piccritic/website/PicCritic.java | 7 ++-- .../website/feedback/CommentForm.java | 3 +- .../piccritic/website/post/CreatePost.java | 5 ++- .../piccritic/website/post/ImageReceiver.java | 8 +--- .../com/piccritic/website/post/ViewPost.java | 8 ++-- .../com/piccritic/website/user/UserForm.java | 7 ++-- .../com/piccritic/website/user/UserView.java | 12 +++--- .../com/piccritic/website/PicCriticIT.java | 9 +++-- .../website/post/AddPostComponentsIT.java | 2 +- .../website/user/ViewUserPostsIT.java | 2 +- 21 files changed, 174 insertions(+), 73 deletions(-) create mode 100644 compute/src/main/java/com/piccritic/compute/MasterConnector.java create mode 100644 compute/src/main/java/com/piccritic/compute/MasterService.java diff --git a/compute/src/main/java/com/piccritic/compute/MasterConnector.java b/compute/src/main/java/com/piccritic/compute/MasterConnector.java new file mode 100644 index 0000000..ecef024 --- /dev/null +++ b/compute/src/main/java/com/piccritic/compute/MasterConnector.java @@ -0,0 +1,37 @@ +package com.piccritic.compute; + +import com.piccritic.database.feedback.CommentConnector; +import com.piccritic.database.feedback.JPACommentConnector; +import com.piccritic.database.feedback.JPARatingConnector; +import com.piccritic.database.feedback.JPAVoteConnector; +import com.piccritic.database.feedback.RatingConnector; +import com.piccritic.database.feedback.VoteConnector; +import com.piccritic.database.license.JPALicenseConnector; +import com.piccritic.database.license.LicenseConnector; +import com.piccritic.database.post.AlbumConnector; +import com.piccritic.database.post.JPAAlbumConnector; +import com.piccritic.database.post.JPAPostConnector; +import com.piccritic.database.post.PostConnector; +import com.piccritic.database.user.JPAUserConnector; +import com.piccritic.database.user.UserConnector; + +public class MasterConnector { + + public static PostConnector postConnector; + public static AlbumConnector albumConnector; + public static UserConnector userConnector; + public static LicenseConnector licenseConnector; + public static CommentConnector commentConnector; + public static VoteConnector voteConnector; + public static RatingConnector ratingConnector; + + public static void init() { + if (postConnector == null) postConnector = new JPAPostConnector(); + if (albumConnector == null) albumConnector = new JPAAlbumConnector(); + if (userConnector == null) userConnector = new JPAUserConnector(); + if (licenseConnector == null) licenseConnector = new JPALicenseConnector(); + if (commentConnector == null) commentConnector = new JPACommentConnector(); + if (voteConnector == null) voteConnector = new JPAVoteConnector(); + if (ratingConnector == null) ratingConnector = new JPARatingConnector(); + } +} diff --git a/compute/src/main/java/com/piccritic/compute/MasterService.java b/compute/src/main/java/com/piccritic/compute/MasterService.java new file mode 100644 index 0000000..3e898b8 --- /dev/null +++ b/compute/src/main/java/com/piccritic/compute/MasterService.java @@ -0,0 +1,25 @@ +package com.piccritic.compute; + +import com.piccritic.compute.feedback.FeedbackService; +import com.piccritic.compute.feedback.FeedbackServiceInterface; +import com.piccritic.compute.license.LicenseService; +import com.piccritic.compute.license.LicenseServiceInterface; +import com.piccritic.compute.post.PostService; +import com.piccritic.compute.post.PostServiceInterface; +import com.piccritic.compute.user.UserService; +import com.piccritic.compute.user.UserServiceInterface; + +public class MasterService { + + public static FeedbackServiceInterface feedbackService; + public static UserServiceInterface userService; + public static LicenseServiceInterface licenseService; + public static PostServiceInterface postService; + + public static void init() { + if (feedbackService == null) feedbackService = FeedbackService.createService(); + if (userService == null) userService = UserService.createService(); + if (licenseService == null) licenseService = new LicenseService(); + if (postService == null) postService = new PostService(); + } +} diff --git a/compute/src/main/java/com/piccritic/compute/feedback/FeedbackService.java b/compute/src/main/java/com/piccritic/compute/feedback/FeedbackService.java index 42deff4..40644cd 100644 --- a/compute/src/main/java/com/piccritic/compute/feedback/FeedbackService.java +++ b/compute/src/main/java/com/piccritic/compute/feedback/FeedbackService.java @@ -8,6 +8,7 @@ import java.util.Calendar; import java.util.List; +import com.piccritic.compute.MasterConnector; import com.piccritic.database.feedback.Comment; import com.piccritic.database.feedback.CommentConnector; import com.piccritic.database.feedback.CommentException; @@ -46,10 +47,20 @@ public class FeedbackService implements FeedbackServiceInterface { final private static int repLoss = -1; private FeedbackService() { - cc = new JPACommentConnector(); - vc = new JPAVoteConnector(); - rc = new JPARatingConnector(); - pc = new JPAPostConnector(); + MasterConnector.init(); + cc = MasterConnector.commentConnector; + vc = MasterConnector.voteConnector; + rc = MasterConnector.ratingConnector; + pc = MasterConnector.postConnector; + } + + public static FeedbackServiceInterface createService() { + if (instance == null) { + final FeedbackServiceInterface service = new FeedbackService(); + instance = service; + } + + return instance; } /* (non-Javadoc) @@ -92,14 +103,6 @@ public int getRatingWeight(){ return ratingWeight; } - public static FeedbackServiceInterface createService() { - if (instance == null) { - final FeedbackServiceInterface service = new FeedbackService(); - instance = service; - } - - return instance; - } /* (non-Javadoc) * @see com.piccritic.compute.feedback.FeedbackServiceInterface#getComments(com.piccritic.database.post.Post) diff --git a/compute/src/main/java/com/piccritic/compute/license/LicenseService.java b/compute/src/main/java/com/piccritic/compute/license/LicenseService.java index d699396..0fe43ab 100644 --- a/compute/src/main/java/com/piccritic/compute/license/LicenseService.java +++ b/compute/src/main/java/com/piccritic/compute/license/LicenseService.java @@ -1,5 +1,6 @@ package com.piccritic.compute.license; +import com.piccritic.compute.MasterConnector; import com.piccritic.database.license.AttributionLicense; import com.piccritic.database.license.AttributionNoDerivsLicense; import com.piccritic.database.license.AttributionNonComNDerivsLicense; @@ -12,8 +13,13 @@ public class LicenseService implements LicenseServiceInterface { - public LicenseConnector lc = new JPALicenseConnector(); + public LicenseConnector lc; + public LicenseService() { + MasterConnector.init(); + lc = MasterConnector.licenseConnector; + } + @Override public AttributionLicense makeAttribution() { return (AttributionLicense) lc.selectLicense(License.ATTRIBUTION); diff --git a/compute/src/main/java/com/piccritic/compute/post/PostService.java b/compute/src/main/java/com/piccritic/compute/post/PostService.java index b89f7ad..ef0b8da 100644 --- a/compute/src/main/java/com/piccritic/compute/post/PostService.java +++ b/compute/src/main/java/com/piccritic/compute/post/PostService.java @@ -12,6 +12,7 @@ import org.hibernate.Hibernate; +import com.piccritic.compute.MasterConnector; import com.piccritic.compute.feedback.FeedbackService; import com.piccritic.compute.feedback.FeedbackServiceInterface; import com.piccritic.database.feedback.Comment; @@ -21,13 +22,12 @@ import com.piccritic.database.post.Album; import com.piccritic.database.post.AlbumConnector; import com.piccritic.database.post.AlbumException; -import com.piccritic.database.post.JPAAlbumConnector; -import com.piccritic.database.post.JPAPostConnector; import com.piccritic.database.post.Post; import com.piccritic.database.post.PostConnector; import com.piccritic.database.post.PostException; import com.piccritic.database.user.Critic; -import com.piccritic.database.user.JPAUserConnector; +import com.piccritic.database.user.JPAUserConnector; +import com.piccritic.database.user.UserConnector; /** * This class implements the PostServiceInterface. * @@ -39,10 +39,18 @@ public class PostService implements PostServiceInterface { public static final String USERS_DIR = "users"; - static PostConnector pc = new JPAPostConnector(); - static AlbumConnector ac = new JPAAlbumConnector(); + static PostConnector pc; + static AlbumConnector ac; + static UserConnector uc; private FeedbackServiceInterface fs = FeedbackService.createService(); + public PostService() { + MasterConnector.init(); + pc = MasterConnector.postConnector; + ac = MasterConnector.albumConnector; + uc = MasterConnector.userConnector; + } + public File getImageFile(String handle) { Path p0 = Paths.get(USERS_DIR, handle); File directory = p0.toFile(); @@ -120,7 +128,7 @@ public Album updateAlbum(Album album) throws AlbumException { } public Album getDefaultAlbum(String handle) { - JPAUserConnector uc = new JPAUserConnector(); + uc = new JPAUserConnector(); Critic user = uc.selectCritic(handle); Set albums = user.getAlbums(); Hibernate.initialize(albums); diff --git a/compute/src/main/java/com/piccritic/compute/user/UserService.java b/compute/src/main/java/com/piccritic/compute/user/UserService.java index 81f3377..0df608e 100644 --- a/compute/src/main/java/com/piccritic/compute/user/UserService.java +++ b/compute/src/main/java/com/piccritic/compute/user/UserService.java @@ -13,6 +13,7 @@ import java.util.HashSet; import java.util.Set; +import com.piccritic.compute.MasterConnector; import com.piccritic.compute.hashing.Hasher; import com.piccritic.database.post.Album; import com.piccritic.database.post.AlbumConnector; @@ -30,6 +31,7 @@ public class UserService implements UserServiceInterface { private static UserServiceInterface instance; private static UserConnector uc; + private static AlbumConnector ac; private static String createSuccess = "Profile successfully created!"; private static String updateSuccess = "Profile successfully updated!"; private static String createFailure = "Profile could not be created."; @@ -38,7 +40,9 @@ public class UserService implements UserServiceInterface { private static String handleInUse = "Handle already in use."; private UserService() { - uc = new JPAUserConnector(); + MasterConnector.init(); + uc = MasterConnector.userConnector; + ac = MasterConnector.albumConnector; } @@ -91,7 +95,7 @@ public String create(Critic critic, String password) throws UserException { critic.setAlbums(albums); try { - AlbumConnector ac = new JPAAlbumConnector(); + ac = new JPAAlbumConnector(); ac.insertAlbum(defaultAlbum); } catch (AlbumException e) { e.printStackTrace(); diff --git a/compute/src/test/java/com/piccritic/compute/feedback/FeedbackServiceTest.java b/compute/src/test/java/com/piccritic/compute/feedback/FeedbackServiceTest.java index 4bf4c09..9842e7f 100644 --- a/compute/src/test/java/com/piccritic/compute/feedback/FeedbackServiceTest.java +++ b/compute/src/test/java/com/piccritic/compute/feedback/FeedbackServiceTest.java @@ -17,6 +17,7 @@ import org.junit.Before; import org.junit.Test; +import com.piccritic.compute.MasterConnector; import com.piccritic.database.feedback.Comment; import com.piccritic.database.feedback.CommentConnector; import com.piccritic.database.feedback.CommentException; @@ -70,17 +71,25 @@ public class FeedbackServiceTest { private AttributionLicense license = new AttributionLicense(); //private JPALicenseConnector lc = new JPALicenseConnector(); //commented out because unused - private UserConnector uc = new JPAUserConnector(); - private PostConnector pc = new JPAPostConnector(); - private AlbumConnector ac = new JPAAlbumConnector(); - private CommentConnector cc = new JPACommentConnector(); - private VoteConnector vc = new JPAVoteConnector(); - private RatingConnector rc = new JPARatingConnector(); + private UserConnector uc; + private PostConnector pc; + private AlbumConnector ac; + private CommentConnector cc; + private VoteConnector vc; + private RatingConnector rc; private FeedbackServiceInterface fs = FeedbackService.createService(); @Before public void init() { + MasterConnector.init(); + uc = MasterConnector.userConnector; + pc = MasterConnector.postConnector; + ac = MasterConnector.albumConnector; + cc = MasterConnector.commentConnector; + vc = MasterConnector.voteConnector; + rc = MasterConnector.ratingConnector; + critic.setHandle("tester"); critic.setFirstName("firstName"); critic.setLastName("lastName"); diff --git a/compute/src/test/java/com/piccritic/compute/post/PostServiceTest.java b/compute/src/test/java/com/piccritic/compute/post/PostServiceTest.java index f4d9bb5..46e2e36 100644 --- a/compute/src/test/java/com/piccritic/compute/post/PostServiceTest.java +++ b/compute/src/test/java/com/piccritic/compute/post/PostServiceTest.java @@ -12,6 +12,7 @@ import org.junit.Before; import org.junit.Test; +import com.piccritic.compute.MasterConnector; import com.piccritic.compute.feedback.FeedbackService; import com.piccritic.compute.feedback.FeedbackServiceInterface; import com.piccritic.database.feedback.Comment; @@ -21,13 +22,10 @@ import com.piccritic.database.post.Album; import com.piccritic.database.post.AlbumConnector; import com.piccritic.database.post.AlbumException; -import com.piccritic.database.post.JPAAlbumConnector; -import com.piccritic.database.post.JPAPostConnector; import com.piccritic.database.post.Post; import com.piccritic.database.post.PostConnector; import com.piccritic.database.post.PostException; import com.piccritic.database.user.Critic; -import com.piccritic.database.user.JPAUserConnector; import com.piccritic.database.user.UserConnector; import com.piccritic.database.user.UserException; @@ -46,15 +44,20 @@ public class PostServiceTest { private Set criticComments = new HashSet(); private Set postComments = new HashSet(); - private UserConnector uc = new JPAUserConnector(); - private PostConnector pc = new JPAPostConnector(); - private AlbumConnector ac = new JPAAlbumConnector(); + private UserConnector uc; + private PostConnector pc; + private AlbumConnector ac; private PostService ps = new PostService(); private FeedbackServiceInterface fs = FeedbackService.createService(); @Before public void setup() throws Exception{ + MasterConnector.init(); + uc = MasterConnector.userConnector; + pc = MasterConnector.postConnector; + ac = MasterConnector.albumConnector; + album = new Album(); post = new Post(); postSet = new HashSet(); @@ -67,7 +70,7 @@ public void setup() throws Exception{ critic.setLastName("lastName"); critic.setJoinDate(new Date(0)); critic.setLicense(new AttributionLicense()); - critic.setHandle("handle"); + critic.setHandle("handlePST"); critic.setComments(criticComments); albumSet.add(album); @@ -91,16 +94,16 @@ public void setup() throws Exception{ comment.setScore(0); critic = uc.insertCritic(critic, "hash"); - ac.insertAlbum(album); + album = ac.insertAlbum(album); post.setPath("path"); } @Test public void testCreatePost() { try { - Post created = ps.createPost(post); - assertNotNull( created ); - assertNotNull( created.getUploadDate()); + ps.createPost(post); + assertNotNull( post ); + assertNotNull( post.getUploadDate()); } catch(PostException | AlbumException e){ fail(e.getLocalizedMessage()); } @@ -130,7 +133,8 @@ public void testDeletePost(){ comment = fs.insertComment(comment); ps.deletePost(created); post.setComments(new HashSet()); - pc.insertPost(post); + post.setUploadDate(null); + ps.createPost(post); } catch(PostException | AlbumException | CommentException | VoteException e) { e.printStackTrace(); fail(e.getLocalizedMessage()); @@ -141,7 +145,7 @@ public void testDeletePost(){ public void testGetImageFile(){ //TODO test later... try { - ps.createPost(post); + post = ps.createPost(post); } catch (PostException | AlbumException e) { fail(e.getLocalizedMessage()); } diff --git a/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java b/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java index 4f23194..abb8fdc 100644 --- a/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java +++ b/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java @@ -82,7 +82,7 @@ protected void validate(Post post) throws PostException { try { super.validate(post); } catch(Exception e) { - throw new PostException(e.getMessage()); + throw new PostException(e.getLocalizedMessage()); } } diff --git a/database/src/test/java/com/piccritic/database/post/PostConnectorTest.java b/database/src/test/java/com/piccritic/database/post/PostConnectorTest.java index 95ae156..6375bdf 100644 --- a/database/src/test/java/com/piccritic/database/post/PostConnectorTest.java +++ b/database/src/test/java/com/piccritic/database/post/PostConnectorTest.java @@ -45,7 +45,7 @@ public class PostConnectorTest { Critic critic = new Critic(); private String firstName = "firstName"; - private String handle = "handle"; + private String handle = "handlePCT"; private String lastName = "lastName"; private String hash = "hash"; private String albumName = "album"; diff --git a/website/src/main/java/com/piccritic/website/DefaultView.java b/website/src/main/java/com/piccritic/website/DefaultView.java index e2688b4..a0f4a9b 100644 --- a/website/src/main/java/com/piccritic/website/DefaultView.java +++ b/website/src/main/java/com/piccritic/website/DefaultView.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; +import com.piccritic.compute.MasterService; import com.piccritic.database.post.Post; import com.piccritic.database.post.PostException; import com.vaadin.navigator.View; @@ -22,7 +23,8 @@ public class DefaultView extends PostQuickView implements View { public DefaultView(){ try { - posts.addAll(PicCritic.postService.getPosts(9)); + //TODO + posts.addAll(MasterService.postService.getPosts(9)); } catch (PostException e) { Notification.show(e.getLocalizedMessage(), Type.WARNING_MESSAGE); e.printStackTrace(); diff --git a/website/src/main/java/com/piccritic/website/PicCritic.java b/website/src/main/java/com/piccritic/website/PicCritic.java index 0bb646b..2cb630c 100644 --- a/website/src/main/java/com/piccritic/website/PicCritic.java +++ b/website/src/main/java/com/piccritic/website/PicCritic.java @@ -2,6 +2,9 @@ import javax.servlet.annotation.WebServlet; +import com.piccritic.compute.MasterService; +import com.piccritic.compute.feedback.FeedbackService; +import com.piccritic.compute.feedback.FeedbackServiceInterface; import com.piccritic.compute.license.LicenseService; import com.piccritic.compute.license.LicenseServiceInterface; import com.piccritic.compute.post.PostService; @@ -27,12 +30,10 @@ public class PicCritic extends UI { private static final long serialVersionUID = 1L; - public static LicenseServiceInterface license = new LicenseService(); - public static PostServiceInterface postService = new PostService(); - public static UserServiceInterface userService = UserService.createService(); @Override protected void init(VaadinRequest vaadinRequest) { + MasterService.init(); setContent(new Home()); } diff --git a/website/src/main/java/com/piccritic/website/feedback/CommentForm.java b/website/src/main/java/com/piccritic/website/feedback/CommentForm.java index 7be1f5b..93a04e7 100644 --- a/website/src/main/java/com/piccritic/website/feedback/CommentForm.java +++ b/website/src/main/java/com/piccritic/website/feedback/CommentForm.java @@ -1,5 +1,6 @@ package com.piccritic.website.feedback; +import com.piccritic.compute.MasterService; import com.piccritic.compute.feedback.FeedbackServiceInterface; import com.piccritic.compute.user.UserService; import com.piccritic.compute.user.UserServiceInterface; @@ -27,7 +28,7 @@ public class CommentForm extends FormLayout { } }); private Post post; - private UserServiceInterface us = UserService.createService(); + private UserServiceInterface us = MasterService.userService; private FeedbackServiceInterface fs; public CommentForm(FeedbackServiceInterface fs) { diff --git a/website/src/main/java/com/piccritic/website/post/CreatePost.java b/website/src/main/java/com/piccritic/website/post/CreatePost.java index 84c4e2d..15d4739 100644 --- a/website/src/main/java/com/piccritic/website/post/CreatePost.java +++ b/website/src/main/java/com/piccritic/website/post/CreatePost.java @@ -3,7 +3,9 @@ import java.util.HashSet; import java.util.Set; +import com.piccritic.compute.MasterService; import com.piccritic.compute.post.PostService; +import com.piccritic.compute.post.PostServiceInterface; import com.piccritic.database.post.Album; import com.piccritic.database.post.AlbumException; import com.piccritic.database.post.Post; @@ -24,7 +26,6 @@ import com.vaadin.ui.TextField; import com.vaadin.ui.UI; import com.vaadin.ui.Upload; -import com.vaadin.ui.Upload.ChangeListener; import com.vaadin.ui.Upload.SucceededEvent; import com.vaadin.ui.Upload.SucceededListener; import com.vaadin.ui.Window; @@ -48,7 +49,7 @@ public class CreatePost extends Window implements SucceededListener { private ImageReceiver receiver; private Image image = new Image("Uploaded Image"); private Button confirm = new Button("Confirm", this::confirmUpload); - private PostService service = new PostService(); + private PostServiceInterface service = MasterService.postService; private String handle; private FormLayout form = new FormLayout(); diff --git a/website/src/main/java/com/piccritic/website/post/ImageReceiver.java b/website/src/main/java/com/piccritic/website/post/ImageReceiver.java index 66180ff..feffccb 100644 --- a/website/src/main/java/com/piccritic/website/post/ImageReceiver.java +++ b/website/src/main/java/com/piccritic/website/post/ImageReceiver.java @@ -4,8 +4,8 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.OutputStream; -import java.net.MalformedURLException; +import com.piccritic.compute.MasterService; import com.piccritic.database.post.Post; import com.piccritic.website.PicCritic; import com.vaadin.ui.Notification; @@ -52,10 +52,6 @@ public OutputStream receiveUpload(String filename, String mimeType) { if (ui == null) { return null; } - - if (ui.postService == null) { - return null; - } if (filename == null || filename.equals("")) { return null; @@ -63,7 +59,7 @@ public OutputStream receiveUpload(String filename, String mimeType) { System.out.println(post.getPath()); if (post == null || post.getPath() == null) { - file = ui.postService.getImageFile(handle); + file = MasterService.postService.getImageFile(handle); post.setPath(file.getPath().replaceAll("\\\\", "/")); } FileOutputStream fos; diff --git a/website/src/main/java/com/piccritic/website/post/ViewPost.java b/website/src/main/java/com/piccritic/website/post/ViewPost.java index a44e888..bf8826f 100644 --- a/website/src/main/java/com/piccritic/website/post/ViewPost.java +++ b/website/src/main/java/com/piccritic/website/post/ViewPost.java @@ -3,9 +3,11 @@ import java.io.File; import java.util.List; +import com.piccritic.compute.MasterService; import com.piccritic.compute.feedback.FeedbackService; import com.piccritic.compute.feedback.FeedbackServiceInterface; import com.piccritic.compute.post.PostService; +import com.piccritic.compute.post.PostServiceInterface; import com.piccritic.compute.user.UserService; import com.piccritic.compute.user.UserServiceInterface; import com.piccritic.database.feedback.Comment; @@ -43,8 +45,8 @@ public class ViewPost extends VerticalLayout implements View { private Label postDescription = new Label(); private RatingComponent ratings = new RatingComponent(); private List comments; - private UserServiceInterface us = UserService.createService(); - private FeedbackServiceInterface fs = FeedbackService.createService(); + private UserServiceInterface us = MasterService.userService; + private FeedbackServiceInterface fs = MasterService.feedbackService; private CommentForm commentForm = new CommentForm(fs); private Label license = new Label(); @@ -65,7 +67,7 @@ public ViewPost() { @Override public void enter(ViewChangeEvent event) { - PostService service = new PostService(); + PostServiceInterface service = MasterService.postService; post = service.getPost(event.getParameters()); if (post == null) { Notification.show("Error 404"+event.getParameters(), Type.ERROR_MESSAGE); diff --git a/website/src/main/java/com/piccritic/website/user/UserForm.java b/website/src/main/java/com/piccritic/website/user/UserForm.java index 8febb60..8caa4e2 100644 --- a/website/src/main/java/com/piccritic/website/user/UserForm.java +++ b/website/src/main/java/com/piccritic/website/user/UserForm.java @@ -4,6 +4,7 @@ */ package com.piccritic.website.user; +import com.piccritic.compute.MasterService; import com.piccritic.database.user.Critic; import com.piccritic.database.user.UserException; import com.piccritic.website.PicCritic; @@ -54,7 +55,7 @@ public class UserForm extends FormLayout { public UserForm(String userHandle) { newProfile = (userHandle == null); handle.setValue((userHandle == null) ? "" : userHandle); - critic = PicCritic.userService.select(userHandle); + critic = MasterService.userService.select(userHandle); if (critic != null) { firstName.setValue(critic.getFirstName()); lastName.setValue(critic.getLastName()); @@ -101,9 +102,9 @@ public void save(Button.ClickEvent e) { try { if (newProfile) { - status = PicCritic.userService.create(critic, password.getValue()); + status = MasterService.userService.create(critic, password.getValue()); } else { - status = PicCritic.userService.update(critic, password.getValue()); + status = MasterService.userService.update(critic, password.getValue()); } } catch (UserException ue) { Notification.show(ue.getLocalizedMessage(), Type.WARNING_MESSAGE); diff --git a/website/src/main/java/com/piccritic/website/user/UserView.java b/website/src/main/java/com/piccritic/website/user/UserView.java index d80c098..a74dcdb 100644 --- a/website/src/main/java/com/piccritic/website/user/UserView.java +++ b/website/src/main/java/com/piccritic/website/user/UserView.java @@ -1,6 +1,10 @@ package com.piccritic.website.user; -import com.piccritic.compute.user.UserService; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import com.piccritic.compute.MasterService; import com.piccritic.compute.user.UserServiceInterface; import com.piccritic.database.post.Album; import com.piccritic.database.post.Post; @@ -11,10 +15,6 @@ import com.vaadin.ui.Notification; import com.vaadin.ui.Notification.Type; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - /** * This class implements the users posts page * @@ -31,7 +31,7 @@ public class UserView extends PostQuickView implements View { @Override public void enter(ViewChangeEvent event) { if (event.getParameters() != null && event.getParameters() != "") { - UserServiceInterface service = UserService.createService(); + UserServiceInterface service = MasterService.userService; Critic critic = service.select(event.getParameters()); if (critic != null) { Iterator i = critic.getAlbums().iterator(); diff --git a/website/src/test/java/com/piccritic/website/PicCriticIT.java b/website/src/test/java/com/piccritic/website/PicCriticIT.java index 91784ae..821afc1 100644 --- a/website/src/test/java/com/piccritic/website/PicCriticIT.java +++ b/website/src/test/java/com/piccritic/website/PicCriticIT.java @@ -4,10 +4,11 @@ import org.junit.Rule; import org.openqa.selenium.phantomjs.PhantomJSDriver; -import com.piccritic.compute.user.UserService; +import com.piccritic.compute.MasterConnector; +import com.piccritic.compute.MasterService; import com.piccritic.compute.user.UserServiceInterface; import com.piccritic.database.license.AttributionLicense; -import com.piccritic.database.license.JPALicenseConnector; +import com.piccritic.database.license.LicenseConnector; import com.piccritic.database.user.UserException; import com.vaadin.testbench.ScreenshotOnFailureRule; import com.vaadin.testbench.TestBenchTestCase; @@ -21,8 +22,8 @@ * @author Damien
Francis bosse */ public class PicCriticIT extends TestBenchTestCase { - public UserServiceInterface userService = UserService.createService(); - public JPALicenseConnector lc = new JPALicenseConnector(); + public UserServiceInterface userService = MasterService.userService; + public LicenseConnector lc = MasterConnector.licenseConnector; @Rule public ScreenshotOnFailureRule screenshotOnFailureRule = diff --git a/website/src/test/java/com/piccritic/website/post/AddPostComponentsIT.java b/website/src/test/java/com/piccritic/website/post/AddPostComponentsIT.java index 13bd8c9..0311c1a 100644 --- a/website/src/test/java/com/piccritic/website/post/AddPostComponentsIT.java +++ b/website/src/test/java/com/piccritic/website/post/AddPostComponentsIT.java @@ -47,7 +47,7 @@ public class AddPostComponentsIT extends PicCriticIT { Vote vote = new Vote(); private String firstName = "firstName"; - private String handle = "handle"; + private String handle = "handleAPCIT"; private String lastName = "lastName"; private String hash = "hash"; private String albumName = "album"; diff --git a/website/src/test/java/com/piccritic/website/user/ViewUserPostsIT.java b/website/src/test/java/com/piccritic/website/user/ViewUserPostsIT.java index 6dfb774..4d2d6d3 100644 --- a/website/src/test/java/com/piccritic/website/user/ViewUserPostsIT.java +++ b/website/src/test/java/com/piccritic/website/user/ViewUserPostsIT.java @@ -46,7 +46,7 @@ public class ViewUserPostsIT extends PicCriticIT { Critic critic = new Critic(); private String firstName = "firstName"; - private String handle = "handle"; + private String handle = "handleVUPIT"; private String lastName = "lastName"; private String hash = "hash"; private String albumName = "album"; From 1648cc9dd03929beec7047757b62ccaac4481ded Mon Sep 17 00:00:00 2001 From: ajsteadly Date: Mon, 3 Apr 2017 09:11:26 -0300 Subject: [PATCH 07/12] Minimally functional sorting feature for posts view. Can currently sort by title and upload date (precision to upload day, not time). @author ajsteadly & rezdamir --- .../piccritic/compute/post/PostService.java | 6 +++ .../compute/post/PostServiceInterface.java | 12 ++++- .../database/post/JPAPostConnector.java | 29 ++++++++++ .../database/post/PostConnector.java | 16 +++++- .../database/post/PostConnectorTest.java | 53 +++++++++++++++---- .../com/piccritic/website/DefaultView.java | 22 +++++++- .../com/piccritic/website/PostQuickView.java | 33 +++++++++++- 7 files changed, 158 insertions(+), 13 deletions(-) diff --git a/compute/src/main/java/com/piccritic/compute/post/PostService.java b/compute/src/main/java/com/piccritic/compute/post/PostService.java index ef0b8da..0a352ae 100644 --- a/compute/src/main/java/com/piccritic/compute/post/PostService.java +++ b/compute/src/main/java/com/piccritic/compute/post/PostService.java @@ -25,6 +25,7 @@ import com.piccritic.database.post.Post; import com.piccritic.database.post.PostConnector; import com.piccritic.database.post.PostException; +import com.piccritic.database.post.PostConnector.PostSortOption; import com.piccritic.database.user.Critic; import com.piccritic.database.user.JPAUserConnector; import com.piccritic.database.user.UserConnector; @@ -151,4 +152,9 @@ public List getPosts(int number) throws PostException { return pc.getPosts(number); } + + public List getPosts(int number, PostSortOption option) throws PostException { + return pc.getPosts(number, option); + } + } diff --git a/compute/src/main/java/com/piccritic/compute/post/PostServiceInterface.java b/compute/src/main/java/com/piccritic/compute/post/PostServiceInterface.java index 448df32..5b12564 100644 --- a/compute/src/main/java/com/piccritic/compute/post/PostServiceInterface.java +++ b/compute/src/main/java/com/piccritic/compute/post/PostServiceInterface.java @@ -9,6 +9,7 @@ import com.piccritic.database.post.AlbumException; import com.piccritic.database.post.Post; import com.piccritic.database.post.PostException; +import com.piccritic.database.post.PostConnector.PostSortOption; /** * @@ -18,7 +19,7 @@ * */ public interface PostServiceInterface { - + /** * Returns a FileOutputStream where the current file should be stored. Used * by the front-end to store an Image object. @@ -84,4 +85,13 @@ public interface PostServiceInterface { * @throws PostException */ public List getPosts(int number) throws PostException; + + /** + * Gets a specified number of posts from the database, sorted by option. + * @param number of posts to get. + * @param option to sort posts by. + * @return sorted list of posts from the database. + * @throws PostException + */ + public List getPosts(int number, PostSortOption option) throws PostException; } diff --git a/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java b/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java index abb8fdc..ee0e4a6 100644 --- a/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java +++ b/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java @@ -122,4 +122,33 @@ public List getPosts(int number) throws PostException { return null; } } + + /** + * Gets a specified number of posts from the database, sorted by option. + * @param number of posts to get. + * @param option to sort posts by. + * @return sorted list of posts from the database. + * @throws PostException + */ + public List getPosts(int number, PostSortOption option) throws PostException { + try { + TypedQuery q; + switch (option) { + case UPLOAD_DATE: + q = entity.createQuery("SELECT c from Post c ORDER BY c.uploadDate", Post.class); + break; + case TITLE: + q = entity.createQuery("SELECT c from Post c ORDER BY c.title", Post.class); + break; + default: + q = entity.createQuery("SELECT c from Post c", Post.class); + break; + } + q.setMaxResults(number); + return q.getResultList(); + } catch (Exception e) { + System.out.println(e.toString()); + return null; + } + } } diff --git a/database/src/main/java/com/piccritic/database/post/PostConnector.java b/database/src/main/java/com/piccritic/database/post/PostConnector.java index b00af35..a7a86a2 100644 --- a/database/src/main/java/com/piccritic/database/post/PostConnector.java +++ b/database/src/main/java/com/piccritic/database/post/PostConnector.java @@ -18,6 +18,11 @@ */ public interface PostConnector { + public enum PostSortOption { + UPLOAD_DATE, + TITLE + } + /** * Inserts the specified Post into the database. * @@ -61,11 +66,20 @@ public interface PostConnector { */ public List getPosts(Critic critic); - /* + /** * Gets a specified number of posts from the database. * @param number of posts to get. * @return list of posts from the database. * @throws PostException */ public List getPosts(int number) throws PostException; + + /** + * Gets a specified number of posts from the database, sorted by option. + * @param number of posts to get. + * @param option to sort posts by. + * @return sorted list of posts from the database. + * @throws PostException + */ + public List getPosts(int number, PostSortOption option) throws PostException; } diff --git a/database/src/test/java/com/piccritic/database/post/PostConnectorTest.java b/database/src/test/java/com/piccritic/database/post/PostConnectorTest.java index 6375bdf..22b7ba9 100644 --- a/database/src/test/java/com/piccritic/database/post/PostConnectorTest.java +++ b/database/src/test/java/com/piccritic/database/post/PostConnectorTest.java @@ -11,6 +11,7 @@ import java.sql.Date; import java.util.HashSet; +import java.util.List; import java.util.Set; import org.junit.After; @@ -21,6 +22,7 @@ import com.piccritic.database.feedback.Rating; import com.piccritic.database.license.AttributionNonCommercialLicense; import com.piccritic.database.license.JPALicenseConnector; +import com.piccritic.database.post.PostConnector.PostSortOption; import com.piccritic.database.user.Critic; import com.piccritic.database.user.JPAUserConnector; import com.piccritic.database.user.UserConnector; @@ -55,7 +57,12 @@ public class PostConnectorTest { private String path2 = "/path2"; private String path3 = "/path3"; private String path4 = "/path4"; + private long day = 24*3600000; private Date date = new Date(0); + private Date date1 = new Date(day); + private Date date2 = new Date(2*day); + private Date date3 = new Date(3*day); + private Date date4 = new Date(4*day); private Set postSet = new HashSet(); private Set albumSet = new HashSet(); @@ -68,6 +75,7 @@ public class PostConnectorTest { @Before public void init() { + new JPALicenseConnector(); postSet.add(post); postSet.add(post1); @@ -87,26 +95,26 @@ public void init() { album.setPosts(postSet); album.setName(albumName); - post.setUploadDate(date); - post.setTitle(postTitle); + post.setUploadDate(date4); + post.setTitle("Epsilon"); post.setDescription("description"); post.setComments(commentSet); post.setRatings(ratingSet); - post1.setUploadDate(date); - post1.setTitle(postTitle); + post1.setUploadDate(date2); + post1.setTitle("Delta"); post1.setDescription("description"); post2.setUploadDate(date); - post2.setTitle(postTitle); + post2.setTitle("Cookie"); post2.setDescription("description"); - post3.setUploadDate(date); - post3.setTitle(postTitle); + post3.setUploadDate(date1); + post3.setTitle("Beta"); post3.setDescription("description"); - post4.setUploadDate(date); - post4.setTitle(postTitle); + post4.setUploadDate(date3); + post4.setTitle("Alpha"); post4.setDescription("description"); try { uc.insertCritic(critic, hash); @@ -219,6 +227,33 @@ public void testGetPostNumber() { } } + @Test + public void testGetPostSortTitle() { + try { + List posts = pc.getPosts(5, PostSortOption.TITLE); + assertEquals(post4, posts.get(0)); + assertEquals(post3, posts.get(1)); + assertEquals(post2, posts.get(2)); + assertEquals(post1, posts.get(3)); + assertEquals(post, posts.get(4)); + } catch (PostException e) { + fail(e.getLocalizedMessage()); + } + } + + @Test + public void testGetPostSortDate() { + try { + List posts = pc.getPosts(5, PostSortOption.UPLOAD_DATE); + assertEquals(post2, posts.get(0)); + assertEquals(post3, posts.get(1)); + assertEquals(post1, posts.get(2)); + assertEquals(post4, posts.get(3)); + assertEquals(post, posts.get(4)); + } catch (PostException e) { + fail(e.getLocalizedMessage()); + } + } @After public void tearDown() { diff --git a/website/src/main/java/com/piccritic/website/DefaultView.java b/website/src/main/java/com/piccritic/website/DefaultView.java index a0f4a9b..7edcbc2 100644 --- a/website/src/main/java/com/piccritic/website/DefaultView.java +++ b/website/src/main/java/com/piccritic/website/DefaultView.java @@ -6,6 +6,7 @@ import com.piccritic.compute.MasterService; import com.piccritic.database.post.Post; +import com.piccritic.database.post.PostConnector.PostSortOption; import com.piccritic.database.post.PostException; import com.vaadin.navigator.View; import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent; @@ -38,7 +39,26 @@ public DefaultView(){ @Override public void enter(ViewChangeEvent event) { - //addComponent(new Label("Welcome to Pic Critic!!")); + String sortOption = event.getParameters(); + PostSortOption option = null; + + if (sortOption.equals("Title")) { + option = PostSortOption.TITLE; + } else if (sortOption.equals("UploadDate")) { + option = PostSortOption.UPLOAD_DATE; + } + posts.clear(); + + try { + if (option != null) { + posts.addAll(MasterService.postService.getPosts(9, option)); + } else { + posts.addAll(MasterService.postService.getPosts(9)); + } + } catch (PostException e) { + e.printStackTrace(); + } + initPosts(posts); } } diff --git a/website/src/main/java/com/piccritic/website/PostQuickView.java b/website/src/main/java/com/piccritic/website/PostQuickView.java index 16f328c..f633f7f 100644 --- a/website/src/main/java/com/piccritic/website/PostQuickView.java +++ b/website/src/main/java/com/piccritic/website/PostQuickView.java @@ -1,12 +1,16 @@ package com.piccritic.website; import java.io.File; +import java.util.ArrayList; import java.util.Iterator; import java.util.List; import com.piccritic.database.post.Post; +import com.vaadin.data.Property.ValueChangeEvent; +import com.vaadin.data.Property.ValueChangeListener; import com.vaadin.server.FileResource; import com.vaadin.ui.Alignment; +import com.vaadin.ui.ComboBox; import com.vaadin.ui.GridLayout; import com.vaadin.ui.Image; import com.vaadin.ui.Label; @@ -19,13 +23,40 @@ public class PostQuickView extends GridLayout { Image image; public PostQuickView() { - super(3, 3); + super(4, 3); setSizeFull(); setHeightUndefined(); } public void initPosts(List posts) { + this.removeAllComponents(); + System.out.println("does this happen twice?"); + + //add sorting options combo box + List sortOptions = new ArrayList<>(); + sortOptions.add("Title"); + sortOptions.add("Upload Date"); + + ComboBox select = new ComboBox("Select sorting option"); + select.addItems(sortOptions); + + ValueChangeListener listener = new ValueChangeListener() { + + @Override + public void valueChange(ValueChangeEvent event) { + String option = (String) event.getProperty().getValue(); + option = option.replaceAll(" ", ""); + UI.getCurrent().getPage().setLocation("#!home/" + option); + UI.getCurrent().getPage().reload(); + } + }; + + select.addValueChangeListener(listener); + + addComponent(select, 0, 0, 3, 1); + + //add posts Iterator it = posts.iterator(); for (int y = 0; y < 9; y++) { if (it.hasNext()) { From 48105e93254ac8ac7eed29f4dc2710581861b389 Mon Sep 17 00:00:00 2001 From: ajsteadly Date: Mon, 3 Apr 2017 12:43:10 -0300 Subject: [PATCH 08/12] Added sorting by license @author ajsteadly & rezdamir --- .../database/post/JPAPostConnector.java | 12 +++++-- .../database/post/PostConnector.java | 3 +- .../database/post/PostConnectorTest.java | 31 +++++++++++++++++-- .../com/piccritic/website/DefaultView.java | 2 ++ .../com/piccritic/website/PostQuickView.java | 1 + 5 files changed, 42 insertions(+), 7 deletions(-) diff --git a/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java b/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java index ee0e4a6..ef0b6c2 100644 --- a/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java +++ b/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java @@ -135,13 +135,19 @@ public List getPosts(int number, PostSortOption option) throws PostExcepti TypedQuery q; switch (option) { case UPLOAD_DATE: - q = entity.createQuery("SELECT c from Post c ORDER BY c.uploadDate", Post.class); + q = entity.createQuery("SELECT p FROM Post p ORDER BY p.uploadDate", Post.class); break; case TITLE: - q = entity.createQuery("SELECT c from Post c ORDER BY c.title", Post.class); + q = entity.createQuery("SELECT p FROM Post p ORDER BY p.title", Post.class); break; + case LICENSE: + q = entity.createQuery("SELECT p FROM Post p LEFT JOIN p.license l ORDER BY l.licenseType", Post.class); + break; +// case NUM_COMMENTS: +// q = entity.createQuery("SELECT p FROM Post p JOIN p.comments comments GROUP BY p.path ORDER BY COUNT(p.path)", Post.class); +// break; default: - q = entity.createQuery("SELECT c from Post c", Post.class); + q = entity.createQuery("SELECT p FROM Post p", Post.class); break; } q.setMaxResults(number); diff --git a/database/src/main/java/com/piccritic/database/post/PostConnector.java b/database/src/main/java/com/piccritic/database/post/PostConnector.java index a7a86a2..b0faa00 100644 --- a/database/src/main/java/com/piccritic/database/post/PostConnector.java +++ b/database/src/main/java/com/piccritic/database/post/PostConnector.java @@ -20,7 +20,8 @@ public interface PostConnector { public enum PostSortOption { UPLOAD_DATE, - TITLE + TITLE, + LICENSE } /** diff --git a/database/src/test/java/com/piccritic/database/post/PostConnectorTest.java b/database/src/test/java/com/piccritic/database/post/PostConnectorTest.java index 22b7ba9..9b7b362 100644 --- a/database/src/test/java/com/piccritic/database/post/PostConnectorTest.java +++ b/database/src/test/java/com/piccritic/database/post/PostConnectorTest.java @@ -10,6 +10,7 @@ import static org.junit.Assert.fail; import java.sql.Date; +import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -22,6 +23,8 @@ import com.piccritic.database.feedback.Rating; import com.piccritic.database.license.AttributionNonCommercialLicense; import com.piccritic.database.license.JPALicenseConnector; +import com.piccritic.database.license.License; +import com.piccritic.database.license.LicenseConnector; import com.piccritic.database.post.PostConnector.PostSortOption; import com.piccritic.database.user.Critic; import com.piccritic.database.user.JPAUserConnector; @@ -45,6 +48,7 @@ public class PostConnectorTest { Post post4 = new Post(); Album album = new Album(); Critic critic = new Critic(); + Comment comments[] = new Comment[10]; private String firstName = "firstName"; private String handle = "handlePCT"; @@ -72,11 +76,12 @@ public class PostConnectorTest { PostConnector pc = new JPAPostConnector(); AlbumConnector ac = new JPAAlbumConnector(); UserConnector uc = new JPAUserConnector(); + LicenseConnector lc = new JPALicenseConnector(); @Before public void init() { - new JPALicenseConnector(); + postSet.add(post); postSet.add(post1); postSet.add(post2); @@ -98,30 +103,34 @@ public void init() { post.setUploadDate(date4); post.setTitle("Epsilon"); post.setDescription("description"); - post.setComments(commentSet); post.setRatings(ratingSet); + post.setComments(commentSet); + post.setLicense(lc.selectLicense(License.ATTRIBUTION_NON_COMMERCIAL_NO_DERIVS)); post1.setUploadDate(date2); post1.setTitle("Delta"); post1.setDescription("description"); + post1.setLicense(lc.selectLicense(License.ATTRIBUTION_NON_COMMERCIAL)); post2.setUploadDate(date); post2.setTitle("Cookie"); post2.setDescription("description"); + post2.setLicense(lc.selectLicense(License.ATTRIBUTION_NO_DERIVS)); post3.setUploadDate(date1); post3.setTitle("Beta"); post3.setDescription("description"); + post3.setLicense(lc.selectLicense(License.ATTRIBUTION_NON_COMMERCIAL_SHAREALIKE)); post4.setUploadDate(date3); post4.setTitle("Alpha"); post4.setDescription("description"); + post4.setLicense(lc.selectLicense(License.ATTRIBUTION_SHAREALIKE)); try { uc.insertCritic(critic, hash); album.setCritic(critic); ac.insertAlbum(album); post.setPath(path); - post.setLicense(new AttributionNonCommercialLicense()); post1.setPath(path1); post2.setPath(path2); post3.setPath(path3); @@ -136,6 +145,7 @@ public void init() { pc.insertPost(post2); pc.insertPost(post3); pc.insertPost(post4); + } catch (UserException|PostException|AlbumException e) { fail(e.getMessage()); } @@ -255,6 +265,21 @@ public void testGetPostSortDate() { } } + @Test + public void testGetPostSortLicense() { + try { + List posts = pc.getPosts(5, PostSortOption.LICENSE); + + assertEquals(post2, posts.get(0)); + assertEquals(post1, posts.get(1)); + assertEquals(post, posts.get(2)); + assertEquals(post3, posts.get(3)); + assertEquals(post4, posts.get(4)); + } catch (PostException e) { + fail(e.getLocalizedMessage()); + } + } + @After public void tearDown() { try { diff --git a/website/src/main/java/com/piccritic/website/DefaultView.java b/website/src/main/java/com/piccritic/website/DefaultView.java index 7edcbc2..a9e94e2 100644 --- a/website/src/main/java/com/piccritic/website/DefaultView.java +++ b/website/src/main/java/com/piccritic/website/DefaultView.java @@ -46,6 +46,8 @@ public void enter(ViewChangeEvent event) { option = PostSortOption.TITLE; } else if (sortOption.equals("UploadDate")) { option = PostSortOption.UPLOAD_DATE; + } else if (sortOption.equals("License")) { + option = PostSortOption.LICENSE; } posts.clear(); diff --git a/website/src/main/java/com/piccritic/website/PostQuickView.java b/website/src/main/java/com/piccritic/website/PostQuickView.java index f633f7f..a20e111 100644 --- a/website/src/main/java/com/piccritic/website/PostQuickView.java +++ b/website/src/main/java/com/piccritic/website/PostQuickView.java @@ -37,6 +37,7 @@ public void initPosts(List posts) { List sortOptions = new ArrayList<>(); sortOptions.add("Title"); sortOptions.add("Upload Date"); + sortOptions.add("License"); ComboBox select = new ComboBox("Select sorting option"); select.addItems(sortOptions); From 200e973cc357ebfa7feebb5ec1a4db930e204201 Mon Sep 17 00:00:00 2001 From: ian-dawson Date: Fri, 31 Mar 2017 14:19:02 -0300 Subject: [PATCH 09/12] Added Tag entity. Added Tag tests. Authors: damienr74, ian-dawson --- .../com/piccritic/database/post/Post.java | 17 +++++- .../database/tag/JPATagConnector.java | 45 +++++++++++++++ .../java/com/piccritic/database/tag/Tag.java | 56 +++++++++++++++++++ .../piccritic/database/tag/TagConnector.java | 37 ++++++++++++ .../piccritic/database/tag/TagException.java | 9 +++ .../database/tag/TagConnectorTest.java | 50 +++++++++++++++++ 6 files changed, 212 insertions(+), 2 deletions(-) create mode 100644 database/src/main/java/com/piccritic/database/tag/JPATagConnector.java create mode 100644 database/src/main/java/com/piccritic/database/tag/Tag.java create mode 100644 database/src/main/java/com/piccritic/database/tag/TagConnector.java create mode 100644 database/src/main/java/com/piccritic/database/tag/TagException.java create mode 100644 database/src/test/java/com/piccritic/database/tag/TagConnectorTest.java diff --git a/database/src/main/java/com/piccritic/database/post/Post.java b/database/src/main/java/com/piccritic/database/post/Post.java index 03947c0..c048169 100644 --- a/database/src/main/java/com/piccritic/database/post/Post.java +++ b/database/src/main/java/com/piccritic/database/post/Post.java @@ -10,6 +10,7 @@ import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; +import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.validation.constraints.NotNull; @@ -20,6 +21,7 @@ import com.piccritic.database.feedback.Comment; import com.piccritic.database.feedback.Rating; import com.piccritic.database.license.License; +import com.piccritic.database.tag.Tag; /** * this class is a Java bean that defines the database table @@ -40,7 +42,7 @@ public class Post { private Date uploadDate; private String title; private String description; - + @ManyToOne(optional=true, fetch=FetchType.EAGER) private License license; @ManyToOne(optional=false) @@ -52,6 +54,9 @@ public class Post { @OneToMany(fetch=FetchType.EAGER, mappedBy="post") private Set ratings; + + @ManyToMany(fetch=FetchType.EAGER, mappedBy="posts") + private Set tags; public String getPath() { return path; @@ -123,9 +128,17 @@ public void setLicense(License license) { this.license = license; } + public Set getTags() { + return tags; + } + + public void setTags(Set tags) { + this.tags = tags; + } + public String toString() { return String.format("Post{path=%s, uploadDate=%s, title=%s, description=%s, album.id=%d}", - /*(id == null) ? null:id.longValue(),*/ path, (uploadDate == null) ? null :uploadDate.toString(), + path, (uploadDate == null) ? null :uploadDate.toString(), title, description, (album == null) ? null : album.getId()); } diff --git a/database/src/main/java/com/piccritic/database/tag/JPATagConnector.java b/database/src/main/java/com/piccritic/database/tag/JPATagConnector.java new file mode 100644 index 0000000..ab84797 --- /dev/null +++ b/database/src/main/java/com/piccritic/database/tag/JPATagConnector.java @@ -0,0 +1,45 @@ +/** + * @author damienr74, ian-dawson + */ + +package com.piccritic.database.tag; + +import java.util.List; + +import javax.persistence.TypedQuery; + +import com.piccritic.database.JPAConnector; + +public class JPATagConnector extends JPAConnector implements TagConnector { + + public JPATagConnector() { + super(Tag.class); + } + + @Override + public void insertTag(Tag tag) throws TagException { + validate(tag); + container.addEntity(tag); + } + + @Override + public List findTags(String query) throws TagException { + String q = "SELECT t FROM Tag t WHERE t.tag LIKE CONCAT('%', :query, '%')"; + TypedQuery t = entity.createQuery(q, Tag.class).setParameter("query", query); + return t.getResultList(); + } + + @Override + public boolean deleteTag(Tag tag) throws TagException { + return container.removeItem(tag.getTag()); + } + + protected void validate(Tag tag) throws TagException { + try { + super.validate(tag); + } catch (Exception e) { + throw new TagException(e.getLocalizedMessage()); + } + } + +} diff --git a/database/src/main/java/com/piccritic/database/tag/Tag.java b/database/src/main/java/com/piccritic/database/tag/Tag.java new file mode 100644 index 0000000..e43302f --- /dev/null +++ b/database/src/main/java/com/piccritic/database/tag/Tag.java @@ -0,0 +1,56 @@ +/** + * @author damienr74, ian-dawson + */ + +package com.piccritic.database.tag; + +import java.util.Set; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.ManyToMany; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; + +import com.piccritic.database.post.Post; + + +@Entity +public class Tag { + + @Id + @Size(max=15) + @NotNull + @Pattern(regexp="[a-z]+") + private String tag; + + @ManyToMany(fetch=FetchType.EAGER) + private Set posts; + + public String getTag() { + return tag; + } + + + + public void setTag(String tag) { + + this.tag = tag; + } + + + + public Set getPosts() { + return posts; + } + + + + public void setPosts(Set posts) { + this.posts = posts; + } + + +} diff --git a/database/src/main/java/com/piccritic/database/tag/TagConnector.java b/database/src/main/java/com/piccritic/database/tag/TagConnector.java new file mode 100644 index 0000000..6262030 --- /dev/null +++ b/database/src/main/java/com/piccritic/database/tag/TagConnector.java @@ -0,0 +1,37 @@ +/** + * @author damienr74, ian-dawson + */ + +package com.piccritic.database.tag; + +import java.util.List; + +public interface TagConnector { + /** + * Inserts a new tag into the database + * + * @param tag + * @throws TagException + */ + public void insertTag(Tag tag) throws TagException; + + /** + * Returns a list of tags that match the user query + * + * @param query + * @return List of tags + * @throws TagException + */ + public List findTags(String query) throws TagException; + + /** + * Deletes a tag + * @param tag + * @return + * @throws TagException + */ + public boolean deleteTag(Tag tag) throws TagException; + + + +} diff --git a/database/src/main/java/com/piccritic/database/tag/TagException.java b/database/src/main/java/com/piccritic/database/tag/TagException.java new file mode 100644 index 0000000..3a596a1 --- /dev/null +++ b/database/src/main/java/com/piccritic/database/tag/TagException.java @@ -0,0 +1,9 @@ +package com.piccritic.database.tag; + +@SuppressWarnings("serial") +public class TagException extends Exception { + public TagException(String message) { + super(message); + } + +} diff --git a/database/src/test/java/com/piccritic/database/tag/TagConnectorTest.java b/database/src/test/java/com/piccritic/database/tag/TagConnectorTest.java new file mode 100644 index 0000000..33097bf --- /dev/null +++ b/database/src/test/java/com/piccritic/database/tag/TagConnectorTest.java @@ -0,0 +1,50 @@ +/** + * @author damienr74, ian-dawson + */ + +package com.piccritic.database.tag; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TagConnectorTest { + String dog_tag = "dog"; + String bad_tag = "123"; + + TagConnector tc = new JPATagConnector(); + + @Before + public void init() { + + } + + @Test + public void TestDogTag() throws TagException { + assertTrue(tc.findTags("").isEmpty()); + assertTrue(tc.findTags("dog").isEmpty()); + Tag t = new Tag(); + t.setTag(dog_tag); + tc.insertTag(t); + assertEquals(1, tc.findTags("").size()); + assertEquals(1, tc.findTags("dog").size()); + tc.deleteTag(t); + assertTrue(tc.findTags("dog").isEmpty()); + } + + @Test(expected = TagException.class) + public void invalidTagTest() throws TagException { + Tag t = new Tag(); + t.setTag(bad_tag); + tc.insertTag(t); + } + + @After + public void teardown() { + + } + +} From 7903c5af417ea54ad917a3551b6b2c1c85f6e227 Mon Sep 17 00:00:00 2001 From: ian-dawson Date: Tue, 4 Apr 2017 23:49:22 -0300 Subject: [PATCH 10/12] Implemented tag filtering Authors: damienr74, ian-dawson Added database tag implementation. Added tag filtering UI --- .../piccritic/compute/MasterConnector.java | 4 ++ .../com/piccritic/compute/MasterService.java | 4 ++ .../piccritic/compute/tag/TagInterface.java | 8 ++++ .../com/piccritic/compute/tag/TagService.java | 36 ++++++++++++++ .../com/piccritic/database/post/Post.java | 2 +- .../database/tag/JPATagConnector.java | 40 +++++++++++++++- .../java/com/piccritic/database/tag/Tag.java | 2 +- .../piccritic/database/tag/TagConnector.java | 13 ++++- website/pom.xml | 9 ++++ .../piccritic/website/AppWidgetSet.gwt.xml | 2 + .../main/java/com/piccritic/website/Home.java | 1 - .../java/com/piccritic/website/PicCritic.java | 2 +- .../com/piccritic/website/PostQuickView.java | 48 +++++++++++++++---- .../piccritic/website/post/CreatePost.java | 35 ++++++++++---- .../piccritic/website/post/TagChooser.java | 48 +++++++++++++++++++ .../com/piccritic/website/user/UserView.java | 4 +- 16 files changed, 233 insertions(+), 25 deletions(-) create mode 100644 compute/src/main/java/com/piccritic/compute/tag/TagInterface.java create mode 100644 compute/src/main/java/com/piccritic/compute/tag/TagService.java create mode 100644 website/src/main/java/com/piccritic/website/post/TagChooser.java diff --git a/compute/src/main/java/com/piccritic/compute/MasterConnector.java b/compute/src/main/java/com/piccritic/compute/MasterConnector.java index ecef024..53f4755 100644 --- a/compute/src/main/java/com/piccritic/compute/MasterConnector.java +++ b/compute/src/main/java/com/piccritic/compute/MasterConnector.java @@ -12,6 +12,8 @@ import com.piccritic.database.post.JPAAlbumConnector; import com.piccritic.database.post.JPAPostConnector; import com.piccritic.database.post.PostConnector; +import com.piccritic.database.tag.JPATagConnector; +import com.piccritic.database.tag.TagConnector; import com.piccritic.database.user.JPAUserConnector; import com.piccritic.database.user.UserConnector; @@ -24,6 +26,7 @@ public class MasterConnector { public static CommentConnector commentConnector; public static VoteConnector voteConnector; public static RatingConnector ratingConnector; + public static TagConnector tagConnector; public static void init() { if (postConnector == null) postConnector = new JPAPostConnector(); @@ -33,5 +36,6 @@ public static void init() { if (commentConnector == null) commentConnector = new JPACommentConnector(); if (voteConnector == null) voteConnector = new JPAVoteConnector(); if (ratingConnector == null) ratingConnector = new JPARatingConnector(); + if (tagConnector == null) tagConnector = new JPATagConnector(); } } diff --git a/compute/src/main/java/com/piccritic/compute/MasterService.java b/compute/src/main/java/com/piccritic/compute/MasterService.java index 3e898b8..56255d1 100644 --- a/compute/src/main/java/com/piccritic/compute/MasterService.java +++ b/compute/src/main/java/com/piccritic/compute/MasterService.java @@ -6,6 +6,8 @@ import com.piccritic.compute.license.LicenseServiceInterface; import com.piccritic.compute.post.PostService; import com.piccritic.compute.post.PostServiceInterface; +import com.piccritic.compute.tag.TagInterface; +import com.piccritic.compute.tag.TagService; import com.piccritic.compute.user.UserService; import com.piccritic.compute.user.UserServiceInterface; @@ -15,11 +17,13 @@ public class MasterService { public static UserServiceInterface userService; public static LicenseServiceInterface licenseService; public static PostServiceInterface postService; + public static TagInterface tagService; public static void init() { if (feedbackService == null) feedbackService = FeedbackService.createService(); if (userService == null) userService = UserService.createService(); if (licenseService == null) licenseService = new LicenseService(); if (postService == null) postService = new PostService(); + if (tagService == null) tagService = new TagService(); } } diff --git a/compute/src/main/java/com/piccritic/compute/tag/TagInterface.java b/compute/src/main/java/com/piccritic/compute/tag/TagInterface.java new file mode 100644 index 0000000..523a28a --- /dev/null +++ b/compute/src/main/java/com/piccritic/compute/tag/TagInterface.java @@ -0,0 +1,8 @@ +package com.piccritic.compute.tag; + +import com.piccritic.database.tag.TagConnector; + +public interface TagInterface extends TagConnector{ + + +} diff --git a/compute/src/main/java/com/piccritic/compute/tag/TagService.java b/compute/src/main/java/com/piccritic/compute/tag/TagService.java new file mode 100644 index 0000000..9e2594d --- /dev/null +++ b/compute/src/main/java/com/piccritic/compute/tag/TagService.java @@ -0,0 +1,36 @@ +package com.piccritic.compute.tag; + +import java.util.List; + +import com.piccritic.compute.MasterConnector; +import com.piccritic.database.post.Post; +import com.piccritic.database.tag.Tag; +import com.piccritic.database.tag.TagException; +import com.piccritic.database.user.Critic; + +public class TagService implements TagInterface { + + @Override + public void insertTag(Tag tag) throws TagException { + MasterConnector.tagConnector.insertTag(tag); + } + + @Override + public List findTags(String query) { + if (query == null) { + return null; + } + return MasterConnector.tagConnector.findTags(query); + } + + @Override + public boolean deleteTag(Tag tag) throws TagException { + return MasterConnector.tagConnector.deleteTag(tag); + } + + @Override + public List findPosts(List tags, Critic critic) { + return MasterConnector.tagConnector.findPosts(tags, critic); + } + +} diff --git a/database/src/main/java/com/piccritic/database/post/Post.java b/database/src/main/java/com/piccritic/database/post/Post.java index c048169..a851bca 100644 --- a/database/src/main/java/com/piccritic/database/post/Post.java +++ b/database/src/main/java/com/piccritic/database/post/Post.java @@ -55,7 +55,7 @@ public class Post { @OneToMany(fetch=FetchType.EAGER, mappedBy="post") private Set ratings; - @ManyToMany(fetch=FetchType.EAGER, mappedBy="posts") + @ManyToMany(fetch=FetchType.EAGER) private Set tags; public String getPath() { diff --git a/database/src/main/java/com/piccritic/database/tag/JPATagConnector.java b/database/src/main/java/com/piccritic/database/tag/JPATagConnector.java index ab84797..d2d36b1 100644 --- a/database/src/main/java/com/piccritic/database/tag/JPATagConnector.java +++ b/database/src/main/java/com/piccritic/database/tag/JPATagConnector.java @@ -9,6 +9,8 @@ import javax.persistence.TypedQuery; import com.piccritic.database.JPAConnector; +import com.piccritic.database.post.Post; +import com.piccritic.database.user.Critic; public class JPATagConnector extends JPAConnector implements TagConnector { @@ -19,15 +21,51 @@ public JPATagConnector() { @Override public void insertTag(Tag tag) throws TagException { validate(tag); + if (container.containsId(tag.getTag())) { + return; + } container.addEntity(tag); } @Override - public List findTags(String query) throws TagException { + public List findTags(String query) { String q = "SELECT t FROM Tag t WHERE t.tag LIKE CONCAT('%', :query, '%')"; TypedQuery t = entity.createQuery(q, Tag.class).setParameter("query", query); return t.getResultList(); } + + public List findPosts(List tags, Critic critic) { + if (tags == null || tags.size() < 1) { + return null; + } + + String beforeConcat = "t.tag LIKE CONCAT('%', :query"; + String afterConcat = ", '%')"; + // select c FROM Course c JOIN c.enrolledStudents u WHERE u.id = :userId + String q = "SELECT p FROM Post p JOIN p.tags t WHERE "; + + for (int i = 0; i < tags.size(); i++) { + q += beforeConcat+i+afterConcat; + if (i+1 < tags.size()) { + q += " AND "; + } + } + + if (critic != null) { + q += " AND p.album.critic = :critic"; + } + + TypedQuery t = entity.createQuery(q, Post.class); + for (int i = 0; i < tags.size(); i++) { + t = t.setParameter("query"+i, tags.get(i)); + } + + if (critic != null) { + t = t.setParameter("critic", critic); + } + + return t.getResultList(); + } @Override public boolean deleteTag(Tag tag) throws TagException { diff --git a/database/src/main/java/com/piccritic/database/tag/Tag.java b/database/src/main/java/com/piccritic/database/tag/Tag.java index e43302f..29727c4 100644 --- a/database/src/main/java/com/piccritic/database/tag/Tag.java +++ b/database/src/main/java/com/piccritic/database/tag/Tag.java @@ -26,7 +26,7 @@ public class Tag { @Pattern(regexp="[a-z]+") private String tag; - @ManyToMany(fetch=FetchType.EAGER) + @ManyToMany(fetch=FetchType.EAGER, mappedBy="tags") private Set posts; public String getTag() { diff --git a/database/src/main/java/com/piccritic/database/tag/TagConnector.java b/database/src/main/java/com/piccritic/database/tag/TagConnector.java index 6262030..f9a0813 100644 --- a/database/src/main/java/com/piccritic/database/tag/TagConnector.java +++ b/database/src/main/java/com/piccritic/database/tag/TagConnector.java @@ -6,6 +6,9 @@ import java.util.List; +import com.piccritic.database.post.Post; +import com.piccritic.database.user.Critic; + public interface TagConnector { /** * Inserts a new tag into the database @@ -20,9 +23,8 @@ public interface TagConnector { * * @param query * @return List of tags - * @throws TagException */ - public List findTags(String query) throws TagException; + public List findTags(String query); /** * Deletes a tag @@ -32,6 +34,13 @@ public interface TagConnector { */ public boolean deleteTag(Tag tag) throws TagException; + /** + * Selects posts by tags + * @param tags + * @param critic + * @return + */ + public List findPosts(List tags, Critic critic); } diff --git a/website/pom.xml b/website/pom.xml index 1807e96..61f4ead 100644 --- a/website/pom.xml +++ b/website/pom.xml @@ -83,6 +83,11 @@ 4.2.0 test + + org.vaadin.addons + tokenfield + 7.0.1 + @@ -233,6 +238,10 @@ true + + vaadin-addons + http://maven.vaadin.com/vaadin-addons + diff --git a/website/src/main/java/com/piccritic/website/AppWidgetSet.gwt.xml b/website/src/main/java/com/piccritic/website/AppWidgetSet.gwt.xml index 85debf5..11adb62 100644 --- a/website/src/main/java/com/piccritic/website/AppWidgetSet.gwt.xml +++ b/website/src/main/java/com/piccritic/website/AppWidgetSet.gwt.xml @@ -30,4 +30,6 @@ --> + + diff --git a/website/src/main/java/com/piccritic/website/Home.java b/website/src/main/java/com/piccritic/website/Home.java index 10eb7a7..b51c8db 100644 --- a/website/src/main/java/com/piccritic/website/Home.java +++ b/website/src/main/java/com/piccritic/website/Home.java @@ -46,7 +46,6 @@ public Home() { navigator.addView(UserView.NAME, UserView.class); if (navigator.getState().isEmpty()) { navigator.navigateTo(DefaultView.NAME); - System.out.println(navigator.getState()); } else { navigator.navigateTo(navigator.getState()); } diff --git a/website/src/main/java/com/piccritic/website/PicCritic.java b/website/src/main/java/com/piccritic/website/PicCritic.java index 2cb630c..f6c1888 100644 --- a/website/src/main/java/com/piccritic/website/PicCritic.java +++ b/website/src/main/java/com/piccritic/website/PicCritic.java @@ -39,7 +39,7 @@ protected void init(VaadinRequest vaadinRequest) { @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true) - @VaadinServletConfiguration(ui = PicCritic.class, productionMode = false) + @VaadinServletConfiguration(ui = PicCritic.class, productionMode = true) public static class MyUIServlet extends VaadinServlet { private static final long serialVersionUID = 1L; } diff --git a/website/src/main/java/com/piccritic/website/PostQuickView.java b/website/src/main/java/com/piccritic/website/PostQuickView.java index a20e111..53a9ada 100644 --- a/website/src/main/java/com/piccritic/website/PostQuickView.java +++ b/website/src/main/java/com/piccritic/website/PostQuickView.java @@ -5,34 +5,62 @@ import java.util.Iterator; import java.util.List; +import com.piccritic.compute.MasterConnector; import com.piccritic.database.post.Post; import com.vaadin.data.Property.ValueChangeEvent; import com.vaadin.data.Property.ValueChangeListener; +import com.piccritic.database.user.Critic; +import com.piccritic.website.post.TagChooser; import com.vaadin.server.FileResource; import com.vaadin.ui.Alignment; import com.vaadin.ui.ComboBox; +import com.vaadin.ui.Button; import com.vaadin.ui.GridLayout; +import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Image; import com.vaadin.ui.Label; +import com.vaadin.ui.Notification; +import com.vaadin.ui.Notification.Type; import com.vaadin.ui.UI; import com.vaadin.ui.VerticalLayout; import com.vaadin.ui.themes.ValoTheme; -public class PostQuickView extends GridLayout { +public class PostQuickView extends VerticalLayout { + protected Critic critic; Image image; + HorizontalLayout tagSearch = new HorizontalLayout(); + GridLayout gl = new GridLayout(3, 3); + TagChooser chooser = new TagChooser(); + Button searchTags = new Button("Filter", e -> { + List tags = chooser.getTags(); + List posts = MasterConnector.tagConnector.findPosts(tags, critic); + if (posts == null || posts.isEmpty() ) { + Notification.show("No results", Type.WARNING_MESSAGE); + } else { + initPosts(posts); + } + }); public PostQuickView() { - super(4, 3); setSizeFull(); setHeightUndefined(); + tagSearch.addComponent(chooser); + tagSearch.addComponent(searchTags); + addComponent(tagSearch); + gl.setSizeFull(); + gl.setHeightUndefined(); + addComponent(gl); + } + + public PostQuickView(Critic c) { + this(); + critic = c; } public void initPosts(List posts) { - this.removeAllComponents(); - - System.out.println("does this happen twice?"); - + gl.removeAllComponents(); + //add sorting options combo box List sortOptions = new ArrayList<>(); sortOptions.add("Title"); @@ -55,13 +83,17 @@ public void valueChange(ValueChangeEvent event) { select.addValueChangeListener(listener); - addComponent(select, 0, 0, 3, 1); + addComponent(select); //add posts + gl.removeAllComponents(); + if (posts == null) { + return; + } Iterator it = posts.iterator(); for (int y = 0; y < 9; y++) { if (it.hasNext()) { - addComponent(new PostTile(it.next())); + gl.addComponent(new PostTile(it.next())); } else { return; } diff --git a/website/src/main/java/com/piccritic/website/post/CreatePost.java b/website/src/main/java/com/piccritic/website/post/CreatePost.java index 15d4739..37defbb 100644 --- a/website/src/main/java/com/piccritic/website/post/CreatePost.java +++ b/website/src/main/java/com/piccritic/website/post/CreatePost.java @@ -1,23 +1,24 @@ package com.piccritic.website.post; +import java.util.Collection; import java.util.HashSet; import java.util.Set; import com.piccritic.compute.MasterService; -import com.piccritic.compute.post.PostService; import com.piccritic.compute.post.PostServiceInterface; +import com.piccritic.compute.tag.TagInterface; import com.piccritic.database.post.Album; import com.piccritic.database.post.AlbumException; import com.piccritic.database.post.Post; import com.piccritic.database.post.PostException; +import com.piccritic.database.tag.Tag; +import com.piccritic.database.tag.TagException; import com.piccritic.website.license.LicenseChooser; import com.piccritic.website.login.LoginService; import com.vaadin.annotations.Theme; import com.vaadin.server.FileResource; import com.vaadin.ui.Button; -import com.vaadin.ui.CheckBox; import com.vaadin.ui.FormLayout; -import com.vaadin.ui.GridLayout; import com.vaadin.ui.Image; import com.vaadin.ui.Notification; import com.vaadin.ui.Notification.Type; @@ -44,14 +45,15 @@ public class CreatePost extends Window implements SucceededListener { private TextField title = new TextField("Post Title"); private LicenseChooser license = new LicenseChooser(); private Upload upload; - private GridLayout tags = new GridLayout(2, 2); private Post post = new Post(); private ImageReceiver receiver; private Image image = new Image("Uploaded Image"); private Button confirm = new Button("Confirm", this::confirmUpload); private PostServiceInterface service = MasterService.postService; + private TagInterface tagservice = MasterService.tagService; private String handle; private FormLayout form = new FormLayout(); + private TagChooser tags = new TagChooser(); /** * This window is responsible for creating a post form with an @see Upload @@ -88,10 +90,7 @@ public CreatePost(String handle) { confirm.setEnabled(false); description.setRequired(true); - tags.addComponent(new CheckBox("Nature"), 0, 0); - tags.addComponent(new CheckBox("People"), 1, 0); - tags.addComponent(new CheckBox("Sports"), 0, 1); - tags.addComponent(new CheckBox("Urban"), 1, 1); + title.setRequired(true); setupImagereceiver(); } @@ -130,6 +129,7 @@ public CreatePost(String handle, Post post) { image.setVisible(true); confirm.setEnabled(true); } + tags.setVisible(false); } @Override @@ -153,6 +153,23 @@ private void confirmUpload(Button.ClickEvent event) { description.validate(); post.setDescription(description.getValue()); post.setTitle(title.getValue()); + + Collection tagCollection = tags.getTags(); + Set tagSet = new HashSet(); + + for (String t: tagCollection) { + Tag tag = new Tag(); + tag.setTag(t); + + //Set posts = tag.getPosts(); + //posts.add(post); + //tag.setPosts(posts); + tagservice.insertTag(tag); + tagSet.add(tag); + } + + post.setTags(tagSet); + if (service != null) { Album defaultAlbum = service.getDefaultAlbum(LoginService.getHandle()); post.setAlbum(defaultAlbum); @@ -174,7 +191,7 @@ private void confirmUpload(Button.ClickEvent event) { Notification.show("Could not create post.", Type.WARNING_MESSAGE); - } catch (PostException|AlbumException e) { + } catch (PostException|AlbumException | TagException e) { upload.interruptUpload(); Notification.show(e.getMessage(), Type.WARNING_MESSAGE); } diff --git a/website/src/main/java/com/piccritic/website/post/TagChooser.java b/website/src/main/java/com/piccritic/website/post/TagChooser.java new file mode 100644 index 0000000..916c662 --- /dev/null +++ b/website/src/main/java/com/piccritic/website/post/TagChooser.java @@ -0,0 +1,48 @@ +package com.piccritic.website.post; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.vaadin.tokenfield.TokenField; + +import com.piccritic.compute.MasterService; +import com.piccritic.database.tag.Tag; +import com.vaadin.ui.VerticalLayout; + +public class TagChooser extends VerticalLayout { + static String caption = "Tags"; + private Collection tags; + + private QueryToken tokens; + + private class QueryToken extends TokenField { + public QueryToken() { + tags = MasterService.tagService.findTags(""); + for (Tag t : tags) { + cb.addItem(t.getTag()); + } + } + + public List getTags() { + List tags = new ArrayList(); + for (Object o : buttons.keySet()) { + tags.add(o.toString()); + } + return tags; + } + } + + public TagChooser() { + tokens = new QueryToken(); + addComponent(tokens); + } + + public List getTags() { + return tokens.getTags(); + } + + + + +} diff --git a/website/src/main/java/com/piccritic/website/user/UserView.java b/website/src/main/java/com/piccritic/website/user/UserView.java index a74dcdb..a39338a 100644 --- a/website/src/main/java/com/piccritic/website/user/UserView.java +++ b/website/src/main/java/com/piccritic/website/user/UserView.java @@ -32,7 +32,7 @@ public class UserView extends PostQuickView implements View { public void enter(ViewChangeEvent event) { if (event.getParameters() != null && event.getParameters() != "") { UserServiceInterface service = MasterService.userService; - Critic critic = service.select(event.getParameters()); + critic = service.select(event.getParameters()); if (critic != null) { Iterator i = critic.getAlbums().iterator(); if (i.hasNext()) { @@ -43,6 +43,8 @@ public void enter(ViewChangeEvent event) { } else { Notification.show("This user has no posts", Type.WARNING_MESSAGE); } + } else { + Notification.show("This user has no albums", Type.WARNING_MESSAGE); } } else { Notification.show("Invalid user", Type.WARNING_MESSAGE); //critic is null From 7200ee8f013d99eae498542b0613a766a1b5097f Mon Sep 17 00:00:00 2001 From: ian-dawson Date: Wed, 5 Apr 2017 00:47:35 -0300 Subject: [PATCH 11/12] Fixed bug with user sorting Authors: damienr74, ian-dawson --- .../com/piccritic/website/PostQuickView.java | 21 ++++++++----------- .../com/piccritic/website/user/UserView.java | 5 ++++- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/website/src/main/java/com/piccritic/website/PostQuickView.java b/website/src/main/java/com/piccritic/website/PostQuickView.java index 53a9ada..b1608f2 100644 --- a/website/src/main/java/com/piccritic/website/PostQuickView.java +++ b/website/src/main/java/com/piccritic/website/PostQuickView.java @@ -28,6 +28,7 @@ public class PostQuickView extends VerticalLayout { protected Critic critic; + protected ComboBox select; Image image; HorizontalLayout tagSearch = new HorizontalLayout(); GridLayout gl = new GridLayout(3, 3); @@ -50,24 +51,14 @@ public PostQuickView() { addComponent(tagSearch); gl.setSizeFull(); gl.setHeightUndefined(); - addComponent(gl); - } - - public PostQuickView(Critic c) { - this(); - critic = c; - } - - public void initPosts(List posts) { - gl.removeAllComponents(); - + //add sorting options combo box List sortOptions = new ArrayList<>(); sortOptions.add("Title"); sortOptions.add("Upload Date"); sortOptions.add("License"); - ComboBox select = new ComboBox("Select sorting option"); + select = new ComboBox("Select sorting option"); select.addItems(sortOptions); ValueChangeListener listener = new ValueChangeListener() { @@ -85,6 +76,12 @@ public void valueChange(ValueChangeEvent event) { addComponent(select); + addComponent(gl); + + + } + + public void initPosts(List posts) { //add posts gl.removeAllComponents(); if (posts == null) { diff --git a/website/src/main/java/com/piccritic/website/user/UserView.java b/website/src/main/java/com/piccritic/website/user/UserView.java index a39338a..6359d79 100644 --- a/website/src/main/java/com/piccritic/website/user/UserView.java +++ b/website/src/main/java/com/piccritic/website/user/UserView.java @@ -32,7 +32,10 @@ public class UserView extends PostQuickView implements View { public void enter(ViewChangeEvent event) { if (event.getParameters() != null && event.getParameters() != "") { UserServiceInterface service = MasterService.userService; - critic = service.select(event.getParameters()); + System.out.println(event.getParameters()); + System.out.println(event.getParameters().replaceAll("/.*", "")); + critic = service.select(event.getParameters().replaceAll("/.*", "")); + removeComponent(select); if (critic != null) { Iterator i = critic.getAlbums().iterator(); if (i.hasNext()) { From a4f0df72e247b29e971f8724b278cb0be69ac9f9 Mon Sep 17 00:00:00 2001 From: Damien Robichaud Date: Wed, 5 Apr 2017 02:08:06 -0300 Subject: [PATCH 12/12] Fix License error with non-default license --- .../piccritic/website/post/CreatePost.java | 387 +++++++++--------- 1 file changed, 194 insertions(+), 193 deletions(-) diff --git a/website/src/main/java/com/piccritic/website/post/CreatePost.java b/website/src/main/java/com/piccritic/website/post/CreatePost.java index 37defbb..0fae015 100644 --- a/website/src/main/java/com/piccritic/website/post/CreatePost.java +++ b/website/src/main/java/com/piccritic/website/post/CreatePost.java @@ -1,200 +1,201 @@ -package com.piccritic.website.post; - -import java.util.Collection; -import java.util.HashSet; -import java.util.Set; - -import com.piccritic.compute.MasterService; -import com.piccritic.compute.post.PostServiceInterface; -import com.piccritic.compute.tag.TagInterface; -import com.piccritic.database.post.Album; -import com.piccritic.database.post.AlbumException; -import com.piccritic.database.post.Post; -import com.piccritic.database.post.PostException; -import com.piccritic.database.tag.Tag; -import com.piccritic.database.tag.TagException; -import com.piccritic.website.license.LicenseChooser; -import com.piccritic.website.login.LoginService; -import com.vaadin.annotations.Theme; -import com.vaadin.server.FileResource; -import com.vaadin.ui.Button; -import com.vaadin.ui.FormLayout; -import com.vaadin.ui.Image; -import com.vaadin.ui.Notification; -import com.vaadin.ui.Notification.Type; -import com.vaadin.ui.Panel; -import com.vaadin.ui.TextArea; -import com.vaadin.ui.TextField; -import com.vaadin.ui.UI; -import com.vaadin.ui.Upload; -import com.vaadin.ui.Upload.SucceededEvent; -import com.vaadin.ui.Upload.SucceededListener; -import com.vaadin.ui.Window; - -/** +package com.piccritic.website.post; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +import com.piccritic.compute.MasterService; +import com.piccritic.compute.post.PostServiceInterface; +import com.piccritic.compute.tag.TagInterface; +import com.piccritic.database.post.Album; +import com.piccritic.database.post.AlbumException; +import com.piccritic.database.post.Post; +import com.piccritic.database.post.PostException; +import com.piccritic.database.tag.Tag; +import com.piccritic.database.tag.TagException; +import com.piccritic.website.license.LicenseChooser; +import com.piccritic.website.login.LoginService; +import com.vaadin.annotations.Theme; +import com.vaadin.server.FileResource; +import com.vaadin.ui.Button; +import com.vaadin.ui.FormLayout; +import com.vaadin.ui.Image; +import com.vaadin.ui.Notification; +import com.vaadin.ui.Notification.Type; +import com.vaadin.ui.Panel; +import com.vaadin.ui.TextArea; +import com.vaadin.ui.TextField; +import com.vaadin.ui.UI; +import com.vaadin.ui.Upload; +import com.vaadin.ui.Upload.SucceededEvent; +import com.vaadin.ui.Upload.SucceededListener; +import com.vaadin.ui.Window; + +/** * CreatePost Window to allow user to create post. - * + * * @author Rhianna Gogen - * Also edited By Damien Robichaud and Francis Bosse - */ -@Theme("mytheme") -public class CreatePost extends Window implements SucceededListener { - - private static final long serialVersionUID = 8544572658091510439L; - private TextArea description = new TextArea("Post Description"); - private TextField title = new TextField("Post Title"); - private LicenseChooser license = new LicenseChooser(); - private Upload upload; - private Post post = new Post(); - private ImageReceiver receiver; - private Image image = new Image("Uploaded Image"); - private Button confirm = new Button("Confirm", this::confirmUpload); - private PostServiceInterface service = MasterService.postService; - private TagInterface tagservice = MasterService.tagService; - private String handle; - private FormLayout form = new FormLayout(); - private TagChooser tags = new TagChooser(); + * Also edited By Damien Robichaud and Francis Bosse + */ +@Theme("mytheme") +public class CreatePost extends Window implements SucceededListener { + + private static final long serialVersionUID = 8544572658091510439L; + private TextArea description = new TextArea("Post Description"); + private TextField title = new TextField("Post Title"); + private LicenseChooser license = new LicenseChooser(); + private Upload upload; + private Post post = new Post(); + private ImageReceiver receiver; + private Image image = new Image("Uploaded Image"); + private Button confirm = new Button("Confirm", this::confirmUpload); + private PostServiceInterface service = MasterService.postService; + private TagInterface tagservice = MasterService.tagService; + private String handle; + private FormLayout form = new FormLayout(); + private TagChooser tags = new TagChooser(); /** * This window is responsible for creating a post form with an @see Upload - * component a post will only be uploaded if a picture, a title and a - * description. - * - * @param handle user that is creating the post - */ - public CreatePost(String handle) { - this.handle = handle; - setSizeFull(); - setModal(true); - Panel layout = new Panel(); - layout.setSizeFull(); - setContent(layout); - layout.setContent(form); - if (handle == null) { - close(); - } - - image.setVisible(false); - image.setSizeFull(); - image.setHeightUndefined(); - - form.setMargin(true); - form.addComponent(title); - form.addComponent(image); - form.addComponent(description); - form.addComponent(tags); - form.addComponent(license); - form.getComponent(0); - title.setSizeFull(); - description.setSizeFull(); - - confirm.setEnabled(false); - description.setRequired(true); - - title.setRequired(true); - setupImagereceiver(); - } - - public void setupImagereceiver() { - if (upload != null) { - form.removeComponent(upload); - } - receiver = new ImageReceiver(handle, post); - upload = new Upload("Upload Image Here", receiver); - upload.addSucceededListener(this); - - upload.addStartedListener(e -> { - confirm.setEnabled(false); - }); - - upload.setButtonCaption(null); - upload.addChangeListener(e -> { - if (e.getFilename() != null) { - upload.setButtonCaption("Upload"); - } - }); - - form.addComponent(upload); - form.addComponent(confirm); - } - - public CreatePost(String handle, Post post) { - this(handle); - if (post != null) { - this.post = post; - title.setValue(post.getTitle()); - description.setValue(post.getDescription()); - setupImagereceiver(); - image.setSource(new FileResource(receiver.getFile())); - image.setVisible(true); - confirm.setEnabled(true); - } - tags.setVisible(false); - } + * component a post will only be uploaded if a picture, a title and a + * description. + * + * @param handle user that is creating the post + */ + public CreatePost(String handle) { + this.handle = handle; + setSizeFull(); + setModal(true); + Panel layout = new Panel(); + layout.setSizeFull(); + setContent(layout); + layout.setContent(form); + if (handle == null) { + close(); + } + + image.setVisible(false); + image.setSizeFull(); + image.setHeightUndefined(); + + form.setMargin(true); + form.addComponent(title); + form.addComponent(image); + form.addComponent(description); + form.addComponent(tags); + form.addComponent(license); + form.getComponent(0); + title.setSizeFull(); + description.setSizeFull(); + + confirm.setEnabled(false); + description.setRequired(true); + + title.setRequired(true); + setupImagereceiver(); + } + + public void setupImagereceiver() { + if (upload != null) { + form.removeComponent(upload); + } + receiver = new ImageReceiver(handle, post); + upload = new Upload("Upload Image Here", receiver); + upload.addSucceededListener(this); + + upload.addStartedListener(e -> { + confirm.setEnabled(false); + }); + + upload.setButtonCaption(null); + upload.addChangeListener(e -> { + if (e.getFilename() != null) { + upload.setButtonCaption("Upload"); + } + }); + + form.addComponent(upload); + form.addComponent(confirm); + } + + public CreatePost(String handle, Post post) { + this(handle); + if (post != null) { + this.post = post; + title.setValue(post.getTitle()); + description.setValue(post.getDescription()); + setupImagereceiver(); + image.setSource(new FileResource(receiver.getFile())); + image.setVisible(true); + confirm.setEnabled(true); + } + tags.setVisible(false); + } @Override - public void uploadSucceeded(SucceededEvent event) { - // Show the uploaded file in the image viewer - confirm.setEnabled(true); - image.setVisible(true); - image.setSource(new FileResource(receiver.getFile())); - Notification.show("Image Saved", Type.TRAY_NOTIFICATION); - upload.setButtonCaption(null); - } - - /** - * Validates data and asks back-end to upload post. - * - * @param event - */ - private void confirmUpload(Button.ClickEvent event) { - try { - title.validate(); - description.validate(); - post.setDescription(description.getValue()); - post.setTitle(title.getValue()); - - Collection tagCollection = tags.getTags(); - Set tagSet = new HashSet(); - - for (String t: tagCollection) { - Tag tag = new Tag(); - tag.setTag(t); - - //Set posts = tag.getPosts(); - //posts.add(post); - //tag.setPosts(posts); - tagservice.insertTag(tag); - tagSet.add(tag); - } - - post.setTags(tagSet); - - if (service != null) { - Album defaultAlbum = service.getDefaultAlbum(LoginService.getHandle()); - post.setAlbum(defaultAlbum); - Post created = service.createPost(post); - - Set posts = new HashSet<>(); - posts.add(post); - defaultAlbum.setPosts(posts); - service.updateAlbum(post.getAlbum()); - - if (created != null) { - Notification.show("Post Uploaded", Type.TRAY_NOTIFICATION); - close(); - String postLocation = "#!post/" + post.getPath(); - UI.getCurrent().getPage().setLocation(postLocation); - UI.getCurrent().getPage().reload(); - } - } - - Notification.show("Could not create post.", Type.WARNING_MESSAGE); - - } catch (PostException|AlbumException | TagException e) { - upload.interruptUpload(); - Notification.show(e.getMessage(), Type.WARNING_MESSAGE); - } - - } -} + public void uploadSucceeded(SucceededEvent event) { + // Show the uploaded file in the image viewer + confirm.setEnabled(true); + image.setVisible(true); + image.setSource(new FileResource(receiver.getFile())); + Notification.show("Image Saved", Type.TRAY_NOTIFICATION); + upload.setButtonCaption(null); + } + + /** + * Validates data and asks back-end to upload post. + * + * @param event + */ + private void confirmUpload(Button.ClickEvent event) { + try { + title.validate(); + description.validate(); + post.setDescription(description.getValue()); + post.setTitle(title.getValue()); + post.setLicense(license.getValue()); + + Collection tagCollection = tags.getTags(); + Set tagSet = new HashSet(); + + for (String t: tagCollection) { + Tag tag = new Tag(); + tag.setTag(t); + + //Set posts = tag.getPosts(); + //posts.add(post); + //tag.setPosts(posts); + tagservice.insertTag(tag); + tagSet.add(tag); + } + + post.setTags(tagSet); + + if (service != null) { + Album defaultAlbum = service.getDefaultAlbum(LoginService.getHandle()); + post.setAlbum(defaultAlbum); + Post created = service.createPost(post); + + Set posts = new HashSet<>(); + posts.add(post); + defaultAlbum.setPosts(posts); + service.updateAlbum(post.getAlbum()); + + if (created != null) { + Notification.show("Post Uploaded", Type.TRAY_NOTIFICATION); + close(); + String postLocation = "#!post/" + post.getPath(); + UI.getCurrent().getPage().setLocation(postLocation); + UI.getCurrent().getPage().reload(); + } + } + + Notification.show("Could not create post.", Type.WARNING_MESSAGE); + + } catch (PostException|AlbumException | TagException e) { + upload.interruptUpload(); + Notification.show(e.getMessage(), Type.WARNING_MESSAGE); + } + + } +}