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

FieldBean.java

00001 package edu.virtualschool.jwaa.bean;
00002 import java.sql.Timestamp;
00003 
00004 import org.apache.log4j.Logger;
00005 
00006 import edu.virtualschool.jwaa.AccountAbstraction;
00007 import edu.virtualschool.jwaa.Fault;
00008 import edu.virtualschool.jwaa.dbms.DB;
00009 import edu.virtualschool.jwaa.dbms.DBQuery;
00010 import edu.virtualschool.jwaa.dbms.DBUpdate;
00011 import edu.virtualschool.jwaa.field.CommaSeparatedField;
00012 
00021 public class FieldBean
00022 {
00023   public final String accountID;
00024   public final String pageID;
00025   public final String fieldID;
00026   public String value;
00027   public Timestamp when;
00028   
00029   final static Logger logger = Logger.getLogger(FieldBean.class.getName());
00030 
00031   public FieldBean(DBQuery query) throws DB.Fault
00032   {
00033     this(
00034       query.getString("account"),
00035       query.getString("page"),
00036       query.getString("field"),
00037       query.getString("value"),
00038       query.getTimestamp("when")
00039     );
00040   }
00041   public FieldBean(
00042     String accountID,
00043     String pageID,
00044     String fieldID,
00045     String value,
00046     Timestamp time
00047   )
00048   {
00049     this.accountID = accountID;
00050     this.pageID = pageID;
00051     this.fieldID = fieldID;
00052     this.value = value;
00053     this.when = time;
00054   }
00055 //  public static HashMap loadMap(
00056 //      DB dbms, 
00057 //      Page page,
00058 //      String accountID
00059 //  ) throws DB.Fault
00060 //  {
00061 //    final HashMap map = new HashMap();
00062 //    if (accountID == null || page == null)
00063 //      return map;
00064 //    final String sql = "select * from Answers " +
00065 //      "\n where task="+DBUtil.quote(page.getPages())+
00066 //      "\n and page="+DBUtil.quote(page)+
00067 //      "\n and account="+DBUtil.quote(accountID);
00068 //    try
00069 //    {
00070 //      DBQuery query = dbms.query(sql);
00071 //      while(query.hasNext())
00072 //      {
00073 //        FormBean answer = new FormBean(query);
00074 //        map.put(answer.fieldID.toString(), answer);
00075 //      }
00076 //      query.close();
00077 //    }
00078 //    catch (DB.Fault e)
00079 //    {
00080 //      logger.error(e, e);
00081 //    }
00082 //    for (Iterator iterator = page.getQuestions().iterator(); iterator.hasNext(); )
00083 //    {
00084 //      Question form = (Question)iterator.next();
00085 //      String key = form.getIdentity();
00086 //      if (!map.containsKey(key))
00087 //        map.put(key, new FormBean(page, form, accountID));
00088 //    }
00089 //    return map;
00090 //  }
00091   public void update(DB dbms, AccountAbstraction account, CommaSeparatedField value) 
00092     throws Fault
00093   {
00094     if (value.equals(this.value))
00095       return;
00096     this.value = value.stringValue; 
00097     this.when = new Timestamp(System.currentTimeMillis());
00098     save(dbms);
00099   }
00100   public final String getQuestionID() { return fieldID.toString(); }
00101   public final String getStudentID() { return accountID.toString(); }
00102   public final String getField() { return value.toString(); }
00103   public final Timestamp getWhen() { return when; }
00104   public void save(DB dbms) throws Fault
00105   {
00106     String sql = "replace into Form set\n" +
00107       "account=?,\n"+
00108       "page=?,\n"+
00109       "field=?,\n"+
00110       "value=?,\n"+
00111       "time=?\n";
00112     DBUpdate update = dbms.update(sql);
00113     try 
00114     {
00115       update.setString(accountID);
00116       update.setString(pageID);
00117       update.setString(fieldID);
00118       update.setString(value);
00119       update.setTimestamp(when);
00120       update.executeUpdate();
00121       update.close();
00122     } 
00123     catch (Fault e) 
00124     {
00125       String s = "CREATE TABLE Form (\n"+
00126         "account varchar(255) NOT NULL,\n" +
00127         "page varchar(255) NOT NULL,\n" +
00128         "field varchar(255) NOT NULL,\n" +
00129         "value text NOT NULL,\n" +
00130         "time datetime NOT NULL,\n" +
00131         "PRIMARY KEY (account, page, field)\n"+
00132       ") TYPE=InnoDB PACK_KEYS=1\n";
00133       dbms.execute(s);
00134       update.setString(accountID);
00135       update.setString(pageID);
00136       update.setString(fieldID);
00137       update.setString(value);
00138       update.setTimestamp(when);
00139       update.executeUpdate();
00140       update.close();
00141     }
00142   }
00143   public String toString()
00144   {
00145     return value;
00146   }
00147 }