Skip to content

Commit

Permalink
Merge pull request #31 from CSCI3130/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
damienr74 authored Apr 10, 2017
2 parents 609c251 + 35d3b3c commit 0d04ec7
Show file tree
Hide file tree
Showing 52 changed files with 1,412 additions and 692 deletions.
41 changes: 41 additions & 0 deletions compute/src/main/java/com/piccritic/compute/MasterConnector.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
29 changes: 29 additions & 0 deletions compute/src/main/java/com/piccritic/compute/MasterService.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down
27 changes: 22 additions & 5 deletions compute/src/main/java/com/piccritic/compute/post/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@

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;
import com.piccritic.database.feedback.CommentException;
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.
*
Expand All @@ -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();
Expand Down Expand Up @@ -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<Album> albums = user.getAlbums();
Hibernate.initialize(albums);
Expand All @@ -140,4 +152,9 @@ public List<Post> getPosts(int number) throws PostException {
return pc.getPosts(number);
}


public List<Post> getPosts(int number, PostSortOption option) throws PostException {
return pc.getPosts(number, option);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
*
Expand All @@ -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.
Expand Down Expand Up @@ -84,4 +85,13 @@ public interface PostServiceInterface {
* @throws PostException
*/
public List<Post> 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<Post> getPosts(int number, PostSortOption option) throws PostException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.piccritic.compute.tag;

import com.piccritic.database.tag.TagConnector;

public interface TagInterface extends TagConnector{


}
36 changes: 36 additions & 0 deletions compute/src/main/java/com/piccritic/compute/tag/TagService.java
Original file line number Diff line number Diff line change
@@ -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<Tag> 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<Post> findPosts(List<String> tags, Critic critic) {
return MasterConnector.tagConnector.findPosts(tags, critic);
}

}
30 changes: 18 additions & 12 deletions compute/src/main/java/com/piccritic/compute/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.";
Expand All @@ -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() {
Expand All @@ -63,15 +69,15 @@ 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);
}

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);
}
Expand All @@ -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());
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
}
Loading

0 comments on commit 0d04ec7

Please sign in to comment.