Search in sources :

Example 26 with Hierarchy

use of mondrian.olap.Hierarchy in project pentaho-platform by pentaho.

the class MDXMetaData method createColumnNames.

/**
 * Flattens the row headers into column names (where the useful columns have useful names and the unuseful columns
 * have unusful names).
 *
 * @return the row headers in a String array
 */
protected String[] createColumnNames() {
    String[] colNames = null;
    if (nativeResultSet != null) {
        // HACK for BISERVER-2640; need backward compatibility to old format of column
        // names, yet with the old format cross joins will have problems (BISERVER-1266).
        Axis[] axes = nativeResultSet.getAxes();
        // Another IndexOOB Fix
        if ((axes.length <= MDXMetaData.AXIS_ROW) || (axes[MDXMetaData.AXIS_ROW] == null)) {
            // no rows...
            return new String[0];
        }
        List positions = axes[MDXMetaData.AXIS_ROW].getPositions();
        if (useExtendedColumnNames) {
            if ((this.rowHeaders.length > 0) && (positions != null) && (positions.size() > 0)) {
                colNames = new String[this.rowHeaders[0].length];
                // Flatten out the column headers into one column-name
                for (int i = 0; i < colNames.length; ++i) {
                    Member member = (Member) ((List) positions.get(0)).get(i);
                    colNames[i] = "[" + member.getDimension().getName() + "].[" + member.getHierarchy().getName() + "].[" + member.getLevel().getName() + "]";
                }
            } else {
                colNames = new String[0];
            }
        } else {
            if ((positions != null) && (positions.size() > 0)) {
                colNames = new String[getColumnCount()];
                // Flatten out the column headers into one column-name
                for (int i = 0; i < colNames.length; ++i) {
                    if (i < ((List) positions.get(0)).size()) {
                        Member member = (Member) ((List) positions.get(0)).get(i);
                        Hierarchy hierarchy = member.getHierarchy();
                        colNames[i] = hierarchy.getCaption();
                    } else {
                        colNames[i] = ((Member) ((List) positions.get(0)).get(((List) positions.get(0)).size() - 1)).getHierarchy().getName() + "{" + i + // $NON-NLS-1$ //$NON-NLS-2$
                        "}";
                    }
                }
            } else {
                colNames = new String[0];
            }
        }
    }
    return colNames;
}
Also used : Hierarchy(mondrian.olap.Hierarchy) List(java.util.List) Member(mondrian.olap.Member) Axis(mondrian.olap.Axis)

Example 27 with Hierarchy

use of mondrian.olap.Hierarchy in project pentaho-platform by pentaho.

the class MondrianModelComponent method getInitialQuery.

public static String getInitialQuery(final Connection connection, final String cubeName) throws Throwable {
    String measuresMdx = null;
    String columnsMdx = null;
    // $NON-NLS-1$
    String whereMdx = "";
    StringBuffer rowsMdx = new StringBuffer();
    // Get catalog info, if exists
    String catalog = connection.getCatalogName();
    MondrianCatalogComplementInfo catalogComplementInfo = MondrianCatalogHelper.getInstance().getCatalogComplementInfoMap(catalog);
    try {
        Schema schema = connection.getSchema();
        if (schema == null) {
            Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0002_INVALID_SCHEMA", // $NON-NLS-1$ //$NON-NLS-2$
            connection.getConnectString()));
            return null;
        }
        Cube[] cubes = schema.getCubes();
        if ((cubes == null) || (cubes.length == 0)) {
            Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0003_NO_CUBES", // $NON-NLS-1$ //$NON-NLS-2$
            connection.getConnectString()));
            return null;
        }
        if ((cubes.length > 1) && (cubeName == null)) {
            Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0004_CUBE_NOT_SPECIFIED", // $NON-NLS-1$ //$NON-NLS-2$
            connection.getConnectString()));
            return null;
        }
        Cube cube = null;
        if (cubes.length == 1) {
            cube = cubes[0];
        } else {
            for (Cube element : cubes) {
                if (element.getName().equals(cubeName)) {
                    cube = element;
                    break;
                }
            }
        }
        if (cube == null) {
            Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0005_CUBE_NOT_FOUND", cubeName, // $NON-NLS-1$ //$NON-NLS-2$
            connection.getConnectString()));
            return null;
        }
        // If we have any whereConditions block, we need to find which hierarchies they are in
        // and not include them in the rows
        HashSet<Hierarchy> whereHierarchies = new HashSet<Hierarchy>();
        if (catalogComplementInfo != null && catalogComplementInfo.getWhereCondition(cube.getName()) != null && !catalogComplementInfo.getWhereCondition(cube.getName()).equals("")) {
            // $NON-NLS-1$
            final String rawString = catalogComplementInfo.getWhereCondition(cube.getName());
            try {
                // According to Julian, the better way to resolve the names is to build a query
                final String queryStr = // $NON-NLS-1$ //$NON-NLS-2$
                "select " + rawString + " on columns, {} on rows from " + cube.getName();
                final Query query = connection.parseQuery(queryStr);
                final Hierarchy[] hierarchies = query.getMdxHierarchiesOnAxis(AxisOrdinal.StandardAxisOrdinal.COLUMNS);
                boolean isWhereValid = true;
                for (int i = 0; i < hierarchies.length && isWhereValid; i++) {
                    final Hierarchy hierarchy = hierarchies[i];
                    if (connection.getRole().canAccess(hierarchy)) {
                        whereHierarchies.add(hierarchy);
                    } else {
                        isWhereValid = false;
                        whereHierarchies.clear();
                    }
                }
                if (isWhereValid) {
                    // $NON-NLS-1$
                    whereMdx = " WHERE " + rawString;
                }
            } catch (Exception e) {
                // We found an error in the where slicer, so we'll just act like it wasn't here
                whereHierarchies.clear();
            }
        }
        Dimension[] dimensions = cube.getDimensions();
        if ((dimensions == null) || (dimensions.length == 0)) {
            Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0006_NO_DIMENSIONS", cubeName, // $NON-NLS-1$ //$NON-NLS-2$
            connection.getConnectString()));
            return null;
        }
        for (Dimension element : dimensions) {
            final Hierarchy hierarchy = element.getHierarchy();
            if (hierarchy == null) {
                Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0007_NO_HIERARCHIES", element.getName(), cubeName, // $NON-NLS-1$ //$NON-NLS-2$
                connection.getConnectString()));
                return null;
            }
            if (!connection.getRole().canAccess(hierarchy)) {
                // We can't access this element
                continue;
            }
            if (whereHierarchies.contains(hierarchy)) {
                // We have it on the where condition - skip it
                continue;
            }
            Member member = Locus.execute((RolapConnection) connection, "Retrieving default members in plugin", new Locus.Action<Member>() {

                public Member execute() {
                    return connection.getSchemaReader().getHierarchyDefaultMember(hierarchy);
                }
            });
            if (member == null) {
                Logger.error("MondrianModelComponent", Messages.getInstance().getErrorString("MondrianModel.ERROR_0008_NO_DEFAULT_MEMBER", element.getName(), cubeName, // $NON-NLS-1$ //$NON-NLS-2$
                connection.getConnectString()));
                return null;
            }
            if (element.isMeasures()) {
                // measuresMdx = "with member "+ member.getUniqueName();
                // //$NON-NLS-1$
                // $NON-NLS-1$
                measuresMdx = "";
                // $NON-NLS-1$ //$NON-NLS-2$
                columnsMdx = " select NON EMPTY {" + member.getUniqueName() + "} ON columns, ";
            } else {
                if (rowsMdx.length() > 0) {
                    // $NON-NLS-1$
                    rowsMdx.append(", ");
                }
                rowsMdx.append(member.getUniqueName());
            }
        }
        if ((measuresMdx != null) && (columnsMdx != null) && (rowsMdx.length() > 0)) {
            StringBuffer result = new StringBuffer(measuresMdx.length() + columnsMdx.length() + rowsMdx.length() + 50);
            // $NON-NLS-1$
            result.append(measuresMdx).append(columnsMdx).append("NON EMPTY {(").append(rowsMdx).append(// $NON-NLS-1$
            ")} ON rows ").append(// $NON-NLS-1$ //$NON-NLS-2$
            "from [" + cube.getName() + "]").append(whereMdx);
            return result.toString();
        }
        return null;
    } catch (Throwable t) {
        if (t instanceof MondrianException) {
            // pull the cause out, otherwise it never gets logged
            Throwable cause = ((MondrianException) t).getCause();
            if (cause != null) {
                throw cause;
            } else {
                throw t;
            }
        } else {
            throw t;
        }
    }
}
Also used : Query(mondrian.olap.Query) Schema(mondrian.olap.Schema) Dimension(mondrian.olap.Dimension) MondrianException(mondrian.olap.MondrianException) Hierarchy(mondrian.olap.Hierarchy) Cube(mondrian.olap.Cube) MondrianCatalogComplementInfo(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalogComplementInfo) Locus(mondrian.server.Locus) MondrianException(mondrian.olap.MondrianException) Member(mondrian.olap.Member) HashSet(java.util.HashSet)

Aggregations

Hierarchy (mondrian.olap.Hierarchy)27 Level (mondrian.olap.Level)9 Member (mondrian.olap.Member)8 Role (mondrian.olap.Role)8 HierarchyAccess (mondrian.olap.Role.HierarchyAccess)7 MultiCardinalityDefaultMember (mondrian.rolap.RestrictedMemberReader.MultiCardinalityDefaultMember)6 MondrianDef (mondrian.olap.MondrianDef)5 DataSource (javax.sql.DataSource)4 Dimension (mondrian.olap.Dimension)4 mondrian.olap (mondrian.olap)3 Schema (mondrian.olap.Schema)3 RolapCube (mondrian.rolap.RolapCube)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 Axis (mondrian.olap.Axis)2 RelationOrJoin (mondrian.olap.MondrianDef.RelationOrJoin)2 LimitedRollupMember (mondrian.rolap.RolapHierarchy.LimitedRollupMember)2 Level (org.apache.log4j.Level)2