343 lines
9.0 KiB
Java
343 lines
9.0 KiB
Java
package org.gcube.portlets.user.statisticalalgorithmsimporter.client;
|
|
|
|
import java.util.Date;
|
|
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.event.DeleteItemEvent;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.event.InputReadyEvent;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.event.InputRequestEvent;
|
|
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.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.upload.CodeUploadDialog;
|
|
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.user.statisticalalgorithmsimporter.shared.workspace.ItemDescription;
|
|
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;
|
|
|
|
|
|
private enum InputRequestType {
|
|
Save,
|
|
SoftwareCreate;
|
|
}
|
|
private InputRequestType inputRequestType;
|
|
|
|
|
|
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(MainCodeSetEvent.TYPE,
|
|
new MainCodeSetEvent.MainCodeSetEventHandler() {
|
|
|
|
@Override
|
|
public void onMainCodeSet(MainCodeSetEvent event) {
|
|
Log.debug("Catch MainCodeSetEvent");
|
|
doMainCodeSetCommand(event);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
eventBus.addHandler(DeleteItemEvent.TYPE,
|
|
new DeleteItemEvent.DeleteItemEventHandler() {
|
|
|
|
@Override
|
|
public void onDelete(DeleteItemEvent event) {
|
|
Log.debug("Catch DeleteItemEvent");
|
|
doDeleteItemCommand(event);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
eventBus.addHandler(InputReadyEvent.TYPE,
|
|
new InputReadyEvent.InputReadyEventHandler() {
|
|
|
|
@Override
|
|
public void onInputReady(InputReadyEvent event) {
|
|
Log.debug("Catch InputReadyEvent");
|
|
doInputReadyCommand(event);
|
|
|
|
}
|
|
});
|
|
|
|
pm.startProjectManager();
|
|
|
|
}
|
|
|
|
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:
|
|
showOpenProjectDialog();
|
|
break;
|
|
case PROJECT_SAVE:
|
|
projectSaveRequest();
|
|
break;
|
|
case RESOURCE_ADD:
|
|
showAddResourceDialog();
|
|
break;
|
|
case SOFTWARE_CREATE:
|
|
softwareCreateRequest();
|
|
break;
|
|
case HELP:
|
|
break;
|
|
default:
|
|
break;
|
|
|
|
}
|
|
}
|
|
|
|
private void doInputReadyCommand(InputReadyEvent event) {
|
|
switch(inputRequestType){
|
|
case Save:
|
|
pm.saveProject(event.getInputData());
|
|
break;
|
|
case SoftwareCreate:
|
|
pm.createSofware(event.getInputData());
|
|
break;
|
|
default:
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private void projectSaveRequest() {
|
|
inputRequestType=InputRequestType.Save;
|
|
InputRequestEvent inputRequestEvent = new InputRequestEvent();
|
|
eventBus.fireEvent(inputRequestEvent);
|
|
Log.debug("ProjectSaveRequest: " + inputRequestEvent);
|
|
}
|
|
|
|
private void softwareCreateRequest() {
|
|
inputRequestType=InputRequestType.SoftwareCreate;
|
|
InputRequestEvent inputRequestEvent = new InputRequestEvent();
|
|
eventBus.fireEvent(inputRequestEvent);
|
|
Log.debug("SoftwareCreateRequest: " + inputRequestEvent);
|
|
|
|
}
|
|
|
|
|
|
private void showCreateProjectDialog() {
|
|
pm.createProject();
|
|
|
|
}
|
|
|
|
private void showOpenProjectDialog() {
|
|
pm.openProject();
|
|
|
|
}
|
|
|
|
private void showAddResourceDialog() {
|
|
pm.addResource();
|
|
}
|
|
|
|
|
|
|
|
private void doMainCodeSetCommand(MainCodeSetEvent event) {
|
|
ItemDescription itemDescription = event.getItemDescription();
|
|
if (itemDescription != null && itemDescription.getId() != null) {
|
|
pm.setMainCode(itemDescription);
|
|
}
|
|
|
|
}
|
|
|
|
private void doDeleteItemCommand(DeleteItemEvent event) {
|
|
ItemDescription itemDescription = event.getItemDescription();
|
|
if (itemDescription != null && itemDescription.getId() != null) {
|
|
pm.deleteItem(itemDescription);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
|
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));
|
|
|
|
}
|
|
|
|
}
|