package de.database.nanoleaf;

import java.io.Serializable;
import java.util.List;

import org.eclnt.editor.annotations.CCGenClass;
import org.eclnt.jsfserver.defaultscreens.Statusbar;
import org.eclnt.jsfserver.pagebean.PageBean;

import de.tvo.database.tables.NLCONNECT.Nlconnect;
import de.tvo.database.tables.NLCONNECT.NlconnectWorker;
import de.tvo.nanoleaf.enums.NanoTypeEnum;

@CCGenClass (expressionBase="#{d.CreateConnectionBean}")

public class CreateConnectionBean
    extends PageBean 
    implements Serializable
{
    public void onLoad(org.eclnt.jsfserver.base.faces.event.ActionEvent event) {
    	System.out.println("Load...");
    	List<Nlconnect> nlconnectList = null;
    	
    	try {
			NlconnectWorker worker = new NlconnectWorker();
			nlconnectList = worker.loadNlconnect();
			
			if (nlconnectList != null && nlconnectList.size() > 0) {
				m_nlconnect = nlconnectList.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
			
			Statusbar.outputWarning("Fehler beim laden!!!");
		}
    	
    }

    public void onClear(org.eclnt.jsfserver.base.faces.event.ActionEvent event) {
    	System.out.println("clear");
    	m_nlconnect = new Nlconnect();
    }

    public void onSave(org.eclnt.jsfserver.base.faces.event.ActionEvent event) {
    	boolean saved = true;
    	try {
			NlconnectWorker worker = new NlconnectWorker();
			saved = worker.insertNlconnect(getNlconnect());
		} catch (Exception e) {
			e.printStackTrace();
			saved = false;
			Statusbar.outputWarning("Fehler beim speichern!!!");
		}
    	
    	if (saved) {
    		m_nlconnect = new Nlconnect();
    		m_test = false;
    		Statusbar.outputMessage("Connection gespeichert.");
    	}
    	
    	
    }

	private boolean m_test = true;
	public boolean getTest() { return m_test; }
	
	
    // ------------------------------------------------------------------------
    // inner classes
    // ------------------------------------------------------------------------
    
    

	/* Listener to the user of the page bean. */
    public interface IListener extends Serializable {
    }
    
    // ------------------------------------------------------------------------
    // members
    // ------------------------------------------------------------------------
    
    private IListener m_listener;
    
    // ------------------------------------------------------------------------
    // constructors & initialization
    // ------------------------------------------------------------------------

    public CreateConnectionBean() {
    	/*
    	if (getTest()) {
    		m_nlconnect = new Nlconnect();
    		
    		//m_nlconnect.setNcid(new BigInteger("1"));
    		m_nlconnect.setNchostname("Canvas-72FA.fritz.box");
    		m_nlconnect.setNcipadress("192.168.178.141");
    		m_nlconnect.setNcport(16021);
    		m_nlconnect.setNccountpanel(15);
    		m_nlconnect.setNctoken("LAG28BQWrXK2p79NbHMQsvcYYCN1TcD4");
    		m_nlconnect.setNctype(NanoTypeEnum.CANVAS);
    		m_nlconnect.setNcactiv(1);
    	}
    	*/
    }

    @Override
    public String getPageName() { return "/database/nanoleaf/createConnection.xml"; }
    @Override
    public String getRootExpressionUsedInPage() { return "#{d.CreateConnectionBean}"; }

    // ------------------------------------------------------------------------
    // public usage
    // ------------------------------------------------------------------------
    private Nlconnect m_nlconnect = new Nlconnect();
    public void setNlconnect(Nlconnect nlconnect) {
		m_nlconnect = nlconnect;
	}

	public Nlconnect getNlconnect() { return m_nlconnect; }

	/* Initialization of the bean. Add any parameter that is required within your scenario. */
    public void prepare(IListener listener)
    {
        m_listener = listener;
    }

    // ------------------------------------------------------------------------
    // private usage
    // ------------------------------------------------------------------------
}
