// corner.
final int yOffset = columnsAxisInfo.getWidth();
final int xOffsset = rowsAxisInfo.getWidth();
// Populate a string matrix
final Matrix matrix = new Matrix(xOffsset + (columnsAxis == null ? 1 : columnsAxis.getPositions().size()),
yOffset + (rowsAxis == null ? 1 : rowsAxis.getPositions().size()));
// Populate corner
List<Level> levels = new ArrayList<Level>();
if (rowsAxis != null && rowsAxis.getPositions().size() > 0) {
Position p = rowsAxis.getPositions().get(0);
for (int m = 0; m < p.getMembers().size(); m++) {
AxisOrdinalInfo a = rowsAxisInfo.ordinalInfos.get(m);
for (Integer depth : a.getDepths()) {
levels.add(a.getLevel(depth));
}
}
for (int x = 0; x < xOffsset; x++) {
Level xLevel = levels.get(x);
String s = xLevel.getCaption();
for (int y = 0; y < yOffset; y++) {
final MemberCell memberInfo = new MemberCell(x > 0);
if (y == yOffset - 1) {
memberInfo.setRawValue(s);
memberInfo.setFormattedValue(s);
memberInfo.setProperty("__headertype", "row_header_header");
memberInfo.setProperty("levelindex", "" + levels.indexOf(xLevel));
memberInfo.setHierarchy(xLevel.getHierarchy().getUniqueName());
memberInfo.setParentDimension(xLevel.getDimension().getName());
memberInfo.setLevel(xLevel.getUniqueName());
}
matrix.set(x, y, memberInfo);
}
}
}
// Populate matrix with cells representing axes
populateAxis(matrix, columnsAxis, columnsAxisInfo, true, xOffsset);
populateAxis(matrix, rowsAxis, rowsAxisInfo, false, yOffset);
// TODO - why did we do this in the first place??? HERE BE DRAGONS
//int headerwidth = matrix.getMatrixWidth();
//if (headerwidth > 2) {
//for(int yy=matrix.getMatrixHeight(); yy > matrix.getOffset() ; yy--) {
//for(int xx=0; xx < headerwidth-1;xx++) {
//if (matrix.get(xx,yy-1) != null
// && matrix.get(xx,yy) != null && matrix.get(xx,yy-1).getRawValue() != null
//&& matrix.get(xx,yy-1).
// getRawValue().equals(matrix.get(xx, yy).getRawValue()))
//{
//matrix.set(xx, yy, new MemberCell());
//}
//else {
//break;
//}
//}
//}
//}
// Populate cell values
int newyOffset = yOffset;
int newxOffset = xOffsset;
List<Integer> donex = new ArrayList<Integer>();
List<Integer> doney = new ArrayList<Integer>();
for (final Cell cell : cellIter(pageCoords, cellSet)) {
final List<Integer> coordList = cell.getCoordinateList();
int y = newyOffset;
int x = newxOffset;
if (coordList.size() > 0) {
if (coordList.get(0) == 0) {
newxOffset = xOffsset;
donex = new ArrayList<Integer>();
}
x = newxOffset;
if (coordList.size() > 0) {
x += coordList.get(0);
}
y = newyOffset;
if (coordList.size() > 1) {
y += coordList.get(1);
}
boolean stop = false;
if (coordList.size() > 0 && ignorex.contains(coordList.get(0))) {
if (!donex.contains(coordList.get(0))) {
newxOffset--;
donex.add(coordList.get(0));
}
stop = true;
}
if (coordList.size() > 1 && ignorey.contains(coordList.get(1))) {
if (!doney.contains(coordList.get(1))) {
newyOffset--;
doney.add(coordList.get(1));
}
stop = true;
}
if (stop) {
continue;
}
}
final DataCell cellInfo = new DataCell(true, coordList);
cellInfo.setCoordinates(cell.getCoordinateList());
if (cell.getValue() != null) {
try {
cellInfo.setRawNumber(cell.getDoubleValue());
} catch (Exception e1) {
LOG.error("Could not get double", e1);
}
}
String cellValue = cell.getFormattedValue(); // First try to get a
// formatted value
if (cellValue == null || cellValue.equals("null")) { //$NON-NLS-1$
cellValue = ""; //$NON-NLS-1$
}
if (cellValue.length() < 1) {
final Object value = cell.getValue();
if (value == null || value.equals("null")) { //$NON-NLS-1$
cellValue = ""; //$NON-NLS-1$
} else {
try {
// TODO this needs to become query / execution specific
DecimalFormat myFormatter = new DecimalFormat(SaikuProperties.FORMATDEFAULTNUMBERFORMAT); //$NON-NLS-1$
DecimalFormatSymbols dfs = new DecimalFormatSymbols(SaikuProperties.LOCALE);
myFormatter.setDecimalFormatSymbols(dfs);
String output = myFormatter.format(cell.getValue());
cellValue = output;
} catch (Exception e) {
// TODO: handle exception
}
}
// the raw value
}
// Format string is relevant for Excel export
// xmla cells can throw an error on this
try {
String formatString = (String) cell.getPropertyValue(Property.StandardCellProperty.FORMAT_STRING);
if (formatString != null && !formatString.startsWith("|")) {
cellInfo.setFormatString(formatString);
} else {
formatString = formatString.substring(1, formatString.length());
cellInfo.setFormatString(formatString.substring(0, formatString.indexOf("|")));
}
} catch (Exception e) {
// we tried
}
Map<String, String> cellProperties = new HashMap<String, String>();
String val = Olap4jUtil.parseFormattedCellValue(cellValue, cellProperties);
if (!cellProperties.isEmpty()) {
cellInfo.setProperties(cellProperties);
}
cellInfo.setFormattedValue(val);
matrix.set(x, y, cellInfo);
}
return matrix;
}