001: package com.jamonapi.utils;
002:
003: /** This class builds the returned array data based on info from header and first row.
004: * If header has more columns than data then each data is padded with nulls.
005: * If first rows data has more columns than header then the header is padded with headers named 'colN' where N is the column number.
006: * If any rows of data have fewer columns than either the data in the first row or the header
007: * they are shrunken or grown as neccesary filling any excess columns with nulls, and truncating any excess columns.
008: *
009: * @author steve souza
010: *
011: */
012: public class BufferListDetailData implements DetailData {
013:
014: private String[] header;
015: private Object[][] data;
016: private Object[] firstRow;
017: private int colsInHeader;
018: private int colsInFirstRow;
019:
020: public BufferListDetailData(BufferList bufferList) {
021: initalize(bufferList);
022: }
023:
024: public String[] getHeader() {
025: return header;
026: }
027:
028: public Object[][] getData() {
029: return data;
030: }
031:
032: public int getRowCount() {
033: if (isEmpty())
034: return 0;
035: else
036: return data.length;
037:
038: }
039:
040: public boolean hasData() {
041: return !isEmpty();
042: }
043:
044: public boolean isEmpty() {
045: return (data == null || data.length == 0);
046: }
047:
048: private void initalize(BufferList bufferList) {
049:
050: /* object is value object.
051: * if header is bigger than data of first row then return header up to column width
052: * if header is smaller than data then PAD HEADER with colN
053: * if first row is smaller than subsequent rows then use colsize of first row and exclude other cols
054: * if first row is bigger than subsequent rows then pad row iwth nulls
055: */
056:
057: Object[] rows = getAllRows(bufferList);
058: if (rows != null) {
059: firstRow = getRow(rows[0]);
060: colsInFirstRow = firstRow.length;
061: }
062:
063: colsInHeader = (bufferList.getHeader() == null) ? 0
064: : bufferList.getHeader().length;
065: header = buildHeader(bufferList.getHeader());
066: data = buildData(rows);
067: }
068:
069: private Object[] getAllRows(BufferList bufferList) {
070: Object[] bufferListArray = null;
071: synchronized (bufferList) {
072: if (bufferList.getRowCount() > 0) {
073: bufferListArray = bufferList.getCollection().toArray();
074: }
075: }
076:
077: return bufferListArray;
078: }
079:
080: private String[] buildHeader(String[] h) {
081: // if header is bigger than data of first row then return header up to column width
082: // if header is smaller than data then PAD HEADER with colN
083:
084: Object[] head = resize(h, getColCount());
085: if (head == null)
086: return null;
087:
088: String[] headerStr = new String[head.length];
089: for (int i = 0; i < head.length; i++) {
090: if (head[i] == null) {
091: headerStr[i] = "col" + i;
092: } else
093: headerStr[i] = head[i].toString();
094: }
095:
096: return headerStr;
097:
098: }
099:
100: private Object[][] buildData(Object[] rows) {
101: if (rows == null)
102: return null;
103:
104: Object[][] localData = new Object[rows.length][];
105: int numCols = getColCount();
106: for (int i = 0; i < rows.length; i++) {
107: localData[i] = resize(getRow(rows[i]), numCols);
108: }
109:
110: return localData;
111: }
112:
113: private Object[] resize(Object[] originalData, int size) {
114: if (originalData == null)
115: return null;
116: else if (size == originalData.length)
117: return originalData;
118:
119: Object[] newData = new Object[size];
120: // (shouldShrink) ? shrink : grow
121: int loopSize = (originalData.length > size) ? size
122: : originalData.length;
123:
124: for (int i = 0; i < loopSize; i++) {
125: newData[i] = originalData[i];
126: }
127:
128: return newData;
129: }
130:
131: private Object[] getRow(Object obj) {
132: if (obj instanceof Object[])
133: return (Object[]) obj;
134: else if (obj instanceof ToArray)
135: return ((ToArray) obj).toArray();
136: else
137: return new Object[] { obj };
138: }
139:
140: private int getColCount() {
141: return (colsInHeader > colsInFirstRow) ? colsInHeader
142: : colsInFirstRow;
143: }
144:
145: }
|