/*
 * ====================================================================
 * This software is subject to the terms of the Common Public License
 * Agreement, available at the following URL:
 *   http://www.opensource.org/licenses/cpl.html .
 * You must accept the terms of that agreement to use this software.
 * ====================================================================
 */
package com.eyeq.pivot4j.ui.command;

import org.olap4j.CellSet;
import org.olap4j.CellSetAxis;
import org.olap4j.Position;
import org.olap4j.metadata.Hierarchy;
import org.olap4j.metadata.Member;

import com.eyeq.pivot4j.PivotModel;
import com.eyeq.pivot4j.transform.DrillReplace;
import com.eyeq.pivot4j.ui.PivotRenderer;
import com.eyeq.pivot4j.ui.RenderContext;

public class DrillUpReplaceCommand extends AbstractDrillDownCommand {

	public static final String NAME = "drillUp";

	/**
	 * @param renderer
	 */
	public DrillUpReplaceCommand(PivotRenderer renderer) {
		super(renderer);
	}

	/**
	 * @see com.eyeq.pivot4j.ui.command.CellCommand#getName()
	 */
	@Override
	public String getName() {
		return NAME;
	}

	/**
	 * @see com.eyeq.pivot4j.ui.command.AbstractCellCommand#getMode(com.eyeq.pivot4j.ui.RenderContext)
	 */
	@Override
	public String getMode(RenderContext context) {
		return MODE_REPLACE;
	}

	/**
	 * @see com.eyeq.pivot4j.ui.command.CellCommand#canExecute(com.eyeq.pivot4j.ui
	 *      .RenderContext)
	 */
	@Override
	public boolean canExecute(RenderContext context) {
		if (!super.canExecute(context)) {
			return false;
		}

		PivotModel model = context.getModel();

		DrillReplace transform = model.getTransform(DrillReplace.class);

		Hierarchy hierarchy = context.getHierarchy();
		if (hierarchy == null || context.getMember() != null) {
			return false;
		}

		return transform.canDrillUp(hierarchy);
	}

	/**
	 * @see com.eyeq.pivot4j.ui.command.CellCommand#createParameters(com.eyeq.pivot4j
	 *      .ui.RenderContext)
	 */
	@Override
	public CellParameters createParameters(RenderContext context) {
		CellParameters parameters = new CellParameters();
		parameters.setAxisOrdinal(context.getAxis().axisOrdinal());
		parameters.setPositionOrdinal(context.getPosition().getOrdinal());
		parameters.setMemberOrdinal(context.getPosition().getMembers()
				.indexOf(context.getMember()));

		return parameters;
	}

	/**
	 * @see com.eyeq.pivot4j.ui.command.CellCommand#execute(com.eyeq.pivot4j.PivotModel
	 *      , com.eyeq.pivot4j.ui.command.CellParameters)
	 */
	@Override
	public void execute(PivotModel model, CellParameters parameters) {
		CellSet cellSet = model.getCellSet();

		CellSetAxis axis = cellSet.getAxes().get(parameters.getAxisOrdinal());
		Position position = axis.getPositions().get(
				parameters.getPositionOrdinal());

		Member member = position.getMembers()
				.get(parameters.getMemberOrdinal());

		DrillReplace transform = model.getTransform(DrillReplace.class);
		transform.drillUp(member.getHierarchy());
	}
}

@@ -418,16 +418,40 @@
 mmand;%0A%0A
+import java.util.List;%0A%0A
 import o
@@ -504,36 +504,8 @@
 is;%0A
-import org.olap4j.Position;%0A
 impo
@@ -541,43 +541,8 @@
 chy;
-%0Aimport org.olap4j.metadata.Member;
 %0A%0Aim
@@ -623,16 +623,74 @@
 eplace;%0A
+import com.eyeq.pivot4j.transform.PlaceHierarchiesOnAxes;%0A
 import c
@@ -1731,39 +1731,8 @@
 y ==
- null %7C%7C context.getMember() !=
  nul
@@ -2007,93 +2007,184 @@
 %7B%0A%09%09
-CellParameters parameters = new CellParameters(
+PlaceHierarchiesOnAxes transform = context.getModel().getTransform(%0A%09%09%09%09PlaceHierarchiesOnAxes.class
 );%0A
+%0A
 %09%09
-parameters.setAxisOrdinal
+List%3CHierarchy%3E hierarchies = transform.findVisibleHierarchies
 (context
 .get
@@ -2175,24 +2175,29 @@
 hies(context
+%0A%09%09%09%09
 .getAxis().a
@@ -2198,22 +2198,61 @@
 is()
-.axisOrdinal()
+);%0A%0A%09%09CellParameters parameters = new CellParameters(
 );%0A%09
@@ -2266,24 +2266,20 @@
 ters.set
-Position
+Axis
 Ordinal(
@@ -2289,30 +2289,27 @@
 text.get
-Position().get
+Axis().axis
 Ordinal(
@@ -2332,87 +2332,70 @@
 .set
-MemberOrdinal(context.getPosition().getMembers()%0A%09%09%09%09.indexOf(context.getMember
+HierarchyOrdinal(hierarchies.indexOf(context%0A%09%09%09%09.getHierarchy
 ()))
@@ -2769,138 +2769,232 @@
 ));%0A
+%0A
 %09%09P
-osition position = axis.getPositions().get(%0A%09%09%09%09parameters.getPositionOrdinal());%0A%0A%09%09Member member = position.getMembers()%0A%09%09%09%09
+laceHierarchiesOnAxes transform = model%0A%09%09%09%09.getTransform(PlaceHierarchiesOnAxes.class);%0A%0A%09%09List%3CHierarchy%3E hierarchies = transform.findVisibleHierarchies(axis%0A%09%09%09%09.getAxisOrdinal());%0A%0A%09%09Hierarchy hierarchy = hierarchies
 .get
@@ -3012,14 +3012,17 @@
 .get
-Member
+Hierarchy
 Ordi
@@ -3037,33 +3037,38 @@
 %0A%09%09DrillReplace 
-t
+drillT
 ransform = model
@@ -3104,17 +3104,22 @@
 ass);%0A%09%09
-t
+drillT
 ransform
@@ -3131,27 +3131,17 @@
 lUp(
-member.getH
+h
 ierarchy
 ());
@@ -3136,18 +3136,16 @@
 ierarchy
-()
 );%0A%09%7D%0A%7D%0A

Make drill replace command executable without member/position parameters