package org.projectapollo.demo.General.MysqlAdmin.Login;
import apollo.*;
import java.util.zip.*;
import java.io.*;
import apollo.Log.*;
import apollo.Template.*;
import apollo.Storable.*;
import apollo.Session.*;
import org.projectapollo.demo.Storable.*;
import java.util.*;
 
public class LoginBroker extends PageBroker {
    private Vector AS;
 
    AcceptClause displayEntry;
    AcceptClause mysqlAdminLogin;
    public LoginBroker(ManagerTracker MT, String fquid, PageBroker PB) throws TemplatePageException {
        super(MT,fquid,PB);
        MT.getPM().registerTemplatePage(FQUID,
new TemplatePage(MT,this, false));
        AS =
new Vector();
        displayEntry=
new AcceptClause("DisplayEntry");
        mysqlAdminLogin=
new AcceptClause("MysqlAdmin Login");
        mysqlAdminLogin.addAcceptClause(AcceptClause.STRING,
"MysqlAdminLogin", AcceptClause.REQUIRED);
        mysqlAdminLogin.addAcceptClause(AcceptClause.STRING,
"Hostname", AcceptClause.REQUIRED);
        mysqlAdminLogin.addAcceptClause(AcceptClause.STRING,
"Password", AcceptClause.REQUIRED);
        mysqlAdminLogin.addAcceptClause(AcceptClause.STRING,
"UserID", AcceptClause.REQUIRED);
        mysqlAdminLogin.addAcceptClause(AcceptClause.STRING,
"Database", AcceptClause.REQUIRED);
        AS.addElement(mysqlAdminLogin);
        AS.addElement(displayEntry);
    }
 
    public HTTPResponse render(TransactionTracker TT, HTTPRequest req, WebSession thisSession) throws ApolloException {
        EntryAssertionManager EAM =
new EntryAssertionManager(MT, AS);
        EAM.evaluate(req);
        User user = (User)thisSession.getValue(
"User");
        Hashtable RT =
new Hashtable();
        if (EAM.isAccept(mysqlAdminLogin)) {
             //Ok, now try to establish a database connection
            try {
                SerializableJDBCConnection sc =
new SerializableJDBCConnection("org.gjt.mm.mysql.Driver","jdbc:mysql://"+req.getValue("Hostname")+":"+req.getValue("Port")+"/"+req.getValue("Database")+"?user="+req.getValue("UserID")+"&password="+req.getValue("Password"));
 
                  // Class.forName("org.gjt.mm.mysql.Driver").newInstance();
                // Connection c = DriverManager.getConnection("jdbc:mysql://"+req.getValue("Hostname")+":"+req.getValue("Port")+"/"+req.getValue("Database")+"?user="+req.getValue("Username")+"&password="+req.getValue("Password"));
                // SerializableJDBCConnection sc = new SerializableJDBCConnection(c);
                thisSession.setValue("DBConnection", sc);
                thisSession.setValue(
"DatabaseName", req.getValue("Database"));
                thisSession.setValue(
"Hostname", req.getValue("Hostname"));
                thisSession.setValue(
"ServletPath", req.getServletPath());
                thisSession.setServletPath(req.getServletPath());
            }
catch (Exception e) {
                Hashtable hashReplacement =
new Hashtable();
                hashReplacement.put(
"Error",e.getMessage());
                return MT.getPM().getPage(FQUID).render(hashReplacement,thisSession);
            }
            return MT.getPM().handleRequest(TT,"MAMainMenu",req,thisSession);
        }
else {
            return MT.getPM().getPage(FQUID).render(RT,thisSession);
        }
    }
// end render
 
}