package demo29; import org.openswing.swing.table.client.GridController; import java.util.*; import org.openswing.swing.message.receive.java.*; import java.sql.*; import org.openswing.swing.message.send.java.FilterWhereClause; import org.openswing.swing.table.java.GridDataLocator; import org.openswing.swing.mdi.client.MDIFrame; import java.awt.Color; import org.openswing.swing.server.QueryUtil; import org.openswing.swing.message.send.java.GridParams; /** * <p>Title: OpenSwing Framework</p> * <p>Description: Grid controller for employees.</p> * <p>Copyright: Copyright (C) 2006 Mauro Carniel</p> * <p> </p> * @author Mauro Carniel * @version 1.0 */ public class EmpGridController extends GridController implements GridDataLocator { private EmpDetailFrame frame = null; private Connection conn = null; public EmpGridController(Connection conn,EmpDetailFrame frame) { this.conn = conn; this.frame = frame; } /** * Callback method invoked to load data on the grid. * @param action fetching versus: PREVIOUS_BLOCK_ACTION, NEXT_BLOCK_ACTION or LAST_BLOCK_ACTION * @param startPos start position of data fetching in result set * @param filteredColumns filtered columns * @param currentSortedColumns sorted columns * @param currentSortedVersusColumns ordering versus of sorted columns * @param valueObjectType v.o. type * @param otherGridParams other grid parameters * @return response from the server: an object of type VOListResponse if data loading was successfully completed, or an ErrorResponse onject if some error occours */ public Response loadData( int action, int startIndex, Map filteredColumns, ArrayList currentSortedColumns, ArrayList currentSortedVersusColumns, Class valueObjectType, Map otherGridParams) { try { String sql = "select EMP.EMP_CODE,EMP.FIRST_NAME,EMP.LAST_NAME,EMP.DEPT_CODE,DEPT.DESCRIPTION from EMP,DEPT where EMP.DEPT_CODE=DEPT.DEPT_CODE"; // mapping between attributes and database fields... Map attribute2dbField = new HashMap(); attribute2dbField.put("empCode","EMP.EMP_CODE"); attribute2dbField.put("firstName","EMP.FIRST_NAME"); attribute2dbField.put("lastName","EMP.LAST_NAME"); attribute2dbField.put("deptCode","EMP.DEPT_CODE"); attribute2dbField.put("deptDescription","DEPT.DESCRIPTION"); return QueryUtil.getQuery( conn, sql, new ArrayList(), // list of values linked to "?" parameters in sql attribute2dbField, GridEmpVO.class, // v.o. to dinamically create for each row... "Y", "N", new GridParams( action, startIndex, filteredColumns, currentSortedColumns, currentSortedVersusColumns, new HashMap() // other params... ), 50, // pagination size... true // log query... ); } catch (Exception ex) { ex.printStackTrace(); return new ErrorResponse(ex.getMessage()); } /* // an alternative way: you can define your own business logic to retrieve data and adding filtering/sorting conditions at hand... PreparedStatement stmt = null; try { String sql = "select EMP.EMP_CODE,EMP.FIRST_NAME, EMP.LAST_NAME,EMP.DEPT_CODE,DEPT.DESCRIPTION from EMP,DEPT where EMP.DEPT_CODE=DEPT.DEPT_CODE"; Vector vals = new Vector(); if (filteredColumns.size()>0) { FilterWhereClause[] filter = (FilterWhereClause[])filteredColumns.get("deptCode"); sql += " and EMP.DEPT_CODE "+ filter[0].getOperator()+"?"; vals.add(filter[0].getValue()); if (filter[1]!=null) { sql += " and EMP.DEPT_CODE "+ filter[1].getOperator()+"?"; vals.add(filter[1].getValue()); } } if (currentSortedColumns.size()>0) { sql += " ORDER BY EMP.FIRST_NAME "+currentSortedVersusColumns.get(0); } stmt = conn.prepareStatement(sql); for(int i=0;i<vals.size();i++) stmt.setObject(i+1,vals.get(i)); ResultSet rset = stmt.executeQuery(); ArrayList list = new ArrayList(); GridEmpVO vo = null; while (rset.next()) { vo = new GridEmpVO(); vo.setEmpCode(rset.getString(1)); vo.setFirstName(rset.getString(2)); vo.setLastName(rset.getString(3)); vo.setDeptCode(rset.getString(4)); vo.setDeptDescription(rset.getString(5)); list.add(vo); } return new VOListResponse(list,false,list.size()); } catch (SQLException ex) { ex.printStackTrace(); return new ErrorResponse(ex.getMessage()); } finally { try { stmt.close(); } catch (SQLException ex1) { } } */ } /** * Method invoked when the user has clicked on delete button and the grid is in READONLY mode. * @param persistentObjects value objects to delete (related to the currently selected rows) * @return an ErrorResponse value object in case of errors, VOResponse if the operation is successfully completed */ public Response deleteRecords(ArrayList persistentObjects) throws Exception { PreparedStatement stmt = null; try { stmt = conn.prepareStatement("delete from EMP where EMP_CODE=?"); GridEmpVO vo = null; for(int i=0;i<persistentObjects.size();i++) { vo = (GridEmpVO)persistentObjects.get(i); stmt.setString(1,vo.getEmpCode()); stmt.execute(); } return new VOResponse(new Boolean(true)); } catch (SQLException ex) { ex.printStackTrace(); return new ErrorResponse(ex.getMessage()); } finally { try { stmt.close(); conn.commit(); } catch (SQLException ex1) { } } } /** * Method used to define the background color for each cell of the grid. * @param rowNumber selected row index * @param attributedName attribute name related to the column currently selected * @param value object contained in the selected cell * @return background color of the selected cell */ public Color getBackgroundColor(int row,String attributedName,Object value) { if (attributedName.equals("deptCode")) { if (value.equals("SF")) return new Color(255,100,100); else if (value.equals("S")) return new Color(210,100,100); else if (value.equals("P")) return new Color(170,100,100); } return super.getBackgroundColor(row,attributedName,value); } /** * Callback method invoked when the data loading is completed. * @param error <code>true</code> if data loading has terminated with errors, <code>false</code> otherwise */ public void loadDataCompleted(boolean error) { frame.getMainPanel().reload(); } }