cloud work

This commit is contained in:
bancika
2016-10-21 18:02:10 +02:00
parent eef4988177
commit c75711642a
3 changed files with 243 additions and 33 deletions
@@ -0,0 +1,8 @@
package org.diylc.swing.plugins.online.presenter;
public interface CloudListener {
public void loggedIn();
public void loggedOut();
}
@@ -12,31 +12,79 @@ import com.diyfever.httpproxy.ProxyFactory;
public class OnlinePresenter {
private static String USERNAME_KEY = "cloud.Username";
private static String TOKEN_KEY = "cloud.token";
private final static Logger LOG = Logger.getLogger(OnlinePresenter.class);
private ServiceAPI service;
private String serviceUrl;
private String machineId;
public OnlinePresenter() {
private CloudListener listener;
private boolean loggedIn = false;
public OnlinePresenter(CloudListener listener) {
serviceUrl = ConfigurationManager.getInstance().readString(
ServiceAPI.URL_KEY, "http://www.diy-fever.com/diylc/api");
ProxyFactory factory = new ProxyFactory(new PhpFlatProxy());
service = factory.createProxy(ServiceAPI.class, serviceUrl);
this.listener = listener;
}
public boolean login(String username, String password) {
public boolean logIn(String username, String password) {
LOG.info("Trying to log in to cloud as " + username);
String res = service.login(username, password, getMachineId());
if (res.equals("Error"))
{
if (res.equals("Error")) {
LOG.info("Log in failed");
return false;
}
else {
} else {
LOG.info("Log in success");
ConfigurationManager.getInstance().writeValue(USERNAME_KEY,
username);
ConfigurationManager.getInstance().writeValue(TOKEN_KEY, res);
listener.loggedIn();
this.loggedIn = true;
return true;
}
}
public String getMachineId() {
public boolean tryLogInWithToken() {
String username = ConfigurationManager.getInstance().readString(
USERNAME_KEY, null);
String token = ConfigurationManager.getInstance().readString(TOKEN_KEY,
null);
if (username != null && token != null) {
LOG.info("Trying to log in to cloud using a token as " + username);
String res = service
.loginWithToken(username, token, getMachineId());
if (res.equals("Error")) {
LOG.info("Log in failed");
return false;
} else {
LOG.info("Log in success");
listener.loggedIn();
this.loggedIn = true;
return true;
}
} else
return false;
}
public void logOut() {
LOG.info("Logged out");
ConfigurationManager.getInstance().writeValue(TOKEN_KEY, null);
this.loggedIn = false;
listener.loggedOut();
}
public boolean isLoggedIn() {
return loggedIn;
}
private String getMachineId() {
if (machineId == null) {
try {
InetAddress ip = InetAddress.getLocalHost();
@@ -57,6 +105,7 @@ public class OnlinePresenter {
} catch (Exception e) {
machineId = "Generic";
}
LOG.info("Local machine id: " + machineId);
}
return machineId;
}
@@ -2,68 +2,146 @@ package org.diylc.swing.plugins.online;
import java.awt.event.ActionEvent;
import java.util.EnumSet;
import java.util.concurrent.ExecutionException;
import javax.swing.AbstractAction;
import javax.swing.SwingWorker;
import org.apache.log4j.Logger;
import org.diylc.common.EventType;
import org.diylc.common.IPlugIn;
import org.diylc.common.IPlugInPort;
import org.diylc.core.IView;
import org.diylc.images.IconLoader;
import org.diylc.swing.ISwingUI;
import org.diylc.swing.gui.DialogFactory;
import org.diylc.swing.plugins.online.presenter.CloudListener;
import org.diylc.swing.plugins.online.presenter.OnlinePresenter;
import org.diylc.swing.plugins.online.view.LoginDialog;
import org.diylc.swing.plugins.online.view.NewUserDialog;
import org.diylc.swing.plugins.online.view.UploadDialog;
import org.diylc.swingframework.ButtonDialog;
import org.diylc.swingframework.ProgressDialog;
public class OnlineManager implements IPlugIn {
public class OnlineManager implements IPlugIn, CloudListener {
private static final String ONLINE_TITLE = "Project Cloud";
private final static Logger LOG = Logger.getLogger(OnlineManager.class);
private ISwingUI swingUI;
private IPlugInPort plugInPort;
private OnlinePresenter onlinePresenter;
private OnlinePresenter onlinePresenter;
private LibraryAction libraryAction;
private LoginAction loginAction;
private LogOutAction logOutAction;
private CreateAccountAction createAccountAction;
private ManageAccountAction manageAccountAction;
private UploadAction uploadAction;
private ManageProjectsAction manageProjectsAction;
public OnlineManager(ISwingUI swingUI) {
super();
// plugInPort.injectMenuAction(new LibraryAction(), ONLINE_TITLE);
// plugInPort.injectMenuAction(null, ONLINE_TITLE);
swingUI.injectMenuAction(new LoginAction(), ONLINE_TITLE);
swingUI.injectMenuAction(new CreateAccountAction(), ONLINE_TITLE);
this.swingUI = swingUI;
swingUI.injectMenuAction(getLibraryAction(), ONLINE_TITLE);
swingUI.injectMenuAction(null, ONLINE_TITLE);
swingUI.injectMenuAction(new UploadAction(), ONLINE_TITLE);
swingUI.injectMenuAction(new ManageProjectsAction(), ONLINE_TITLE);
swingUI.injectMenuAction(getLoginAction(), ONLINE_TITLE);
swingUI.injectMenuAction(getCreateAccountAction(), ONLINE_TITLE);
swingUI.injectMenuAction(null, ONLINE_TITLE);
swingUI.injectMenuAction(getUploadAction(), ONLINE_TITLE);
swingUI.injectMenuAction(getManageProjectsAction(), ONLINE_TITLE);
swingUI.injectMenuAction(null, ONLINE_TITLE);
swingUI.injectMenuAction(getManageAccountAction(), ONLINE_TITLE);
swingUI.injectMenuAction(getLogOutAction(), ONLINE_TITLE);
// default state
getUploadAction().setEnabled(false);
getManageProjectsAction().setEnabled(false);
getLogOutAction().setEnabled(false);
}
@Override
public void connect(IPlugInPort plugInPort) {
this.plugInPort = plugInPort;
this.onlinePresenter = new OnlinePresenter();
this.onlinePresenter = new OnlinePresenter(this);
initialize();
}
private void initialize() {
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {
@Override
protected Void doInBackground() throws Exception {
//libraryPresenter.connectDb();
return null;
protected Boolean doInBackground() throws Exception {
return onlinePresenter.tryLogInWithToken();
}
@Override
protected void done() {
// if (libraryPresenter.isLoggedIn()) {
//
// }
try {
if (get())
loggedIn();
} catch (Exception e) {
LOG.error("Error while trying to login with token", e);
}
}
};
worker.execute();
}
public LibraryAction getLibraryAction() {
if (libraryAction == null) {
libraryAction = new LibraryAction();
}
return libraryAction;
}
public LoginAction getLoginAction() {
if (loginAction == null) {
loginAction = new LoginAction();
}
return loginAction;
}
public LogOutAction getLogOutAction() {
if (logOutAction == null) {
logOutAction = new LogOutAction();
}
return logOutAction;
}
public CreateAccountAction getCreateAccountAction() {
if (createAccountAction == null) {
createAccountAction = new CreateAccountAction();
}
return createAccountAction;
}
public ManageAccountAction getManageAccountAction() {
if (manageAccountAction == null) {
manageAccountAction = new ManageAccountAction();
}
return manageAccountAction;
}
public UploadAction getUploadAction() {
if (uploadAction == null) {
uploadAction = new UploadAction();
}
return uploadAction;
}
public ManageProjectsAction getManageProjectsAction() {
if (manageProjectsAction == null) {
manageProjectsAction = new ManageProjectsAction();
}
return manageProjectsAction;
}
@Override
public EnumSet<EventType> getSubscribedEventTypes() {
return null;
@@ -79,7 +157,7 @@ public class OnlineManager implements IPlugIn {
public LibraryAction() {
super();
putValue(AbstractAction.NAME, "Browse Library");
putValue(AbstractAction.NAME, "Browse The Cloud");
putValue(AbstractAction.SMALL_ICON, IconLoader.Chest.getIcon());
}
@@ -100,9 +178,41 @@ public class OnlineManager implements IPlugIn {
@Override
public void actionPerformed(ActionEvent e) {
LoginDialog dialog = DialogFactory.getInstance().createLoginDialog();
dialog.setVisible(true);
onlinePresenter.login(dialog.getName(), dialog.getPassword());
LoginDialog dialog = DialogFactory.getInstance()
.createLoginDialog();
do {
dialog.setVisible(true);
if (ButtonDialog.OK.equals(dialog.getSelectedButtonCaption())) {
if (onlinePresenter.logIn(dialog.getUserName(),
dialog.getPassword())) {
swingUI.showMessage(
"You have successfully logged into the system. You will remain logged in on this machine until logged out.",
"Login Successful", IView.INFORMATION_MESSAGE);
break;
} else {
swingUI.showMessage(
"Could not login. Possible reasons are wrong credentials or lack of internet connection.",
"Login Error", IView.ERROR_MESSAGE);
}
} else
break;
} while (true);
}
}
class LogOutAction extends AbstractAction {
private static final long serialVersionUID = 1L;
public LogOutAction() {
super();
putValue(AbstractAction.NAME, "Log Out");
putValue(AbstractAction.SMALL_ICON, IconLoader.IdCard.getIcon());
}
@Override
public void actionPerformed(ActionEvent e) {
onlinePresenter.logOut();
}
}
@@ -118,7 +228,26 @@ public class OnlineManager implements IPlugIn {
@Override
public void actionPerformed(ActionEvent e) {
NewUserDialog dialog = DialogFactory.getInstance().createNewUserDialog(null);
NewUserDialog dialog = DialogFactory.getInstance()
.createNewUserDialog(null);
dialog.setVisible(true);
}
}
class ManageAccountAction extends AbstractAction {
private static final long serialVersionUID = 1L;
public ManageAccountAction() {
super();
putValue(AbstractAction.NAME, "Manage Account");
putValue(AbstractAction.SMALL_ICON, IconLoader.IdCard.getIcon());
}
@Override
public void actionPerformed(ActionEvent e) {
NewUserDialog dialog = DialogFactory.getInstance()
.createNewUserDialog(null);
dialog.setVisible(true);
}
}
@@ -135,7 +264,8 @@ public class OnlineManager implements IPlugIn {
@Override
public void actionPerformed(ActionEvent e) {
UploadDialog dialog = DialogFactory.getInstance().createUploadDialog();
UploadDialog dialog = DialogFactory.getInstance()
.createUploadDialog();
dialog.setVisible(true);
}
}
@@ -152,9 +282,32 @@ public class OnlineManager implements IPlugIn {
@Override
public void actionPerformed(ActionEvent e) {
ProgressDialog dialog = DialogFactory.getInstance().createProgressDialog("Test",
new String[] {}, "Some text", false);
ProgressDialog dialog = DialogFactory.getInstance()
.createProgressDialog("Test", new String[] {}, "Some text",
false);
dialog.setVisible(true);
}
}
@Override
public void loggedIn() {
getLoginAction().setEnabled(false);
getCreateAccountAction().setEnabled(false);
getLogOutAction().setEnabled(true);
getManageAccountAction().setEnabled(true);
getUploadAction().setEnabled(true);
getManageProjectsAction().setEnabled(true);
}
@Override
public void loggedOut() {
getLoginAction().setEnabled(true);
getCreateAccountAction().setEnabled(true);
getLogOutAction().setEnabled(false);
getManageAccountAction().setEnabled(false);
getUploadAction().setEnabled(false);
getManageProjectsAction().setEnabled(false);
}
}