9 Replies Latest reply on May 10, 2008 10:44 AM by Bjoern Eickvonder

    need urgent Help... in richfaces.. nodeselectlistner

    rajesh ninganola Newbie

      Hi.. am newbie to Richfaces... please any one.. help me out..
      thanks in advance

      i ve two frames...in one frame i ve generated tree taking values from database on given condition. which its generating perfectly. now i want is what ever node is selected it has to be displayed in second frame.

      anyone plz help me..

        • 1. Re: need urgent Help... in richfaces.. nodeselectlistner
          rajesh ninganola Newbie

          Menu.xhtml
          !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <ui:composition xmlns="http://www.w3.org/1999/xhtml"
          xmlns:ui="http://java.sun.com/jsf/facelets"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:a4j="http://richfaces.org/a4j"
          xmlns:rich="http://richfaces.org/rich">

          <h:form id="detailForm">
          <rich:tree switchType="client" id="menuTreeBean" value="#{menuTreeBean.rootNode}" nodeSelectedListener="#{menuTreeBean.processSelection}"
          reRender="rootNode" var="item">
          </rich:tree>
          </h:form>
          <h:outputText escape="false" value="Selected :#{menuTreeBean.nodeTitle}" id="rootNode" />

          </ui:composition>



          and MenuTreeBean class where am generating dynamic tree from database

          package cms.web.view.pages;

          /**
          * @author rningano
          *
          */

          import java.sql.Connection;
          import java.sql.ResultSet;
          import java.sql.ResultSetMetaData;
          import java.sql.Statement;
          import java.util.HashMap;
          import java.util.Hashtable;
          import java.util.Iterator;
          import java.util.ResourceBundle;
          import java.util.Set;
          import java.util.Vector;

          import javax.faces.component.UIComponent;
          import javax.faces.component.UIComponentBase;
          import javax.faces.component.html.HtmlCommandLink;
          import javax.faces.context.FacesContext;
          import javax.faces.event.AbortProcessingException;
          import javax.faces.event.ActionEvent;
          import javax.servlet.http.HttpSession;
          import javax.sql.DataSource;

          import org.hibernate.Session;
          import org.richfaces.component.UITree;
          import org.richfaces.event.NodeSelectedEvent;
          import org.richfaces.event.NodeSelectedListener;
          import org.richfaces.model.TreeNode;
          import org.richfaces.model.TreeNodeImpl;

          import cms.database.hibernate.util.HibernateUtil;
          import cms.web.view.SessionBean;

          public class MenuTreeBean implements NodeSelectedListener {

          private TreeNode rootNode = null;
          private String nodeTitle;

          public void setNodeTitle(String nodeTitle){
          this.nodeTitle = nodeTitle;
          }
          public String getNodeTitle(){
          return nodeTitle;
          }
          private void buildTree() {

          rootNode = new TreeNodeImpl();

          HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
          .getExternalContext().getSession(true);
          @SuppressWarnings("unused")
          SessionBean bn = (SessionBean) session.getAttribute("sessionBean");
          String userId = bn.getUserId();

          Vector res = getMenuFuncList(userId);

          String tmp_func_id = "";
          HashMap hs = new HashMap();

          if (res.size() > 0) {
          for (int i = 0; i < res.size(); i++) {

          Hashtable h1 = (Hashtable) res.get(i);

          String func_id = h1.get("func_id").toString().trim();
          String func_desc = h1.get("func_desc").toString().trim();
          String func_type_cd = h1.get("func_type_cd").toString().trim();
          String child_func_id1 = h1.get("child_func_id1").toString()
          .trim();
          String func_desc1 = h1.get("func_desc1").toString().trim();
          String child_func_id2 = h1.get("child_func_id2").toString()
          .trim();
          String func_desc2 = h1.get("func_desc2").toString().trim();
          @SuppressWarnings("unused")
          String sort_order_seq = h1.get("sort_order_seq").toString()
          .trim();

          if (func_type_cd.equalsIgnoreCase("M")) {
          if (!(tmp_func_id.equals(func_id))) {

          TreeNodeImpl accting = null;
          TreeNodeImpl acc = new TreeNodeImpl();
          TreeNodeImpl acc1 = new TreeNodeImpl();
          if (hs.containsKey(func_id)) {

          accting = (TreeNodeImpl) hs.get(func_id);
          accting.setData(func_desc);

          if (child_func_id1 != "") {
          acc.setData(func_desc1);

          if (!(child_func_id2.equals("") || child_func_id2 == null)) {
          acc1.setData(func_desc2);
          acc.addChild(func_desc2, acc1);
          }
          }
          accting.addChild(func_desc1, acc);
          nodeLink(child_func_id2, func_desc2);
          nodeLink(child_func_id1, func_desc1);

          } else {

          accting = new TreeNodeImpl();
          accting.setData(func_desc);

          if (child_func_id1 != "") {
          acc.setData(func_desc1);
          if (child_func_id2 != "") {
          acc1.setData(func_desc2);
          acc.addChild(func_desc2, acc1);
          }
          }
          accting.addChild(func_desc1, acc);
          nodeLink(child_func_id2, func_desc2);
          nodeLink(child_func_id1, func_desc1);
          }
          hs.put(func_id, accting);
          }
          }
          }
          if (hs.size() > 0) {
          Set s1 = hs.keySet();
          Iterator itr = s1.iterator();
          while (itr.hasNext()) {
          String ne = (String) itr.next();
          rootNode.addChild(ne, (TreeNodeImpl) hs.get((ne)));
          }
          }
          }
          }

          public void setRootNode(TreeNode rootNode) {
          this.rootNode = rootNode;
          }

          public TreeNode getRootNode() {
          if (rootNode == null) {
          buildTree();
          }
          return rootNode;
          }

          @SuppressWarnings("unchecked")
          public Vector getMenuFuncList(String userId) {

          Connection conn = getConn();

          String query = "SELECT DISTINCT f.func_id, f.func_desc func_desc, f.func_type_cd, s.child_func_id child_func_id1,"
          + " f1.func_desc func_desc1,"
          + " o.child_func_id child_func_id2, f2.func_desc func_desc2, o.sort_order_seq"
          + " FROM function f,user_group ug,group_function gf, sub_function s,"
          + " function f1, OUTER (sub_function o, function f2)"
          + " WHERE ug.user_id = UPPER('"
          + userId
          + "')"
          + " AND ug.eff_from_dt <= TODAY"
          + " AND ug.eff_to_dt >= TODAY"
          + " AND f.func_stat = 'A'"
          + " AND gf.group_id = ug.group_id"
          + " AND f.func_id = gf.func_id"
          + " AND f1.func_id = s.child_func_id"
          + " AND f1.func_stat = 'A' "
          + " AND ( f.func_id = s.parent_func_id"
          + " AND s.child_func_id in (SELECT func_id FROM function"
          + " WHERE func_stat = 'A' "
          + " AND (global_access_ind = 'Y'"
          + " OR (func_id IN ( SELECT f.func_id "
          + " FROM function f,user_group ug, group_function gf"
          + " WHERE ug.user_id = UPPER('"
          + userId
          + "')"
          + " AND ug.eff_from_dt <= TODAY"
          + " AND ug.eff_to_dt >= TODAY"
          + " AND f.func_stat = 'A'"
          + " AND f.func_type_cd != 'M'"
          + " AND gf.group_id = ug.group_id"
          + " AND f.func_id = gf.func_id)))) "
          + " OR (f.func_id = s.child_func_id AND f.func_type_cd != 'M')) "
          + " AND s.child_func_id NOT IN ( SELECT f.func_id "
          + " FROM function f,user_group ug, group_function gf "
          + " WHERE ug.user_id = UPPER('"
          + userId
          + "')"
          + " AND ug.eff_from_dt <= TODAY "
          + " AND ug.eff_to_dt >= TODAY "
          + " AND f.func_stat = 'A' "
          + " AND f.func_type_cd = 'M' "
          + " AND gf.group_id = ug.group_id "
          + " AND f.func_id = gf.func_id) "
          + " AND s.child_func_id = o.parent_func_id "
          + " AND o.parent_func_id != f.func_id "
          + " AND f2.func_id = o.child_func_id"
          + " AND f2.func_stat = 'A' "
          + " AND o.child_func_id IN ( SELECT func_id from function "
          + " WHERE func_stat = 'A' "
          + " AND (global_access_ind = 'Y' "
          + " OR (func_id IN ( SELECT f.func_id "
          + " FROM function f,user_group ug, group_function gf "
          + " WHERE ug.user_id = UPPER('"
          + userId
          + "')"
          + " AND ug.eff_from_dt <= TODAY "
          + " AND ug.eff_to_dt >= TODAY "
          + " AND f.func_stat = 'A' "
          + " AND f.func_type_cd != 'M' "
          + " AND gf.group_id = ug.group_id "
          + " AND f.func_id = gf.func_id)) )) "
          + " UNION "
          + " SELECT DISTINCT f.func_id, f.func_desc func_desc, f.func_type_cd, "
          + " s2.child_func_id child_func_id1,"
          + " f1.func_desc func_desc1, "
          + " o.child_func_id child_func_id2,"
          + " f2.func_desc func_desc2, "
          + " o.sort_order_seq seq2"
          + " FROM function f, sub_function s, sub_function s2, function f1, "
          + " OUTER (sub_function o, function f2)"
          + " WHERE f.wkstn_restrict_ind = 'N' "
          + " AND f.global_access_ind = 'Y' "
          + " AND f.func_stat = 'A' "
          + " AND f.func_id = s.child_func_id"
          + " AND s.parent_func_id = 'CMS' "
          + " AND s.child_func_id = s2.parent_func_id"
          + " AND f1.func_id = s2.child_func_id"
          + " AND f1.wkstn_restrict_ind = 'N' "
          + " AND f1.global_access_ind = 'Y' "
          + " AND f1.func_stat = 'A' "
          + " AND s2.child_func_id = o.parent_func_id "
          + " AND f2.func_id = o.child_func_id"
          + " AND f2.wkstn_restrict_ind = 'N' "
          + " AND f2.global_access_ind = 'Y' "
          + " AND f2.func_stat = 'A' "
          + " AND s2.child_func_id NOT IN (SELECT f.func_id"
          + " FROM function f, sub_function s"
          + " WHERE f.wkstn_restrict_ind = 'N'"
          + " AND f.global_access_ind = 'Y' "
          + " AND f.func_stat == 'A' "
          + " AND f.func_id = s.child_func_id"
          + " AND s.parent_func_id = 'CMS') "
          + " AND o.child_func_id NOT IN ( SELECT f.func_id"
          + " FROM function f, sub_function s, sub_function s2"
          + " WHERE f.wkstn_restrict_ind = 'N' "
          + " AND f.global_access_ind = 'Y' "
          + " AND f.func_stat = 'A' "
          + " AND f.func_id = s.child_func_id"
          + " AND s.parent_func_id = 'CMS' "
          + " AND s.child_func_id = s2.parent_func_id)"
          + " ORDER BY f.func_type_cd, f.func_id, s.child_func_id,"
          + " o.sort_order_seq, o.child_func_id";

          Vector res = new Vector();
          try {
          Statement stmt = conn.createStatement();
          ResultSet rs = stmt.executeQuery(query);
          ResultSetMetaData rsmd = rs.getMetaData();
          int numCols = rsmd.getColumnCount();
          while (rs.next()) {
          Hashtable newRow = new Hashtable();
          for (int ii = 1; ii <= numCols; ii++) {
          if (rs.getObject(ii) == null) {
          newRow.put(rsmd.getColumnName(ii), "");
          } else {
          newRow.put(rsmd.getColumnName(ii), rs.getObject(ii)
          .toString());
          }
          }
          res.addElement(newRow);
          }
          stmt.close();
          } catch (Exception ex) {

          }
          return res;
          }

          public Connection getConn() {
          Connection conn = null;
          HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
          .getExternalContext().getSession(true);
          SessionBean bn = (SessionBean) session.getAttribute("sessionBean");
          if (bn == null) {
          bn = new SessionBean();
          }
          try {
          Session hibernateSess;
          if (bn.getHibernateSession() == null) {

          javax.naming.Context ctx = new javax.naming.InitialContext();
          DataSource ds = (DataSource) ctx
          .lookup("java:comp/env/jdbc/InformixDb");

          hibernateSess = HibernateUtil.getSessionFactory().openSession(
          ds.getConnection());
          bn.setHibernateSession(hibernateSess);
          conn = ds.getConnection();

          }
          } catch (Exception e) {
          System.out.println("GET CONN FAILED " + e);
          e.printStackTrace();
          }
          return conn;
          }
          private Boolean scrDeveloped(String label) {

          boolean screenIsDeveloped = false;
          String retVal = "";

          ResourceBundle bundle = ResourceBundle
          .getBundle("cms.web.view.devlopedScreens");

          if (!(bundle == null)) {
          try {
          retVal = bundle.getString(label);
          if (!(retVal == "" || retVal == null)) {
          screenIsDeveloped = true;
          } else {
          screenIsDeveloped = false;
          }
          } catch (Exception ex) {
          return screenIsDeveloped;
          }
          } else {
          screenIsDeveloped = false;
          }
          return screenIsDeveloped;
          }

          @SuppressWarnings("unchecked")
          private HtmlCommandLink nodeLink(String label, String labelDesc) {
          HtmlCommandLink link = new HtmlCommandLink();

          link.setTarget("detail");
          if (scrDeveloped(label)) {

          link.setValue(labelDesc);
          link.setRel(labelDesc);
          link.setRel(label);
          } else {
          link.setTarget("welcome.xhtml");
          }
          return link;
          }

          @Override
          public void processSelection(NodeSelectedEvent event)
          throws AbortProcessingException {
          UITree ut = (UITree) event.getComponent();
          nodeTitle = (String) ut.getRowData();
          System.out.println("in process selction menu tree bean"
          + event.getComponent().getId());

          }
          }



          please anyone help me out from this issue...
          thanks in advance

          • 3. Re: need urgent Help... in richfaces.. nodeselectlistner
            rajesh ninganola Newbie

            hello.......... any one ... there.. can anyone... look at this.. i need some urgent help...
            thanks in advance

            • 4. Re: need urgent Help... in richfaces.. nodeselectlistner
              Florian Marwede Expert

              1.) Take an a4j:commandButton as node
              2.) Take the id of the data item represented as node as parameter for the action listener of the commandButton
              3.) Point the reRender-Attribute of the commandButton to the container element of the area to want to reRender (for example an a4j:outputPanel)

              • 5. Re: need urgent Help... in richfaces.. nodeselectlistner
                rajesh ninganola Newbie

                Hi,
                I didnt get exactly what u want to say.. can u explain me a little bit more.
                thanks

                • 6. Re: need urgent Help... in richfaces.. nodeselectlistner
                  Florian Marwede Expert

                  Sorry, commandButton was wrong. I wanted to say commandLink

                  Maybe this code snippet helps:

                   <rich:treeNode>
                   <a4j:commandLink value="" reRender="idOfAreaToReRender"
                   actionListener="#{yourBean.processNodeSelection}">
                   <h:outputText value="#{item.name}" />
                   <f:param name="id" value="#{item.id}" />
                   </a4j:commandLink>
                   </rich:treeNode>
                  


                  • 8. Re: need urgent Help... in richfaces.. nodeselectlistner
                    Florian Marwede Expert

                    The idea is very simple: You define your tree node as link and on onclick you define the rerendering of a specific part of your page.

                    Please tell me what you don't understand.

                    • 9. Re: need urgent Help... in richfaces.. nodeselectlistner
                      Bjoern Eickvonder Newbie

                      Maybe just adding ajaxSubmitSelection=true could help.