Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

DemoAccountBean.java

00001 package edu.virtualschool.jwaa.bean;
00002 import java.sql.Timestamp;
00003 import java.util.ArrayList;
00004 import java.util.Iterator;
00005 import java.util.List;
00006 import java.util.NoSuchElementException;
00007 
00008 import org.apache.log4j.Logger;
00009 
00010 import edu.virtualschool.jwaa.AccountAbstraction;
00011 import edu.virtualschool.jwaa.Fault;
00012 import edu.virtualschool.jwaa.GenericAccount;
00013 import edu.virtualschool.jwaa.StringUtil;
00014 import edu.virtualschool.jwaa.TimeUtil;
00015 import edu.virtualschool.jwaa.UserFault;
00016 import edu.virtualschool.jwaa.dbms.DB;
00017 import edu.virtualschool.jwaa.dbms.DBQuery;
00018 import edu.virtualschool.jwaa.dbms.DBUpdate;
00019 import edu.virtualschool.jwaa.dbms.DBUtil;
00020 import edu.virtualschool.jwaa.field.CityField;
00021 import edu.virtualschool.jwaa.field.CommaSeparatedField;
00022 import edu.virtualschool.jwaa.field.CountryField;
00023 import edu.virtualschool.jwaa.field.EmailField;
00024 import edu.virtualschool.jwaa.field.NameField;
00025 import edu.virtualschool.jwaa.field.OpField;
00026 import edu.virtualschool.jwaa.field.PasswordField;
00027 import edu.virtualschool.jwaa.field.PhoneField;
00028 import edu.virtualschool.jwaa.field.StreetField;
00029 import edu.virtualschool.jwaa.field.USStateField;
00030 import edu.virtualschool.jwaa.field.Validatable;
00031 import edu.virtualschool.jwaa.field.ZipcodeField;
00032 import edu.virtualschool.jwaa.xml.GenericForm;
00033 import edu.virtualschool.jwaa.xml.ValidationFault;
00034 import edu.virtualschool.jwaa.xml.Controller;
00035 
00036 public class DemoAccountBean extends GenericAccount implements AccountAbstraction
00037 {
00038   public EmailField email;
00039   public PasswordField password;
00040   public NameField name;
00041   public StreetField street;
00042   public CityField city;
00043   public USStateField state;
00044   public ZipcodeField zipcode;
00045   public CountryField country;
00046   public PhoneField phone;
00047   public Timestamp whenCreated;
00048 
00049   private final static Logger logger =
00050     Logger.getLogger(DemoAccountBean.class.getName());
00051 
00052   DemoAccountBean(
00053     List roles,
00054     EmailField email,
00055     PasswordField password,
00056     NameField name,
00057     StreetField street,
00058     CityField city,
00059     USStateField state,
00060     ZipcodeField zipcode,
00061     CountryField country,
00062     PhoneField phone,
00063     Timestamp whenCreated)
00064   {
00065     super(email.stringValue, roles);
00066     this.email = email;
00067     this.password = password;
00068     this.name = name;
00069     this.street = street;
00070     this.city = city;
00071     this.state = state;
00072     this.zipcode = zipcode;
00073     this.country = country;
00074     this.phone = phone;
00075     this.whenCreated = whenCreated;
00076   }
00077   public static DemoAccountBean load(DBQuery query) 
00078     throws Fault
00079   {
00080     CommaSeparatedField roles = new CommaSeparatedField(query.getString("roles"), "");
00081     DemoAccountBean person = new DemoAccountBean(
00082       DemoRole.createRoleList(roles),
00083       new EmailField(query.getString("email"), ""),
00084       new PasswordField(query.getString("password"), ""),
00085       new NameField(query.getString("name"), ""),
00086       new StreetField(query.getString("street"), ""),
00087       new CityField(query.getString("city"), ""),
00088       new USStateField(query.getString("state"), ""),
00089       new ZipcodeField(query.getString("zipcode"), ""),
00090       new CountryField(query.getString("country"), ""),
00091       new PhoneField(query.getString("phone"), ""),
00092       query.getTimestamp("whenCreated")
00093     );
00094     return person;
00095   }
00096   public static Iterator loadAll(final DB dbms, Object sortBy) throws Fault
00097   {
00098     final String sql = "select * from DemoAccount order by " + sortBy;
00099     return new DBQuery.Iterator(dbms, sql)
00100     {
00101       public Object next()
00102       {
00103         try
00104         {
00105           return DemoAccountBean.load(query);
00106         }
00107         catch (Fault e)
00108         {
00109           logger.error("SQL Fault: " + sql, e);
00110           throw new NoSuchElementException(sql);
00111         }
00112       }
00113     };
00114   }
00115   private static List createRoleList(CommaSeparatedField field)
00116     throws ValidationFault
00117   {
00118     List list = new ArrayList();
00119     for (Iterator iterator = field.arrayValue.iterator(); iterator.hasNext(); )
00120     {
00121       Object o = iterator.next();
00122       String name = (String) o;
00123       DemoRole role = DemoRole.findRole(name);
00124       list.add(role);
00125     }
00126     return list;
00127   }
00128   public static DemoAccountBean findByEmail(DB dbms, EmailField email)
00129     throws Fault
00130   {
00131     String sql = "select * from DemoAccount \n"
00132         + "where email=" + DBUtil.quote(email);
00133     DBQuery query = dbms.query(sql);
00134     if (!query.hasNext())
00135       throw new UserFault("Couldn't find account with email: " + email);
00136     DemoAccountBean person = load(query);
00137     query.close();
00138     return person;
00139   }
00140   public static DemoAccountBean findByEmailAndPassword(DB dbms, EmailField email, PasswordField password)
00141      throws Fault
00142   {
00143     String sql = "select * from DemoAccount\n"
00144         + "where email=" + DBUtil.quote(email.stringValue) + "\n"
00145         + "and password=" + DBUtil.quote(password.stringValue);
00146     DBQuery query = dbms.query(sql);
00147     if (!query.hasNext())
00148       throw new UserFault("No account has that email and password");
00149     DemoAccountBean person = load(query);
00150     query.close();
00151     return person;
00152   }
00153   public EmailField getEmail()
00154   {
00155     return email;
00156   }
00157   public PasswordField getPassword()
00158   {
00159     return password;
00160   }
00161   public NameField getName()
00162   {
00163     return name;
00164   }
00165   public PhoneField getPhone()
00166   {
00167     return phone;
00168   }
00169   public StreetField getStreet()
00170   {
00171     return street;
00172   }
00173   public CityField getCity()
00174   {
00175     return city;
00176   }
00177   public USStateField getState()
00178   {
00179     return state;
00180   }
00181   public ZipcodeField getZipcode()
00182   {
00183     return zipcode;
00184   }
00185   public CountryField getCountry()
00186   {
00187     return country;
00188   }
00189   public boolean isNull()
00190   {
00191     return this == DemoAccountBean.Null;
00192   }
00193   public Timestamp getWhenCreated()
00194   {
00195     return whenCreated;
00196   }
00197   public void save(DB dbms) throws Fault
00198   {
00199     String sql =  "replace into DemoAccount set "
00200         + "email=?,"
00201         + "password=?,"
00202         + "roles=?, "
00203         + "name=?,"
00204         + "street=?,"
00205         + "city=?,"
00206         + "state=?,"
00207         + "zipcode=?,"
00208         + "country=?,"
00209         + "phone=?,"
00210         + "whenCreated=?";
00211     DBUpdate s = dbms.update(sql);
00212     s.setString(email);
00213     s.setString(password);
00214     s.setString(StringUtil.join(',', roleList));
00215     s.setString(name);
00216     s.setString(street);
00217     s.setString(city);
00218     s.setString(state);
00219     s.setString(zipcode);
00220     s.setString(country);
00221     s.setString(phone);
00222     s.setTimestamp(whenCreated);
00223     int n = s.executeUpdate();
00224     s.close();
00225   }
00226   public void update(
00227     EmailField email,
00228     PasswordField password,
00229     NameField name,
00230     StreetField street,
00231     CityField city,
00232     USStateField state,
00233     ZipcodeField zipcode,
00234     CountryField country,
00235     PhoneField phone
00236   )
00237   {
00238     this.email = email;
00239     this.password = password;
00240     this.name = name;
00241     this.street = street;
00242     this.city = city;
00243     this.state = state;
00244     this.zipcode = zipcode;
00245     this.country = country;
00246     this.phone = phone;
00247   }
00248   public String getAddress()
00249   {
00250     if (street.ok() && city.ok() && state.ok() && zipcode.ok() && country.ok())
00251       return street
00252         + "; "
00253         + city
00254         + ", "
00255         + state
00256         + " "
00257         + zipcode
00258         + " "
00259         + country;
00260     else
00261       return "Address not available";
00262   }
00263   public void createDBMS(DB dbms) throws Fault
00264   {
00265     dbms.execute(
00266       "CREATE TABLE DemoAccount (\n"
00267         + "  email varchar(128) NOT NULL default '',\n"
00268         + "  password varchar(64) NOT NULL default '',\n"
00269         + "  role varchar(64) unsigned NOT NULL default '',\n"
00270         + "  name varchar(32) NOT NULL default '',\n"
00271         + "  street varchar(64) NOT NULL default '',\n"
00272         + "  city varchar(64) NOT NULL default '',\n"
00273         + "  state char(2) NOT NULL default '',\n"
00274         + "  zipcode varchar(9) NOT NULL default '',\n"
00275         + "  country varchar(64) NOT NULL default '',\n"
00276         + "  phone varchar(10) NOT NULL default '',\n"
00277         + "  whenCreated datetime NOT NULL default '0000-00-00 00:00:00',\n"
00278         + " INDEX login (email, password),\n"
00279         + "  PRIMARY KEY (email)\n"
00280         + ") TYPE=InnoDB PACK_KEYS=1;\n"
00281     );
00282     ArrayList roles = new ArrayList();
00283     roles.add(DemoRole.ADMINISTRATOR);
00284     DemoAccountBean adminAccount = new DemoAccountBean(
00285       roles,
00286       new EmailField("bcox@virtualschool.edu"),
00287       new PasswordField("++++"),
00288       new NameField("Admin"),
00289       StreetField.Null,
00290       CityField.Null,
00291       USStateField.Null,
00292       ZipcodeField.Null,
00293       CountryField.Null,
00294       PhoneField.Null,
00295       TimeUtil.now()
00296     );
00297     adminAccount.save(dbms);
00298   }
00299   public static DemoAccountBean Null = new DemoAccountBean(
00300     new ArrayList(),
00301     EmailField.Null,
00302     PasswordField.Null,
00303     NameField.Null,
00304     StreetField.Null,
00305     CityField.Null,
00306     USStateField.Null,
00307     ZipcodeField.Null,
00308     CountryField.Null,
00309     PhoneField.Null,
00310     new Timestamp(System.currentTimeMillis())
00311   );
00312   public static class LoginForm extends GenericForm
00313   {
00314     final OpField op;
00315     final EmailField email;
00316     final PasswordField password;
00317 
00318     public LoginForm(Controller p)
00319     {
00320       this.op = new OpField(p.getParameter("op", ""));
00321       this.email = new EmailField(p.getParameter("email", ""));
00322       this.password = new PasswordField(p.getParameter("password", ""));
00323       validate(new Validatable[] { op, email, password });
00324     }
00325     public AccountAbstraction load(DB dbms) throws Fault
00326     {
00327       try
00328       {
00329         DemoAccountBean account = DemoAccountBean.findByEmailAndPassword(dbms, email, password);
00330         return account;
00331       }
00332       catch (Fault e)
00333       {
00334         return DemoAccountBean.Null;
00335       }
00336     }
00340     public EmailField getEmail()
00341     {
00342       return email;
00343     }
00344 
00348     public OpField getOp()
00349     {
00350       return op;
00351     }
00352 
00356     public PasswordField getPassword()
00357     {
00358       return password;
00359     }
00360 
00361   }
00362   public static class UpdateForm extends GenericForm
00363   {
00364     final OpField op;
00365     final EmailField email;
00366     final PasswordField password;
00367     final NameField name;
00368     final StreetField street;
00369     final CityField city;
00370     final USStateField state;
00371     final ZipcodeField zipcode;
00372     final CountryField country;
00373     final PhoneField phone;
00374     final PasswordField passwordConfirm;
00375 
00376     public UpdateForm(Controller p)
00377     {
00378       DemoAccountBean pb = (DemoAccountBean)p.getAccount();
00379       this.op = new OpField(p.getParameter("op", ""));
00380       this.email = new EmailField(p.getParameter("email", pb.email));
00381       this.password = new PasswordField(p.getParameter("password", pb.password));
00382       this.name = new NameField(p.getParameter("name", pb.name));
00383       this.street = new StreetField(p.getParameter("street", pb.street));
00384       this.city = new CityField(p.getParameter("city", pb.city));
00385       this.state = new USStateField(p.getParameter("state", pb.state));
00386       this.zipcode = new ZipcodeField(p.getParameter("zipcode", pb.zipcode));
00387       this.country = new CountryField(p.getParameter("country", pb.country));
00388       this.phone = new PhoneField(p.getParameter("phone", pb.phone));
00389       this.passwordConfirm = new PasswordField(p.getParameter("passwordConfirm", PasswordField.Null));
00390       if (!password.stringValue.equals(passwordConfirm.stringValue))
00391         passwordConfirm.setValid(false, "The passwords don't match");       
00392       validate(new Validatable[] { 
00393         op,
00394         email,
00395         password,
00396         name,
00397         street,
00398         city,
00399         state,
00400         zipcode,
00401         country,
00402         phone, 
00403         passwordConfirm
00404       });
00405     }
00406     public AccountAbstraction createAccount(DB dbms)
00407     {
00408       try
00409       {
00410         DemoAccountBean account = DemoAccountBean.findByEmailAndPassword(dbms, email, password);
00411         updateAccount(dbms, account);
00412         return account;
00413       }
00414       catch (Fault e1)
00415       {
00416         try
00417         {
00418           DemoAccountBean account = DemoAccountBean.findByEmail(dbms, email);
00419           email.setValid(false, "That email address is already registered.");
00420           return Null;
00421         }
00422         catch (Fault e2)
00423         {
00424           List roles = new ArrayList();
00425           roles.add(DemoRole.STUDENT);
00426           DemoAccountBean account = new DemoAccountBean(
00427             roles,
00428             email,
00429             password,
00430             name,
00431             street,
00432             city,
00433             state,
00434             zipcode,
00435             country,
00436             phone,
00437             new Timestamp(System.currentTimeMillis())
00438           );
00439           try
00440           {
00441             account.save(dbms);
00442             return account;
00443           }
00444           catch (Fault e)
00445           {
00446             return DemoAccountBean.Null;
00447           }
00448         }
00449       }
00450     }
00451     public void updateAccount(DB dbms, DemoAccountBean person)
00452       throws Fault
00453     {
00454       person.update(
00455         email,
00456         password,
00457         name,
00458         street,
00459         city,
00460         state,
00461         zipcode,
00462         country,
00463         phone
00464       );
00465       person.save(dbms);
00466     }
00470     public CityField getCity()
00471     {
00472       return city;
00473     }
00474 
00478     public CountryField getCountry()
00479     {
00480       return country;
00481     }
00482 
00486     public EmailField getEmail()
00487     {
00488       return email;
00489     }
00490 
00494     public NameField getName()
00495     {
00496       return name;
00497     }
00498 
00502     public OpField getOp()
00503     {
00504       return op;
00505     }
00506 
00510     public PasswordField getPassword()
00511     {
00512       return password;
00513     }
00514     public PasswordField getPasswordConfirm()
00515     {
00516       return passwordConfirm;
00517     }
00521     public PhoneField getPhone()
00522     {
00523       return phone;
00524     }
00528     public USStateField getState()
00529     {
00530       return state;
00531     }
00532 
00536     public StreetField getStreet()
00537     {
00538       return street;
00539     }
00540 
00544     public ZipcodeField getZipcode()
00545     {
00546       return zipcode;
00547     }
00548 
00549   }
00550   public static class AdminForm extends GenericForm
00551   {
00552     public AdminForm(Controller p)
00553     {
00554     }
00555   }
00556 }