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;
/**
* Represents a single post in the comment section of a cloud project.
*
* @author Branislav Stojkovic
*/
public class CommentEntity implements Serializable {
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
*
* @author bancika
* @author Branislav Stojkovic
*
*/
public interface IServiceAPI {
@@ -4,6 +4,11 @@ import java.io.Serializable;
import org.diylc.core.annotations.EditableProperty;
/**
* Represents a cloud project object, identified by {@link ProjectEntity#id}.
*
* @author Branislav Stojkovic
*/
public class ProjectEntity implements Serializable {
private static final long serialVersionUID = 1L;
@@ -60,7 +65,7 @@ public class ProjectEntity implements Serializable {
public void setOwner(String owner) {
this.owner = owner;
}
public String getCategory() {
return category;
}
@@ -68,12 +73,13 @@ public class ProjectEntity implements Serializable {
public void setCategory(String 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() {
return categoryForDisplay;
}
public void setCategoryForDisplay(String categoryForDisplay) {
this.categoryForDisplay = categoryForDisplay;
}
@@ -2,6 +2,11 @@ package org.diylc.plugins.cloud.model;
import java.io.Serializable;
/**
* Represents cloud user details.
*
* @author Branislav Stojkovic
*/
public class UserEntity implements Serializable {
private static final long serialVersionUID = 1L;
@@ -24,6 +24,11 @@ import org.diylc.presenter.ComponentProcessor;
import com.diyfever.httpproxy.PhpFlatProxy;
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 {
private static String USERNAME_KEY = "cloud.Username";
@@ -119,6 +124,7 @@ public class CloudPresenter {
public String[] getCategories() throws CloudException {
if (categories == null) {
Object res;
LOG.info("Fetching categories");
try {
res = getService().getCategories();
if (res != null && res.equals(ERROR))
@@ -146,6 +152,7 @@ public class CloudPresenter {
if (username == null || token == null)
throw new CloudException("Login failed. Please try to login again.");
LOG.info("Uploading a new project: " + projectName);
try {
String res =
getService().uploadProject(username, token, getMachineId(), projectName, category, description, diylcVersion,
@@ -163,11 +170,12 @@ public class CloudPresenter {
if (username == null || token == null)
throw new CloudException("Login failed. Please try to login again.");
LOG.info("Updating project with projectId: " + project.getId());
try {
String res =
getService().uploadProject(username, token, getMachineId(), project.getName(), project.getCategoryForDisplay(),
project.getDescription(), diylcVersion, project.getKeywords(), null, null, project.getId());
getService().uploadProject(username, token, getMachineId(), project.getName(),
project.getCategoryForDisplay(), project.getDescription(), diylcVersion, project.getKeywords(), null,
null, project.getId());
if (!res.equals(SUCCESS))
throw new CloudException(res);
} catch (Exception e) {
@@ -182,6 +190,7 @@ public class CloudPresenter {
if (username == null || token == null)
throw new CloudException("Login failed. Please try to login again.");
LOG.info("Deleting project with projectId: " + projectId);
try {
String res = getService().deleteProject(username, token, getMachineId(), projectId);
if (!res.equals(SUCCESS))
@@ -198,6 +207,8 @@ public class CloudPresenter {
if (username == null || token == null)
throw new CloudException("Login failed. Please try to login again.");
LOG.info("Posting a new comment to projectId: " + projectId);
try {
String res = getService().postComment(username, token, getMachineId(), projectId, comment);
if (!res.equals(SUCCESS))
@@ -209,6 +220,7 @@ public class CloudPresenter {
public void createUserAccount(String username, String password, String email, String website, String bio)
throws CloudException {
LOG.info("Creating a new user: " + username);
try {
String res = getService().createUser(username, password, email, website, bio);
if (res == null)
@@ -223,7 +235,7 @@ public class CloudPresenter {
public UserEntity getUserDetails() throws CloudException {
String username = ConfigurationManager.getInstance().readString(USERNAME_KEY, null);
String token = ConfigurationManager.getInstance().readString(TOKEN_KEY, null);
LOG.info("Retreiving user details for: " + username);
Object res;
try {
res = getService().getUserDetails(username, token, getMachineId());
@@ -244,6 +256,7 @@ public class CloudPresenter {
public void updatePassword(String oldPassword, String newPassword) throws CloudException {
String username = ConfigurationManager.getInstance().readString(USERNAME_KEY, null);
LOG.info("Updating password for: " + username);
String res;
try {
res = getService().updatePassword(username, oldPassword, newPassword);
@@ -258,6 +271,7 @@ public class CloudPresenter {
String username = ConfigurationManager.getInstance().readString(USERNAME_KEY, null);
String token = ConfigurationManager.getInstance().readString(TOKEN_KEY, null);
LOG.info("Updating user details for: " + username);
String res;
try {
res = getService().updateUserDetails(username, token, getMachineId(), email, website, bio);
@@ -283,6 +297,7 @@ public class CloudPresenter {
public List<ProjectEntity> fetchUserUploads(Integer projectId) throws CloudException {
UserEntity user = getUserDetails();
LOG.info("Fetching all user uploads for: " + user.getUsername());
try {
Object res = getService().search("", "", 1, Integer.MAX_VALUE, getSortings()[0], user.getUsername(), projectId);
return processResults(res);
@@ -322,6 +337,7 @@ public class CloudPresenter {
}
public String[] getSortings() throws CloudException {
LOG.info("Fetching sortings");
try {
return getService().getSortings().toArray(new String[0]);
} catch (Exception e) {
@@ -330,6 +346,7 @@ public class CloudPresenter {
}
public List<CommentEntity> getComments(int projectId) throws CloudException {
LOG.info("Fetching comments for projectId: " + projectId);
try {
Object res = getService().getComments(projectId);
if (res == null)
@@ -4,6 +4,14 @@ import java.util.List;
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 {
private CloudPresenter cloudPresenter;
@@ -17,12 +25,7 @@ public class PagingProvider {
private List<ProjectEntity> currentResults;
public PagingProvider(CloudPresenter cloudPresenter) {
this(cloudPresenter, 10);
}
public PagingProvider(CloudPresenter cloudPresenter, int itemsPerPage) {
this.cloudPresenter = cloudPresenter;
this.itemsPerPage = itemsPerPage;
}
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.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 {
private static final String JUST_NOW = "just now";
@@ -45,7 +51,7 @@ public class CommentDialog extends JDialog {
private int lastPosition = 0;
private ISimpleView owner;
private ISimpleView cloudUI;
public CommentDialog(ISimpleView cloudUI, ProjectEntity project, List<CommentEntity> comments,
CloudPresenter presenter) {
@@ -53,6 +59,7 @@ public class CommentDialog extends JDialog {
this.project = project;
this.comments = comments;
this.presenter = presenter;
this.cloudUI = cloudUI;
setMinimumSize(new Dimension(400, 600));
setContentPane(getMainPanel());
pack();
@@ -69,7 +76,7 @@ public class CommentDialog extends JDialog {
if (listPanel == null) {
listPanel = new JPanel(new GridBagLayout());
listPanel.setBackground(Color.white);
for (CommentEntity c : comments) {
for (CommentEntity c : comments) {
addComment(c);
GridBagConstraints gbc = new GridBagConstraints();
@@ -135,9 +142,9 @@ public class CommentDialog extends JDialog {
public void actionPerformed(ActionEvent e) {
final String comment = getReplyArea().getText();
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
public Void doInBackground() throws Exception {
@@ -147,7 +154,7 @@ public class CommentDialog extends JDialog {
@Override
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
@@ -12,6 +12,7 @@ import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
@@ -128,7 +129,8 @@ public class UserEditDialog extends ButtonDialog {
mainPanel.add(getWebsiteField(), gbc);
gbc.gridy = 5;
mainPanel.add(getBioArea(), gbc);
gbc.fill = GridBagConstraints.BOTH;
mainPanel.add(new JScrollPane(getBioArea()), gbc);
}
return mainPanel;
}
@@ -32,6 +32,11 @@ import org.diylc.swing.ISimpleView;
import org.diylc.swing.ISwingUI;
import org.diylc.utils.Pair;
/**
* {@link JFrame} for searching the projects on the cloud.
*
* @author Branislav Stojkovic
*/
public class CloudBrowserFrame extends JFrame implements ISimpleView {
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.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 {
private static final long serialVersionUID = 1L;