Skip to content

Commit

Permalink
Fix Bug 2850060 (QueryModel needs to perform selections on levels)
Browse files Browse the repository at this point in the history
git-svn-id: https://olap4j.svn.sourceforge.net/svnroot/olap4j/trunk@400 c6a108a4-781c-0410-a6c6-c2d559e19af0
  • Loading branch information
pstoellberger committed Feb 3, 2011
1 parent 99a5541 commit 633ca0a
Show file tree
Hide file tree
Showing 7 changed files with 300 additions and 7 deletions.
183 changes: 183 additions & 0 deletions src/org/olap4j/query/LevelSelectionImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/*
// $Id: LevelSelectionImpl.java 399 2011-02-03 20:53:50Z pstoellberger $
// This software is subject to the terms of the Eclipse Public License v1.0
// Agreement, available at the following URL:
// http://www.eclipse.org/legal/epl-v10.html.
// Copyright (C) 2007-2010 Julian Hyde
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
*/
package org.olap4j.query;

import java.util.ArrayList;
import java.util.List;

import org.olap4j.metadata.Dimension;
import org.olap4j.metadata.Level;
import org.olap4j.metadata.Member;

/**
* Abstract implementation of {@link Selection}.
*
* @author pstoellberger
* @version $Id: LevelSelectionImpl.java 399 2011-02-03 20:53:50Z pstoellberger $
* @since Feb 3, 2011
*/
class LevelSelectionImpl extends QueryNodeImpl implements Selection {

protected Level level;
protected String dimensionName;
protected String hierarchyName;
protected String levelName;
protected Dimension dimension;
protected Operator operator = Operator.MEMBER;
protected List<Selection> selectionContext;

/**
* Creates a SelectionImpl.
*
* @pre operator != null
*/
public LevelSelectionImpl(
Level level,
Dimension dimension,
String hierarchyName,
String levelName,
Operator operator)
{
super();
this.level = level;
this.dimension = dimension;
this.hierarchyName = hierarchyName;
this.levelName = levelName;
this.operator = operator;
}

public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((level == null) ? 0 : level.getUniqueName().hashCode());
result = prime * result
+ ((operator == null) ? 0 : operator.hashCode());
result = prime * result
+ ((selectionContext == null) ? 0 : selectionContext.hashCode());
return result;
}

public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof LevelSelectionImpl)) {
return false;
}
LevelSelectionImpl other = (LevelSelectionImpl) obj;
if (level == null) {
if (other.level != null) {
return false;
}
} else if (!level.getUniqueName().equals(
other.level.getUniqueName()))
{
return false;
}
if (operator == null) {
if (other.operator != null) {
return false;
}
} else if (!operator.equals(other.operator)) {
return false;
}
if (selectionContext == null) {
if (other.selectionContext != null) {
return false;
}
} else if (!selectionContext.equals(other.selectionContext)) {
return false;
}
return true;
}

public String getName() {
return levelName;
}

public void setName(String name) {
levelName = name;
}

public Dimension getDimension() {
return dimension;
}

public void setDimension(Dimension dimension) {
this.dimension = dimension;
}

public Level getLevel() {
return level;
}

public String getDimensionName() {
return dimensionName;
}

public void setDimensionName(String dimensionName) {
this.dimensionName = dimensionName;
}

public String getHierarchyName() {
return hierarchyName;
}

public void setHierarchyName(String hierarchyName) {
this.hierarchyName = hierarchyName;
}

public String getLevelName() {
return levelName;
}

public void setLevelName(String levelName) {
this.levelName = levelName;
}

public Operator getOperator() {
return operator;
}

public void setOperator(Operator operator) {
assert operator != null;
this.operator = operator;
notifyChange(this,-1);
}

void tearDown() {
}

public List<Selection> getSelectionContext() {
return selectionContext;
}

public void addContext(Selection selection) {
if (selectionContext == null) {
selectionContext = new ArrayList<Selection>();
}
selectionContext.add(selection);
}

public void removeContext(Selection selection) {
selectionContext.remove(selection);
}

public Member getMember() {
// there is no member in this type of Selections
return null;
}
}

// End LevelSelectionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @version $Id$
* @since May 30, 2007
*/
class SelectionImpl extends QueryNodeImpl implements Selection {
class MemberSelectionImpl extends QueryNodeImpl implements Selection {

protected Member member;
protected String dimensionName;
Expand All @@ -38,7 +38,7 @@ class SelectionImpl extends QueryNodeImpl implements Selection {
*
* @pre operator != null
*/
public SelectionImpl(
public MemberSelectionImpl(
Member member,
Dimension dimension,
String hierarchyName,
Expand Down Expand Up @@ -74,10 +74,10 @@ public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof SelectionImpl)) {
if (!(obj instanceof MemberSelectionImpl)) {
return false;
}
SelectionImpl other = (SelectionImpl) obj;
MemberSelectionImpl other = (MemberSelectionImpl) obj;
if (member == null) {
if (other.member != null) {
return false;
Expand Down Expand Up @@ -177,4 +177,4 @@ public void removeContext(Selection selection) {
}
}

// End SelectionImpl.java
// End MemberSelectionImpl.java
35 changes: 34 additions & 1 deletion src/org/olap4j/query/Olap4jNodeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import org.olap4j.mdx.CubeNode;
import org.olap4j.mdx.DimensionNode;
import org.olap4j.mdx.IdentifierNode;
import org.olap4j.mdx.LevelNode;
import org.olap4j.mdx.LiteralNode;
import org.olap4j.mdx.MemberNode;
import org.olap4j.mdx.ParseTreeNode;
import org.olap4j.mdx.SelectNode;
import org.olap4j.mdx.Syntax;
import org.olap4j.metadata.Level;
import org.olap4j.metadata.Member;

/**
Expand Down Expand Up @@ -405,7 +407,14 @@ private static List<ParseTreeNode> toOlap4j(QueryDimension dimension) {

private static ParseTreeNode toOlap4j(Selection selection) {
try {
return toOlap4j(selection.getMember(), selection.getOperator());
if (selection instanceof MemberSelectionImpl) {
return toOlap4j(selection.getMember(), selection.getOperator());
}
if (selection instanceof LevelSelectionImpl) {
return toOlap4j(
((LevelSelectionImpl) selection).getLevel(),
selection.getOperator());
}
} catch (Exception e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -469,6 +478,30 @@ private static ParseTreeNode toOlap4j(
return node;
}

private static ParseTreeNode toOlap4j(
Level level,
Selection.Operator oper)
{
ParseTreeNode node = null;
try {
switch (oper) {
case MEMBERS:
node =
new CallNode(
null,
"Members",
Syntax.Property,
new LevelNode(null, level));
break;
default:
System.out.println("NOT IMPLEMENTED: " + oper);
}
} catch (Exception e) {
e.printStackTrace();
}
return node;
}

private static List<AxisNode> toOlap4j(List<QueryAxis> axes) {
final ArrayList<AxisNode> axisList = new ArrayList<AxisNode>();
for (QueryAxis axis : axes) {
Expand Down
43 changes: 43 additions & 0 deletions src/org/olap4j/query/QueryDimension.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ public Selection createSelection(Member member) {
return createSelection(Selection.Operator.MEMBER, member);
}

/**
* Selects a level and includes it in the query.
* <p>This method selects and includes a all members of the given
* query using the {@link Selection.Operator#MEMBERS} selection operator.
* @param member The member to select and include in the query.
*/
public void include(Level level) {
if (level.getDimension().equals(this.dimension)) {
Selection selection =
query.getSelectionFactory().createLevelSelection(level);
this.include(selection);
}
}
/**
* Selects members and includes them in the query.
* <p>This method selects and includes a member along with it's
Expand All @@ -167,6 +180,22 @@ public Selection createSelection(
return null;
}

/**
* Selects level and includes all members in the query.
* <p>This method selects and includes all members of a
* given Level, using the MEMBERS operator {@link Selection.Operator}
* @param member Root member to select and include.
*/
public Selection createSelection(Level level)
{
if (level.getDimension().equals(this.dimension)) {
Selection selection =
query.getSelectionFactory().createLevelSelection(level);
return selection;
}
return null;
}

/**
* Selects members and includes them in the query.
* <p>This method selects and includes a member along with it's
Expand Down Expand Up @@ -260,6 +289,20 @@ public void exclude(
rootMember);
}

/**
* Selects level members and excludes them from the query.
* <p>This method selects and excludes members of a level with the
* {@link Selection.Operator#MEMBERS} selection operator.
* @param level The level to select and exclude from the query.
*/
public void exclude(Level level) {
if (level.getDimension().equals(this.dimension)) {
Selection selection =
query.getSelectionFactory().createLevelSelection(level);
this.exclude(selection);
}
}

/**
* Selects members and excludes them from the query.
* <p>This method selects and excludes a single member with the
Expand Down
4 changes: 4 additions & 0 deletions src/org/olap4j/query/Selection.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public enum Operator {
* Only the root member will be selected.
*/
MEMBER,
/**
* All members of Level will be selected (LevelSelection only)
*/
MEMBERS,
/**
* Only the children of the root member will be selected.
* This excludes the root member itself.
Expand Down
15 changes: 14 additions & 1 deletion src/org/olap4j/query/SelectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package org.olap4j.query;

import org.olap4j.metadata.Level;
import org.olap4j.metadata.Member;

/**
Expand All @@ -27,14 +28,26 @@ Selection createMemberSelection(
Selection.Operator operator)
{
return
new SelectionImpl(
new MemberSelectionImpl(
member,
member.getDimension(),
member.getHierarchy().getUniqueName(),
member.getLevel().getUniqueName(),
member.getUniqueName(),
operator);
}

Selection createLevelSelection(
Level level)
{
return
new LevelSelectionImpl(
level,
level.getDimension(),
level.getHierarchy().getUniqueName(),
level.getUniqueName(),
Selection.Operator.MEMBERS);
}
}

// End SelectionFactory.java
Loading

0 comments on commit 633ca0a

Please sign in to comment.