blob: 1aa783256e75b14d60fdb6f0ee13ffd7303ded4a [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 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.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;
import mondrian.olap.Result;
import org.apache.commons.lang3.ArrayUtils;
import org.eclipse.osbp.bpm.api.BPMTaskSummary;
import org.eclipse.osbp.ui.api.metadata.IDSLMetadataService;
import org.eclipse.osbp.ui.api.user.IUser;
import org.eclipse.osbp.xtext.datamart.common.sql.OperativeDtoContainer;
import org.eclipse.osbp.xtext.datamart.common.sql.SqlCellSet;
import org.olap4j.CellSet;
import org.olap4j.CellSetAxis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 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 DerivedCellSet{
private static final Logger cellLog = LoggerFactory.getLogger(DerivedCellSet.class);
private final ResultSetMetaData resultSetMetaData;
private CellSet olapCellset = null;
private Result mondrianCellset = null;
private List<BPMTaskSummary> taskCellset = null;
private List<DerivedAxis> axisList = new ArrayList<>();
private IDSLMetadataService dslMetadataService;
private IUser user;
public DerivedCellSet(CellSet olapCellset, ResultSetMetaData resultSetMetaData, IDSLMetadataService dslMetadataService, IUser user) {
super();
this.dslMetadataService = dslMetadataService;
this.user = user;
this.resultSetMetaData = resultSetMetaData;
this.olapCellset = olapCellset;
if (olapCellset != null) {
for (CellSetAxis olapAxis : olapCellset.getAxes()) {
axisList.add(new DerivedAxis(olapAxis, this));
}
}
}
public DerivedCellSet(Result mondrianCellset, IDSLMetadataService dslMetadataService, IUser user) {
super();
this.dslMetadataService = dslMetadataService;
this.user = user;
this.resultSetMetaData = null;
this.mondrianCellset = mondrianCellset;
if (mondrianCellset != null) {
for (int i = 0; i < mondrianCellset.getAxes().length; i++) {
axisList.add(new DerivedAxis(mondrianCellset.getAxes()[i], this));
}
}
}
public DerivedCellSet(List<BPMTaskSummary> tasks, IDSLMetadataService dslMetadataService, IUser user) {
super();
this.dslMetadataService = dslMetadataService;
this.user = user;
this.resultSetMetaData = null;
this.taskCellset = tasks;
axisList.add(new DerivedAxis(tasks, 0, this));
axisList.add(new DerivedAxis(tasks, 1, this));
}
public OperativeDtoContainer getOperativeDtoContainer() {
if (olapCellset instanceof SqlCellSet) {
return ((SqlCellSet) olapCellset).getOperativeDtoContainer();
}
return null;
}
public ResultSetMetaData getResultSetMetaData() {
return resultSetMetaData;
}
public Object getInstance() {
if (olapCellset!=null){
return olapCellset;
} else if (mondrianCellset != null){
return mondrianCellset;
} else if (taskCellset != null) {
return taskCellset;
}
return null;
}
public List<DerivedAxis> getAxes(){
return axisList;
}
/**
* <b>Do not mistake the position of <a>List<Integer> coordinate</a> with <a>int headerName</a>/<a>int rowIndex</a>!</b><br>
* This method gets the cell as described in OLAP, Mondrian or the like!
* <ul>
* <li>{@link #getCell(List)} is <u><b>neither</b> analog</u> to {@link Cell2dTable#getCell(int, int)} <u><b>nor</b></u> to {@link Cell2dTable#getCell(String, int)}</li>
* <li>{@link Cell2dTable#getCell(int, int)} is <u>analog</u> to {@link Cell2dTable#getCell(String, int)}</li>
* </ul>
* @param headerIndex
* @param rowIndex
* @return
*/
public DerivedCell getCell(List<Integer> coordinate) {
DerivedCell cxCell = null;
try {
if (olapCellset!=null){
cxCell = new DerivedCell(olapCellset.getCell(coordinate));
} else if (mondrianCellset != null){
cxCell = new DerivedCell(getMondrianCell(mondrianCellset,coordinate));
} else if (taskCellset != null) {
cxCell = new DerivedCell(getTaskCellValue(taskCellset,coordinate), getTaskCellFormattedValue(taskCellset,coordinate));
}
} catch (Exception e) {
cellLog.error("{}", e);
}
return cxCell;
}
private mondrian.olap.Cell getMondrianCell(Result mondrianCellset, List<Integer> coordinate){
int[] coordinateArray = ArrayUtils.toPrimitive(coordinate
.toArray(new Integer[coordinate.size()]));
return mondrianCellset.getCell(coordinateArray);
}
private Object getTaskCellValue(List<BPMTaskSummary> taskCellset, List<Integer> coordinate){
BPMTaskSummary task = taskCellset.get(coordinate.get(1));
switch(coordinate.get(0)) {
case 0: return task.getName();
case 1: return task.getPriority();
case 2: return task.getStatus().ordinal();
case 3: return task.getSubject();
case 4: return task.getDescription();
case 5: return task.getExpirationTime();
case 6: return task.getCreatedOn();
case 7: return task.getCreatedBy();
case 8: return task.getActivationTime();
case 9: return task.getActualOwner();
case 10:return task.getId();
default:
break;
}
return null;
}
private String getTaskCellFormattedValue(List<BPMTaskSummary> taskCellset, List<Integer> coordinate){
BPMTaskSummary task = taskCellset.get(coordinate.get(1));
switch(coordinate.get(0)) {
case 0: return task.getName();
case 1:
return Integer.toString(task.getPriority());
case 2:
return task.getStatus().name();
case 3: return task.getSubject();
case 4: return task.getDescription();
case 5:
if (task.getExpirationTime() != null) {
return task.getExpirationTime().toString();
}
return null;
case 6:
if (task.getCreatedOn() != null) {
return task.getCreatedOn().toString();
}
return null;
case 7:
if (task.getCreatedBy() != null) {
return task.getCreatedBy().toString();
}
return null;
case 8:
if (task.getActivationTime() != null) {
return task.getActivationTime().toString();
}
return null;
case 9:
if (task.getActualOwner() != null) {
return task.getActualOwner().toString();
}
return null;
case 10:
if (task.getId() != -1) {
return Long.toString(task.getId());
}
return null;
default:
break;
}
return null;
}
public IDSLMetadataService getDSLMetadataService() {
return dslMetadataService;
}
public IUser getUser() {
return user;
}
public boolean idsMatch(DerivedCellSet other, List<Integer>coordinateSystem, int rowOrdinal) {
List<Integer> coordinates = new ArrayList<>(coordinateSystem);
coordinates.set(1, rowOrdinal);
Object value = null;
Object otherValue = null;
for(DerivedPosition column: getAxes().get(0).getPositions()) { // search the columns
coordinates.set(0, column.getOrdinal());
if(getCell(coordinates).isId()) {
value = getCell(coordinates).getValue();
}
}
for(DerivedPosition column: other.getAxes().get(0).getPositions()) { // search the columns
coordinates.set(0, column.getOrdinal());
if(other.getCell(coordinates).isId()) {
otherValue = other.getCell(coordinates).getValue();
}
}
if (value == null && otherValue == null) {
return true; // there are no ids in table
}
return (value != null && otherValue != null && value.equals(otherValue));
}
}