blob: 591659505269bc921112beb3cb426113c653c446 [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*/
package org.eclipse.osbp.xtext.datamart.common.olap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.eclipse.osbp.dsl.entity.xtext.util.PersistenceNamingUtils;
import org.eclipse.osbp.ui.api.datamart.DerivedOlapException;
/**
* Do not mistake {@link Cell2dTable} with {@link DerivedCellSet}!
* <ul>
* <li>{@link DerivedCellSet} corresponds to OLAP and Mondrian structure of data!</li>
* <li>{@link Cell2dTable} allows a 2d access to the data via <code>headers</code> and <code>rows</code>!</li>
* </ul>
*/
public class Cell2dTable {
private final DerivedCellSet fCellSet;
private final Map<Integer,HeaderMapping> fIndexToHeaderMap;
private final Map<String,HeaderMapping> fNameToHeaderMap;
public Cell2dTable(DerivedCellSet cellSet) throws DerivedOlapException {
fCellSet = cellSet;
fIndexToHeaderMap = new TreeMap<Integer,HeaderMapping>();
fNameToHeaderMap = new HashMap<String,HeaderMapping>();
if (cellSet == null) {
throw new DerivedOlapException("referenced datamart EmployeeSalary generates no results");
} else if (cellSet.getAxes().size() < 2) {
throw new DerivedOlapException("at least 2 axes from referenced datamart EmployeeSalary are needed to render EmployeeSalary");
} else {
try {
// --- first get the header for all "rows" ---
DerivedHierarchy hier = fCellSet.getAxes().get(DerivedAxis.AXIS_ROWS).getPositions().get(0).getMembers().get(0).getHierarchy();
if (hier != null) {
int count = 0;
for (DerivedLevel lev : hier.getLevels()) {
HeaderMapping headerMap = new HeaderMapping(
fIndexToHeaderMap.size(),
// see DatamartDSLJvmInferrer & ReportDSLJvmInferrer
PersistenceNamingUtils.camelCaseToUpperCase(lev.getName()).toLowerCase(),
DerivedAxis.AXIS_ROWS,
count);
fIndexToHeaderMap.put(headerMap.fHeaderIndex, headerMap);
fNameToHeaderMap.put(headerMap.fHeaderName, headerMap);
count++;
}
}
}
catch (DerivedOlapException oe) {
throw oe;
}
catch (Exception e) {
}
try {
// --- next get the header for all "columns" ---
// Column header
// See
// http://www.olap4j.org/api/index.html?org/olap4j/Position.html
// on how Position works, it helps a lot
// Every position will be a column in the header
int count = 0;
for (DerivedPosition pos : fCellSet.getAxes().get(DerivedAxis.AXIS_COLUMNS).getPositions()) {
// --- only use 1st member ---
HeaderMapping headerMap = new HeaderMapping(
fIndexToHeaderMap.size(),
// see DatamartDSLJvmInferrer & ReportDSLJvmInferrer
PersistenceNamingUtils.camelCaseToUpperCase(pos.getMembers().get(0).getCaption()).toLowerCase(),
DerivedAxis.AXIS_COLUMNS,
count);
fIndexToHeaderMap.put(headerMap.fHeaderIndex, headerMap);
fNameToHeaderMap.put(headerMap.fHeaderName, headerMap);
count++;
}
}
catch (Exception e) {
throw new DerivedOlapException(e.getLocalizedMessage());
}
}
}
private HeaderMapping header(int index) {
return fIndexToHeaderMap.get(index);
}
private HeaderMapping header(String name) {
// see DatamartDSLJvmInferrer & ReportDSLJvmInferrer
return fNameToHeaderMap.get(PersistenceNamingUtils.camelCaseToUpperCase(name).toLowerCase());
}
/**
* @return the count of headers
*/
public int getHeaderCount() {
return fIndexToHeaderMap.size();
}
/**
* @param name
* @return the index of the header or -1 if it doesn't exist
*/
public int getHeaderIndex(String name) {
HeaderMapping header = header(name);
return header == null ? -1 : header.fHeaderIndex;
}
/**
* @param index
* @return the name of the header or null if it doesn't exist
*/
public String getHeaderName(int index) {
HeaderMapping header = header(index);
return header == null ? null : header.fHeaderName;
}
/**
* @return count of rows
*/
public int getRowCount() {
return fCellSet.getAxes().get(DerivedAxis.AXIS_ROWS).getPositionCount();
}
/**
* <b>Do not mistake the position of <a>int headerIndex</a>/<a>int rowIndex</a> with <a>List<Integer> coordinate</a>!</b><br>
* This method gets the cell depending on the headerIndex as retrieved by {@link #getHeaderIndex(String)}!<br>
* <ul>
* <li>{@link #getCell(int, int)} is <u>analog</u> to {@link #getCell(String, int)}</li>
* <li>{@link #getCell(int, int)} is <u><b>not</b> analog</u> to {@link DerivedCellSet#getCell(List)}</li>
* </ul>
* @param headerIndex
* @param rowIndex
* @return
*/
public DerivedCell getCell(int headerIndex, int rowIndex) {
return getCell(header(headerIndex), rowIndex);
}
/**
* <b>Do not mistake the position of <a>int headerName</a>/<a>int rowIndex</a> with <a>List<Integer> coordinate</a>!</b><br>
* This method gets the cell depending on the headerIndex as retrieved by {@link #getHeaderName(int)}!
* <ul>
* <li>{@link #getCell(String, int)} is <u>analog</u> to {@link #getCell(int, int)}</li>
* <li>{@link #getCell(String, int)} is <u><b>not</b> analog</u> to {@link DerivedCellSet#getCell(List)}</li>
* </ul>
* @param headerIndex
* @param rowIndex
* @return
*/
public DerivedCell getCell(String headerName, int rowIndex) {
return getCell(header(headerName), rowIndex);
}
private DerivedCell getCell(HeaderMapping header, int rowIndex) {
DerivedCell result = null;
if (header != null) {
switch (header.fAxisNo) {
case DerivedAxis.AXIS_ROWS:
DerivedMember member = fCellSet.getAxes().get(DerivedAxis.AXIS_ROWS).getPositions().get(rowIndex).getMembers().get(0);
String[] tokens = member.getUniqueName().split("\\]\\.\\[");
if (header.fIndexOnAxis < tokens.length) {
String value = tokens[header.fIndexOnAxis+1].replace("[", "").replace("]", "");
result = new DerivedCell(value, value);
}
break;
case DerivedAxis.AXIS_COLUMNS:
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(header.fIndexOnAxis); // coordinte
list.add(rowIndex); // coordinte
try {
result = fCellSet.getCell(list);
} catch (DerivedOlapException e) {
e.printStackTrace();
}
break;
}
}
return result;
}
}