/* * Question.java * * Created on 3. huhtikuuta 2003, 13:01 */ package kottarainenbeans; import java.beans.*; import kotkabeans.*; import java.sql.*; import java.util.*; import javax.servlet.http.*; /** * * @author tmpoyhon * 8.4.2003: Removed deleted-attribute * 10.4.2003: getValuesFrom db works.. * 14.4.2003: - question now knows its questionFields * - started method for updating attributes from JSP-space.. * 16.4.2003: insert working.. * 23.4.2003: - Added statusid and ordernum * - Fixed getValuesFromDb doing different queries for question and * it's fields * 3.5.2003: Added Vector metaElements for handling Metadata-creatures * 14.5.2003: Changed so that personid doesn't change if is permanent and * personid's update doesn't include JSPvid * 20.5.2003: tylonen and tmpoyhon changed using on db connection in saving * 11.6.2003 - code cleanup */ public class Question extends Object implements java.io.Serializable { protected TemplateHandler TH; /** Holds value of property label. */ private String label; /** Holds value of property questionid. */ private int questionid; /** Holds value of property questiontypeid. */ private int questiontypeid; /** Holds value of property uri. */ private String uri; /** Holds value of property bankgroupid. */ private int bankgroupid; /** Holds value of property personid. */ private int personid; /** Holds value of property bankuserightid. */ private int bankuserightid; /** Holds value of property modified. */ private boolean modified; /** Holds value of property deleted. */ private boolean deleted; /** Holds value of property JSPvid. * This is the id with which the question is handled outside the database. * Tells somehow about the location of the question in an questionnaire. */ private String JSPvid; /** Collection-class for questionfields owned by this question. */ private Vector subElements=new Vector(); /** Collection-class for metaelements owned by this question. */ private Vector metaElements=new Vector(); /** Holds value of property permanent. */ private boolean permanent; /** Holds value of property statusid. */ private int statusid; /** Holds value of property ordernum. */ private int ordernum; /** Holds value of property pointerDeleted. */ private boolean pointerDeleted; /** Holds value of property templatePath. */ private String templatePath; /** Constructor without parameters... * */ public Question() { } public Question(String JSPvid,String templatePath){ setTemplatePath(templatePath); setTH(getTemplatePath()); setJSPvid(JSPvid); setBankuserightid(0); setBankgroupid(0); setPersonid(0); setQuestionid(0); setQuestiontypeid(0); setLabel(""); setUri(""); setDeleted(false); setModified(true); setPermanent(false); } /** Getter for Vector subElements * @return Value of property label. * */ public Vector getSubElements(){ return this.subElements; } /** Getter for Vector metaElements * @return Value of property label. * */ public Vector getMetaElements(){ return this.metaElements; } /** Getter for property label. * @return Value of property label. * */ public void setTH(String templatePath) { this.TH = new TemplateHandler(templatePath+"/Question.sqlt"); } /** 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 questionid. * @return Value of property questionid. * */ 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 questiontypeid. * @return Value of property questiontypeid. * */ public int getQuestiontypeid() { return this.questiontypeid; } /** Setter for property questiontypeid. * @param questiontypeid New value of property questiontypeid. * */ public void setQuestiontypeid(int questiontypeid) { this.questiontypeid = questiontypeid; setModified(true); } /** Getter for property uri. * @return Value of property uri. * */ public String getUri() { return this.uri; } /** Setter for property uri. * @param uri New value of property uri. * */ public void setUri(String uri) { this.uri = uri; setModified(true); } /** Getter for property bankgroupid. * @return Value of property bankgroupid. * */ public int getBankgroupid() { return this.bankgroupid; } /** Setter for property bankgroupid. * @param bankgroupid New value of property bankgroupid. * */ public void setBankgroupid(int bankgroupid) { this.bankgroupid = bankgroupid; setModified(true); } /** 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; this.personid = personid; setModified(true); } /** Takes question out of database using Question's own questionid. * @return Error message */ public String getValuesFromDb(DB db) { String error = getValuesFromDb(getQuestionid(), db); return error; } /** Takes question with id as "questionId" out of database * @param questionId Tells the identifying id for the query to the database */ public String getValuesFromDb(int questionId, DB db){ try { //DBhandler hand= new DBhandler(); RS2 rs; Properties macroTable = new Properties(); macroTable.setProperty("questionId", String.valueOf(questionId)); String SQLsentence = TH.CompleteTemplate("get_question",macroTable); rs = DBhandler.bringFromDatabase(db,"QuestionsFrDb", SQLsentence,"getQuestion"); if(rs.next()){ setBankuserightid(rs.getInt("bankuserightid")); setBankgroupid(rs.getInt("bankgroupid")); setPersonid(rs.getInt("personid")); setQuestionid(rs.getInt("questionid")); setQuestiontypeid(rs.getInt("questiontypeid")); setLabel(rs.getString("label")); setUri(rs.getString("uri")); try { getSubElementsFromdb(db); if(!Questionnaire.getState().equals("answering")) getMetaElementsFromdb(db); } catch(Exception e) { System.out.println(e.getMessage()); } setModified(false); setPermanent(true); } } catch (Exception e){ System.out.println(e.getMessage()); return "Kysymyksen lukemisessa kannasta tuli poikkeus."; } return "Kysymyksen lukeminen kannasta onnistui."; } /* Asks subcreatures to get their values from db. * @param rs Is brought from method getValuesFromdb.. has all the stuff * from db */ private void getSubElementsFromdb(DB db) throws Exception { String logInfo="Get questions and fields"; Properties macroTable = new Properties(); macroTable.setProperty("questionId", String.valueOf(getQuestionid())); String SQLsentence = TH.CompleteTemplate("get_fields",macroTable); RS2 rs; rs = DBhandler.bringFromDatabase(db,"QuestionsFrDb", SQLsentence,"getFieldnumbersForQuestion"); int i=1; try { while(rs.next()){ subElements.addElement(new Questionfield( createSubId("questionfield")+i,getQuestionid(),templatePath)); Questionfield qf = ((Questionfield)subElements.lastElement()); qf.setDefaultvalue(rs.getString("defaultvalue")); qf.setGenericname(rs.getString("genericname")); qf.setLabel(rs.getString("label")); qf.setOrdernum(rs.getInt("ordernum")); qf.setQuestionfieldid(rs.getInt("questionfieldid")); qf.setWeight(rs.getInt("weight")); if(qf.getState().equals("answering")) //in the future latter should be getMarkerid() qf.getResultValue(getPersonid(),getPersonid(),db); //use this state to get all results else if (qf.getState().equals("all_results")) qf.getSubElementsFromDb(db); qf.setPermanent(true); qf.setModified(false); //finally modified=false.. i++; } } catch(Exception e){ System.out.println(e.getMessage()); } } private void getMetaElementsFromdb(DB db) throws Exception { Properties macroTable = new Properties(); macroTable.setProperty("questionId", String.valueOf(getQuestionid())); String SQLsentence = TH.CompleteTemplate("get_metadatas",macroTable); RS2 rs = DBhandler.bringFromDatabase(db,"QuestionsFrDb", SQLsentence,"getMetadataNumbersForQuestion"); int i=1; try { while(rs.next()){ metaElements.addElement(new Metadata(JSPvid+"_meta_"+i, templatePath)); ((Metadata)metaElements.lastElement()).getValuesFromDb( getQuestionid(),rs.getInt("parametertypeid"), db); i++; } } catch(Exception e){ System.out.println(e.getMessage()); } } /** Getter for property bankuserightid. * @return Value of property bankuserightid. * */ public int getBankuserightid() { return this.bankuserightid; } /** Setter for property bankuserightid. * @param bankuserightid New value of property bankuserightid. * */ public void setBankuserightid(int bankuserightid) { this.bankuserightid = bankuserightid; 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; } /** This saves the question to db. * If modified is true then saves to db. Else does nothing.. */ public int saveToDatabase(DB db) { //if nothing has changed, question doesn't need to be saved to db if(!isModified()) { saveMetaElementsTodb(db); // also questionfields must be ordered to save themselves saveSubElementsTodb(db); return -1; } RS2 rs=null; String sqlInfo = ""; String logInfo = ""; String SQLsentence = ""; Properties macroTable = new Properties(); macroTable.setProperty("deleted",String.valueOf(isDeleted())); macroTable.setProperty("label",getLabel()); macroTable.setProperty("uri",getUri()); macroTable.setProperty("questiontypeId", String.valueOf(getQuestiontypeid())); macroTable.setProperty("bankgroupId",String.valueOf(getBankgroupid())); macroTable.setProperty("bankuserightId", String.valueOf(getBankuserightid())); macroTable.setProperty("personId",String.valueOf(getPersonid())); int revalue; /*this part inserts new question into db..*/ if (isPermanent()==false) { logInfo= "insertQuestion"; if (!buildQuestionId(db)) //Sets Unique id to this class return -1; macroTable.setProperty("questionId",String.valueOf(getQuestionid())); SQLsentence=TH.CompleteTemplate("insert_question",macroTable); sqlInfo="inserts question to Database"; revalue = getQuestionid(); } else { macroTable.setProperty("questionId",String.valueOf(getQuestionid())); SQLsentence = TH.CompleteTemplate("update_question",macroTable); sqlInfo = "question-update"; logInfo = "Updates question's information to the db"; revalue = 0; } try { int error = DBhandler.insertIntoDatabase(db, logInfo, SQLsentence,sqlInfo); // also questionfields must be ordered to save themselves saveSubElementsTodb(db); saveMetaElementsTodb(db); } catch (Exception e) { System.out.println(e.getMessage()); } setModified(false); //when inserted into database modified=false.. setPermanent(true); return revalue; } /** 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) { if(isPermanent()) { setPointerDeleted(deleted); } else { setPointerDeleted(deleted); this.deleted = deleted; } setModified(true); } /** 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; } /** Takes values from Properties-table and sets them to class's attributes * */ public void update(Properties commonVars){ if (!commonVars.getProperty("edittool","").equals("")) emptySubElements(); //if in "answering" update musn't be done if(!Questionnaire.getState().equals("answering")){ setLabel(commonVars.getProperty(JSPvid+"_label",getLabel())); setUri(commonVars.getProperty(JSPvid+"_uri", getUri())); try { setBankgroupid(Integer.parseInt(commonVars.getProperty(JSPvid+ "_bankgroupid",String.valueOf(getBankgroupid())))); } catch (NumberFormatException e) {} try { setBankuserightid(Integer.parseInt(commonVars.getProperty(JSPvid+ "_bankuserightid",String.valueOf(getBankuserightid())))); } catch (NumberFormatException e) {} try { setPersonid(Integer.parseInt(commonVars.getProperty("personid", String.valueOf(getPersonid())))); } catch (NumberFormatException e) {} try { setStatusid(Integer.parseInt(commonVars.getProperty(JSPvid+ "_statusid", String.valueOf(getStatusid())))); } catch (NumberFormatException e) {} try { setOrdernum(Integer.parseInt(commonVars.getProperty(JSPvid+ "_ordernum", String.valueOf(getOrdernum())))); } catch (NumberFormatException e) {} if (commonVars.getProperty(JSPvid+"_deleted","").equals("TRUE")) setDeleted(true); else if (commonVars.getProperty(JSPvid+"_deleted","").equals("FALSE")) setDeleted(false); try { int howmany = Integer.parseInt(commonVars.getProperty(JSPvid+ "_addsubelements","0")); addSubElements(howmany); } catch (NumberFormatException e) {} metaElementsUpdate(commonVars); // updates Question's Metadatas } if (!commonVars.getProperty(getJSPvid()+"_subdefaultvalue","").equals("")) commonVars.setProperty(commonVars.getProperty(getJSPvid()+ "_subdefaultvalue"),"on"); if (!commonVars.getProperty(getJSPvid()+"_subvalue","").equals("")) commonVars.setProperty(commonVars.getProperty(getJSPvid()+ "_subvalue"),"on"); if (!commonVars.getProperty(getJSPvid(),"").equals("")) commonVars.setProperty(commonVars.getProperty(getJSPvid()),"on"); subElementsUpdate(commonVars); // updates Questionfields; } private void emptySubElements() { if (getQuestiontypeid() == 3 || getQuestiontypeid() == 4 || getQuestiontypeid() == 5) { Enumeration en = subElements.elements(); while(en.hasMoreElements()){ Questionfield field=(Questionfield)en.nextElement(); field.setDefaultvalue(""); } } } /** Asks all the questionfields in the qfield-vector to update themselves * with commonVar-table. * @param commonVar has all.. * */ protected void subElementsUpdate(Properties commonVars){ Enumeration en = subElements.elements(); while(en.hasMoreElements()){ try { Questionfield field=(Questionfield)en.nextElement(); field.update(commonVars); } catch (ClassCastException e) { System.out.println(e.getMessage()); } } } /** Asks all the metadatas in the metaElements-vector to update themselves * with commonVar-table. * @param commonVar has all.. * */ private void metaElementsUpdate(Properties commonVars){ Enumeration en = metaElements.elements(); while(en.hasMoreElements()){ Metadata meta=(Metadata)en.nextElement(); meta.update(commonVars); } } /** Returns attribute-values in variableTable * @param variableTable ... */ public Properties getValues(Properties variableTable) { variableTable.setProperty(JSPvid+"_label",this.getLabel()); variableTable.setProperty(JSPvid+"_uri",this.getUri()); variableTable.setProperty(JSPvid+"_questiontypeid", Integer.toString(getQuestiontypeid())); variableTable.setProperty(JSPvid+"_statusid", Integer.toString(getStatusid())); variableTable.setProperty(JSPvid+"_deleted", String.valueOf(isDeleted())); variableTable.setProperty(JSPvid+"_bankgroupid", Integer.toString(getBankgroupid())); variableTable.setProperty(JSPvid+"_bankuserightid", Integer.toString(getBankuserightid())); variableTable.setProperty(JSPvid+"_personid", Integer.toString(getPersonid())); variableTable.setProperty(JSPvid+"_questionid", Integer.toString(getQuestionid())); variableTable.setProperty(JSPvid+"_ordernum", Integer.toString(getOrdernum())); variableTable.setProperty(JSPvid+"_subelementcount", Integer.toString(subElements.size())); getSubElementsValues(variableTable); // for Questionfields; getMetaElementsValues(variableTable); // for Question's Metadatas return variableTable; } private void getSubElementsValues(Properties variableTable){ Enumeration en = subElements.elements(); while(en.hasMoreElements()){ Questionfield field=(Questionfield)en.nextElement(); field.getValues(variableTable); } } private void getMetaElementsValues(Properties variableTable){ Enumeration en = metaElements.elements(); while(en.hasMoreElements()){ Metadata meta=(Metadata)en.nextElement(); meta.getValues(variableTable); } } /** 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; } /** Creates JSPvid for Questions subcreatures (Questionfields) * Modifies for example "question_1_2_1" to "whatSubCreature_1_2_1_" * */ private String createSubId(String whatSubCreature){ int index=getJSPvid().indexOf("_"); if( index!=-1) { return whatSubCreature + getJSPvid().substring(index)+"_"; } return whatSubCreature+"_"; } /**Asks all the questionfields in the qfield-vector to save themself into db. * * */ protected void saveSubElementsTodb(DB db){ Enumeration en = subElements.elements(); while(en.hasMoreElements()){ try{ Questionfield field=(Questionfield)en.nextElement(); field.saveToDatabase(getQuestionid(),db); } catch (ClassCastException e){ System.out.println(e.getMessage()); } } } /**Asks all the metadatas in the metaElements-vector to save themself into db. * */ private void saveMetaElementsTodb(DB db){ Enumeration en = metaElements.elements(); while(en.hasMoreElements()){ Metadata meta=(Metadata)en.nextElement(); meta.saveToDatabase(getQuestionid(),db); } } /** Adds Questionfields to subElements-Vector * @param howmany Tells how many Questionfields are added. */ public void addSubElements(int howmany) { int elements; for (int i=0;i=startDeletingIndex) field.setDeleted(true); i++; } } }