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..53f4755 --- /dev/null +++ b/compute/src/main/java/com/piccritic/compute/MasterConnector.java @@ -0,0 +1,41 @@ +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.tag.JPATagConnector; +import com.piccritic.database.tag.TagConnector; +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 TagConnector tagConnector; + + 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(); + 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 new file mode 100644 index 0000000..56255d1 --- /dev/null +++ b/compute/src/main/java/com/piccritic/compute/MasterService.java @@ -0,0 +1,29 @@ +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.tag.TagInterface; +import com.piccritic.compute.tag.TagService; +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 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/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 8919cac..0a352ae 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; @@ -19,13 +20,15 @@ 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.JPAPostConnector; 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.JPAUserConnector; +import com.piccritic.database.user.UserConnector; /** * This class implements the PostServiceInterface. * @@ -37,9 +40,18 @@ public class PostService implements PostServiceInterface { public static final String USERS_DIR = "users"; - static PostConnector pc = new JPAPostConnector(); + 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(); @@ -113,11 +125,11 @@ 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) { - JPAUserConnector uc = new JPAUserConnector(); + uc = new JPAUserConnector(); Critic user = uc.selectCritic(handle); Set albums = user.getAlbums(); Hibernate.initialize(albums); @@ -140,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/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/compute/src/main/java/com/piccritic/compute/user/UserService.java b/compute/src/main/java/com/piccritic/compute/user/UserService.java index e542763..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,10 +13,12 @@ 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; 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 +30,8 @@ */ public class UserService implements UserServiceInterface { private static UserServiceInterface instance; - private static UserConnector connector; + 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."; @@ -37,7 +40,10 @@ public class UserService implements UserServiceInterface { private static String handleInUse = "Handle already in use."; private UserService() { - connector = new JPAUserConnector(); + MasterConnector.init(); + uc = MasterConnector.userConnector; + ac = MasterConnector.albumConnector; + } public static UserServiceInterface createService() { @@ -63,7 +69,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 +77,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 +95,8 @@ public String create(Critic critic, String password) throws UserException { critic.setAlbums(albums); try { - JPAPostConnector pc = new JPAPostConnector(); - pc.insertAlbum(defaultAlbum); + ac = new JPAAlbumConnector(); + ac.insertAlbum(defaultAlbum); } catch (AlbumException e) { e.printStackTrace(); throw new UserException(e.getLocalizedMessage()); @@ -110,14 +116,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 +132,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 +142,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 +155,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..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,8 +17,7 @@ import org.junit.Before; import org.junit.Test; -import com.piccritic.database.license.AttributionLicense; -import com.piccritic.database.license.JPALicenseConnector; +import com.piccritic.compute.MasterConnector; import com.piccritic.database.feedback.Comment; import com.piccritic.database.feedback.CommentConnector; import com.piccritic.database.feedback.CommentException; @@ -31,8 +30,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,17 +70,26 @@ 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; + private PostConnector pc; + private AlbumConnector ac; + private CommentConnector cc; + private VoteConnector vc; + private RatingConnector rc; - FeedbackServiceInterface fs = FeedbackService.createService(); + 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"); @@ -139,7 +150,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 +331,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..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; @@ -19,12 +20,13 @@ 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.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,13 +43,21 @@ 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; + 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(); @@ -60,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); @@ -84,16 +94,16 @@ public void setup() throws Exception{ comment.setScore(0); critic = uc.insertCritic(critic, "hash"); - pc.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()); } @@ -123,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()); @@ -134,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()); } @@ -145,8 +156,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 8723509..b717bec 100644 --- a/database/src/main/java/com/piccritic/database/JPAConnector.java +++ b/database/src/main/java/com/piccritic/database/JPAConnector.java @@ -2,19 +2,50 @@ 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.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; - public JPAConnector() { + /** + * 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) { 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); + } + + /** + * 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); + for (ConstraintViolation violation : violations) { + throw new Exception(violation.getMessage()); + } } - } + 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 5308ad1..6f82916 100644 --- a/database/src/main/java/com/piccritic/database/feedback/JPAVoteConnector.java +++ b/database/src/main/java/com/piccritic/database/feedback/JPAVoteConnector.java @@ -5,66 +5,35 @@ 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 + * 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 implements VoteConnector{ - - private JPAContainer votes; +public class JPAVoteConnector extends JPAConnector implements VoteConnector{ + /** + * Initializes the JPAContainer for this 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; } - - /* (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 = votes.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) @@ -74,9 +43,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 +67,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 +92,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 +108,49 @@ 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 + /* + * (non-Javadoc) + * @see com.piccritic.database.JPAConnector#validate(java.lang.Object) */ - 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()); + } + } + + /* (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 = votes.getEntityProvider().getEntityManager().createQuery(query, Vote.class) + TypedQuery q = entity.createQuery(query, Vote.class) .setParameter("comment", comment); List voteList = q.getResultList(); @@ -173,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 = votes.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 09926c0..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,46 +1,60 @@ /** - * + * JPALicenseConnector.java + * Created March ??, 2017 */ package com.piccritic.database.license; 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 { +/** + * 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 { - private static JPAContainer licenses; - + /** + * Initializes the JPAContainer for this Connector. + */ 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); + /** + * 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 = 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..8f49e76 --- /dev/null +++ b/database/src/main/java/com/piccritic/database/post/JPAAlbumConnector.java @@ -0,0 +1,77 @@ +package com.piccritic.database.post; + +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 JPAContainer for this AlbumConnector. + */ + 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()); + + } + + /* + * (non-Javadoc) + * @see com.piccritic.database.JPAConnector#validate(java.lang.Object) + */ + 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..ef0b6c2 100644 --- a/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java +++ b/database/src/main/java/com/piccritic/database/post/JPAPostConnector.java @@ -5,89 +5,26 @@ 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 - * 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 { - - private JPAContainer albums; - private JPAContainer posts; +public class JPAPostConnector extends JPAConnector implements PostConnector { /** - * Initializes the JPAContainers for this PostConnector. + * Initializes the JPAContainer 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 +32,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 +42,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 +57,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,35 +70,19 @@ 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()); } - /** - * 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) */ - 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.getLocalizedMessage()); } } @@ -170,16 +91,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 +114,42 @@ 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) { + System.out.println(e.toString()); + 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 p FROM Post p ORDER BY p.uploadDate", Post.class); + break; + case TITLE: + 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 p FROM Post p", Post.class); + break; + } 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..a851bca 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 @@ -29,7 +31,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 { @@ -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) + 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/post/PostConnector.java b/database/src/main/java/com/piccritic/database/post/PostConnector.java index 869ab0b..b0faa00 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,41 +17,12 @@ * 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; + + public enum PostSortOption { + UPLOAD_DATE, + TITLE, + LICENSE + } /** * Inserts the specified Post into the database. @@ -98,11 +67,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/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..d2d36b1 --- /dev/null +++ b/database/src/main/java/com/piccritic/database/tag/JPATagConnector.java @@ -0,0 +1,83 @@ +/** + * @author damienr74, ian-dawson + */ + +package com.piccritic.database.tag; + +import java.util.List; + +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 { + + public JPATagConnector() { + super(Tag.class); + } + + @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) { + 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 { + 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..29727c4 --- /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, mappedBy="tags") + 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..f9a0813 --- /dev/null +++ b/database/src/main/java/com/piccritic/database/tag/TagConnector.java @@ -0,0 +1,46 @@ +/** + * @author damienr74, ian-dawson + */ + +package com.piccritic.database.tag; + +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 + * + * @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 + */ + public List findTags(String query); + + /** + * Deletes a tag + * @param tag + * @return + * @throws TagException + */ + 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/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/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. * 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..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,7 +10,9 @@ 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; import org.junit.After; @@ -18,14 +20,16 @@ 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.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; 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 @@ -44,9 +48,10 @@ 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 = "handle"; + private String handle = "handlePCT"; private String lastName = "lastName"; private String hash = "hash"; private String albumName = "album"; @@ -56,7 +61,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(); @@ -64,11 +74,14 @@ public class PostConnectorTest { private Set ratingSet = new HashSet(); 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); @@ -87,33 +100,37 @@ 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); + post.setComments(commentSet); + post.setLicense(lc.selectLicense(License.ATTRIBUTION_NON_COMMERCIAL_NO_DERIVS)); - post1.setUploadDate(date); - post1.setTitle(postTitle); + post1.setUploadDate(date2); + post1.setTitle("Delta"); post1.setDescription("description"); + post1.setLicense(lc.selectLicense(License.ATTRIBUTION_NON_COMMERCIAL)); post2.setUploadDate(date); - post2.setTitle(postTitle); + post2.setTitle("Cookie"); post2.setDescription("description"); + post2.setLicense(lc.selectLicense(License.ATTRIBUTION_NO_DERIVS)); - post3.setUploadDate(date); - post3.setTitle(postTitle); + post3.setUploadDate(date1); + post3.setTitle("Beta"); post3.setDescription("description"); + post3.setLicense(lc.selectLicense(License.ATTRIBUTION_NON_COMMERCIAL_SHAREALIKE)); - post4.setUploadDate(date); - post4.setTitle(postTitle); + post4.setUploadDate(date3); + post4.setTitle("Alpha"); post4.setDescription("description"); + post4.setLicense(lc.selectLicense(License.ATTRIBUTION_SHAREALIKE)); try { uc.insertCritic(critic, hash); album.setCritic(critic); - pc.insertAlbum(album); + ac.insertAlbum(album); post.setPath(path); - post.setLicense(new AttributionNonCommercialLicense()); post1.setPath(path1); post2.setPath(path2); post3.setPath(path3); @@ -128,6 +145,7 @@ public void init() { pc.insertPost(post2); pc.insertPost(post3); pc.insertPost(post4); + } catch (UserException|PostException|AlbumException e) { fail(e.getMessage()); } @@ -162,14 +180,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 +201,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); @@ -219,6 +237,48 @@ 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()); + } + } + + @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() { @@ -228,7 +288,7 @@ public void tearDown() { pc.deletePost(post2); pc.deletePost(post3); pc.deletePost(post4); - pc.deleteAlbum(album); + ac.deleteAlbum(album); } catch (Exception e) { e.getLocalizedMessage(); } 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() { + + } + +} 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/DefaultView.java b/website/src/main/java/com/piccritic/website/DefaultView.java index e2688b4..a9e94e2 100644 --- a/website/src/main/java/com/piccritic/website/DefaultView.java +++ b/website/src/main/java/com/piccritic/website/DefaultView.java @@ -4,7 +4,9 @@ import java.util.ArrayList; import java.util.List; +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; @@ -22,7 +24,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(); @@ -36,7 +39,28 @@ 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; + } else if (sortOption.equals("License")) { + option = PostSortOption.LICENSE; + } + 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/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 0bb646b..f6c1888 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,18 +30,16 @@ 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()); } @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 16f328c..b1608f2 100644 --- a/website/src/main/java/com/piccritic/website/PostQuickView.java +++ b/website/src/main/java/com/piccritic/website/PostQuickView.java @@ -1,35 +1,96 @@ package com.piccritic.website; import java.io.File; +import java.util.ArrayList; 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; + protected ComboBox select; 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(3, 3); setSizeFull(); setHeightUndefined(); + tagSearch.addComponent(chooser); + tagSearch.addComponent(searchTags); + addComponent(tagSearch); + gl.setSizeFull(); + gl.setHeightUndefined(); + + //add sorting options combo box + List sortOptions = new ArrayList<>(); + sortOptions.add("Title"); + sortOptions.add("Upload Date"); + sortOptions.add("License"); + + 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); + + addComponent(gl); + + } - public void initPosts(List posts) { - System.out.println("does this happen twice?"); + public void initPosts(List posts) { + //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/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..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,182 +1,201 @@ -package com.piccritic.website.post; - -import java.util.HashSet; -import java.util.Set; - -import com.piccritic.compute.post.PostService; -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.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; -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.ChangeListener; -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 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 PostService service = new PostService(); - private String handle; - private FormLayout form = new FormLayout(); + * 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); - 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(); - } - - 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); - } - } + * 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()); - 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 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); + } + + } +} 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/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/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..6359d79 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,8 +31,11 @@ public class UserView extends PostQuickView implements View { @Override public void enter(ViewChangeEvent event) { if (event.getParameters() != null && event.getParameters() != "") { - UserServiceInterface service = UserService.createService(); - Critic critic = service.select(event.getParameters()); + UserServiceInterface service = MasterService.userService; + 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()) { @@ -43,6 +46,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 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..821afc1 100644 --- a/website/src/test/java/com/piccritic/website/PicCriticIT.java +++ b/website/src/test/java/com/piccritic/website/PicCriticIT.java @@ -4,27 +4,26 @@ 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.MasterConnector; +import com.piccritic.compute.MasterService; 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.LicenseConnector; +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 */ 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/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..0311c1a 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; @@ -45,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"; @@ -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..4d2d6d3 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. * @@ -44,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"; @@ -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(); }