| /** |
| * |
| * 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 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * 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; |
| |
| /** |
| * 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; |
| |
| @SuppressWarnings("restriction") |
| public Cell2dTable(DerivedCellSet cellSet) { |
| fCellSet = cellSet; |
| fIndexToHeaderMap = new TreeMap<Integer,HeaderMapping>(); |
| fNameToHeaderMap = new HashMap<String,HeaderMapping>(); |
| |
| if (cellSet != null && cellSet.getAxes().size() > 1) { |
| 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( |
| -hier.getLevels().size() + count, |
| lev.getName(), |
| DerivedAxis.AXIS_ROWS, |
| count); |
| fIndexToHeaderMap.put(headerMap.fHeaderIndex, headerMap); |
| fNameToHeaderMap.put(headerMap.fHeaderName, headerMap); |
| count++; |
| } |
| } |
| } |
| catch (Exception e) { |
| } |
| |
| // --- 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( |
| count, |
| pos.getMembers().get(0).getCaption(), |
| DerivedAxis.AXIS_COLUMNS, |
| count); |
| fIndexToHeaderMap.put(headerMap.fHeaderIndex, headerMap); |
| fNameToHeaderMap.put(headerMap.fHeaderName, headerMap); |
| count++; |
| } |
| } |
| } |
| |
| private HeaderMapping header(int index) { |
| return fIndexToHeaderMap.get(index); |
| } |
| |
| private HeaderMapping header(String name) { |
| // see DatamartDSLJvmInferrer & ReportDSLJvmInferrer |
| for(String col:fNameToHeaderMap.keySet()) { |
| if(col.equalsIgnoreCase(name)) { |
| return fNameToHeaderMap.get(col); |
| } |
| } |
| return null; |
| } |
| |
| /** |
| * @return the count of headers |
| */ |
| public int getHeaderCount() { |
| return fIndexToHeaderMap.size(); |
| } |
| |
| /** |
| * @param name |
| * @return the index of the header or -99 if it doesn't exist |
| */ |
| public int getHeaderIndex(String name) { |
| HeaderMapping header = header(name); |
| return header == null ? -99 : 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 |
| result = fCellSet.getCell(list); |
| break; |
| } |
| } |
| return result; |
| } |
| } |