package org.gcube.portlets.admin.authportletmanager.client;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.gcube.portlets.admin.authportletmanager.client.event.AddPoliciesEvent;
import org.gcube.portlets.admin.authportletmanager.client.event.AddQuoteEvent;
import org.gcube.portlets.admin.authportletmanager.client.event.ListPolicyEvent;
import org.gcube.portlets.admin.authportletmanager.client.event.ListQuoteEvent;
import org.gcube.portlets.admin.authportletmanager.client.event.RemovePoliciesEvent;
import org.gcube.portlets.admin.authportletmanager.client.event.RemoveQuoteEvent;
import org.gcube.portlets.admin.authportletmanager.client.event.UpdatePolicyEvent;
import org.gcube.portlets.admin.authportletmanager.client.event.UpdateQuoteEvent;
import org.gcube.portlets.admin.authportletmanager.client.pagelayout.DialogError;
import org.gcube.portlets.admin.authportletmanager.client.pagelayout.DialogLoader;
import org.gcube.portlets.admin.authportletmanager.client.pagelayout.PolicyAddDialog;
import org.gcube.portlets.admin.authportletmanager.client.pagelayout.PolicyDataGrid;
import org.gcube.portlets.admin.authportletmanager.client.pagelayout.PolicyDataProvider;
import org.gcube.portlets.admin.authportletmanager.client.pagelayout.PolicyDeleteDialog;
import org.gcube.portlets.admin.authportletmanager.client.pagelayout.PolicyFilter;
import org.gcube.portlets.admin.authportletmanager.client.pagelayout.QuotaFilter;
import org.gcube.portlets.admin.authportletmanager.client.pagelayout.QuoteAddDialog;
import org.gcube.portlets.admin.authportletmanager.client.pagelayout.QuoteDataGrid;
import org.gcube.portlets.admin.authportletmanager.client.pagelayout.QuoteDataProvider;
import org.gcube.portlets.admin.authportletmanager.client.pagelayout.QuoteDeleteDialog;
import org.gcube.portlets.admin.authportletmanager.client.resource.AuthResources;
import org.gcube.portlets.admin.authportletmanager.client.rpc.AuthManagerServiceAsync;
import org.gcube.portlets.admin.authportletmanager.shared.Caller;
import org.gcube.portlets.admin.authportletmanager.shared.ConstantsSharing;
import org.gcube.portlets.admin.authportletmanager.shared.PolicyAuth;
import org.gcube.portlets.admin.authportletmanager.shared.Quote;
import org.gcube.portlets.admin.authportletmanager.shared.exceptions.ExpiredSessionServiceException;
import org.gcube.portlets.widgets.sessionchecker.client.CheckSession;
import com.github.gwtbootstrap.client.ui.Container;
import com.github.gwtbootstrap.client.ui.Tab;
import com.github.gwtbootstrap.client.ui.TabPanel;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
/**
*
* @author "Alessandro Pieve " alessandro.pieve@isti.cnr.it
*
*/
public class AuthManagerController {
public static SimpleEventBus eventBus;
private static final String JSP_TAG_ID = "AuthPortletManager";
/** The MainPanell pg mng. */
private Container mainPanelLayout;
/*Start Component for Tab Policy */
/** The SearchPolicy pg mng. */
private PolicyFilter policyfilter =new PolicyFilter();
/** The policyDataGrid pg mng. */
private PolicyDataGrid policydatagrid =new PolicyDataGrid();
/*Start Component for Tab Quota */
/** The SearchQuota pg mng. */
private QuotaFilter quotafilter =new QuotaFilter();
/** The quoteDataGrid pg mng. */
private QuoteDataGrid quotedatagrid =new QuoteDataGrid();
/** Dialog Box for Loading*/
private DialogLoader dialogLoader = new DialogLoader();
/** Dialog Box for Error*/
private DialogError dialogError = new DialogError();
private Integer countLoader=0;
// public String result_test;
public AuthManagerController() {
eventBus = new SimpleEventBus();
init();
}
private void init() {
loadMainPanel();
loadEnviromentPolicy();
//loadEnviromentQuote();
bindToEvents();
}
@SuppressWarnings("unused")
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;
}
private void loadMainPanel(){
GWT.log("AuthManager - LoadMainPanel");
GWT.log("AuthManager -Init Start with -DEBUG_MODE:"+ConstantsSharing.DEBUG_MODE+" DEBUG_TOKEN:"+ConstantsSharing.DEBUG_TOKEN+" MOCKUP:"+ConstantsSharing.MOCK_UP);
AuthResources.INSTANCE.authCSS().ensureInjected();
mainPanelLayout = new Container();
mainPanelLayout.setId("mainPanelLayout_AuthPortlet");
mainPanelLayout.setWidth("98%");
//Panel Policy
DockPanel dockPolicy = new DockPanel();
dockPolicy.setStyleName("Management-Panel");
dockPolicy.setWidth("100%");
dockPolicy.add(policyfilter, DockPanel.NORTH);
dockPolicy.add(policydatagrid, DockPanel.SOUTH);
Tab tab_policy =new Tab();
tab_policy.add(dockPolicy);
tab_policy.setHeading("POLICY");
//Panel Quota
DockPanel dockQuota = new DockPanel();
dockQuota.setStyleName("Management-Panel-Quota");
dockQuota.setWidth("100%");
dockQuota.add(quotafilter, DockPanel.NORTH);
dockQuota.add(quotedatagrid, DockPanel.SOUTH);
/*
Tab tab_quota =new Tab();
tab_quota.add(dockQuota);
tab_quota.setHeading("QUOTA");
*/
TabPanel tabPanel = new TabPanel();
tabPanel.add(tab_policy);
//tabPanel.add(tab_quota);
tab_policy.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
GWT.log("AuthManager - Tab Policy");
loadEnviromentPolicy();
}
});
/*
tab_quota.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
GWT.log("AuthManager - Tab Quota");
loadEnviromentQuote();
}
});
*/
//select first tab
tabPanel.selectTab(0);
mainPanelLayout.add(tabPanel);
bind(mainPanelLayout);
}
protected void bind(Widget mainWidget) {
try {
RootPanel root = RootPanel.get(JSP_TAG_ID);
//Log.info("Root Panel: " + root);
GWT.log("AuthManager - Root Panel: " + root, null);
if (root == null) {
GWT.log("AuthManager - Div with id " + JSP_TAG_ID + " not found, starting in dev mode");
RootPanel.get().add(mainWidget);
} else {
GWT.log("AuthManager - Div with id " + JSP_TAG_ID + " found, starting in portal mode");
root.add(mainWidget);
}
} catch (Exception e) {
e.printStackTrace();
GWT.log("AuthManager - Error in attach viewport:" + e.getLocalizedMessage(), null);
}
}
private void loadEnviromentPolicy() {
countLoader=0;
dialogLoader.startLoader();
loadPolicy(4);
loadCaller(4);
loadServicePolicy(4);
loadAccessPolicy(4);
}
private void loadEnviromentQuote() {
countLoader=0;
dialogLoader.startLoader();
loadCaller(2);
loadQuote(2);
}
private void loadPolicy(final int numberLoad){
//reset provider
PolicyDataProvider.get().resetPolicyProvider();
AuthManagerServiceAsync.INSTANCE
.loadListPolicy(new AsyncCallback>() {
@Override
public void onFailure(Throwable caught) {
if (caught instanceof ExpiredSessionServiceException) {
GWT.log("AuthManager - Alert Expired Session");
sessionExpiredShowDelayed();
} else {
dialogLoader.stopLoader();
dialogError.showError(caught.getLocalizedMessage());
GWT.log("AuthManager - Failed to load a list policy:"+caught.getLocalizedMessage());
caught.printStackTrace();
}
}
@Override
public void onSuccess(ArrayList result) {
PolicyDataProvider.get().loadPolicyProvider(result);
GWT.log("AuthManager - Load Policy complete");
checkLoader(numberLoad,0);
}
});
}
/**
* Load a caller from AsyncCallBack from server
*/
private void loadCaller(final int numberLoad){
AuthManagerServiceAsync.INSTANCE
.loadListCaller(new AsyncCallback>() {
@Override
public void onFailure(Throwable caught) {
if (caught instanceof ExpiredSessionServiceException) {
GWT.log("AuthManager - Alert Expired Session");
sessionExpiredShowDelayed();
} else {
dialogLoader.stopLoader();
dialogError.showError(caught.getLocalizedMessage());
GWT.log("AuthManager - Failed to load a caller:"+caught.getLocalizedMessage());
caught.printStackTrace();
}
}
@Override
public void onSuccess(ArrayList callerList) {
Entities.getInstance();
Entities.setCallers(callerList);
GWT.log("AuthManager - Load Caller complete");
checkLoader(numberLoad,0);
}
});
}
public void loadServicePolicy(final int numberLoad){
AuthManagerServiceAsync.INSTANCE
.loadListService(new AsyncCallback