package org.projectapollo.demo.Storable;

import java.util.*;

import apollo.ManagerTracker;
import apollo.TransactionTracker;
import apollo.Storable.*;

import java.io.*;

import apollo.Job.*;

import org.projectapollo.demo.*;

/** User
*
* Created from MysqlAdmin
*/

public
class User extends Storable implements Serializable {
    final static public Object[][] fieldType = {
        {
"userID", new StorableType(StorableType.VARCHAR, 50), String.class, False },
        {
"firstName", new StorableType(StorableType.VARCHAR, 50), String.class, False },
        {
"lastName", new StorableType(StorableType.VARCHAR, 50), String.class, False },
        {
"passwordField", new StorableType(StorableType.VARCHAR, 12), String.class, False },
        {
"lastLoginDate", new StorableType(StorableType.DOUBLE), Date.class, True },
        {
"numLogins", new StorableType(StorableType.INTEGER), Integer.class, False },
        {
"emailAddress", new StorableType(StorableType.VARCHAR, 75), String.class, False },
        {
"securityLevel", new StorableType(StorableType.INTEGER), Integer.class, False },
    };

    final static public String[] fieldPrimaryKey = { "userID" };
 
    //These indexes are probably not used by any of the apollodemo queries, and really shouldn't exist. But they're here
    // so you can see how you add indexes to objects.
    /* final static public Object[][] fieldIndex = {
        { "index1", NON_UNIQUE, new String[] {"numLogins", "lastLoginDate"} },
        { "index2", NON_UNIQUE, new String[] {"emailAddress" } }
    };*/

    private String userID;
    private String firstName;
    private String lastName;
    private String password;
    private Date lastLoginDate;
    private Integer numLogins;
    private String emailAddress;
    private Integer securityLevel;
 
    public User(String userID, String firstName, String lastName, String password, Date lastLoginDate, Integer numLogins, String emailAddress, Integer securityLevel) {
        super(fieldType, fieldPrimaryKey);
        super.testStorableConfiguration(this);
        this.userID = userID;
        this.firstName = firstName;
        this.lastName = lastName;
        this.password = password;
        this.lastLoginDate = lastLoginDate;
        this.numLogins = numLogins;
        this.emailAddress = emailAddress;
        this.securityLevel = securityLevel;
    }

    public User(String userID, String firstName, String lastName, String password, String emailAddress) {
        super(fieldType, fieldPrimaryKey);
        super.testStorableConfiguration(this);
        this.userID = userID;
        this.firstName = firstName;
        this.lastName = lastName;
        this.password = password;
        this.emailAddress = emailAddress;
        this.securityLevel=new Integer(0);
        this.numLogins=new Integer(0);
    }
 
    public Object[] getFieldValues() {
        return new Object[] { userID, firstName, lastName, password, lastLoginDate, numLogins, emailAddress, securityLevel };
    }

    public String getUserID() {
        return this.userID;
    }

    public void setUserID(String userID) {
        this.userID=userID;
        setUpdatedField(
"userID");
    }

    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName=firstName;
        setUpdatedField(
"firstName");
    }

    public String getLastName() {
        return this.lastName;
    }

    public void setLastName(String lastName) {
        this.lastName=lastName;
        setUpdatedField(
"lastName");
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password=password;
        setUpdatedField(
"password");
    }

    public Date getLastLoginDate() {
        return this.lastLoginDate;
    }
 
    public void setLastLoginDate(Date lastLoginDate) {
        this.lastLoginDate=lastLoginDate;
        setUpdatedField(
"lastLoginDate");
    }

    public Integer getNumLogins() {
        return this.numLogins;
    }

    public void setNumLogins(Integer numLogins) {
        this.numLogins=numLogins;
        setUpdatedField(
"numLogins");
    }
 
    public String getEmailAddress(){
        return this.emailAddress;
    }
 
    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
        setUpdatedField(
"emailAddress");
    }
 
    public Integer getSecurityLevel() {
        return this.securityLevel;
    }

    public void setSecurityLevel(Integer securityLevel) {
        this.securityLevel=securityLevel;
        setUpdatedField(
"securityLevel");
    }

    static public User loadInstance(ManagerTracker MT, Object[] theLoadKey) {
        return (User) Storable.loadInstance(MT, User.class, theLoadKey);
    }

    static public User[] loadArrayInstance(ManagerTracker MT, WhereStatement whereStatement) {
        return (User[]) Storable.loadArrayInstance(MT, User.class, whereStatement);
    }

    static public Vector loadVectorInstance(ManagerTracker MT, WhereStatement whereStatement) {
        return Storable.loadVectorInstance(MT, User.class, whereStatement);
    }
 
    public static void main(String[] s) {
        ManagerTracker MT =
new ManagerTracker(ManagerTracker.STANDALONE);
        Properties prop =
new Properties();
        try {
            String propFileName =
"/usr/local/apache/apollodemo/org/projectapollo/demo/demo.properties";
            prop.load(
new FileInputStream(propFileName));
        }
catch (IOException io) {
            io.printStackTrace();
            System.exit(-
1);
        }
        try {
            MT.setDM(
new DatabaseManager(MT));
            DBConnectors.connectAdapters(MT,prop);
        }
catch (Exception e) {
            e.printStackTrace();
            System.out.println(
"Exception"+e);
        }

        TransactionTracker TT =
new TransactionTracker(MT);
         //Storable.begin(TT,User.class);
        //Storable.commit(TT,User.class);
        System.out.println(User.loadInstance(MT, new Object[] {"joe"}));
 
       
/* Test insert */
        User joe = new User("joe", "Joe", "Kislo", "jk", null, new Integer(0), "[email protected]",new Integer(0));
        joe.insert(TT);
 
        User user_from_database = User.loadInstance(TT,
new Object[] {"joe"});
       
if (joe.getFieldValues() != user_from_database.getFieldValues()) {
            System.out.println(
"1: Inserted user does not match retrieved user");
        }

        joe.remove(TT);
       
/* Test Insert */
        User tom = new User("tom", "Tom", "Yza", "jzk", null, new Integer(0), "[email protected]",new Integer(0));
        tom.insert(TT);

        User jill =
new User("jill", "Jill", "Xyz", "zk", null, new Integer(0), "[email protected]",new Integer(0));
        jill.insert(TT);

        User jane =
new User("jane", "Jane", "Kislo", "jkz", null, new Integer(0), "[email protected]",new Integer(0));
        jane.insert(TT);

        User sam =
new User("sam", "Sam", "Kislo", "jk", null, new Integer(0), "[email protected]",new Integer(0));
        sam.insert(TT);
 
 
        User user =
new User("joe", "Joe", "Kislo", "jk", null, new Integer(0), "[email protected]",new Integer(0));
        user.insert(TT);
        Date date =
new Date();
       
for (int i = 0 ; i<1000; i++) {
           
//Vector v = User.loadVectorInstance(TT, new RawWhereQuery(""));
            User u = User.loadInstance(TT, new Object[] {"joe"});
        }

        System.out.println((
new Date()).getTime()-date.getTime());
        date =
new Date();
       
for (int i = 0 ; i<1000; i++) {
           
//Vector v = User.loadVectorInstance(TT, new RawWhereQuery(""));
            User u = User.loadInstance(TT, new Object[] {"joe"});
        }

        System.out.println((
new Date()).getTime()-date.getTime());

        date =
new Date();
       
for (int i = 0 ; i<1000; i++) {
            user.remove(TT);
            user.insert(TT);
        }

        System.out.println((
new Date()).getTime()-date.getTime());
        GregorianCalendar gc =
new GregorianCalendar();
        gc.set(Calendar.MINUTE,
0);
        gc.set(Calendar.HOUR,
0);
        gc.set(Calendar.SECOND,
0);
 
        Date startOfToday = gc.getTime();
        WhereQuery wq =
new WhereQuery();
        wq.insertWhereClause(WhereQuery.AND,
"lastLoginDate", User.class, startOfToday, WhereQuery.GREATER);
 
        Vector usersLoggedInToday = User.loadVectorInstance(TT, wq);
 
       
//Something using a join
 
        wq =
new WhereQuery();
        wq.insertWhereClause(WhereQuery.AND,
"userID", User.class, "strUserID", JobMembership.class);
        wq.insertWhereClause(WhereQuery.AND,
"jobID", JobRecord.class, "jobID", JobMembership.class);
        wq.insertWhereClause(WhereQuery.AND,
"jobID", JobRecord.class, "jobID", JobMembership.class);
        wq.insertWhereClause(WhereQuery.AND,
"resultTypeID", JobRecord.class, new Integer(JobManager.JOB_FAILED));
        wq.insertWhereClause(WhereQuery.AND,
"startDate", JobRecord.class, startOfToday, WhereQuery.GREATER);

       
//Vector of user Objects from users who had Jobs fail today
        Vector usersWithFailedJobsToday = User.loadVectorInstance(TT, wq);

        //Vector of those failed JobRecords
        Vector failedJobRecordsToday = JobRecord.loadVectorInstance(TT, wq);

        wq =
new WhereQuery();
        wq.insertWhereClause(WhereQuery.AND,
"numLogins", User.class, new Integer(2), WhereQuery.GREATER_EQUAL);
        java.sql.ResultSet rs = (java.sql.ResultSet) Storable.loadFunction(TT,
new StorableFunction("UserTable.firstName, sum(UserTable.numLogins)"), new Class[] {User.class}, new String[] {"firstName"},wq);
        System.out.println(rs);
 
       
// System.out.println(u);
        System.out.println(User.loadInstance(TT, new Object[] {"joe"}));
        System.out.println(
"COMMIT");
        Storable.commit(TT, User.
class);
        System.out.println(
"COMMIT");
        TT.finalize();
        System.out.println(
"FINALIZED");
        MT.destroy();
    }
}