use of mondrian.olap.Hierarchy in project mondrian by pentaho.
the class MondrianOlap4jSchema method getSharedDimensions.
public NamedList<Dimension> getSharedDimensions() throws OlapException {
final MondrianOlap4jConnection olap4jConnection = olap4jCatalog.olap4jDatabaseMetaData.olap4jConnection;
final SortedSet<MondrianOlap4jDimension> dimensions = new TreeSet<MondrianOlap4jDimension>(new Comparator<MondrianOlap4jDimension>() {
public int compare(MondrianOlap4jDimension o1, MondrianOlap4jDimension o2) {
return o1.getName().compareTo(o2.getName());
}
});
final Role role = olap4jConnection.getMondrianConnection().getRole();
for (Hierarchy hierarchy : schema.getSharedHierarchies()) {
if (role.canAccess(hierarchy)) {
dimensions.add(olap4jConnection.toOlap4j(hierarchy.getDimension()));
}
}
NamedList<MondrianOlap4jDimension> list = new NamedListImpl<MondrianOlap4jDimension>();
list.addAll(dimensions);
return Olap4jUtil.cast(list);
}
use of mondrian.olap.Hierarchy in project mondrian by pentaho.
the class SqlConstraintUtils method hasMultiPositionSlicer.
public static boolean hasMultiPositionSlicer(List<Member> slicerMembers) {
Map<Hierarchy, Member> mapOfSlicerMembers = new HashMap<Hierarchy, Member>();
for (Member slicerMember : slicerMembers) {
Hierarchy hierarchy = slicerMember.getHierarchy();
if (mapOfSlicerMembers.containsKey(hierarchy)) {
// We have found a second member in this hierarchy
return true;
}
mapOfSlicerMembers.put(hierarchy, slicerMember);
}
return false;
}
use of mondrian.olap.Hierarchy in project mondrian by pentaho.
the class SqlConstraintUtils method expandMultiPositionSlicerMembers.
protected static Member[] expandMultiPositionSlicerMembers(Member[] members, Evaluator evaluator) {
List<Member> slicerMembers = null;
if (evaluator instanceof RolapEvaluator) {
// get the slicer members from the evaluator
slicerMembers = ((RolapEvaluator) evaluator).getSlicerMembers();
}
if (slicerMembers != null) {
// Iterate the list of slicer members, grouping them by hierarchy
Map<Hierarchy, Set<Member>> mapOfSlicerMembers = new HashMap<Hierarchy, Set<Member>>();
for (Member slicerMember : slicerMembers) {
Hierarchy hierarchy = slicerMember.getHierarchy();
if (!mapOfSlicerMembers.containsKey(hierarchy)) {
mapOfSlicerMembers.put(hierarchy, new HashSet<Member>());
}
mapOfSlicerMembers.get(hierarchy).add(slicerMember);
}
List<Member> listOfMembers = new ArrayList<Member>();
// has multiple members on the slicer axis
for (Member member : members) {
Hierarchy hierarchy = member.getHierarchy();
if (!mapOfSlicerMembers.containsKey(hierarchy) || mapOfSlicerMembers.get(hierarchy).size() < 2 || member instanceof RolapResult.CompoundSlicerRolapMember) {
listOfMembers.add(member);
} else {
listOfMembers.addAll(mapOfSlicerMembers.get(hierarchy));
}
}
members = listOfMembers.toArray(new Member[listOfMembers.size()]);
}
return members;
}
use of mondrian.olap.Hierarchy in project mondrian by pentaho.
the class SchemaTest method testCaptionDescriptionAndAnnotation.
/**
* Test for descriptions, captions and annotations of various schema
* elements.
*/
public void testCaptionDescriptionAndAnnotation() {
final String schemaName = "Description schema";
final String salesCubeName = "DescSales";
final String virtualCubeName = "DescWarehouseAndSales";
final String warehouseCubeName = "Warehouse";
final TestContext testContext = TestContext.instance().withSchema("<Schema name=\"" + schemaName + "\"\n" + " description=\"Schema to test descriptions and captions\">\n" + " <Annotations>\n" + " <Annotation name=\"a\">Schema</Annotation>\n" + " <Annotation name=\"b\">Xyz</Annotation>\n" + " </Annotations>\n" + " <Dimension name=\"Time\" type=\"TimeDimension\"\n" + " caption=\"Time shared caption\"\n" + " description=\"Time shared description\">\n" + " <Annotations><Annotation name=\"a\">Time shared</Annotation></Annotations>\n" + " <Hierarchy hasAll=\"false\" primaryKey=\"time_id\"\n" + " caption=\"Time shared hierarchy caption\"\n" + " description=\"Time shared hierarchy description\">\n" + " <Table name=\"time_by_day\"/>\n" + " <Level name=\"Year\" column=\"the_year\" type=\"Numeric\" uniqueMembers=\"true\"\n" + " levelType=\"TimeYears\"/>\n" + " <Level name=\"Quarter\" column=\"quarter\" uniqueMembers=\"false\"\n" + " levelType=\"TimeQuarters\"/>\n" + " <Level name=\"Month\" column=\"month_of_year\" uniqueMembers=\"false\" type=\"Numeric\"\n" + " levelType=\"TimeMonths\"/>\n" + " </Hierarchy>\n" + " </Dimension>\n" + " <Dimension name=\"Warehouse\">\n" + " <Hierarchy hasAll=\"true\" primaryKey=\"warehouse_id\">\n" + " <Table name=\"warehouse\"/>\n" + " <Level name=\"Country\" column=\"warehouse_country\" uniqueMembers=\"true\"/>\n" + " <Level name=\"State Province\" column=\"warehouse_state_province\"\n" + " uniqueMembers=\"true\"/>\n" + " <Level name=\"City\" column=\"warehouse_city\" uniqueMembers=\"false\"/>\n" + " <Level name=\"Warehouse Name\" column=\"warehouse_name\" uniqueMembers=\"true\"/>\n" + " </Hierarchy>\n" + " </Dimension>\n" + " <Cube name=\"" + salesCubeName + "\"\n" + " description=\"Cube description\">\n" + " <Annotations><Annotation name=\"a\">Cube</Annotation></Annotations>\n" + " <Table name=\"sales_fact_1997\"/>\n" + " <Dimension name=\"Store\" foreignKey=\"store_id\"\n" + " caption=\"Dimension caption\"\n" + " description=\"Dimension description\">\n" + " <Annotations><Annotation name=\"a\">Dimension</Annotation></Annotations>\n" + " <Hierarchy hasAll=\"true\" primaryKeyTable=\"store\" primaryKey=\"store_id\"\n" + " caption=\"Hierarchy caption\"\n" + " description=\"Hierarchy description\">\n" + " <Annotations><Annotation name=\"a\">Hierarchy</Annotation></Annotations>\n" + " <Join leftKey=\"region_id\" rightKey=\"region_id\">\n" + " <Table name=\"store\"/>\n" + " <Join leftKey=\"sales_district_id\" rightKey=\"promotion_id\">\n" + " <Table name=\"region\"/>\n" + " <Table name=\"promotion\"/>\n" + " </Join>\n" + " </Join>\n" + " <Level name=\"Store Country\" table=\"store\" column=\"store_country\"\n" + " description=\"Level description\"" + " caption=\"Level caption\">\n" + " <Annotations><Annotation name=\"a\">Level</Annotation></Annotations>\n" + " </Level>\n" + " <Level name=\"Store Region\" table=\"region\" column=\"sales_region\" />\n" + " <Level name=\"Store Name\" table=\"store\" column=\"store_name\" />\n" + " </Hierarchy>\n" + " </Dimension>\n" + " <DimensionUsage name=\"Time1\"\n" + " caption=\"Time usage caption\"\n" + " description=\"Time usage description\"\n" + " source=\"Time\" foreignKey=\"time_id\">\n" + " <Annotations><Annotation name=\"a\">Time usage</Annotation></Annotations>\n" + " </DimensionUsage>\n" + " <DimensionUsage name=\"Time2\"\n" + " source=\"Time\" foreignKey=\"time_id\"/>\n" + "<Measure name=\"Unit Sales\" column=\"unit_sales\"\n" + " aggregator=\"sum\" formatString=\"Standard\"\n" + " caption=\"Measure caption\"\n" + " description=\"Measure description\">\n" + " <Annotations><Annotation name=\"a\">Measure</Annotation></Annotations>\n" + "</Measure>\n" + "<CalculatedMember name=\"Foo\" dimension=\"Measures\" \n" + " caption=\"Calc member caption\"\n" + " description=\"Calc member description\">\n" + " <Annotations><Annotation name=\"a\">Calc member</Annotation></Annotations>\n" + " <Formula>[Measures].[Unit Sales] + 1</Formula>\n" + " <CalculatedMemberProperty name=\"FORMAT_STRING\" value=\"$#,##0.00\"/>\n" + " </CalculatedMember>\n" + " <NamedSet name=\"Top Periods\"\n" + " caption=\"Named set caption\"\n" + " description=\"Named set description\">\n" + " <Annotations><Annotation name=\"a\">Named set</Annotation></Annotations>\n" + " <Formula>TopCount([Time1].MEMBERS, 5, [Measures].[Foo])</Formula>\n" + " </NamedSet>\n" + "</Cube>\n" + "<Cube name=\"" + warehouseCubeName + "\">\n" + " <Table name=\"inventory_fact_1997\"/>\n" + "\n" + " <DimensionUsage name=\"Time\" source=\"Time\" foreignKey=\"time_id\"/>\n" + " <DimensionUsage name=\"Warehouse\" source=\"Warehouse\" foreignKey=\"warehouse_id\"/>\n" + "\n" + " <Measure name=\"Units Shipped\" column=\"units_shipped\" aggregator=\"sum\" formatString=\"#.0\"/>\n" + "</Cube>\n" + "<VirtualCube name=\"" + virtualCubeName + "\"\n" + " caption=\"Virtual cube caption\"\n" + " description=\"Virtual cube description\">\n" + " <Annotations><Annotation name=\"a\">Virtual cube</Annotation></Annotations>\n" + " <VirtualCubeDimension name=\"Time\"/>\n" + " <VirtualCubeDimension cubeName=\"" + warehouseCubeName + "\" name=\"Warehouse\"/>\n" + " <VirtualCubeMeasure cubeName=\"" + salesCubeName + "\" name=\"[Measures].[Unit Sales]\">\n" + " <Annotations><Annotation name=\"a\">Virtual cube measure</Annotation></Annotations>\n" + " </VirtualCubeMeasure>\n" + " <VirtualCubeMeasure cubeName=\"" + warehouseCubeName + "\" name=\"[Measures].[Units Shipped]\"/>\n" + " <CalculatedMember name=\"Profit Per Unit Shipped\" dimension=\"Measures\">\n" + " <Formula>1 / [Measures].[Units Shipped]</Formula>\n" + " </CalculatedMember>\n" + "</VirtualCube>" + "</Schema>");
final Result result = testContext.executeQuery("select from [" + salesCubeName + "]");
final Cube cube = result.getQuery().getCube();
assertEquals("Cube description", cube.getDescription());
checkAnnotations(cube.getAnnotationMap(), "a", "Cube");
final Schema schema = cube.getSchema();
checkAnnotations(schema.getAnnotationMap(), "a", "Schema", "b", "Xyz");
final Dimension dimension = cube.getDimensions()[1];
assertEquals("Dimension description", dimension.getDescription());
assertEquals("Dimension caption", dimension.getCaption());
checkAnnotations(dimension.getAnnotationMap(), "a", "Dimension");
final Hierarchy hierarchy = dimension.getHierarchies()[0];
assertEquals("Hierarchy description", hierarchy.getDescription());
assertEquals("Hierarchy caption", hierarchy.getCaption());
checkAnnotations(hierarchy.getAnnotationMap(), "a", "Hierarchy");
final mondrian.olap.Level level = hierarchy.getLevels()[1];
assertEquals("Level description", level.getDescription());
assertEquals("Level caption", level.getCaption());
checkAnnotations(level.getAnnotationMap(), "a", "Level");
// Caption comes from the CAPTION member property, defaults to name.
// Description comes from the DESCRIPTION member property.
// Annotations are always empty for regular members.
final List<Member> memberList = cube.getSchemaReader(null).withLocus().getLevelMembers(level, false);
final Member member = memberList.get(0);
assertEquals("Canada", member.getName());
assertEquals("Canada", member.getCaption());
assertNull(member.getDescription());
checkAnnotations(member.getAnnotationMap());
// All member. Caption defaults to name; description is null.
final Member allMember = member.getParentMember();
assertEquals("All Stores", allMember.getName());
assertEquals("All Stores", allMember.getCaption());
assertNull(allMember.getDescription());
// All level.
final mondrian.olap.Level allLevel = hierarchy.getLevels()[0];
assertEquals("(All)", allLevel.getName());
assertNull(allLevel.getDescription());
assertEquals(allLevel.getName(), allLevel.getCaption());
checkAnnotations(allLevel.getAnnotationMap());
// the first time dimension overrides the caption and description of the
// shared time dimension
final Dimension timeDimension = cube.getDimensions()[2];
assertEquals("Time1", timeDimension.getName());
assertEquals("Time usage description", timeDimension.getDescription());
assertEquals("Time usage caption", timeDimension.getCaption());
checkAnnotations(timeDimension.getAnnotationMap(), "a", "Time usage");
// Time1 is a usage of a shared dimension Time.
// Now look at the hierarchy usage within that dimension usage.
// Because the dimension usage has a name, use that as a prefix for
// name, caption and description of the hierarchy usage.
final Hierarchy timeHierarchy = timeDimension.getHierarchies()[0];
// The hierarchy in the shared dimension does not have a name, so the
// hierarchy usage inherits the name of the dimension usage, Time1.
final boolean ssasCompatibleNaming = MondrianProperties.instance().SsasCompatibleNaming.get();
if (ssasCompatibleNaming) {
assertEquals("Time", timeHierarchy.getName());
assertEquals("Time1", timeHierarchy.getDimension().getName());
} else {
assertEquals("Time1", timeHierarchy.getName());
}
// The description is prefixed by the dimension usage name.
assertEquals("Time usage caption.Time shared hierarchy description", timeHierarchy.getDescription());
// The hierarchy caption is prefixed by the caption of the dimension
// usage.
assertEquals("Time usage caption.Time shared hierarchy caption", timeHierarchy.getCaption());
// No annotations.
checkAnnotations(timeHierarchy.getAnnotationMap());
// the second time dimension does not overrides caption and description
final Dimension time2Dimension = cube.getDimensions()[3];
assertEquals("Time2", time2Dimension.getName());
assertEquals("Time shared description", time2Dimension.getDescription());
assertEquals("Time shared caption", time2Dimension.getCaption());
checkAnnotations(time2Dimension.getAnnotationMap(), "a", "Time shared");
final Hierarchy time2Hierarchy = time2Dimension.getHierarchies()[0];
// hierarchy usage inherits the name of the dimension usage, Time2.
if (ssasCompatibleNaming) {
assertEquals("Time", time2Hierarchy.getName());
assertEquals("Time2", time2Hierarchy.getDimension().getName());
} else {
assertEquals("Time2", time2Hierarchy.getName());
}
// The description is prefixed by the dimension usage name (because
// dimension usage has no caption).
assertEquals("Time2.Time shared hierarchy description", time2Hierarchy.getDescription());
// The hierarchy caption is prefixed by the dimension usage name
// (because the dimension usage has no caption.
assertEquals("Time2.Time shared hierarchy caption", time2Hierarchy.getCaption());
// No annotations.
checkAnnotations(time2Hierarchy.getAnnotationMap());
final Dimension measuresDimension = cube.getDimensions()[0];
final Hierarchy measuresHierarchy = measuresDimension.getHierarchies()[0];
final mondrian.olap.Level measuresLevel = measuresHierarchy.getLevels()[0];
final SchemaReader schemaReader = cube.getSchemaReader(null);
final List<Member> measures = schemaReader.getLevelMembers(measuresLevel, true);
final Member measure = measures.get(0);
assertEquals("Unit Sales", measure.getName());
assertEquals("Measure caption", measure.getCaption());
assertEquals("Measure description", measure.getDescription());
assertEquals(measure.getDescription(), measure.getPropertyValue(Property.DESCRIPTION.name));
assertEquals(measure.getCaption(), measure.getPropertyValue(Property.CAPTION.name));
assertEquals(measure.getCaption(), measure.getPropertyValue(Property.MEMBER_CAPTION.name));
checkAnnotations(measure.getAnnotationMap(), "a", "Measure");
// The implicitly created [Fact Count] measure
final Member factCountMeasure = measures.get(1);
assertEquals("Fact Count", factCountMeasure.getName());
assertEquals(false, factCountMeasure.getPropertyValue(Property.VISIBLE.name));
checkAnnotations(factCountMeasure.getAnnotationMap(), "Internal Use", "For internal use");
final Member calcMeasure = measures.get(2);
assertEquals("Foo", calcMeasure.getName());
assertEquals("Calc member caption", calcMeasure.getCaption());
assertEquals("Calc member description", calcMeasure.getDescription());
assertEquals(calcMeasure.getDescription(), calcMeasure.getPropertyValue(Property.DESCRIPTION.name));
assertEquals(calcMeasure.getCaption(), calcMeasure.getPropertyValue(Property.CAPTION.name));
assertEquals(calcMeasure.getCaption(), calcMeasure.getPropertyValue(Property.MEMBER_CAPTION.name));
checkAnnotations(calcMeasure.getAnnotationMap(), "a", "Calc member");
final NamedSet namedSet = cube.getNamedSets()[0];
assertEquals("Top Periods", namedSet.getName());
assertEquals("Named set caption", namedSet.getCaption());
assertEquals("Named set description", namedSet.getDescription());
checkAnnotations(namedSet.getAnnotationMap(), "a", "Named set");
final Result result2 = testContext.executeQuery("select from [" + virtualCubeName + "]");
final Cube cube2 = result2.getQuery().getCube();
assertEquals("Virtual cube description", cube2.getDescription());
checkAnnotations(cube2.getAnnotationMap(), "a", "Virtual cube");
final SchemaReader schemaReader2 = cube2.getSchemaReader(null);
final Dimension measuresDimension2 = cube2.getDimensions()[0];
final Hierarchy measuresHierarchy2 = measuresDimension2.getHierarchies()[0];
final mondrian.olap.Level measuresLevel2 = measuresHierarchy2.getLevels()[0];
final List<Member> measures2 = schemaReader2.getLevelMembers(measuresLevel2, true);
final Member measure2 = measures2.get(0);
assertEquals("Unit Sales", measure2.getName());
assertEquals("Measure caption", measure2.getCaption());
assertEquals("Measure description", measure2.getDescription());
assertEquals(measure2.getDescription(), measure2.getPropertyValue(Property.DESCRIPTION.name));
assertEquals(measure2.getCaption(), measure2.getPropertyValue(Property.CAPTION.name));
assertEquals(measure2.getCaption(), measure2.getPropertyValue(Property.MEMBER_CAPTION.name));
checkAnnotations(measure2.getAnnotationMap(), "a", "Virtual cube measure");
}
use of mondrian.olap.Hierarchy in project mondrian by pentaho.
the class SchemaTest method testLevelVisibility.
public void testLevelVisibility() throws Exception {
for (Boolean testValue : new Boolean[] { true, false }) {
String cubeDef = "<Cube name=\"Foo\">\n" + " <Table name=\"store\"/>\n" + " <Dimension name=\"Bar\">\n" + " <Hierarchy name=\"Bacon\" hasAll=\"false\">\n" + " <Level name=\"Samosa\" column=\"store_type\" uniqueMembers=\"true\" visible=\"@REPLACE_ME@\"/>\n" + " </Hierarchy>\n" + " </Dimension>\n" + " <Measure name=\"Store Sqft\" column=\"store_sqft\" aggregator=\"sum\"\n" + " formatString=\"#,###\"/>\n" + "</Cube>\n";
cubeDef = cubeDef.replace("@REPLACE_ME@", String.valueOf(testValue));
final TestContext context = TestContext.instance().create(null, cubeDef, null, null, null, null);
final Cube cube = context.getConnection().getSchema().lookupCube("Foo", true);
Dimension dim = null;
for (Dimension dimCheck : cube.getDimensions()) {
if (dimCheck.getName().equals("Bar")) {
dim = dimCheck;
}
}
assertNotNull(dim);
final Hierarchy hier = dim.getHierarchy();
assertNotNull(hier);
assertEquals(MondrianProperties.instance().SsasCompatibleNaming.get() ? "Bacon" : "Bar.Bacon", hier.getName());
final mondrian.olap.Level level = hier.getLevels()[0];
assertEquals("Samosa", level.getName());
assertTrue(testValue.equals(level.isVisible()));
}
}
Aggregations