cloud work - user management

This commit is contained in:
Branislav Stojkovic
2016-10-22 15:09:10 +02:00
parent d8b915f8a5
commit 5b5b75142f
18 changed files with 192 additions and 118 deletions
@@ -1,4 +1,4 @@
package org.diylc.swing.plugins.cloud.model;
package org.diylc.plugins.cloud.model;
import java.io.Serializable;
@@ -1,4 +1,4 @@
package org.diylc.swing.plugins.cloud.model;
package org.diylc.plugins.cloud.model;
import java.io.File;
import java.util.List;
@@ -62,6 +62,19 @@ public interface ServiceAPI {
@ParamName("password") String password,
@ParamName("machineId") String machineId);
/***
* Retrieves current user's details.
*
* @param username
* @param password
* @param machineId
* @return string with an error message if it fails, or an instance of
* {@link UserEntity} if it succeeds.
*/
Object getUserDetails(@ParamName("username") String username,
@ParamName("token") String token,
@ParamName("machineId") String machineId);
/***
* Tries to authenticate with the token that was previously created by
* calling login() function.
@@ -1,4 +1,4 @@
package org.diylc.swing.plugins.cloud.model;
package org.diylc.plugins.cloud.model;
import java.io.Serializable;
@@ -6,46 +6,25 @@ public class UserEntity implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String userName;
private String password;
private String username;
private String email;
private String website;
private String bio;
public UserEntity(int id, String userName, String password, String email,
String website, String bio) {
public UserEntity(String userName, String email, String website, String bio) {
super();
this.id = id;
this.userName = userName;
this.password = password;
this.username = username;
this.email = email;
this.website = website;
this.bio = bio;
}
public int getId() {
return id;
public String getUsername() {
return username;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
@@ -78,11 +57,8 @@ public class UserEntity implements Serializable {
int result = 1;
result = prime * result + ((bio == null) ? 0 : bio.hashCode());
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + id;
result = prime * result
+ ((password == null) ? 0 : password.hashCode());
result = prime * result
+ ((userName == null) ? 0 : userName.hashCode());
+ ((username == null) ? 0 : username.hashCode());
result = prime * result + ((website == null) ? 0 : website.hashCode());
return result;
}
@@ -106,17 +82,10 @@ public class UserEntity implements Serializable {
return false;
} else if (!email.equals(other.email))
return false;
if (id != other.id)
return false;
if (password == null) {
if (other.password != null)
if (username == null) {
if (other.username != null)
return false;
} else if (!password.equals(other.password))
return false;
if (userName == null) {
if (other.userName != null)
return false;
} else if (!userName.equals(other.userName))
} else if (!username.equals(other.username))
return false;
if (website == null) {
if (other.website != null)
@@ -128,7 +97,7 @@ public class UserEntity implements Serializable {
@Override
public String toString() {
return "UserEntity [userName=" + userName + "]";
return "UserEntity [userName=" + username + "]";
}
}
@@ -1,4 +1,4 @@
package org.diylc.swing.plugins.cloud.presenter;
package org.diylc.plugins.cloud.presenter;
public class CloudException extends Exception {
@@ -1,4 +1,4 @@
package org.diylc.swing.plugins.cloud.presenter;
package org.diylc.plugins.cloud.presenter;
public interface CloudListener {
@@ -1,17 +1,15 @@
package org.diylc.swing.plugins.cloud.presenter;
package org.diylc.plugins.cloud.presenter;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
import org.diylc.appframework.miscutils.ConfigurationManager;
import org.diylc.swing.plugins.cloud.model.ServiceAPI;
import org.diylc.plugins.cloud.model.ServiceAPI;
import org.diylc.plugins.cloud.model.UserEntity;
import com.diyfever.httpproxy.PhpFlatProxy;
import com.diyfever.httpproxy.ProxyFactory;
@@ -160,6 +158,27 @@ public class CloudPresenter {
}
public UserEntity getUserDetails() throws CloudException {
String username = ConfigurationManager.getInstance().readString(
USERNAME_KEY, null);
String token = ConfigurationManager.getInstance().readString(TOKEN_KEY,
null);
Object res;
try {
res = service.getUserDetails(username, token, getMachineId());
} catch (Exception e) {
throw new CloudException(e);
}
if (res instanceof String)
throw new CloudException(res.toString());
if (res instanceof UserEntity)
return (UserEntity) res;
throw new CloudException(
"Unexpected server response received for category list: "
+ res.getClass().getName());
}
private String getMachineId() {
if (machineId == null) {
try {
@@ -1,4 +1,4 @@
package org.diylc.swing.plugins.cloud.presenter;
package org.diylc.plugins.cloud.presenter;
public class NotConnectedException extends Exception {
@@ -1,4 +1,4 @@
package org.diylc.swing.plugins.cloud.presenter;
package org.diylc.plugins.cloud.presenter;
public class NotExistsException extends Exception {
@@ -1,4 +1,4 @@
package org.diylc.swing.plugins.cloud.presenter;
package org.diylc.plugins.cloud.presenter;
public class NotLoggedInException extends Exception {
@@ -2,5 +2,7 @@
/.apt_generated
/log/
/build/
/library/
/lib/diylc-core.jar
autoSave.diy
config.xml
@@ -41,7 +41,8 @@ public enum IconLoader {
"briefcase_into.png"), RotateCW("rotate_cw.png"), RotateCCW(
"rotate_ccw.png"), ElementInto("element_into.png"), Arrow(
"arrow.png"), Cloud("cloud.png"), CloudUp("cloud_up.png"), CloudGear(
"cloud_gear.png"), IdCardEdit("id_card_edit.png");
"cloud_gear.png"), IdCardEdit("id_card_edit.png"), KeyEdit(
"key_edit.png");
protected String name;
Binary file not shown.

After

Width:  |  Height:  |  Size: 924 B

@@ -11,12 +11,12 @@ import javax.swing.filechooser.FileFilter;
import org.diylc.appframework.miscutils.ConfigurationManager;
import org.diylc.common.IPlugInPort;
import org.diylc.common.PropertyWrapper;
import org.diylc.plugins.cloud.model.UserEntity;
import org.diylc.plugins.cloud.presenter.CloudPresenter;
import org.diylc.swing.gui.components.OverwritePromptFileChooser;
import org.diylc.swing.gui.editor.PropertyEditorDialog;
import org.diylc.swing.plugins.cloud.model.UserEntity;
import org.diylc.swing.plugins.cloud.presenter.CloudPresenter;
import org.diylc.swing.plugins.cloud.view.LoginDialog;
import org.diylc.swing.plugins.cloud.view.NewUserDialog;
import org.diylc.swing.plugins.cloud.view.UserEditDialog;
import org.diylc.swing.plugins.cloud.view.UploadDialog;
import org.diylc.swing.plugins.file.BomDialog;
import org.diylc.swingframework.AboutDialog;
@@ -141,8 +141,8 @@ public class DialogFactory {
return dialog;
}
public NewUserDialog createNewUserDialog(UserEntity existingUser) {
NewUserDialog dialog = new NewUserDialog(mainFrame, existingUser);
public UserEditDialog createUserEditDialog(UserEntity existingUser) {
UserEditDialog dialog = new UserEditDialog(mainFrame, existingUser);
return dialog;
}
@@ -14,15 +14,16 @@ import org.diylc.common.IPlugInPort;
import org.diylc.common.ITask;
import org.diylc.core.IView;
import org.diylc.images.IconLoader;
import org.diylc.plugins.cloud.model.UserEntity;
import org.diylc.plugins.cloud.presenter.CloudException;
import org.diylc.plugins.cloud.presenter.CloudListener;
import org.diylc.plugins.cloud.presenter.CloudPresenter;
import org.diylc.presenter.Presenter;
import org.diylc.swing.ISwingUI;
import org.diylc.swing.gui.DialogFactory;
import org.diylc.swing.gui.DummyView;
import org.diylc.swing.plugins.cloud.presenter.CloudException;
import org.diylc.swing.plugins.cloud.presenter.CloudListener;
import org.diylc.swing.plugins.cloud.presenter.CloudPresenter;
import org.diylc.swing.plugins.cloud.view.LoginDialog;
import org.diylc.swing.plugins.cloud.view.NewUserDialog;
import org.diylc.swing.plugins.cloud.view.UserEditDialog;
import org.diylc.swing.plugins.cloud.view.UploadDialog;
import org.diylc.swing.plugins.file.FileFilterEnum;
import org.diylc.swingframework.ButtonDialog;
@@ -47,6 +48,7 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
private ManageAccountAction manageAccountAction;
private UploadAction uploadAction;
private ChangePasswordAction changePasswordAction;
private ManageProjectsAction manageProjectsAction;
public CloudPlugIn(ISwingUI swingUI) {
@@ -64,6 +66,7 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
swingUI.injectMenuAction(getManageProjectsAction(), ONLINE_TITLE);
swingUI.injectMenuAction(null, ONLINE_TITLE);
swingUI.injectMenuAction(getManageAccountAction(), ONLINE_TITLE);
swingUI.injectMenuAction(getChangePasswordAction(), ONLINE_TITLE);
swingUI.injectMenuAction(getLogOutAction(), ONLINE_TITLE);
// default state
@@ -147,6 +150,14 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
return uploadAction;
}
public ChangePasswordAction getChangePasswordAction() {
if (changePasswordAction == null)
{
changePasswordAction = new ChangePasswordAction();
}
return changePasswordAction;
}
public ManageProjectsAction getManageProjectsAction() {
if (manageProjectsAction == null) {
manageProjectsAction = new ManageProjectsAction();
@@ -247,8 +258,8 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
@Override
public void actionPerformed(ActionEvent e) {
NewUserDialog dialog = DialogFactory.getInstance()
.createNewUserDialog(null);
UserEditDialog dialog = DialogFactory.getInstance()
.createUserEditDialog(null);
dialog.setVisible(true);
}
}
@@ -265,9 +276,63 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
@Override
public void actionPerformed(ActionEvent e) {
NewUserDialog dialog = DialogFactory.getInstance()
.createNewUserDialog(null);
dialog.setVisible(true);
swingUI.executeBackgroundTask(new ITask<UserEntity>() {
@Override
public UserEntity doInBackground() throws Exception {
return cloudPresenter.getUserDetails();
}
@Override
public void failed(Exception e) {
swingUI.showMessage(
"Failed to retreive user details from the server.",
"Cloud Error", IView.ERROR_MESSAGE);
}
@Override
public void complete(UserEntity result) {
UserEditDialog dialog = DialogFactory.getInstance()
.createUserEditDialog(result);
dialog.setVisible(true);
}
});
}
}
class ChangePasswordAction extends AbstractAction {
private static final long serialVersionUID = 1L;
public ChangePasswordAction() {
super();
putValue(AbstractAction.NAME, "Change Password");
putValue(AbstractAction.SMALL_ICON, IconLoader.KeyEdit.getIcon());
}
@Override
public void actionPerformed(ActionEvent e) {
swingUI.executeBackgroundTask(new ITask<UserEntity>() {
@Override
public UserEntity doInBackground() throws Exception {
return cloudPresenter.getUserDetails();
}
@Override
public void failed(Exception e) {
swingUI.showMessage(
"Failed to retreive user details from the server.",
"Cloud Error", IView.ERROR_MESSAGE);
}
@Override
public void complete(UserEntity result) {
UserEditDialog dialog = DialogFactory.getInstance()
.createUserEditDialog(result);
dialog.setVisible(true);
}
});
}
}
@@ -26,8 +26,8 @@ import javax.swing.event.ChangeListener;
import org.diylc.common.DrawOption;
import org.diylc.common.IPlugInPort;
import org.diylc.swing.plugins.cloud.presenter.CloudException;
import org.diylc.swing.plugins.cloud.presenter.CloudPresenter;
import org.diylc.plugins.cloud.presenter.CloudException;
import org.diylc.plugins.cloud.presenter.CloudPresenter;
import org.diylc.swingframework.ButtonDialog;
public class UploadDialog extends ButtonDialog {
@@ -87,43 +87,38 @@ public class UploadDialog extends ButtonDialog {
gbc.gridheight = 1;
gbc.gridx = 1;
gbc.weightx = 0;
gbc.insets = new Insets(4, 2, 2, 2);
gbc.fill = GridBagConstraints.HORIZONTAL;
mainPanel.add(new JLabel("Project Name: "), gbc);
gbc.gridx = 2;
gbc.weightx = 1;
mainPanel.add(getNameField(), gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.weightx = 0;
mainPanel.add(new JLabel("Category:"), gbc);
gbc.gridx = 2;
gbc.weightx = 1;
mainPanel.add(getCategoryBox(), gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.weightx = 0;
mainPanel.add(new JLabel("Description: "), gbc);
gbc.gridy = 3;
mainPanel.add(new JLabel("Keywords: "), gbc);
gbc.gridx = 2;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.insets = new Insets(2, 2, 2, 2);
mainPanel.add(getNameField(), gbc);
gbc.gridy = 1;
mainPanel.add(getCategoryBox(), gbc);
gbc.gridy = 2;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
mainPanel.add(getDescriptionArea(), gbc);
gbc.gridx = 1;
gbc.gridy = 3;
gbc.weightx = 0;
gbc.weighty = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
mainPanel.add(new JLabel("Keywords: "), gbc);
gbc.gridx = 2;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
mainPanel.add(getKeywordsField(), gbc);
gbc.gridx = 0;
@@ -17,10 +17,10 @@ import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.diylc.swing.plugins.cloud.model.UserEntity;
import org.diylc.plugins.cloud.model.UserEntity;
import org.diylc.swingframework.ButtonDialog;
public class NewUserDialog extends ButtonDialog {
public class UserEditDialog extends ButtonDialog {
private static final long serialVersionUID = 1L;
@@ -41,13 +41,15 @@ public class NewUserDialog extends ButtonDialog {
private UserEntity existingEntity;
public NewUserDialog(JFrame owner, UserEntity existingEntity) {
super(owner, "New Account", new String[] { OK, CANCEL });
public UserEditDialog(JFrame owner, UserEntity existingEntity) {
super(owner, existingEntity == null ? "New Account" : "Manage Account",
new String[] { OK, CANCEL });
this.existingEntity = existingEntity;
setMinimumSize(new Dimension(240, 32));
layoutGui();
refreshState();
userNameField.setText(System.getProperty("user.name"));
this.existingEntity = existingEntity;
}
public String getUserName() {
@@ -77,20 +79,22 @@ public class NewUserDialog extends ButtonDialog {
mainPanel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.LINE_START;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(2, 2, 2, 2);
gbc.insets = new Insets(4, 2, 2, 2);
gbc.gridx = 0;
gbc.gridy = 0;
mainPanel.add(new JLabel("User Name:"), gbc);
gbc.gridy = 1;
mainPanel.add(new JLabel("Password:"), gbc);
if (existingEntity == null) {
gbc.gridy = 1;
mainPanel.add(new JLabel("Password:"), gbc);
gbc.gridy = 2;
mainPanel.add(new JLabel("Confirm Password:"), gbc);
gbc.gridy = 2;
mainPanel.add(new JLabel("Confirm Password:"), gbc);
}
gbc.gridy = 3;
mainPanel.add(new JLabel("eMail:"), gbc);
@@ -103,16 +107,19 @@ public class NewUserDialog extends ButtonDialog {
gbc.gridx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(2, 2, 2, 2);
gbc.weightx = 1;
gbc.gridy = 0;
mainPanel.add(getUserNameField(), gbc);
gbc.gridy = 1;
mainPanel.add(getPasswordField(), gbc);
if (existingEntity == null) {
gbc.gridy = 1;
mainPanel.add(getPasswordField(), gbc);
gbc.gridy = 2;
mainPanel.add(getConfirmPasswordField(), gbc);
gbc.gridy = 2;
mainPanel.add(getConfirmPasswordField(), gbc);
}
gbc.gridy = 3;
mainPanel.add(getEmailField(), gbc);
@@ -148,7 +155,7 @@ public class NewUserDialog extends ButtonDialog {
if (userNameField == null) {
userNameField = new JTextField();
if (existingEntity != null) {
userNameField.setText(existingEntity.getUserName());
userNameField.setText(existingEntity.getUsername());
userNameField.setEditable(false);
}
userNameField.getDocument().addDocumentListener(
@@ -282,6 +289,9 @@ public class NewUserDialog extends ButtonDialog {
bioArea = new JTextArea();
if (existingEntity != null)
bioArea.setText(existingEntity.getBio());
bioArea.setFont(getUserNameField().getFont());
bioArea.setBorder(getUserNameField().getBorder());
bioArea.setPreferredSize(new Dimension(192, 69));
bioArea.getDocument().addDocumentListener(new DocumentListener() {
@Override
@@ -8,7 +8,7 @@ import javax.swing.JFrame;
import javax.swing.JScrollPane;
import org.apache.log4j.BasicConfigurator;
import org.diylc.swing.plugins.cloud.model.ProjectEntity;
import org.diylc.plugins.cloud.model.ProjectEntity;
import org.diylc.swingframework.objecttable.IActionProcessor;
import org.diylc.swingframework.objecttable.ObjectListTable;