/* * Questiongroup.java * * Created on 8. huhtikuuta 2003, 13:05 * 16.4.2003: -Created constructor * -added JSPvid property * 17.4.2003: -Get from db * -Save to db * 22.4.2003: -First version of updating * 24.4.2003: .. * 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 kottarainenbeans.*; import kotkabeans.*; import java.sql.*; import java.util.*; /** * * @author tmpoyhon * */ public class Questiongroup extends Object implements java.io.Serializable { /** Holds value of property questiongroupid. */ private int questiongroupid; /** Holds value of property parentid. */ private int parentid; /** Holds value of property uri. */ private String uri; /** Holds value of property label. */ private String label; /** Holds value of property bankgroupid. */ private int bankgroupid; /** Holds value of property bankuserightid. */ private int bankuserightid; /** Holds value of property personid. */ private int personid; /** Holds value of property modified. */ private boolean modified; /** Holds value of property JSPvid. * This is the id with which the question is handled outside the database. * Tells about the location of the questiongroup in an questionnaire. */ private String JSPvid; /** Holds value of property deleted. */ protected boolean deleted; /** Holds value of property permanent */ private boolean permanent; /** Holds subelement objects */ protected Vector subElements=new Vector(); protected Vector metaElements=new Vector(); /** Holds value of property statusid. */ private int statusid; /** Holds value of property ordernum. */ private int ordernum; /** Holds value of property templatePath. */ private String templatePath; protected TemplateHandler TH; /** Holds value of property pointerDeleted. */ private boolean pointerDeleted; public Questiongroup() { } public Questiongroup(String JSPvid, String templatePath){ setJSPvid(JSPvid); setTemplatePath(templatePath); setTH(); setBankuserightid(0); setBankgroupid(0); setPersonid(0); setQuestiongroupid(0); setLabel(""); setUri(""); setDeleted(false); setPermanent(false); // InitializeMetadatas(); //temporary out of use } /** Constructor with some parameters. * */ public Questiongroup( String JSPvid, int questiongroupid, int parentid, String uri, String label,int bankgroupid,int bankuserightid,int personid){ setJSPvid(JSPvid); setBankuserightid(bankuserightid); setBankgroupid(bankgroupid); setPersonid(personid); setQuestiongroupid(questiongroupid); setLabel(label); setUri(uri); setModified(true); setPermanent(false); setDeleted(false); setParentid(parentid); // InitializeMetadatas(); //temporary out of use } /* Set's a creature for each metadatatype found in database * */ public void InitializeMetadatas(){ try { DBhandler hand= new DBhandler(); RS2 rs; Properties macroTable = new Properties(); String SQLsentence = TH.CompleteTemplate("get_parametertypes",macroTable); System.out.println(SQLsentence); rs = hand.bringFromDatabase("ParametedtypesFrDb", SQLsentence, "getQuestionparametertypes"); while(rs.next()){ addMetaElements(1); Metadata meta=(Metadata)getMetaElements().lastElement(); meta.setParentid(getQuestiongroupid()); meta.setParametertypeid(rs.getInt("parametertypeid")); meta.setParameterTypeName(rs.getString("name")); } } catch (Exception e){} } protected void subElementsInsert(DB db) { Enumeration en= subElements.elements(); while(en.hasMoreElements()){ Questionasgroup qag=(Questionasgroup)en.nextElement(); qag.saveToDatabase(db); } } public Vector getMetaElements(){ return this.metaElements; } /** Creates JSPvid for Questions subcreatures (Questionfields) * Modifies for example "question_1_2_1" to "whatSubCreature_1_2_1_" * */ protected String createSubId(String whatSubCreature){ int index=getJSPvid().indexOf("_"); if( index!=-1) { return whatSubCreature + getJSPvid().substring(index)+"_"; } return whatSubCreature+"_"; } protected void getSubElementsFromdb(DB db) { try { RS2 rs; Properties macroTable = new Properties(); macroTable.setProperty("questiongroupId", Integer.toString(getQuestiongroupid())); String SQLsentence = TH.CompleteTemplate("get_questiongroup_subitems",macroTable); rs = DBhandler.bringFromDatabase(db,"QuestiongroupsFrDb", SQLsentence,"getQuestiongroups"); int i=1; while(rs.next()) { subElements.addElement(new Questionasgroup( createSubId("questionasgroup")+i,getTemplatePath())); ((Questionasgroup)subElements.lastElement()).getValuesFromDb( rs.getInt("questiongroupid"),db); i++; } } catch (Exception e){ return; } } /** Getter for property questiongroupid. * @return Value of property questiongroupid. * */ public int getQuestiongroupid() { return this.questiongroupid; } /** Setter for property questiongroupid. * @param questiongroupid New value of property questiongroupid. * */ public void setQuestiongroupid(int questiongroupid) { this.questiongroupid = questiongroupid; setModified(true); } /** Getter for property parentid. * @return Value of property parentid. * */ public int getParentid() { return this.parentid; } /** Setter for property parentid. * @param parentid New value of property parentid. * */ public void setParentid(int parentid) { this.parentid = parentid; 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 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 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 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 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); } /** 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; } protected void saveSubElementsTodb(DB db) { Enumeration en = subElements.elements(); while(en.hasMoreElements()){ Questionasgroup qag=(Questionasgroup)en.nextElement(); qag.setParentid(getQuestiongroupid()); qag.saveToDatabase(db); } } public int saveToDatabase(DB db) { if(!modified) { return -1; }//this jumps out if the object hasn't been modified RS2 rs=null; String SQLInfo = "questiongroup-insert"; String SQLSentence = ""; String logInfo = ""; Properties macroTable = new Properties(); macroTable.setProperty("deleted", String.valueOf(isDeleted())); macroTable.setProperty("questiongroupId", Integer.toString(getQuestiongroupid())); macroTable.setProperty("parentId", Integer.toString(getParentid())); macroTable.setProperty("uri", getUri()); macroTable.setProperty("label", getLabel()); macroTable.setProperty("bankgroupId", Integer.toString(getBankgroupid())); macroTable.setProperty("bankuserightId", Integer.toString(getBankuserightid())); macroTable.setProperty("personId", Integer.toString(getPersonid())); int revalue = -1; if (isPermanent()==false && isDeleted() == false) { SQLSentence = TH.CompleteTemplate("get_max_questiongroupid", macroTable); SQLInfo = "questiongroup-query"; try { rs = DBhandler.bringFromDatabase(db,logInfo, SQLSentence, SQLInfo); } catch (Exception e){ System.out.println(e.getMessage()); } if (rs.next()) { try { setQuestiongroupid(rs.getInt("questiongroupid")); } catch (Exception e) { rs=null; } finally { SQLInfo = "questiongroup-insert"; } } macroTable.setProperty("questiongroupId", Integer.toString(getQuestiongroupid())); SQLSentence = TH.CompleteTemplate("insert_questiongroup",macroTable); logInfo = "Inserts questiongroups information to the db"; revalue = getQuestiongroupid(); } else { SQLSentence = TH.CompleteTemplate("update_questiongroup",macroTable); SQLInfo = "questiongroup-update"; logInfo = "Updates questiongroups information to the db"; revalue = 0; } try { int error = DBhandler.insertIntoDatabase(db,logInfo, SQLSentence, SQLInfo); saveSubElementsTodb(db); saveMetaElementsTodb(db); } catch (Exception e) { System.out.println(e.getMessage()); } setModified(false); //finally modified=false.. setPermanent(true); return revalue; } /** 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 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 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; } public boolean getValuesFromDb(int questiongroupid, DB db) { try { RS2 rs; Properties macroTable = new Properties(); macroTable.setProperty("questiongroupId", Integer.toString(questiongroupid)); String SQLsentence = TH.CompleteTemplate("get_questiongroup",macroTable); rs = DBhandler.bringFromDatabase(db,"QuestiongroupsFrDb", SQLsentence,"getQuestiongroups"); if(rs.next()){ setBankuserightid(rs.getInt("bankuserightid")); setBankgroupid(rs.getInt("bankgroupid")); setPersonid(rs.getInt("personid")); setQuestiongroupid(rs.getInt("questiongroupid")); setLabel(rs.getString("label")); setUri(rs.getString("uri")); setParentid(rs.getInt("parentid")); getSubElementsFromdb(db); if(!Questionnaire.getState().equals("answering")) getMetaElementsFromdb(db); setModified(false); setPermanent(true); } } catch (Exception e){} return false; } private void getMetaElementsFromdb(DB db) throws Exception { Properties macroTable = new Properties(); macroTable.setProperty("questiongroupId", String.valueOf(getQuestiongroupid())); String SQLsentence = TH.CompleteTemplate("get_metadatas",macroTable); RS2 rs = DBhandler.bringFromDatabase(db, "QuestiongroupFrDb", SQLsentence,"getMetadataNumbersForQuestiongroup"); int i=1; try { while(rs.next()){ metaElements.addElement(new Metadata(createSubId(""+ "questiongroup")+"meta_"+i,templatePath)); ((Metadata)metaElements.lastElement()).getValuesFromDb( getQuestiongroupid(),rs.getInt("parametertypeid"), db); i++; } } catch(Exception e){} } private void saveMetaElementsTodb(DB db){ Enumeration en = metaElements.elements(); while(en.hasMoreElements()){ Metadata meta=(Metadata)en.nextElement(); meta.saveToDatabase(getQuestiongroupid(),db); } } public void update(Properties commonVars){ //if in "answering"-state 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) {} try { setParentid(Integer.parseInt(commonVars.getProperty(JSPvid+ "_parentid", String.valueOf(getParentid())))); } catch (NumberFormatException e) {} if (commonVars.getProperty(JSPvid+"_deleted","").equals("TRUE")) setPointerDeleted(true); else if (commonVars.getProperty(JSPvid+"_deleted","").equals("FALSE")) setPointerDeleted(false); try { int howmany = Integer.parseInt(commonVars.getProperty(JSPvid+ "_addsubelements","0")); addSubElements(howmany); } catch (NumberFormatException e) {} metaElementsUpdate(commonVars); } updateSubElements(commonVars); } protected void updateSubElements(Properties commonVars) { Enumeration en = subElements.elements(); while(en.hasMoreElements()){ Questionasgroup group=(Questionasgroup)en.nextElement(); group.update(commonVars); } } private void metaElementsUpdate(Properties commonVars){ Enumeration en = metaElements.elements(); while(en.hasMoreElements()){ Metadata meta=(Metadata)en.nextElement(); meta.update(commonVars); } } public void getValues(Properties variableTable){ variableTable.setProperty(JSPvid+"_label",this.getLabel()); variableTable.setProperty(JSPvid+"_uri",this.getUri()); variableTable.setProperty(JSPvid+"_bankgroupid", Integer.toString(getBankgroupid())); variableTable.setProperty(JSPvid+"_bankuserightid", Integer.toString(getBankuserightid())); variableTable.setProperty(JSPvid+"_personid", Integer.toString(getPersonid())); variableTable.setProperty(JSPvid+"_deleted", String.valueOf(isDeleted())); variableTable.setProperty(JSPvid+"_statusid", Integer.toString(getStatusid())); variableTable.setProperty(JSPvid+"_questiongroupid", Integer.toString(getQuestiongroupid())); variableTable.setProperty(JSPvid+"_parentid", Integer.toString(getParentid())); variableTable.setProperty(JSPvid+"_ordernum", Integer.toString(getOrdernum())); variableTable.setProperty(JSPvid+"_subelementcount", Integer.toString(subElements.size())); getSubElementsValues(variableTable); // updates Questionfields; } protected void getSubElementsValues(Properties variableTable) { Enumeration en = subElements.elements(); while(en.hasMoreElements()){ Questionasgroup qag=(Questionasgroup)en.nextElement(); qag.getValues(variableTable); } } /** Getter for property statusid. * @return Value of property statusid. * */ public int getStatusid() { return this.statusid; } /** Setter for property statusid. * @param statusid New value of property statusid. * */ public void setStatusid(int statusid) { this.statusid = statusid; } /** 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; } /** Getter for property templatePath. * @return Value of property templatePath. * */ public String getTemplatePath() { return this.templatePath; } /** Setter for property templatePath. * @param templatePath New value of property templatePath. * */ public void setTemplatePath(String templatePath) { this.templatePath = templatePath; } protected void setTH() { this.TH = new TemplateHandler(getTemplatePath()+"/Questiongroup.sqlt"); } /** Adds Questionfields to qfields-Vector * @param howmany Tells how many Questionfields are added. */ public void addSubElements(int howmany) { int elements; for (int i=0;i