001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * for use in the design, construction, operation or maintenance of
035: * any nuclear facility.
036: */
037: package com.sun.portal.iwayutil.sorting;
038:
039: /**
040: *
041: * This class provides the utility methods sort the table data
042: *
043: * @version 1.0
044: * @author Deepak H P
045: * @date 02 May 2005
046: *
047: **/
048:
049: import java.util.*;
050:
051: public class TableSorting {
052:
053: public static Vector sortTable(Vector tableData,
054: Hashtable sortingParams) {
055:
056: int i = 0;
057: int index = 0;
058: String keyValue = null;
059: String[] sortedFields = new String[tableData.size()];
060: Hashtable rowData = new Hashtable();
061: Vector sortedTable = new Vector();
062: Vector listToSort = new Vector();
063: Comparator comp = null;
064: String sortOrder = (String) sortingParams.get("SortOrder");
065: String key = (String) sortingParams.get("SortColumn");
066: String sortElementType = (String) sortingParams
067: .get("SortElementType");
068:
069: debug("Sorting Table of Size " + tableData.size());
070: debug("Sorting Table For Key " + key);
071: debug("Sorting Order " + sortOrder);
072: debug("Sorting sortElementType " + sortElementType);
073:
074: for (i = 0; i < tableData.size(); i++) {
075: sortedTable.add(rowData);
076: }
077:
078: for (i = 0; i < tableData.size(); i++) {
079: rowData = (Hashtable) tableData.elementAt(i);
080: keyValue = (String) rowData.get(key);
081: listToSort.add(keyValue);
082: }
083:
084: if (sortElementType == null) {
085:
086: if (sortOrder.equalsIgnoreCase("up")) {
087: comp = new TableDataComparator("up");
088: } else {
089: comp = new TableDataComparator("down");
090: }
091: } else if (sortElementType.equals("NUMBER")) {
092: if (sortOrder.equalsIgnoreCase("up")) {
093: comp = new TableNumberDataComparator("up");
094: } else {
095: comp = new TableNumberDataComparator("down");
096: }
097: }
098:
099: Collections.sort(listToSort, comp);
100:
101: for (i = 0; i < tableData.size(); i++) {
102: keyValue = (String) listToSort.elementAt(i);
103: sortedFields[i] = keyValue;
104: }
105:
106: for (i = 0; i < tableData.size(); i++) {
107: rowData = (Hashtable) tableData.elementAt(i);
108: keyValue = (String) rowData.get(key);
109: index = getPosition(sortedFields, keyValue);
110: sortedTable.removeElementAt(index);
111: sortedTable.add(index, rowData);
112: }
113: return sortedTable;
114: }
115:
116: public static Vector sortTable(Vector tableData, String key,
117: String sortOrder) {
118:
119: int i = 0;
120: int index = 0;
121: String keyValue = null;
122: String[] sortedFields = new String[tableData.size()];
123: Hashtable rowData = new Hashtable();
124: Vector sortedTable = new Vector();
125: Vector listToSort = new Vector();
126: Comparator comp = null;
127:
128: debug("Sorting Table of Size " + tableData.size());
129: debug("Sorting Table For Key " + key);
130: debug("Sorting Order " + sortOrder);
131:
132: for (i = 0; i < tableData.size(); i++) {
133: sortedTable.add(rowData);
134: }
135:
136: for (i = 0; i < tableData.size(); i++) {
137: rowData = (Hashtable) tableData.elementAt(i);
138: keyValue = (String) rowData.get(key);
139: listToSort.add(keyValue);
140: }
141:
142: if (sortOrder.equalsIgnoreCase("up")) {
143: comp = new TableDataComparator("up");
144: } else {
145: comp = new TableDataComparator("down");
146: }
147:
148: Collections.sort(listToSort, comp);
149:
150: for (i = 0; i < tableData.size(); i++) {
151: keyValue = (String) listToSort.elementAt(i);
152: sortedFields[i] = keyValue;
153: }
154:
155: for (i = 0; i < tableData.size(); i++) {
156: rowData = (Hashtable) tableData.elementAt(i);
157: keyValue = (String) rowData.get(key);
158: index = getPosition(sortedFields, keyValue);
159: sortedTable.removeElementAt(index);
160: sortedTable.add(index, rowData);
161: }
162: return sortedTable;
163: }
164:
165: private static int getPosition(String[] sortedFields,
166: String keyValue) {
167:
168: int i = 0;
169: String value = null;
170:
171: for (i = 0; i < sortedFields.length; i++) {
172:
173: value = sortedFields[i];
174:
175: if ((value != null) && (value.equals(keyValue))) {
176: sortedFields[i] = null;
177: break;
178: }
179: }
180: return i;
181: }
182:
183: /**
184: * This method will print the debug messages.
185: **/
186: private static void debug(Object msg) {
187: System.out.println("TableSorting : " + msg);
188: }
189: }
190:
191: class TableDataComparator implements Comparator {
192:
193: String sortDirection = null;
194:
195: public TableDataComparator(String direction) {
196: sortDirection = direction;
197: }
198:
199: public int compare(Object o1, Object o2) {
200:
201: int retVal = 0;
202:
203: String str1 = (String) o1;
204: str1 = str1.toUpperCase();
205:
206: String str2 = (String) o2;
207: str2 = str2.toUpperCase();
208:
209: int len1 = str1.length();
210: int len2 = str2.length();
211:
212: int j = len1;
213:
214: if (len1 > len2) {
215: j = len2;
216: }
217:
218: for (int i = 0; i < j; i++) {
219:
220: char a = str1.charAt(i);
221: char b = str2.charAt(i);
222:
223: if (a > b) {
224:
225: if (sortDirection.equalsIgnoreCase("up")) {
226: retVal = -1;
227: } else {
228: retVal = 1;
229: }
230: break;
231: } else if (a < b) {
232: if (sortDirection.equalsIgnoreCase("up")) {
233: retVal = 1;
234: } else {
235: retVal = -1;
236: }
237: break;
238: }
239: } // End of For loop
240: return retVal;
241: } // End of Method
242: } // End of class comparator
243:
244: class TableNumberDataComparator implements Comparator {
245:
246: String sortDirection = null;
247:
248: public TableNumberDataComparator(String direction) {
249: sortDirection = direction;
250: }
251:
252: private String getAbsoluteValue(String str) {
253:
254: String retVal = str;
255:
256: try {
257: retVal = str.toUpperCase();
258: int index = retVal.lastIndexOf(" ");
259: if (index > 0) {
260: retVal = retVal.substring(index, retVal.length());
261: }
262: } catch (Exception ex) {
263: retVal = str;
264: }
265: return retVal;
266: }
267:
268: public int compare(Object o1, Object o2) {
269:
270: int retVal = 0;
271:
272: String str1 = (String) o1;
273: str1 = getAbsoluteValue(str1);
274: //str1 = str1.toUpperCase();
275:
276: String str2 = (String) o2;
277: str2 = getAbsoluteValue(str2);
278: //str2 = str2.toUpperCase();
279:
280: int val1 = 0;
281: int val2 = 0;
282:
283: try {
284: val1 = Integer.parseInt(str1);
285: } catch (Exception ex) {
286: if (sortDirection.equalsIgnoreCase("up")) {
287: retVal = 1;
288: } else {
289: retVal = -1;
290: }
291: }
292:
293: try {
294: val2 = Integer.parseInt(str2);
295: } catch (Exception ex) {
296: if (sortDirection.equalsIgnoreCase("up")) {
297: retVal = -1;
298: } else {
299: retVal = 1;
300: }
301: }
302:
303: if (retVal != 0) {
304: return retVal;
305: }
306:
307: if (val1 > val2) {
308:
309: if (sortDirection.equalsIgnoreCase("up")) {
310: retVal = -1;
311: } else {
312: retVal = 1;
313: }
314: } else if (val1 < val2) {
315: if (sortDirection.equalsIgnoreCase("up")) {
316: retVal = 1;
317: } else {
318: retVal = -1;
319: }
320: }
321: return retVal;
322: } // End of Method
323: } // End of class comparator
|