282 lines
7.8 KiB
Java
282 lines
7.8 KiB
Java
package org.gcube.portlets.user.statisticalalgorithmsimporter.client;
|
|
|
|
import java.util.Date;
|
|
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.event.ImportCodeEvent;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.event.MainCodeSetEvent;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.event.SessionExpiredEvent;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.event.StatAlgoImporterRibbonEvent;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.event.UIStateEvent;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.maindata.MainCodeDescription;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.project.ProjectManager;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.rpc.StatAlgoImporterServiceAsync;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.type.SessionExpiredType;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.type.StatAlgoImporterRibbonType;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.type.UIStateType;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.upload.CodeUploadDialog;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.upload.ImportCodeDescription;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.utils.UtilsGXT3;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.Constants;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.exception.StatAlgoImporterSessionExpiredException;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.session.UserInfo;
|
|
import org.gcube.portlets.widgets.sessionchecker.client.CheckSession;
|
|
|
|
import com.allen_sauer.gwt.log.client.Log;
|
|
import com.google.gwt.event.shared.EventBus;
|
|
import com.google.gwt.event.shared.SimpleEventBus;
|
|
import com.google.gwt.i18n.client.LocaleInfo;
|
|
import com.google.gwt.user.client.Cookies;
|
|
import com.google.gwt.user.client.Timer;
|
|
import com.google.gwt.user.client.rpc.AsyncCallback;
|
|
import com.sencha.gxt.widget.core.client.container.BorderLayoutContainer;
|
|
|
|
/**
|
|
*
|
|
* @author "Giancarlo Panichi" <a
|
|
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
|
|
*
|
|
*/
|
|
public class StatAlgoImporterController {
|
|
|
|
private SimpleEventBus eventBus;
|
|
private UserInfo userInfo;
|
|
@SuppressWarnings("unused")
|
|
private BorderLayoutContainer mainPanel;
|
|
private ProjectManager pm;
|
|
|
|
public StatAlgoImporterController() {
|
|
eventBus = new SimpleEventBus();
|
|
pm = new ProjectManager(eventBus);
|
|
init();
|
|
}
|
|
|
|
private void init() {
|
|
callHello();
|
|
checkSession();
|
|
bindToEvents();
|
|
}
|
|
|
|
private void checkSession() {
|
|
// if you do not need to something when the session expire
|
|
CheckSession.getInstance().startPolling();
|
|
}
|
|
|
|
private void sessionExpiredShow() {
|
|
CheckSession.showLogoutDialog();
|
|
}
|
|
|
|
/**
|
|
* @return the eventBus
|
|
*/
|
|
public EventBus getEventBus() {
|
|
return eventBus;
|
|
}
|
|
|
|
public void setMainPanelLayout(BorderLayoutContainer mainPanel) {
|
|
this.mainPanel = mainPanel;
|
|
}
|
|
|
|
private void callHello() {
|
|
StatAlgoImporterServiceAsync.INSTANCE
|
|
.hello(new AsyncCallback<UserInfo>() {
|
|
|
|
@Override
|
|
public void onFailure(Throwable caught) {
|
|
Log.info("No valid user found: " + caught.getMessage());
|
|
if (caught instanceof StatAlgoImporterSessionExpiredException) {
|
|
UtilsGXT3.alert("Error", "Expired Session");
|
|
sessionExpiredShowDelayed();
|
|
} else {
|
|
UtilsGXT3.alert(
|
|
"Error",
|
|
"No user found: "
|
|
+ caught.getLocalizedMessage());
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onSuccess(UserInfo result) {
|
|
userInfo = result;
|
|
Log.info("Hello: " + userInfo.getUsername());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
private void sessionExpiredShowDelayed() {
|
|
Timer timeoutTimer = new Timer() {
|
|
public void run() {
|
|
sessionExpiredShow();
|
|
|
|
}
|
|
};
|
|
int TIMEOUT = 3; // 3 second timeout
|
|
|
|
timeoutTimer.schedule(TIMEOUT * 1000); // timeout is in milliseconds
|
|
|
|
}
|
|
|
|
protected void checkLocale() {
|
|
String[] locales = LocaleInfo.getAvailableLocaleNames();
|
|
|
|
for (String locale : locales) {
|
|
Log.debug("Locale avaible:" + locale);
|
|
}
|
|
|
|
String currentLocaleCookie = Cookies.getCookie(LocaleInfo
|
|
.getLocaleCookieName());
|
|
Log.debug(Constants.STATRUNNER_LANG_COOKIE + ":" + currentLocaleCookie);
|
|
|
|
LocaleInfo currentLocaleInfo = LocaleInfo.getCurrentLocale();
|
|
Log.debug("Current Locale:" + currentLocaleInfo.getLocaleName());
|
|
|
|
}
|
|
|
|
protected void changeLanguage(String localeName) {
|
|
Date now = new Date();
|
|
long nowLong = now.getTime();
|
|
nowLong = nowLong + (1000 * 60 * 60 * 24 * 21);
|
|
now.setTime(nowLong);
|
|
String cookieLang = Cookies.getCookie(Constants.STATRUNNER_LANG_COOKIE);
|
|
if (cookieLang != null) {
|
|
Cookies.removeCookie(Constants.STATRUNNER_LANG_COOKIE);
|
|
}
|
|
Cookies.setCookie(Constants.STATRUNNER_LANG_COOKIE, localeName, now);
|
|
com.google.gwt.user.client.Window.Location.reload();
|
|
}
|
|
|
|
//
|
|
public void restoreUISession() {
|
|
checkLocale();
|
|
}
|
|
|
|
// Bind Controller to events on bus
|
|
private void bindToEvents() {
|
|
eventBus.addHandler(SessionExpiredEvent.TYPE,
|
|
new SessionExpiredEvent.SessionExpiredEventHandler() {
|
|
|
|
@Override
|
|
public void onSessionExpired(SessionExpiredEvent event) {
|
|
Log.debug("Catch Event SessionExpiredEvent");
|
|
doSessionExpiredCommand(event);
|
|
|
|
}
|
|
});
|
|
|
|
eventBus.addHandler(StatAlgoImporterRibbonEvent.TYPE,
|
|
new StatAlgoImporterRibbonEvent.StatRunnerRibbonEventHandler() {
|
|
|
|
@Override
|
|
public void onSelect(StatAlgoImporterRibbonEvent event) {
|
|
Log.debug("Catch StatAlgoImportRibbonEvent");
|
|
doMenuCommand(event);
|
|
|
|
}
|
|
});
|
|
|
|
eventBus.addHandler(ImportCodeEvent.TYPE,
|
|
new ImportCodeEvent.ImportCodeEventHandler() {
|
|
|
|
@Override
|
|
public void onImportCode(ImportCodeEvent event) {
|
|
Log.debug("Catch ImportCodeEvent");
|
|
doImportCodeCommand(event);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
eventBus.addHandler(MainCodeSetEvent.TYPE,
|
|
new MainCodeSetEvent.MainCodeSetEventHandler() {
|
|
|
|
@Override
|
|
public void onMainCodeSet(MainCodeSetEvent event) {
|
|
Log.debug("Catch MainCodeSetEvent");
|
|
doMainCodeSetCommand(event);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
eventBus.fireEvent(new UIStateEvent(UIStateType.START));
|
|
|
|
}
|
|
|
|
private void doMenuCommand(StatAlgoImporterRibbonEvent event) {
|
|
StatAlgoImporterRibbonType eventType = event.getStatRunnerRibbonType();
|
|
if (eventType == null) {
|
|
return;
|
|
}
|
|
Log.debug("StatAlgoRibbonEvent: " + event);
|
|
|
|
switch (eventType) {
|
|
case PROJECT_CREATE:
|
|
showCreateProjectDialog();
|
|
break;
|
|
case PROJECT_OPEN:
|
|
break;
|
|
case ALGORITHM_CREATE:
|
|
break;
|
|
case ALGORITHM_IMPORT:
|
|
showCodeUploadDialog();
|
|
break;
|
|
case HELP:
|
|
break;
|
|
default:
|
|
break;
|
|
|
|
}
|
|
}
|
|
|
|
private void showCreateProjectDialog() {
|
|
pm.createProject();
|
|
|
|
}
|
|
|
|
|
|
private void doMainCodeSetCommand(MainCodeSetEvent event) {
|
|
MainCodeDescription mainCodeDescription = event.getMainCodeDescription();
|
|
if (mainCodeDescription != null
|
|
&& mainCodeDescription.getId() != null) {
|
|
pm.setMainCode(mainCodeDescription);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
private void doImportCodeCommand(ImportCodeEvent event) {
|
|
ImportCodeDescription importCodeDesc = event.getImportCodeDescription();
|
|
if (importCodeDesc != null
|
|
&& importCodeDesc.getImportCodeType() != null) {
|
|
pm.importCode(importCodeDesc);
|
|
}
|
|
|
|
}
|
|
|
|
private void showCodeUploadDialog() {
|
|
CodeUploadDialog codeUploadDialog = new CodeUploadDialog(eventBus);
|
|
codeUploadDialog.show();
|
|
}
|
|
|
|
private void doSessionExpiredCommand(SessionExpiredEvent event) {
|
|
Log.debug("Session Expired Event: " + event.getSessionExpiredType());
|
|
sessionExpiredShow();
|
|
|
|
}
|
|
|
|
@SuppressWarnings("unused")
|
|
private void asyncCodeLoadingFailed(Throwable reason) {
|
|
Log.error("Async code loading failed", reason);
|
|
eventBus.fireEvent(new SessionExpiredEvent(
|
|
SessionExpiredType.EXPIREDONSERVER));
|
|
|
|
}
|
|
|
|
}
|