/**************************************************************************** * Questionfield.java * * Created on 8. huhtikuuta 2003, 12:56 * * Modified on the 10th of April 2003 - Timo Ylönen * - method getValuesFromDb(int id) should work now. * - method saveToDatabase works now, but there's still some work to be * done * Modified on the 14th of April 2003 - Timo * - broke method savetodatabase * Modified on the 16th and 17th of April - Timo * - fixed method savetodb * - added comments * Modified on the 22th of April - Timo * - added update-method * Modified on the 28th of April - Timo * - changed class to use TemplateHandler * Modified on the 29th of April - Timo * - Works together with class Questionresult * Modified on the 11th of June - Tiina * - Code cleanup * TODO: * ****************************************************************************/ package kottarainenbeans; import java.beans.*; import kotkabeans.*; import kottarainenbeans.*; import java.sql.*; import java.util.*; //import javax.servlet.http.*; /** * * @author tmpoyhon */ public class Questionfield extends Object implements java.io.Serializable { private TemplateHandler TH; private String templatePath; /** Holds value of property questionid. */ private int questionid; /** Holds value of property questionfieldid. */ private int questionfieldid; /** Holds value of property defaultvalue. */ private String defaultvalue; /** Holds value of property weight. */ private int weight; /** Holds value of property ordernum. */ private int ordernum; /** Holds value of property label. */ private String label; /** Holds value of property genericname. */ private String genericname; /** Holds value of property modified. */ private boolean modified; /** Holds value of property deleted. */ private boolean deleted; /** Holds value of property JSPvid. */ private String JSPvid; /** Holds value of property permanent. */ private boolean permanent; /** Getter for property questionid. * @return Value of property questionid. * */ public Questionfield(String iJSPvid, String templatePath){ setTemplatePath(templatePath); setTH(templatePath); setJSPvid(iJSPvid); setDefaultvalue(""); setDeleted(false); setGenericname(""); setLabel(""); setOrdernum(1); setPermanent(false); setQuestionid(0); setQuestionfieldid(0); setWeight(1); setState("edit"); setModified(true); } /** Constructor * */ public Questionfield(String iJSPvid, int qid, String templatePath){ setTemplatePath(templatePath); setTH(templatePath); setJSPvid(iJSPvid); setDefaultvalue(""); setDeleted(false); setGenericname(""); setLabel(""); setOrdernum(1); setPermanent(false); setQuestionid(qid); setQuestionfieldid(0); setWeight(1); setState("edit"); setModified(true); } public int getQuestionid() { return this.questionid; } /** Setter for property questionid. * @param questionid New value of property questionid. * */ public void setQuestionid(int questionid) { this.questionid = questionid; setModified(true); } /** Getter for property questionfieldid. * @return Value of property questionfieldid. * */ public int getQuestionfieldid() { return this.questionfieldid; } /** Setter for property questionfieldid. * @param questionfieldid New value of property questionfieldid. * */ public void setQuestionfieldid(int questionfieldid) { this.questionfieldid = questionfieldid; setModified(true); } /** Getter for property defaultvalue. * @return Value of property defaultvalue. * */ public String getDefaultvalue() { return this.defaultvalue; } /** Setter for property defaultvalue. * @param defaultvalue New value of property defaultvalue. * */ public void setDefaultvalue(String defaultvalue) { this.defaultvalue = defaultvalue; setModified(true); } /** Getter for property weight. * @return Value of property weight. * */ public int getWeight() { return this.weight; } /** Setter for property weight. * @param weight New value of property weight. * */ public void setWeight(int weight) { this.weight = weight; setModified(true); } /** Getter for property ordernum. * @return Value of property ordernum. * */ public int getOrdernum() { return this.ordernum; } /** Setter for property ordernum. * @param ordernum New value of property ordernum. * */ public void setOrdernum(int ordernum) { this.ordernum = ordernum; setModified(true); } /** Getter for property label. * @return Value of property label. * */ public String getLabel() { return this.label; } /** Setter for property label. * @param label New value of property label. * */ public void setLabel(String label) { this.label = label; setModified(true); } /** Getter for property genericname. * @return Value of property genericname. * */ public String getGenericname() { return this.genericname; } /** Setter for property genericname. * @param genericname New value of property genericname. * */ public void setGenericname(String genericname) { this.genericname = genericname; setModified(true); } /** Getter for property modified. * @return Value of property modified. * */ public boolean isModified() { return this.modified; } /** Setter for property modified. * @param modified New value of property modified. * */ public void setModified(boolean modified) { this.modified = modified; } /** With this method the object reads its values from db and sets them * to its properties. * @return String with error message, or empty string if all was ok */ public String getValuesFromDb(DB db) throws Exception { String error = getValuesFromDb(getQuestionfieldid(),db); return error; } /**************** getValuesFromDb ************************** * This method reads values from db and sets them to objects * properties. If no parameter is given, the object uses it's * current questionfieldid for the SQL-query **********************************************************/ public String getValuesFromDb(int id, DB db) throws Exception { RS2 rs; int questionfieldId = id; String sqlInfo = "questionfield-query"; String sqlSentence = "SELECT * FROM questionfield WHERE questionfieldid=" + questionfieldId + ";"; String logInfo = "Gets questionfields information to the object"; rs = DBhandler.bringFromDatabase(db,logInfo, sqlSentence, sqlInfo); if (rs.next()) { try { setLabel(rs.getString("label")); setDefaultvalue(rs.getString("defaultvalue")); setQuestionfieldid(rs.getInt("questionfieldid")); setOrdernum(rs.getInt("ordernum")); setGenericname(rs.getString("genericname")); setQuestionid(rs.getInt("questionid")); setWeight(rs.getInt("weight")); setDeleted(rs.getBoolean("deleted")); } catch (Exception e) { rs=null; return "En onnistunut lukemaan itseäni tietokannasta"; } finally { } } if(getState().equals("answering")) //in the future latter should be getMarkerid() getResultValue(getPersonid(),getPersonid(),db); //use this state to get all results else if (Questionnaire.getState().equals("all_results")) getSubElementsFromDb(db); setPermanent(true); setModified(false); //finally modified=false.. return ""; } /********************* saveToDataBase **************************** * Saves object to database. If the object is not permanent the largest * id is retrieved from db. After that the object is added to db with * the highest id + 1. * * If the object is permanent and it has been modified since it was * originally read from db, it's information will be updated to the db. *****************************************************************/ public void saveToDatabase(DB db) { saveToDatabase(getQuestionid(),db); } public void saveToDatabase(int qid, DB db) { if(!modified) { // questionresults must be saved saveSubElementsTodb(db); return; //this jumps out if the object hasn't been modified } RS2 rs=null; String sqlInfo = "questionfield-insert"; String sqlSentence = ""; String logInfo = ""; Properties macroTable = new Properties(); macroTable.setProperty("deleted",String.valueOf(isDeleted())); macroTable.setProperty("defaultvalue", getDefaultvalue()); macroTable.setProperty("genericname", getGenericname()); macroTable.setProperty("JSPvid", getJSPvid()); macroTable.setProperty("label",getLabel()); macroTable.setProperty("modified",String.valueOf(isModified())); macroTable.setProperty("questionfieldid", String.valueOf(getQuestionfieldid())); macroTable.setProperty("ordernum",String.valueOf(getOrdernum())); macroTable.setProperty("questionid",String.valueOf(getQuestionid())); macroTable.setProperty("weight",String.valueOf(getWeight())); if (isPermanent()==false) { sqlSentence=TH.CompleteTemplate("max_questionfield",macroTable); sqlInfo = "questionfield-query"; try { rs = DBhandler.bringFromDatabase(db,logInfo,sqlSentence, sqlInfo); } catch (Exception e) {} if (rs.next()) { try { setQuestionfieldid(rs.getInt("questionfieldid")); macroTable.setProperty("questionfieldid", String.valueOf(getQuestionfieldid())); } catch (Exception e) { rs=null; } finally { sqlInfo = "questionfield-insert"; } } setQuestionid(qid); macroTable.setProperty("questionid",String.valueOf(getQuestionid())); sqlSentence=TH.CompleteTemplate("insert_questionfield",macroTable); logInfo = "Inserts questionfields information to the db"; } else { sqlSentence = TH.CompleteTemplate("update_questionfield",macroTable); sqlInfo = "questionfield-update"; logInfo = "Updates questionfields information to the db"; } try { int error = DBhandler.insertIntoDatabase(db,logInfo, sqlSentence, sqlInfo); saveSubElementsTodb(db); //orders questionresults to save themselves } catch (Exception e){ System.out.println(e.getMessage()); } setModified(false); //finally modified=false.. setPermanent(true); } /** Takes values from HttpServletRequest-object */ public void update(Properties commonVars){ //if in "answering" update musn't be done if(!getState().equals("answering")){ setLabel(commonVars.getProperty(JSPvid+"_label", getLabel())); setDefaultvalue(commonVars.getProperty(JSPvid+"_defaultvalue", getDefaultvalue())); setGenericname(commonVars.getProperty(JSPvid+"_genericname", getGenericname())); try { setWeight(Integer.parseInt(commonVars.getProperty(JSPvid+ "_weight", Integer.toString(getWeight())))); setOrdernum(Integer.parseInt(commonVars.getProperty(JSPvid+ "_ordernum", Integer.toString(getOrdernum())))); } catch (NumberFormatException nfe) {} } try { setPersonid(Integer.parseInt(commonVars.getProperty("personid", String.valueOf(getPersonid())))); } catch (NumberFormatException e) {} String resultvalue = commonVars.getProperty(String.valueOf( getQuestionfieldid()),getDefaultvalue()); if(getState().equals("answering")){ } subElementsUpdate(commonVars); // updates Questionresults; } public Properties getValues(Properties variableTable){ variableTable.setProperty(JSPvid+"_label",this.getLabel()); variableTable.setProperty(JSPvid+"_deleted", String.valueOf(isDeleted())); variableTable.setProperty(JSPvid+"_questionfieldid", Integer.toString(getQuestionfieldid())); variableTable.setProperty(JSPvid+"_questionid", Integer.toString(getQuestionid())); variableTable.setProperty(JSPvid+"_defaultvalue",getDefaultvalue()); variableTable.setProperty(JSPvid+"_ordernum", Integer.toString(getOrdernum())); variableTable.setProperty(JSPvid+"_weight", Integer.toString(getWeight())); variableTable.setProperty(JSPvid+"_genericname",this.getGenericname()); variableTable.setProperty(JSPvid+"_subelementcount", Integer.toString(subElements.size())); getSubElementsValues(variableTable); // updates Questionresults; return variableTable; } /** Getter for property deleted. * @return Value of property deleted. * */ public boolean isDeleted() { return this.deleted; } /** Setter for property deleted. * @param deleted New value of property deleted. * */ public void setDeleted(boolean deleted) { this.deleted = deleted; } /** Getter for property JSPvid. * @return Value of property JSPvid. * */ public String getJSPvid() { return this.JSPvid; } /** Setter for property JSPvid. * @param JSPvid New value of property JSPvid. * */ public void setJSPvid(String JSPvid) { this.JSPvid = JSPvid; } /** Getter for property isPermanent. * @return Value of property isPermanent. * */ public boolean isPermanent() { return this.permanent; } /** Setter for property isPermanent. * @param isPermanent New value of property isPermanent. * */ public void setPermanent(boolean permanent) { this.permanent = permanent; } /** Collection-class for questionresults owned by this field. */ private Vector subElements=new Vector(); /** Holds value of property state. */ private String state; /** Holds value of property personid. */ private int personid; /** Holds value of property defaultIfHasResult. */ private String defaultIfHasResult; /** Getter for results */ public void getSubElementsFromDb(DB db) { try { RS2 rs; Properties macroTable = new Properties(); macroTable.setProperty("questionfieldid", Integer.toString(getQuestionfieldid())); String SQLsentence = TH.CompleteTemplate("get_results",macroTable); rs = DBhandler.bringFromDatabase(db,"QuestionresultsFromDb", SQLsentence,"getQuestionresults"); int i=1; while(rs.next()) { subElements.addElement(new Questionresult(createSubId(""+ "questionresult")+i,getQuestionfieldid(),getTemplatePath())); ((Questionresult)subElements.lastElement()).getValuesFromDb( rs.getInt("questionresultid"),db); i++; } } catch (Exception e){ return;} } /** Asks all the results in the subElements-vector to save themself into db. * */ private void saveSubElementsTodb(DB db){ Enumeration en = subElements.elements(); while(en.hasMoreElements()){ Questionresult result=(Questionresult)en.nextElement(); result.saveToDatabase(getQuestionfieldid(),db); } } /** Tells all the results in the subElements-vector to update themselves * * @param commonVar has all * */ private void subElementsUpdate(Properties commonVars){ try{ Enumeration en = subElements.elements(); while(en.hasMoreElements()){ Questionresult result=(Questionresult)en.nextElement(); result.update(commonVars); } }catch (Exception e){ System.out.println("Fieldien subelementtien updatesta poksahti virhe"); } } /** Adds Questionresults to subElements-Vector * @param howmany Tells how many Questionresults are added. * * /** Getter for subElements() */ public void addSubElements(int howmany) { int elements; for (int i=0;i