Java tutorial
/** * Copyright 2005 The Ontario Lottery Gamming Corporation, Inc. * Yonge 4120, Toronto, Ontario, M2P 2B8, Canada. * All rights reserved. * OLGC PROPRIETARY/CONFIDENTIAL. * Use is subject to license terms. * Created on Nov 24, 2005 */ package ca.on.gov.jus.icon.common.util; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import org.apache.commons.dbutils.ResultSetHandler; /** * Implements result set processing logic for the Lookup Manager. * Expects a result set with three columns ordered by the first column. * The data is organized in a nested map (maps with a map). The * first column (e.g. region_id) is used as the key of the primary map * and the corresponding object is another map. The secondary maps * uses the second column as the key (e.g. site_id) and the * third colum as the object (e.g. site_name). Therefore the map * containing all sites for a given region can be obtained using * a region id. * * <p> * In the long description, you can use <b>HTML</b> tags to spice it * up. If you use Java keywords, package names, variables, code examples, * class names or method names (e.g., <code>method()</code>), place * 'code' tags around them. After the description, edit the tags below. * </P> * * @see AnotherClass * @see AnotherClass#methodName * * Revision History: * * Ver. Date Author Description * ----- ---------- -------------- ------------------------------------ * 1.0 Nov 24, 2005 eborejsza Initial Creation */ public class LookupNestedMapHandler implements ResultSetHandler { private static int MAP_RESULT_SET_COLUMNS = 3; /** * Default constructor. */ public LookupNestedMapHandler() { super(); } /** * Iterates through the query result set and creates a correctly * indexed map of maps. This is only intended to work with a result * set that contains three columns and the first column must * cast successfully to a <code>Integer</code> datatype. * * @param rs The result set that is used to obtain the name/value data. * @return Object The <code>java.util.Map</code> collection containing the results. * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet) */ public Object handle(ResultSet rs) throws SQLException { Map results = new HashMap(); Map nestedMap = new HashMap(); Integer currentId = null; // Get the number of columns in the result set. ResultSetMetaData rsmd = rs.getMetaData(); int cols = rsmd.getColumnCount(); // Populate the Map with the name value pairs // if the result set contains two columns of data. if (cols == MAP_RESULT_SET_COLUMNS) { while (rs.next()) { Object primaryId = rs.getObject(1); Object secondaryId = rs.getObject(2); Object secondaryValue = rs.getObject(3); // Initialize the current id on the first iteration. if (currentId == null) { currentId = (Integer) primaryId; } // Check if the primary id is not the same as the current id. if (currentId.compareTo(primaryId) != 0) { // Put the current nested map into the result map // and create a new nested map. results.put(currentId, nestedMap); nestedMap = new HashMap(); currentId = (Integer) primaryId; } // Put the key & value into the current nested map. // This occurs after checking if a new nested map is required. nestedMap.put(secondaryId, secondaryValue); } // Put the final nested map into the results map // once the iterations are complete. results.put(currentId, nestedMap); } return results; } }