/* * Questionresult.java * * Created on 22. huhtikuuta 2003, 15:18 * Modified * 23.34.2003: saveToDatabase and getValuesFromDatabase * 14.5.2003: Changed so that personid doesn't change if is permanent and * personid's update doesn't include JSPvid * 12.6.2003: Code cleanup by Tiina */ package kottarainenbeans; import java.beans.*; import java.sql.*; import kotkabeans.*; import java.util.*; /** * * @author tmpoyhon */ public class Questionresult extends Object implements java.io.Serializable { private TemplateHandler TH; /** Holds value of property deleted. */ private boolean deleted; /** Holds value of property questionresultid. */ private int questionresultid; /** Holds value of property questionfieldid. */ private int questionfieldid; /** Holds value of property personid. */ private int personid; /** Holds value of property markerid. */ private int markerid; /** Holds value of property value. */ private String value; /** Holds value of property JSPvid. */ private String JSPvid; /** Holds value of property modified. */ private boolean modified; /** Holds value of property permanent. */ private boolean permanent; public void setTH(String templatePath) { this.TH = new TemplateHandler(templatePath+"/Questionresult.sqlt"); } /* Constructor * @param iJSPvid Is to get identifying id for result in questionnaire * @param qfid Tells for which questionfield does result belong * @param templatePath Tells the path for templateHandler */ public Questionresult(String iJSPvid, int qfid,String templatePath){ setTH(templatePath); setDeleted(false); setPersonid(0); setMarkerid(0); setQuestionfieldid(qfid); setQuestionresultid(0); setJSPvid(iJSPvid); setValue(""); setPermanent(false); setModified(true); } /** 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) { setModified(true); this.deleted = deleted; } /** Getter for property questionresultid. * @return Value of property questionresultid. * */ public int getQuestionresultid() { return this.questionresultid; } /** Setter for property questionresultid. * @param questionresultid New value of property questionresultid. * */ public void setQuestionresultid(int questionresultid) { setModified(true); this.questionresultid = questionresultid; } /** 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) { setModified(true); this.questionfieldid = questionfieldid; } /** Getter for property personid. * @return Value of property personid. * */ public int getPersonid() { return this.personid; } /** Setter for property personid. * @param personid New value of property personid. * */ public void setPersonid(int personid) { if(isPermanent()) return; setModified(true); this.personid = personid; } /** Getter for property markedid. * @return Value of property markedid. * */ public int getMarkerid() { return this.markerid; } /** Setter for property markedid. * @param markedid New value of property markedid. * */ public void setMarkerid(int markerid) { setModified(true); this.markerid = markerid; } /** Getter for property value. * @return Value of property value. * */ public String getValue() { return this.value; } /** Setter for property value. * @param value New value of property value. * */ public void setValue(String value) { setModified(true); this.value = value; } /** 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 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; } /** Getter for property permanent. * @return Value of property permanent. * */ public boolean isPermanent() { return this.permanent; } /** Setter for property permanent. * @param permanent New value of property permanent. * */ public void setPermanent(boolean permanent) { this.permanent = permanent; } /** Takes values from Properties-table and sets them to class's attributes * */ public void update(Properties commonVars){ setValue(commonVars.getProperty(""+getQuestionfieldid(), getValue())); try { setPersonid(Integer.parseInt(commonVars.getProperty("personid", Integer.toString(getPersonid())))); } catch (NumberFormatException nfe){} try { setMarkerid(Integer.parseInt(commonVars.getProperty(JSPvid+ "_markerid", Integer.toString(getMarkerid())))); } catch (NumberFormatException nfe){} try { setQuestionfieldid(Integer.parseInt(commonVars.getProperty(JSPvid+ "_questionfieldid", Integer.toString(getQuestionfieldid())))); } catch (NumberFormatException nfe){} if (commonVars.getProperty(JSPvid+"_deleted","").equals("TRUE")) setDeleted(true); else if (commonVars.getProperty(JSPvid+"_deleted","").equals("FALSE")) setDeleted(false); } /* Fuction sets values of the class's attributes to Properties-table and * returns it. * @param variableTable ... */ public Properties getValues(Properties variableTable) { variableTable.setProperty(JSPvid+"_markerid", Integer.toString(getMarkerid())); variableTable.setProperty(JSPvid+"_questionresultid", Integer.toString(getQuestionresultid())); variableTable.setProperty(JSPvid+"_questionfieldid", Integer.toString(getQuestionfieldid())); variableTable.setProperty(JSPvid+"_deleted", String.valueOf(isDeleted())); variableTable.setProperty(JSPvid+"_value",getValue()); variableTable.setProperty(JSPvid+"_personid", Integer.toString(getPersonid())); return variableTable; } /* Method gets values from database and sets them to attributes. * @param id Tells which questionresult is taken from db. */ public String getValuesFromDb(DB db) throws Exception { String error = getValuesFromDb(getQuestionresultid(), db); return error; } public String getValuesFromDb(int id, DB db) throws Exception { if (getPersonid()==0) return ""; RS2 rs; setQuestionresultid(id); Properties macroTable = new Properties(); macroTable.setProperty("questionresultId", String.valueOf(id)); String sqlSentence = TH.CompleteTemplate("get_questionresult",macroTable); String sqlInfo = "questionresult-query"; String logInfo = "Gets questionresults information to the object"; rs = DBhandler.bringFromDatabase(db, logInfo, sqlSentence, sqlInfo); if (rs.next()) { try { setPersonid((rs.getInt("personid"))); setValue(rs.getString("value")); setQuestionfieldid(rs.getInt("questionfieldid")); setMarkerid(rs.getInt("markerid")); setDeleted(rs.getBoolean("deleted")); setPermanent(true); setModified(false); //finally modified=false.. } catch (Exception e) { rs=null; return "Wasn't able to read from database."; } } 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(getQuestionfieldid(),db); } public void saveToDatabase(int qfid, DB db) { if(!modified) return; //this jumps out if the object hasn't been modified RS2 rs; setQuestionfieldid(qfid); String sqlInfo = "questionresult-insert"; String sqlSentence = ""; String logInfo = ""; Properties macroTable = new Properties(); macroTable.setProperty("deleted",String.valueOf(isDeleted())); macroTable.setProperty("questionresultId", String.valueOf(getQuestionresultid())); macroTable.setProperty("questionfieldId", String.valueOf(getQuestionfieldid())); macroTable.setProperty("markerId",String.valueOf(getMarkerid())); macroTable.setProperty("value",getValue()); macroTable.setProperty("personId",String.valueOf(getPersonid())); int revalue; /*this part inserts new question into db..*/ if (isPermanent()==false) { logInfo= "insertQuestionresult"; try { setQuestionresultid(AutoNumber.getNumber("questionresult", "questionresultid")); } catch (Exception e) { ; } macroTable.setProperty("questionresultId", String.valueOf(getQuestionresultid())); sqlSentence=TH.CompleteTemplate("insert_questionresult",macroTable); sqlInfo="inserts questionresult to Database"; revalue = getQuestionresultid(); } else { macroTable.setProperty("questionresultId", String.valueOf(getQuestionresultid())); sqlSentence = TH.CompleteTemplate("update_questionresult",macroTable); sqlInfo = "questionresult-update"; logInfo = "Updates question's information to the db"; revalue = 0; } try { DBhandler.insertIntoDatabase(db,logInfo, sqlSentence, sqlInfo); } catch (Exception e) {} setPermanent(true); setModified(false); //finally modified=false.. } private boolean buildQuestionresultId(DB db) { String SQLsentence = TH.CompleteTemplate("max_questionresult",new Properties()); String logInfo = "max questionresultid"; String sqlInfo = "query for max questionresultid"; RS2 rs=null; try { rs = DBhandler.bringFromDatabase(db, logInfo, SQLsentence, sqlInfo); } catch (Exception e) {} if (rs.next()) { try { setQuestionresultid(rs.getInt("questionresultid")); return true; } catch (Exception e) { return false; } } return false; } }