code cleanup

This commit is contained in:
bancika
2016-11-01 16:43:24 +01:00
parent 3776955d7b
commit 94c8512c11
13 changed files with 78 additions and 62 deletions
@@ -2,6 +2,11 @@ package org.diylc.plugins.cloud.model;
import java.io.Serializable; import java.io.Serializable;
/**
* Represents a single post in the comment section of a cloud project.
*
* @author Branislav Stojkovic
*/
public class CommentEntity implements Serializable { public class CommentEntity implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@@ -1,23 +0,0 @@
package org.diylc.plugins.cloud.model;
import java.util.ArrayList;
import java.util.List;
public class CommentTree {
private CommentEntity rootComment;
private List<CommentEntity> replies;
public CommentTree(CommentEntity rootComment) {
this.rootComment = rootComment;
this.replies = new ArrayList<CommentEntity>();
}
public CommentEntity getRootComment() {
return rootComment;
}
public List<CommentEntity> getReplies() {
return replies;
}
}
@@ -1,7 +0,0 @@
package org.diylc.plugins.cloud.model;
public class CountryDetails {
private String country;
private String details;
}
@@ -1,12 +0,0 @@
package org.diylc.plugins.cloud.model;
import java.util.List;
public class DashboardEntity {
private List<ProjectEntity> latestProjects;
private List<ProjectEntity> mostDownloadedProjects;
private List<UserEntity> topContributors;
private List<CountryDetails> contributorsByCountry;
private List<CountryDetails> visitorsByCountry;
}
@@ -8,7 +8,7 @@ import com.diyfever.httpproxy.ParamName;
/*** /***
* Interface of DIYLC PHP server located at www.diy-fever.com/diylc/api/v1 * Interface of DIYLC PHP server located at www.diy-fever.com/diylc/api/v1
* *
* @author bancika * @author Branislav Stojkovic
* *
*/ */
public interface IServiceAPI { public interface IServiceAPI {
@@ -4,6 +4,11 @@ import java.io.Serializable;
import org.diylc.core.annotations.EditableProperty; import org.diylc.core.annotations.EditableProperty;
/**
* Represents a cloud project object, identified by {@link ProjectEntity#id}.
*
* @author Branislav Stojkovic
*/
public class ProjectEntity implements Serializable { public class ProjectEntity implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@@ -60,7 +65,7 @@ public class ProjectEntity implements Serializable {
public void setOwner(String owner) { public void setOwner(String owner) {
this.owner = owner; this.owner = owner;
} }
public String getCategory() { public String getCategory() {
return category; return category;
} }
@@ -68,12 +73,13 @@ public class ProjectEntity implements Serializable {
public void setCategory(String category) { public void setCategory(String category) {
this.category = category; this.category = category;
} }
@EditableProperty(defaultable = false, sortOrder = 2, additionalOptions = EditableProperty.DYNAMIC_LIST + "getCategories") @EditableProperty(defaultable = false, sortOrder = 2, additionalOptions = EditableProperty.DYNAMIC_LIST
+ "getCategories")
public String getCategoryForDisplay() { public String getCategoryForDisplay() {
return categoryForDisplay; return categoryForDisplay;
} }
public void setCategoryForDisplay(String categoryForDisplay) { public void setCategoryForDisplay(String categoryForDisplay) {
this.categoryForDisplay = categoryForDisplay; this.categoryForDisplay = categoryForDisplay;
} }
@@ -2,6 +2,11 @@ package org.diylc.plugins.cloud.model;
import java.io.Serializable; import java.io.Serializable;
/**
* Represents cloud user details.
*
* @author Branislav Stojkovic
*/
public class UserEntity implements Serializable { public class UserEntity implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@@ -24,6 +24,11 @@ import org.diylc.presenter.ComponentProcessor;
import com.diyfever.httpproxy.PhpFlatProxy; import com.diyfever.httpproxy.PhpFlatProxy;
import com.diyfever.httpproxy.ProxyFactory; import com.diyfever.httpproxy.ProxyFactory;
/**
* Contains all the back-end logic for using the cloud and manipulating projects on the cloud.
*
* @author Branislav Stojkovic
*/
public class CloudPresenter { public class CloudPresenter {
private static String USERNAME_KEY = "cloud.Username"; private static String USERNAME_KEY = "cloud.Username";
@@ -119,6 +124,7 @@ public class CloudPresenter {
public String[] getCategories() throws CloudException { public String[] getCategories() throws CloudException {
if (categories == null) { if (categories == null) {
Object res; Object res;
LOG.info("Fetching categories");
try { try {
res = getService().getCategories(); res = getService().getCategories();
if (res != null && res.equals(ERROR)) if (res != null && res.equals(ERROR))
@@ -146,6 +152,7 @@ public class CloudPresenter {
if (username == null || token == null) if (username == null || token == null)
throw new CloudException("Login failed. Please try to login again."); throw new CloudException("Login failed. Please try to login again.");
LOG.info("Uploading a new project: " + projectName);
try { try {
String res = String res =
getService().uploadProject(username, token, getMachineId(), projectName, category, description, diylcVersion, getService().uploadProject(username, token, getMachineId(), projectName, category, description, diylcVersion,
@@ -163,11 +170,12 @@ public class CloudPresenter {
if (username == null || token == null) if (username == null || token == null)
throw new CloudException("Login failed. Please try to login again."); throw new CloudException("Login failed. Please try to login again.");
LOG.info("Updating project with projectId: " + project.getId());
try { try {
String res = String res =
getService().uploadProject(username, token, getMachineId(), project.getName(), project.getCategoryForDisplay(), getService().uploadProject(username, token, getMachineId(), project.getName(),
project.getDescription(), diylcVersion, project.getKeywords(), null, null, project.getId()); project.getCategoryForDisplay(), project.getDescription(), diylcVersion, project.getKeywords(), null,
null, project.getId());
if (!res.equals(SUCCESS)) if (!res.equals(SUCCESS))
throw new CloudException(res); throw new CloudException(res);
} catch (Exception e) { } catch (Exception e) {
@@ -182,6 +190,7 @@ public class CloudPresenter {
if (username == null || token == null) if (username == null || token == null)
throw new CloudException("Login failed. Please try to login again."); throw new CloudException("Login failed. Please try to login again.");
LOG.info("Deleting project with projectId: " + projectId);
try { try {
String res = getService().deleteProject(username, token, getMachineId(), projectId); String res = getService().deleteProject(username, token, getMachineId(), projectId);
if (!res.equals(SUCCESS)) if (!res.equals(SUCCESS))
@@ -198,6 +207,8 @@ public class CloudPresenter {
if (username == null || token == null) if (username == null || token == null)
throw new CloudException("Login failed. Please try to login again."); throw new CloudException("Login failed. Please try to login again.");
LOG.info("Posting a new comment to projectId: " + projectId);
try { try {
String res = getService().postComment(username, token, getMachineId(), projectId, comment); String res = getService().postComment(username, token, getMachineId(), projectId, comment);
if (!res.equals(SUCCESS)) if (!res.equals(SUCCESS))
@@ -209,6 +220,7 @@ public class CloudPresenter {
public void createUserAccount(String username, String password, String email, String website, String bio) public void createUserAccount(String username, String password, String email, String website, String bio)
throws CloudException { throws CloudException {
LOG.info("Creating a new user: " + username);
try { try {
String res = getService().createUser(username, password, email, website, bio); String res = getService().createUser(username, password, email, website, bio);
if (res == null) if (res == null)
@@ -223,7 +235,7 @@ public class CloudPresenter {
public UserEntity getUserDetails() throws CloudException { public UserEntity getUserDetails() throws CloudException {
String username = ConfigurationManager.getInstance().readString(USERNAME_KEY, null); String username = ConfigurationManager.getInstance().readString(USERNAME_KEY, null);
String token = ConfigurationManager.getInstance().readString(TOKEN_KEY, null); String token = ConfigurationManager.getInstance().readString(TOKEN_KEY, null);
LOG.info("Retreiving user details for: " + username);
Object res; Object res;
try { try {
res = getService().getUserDetails(username, token, getMachineId()); res = getService().getUserDetails(username, token, getMachineId());
@@ -244,6 +256,7 @@ public class CloudPresenter {
public void updatePassword(String oldPassword, String newPassword) throws CloudException { public void updatePassword(String oldPassword, String newPassword) throws CloudException {
String username = ConfigurationManager.getInstance().readString(USERNAME_KEY, null); String username = ConfigurationManager.getInstance().readString(USERNAME_KEY, null);
LOG.info("Updating password for: " + username);
String res; String res;
try { try {
res = getService().updatePassword(username, oldPassword, newPassword); res = getService().updatePassword(username, oldPassword, newPassword);
@@ -258,6 +271,7 @@ public class CloudPresenter {
String username = ConfigurationManager.getInstance().readString(USERNAME_KEY, null); String username = ConfigurationManager.getInstance().readString(USERNAME_KEY, null);
String token = ConfigurationManager.getInstance().readString(TOKEN_KEY, null); String token = ConfigurationManager.getInstance().readString(TOKEN_KEY, null);
LOG.info("Updating user details for: " + username);
String res; String res;
try { try {
res = getService().updateUserDetails(username, token, getMachineId(), email, website, bio); res = getService().updateUserDetails(username, token, getMachineId(), email, website, bio);
@@ -283,6 +297,7 @@ public class CloudPresenter {
public List<ProjectEntity> fetchUserUploads(Integer projectId) throws CloudException { public List<ProjectEntity> fetchUserUploads(Integer projectId) throws CloudException {
UserEntity user = getUserDetails(); UserEntity user = getUserDetails();
LOG.info("Fetching all user uploads for: " + user.getUsername());
try { try {
Object res = getService().search("", "", 1, Integer.MAX_VALUE, getSortings()[0], user.getUsername(), projectId); Object res = getService().search("", "", 1, Integer.MAX_VALUE, getSortings()[0], user.getUsername(), projectId);
return processResults(res); return processResults(res);
@@ -322,6 +337,7 @@ public class CloudPresenter {
} }
public String[] getSortings() throws CloudException { public String[] getSortings() throws CloudException {
LOG.info("Fetching sortings");
try { try {
return getService().getSortings().toArray(new String[0]); return getService().getSortings().toArray(new String[0]);
} catch (Exception e) { } catch (Exception e) {
@@ -330,6 +346,7 @@ public class CloudPresenter {
} }
public List<CommentEntity> getComments(int projectId) throws CloudException { public List<CommentEntity> getComments(int projectId) throws CloudException {
LOG.info("Fetching comments for projectId: " + projectId);
try { try {
Object res = getService().getComments(projectId); Object res = getService().getComments(projectId);
if (res == null) if (res == null)
@@ -4,6 +4,14 @@ import java.util.List;
import org.diylc.plugins.cloud.model.ProjectEntity; import org.diylc.plugins.cloud.model.ProjectEntity;
/**
* Used to create paging of the search results. Use
* {@link PagingProvider#startSession(String, String, String)} to start the search session and then
* {@link PagingProvider#hasMoreData()} returns true if there's more data available and
* {@link PagingProvider#requestMoreData()} returns the next page.
*
* @author Branislav Stojkovic
*/
public class PagingProvider { public class PagingProvider {
private CloudPresenter cloudPresenter; private CloudPresenter cloudPresenter;
@@ -17,12 +25,7 @@ public class PagingProvider {
private List<ProjectEntity> currentResults; private List<ProjectEntity> currentResults;
public PagingProvider(CloudPresenter cloudPresenter) { public PagingProvider(CloudPresenter cloudPresenter) {
this(cloudPresenter, 10);
}
public PagingProvider(CloudPresenter cloudPresenter, int itemsPerPage) {
this.cloudPresenter = cloudPresenter; this.cloudPresenter = cloudPresenter;
this.itemsPerPage = itemsPerPage;
} }
public List<ProjectEntity> startSession(String searchFor, String category, String sort) throws CloudException { public List<ProjectEntity> startSession(String searchFor, String category, String sort) throws CloudException {
@@ -26,6 +26,12 @@ import org.diylc.plugins.cloud.presenter.CloudPresenter;
import org.diylc.swing.ISimpleView; import org.diylc.swing.ISimpleView;
import org.diylc.swing.gui.components.HTMLTextArea; import org.diylc.swing.gui.components.HTMLTextArea;
/**
* {@link JDialog} that shows a list of comments posted to a cloud project and provides controls for
* posting more comments.
*
* @author Branislav Stojkovic
*/
public class CommentDialog extends JDialog { public class CommentDialog extends JDialog {
private static final String JUST_NOW = "just now"; private static final String JUST_NOW = "just now";
@@ -45,7 +51,7 @@ public class CommentDialog extends JDialog {
private int lastPosition = 0; private int lastPosition = 0;
private ISimpleView owner; private ISimpleView cloudUI;
public CommentDialog(ISimpleView cloudUI, ProjectEntity project, List<CommentEntity> comments, public CommentDialog(ISimpleView cloudUI, ProjectEntity project, List<CommentEntity> comments,
CloudPresenter presenter) { CloudPresenter presenter) {
@@ -53,6 +59,7 @@ public class CommentDialog extends JDialog {
this.project = project; this.project = project;
this.comments = comments; this.comments = comments;
this.presenter = presenter; this.presenter = presenter;
this.cloudUI = cloudUI;
setMinimumSize(new Dimension(400, 600)); setMinimumSize(new Dimension(400, 600));
setContentPane(getMainPanel()); setContentPane(getMainPanel());
pack(); pack();
@@ -69,7 +76,7 @@ public class CommentDialog extends JDialog {
if (listPanel == null) { if (listPanel == null) {
listPanel = new JPanel(new GridBagLayout()); listPanel = new JPanel(new GridBagLayout());
listPanel.setBackground(Color.white); listPanel.setBackground(Color.white);
for (CommentEntity c : comments) { for (CommentEntity c : comments) {
addComment(c); addComment(c);
GridBagConstraints gbc = new GridBagConstraints(); GridBagConstraints gbc = new GridBagConstraints();
@@ -135,9 +142,9 @@ public class CommentDialog extends JDialog {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
final String comment = getReplyArea().getText(); final String comment = getReplyArea().getText();
if (comment.trim().length() == 0) if (comment.trim().length() == 0)
owner.showMessage("Cannot post an empty comment.", "Cloud Error", IView.ERROR_MESSAGE); cloudUI.showMessage("Cannot post an empty comment.", "Cloud Error", IView.ERROR_MESSAGE);
owner.executeBackgroundTask(new ITask<Void>() { cloudUI.executeBackgroundTask(new ITask<Void>() {
@Override @Override
public Void doInBackground() throws Exception { public Void doInBackground() throws Exception {
@@ -147,7 +154,7 @@ public class CommentDialog extends JDialog {
@Override @Override
public void failed(Exception e) { public void failed(Exception e) {
owner.showMessage("Error posting comment.", "Cloud Error", IView.ERROR_MESSAGE); cloudUI.showMessage("Error posting comment.", "Cloud Error", IView.ERROR_MESSAGE);
} }
@Override @Override
@@ -12,6 +12,7 @@ import javax.swing.JFrame;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JPasswordField; import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea; import javax.swing.JTextArea;
import javax.swing.JTextField; import javax.swing.JTextField;
import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentEvent;
@@ -128,7 +129,8 @@ public class UserEditDialog extends ButtonDialog {
mainPanel.add(getWebsiteField(), gbc); mainPanel.add(getWebsiteField(), gbc);
gbc.gridy = 5; gbc.gridy = 5;
mainPanel.add(getBioArea(), gbc); gbc.fill = GridBagConstraints.BOTH;
mainPanel.add(new JScrollPane(getBioArea()), gbc);
} }
return mainPanel; return mainPanel;
} }
@@ -32,6 +32,11 @@ import org.diylc.swing.ISimpleView;
import org.diylc.swing.ISwingUI; import org.diylc.swing.ISwingUI;
import org.diylc.utils.Pair; import org.diylc.utils.Pair;
/**
* {@link JFrame} for searching the projects on the cloud.
*
* @author Branislav Stojkovic
*/
public class CloudBrowserFrame extends JFrame implements ISimpleView { public class CloudBrowserFrame extends JFrame implements ISimpleView {
private static final String TITLE = "Search The Cloud"; private static final String TITLE = "Search The Cloud";
@@ -55,6 +55,14 @@ import org.diylc.swing.plugins.cloud.view.UploadDialog;
import org.diylc.swing.plugins.file.FileFilterEnum; import org.diylc.swing.plugins.file.FileFilterEnum;
import org.diylc.swingframework.ButtonDialog; import org.diylc.swingframework.ButtonDialog;
/**
* Component that is capable of showing a list of {@link ProjectEntity} objects. Can work in paging
* mode and pull one page of data at a time.
*
* @see {@link PagingProvider}
*
* @author Branislav Stojkovic
*/
public class ResultsScrollPanel extends JScrollPane { public class ResultsScrollPanel extends JScrollPane {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;