Revision 4875
Added by ben leinfelder over 15 years ago
download.jsp | ||
---|---|---|
56 | 56 |
} |
57 | 57 |
%><%! |
58 | 58 |
private List transpose(ResultSet rs, int idCol, int pivotCol, List pivotAttributes, boolean omitIdValues) throws SQLException { |
59 |
//map keyed by id column - data |
|
59 | 60 |
OrderedMap table = new OrderedMap(); |
61 |
//track all the data columns |
|
60 | 62 |
OrderedMap widestRow = new OrderedMap(); |
63 |
//map keyed by the pivot column - metadata |
|
64 |
OrderedMap headerRows = new OrderedMap(); |
|
65 |
|
|
61 | 66 |
int colCount = rs.getMetaData().getColumnCount(); |
62 | 67 |
String idColName = rs.getMetaData().getColumnName(idCol); |
63 | 68 |
|
... | ... | |
65 | 70 |
String id = rs.getString(idCol); |
66 | 71 |
String pivotValue = rs.getString(pivotCol); |
67 | 72 |
|
68 |
//look up the row we are working on |
|
73 |
//look up the data row we are working on
|
|
69 | 74 |
OrderedMap row = (OrderedMap) table.get(id); |
70 | 75 |
if (row == null) { |
71 | 76 |
row = new OrderedMap(); |
72 | 77 |
} |
78 |
//look up the metadata row we are working on |
|
79 |
OrderedMap metadataRow = (OrderedMap) table.get(pivotValue); |
|
80 |
if (metadataRow == null) { |
|
81 |
metadataRow = new OrderedMap(); |
|
82 |
} |
|
73 | 83 |
|
74 | 84 |
//get the values for this pivot |
75 | 85 |
for (int i = 1; i <= colCount; i++) { |
76 | 86 |
if (i != pivotCol) { |
77 | 87 |
String colName = rs.getMetaData().getColumnName(i); |
88 |
String value = rs.getString(i); |
|
78 | 89 |
//do we include this column in the pivot? |
79 | 90 |
if (pivotAttributes.contains(colName)) { |
80 | 91 |
//annotate the column name with the pivot column value if not the id column |
81 | 92 |
if (i != idCol) { |
82 | 93 |
colName = pivotValue + "_" + colName; |
83 | 94 |
} |
84 |
String value = rs.getString(i); |
|
85 | 95 |
row.put(colName, value); |
86 | 96 |
} |
97 |
else { |
|
98 |
metadataRow.put(colName, value); |
|
99 |
} |
|
87 | 100 |
} |
88 | 101 |
} |
89 |
//track the headers - the values are junk
|
|
102 |
//track the data columns - the values are junk
|
|
90 | 103 |
widestRow.putAll(row); |
91 | 104 |
|
92 | 105 |
//put the row back (or maybe it's the first time) |
93 |
table.put(id, row); |
|
106 |
table.put(id, row); |
|
107 |
|
|
108 |
//put the metadata header back |
|
109 |
headerRows.put(pivotValue, metadataRow); |
|
110 |
|
|
94 | 111 |
} |
95 | 112 |
|
96 | 113 |
//now make it into a list |
97 | 114 |
List retTable = new ArrayList(); |
98 | 115 |
|
99 |
//do the header, based on widest entry |
|
116 |
//map keyed by metadata labels |
|
117 |
OrderedMap metadataHeaders = new OrderedMap(); |
|
118 |
|
|
119 |
//do the data header - drives the other columns - based on widest entry |
|
100 | 120 |
List header = new ArrayList(widestRow.keySet()); |
121 |
|
|
122 |
//do the metadata header rows (basically rotate them around) |
|
123 |
Iterator headerIter = header.iterator(); |
|
124 |
while (headerIter.hasNext()) { |
|
125 |
String column = (String) headerIter.next(); |
|
126 |
//get the pivotValue part of column name |
|
127 |
String pivotValue = column; |
|
128 |
try { |
|
129 |
pivotValue = column.substring(0, column.indexOf("_")); |
|
130 |
} |
|
131 |
catch (Exception e) {} |
|
132 |
//look up the row from the metadata - keyed by pivot value |
|
133 |
OrderedMap metadataRow = (OrderedMap) headerRows.get(pivotValue); |
|
134 |
if (metadataRow != null) { |
|
135 |
//go through the values |
|
136 |
Iterator metadataIter = metadataRow.keySet().iterator(); |
|
137 |
while (metadataIter.hasNext()) { |
|
138 |
String key = (String) metadataIter.next(); |
|
139 |
String value = (String) metadataRow.get(key); |
|
140 |
OrderedMap newMetadataRow = (OrderedMap) metadataHeaders.get(key); |
|
141 |
if (newMetadataRow == null) { |
|
142 |
newMetadataRow = new OrderedMap(); |
|
143 |
} |
|
144 |
newMetadataRow.put(column, value); |
|
145 |
metadataHeaders.put(key, newMetadataRow); |
|
146 |
} |
|
147 |
} |
|
148 |
} |
|
149 |
|
|
150 |
//make metadata rows as list/arrays on the reteurn table |
|
151 |
Iterator metadataLabelIter = metadataHeaders.keySet().iterator(); |
|
152 |
while (metadataLabelIter.hasNext()) { |
|
153 |
String label = (String) metadataLabelIter.next(); |
|
154 |
OrderedMap row = (OrderedMap) metadataHeaders.get(label); |
|
155 |
List rowValues = new ArrayList(row.values()); |
|
156 |
rowValues.add(0, label); |
|
157 |
retTable.add(rowValues.toArray(new String[0])); |
|
158 |
} |
|
159 |
|
|
160 |
//put the data header row on the table |
|
101 | 161 |
retTable.add(header.toArray(new String[0])); |
102 | 162 |
|
103 |
//now the value rows |
|
163 |
//now the value rows in the table
|
|
104 | 164 |
Iterator rowIter = table.values().iterator(); |
105 | 165 |
int rowCount = 1; |
106 | 166 |
while (rowIter.hasNext()) { |
Also available in: Unified diff
Now the metadata attributes show as rows above the data.
very close!
todo: special handling for thesaurus (ie NBII/Bloom) metadata fields