package de.tvo.workplace;

import org.eclnt.jsfserver.defaultscreens.OKPopup;
import org.eclnt.workplace.WorkpageDefaultStarter;

public class DefaultWorkpageStarter extends WorkpageDefaultStarter {

	private static final long serialVersionUID = 1L;

	private boolean m_test;
	public boolean getTest() { return m_test; }
	
//	@Override
//    public IWorkpage startWorkpage(IWorkpageDispatcher workpageDispatcher, IWorkpageContainer workpageContainer, WorkpageStartInfo startInfo) {
//		try {
//			//System.out.println("startWorkpage Juhu: " + startInfo.getWindowTitle());
//			if ((startInfo instanceof WorkpageDefaultStarterInfo)) {
//		        DefaultWorkplaceBean wui = DefaultWorkplaceBean.findWorkplaceUIOfSession();
//		        try {
//		        	if (wui != null) {
//		        		wui.hideNavigation();	
//		        	}
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//	        
//	        if (startInfo.getOpenMultipleInstances() == false) {
//	        	IWorkpage wp = workpageContainer.getWorkpageForId(startInfo.getId());
//	        	if (wp != null) {
//	        		switchToAlreadyOpenedWorkpage(workpageDispatcher,workpageContainer,wp);
//	        		return wp;
//	        	}
//	        }
//	        
//	        if (!(startInfo instanceof WorkpageDefaultStarterInfo)) {
//	        	startInfo = new WorkpageDefaultStarterInfo(startInfo);
//	        }
//	        
//	        
//	        // Load Rechte
//	        WorkpageDefaultStarterInfo workpageGeisStarterInfo = (WorkpageDefaultStarterInfo)startInfo;
//	        SessionVars sessionVars = workpageGeisStarterInfo.getSessionVars();
//	        m_test = WorkplaceConfig.getWorkplaceEnvironment();
//	        
//	        String progNodeId = workpageGeisStarterInfo.getParam(StarterInfoParm.NODEKNID.name());
//	        if (progNodeId == null || progNodeId.equals("") || progNodeId.equals("null")) {
//	        	Statusbar.outputError("progNodeId: \"" + progNodeId + "\"");
//	        	//System.out.println("progNodeId: \"" + progNodeId + "\"");
//	        	//progNodeId = "0";
//	        }
//	        String mandantID = workpageGeisStarterInfo.getParam(StarterInfoParm.MANDANTID.name());
//	        if (mandantID == null || mandantID.equals("") || mandantID.equals("null")) {
//	        	Statusbar.outputError("mandantID prüfen: \"" + mandantID + "\"");
//	        	//System.out.println("mandantID prüfen: \"" + mandantID + "\"");
//	        	//mandantID = "0";
//	        }
//	        
//	        //System.out.println("progNodeId: " + progNodeId);
//	        //System.out.println("mandantID: " + mandantID);
//	        Long usrid = sessionVars.getGpuser().getUsid();
//	        List<Long> groupIdList = RightsInfo.getUserIdGroupsIdMap().get(usrid);
//	        
//	        //#463804 Geisportal - Fehler beim Programmstart
//	        if (groupIdList == null) {
//	    		Method staticMethod = RightsInfo.class.getDeclaredMethod("loadAllGroupsFromUser", Long.class);
//	    		staticMethod.setAccessible(true);
//	    		staticMethod.invoke(null, usrid);
//	    		
//
//	    		staticMethod = RightsInfo.class.getDeclaredMethod("loadAllMandantsFromUser", Long.class);
//	    		staticMethod.setAccessible(true);
//	    		staticMethod.invoke(null, usrid);
//	        	
//	        	groupIdList = RightsInfo.getUserIdGroupsIdMap().get(usrid);
//	        }
//	        
//	        Rights rights = getRightsHandler().loadRights(usrid, groupIdList, Long.parseLong(progNodeId), Long.parseLong(mandantID));
//	        
//	        workpageGeisStarterInfo.setRights(rights);
//	        workpageGeisStarterInfo.setSessionVars(sessionVars);
//
//	        for (PropertyInfo propertyInfo : rights.getModulProperties()) {
//	        	if(propertyInfo.getPropertyKey().trim().equals("SERVER_START") && propertyInfo.getPropertyStatus() == PropertyStatus.ACTIV) {
//	        		System.out.println("RemoteStartBean: " + propertyInfo.getPropertyValue());
//	    	        startInfo.setPageBeanName("RemoteStartBean");
//	        	}
//			}
//
//	        //MK: Wir merken unds hier die Eigendlich Klasse die gestartet werden soll, dies weird dann vom Menu mitgeladen und wieder zurueckgesetzt
//	        workpageGeisStarterInfo.setStartPageBeanName(startInfo.getPageBeanName());
//
//	        if (!workpageGeisStarterInfo.getParamMap().containsKey("POPUP")) {
//		        //MK: Wir oeffnen das Menu das called dann das Eigendliche Programm.
//		        startInfo.setPageBeanName("PortalMenuBean");
//	        }
//
//	        try {
//	        	if (SessionUtil.getHttpSessionID().contains(".") && !workpageGeisStarterInfo.getStartPageBeanName().equals("FavoritsUI")) {
//	        		Gpopenapps openedApp = new Gpopenapps();
//	        		openedApp.setOaportal("Webportal");
//	        		openedApp.setOaserver(SessionUtil.getServerHostname());
//	        		openedApp.setOatomcat(SessionUtil.getHttpSessionID().split("\\.")[1]);
//	        		openedApp.setOapversion(WorkplaceVersion.getPortalVersion());
//	        		openedApp.setOatest(WorkplaceConfig.getWorkplaceEnvironment() ? 1 : 0);
//	        		
//	        		boolean collectUserInfo = PIC_AppHistoryBean.logUserData;;
//	        		if (collectUserInfo) {
//	        			UserLoginInfo userLoginInfo = SessionHandler.getSessionIDUserLoginInfoMap().get(sessionVars.getSessionID());
//	        			openedApp.setOaclientip(userLoginInfo.getClientIP());
//	        			openedApp.setOauser(userLoginInfo.getGpuser().getUsusern());
//	        			openedApp.setOasession(userLoginInfo.getSessionTyp().name());
//	        			openedApp.setOalogin(userLoginInfo.getLoginType().name());
//	        		}
//	        		
//	        		openedApp.setOabeanname(workpageGeisStarterInfo.getStartPageBeanName());
//	        		openedApp.setOabeantitl(workpageGeisStarterInfo.getSelectorTitle());
//	        		
//	        		openedApp.setCrtime(new Date());
//	        		
//	        		new DBHandler(WorkplaceConfig.getWorkplaceEnvironment() ? SchemataENUM.GEISWPT : SchemataENUM.GEISWPE).insertObject(openedApp);
//	        	}
//	        } catch (Throwable t) {
//	        	t.printStackTrace();
//	        }
//	        
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//        
//        return super.startWorkpage(workpageDispatcher, workpageContainer, startInfo);
//    }
	
	
//	RightsHandler m_rightsHandler;
//	public RightsHandler getRightsHandler() {
//		if (m_rightsHandler == null) {
//			m_rightsHandler = new RightsHandler(getTest());
//		}
//		return m_rightsHandler;
//	}
	
    /**
     * Called if an error occurs when starting a page bean. The page bean may
     * e.g. throw an Exception/Error within the constructor.
     * <br><br>
     * Default behavior, implemented in this class: the message of the error
     * is output as OKPopup, so that the user gest notified. - You may override
     * this method in order to output the error information in some different
     * way.
     */
    @Override
	protected void reactOnProblemDuringWorkpageStart(Throwable t) {
        while (true) {
            if (t.getCause() == null) {
                break;
            } else {
                t = t.getCause();
            }
        }
        if (t instanceof NullPointerException && t.getMessage().equals("null")) {
        	//Hier läuft das Portal rein, wenn eine Anwendung sich beim Programmstart automatisch schließt
        	//z. B. bei Locks bei Semikron oder deaktivierten VDA-Lieferanten in der Erfassung
        } else {
        	OKPopup.createInstance("", t.getMessage());
        }
        
    }
}
