[jboss-user] [JBoss Seam] - Re: working with a dynamically sized Datamodel with JSF/SEAM

asookazian do-not-reply at jboss.com
Tue Jan 29 12:10:53 EST 2008


WARNING: I haven't looked at this code for a couple months now.  So just try it out.  Sorry, it's a little sloppy but should work...  let me know how it goes!  Keep in mind that PMuir stated that reconstructing the dataTable (or any other JSF UI component) in the business tier is not a good idea.  It will work but goes against the concept of separation of concerns...

.xhtml:

<!DOCTYPE composition 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:s="http://jboss.com/products/seam/taglib"
  |                 xmlns:ui="http://java.sun.com/jsf/facelets"
  |                 xmlns:f="http://java.sun.com/jsf/core"
  |                 xmlns:h="http://java.sun.com/jsf/html"
  |                 xmlns:rich="http://richfaces.ajax4jsf.org/rich"
  |                 template="layout/template.xhtml">
  | 
  | <ui:define name="body">
  | 
  |  
  | 	<h:form>
  | 	
  | 		<!-- <h:dataTable value="#{myBean.myList}" var="myItem" binding="#{myBean.dynamicDataTable}"/>  -->
  | 		
  | 		<h:dataTable value="#{userRoleSearch.myList}" var="myItem" binding="#{userRoleSearch.dynamicDataTable}"
  | 			 		bgcolor="#F1F1F1" border="10" cellpadding="5" cellspacing="3" dir="LTR" frame="hsides"/> 
  | 
  | 	</h:form>
  | 
  | 	
  |           
  |     
  | 	
  | 	
  | </ui:define> 
  | </ui:composition>

SFSB:

package com.cox.beans.session;
  | 
  | import java.sql.CallableStatement;
  | import java.sql.Connection;
  | import java.sql.DriverManager;
  | import java.sql.ResultSet;
  | import java.sql.ResultSetMetaData;
  | import java.sql.SQLException;
  | import java.util.ArrayList;
  | import java.util.Arrays;
  | import java.util.HashMap;
  | import java.util.LinkedHashMap;
  | import java.util.List;
  | import java.util.TreeSet;
  | 
  | import javax.ejb.Remove;
  | import javax.ejb.Stateful;
  | import javax.faces.component.UIColumn;
  | import javax.faces.component.UIOutput;
  | import javax.faces.component.html.HtmlDataTable;
  | import javax.faces.context.FacesContext;
  | import javax.faces.el.ValueBinding;
  | import javax.faces.event.AbortProcessingException;
  | import javax.faces.event.ValueChangeEvent;
  | import javax.faces.model.SelectItem;
  | import javax.naming.InitialContext;
  | import javax.persistence.EntityManager;
  | import javax.persistence.PersistenceContext;
  | import javax.sql.DataSource;
  | 
  | import org.apache.log4j.Logger;
  | import org.jboss.seam.ScopeType;
  | import org.jboss.seam.annotations.Begin;
  | import org.jboss.seam.annotations.Destroy;
  | import org.jboss.seam.annotations.End;
  | import org.jboss.seam.annotations.In;
  | import org.jboss.seam.annotations.Name;
  | import org.jboss.seam.annotations.Out;
  | import org.jboss.seam.annotations.Scope;
  | import org.jboss.seam.core.FacesMessages;
  | import org.jboss.seam.security.Identity;
  | 
  | import com.cox.beans.entity.User;
  | import com.cox.constants.AdminConstants;
  | 
  | @Stateful
  | //@Scope(ScopeType.SESSION)
  | @Name("userRoleSearch")
  | public class UserRoleSearchSFSB implements UserRoleSearchSFSBLocal {
  | 	
  | 	@In
  | 	Identity identity;
  | 	
  | 	@In 
  | 	org.jboss.seam.contexts.Context sessionContext;
  | 	
  | 	@PersistenceContext
  | 	EntityManager em;	
  | 	
  | 	@In
  | 	User user;
  | 	
  | 	@In(required=false)
  | 	FacesMessages facesMessages;
  | 	
  | 	//@DataModel  -- outjection not working, using getMyRoles() for now
  | 	List<String[]> myRoles = new ArrayList<String[]>();	
  | 	List<String> columnNames = new ArrayList<String>();
  | 	int nextValueCounter = 0;
  | 	int rsCounter = 0;
  | 		
  | 	private List myList;
  |     private String[] headers; // Optional.
  |     private HtmlDataTable dynamicDataTable;
  |     
  | 	Logger log = Logger.getLogger(this.getClass());
  | 	Connection con 			= null;
  | 	ResultSet rs			= null;
  | 	CallableStatement cstmt = null;
  | 	
  | 	private String searchStringFirstName;
  | 	private String searchStringLastName;
  | 	
  | 	HashMap userListMap			= null;
  | 	HashMap<String, String> selectedUserId = new HashMap<String, String>();
  | 
  | 	ArrayList 	defaultUserRolesArrayList  	= new ArrayList();
  | 	HashMap		defaultUserRolesHashMap;
  | 	
  | 	ArrayList 	otherUserRolesArrayListIsChecked1	 	= new ArrayList();
  | 	ArrayList 	otherUserRolesArrayListIsChecked0	 	= new ArrayList();
  | 	
  | 	HashMap		otherUserRolesHashMapIsChecked1;
  | 	HashMap		otherUserRolesHashMapIsChecked0;
  | 	boolean isCheckboxesDisplay	= false;
  | 	boolean displayViewRolesDataTable = false;
  | 	
  | 	List<SelectItem> choices;
  | 	String alreadyPopulated;
  | 	
  | 	//@Out(required=false)
  | 	String[] selectedChoices;
  | 
  | 	@Begin(join=true)
  | 	public HashMap getPopulateUsersList() {
  | 
  | 			log.info("Inside getPopulateUsersList=========>");
  | 			if (userListMap == null) 
  | 			{
  | 				try {
  | 					con = DriverManager.getConnection(AdminConstants.URL, AdminConstants.USERNAME, AdminConstants.PASSWORD);
  | 					cstmt = con.prepareCall("{call usp_u_adm_select_UserList(?, ?)}");
  | 					cstmt.setString(1, searchStringFirstName);
  | 					cstmt.setString(2, searchStringLastName);
  | 					rs = cstmt.executeQuery();
  | 					int count = 0;
  | 					userListMap = new LinkedHashMap();
  | 					
  | 					while(rs.next()) {
  | 						userListMap.put(AdminConstants.SELECT_USER_KEY, AdminConstants.SELECT_USER_VALUE);
  | 						userListMap.put(rs.getString("Name"), rs.getString("UserID").trim());
  | 						count = count + 1;
  | 					}
  | 					if (count==0){
  | 						userListMap.put(AdminConstants.NO_USERS_EXISTS, AdminConstants.NO_USERS_EXISTS);			
  | 					}
  | 					userListMap = getSortedUsers(AdminConstants.ASCENDING_ORDER.trim(),userListMap);
  | 				}
  | 				catch(Exception e) {
  | 					e.printStackTrace();			
  | 				}
  | 				finally {
  | 					this.cleanUp();
  | 				}
  | 		}
  | 		
  | 		return userListMap;	
  | 	}
  | 	
  | 	public void processValueChange(ValueChangeEvent value) throws AbortProcessingException {
  | 		selectedUserId.put("selecteduserid",(String)value.getNewValue());
  | 		displayRolesListForUser();
  | 		if (selectedUserId != null)
  | 		{
  | 			if (AdminConstants.SELECT_USER_VALUE.equalsIgnoreCase(selectedUserId.get("selecteduserid")))
  | 			{
  | 					isCheckboxesDisplay = false;
  | 			}
  | 			else
  | 			{
  | 					isCheckboxesDisplay = true;
  | 			}
  | 		}
  | 		
  | 	}
  | 
  | 	private void displayRolesListForUser() {
  | 		log.info("Inside displayRolesListForUser");
  | 
  | 		defaultUserRolesHashMap 		= new HashMap();
  | 		otherUserRolesHashMapIsChecked1 = new HashMap();
  | 		otherUserRolesHashMapIsChecked0 = new HashMap();		
  | 
  | 			try {
  | 					con = DriverManager.getConnection(AdminConstants.URL, AdminConstants.USERNAME, AdminConstants.PASSWORD);
  | 					cstmt = con.prepareCall("{call usp_u_adm_select_UserRole(?)}");
  | 					cstmt.setString(1, selectedUserId.get("selecteduserid").toString());
  | 					log.info("SELECTED USERID = "+selectedUserId.get("selecteduserid").toString());
  | 					rs 				= cstmt.executeQuery();
  | 					int index=0;
  | 					
  | 					if (rs != null)
  | 					{
  | 						while (rs.next())
  | 						{
  | 							//Default User Role = "User"
  | 							if (rs.getString("IsDefault").equals("1") && rs.getString("IsChecked").equals("0"))
  | 							{
  | 								defaultUserRolesHashMap.put(rs.getString("Name"), rs.getString("ApplicationRoleID"));
  | 								defaultUserRolesArrayList.add(rs.getString("ApplicationRoleID"));
  | 							}
  | 							else if( rs.getString("IsDefault").equalsIgnoreCase("0") &&
  | 									(rs.getString("IsChecked").equalsIgnoreCase("0") || rs.getString("IsChecked").equalsIgnoreCase("1"))) 
  | 											  
  | 							{
  | 								if (rs.getString("IsChecked").equalsIgnoreCase("1")) 
  | 								{
  | 									otherUserRolesHashMapIsChecked1.put(rs.getString("Name"), rs.getString("ApplicationRoleID"));
  | 									otherUserRolesArrayListIsChecked1.add(rs.getString("ApplicationRoleID"));
  | 								}	
  | 								else if (rs.getString("IsChecked").equalsIgnoreCase("0")) 
  | 								{
  | 									otherUserRolesHashMapIsChecked0.put(rs.getString("Name"), rs.getString("ApplicationRoleID"));
  | 									otherUserRolesArrayListIsChecked0.add("false");
  | 								}
  | 							}
  | 							index = index+1;
  | 						}
  | 					}
  | 					destroy();
  | 				}
  | 				catch(Exception e) {
  | 					e.printStackTrace();			
  | 				}
  | 				finally 
  | 				{
  | 						this.cleanUp();
  | 				}
  | 	}
  | 	
  | 	
  | 	public String submitSelection(){
  | 		log.info("Inside submitSelection=========>CLICKED Submit");
  | 		String appRoleIDString = "";
  | 		String xmlParam = "";
  | 		String userId 	= "";
  | 		ArrayList masterUserRolesArrayList = new ArrayList();
  | 		
  | 		if (otherUserRolesArrayListIsChecked1 != null && otherUserRolesArrayListIsChecked0 != null)
  | 		{
  | 			for (int i = 0; i< otherUserRolesArrayListIsChecked1.size();i++)
  | 			{
  | 				masterUserRolesArrayList.add(otherUserRolesArrayListIsChecked1.get(i));
  | 			}
  | 			for (int i = 0; i< otherUserRolesArrayListIsChecked0.size();i++)
  | 			{
  | 				masterUserRolesArrayList.add(otherUserRolesArrayListIsChecked0.get(i));
  | 			}
  | 			int masterCount = masterUserRolesArrayList.size();
  | 			
  | 			for (int i = 0; i< masterCount;i++)
  | 			{
  | 				appRoleIDString += "<ApplicationRole ApplicationRoleID=\""+masterUserRolesArrayList.get(i)+"\" />";
  | 			}
  | 			xmlParam = "<?xml version=\"1.0\"  standalone=\"yes\"?><root>" + appRoleIDString + "</root>";
  | 			try {
  | 				con = DriverManager.getConnection(AdminConstants.URL, AdminConstants.USERNAME, AdminConstants.PASSWORD);
  | 				cstmt = con.prepareCall("{call usp_u_adm_update_UserRoleRecord(?,?,?,?)}");
  | 				userId = selectedUserId.get("selecteduserid").toString(); // User selected from Dropdown
  | 				cstmt.setString(1, userId);
  | 				cstmt.setString(2, xmlParam);
  | 				cstmt.setString(3, user.getNetworkLogin());
  | 				cstmt.setString(4, user.getUserId().toString());
  | 				int rowsUpdated = cstmt.executeUpdate();
  | 			}
  | 			catch(Exception e) {
  | 				e.printStackTrace();			
  | 			}
  | 			finally 
  | 			{
  | 					this.cleanUp();
  | 			}
  | 		}
  | 		return "submitted";
  | 	}
  | 	
  | 	@End(beforeRedirect=true)
  | 	public String submitDone()
  | 	{
  | 		log.info("Inside submitDone");
  | 		return "done";
  | 	}
  | 	
  | 	
  | //	===============START : CHECKBOXES====================================	
  | 	
  | 	public HashMap getDefaultUserRolesHashMap() {
  | 		return defaultUserRolesHashMap;
  | 	}
  | 
  | 	public void setDefaultUserRolesHashMap(HashMap defaultUserRolesHashMap) {
  | 		this.defaultUserRolesHashMap = defaultUserRolesHashMap;
  | 	}
  | 
  | 	public ArrayList getDefaultUserRolesArrayList() {
  | 		return defaultUserRolesArrayList;
  | 	}
  | 
  | 	public void setDefaultUserRolesArrayList(ArrayList defaultUserRolesArrayList) {
  | 		this.defaultUserRolesArrayList = defaultUserRolesArrayList;
  | 	}
  | 
  | 	public ArrayList getOtherUserRolesArrayListIsChecked0() {
  | 		return otherUserRolesArrayListIsChecked0;
  | 	}
  | 
  | 	public void setOtherUserRolesArrayListIsChecked0(
  | 			ArrayList otherUserRolesArrayListIsChecked0) {
  | 		this.otherUserRolesArrayListIsChecked0 = otherUserRolesArrayListIsChecked0;
  | 	}
  | 
  | 	public ArrayList getOtherUserRolesArrayListIsChecked1() {
  | 		return otherUserRolesArrayListIsChecked1;
  | 	}
  | 
  | 	public void setOtherUserRolesArrayListIsChecked1(
  | 			ArrayList otherUserRolesArrayListIsChecked1) {
  | 		this.otherUserRolesArrayListIsChecked1 = otherUserRolesArrayListIsChecked1;
  | 	}
  | 
  | 	public HashMap getOtherUserRolesHashMapIsChecked0() {
  | 		return otherUserRolesHashMapIsChecked0;
  | 	}
  | 
  | 	public void setOtherUserRolesHashMapIsChecked0(
  | 			HashMap otherUserRolesHashMapIsChecked0) {
  | 		this.otherUserRolesHashMapIsChecked0 = otherUserRolesHashMapIsChecked0;
  | 	}
  | 
  | 	public HashMap getOtherUserRolesHashMapIsChecked1() {
  | 		return otherUserRolesHashMapIsChecked1;
  | 	}
  | 
  | 	public void setOtherUserRolesHashMapIsChecked1(
  | 			HashMap otherUserRolesHashMapIsChecked1) {
  | 		this.otherUserRolesHashMapIsChecked1 = otherUserRolesHashMapIsChecked1;
  | 	}
  | 		
  | //===============END : CHECKBOXES====================================	
  | 
  | 		public String getSearchStringFirstName() {
  | 		      return searchStringFirstName;
  | 		}
  | 		   
  | 	    public void setSearchStringFirstName(String searchStringFirstName) {  
  | 	      this.searchStringFirstName = searchStringFirstName;
  | 	    }
  | 	    
  | 		public String getSearchStringLastName() {
  | 			return searchStringLastName;
  | 		}
  | 		
  | 		public void setSearchStringLastName(String searchStringLastName) {
  | 			this.searchStringLastName = searchStringLastName;
  | 		}
  | 		
  | 		public boolean getIsCheckboxesDisplay(){
  | 			return isCheckboxesDisplay;
  | 		}
  | 
  | //=================================================================================================
  | //=========================START - Utility Methods=================================================
  | 		
  | 		private HashMap getSortedUsers(String order, HashMap userListMap)
  | 		{
  | 			List mapKeys 	= new ArrayList(userListMap.keySet());
  | 			List mapValues 	= new ArrayList(userListMap.values());
  | 			userListMap.clear();
  | 
  | 			TreeSet sortedSet = new TreeSet(mapKeys); 	
  | 			Object[] sortedArray = sortedSet.toArray();
  | 		
  | 				if (order != null)  
  | 				{
  | 					if (AdminConstants.ASCENDING_ORDER.equalsIgnoreCase(order))
  | 					{   // Ascending Order
  | 						for (int i=0; i<sortedArray.length; i++)
  | 						{
  | 							userListMap.put(mapKeys.get(mapKeys.indexOf(sortedArray)), mapValues.get(i));
  | 						}
  | 					}
  | 					else 
  | 					{ 	// Descending Order
  | 						for (int i=sortedArray.length; i>0;)
  | 						{
  | 							userListMap.put(mapKeys.get(mapKeys.indexOf(sortedArray)), mapValues.get(i));
  | 						}
  | 					}
  | 				}
  | 			return userListMap;
  | 		}
  | //=========================END -  Utility Methods==================================================
  | //=================================================================================================
  | 
  | 
  | //========================START - INTEGRATION =========================================================		
  | 		
  | 
  | //		@Begin(join=true)
  | 		public boolean getSeedSelection() {
  | 			log.info("in seedSelection()");
  | 			populate();
  | 			return true;
  | 		}
  | 		
  | 		public List<SelectItem> getChoices() {
  | 			return choices;
  | 		}
  | 
  | 		public String[] getSelectedChoices() {
  | 			return selectedChoices;
  | 		}
  | 
  | 		public void setSelectedChoices(String[] selected) {
  | 			this.selectedChoices = selected;
  | 		}
  | 		
  | 		
  | 		public void viewRoles() {						
  | 			viewRolesData(selectedChoices);			
  | 			displayViewRolesDataTable = true;			
  | 		}
  | 		
  | 		/* this viewRolesData is the old version before dynamic column fix
  | 		 
  | 		 
  | 		private void viewRolesData(String[] selectedChoices) {
  | 			
  | 			log.info("begin viewRolesData()");
  | 									
  | 			try {
  | 				//using existing stored proc for now; build XML string to pass as param to sproc
  | 				
  | 				String appRoleIDString = "";
  | 				
  | 				for(String myChoices : selectedChoices) {
  | 					appRoleIDString += "<ApplicationRole ApplicationRoleID=\""+myChoices+"\" />";
  | 				}
  | 				
  | 				log.info("appRoleIDString = " + appRoleIDString);
  | 				
  | 				String xmlParam = "<?xml version=\"1.0\"  standalone=\"yes\"?><root>" + appRoleIDString + "</root>";
  | 				
  | 				log.info("in viewRoles: xmlParam = " + xmlParam);
  | 				
  | 				con = DriverManager.getConnection(AdminConstants.URL, AdminConstants.USERNAME, AdminConstants.PASSWORD);
  | 				
  | 				cstmt = con.prepareCall("{call usp_u_adm_select_ViewUserRole(?)}");
  | 				cstmt.setString(1, xmlParam);
  | 				rs = cstmt.executeQuery();
  | 				ResultSetMetaData rsmd = rs.getMetaData();
  | 			    
  | 			    //find the column names of the resultset
  | 			    log.debug("columnNames are:");
  | 			    int totalCols = rsmd.getColumnCount();		   
  | 			    for (int i = 1; i <= totalCols; i++) {
  | 			    	columnNames.add(i-1, rsmd.getColumnName(i));			    	
  | 			    	log.debug(columnNames.get(i - 1));
  | 			    }
  | 				    	
  | 		    	//load array with data values from resultset
  | 		    	while(rs.next()) {
  | //		    		create a String array with a size = rsmd.getColumnCount() for each row in resultset
  | 			    	String[] sArray = new String[totalCols];
  | 			    	
  | 			    	for (int i = 0; i < totalCols; i++) {
  | 			    		sArray = rs.getString(i+1);		    		
  | 			    	}	    	
  | 			    	//for each row of data in resultset, add array to myRoles
  | 					myRoles.add(sArray);
  | 		    	}
  | 		    	
  | 			}
  | 			catch(Exception e) {
  | 				e.printStackTrace();			
  | 			}
  | 			finally {
  | 				this.cleanUp();
  | 			}
  | 			
  | 		}
  | 		*/
  | 		private void populate() {
  | 			log.info("in populate()");
  | 			
  | 			try {
  | 				con = DriverManager.getConnection(AdminConstants.URL, AdminConstants.USERNAME, AdminConstants.PASSWORD);
  | 				cstmt = con.prepareCall("{call usp_u_adm_select_Role}");			
  | 				rs = cstmt.executeQuery();
  | 				choices = new ArrayList<SelectItem>();
  | 				while(rs.next()) {
  | 					choices.add(new SelectItem(rs.getString("ApplicationRoleID"), rs.getString("Name")));
  | 					
  | 					String name = rs.getString("Name");
  | 					log.info("populate(): name = " + name);
  | 					String applicationRoleID = rs.getString("ApplicationRoleID");
  | 					log.info("populate(): applicationRoleID = " + applicationRoleID);
  | 				}
  | 				alreadyPopulated = "a value indicating we are populated";
  | 			}
  | 			catch(Exception e) {
  | 				e.printStackTrace();			
  | 			}
  | 			finally {
  | 				this.cleanUp();			
  | 			}
  | 			
  | 			
  | 		}
  | 		
  | 		public boolean getDisplayViewRolesDataTable() {
  | 			return displayViewRolesDataTable;
  | 		}
  | 		
  | 		public List<String[]> getMyRoles() {
  | 			return myRoles;
  | 		}
  | 		
  | 		public String getNextValue() {
  | 			log.debug("begin getNextValue()");
  | 						
  | 			String nextVal = "";
  | 			
  | 			int totCols = columnNames.size();
  | 			
  | 			log.info("myRoles.size() = " + myRoles.size());
  | 			
  | 			log.info("nextValueCounter = " + nextValueCounter);
  | 			
  | 			if (nextValueCounter < totCols && rsCounter < myRoles.size()) {
  | 				//get next value from myRoles object
  | 				nextVal = (myRoles.get(rsCounter))[nextValueCounter]==null?"":(myRoles.get(rsCounter))[nextValueCounter];
  | 				nextValueCounter++;
  | 			}		
  | 			
  | //			when we get to last column, increment row counter
  | 			if (rsCounter < myRoles.size() && nextValueCounter >= totCols) {
  | 				rsCounter++;
  | 			}
  | 						
  | 			//reset column counter
  | 			if (nextValueCounter >= totCols) {
  | 				nextValueCounter = 0;
  | 			}
  | 								
  | 			log.debug("nextVal = " + nextVal);
  | 			return nextVal;
  | 		}
  | 		
  | 			    
  | 	    private void viewRolesData(String[] selectedChoices) {
  | 			
  | 			log.info("begin viewRolesData()");
  | 									
  | 			try {
  | 				//using existing stored proc for now; build XML string to pass as param to sproc
  | 				
  | 				String appRoleIDString = "";
  | 				
  | 				for(String myChoices : selectedChoices) {
  | 					appRoleIDString += "<ApplicationRole ApplicationRoleID=\""+myChoices+"\" />";
  | 				}
  | 				
  | 				log.info("appRoleIDString = " + appRoleIDString);
  | 				
  | 				String xmlParam = "<?xml version=\"1.0\"  standalone=\"yes\"?><root>" + appRoleIDString + "</root>";
  | 				
  | 				log.info("in viewRoles: xmlParam = " + xmlParam);
  | 				
  | 				javax.naming.Context ctx = new InitialContext();
  | 			      
  | 				if(ctx == null ) 
  | 			          throw new Exception("Boom - No Context");
  | 		
  | 			    DataSource ds = (DataSource)ctx.lookup("java:adminSeamDatasource");
  | 		
  | 			    if (ds != null) {
  | 			        con = ds.getConnection();
  | 			    }
  | 			    else
  | 			    	throw new Exception("ds is null");
  | 				
  | 			    if (con != null) {
  | 					cstmt = con.prepareCall("{call usp_u_adm_select_ViewUserRole(?)}");
  | 					cstmt.setString(1, xmlParam);
  | 					rs = cstmt.executeQuery();
  | 					ResultSetMetaData rsmd = rs.getMetaData();
  | 				    
  | 				    //find the column names of the resultset
  | 				    log.debug("columnNames are:");
  | 				    int totalCols = rsmd.getColumnCount();		
  | 				    
  | 				    headers = new String[totalCols];
  | 				    
  | 				    for (int i = 1; i <= totalCols; i++) {
  | 				    	//columnNames.add(i-1, rsmd.getColumnName(i));			    	
  | 				    	//log.debug(columnNames.get(i - 1));
  | 				    	headers[i-1] = rsmd.getColumnName(i);
  | 				    	log.debug("headers["+(i-1)+"] = " + headers[i-1]);
  | 				    }
  | 				    
  | 				    myList = new ArrayList();
  | 					    	
  | 			    	//load array with data values from resultset
  | 			    	while(rs.next()) {
  | 		//	    		create a String array with a size = rsmd.getColumnCount() for each row in resultset
  | 				    	String[] sArray = new String[totalCols];
  | 				    	
  | 				    	for (int i = 0; i < totalCols; i++) {
  | 				    		sArray = rs.getString(i+1);		    		
  | 				    	}	    	
  | 				    	//for each row of data in resultset, add array to myList
  | 				    	myList.add(Arrays.asList(sArray));
  | 						
  | 			    	}
  | 			    }
  | 		    	else
  | 		    		throw new Exception("connection is null");
  | 		    	
  | 			}
  | 			catch(Exception e) {
  | 				e.printStackTrace();			
  | 			}
  | 			finally {
  | 				this.cleanUp();
  | 			}
  | 			
  | 		}
  | 	    
  | 	    
  | 
  | 	    public void populateDynamicDataTable() {
  | 
  | 	        // Any columns?
  | 	        if (myList != null && myList.size() > 0) {
  | 	            dynamicDataTable = new HtmlDataTable();
  | 
  | 	            // Get amount of columns.
  | 	            int columns = ((List) myList.get(0)).size();
  | 
  | 	            // Set columns.
  | 	            for (int i = 0; i < columns; i++) {
  | 	                
  | 	                // Set header (optional).
  | 	                UIOutput header = new UIOutput();
  | 	                header.setValue(headers);
  | 
  | 	                // Set output.
  | 	                UIOutput output = new UIOutput();
  | 	                ValueBinding myItem = 
  | 	                    FacesContext
  | 	                        .getCurrentInstance()
  | 	                            .getApplication()
  | 	                                .createValueBinding("#{myItem[" + i + "]}");
  | 	                output.setValueBinding("value", myItem);
  | 
  | 	                // Set column.
  | 	                UIColumn column = new UIColumn();
  | 	                column.setHeader(header);
  | 	                column.getChildren().add(output);
  | 	                
  | 	                // Add column.
  | 	                dynamicDataTable.getChildren().add(column);
  | 	            }
  | 	        }
  | 	    }
  | 
  | 	    // Getters -----------------------------------------------------------------------------------
  | 
  | 	    public List getMyList() {
  | 	        return myList;
  | 	    }
  | 
  | 	    public HtmlDataTable getDynamicDataTable() {
  | 	        if (dynamicDataTable == null) {
  | 	            //loadMyList(); // Reload to get most recent data.
  | 	        	//String[] myArray = new String[] {"1", "2", "4", "5", "6", "7"};
  | 	        	//String[] myArray = new String[] {"1", "2", "7"};
  | 	        	viewRolesData(selectedChoices);
  | 	            populateDynamicDataTable();
  | 	        }
  | 
  | 	        return dynamicDataTable;
  | 	    }
  | 
  | 	    // Setters -----------------------------------------------------------------------------------
  | 
  | 	    public void setMyList(List myList) {
  | 	        this.myList = myList;
  | 	    }
  | 
  | 	    public void setDynamicDataTable(HtmlDataTable dynamicDataTable) {
  | 	        this.dynamicDataTable = dynamicDataTable;
  | 	    }
  | 	    
  | 	    // cleanup
  | 		
  | //========================END - INTEGRATION =========================================================
  | 		
  | 		@End(beforeRedirect=true)
  | 		public String cancel(){
  | 			log.info("in cancel()");
  | 			return "cancelled";
  | 		}
  | 		
  | 		@Remove @Destroy
  | 		public void destroy(){
  | 			
  | 			log.info("Inside destroy ============>");
  | 		}		
  | 		private void cleanUp() {
  | 			try {
  | 				if (rs != null) rs.close();
  | 				if (cstmt != null) cstmt.close();
  | 				if (con != null) con.close();
  | 			}
  | 			catch(SQLException e) {
  | 				e.printStackTrace();
  | 			}
  | 		}		
  | 		
  | 		
  | }

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4124493#4124493

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4124493



More information about the jboss-user mailing list