Skip to content

Instantly share code, notes, and snippets.

@jasonwjones
Last active October 14, 2019 11:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonwjones/d62c88c729250628274bc64886950813 to your computer and use it in GitHub Desktop.
Save jasonwjones/d62c88c729250628274bc64886950813 to your computer and use it in GitHub Desktop.
Forcing Essbase to run an app-level calc script with the Java API
package com.appliedolap.test.calclist;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.essbase.api.base.EssException;
import com.essbase.api.datasource.IEssCube;
import com.essbase.api.datasource.IEssOlapServer;
import com.essbase.api.session.IEssbase;
import com.saxifrages.essbase.primitives.CubeTarget;
/**
* Utility class for performing setup/teardown on an Essbase connection and
* executing a custom operation.
*
* @author jasonwjones
*
*/
public class CubeOpWrapper {
private static final Logger logger = LoggerFactory.getLogger(CubeOpWrapper.class);
public interface CubeOpDelegate<E> {
public E run(IEssCube cube) throws EssException;
}
public static <E> E execute(CubeTarget cubeTarget, CubeOpDelegate<E> delegate) throws EssException {
// System.setProperty("ESS_ES_HOME", "test");
IEssbase essbase = null;
IEssOlapServer olapServer = null;
try {
essbase = IEssbase.Home.create(IEssbase.JAPI_VERSION);
//olapServer = essbase.signOn(cubeTarget.getUsername(), cubeTarget.getPassword(), false, null, "http://epmvirt11124:9000/aps/JAPI", cubeTarget.getServer());
olapServer = essbase.signOn(cubeTarget.getUsername(), cubeTarget.getPassword(), false, null, "embedded", cubeTarget.getServer());
IEssCube cube = olapServer.getApplication(cubeTarget.getApp()).getCube(cubeTarget.getCube());
return delegate.run(cube);
} finally {
try {
logger.info("Signing off Essbase");
if (olapServer != null)
olapServer.disconnect();
essbase.signOff();
} catch (EssException e) {
logger.error("Error signing off Essbase: {}", e.getMessage());
}
}
}
}
package com.appliedolap.test.calclist;
import com.essbase.api.base.EssException;
import com.essbase.api.datasource.EssCube;
import com.essbase.api.datasource.EssOlapApplication;
/**
* Overriding implementation of EssCube/IEssCube that allows us to intercept
* certain calls to an IEssCube.
*
* @author jasonwjones
*
*/
public class EssCube2 extends EssCube {
private boolean useAlternateName = false;
private String fakeName;
public EssCube2(String arg0, int arg1, EssOlapApplication arg2) throws EssException {
super(arg0, arg1, arg2);
}
@Override
public void calcFileWithRunTimeSubVarFile(boolean arg0, String arg1, String arg2) throws EssException {
super.calcFileWithRunTimeSubVarFile(arg0, arg1, arg2);
}
@Override
public String getName() throws EssException {
System.out.println("Asking for name");
if (useAlternateName) {
return fakeName;
} else {
return super.getName();
}
}
public void setAlternateName(String fakeName) {
this.useAlternateName = true;
this.fakeName = fakeName;
}
@Override
public void setActive() throws EssException {
System.out.println("Setting active");
super.setActive();
setAlternateName("");
}
}
package com.appliedolap.test.calclist;
import com.appliedolap.test.calclist.CubeOpWrapper.CubeOpDelegate;
import com.essbase.api.base.EssException;
import com.essbase.api.datasource.EssOlapApplication;
import com.essbase.api.datasource.IEssCube;
import com.saxifrages.essbase.TargetUtils;
import com.saxifrages.essbase.primitives.CubeTarget;
/**
* Use the EssCube2 fake class to call a calc on the App.
*
* @author jasonwjones
*
*/
public class TestCalc {
public static void main(String[] args) throws Exception {
CubeTarget cube = TargetUtils.loadDefault();
CubeOpWrapper.execute(cube, new CubeOpDelegate<Boolean>() {
public Boolean run(IEssCube cube) throws EssException {
// Call our fake cube wrapper
EssCube2 cube2 = new EssCube2("Basic", 4, (EssOlapApplication) cube.getApplication());
// Run an app level calc
cube2.calcFileWithRunTimeSubVarFile(false, "AppCalcA", null);
System.out.println("Calculated");
return Boolean.TRUE;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment