public Scriptable getUserScriptScope(Context jsContext) { Scriptable scope = new ImporterTopLevel(jsContext); for( String name : userScriptObjects.keySet() ) { Object obj = userScriptObjects.get(name); if( obj instanceof Boolean ) { scope.put(name, scope, obj); } else if( obj != null ) { Scriptable jsArgs = Context.toObject(obj, scope); scope.put(name, scope, jsArgs); } } return scope; }
private void initJavaScript() { LogUtils.logMessage("Initialising JavaScript..."); contextFactory.call(new ContextAction() { @Override public Object run(Context cx) { ImporterTopLevel importer = new ImporterTopLevel(); importer.initStandardObjects(cx, false); systemScope = importer; Object frameworkScriptable = Context.javaToJS(Framework.this, systemScope); ScriptableObject.putProperty(systemScope, FRAMEWORK_VARIABLE, frameworkScriptable); systemScope.setAttributes(FRAMEWORK_VARIABLE, ScriptableObject.READONLY); globalScope = (ScriptableObject) cx.newObject(systemScope); globalScope.setPrototype(systemScope); globalScope.setParentScope(null); return null; } }); }
private @NonNull ScriptableObject initJsScope(@NonNull Context jsContext) { // Set the main Rhino goodies ImporterTopLevel importerTopLevel = new ImporterTopLevel(jsContext); ScriptableObject scope = jsContext.initStandardObjects(importerTopLevel, false); ScriptableObject.putProperty(scope, "context", Context.javaToJS(mContext, scope)); try { importClasses(jsContext, scope); importPackages(jsContext, scope); importConsole(scope); importVariables(scope); importFunctions(scope); } catch (StethoJsException e) { String message = String.format("%s\n%s", e.getMessage(), Log.getStackTraceString(e)); LogUtil.e(e, message); CLog.writeToConsole(Console.MessageLevel.ERROR, Console.MessageSource.JAVASCRIPT, message); } return scope; }
public void start() { LOG.debug("Start MacroRuntime {}", this); context = Context.enter(); context.setLanguageVersion(Context.VERSION_ES6); context.setWrapFactory(new SCWrapFactory()); globalScope = new ImporterTopLevel(context); // List<URI> paths; // try { // paths = Collections.singletonList(this.getClass().getResource("/macro/console.js").toURI()); // } catch (URISyntaxException e) { // throw new UnexpectedException(e); // } // ModuleSourceProvider sourceProvider = new UrlModuleSourceProvider(paths, null); // ModuleScriptProvider scriptProvider = new SoftCachingModuleScriptProvider(sourceProvider); // RequireBuilder builder = new RequireBuilder(); // builder.setModuleScriptProvider(scriptProvider); // builder.setSandboxed(false); // Require require = builder.createRequire(context, globalScope); // // require.install(globalScope); // loadScriptToGlobalVariable("console", "console"); context.evaluateString(globalScope, getInitScript(), "init.js", 1, null); started = true; }
/** * Initialize the JavaScript context using given parent scope. * * @param scPrototype * Parent scope object. If it's null, use default scope. */ public void init( Scriptable scPrototype ) throws CrosstabException { final Context cx = Context.enter( ); try { if ( scPrototype == null ) { scope = new ImporterTopLevel( cx ); } else { scope = cx.newObject( scPrototype ); scope.setPrototype( scPrototype ); } } catch ( RhinoException jsx ) { throw convertException( jsx ); } finally { Context.exit( ); } }
/** * The constructor. * * @param context */ public BIRTExternalContext( IReportContext context ) { this.context = context; final Context cx = Context.enter( ); try { Scriptable scope = new ImporterTopLevel( cx ); scriptableContext = cx.getWrapFactory( ).wrapAsJavaObject( cx, scope, context, null ); } catch ( Exception e ) { logger.log( e ); } finally { Context.exit( ); } }
protected void setUp( ) throws Exception { super.setUp( ); // Create test output file // We must make sure this folder will be created successfully // before we do next job. openOutputFolder( ); openOutputFile( ); // Create top-level Javascript scope jsContext = Context.enter( ); jsScope = new ImporterTopLevel( jsContext ); // Add JS functions testPrint and testPrintln for scripts to write // to output file jsScope.put( "_testCase", jsScope, this ); jsContext .evaluateString( jsScope, "function testPrint(str) { _testCase.testPrint(str); }; " + "function testPrintln(str) { _testCase.testPrintln(str); }; ", "BaseTestCase.setUp", 1, null ); }
@Before public void mirrorCursorModelSetUp() throws Exception { this.scope = new ImporterTopLevel( ); DataEngineContext context = DataEngineContext.newInstance( DataEngineContext.DIRECT_PRESENTATION, scope, null, null ); context.setTmpdir( this.getTempDir( ) ); de = (DataEngineImpl) DataEngine.newDataEngine( context ); this.creator = new CubeUtility( ); creator.createCube( de ); creator.createCube1( de ); cube1 = creator.getCube( CubeUtility.cubeName, de ); cube2 = creator.getCube( CubeUtility.timeCube, de ); }
@Before public void baseSetUp() throws Exception { // Create test output file // We must make sure this folder will be created successfully // before we do next job. openOutputFolder(); openOutputFile(); // Create top-level Javascript scope jsContext = Context.enter( ); jsScope = new ImporterTopLevel(jsContext); scriptContext = new ScriptContext().newContext(jsScope); // Add JS functions testPrint and testPrintln for scripts to write // to output file jsScope.put("_testCase", jsScope, this ); jsContext.evaluateString( jsScope, "function testPrint(str) { _testCase.testPrint(str); }; " + "function testPrintln(str) { _testCase.testPrintln(str); }; " , "BaseTestCase.setUp", 1, null ); }
/** * Initialize the engine. * Put the manager into the context-manager * map hashtable too. */ @Override public void initialize(BSFManager mgr, String lang, @SuppressWarnings("rawtypes") // superclass does not support types Vector declaredBeans) throws BSFException { super.initialize(mgr, lang, declaredBeans); // Initialize context and global scope object try { Context cx = Context.enter(); global = new ImporterTopLevel(cx); Scriptable bsf = Context.toObject(new BSFFunctions(mgr, this), global); global.put("bsf", global, bsf); @SuppressWarnings("unchecked") // superclass does not support types final Vector<BSFDeclaredBean> beans = declaredBeans; for (BSFDeclaredBean declaredBean : beans) { declareBean(declaredBean); } } catch (Throwable t) { handleError(t); } finally { Context.exit(); } }
private void initScriptable() throws IOException, RhinoException { Context context = Context.enter(); try{ this.scriptable = new ImporterTopLevel(context); context.evaluateReader(this.getScriptable(), Files.newBufferedReader(this.getFile().toPath(), StandardCharsets.UTF_8), this.getName(), 0, null); ScriptableObject.putProperty(this.getScriptable(), "logger", this.getLogger()); ScriptableObject.putProperty(this.getScriptable(), "takoyaki", Takoyaki.getInstance()); }finally{ Context.exit(); } }
/** * The constructor. * */ public AbstractScriptHandler( ) { final Context cx = Context.enter( ); try { // scope = cx.initStandardObjects(); scope = new ImporterTopLevel( cx ); } finally { Context.exit( ); } }
/** * Initialize the JavaScript context using given parent scope. * * @param scPrototype * Parent scope object. If it's null, use default scope. */ public final void init( Scriptable scPrototype ) throws ChartException { final Context cx = Context.enter( ); try { if ( scPrototype == null ) // NO PROTOTYPE { // scope = cx.initStandardObjects(); scope = new ImporterTopLevel( cx ); } else { scope = cx.newObject( scPrototype ); scope.setPrototype( scPrototype ); // !don't reset the parent scope here. // scope.setParentScope( null ); } // final Scriptable scopePrevious = scope; // !deprecated, remove this later. use script context instead. // registerExistingScriptableObject( this, "chart" ); //$NON-NLS-1$ // scope = scopePrevious; // RESTORE // !deprecated, remove this later, use logger from script context // instead. // ADD LOGGING CAPABILITIES TO JAVASCRIPT ACCESS final Object oConsole = Context.javaToJS( getLogger( ), scope ); scope.put( "logger", scope, oConsole ); //$NON-NLS-1$ } catch ( RhinoException jsx ) { throw convertException( jsx ); } finally { Context.exit( ); } }
@Before public void dimensionFilterProcessorSetUp() { cx = new ScriptContext(); this.baseScope = new ImporterTopLevel( ); this.cubeQuery = createCubeQueryDefinition( ); }
@Before public void cursorModelSetUp() throws Exception { this.scope = new ImporterTopLevel(); DataEngineContext context = DataEngineContext.newInstance( DataEngineContext.DIRECT_PRESENTATION, scope, null, null ); context.setTmpdir( this.getTempDir( ) ); de = (DataEngineImpl) DataEngine.newDataEngine( context ); creator = new CubeUtility(); creator.createCube(de ); cube = creator.getCube( CubeUtility.cubeName, de ); }
@Before public void mirrorCursorNavigatorSetUp() throws Exception { this.scope = new ImporterTopLevel( ); DataEngineContext context = DataEngineContext.newInstance( DataEngineContext.DIRECT_PRESENTATION, scope, null, null ); context.setTmpdir( this.getTempDir( ) ); de = (DataEngineImpl) DataEngine.newDataEngine( context ); creator = new CubeUtility( ); creator.createCube( de ); cube = creator.getCube( CubeUtility.cubeName, de ); }
@Before public void cursorNavigatorSetUp() throws Exception { this.scope = new ImporterTopLevel( ); DataEngineContext context = DataEngineContext.newInstance( DataEngineContext.DIRECT_PRESENTATION, scope, null, null ); context.setTmpdir( this.getTempDir( ) ); de = (DataEngineImpl) DataEngine.newDataEngine( context ); creator = new CubeUtility( ); creator.createCube( de ); cube = creator.getCube( CubeUtility.cubeName, de ); }
@Before public void dateTimeCursorSetUp() throws Exception { this.scope = new ImporterTopLevel( ); DataEngineContext context = DataEngineContext.newInstance( DataEngineContext.DIRECT_PRESENTATION, scope, null, null ); context.setTmpdir( this.getTempDir( ) ); de = (DataEngineImpl) DataEngine.newDataEngine( context ); DateCube util = new DateCube( ); util.createCube( de ); cube = util.getCube( DateCube.cubeName, de ); }
public void run( ) { Context cx = Context.enter( ); vm.attach( cx ); Scriptable global = new ImporterTopLevel( ); cx.evaluateString( global, "\r\nvar a = 2;\r\n \r\nvar b = a*2;\r\n", "sec1", 0, null ); cx.evaluateString( global, "var a = 'ok';\r\nvar b = a;\r\n", "sec2", 0, null ); cx.evaluateString( global, "\r\n\r\nvar a = 2;\r\n\r\n\r\nvar b = a*2;\r\n", "sec1", 0, null ); vm.detach( cx ); }
/** * Test java script object I/O * @throws IOException */ @Test public void testNativeDate( ) throws IOException { final int size = 1000; ByteArrayOutputStream bos = null; DataOutputStream dos = null; byte[] content = null; ByteArrayInputStream bis = null; DataInputStream dis = null; bos = new ByteArrayOutputStream( size ); dos = new DataOutputStream( bos ); Context cx = Context.enter( ); Scriptable sharedScope = new ImporterTopLevel( cx ); Object ob = cx.evaluateString( sharedScope, "new Date", null, -1, null ); IOUtil.writeObject( dos, ob ); content = bos.toByteArray( ); bis = new ByteArrayInputStream( content ); dis = new DataInputStream( bis ); Object ob2 = IOUtil.readObject( dis ); assertTrue( ob instanceof IdScriptableObject ); assertTrue( ob2 instanceof IdScriptableObject ); assertEquals( JavascriptEvalUtil.convertJavascriptValue( ob ), JavascriptEvalUtil.convertJavascriptValue( ob2 ) ); Context.exit( ); }
/** * Test java script object I/O * @throws IOException */ @Test public void testNativeJavaObject( ) throws IOException { final int size = 1000; ByteArrayOutputStream bos = null; DataOutputStream dos = null; byte[] content = null; ByteArrayInputStream bis = null; DataInputStream dis = null; bos = new ByteArrayOutputStream( size ); dos = new DataOutputStream( bos ); Context cx = Context.enter( ); Scriptable sharedScope = new ImporterTopLevel( cx ); HashMap source = new HashMap( ); source.put( "key1", "value1"); source.put( "key2", "value2"); Object ob = cx.javaToJS( source, sharedScope ); IOUtil.writeObject( dos, ob ); content = bos.toByteArray( ); bis = new ByteArrayInputStream( content ); dis = new DataInputStream( bis ); Object ob2 = IOUtil.readObject( dis ); assertTrue( ob2 instanceof HashMap ); assertEquals( source, ob2 ); Context.exit( ); }
public ButterflyScope(ButterflyModule module, Context context) throws Exception { _logger.trace("> new ButterflyScope for module: {}", module.getName()); // first, get a juicy top level scope that contains all the ECMA // objects and some Java related utility methods that Rhino provides Scriptable scope = new ImporterTopLevel(context); // make the "ButteflyModule" object available to this scope defineClass(scope, ScriptableButterfly.class); // and set this scope as our prototype setPrototype(scope); // We want this to be a new top-level scope, so set its // parent scope to null. This means that any variables created // by assignments will be properties of this. setParentScope(null); // We need to create an instance of the ButterflyModule object and // inject it into this scope. We keep a reference to this instance // because it's what we'll need to know if the controller has responded // to the request or not final Object[] args = {}; ScriptableButterfly _scriptableButterfly = (ScriptableButterfly) context.newObject(this, ScriptableButterfly.getName(), args); _scriptableButterfly.init(module); _scriptableButterfly.setParentScope(this); super.put("butterfly", this, _scriptableButterfly); super.put("module", this, module); prepareScope(context, scope, module); _logger.trace("< new ButterflyScope for module: {}", module.getName()); }
/** * Initialize the engine. * Put the manager into the context-manager * map hashtable too. */ @Override public void initialize(BSFManager mgr, String lang, @SuppressWarnings("rawtypes") // superclass does not support types Vector declaredBeans) throws BSFException { super.initialize(mgr, lang, declaredBeans); // Initialize context and global scope object try { Context cx = Context.enter(); global = new ImporterTopLevel(cx); Scriptable bsf = Context.toObject(new BSFFunctions(mgr, this), global); global.put("bsf", global, bsf); for( @SuppressWarnings("unchecked") Iterator<BSFDeclaredBean> it = declaredBeans.iterator(); it.hasNext();) { declareBean(it.next()); } } catch (Throwable t) { handleError(t); } finally { Context.exit(); } }
protected Scriptable createScope(Context context) { ImporterTopLevel importerTopLevel = new ImporterTopLevel(); importerTopLevel.initStandardObjects(context, false); return importerTopLevel; }
@SuppressWarnings("nls") public Scriptable getScope(Context jsContext) { Scriptable scope = new ImporterTopLevel(jsContext); for( String name : scriptObjects.keySet() ) { Object obj = scriptObjects.get(name); if( obj instanceof Boolean ) { scope.put(name, scope, obj); } else if( obj != null ) { Scriptable jsArgs = Context.toObject(obj, scope); scope.put(name, scope, jsArgs); } } // Remove the ability to create new Java objects in the script. List // comes from https://bugzilla.mozilla.org/show_bug.cgi?id=468385 scope.delete("Packages"); scope.delete("JavaImporter"); scope.delete("JavaAdapter"); scope.delete("getClass"); scope.delete("java"); scope.delete("javax"); scope.delete("com"); scope.delete("net"); scope.delete("edu"); scope.delete("org"); try { // Prevent existingObject.getClass().forName('...') jsContext.setClassShutter(new ClassShutter() { @Override public boolean visibleToScripts(String className) { return !className.equals("java.lang.Class"); } }); } catch( SecurityException se ) { // ignore - there's no way to test presence of ClassShutter // (indicative of a Context provided by calling module: ReportEngine // for example) other than by attempting to set ClassShutter. // See #8135 } return scope; }
/** * This method runs part of the core module controller.js script so that we * can register all operations without having to duplicate configuration * here. */ public void loadModule(String name, String path, String... initFunctions) throws IOException { ButterflyModule core = new ButterflyModuleStub(name); // File controllerFile = new File(Configurations.get("refine.root", // "../OpenRefine"), path); String script = IOUtils.toString(getClass().getClassLoader().getResourceAsStream(path)); if (script == null) { fLogger.warn(String.format( "Can't find controller script for module %s at %s -- " + "module may not work as expected.", name, name)); return; } // Compiles and "executes" the controller script. The script basically // contains function declarations. Context context = ContextFactory.getGlobal().enterContext(); Script controller = context.compileString( script, "init.js", 1, null); // Initializes the scope. ScriptableObject scope = new ImporterTopLevel(context); scope.put("module", scope, core); controller.exec(context, scope); for (String function : initFunctions) { // Runs the function that initializes the OperationRegistry. try { Object fun = context.compileString(function, null, 1, null) .exec(context, scope); if (fun != null && fun instanceof Function) { ((Function) fun).call(context, scope, scope, new Object[]{}); } } catch (EcmaError ex) { fLogger.error("Error running controller script.", ex); } } }
public JsScriptEngine() { Context context = getContext(); scope = new ImporterTopLevel(context); // so that we can use importPackage() context.evaluateString(scope, printSource, "print", 1, null); // so that we can print on std out }
/** * test year to date function * * @throws Exception */ @Test public void testRelativeTimePeriod1() throws Exception { ICubeQueryDefinition cqd = new CubeQueryDefinition(DateCube.cubeName); IEdgeDefinition rowEdge = cqd .createEdge(ICubeQueryDefinition.COLUMN_EDGE); IDimensionDefinition productLineDim1 = rowEdge .createDimension("dimension2"); IHierarchyDefinition porductLineHie1 = productLineDim1 .createHierarchy("dimension2"); porductLineHie1.createLevel("level21"); IEdgeDefinition columnEdge = cqd .createEdge(ICubeQueryDefinition.ROW_EDGE); IDimensionDefinition dateDim = columnEdge.createDimension("dimension1"); IHierarchyDefinition dateHier = dateDim.createHierarchy("dimension1"); dateHier.createLevel("level11"); dateHier.createLevel("level12"); dateHier.createLevel("level13"); dateHier.createLevel("level14"); IMeasureDefinition measure =cqd.createMeasure( "measure1" ); measure.setAggrFunction( "SUM" ); TimeFunction timeFunction = new TimeFunction(); TimePeriod timePeriod = new TimePeriod(0, TimePeriodType.YEAR); ReferenceDate referenceDate = new ReferenceDate(new GregorianCalendar( 1999, 8, 20).getTime()); timeFunction.setReferenceDate(referenceDate); timeFunction.setBaseTimePeriod(timePeriod); timeFunction.setTimeDimension("dimension1"); IBinding binding2 = new Binding("measure1"); binding2.setExpression(new ScriptExpression("measure[\"measure1\"]")); binding2.setTimeFunction(timeFunction); binding2.setAggrFunction(IBuildInAggregation.TOTAL_SUM_FUNC); binding2.addAggregateOn("dimension[\"dimension1\"][\"level11\"]"); binding2.addAggregateOn("dimension[\"dimension1\"][\"level12\"]"); binding2.addAggregateOn("dimension[\"dimension1\"][\"level13\"]"); binding2.addAggregateOn("dimension[\"dimension1\"][\"level14\"]"); binding2.addAggregateOn("dimension[\"dimension2\"][\"level21\"]"); cqd.addBinding(binding2); DateCube util = new DateCube(); DataEngineImpl engine = (DataEngineImpl) DataEngine .newDataEngine(createPresentationContext()); util.createCube(engine); ICube cube = util.getCube(DateCube.cubeName, engine); BirtCubeView cubeView = new BirtCubeView(new CubeQueryExecutor(null, cqd, engine.getSession(), new ImporterTopLevel(), engine.getContext()), cube, null, null); CubeCursor cursor = cubeView.getCubeCursor(new StopSign(), cube); List columnEdgeBindingNames = new ArrayList(); List rowEdgeBindingNames = new ArrayList(); this.printCube(cursor, columnEdgeBindingNames, rowEdgeBindingNames, "measure1"); engine.shutdown(); }
/** * test year to date function * * @throws Exception */ @Test public void testRelativeTimePeriod2() throws Exception { ICubeQueryDefinition cqd = new CubeQueryDefinition(DateCube.cubeName); IEdgeDefinition rowEdge = cqd .createEdge(ICubeQueryDefinition.COLUMN_EDGE); IDimensionDefinition productLineDim1 = rowEdge .createDimension("dimension2"); IHierarchyDefinition porductLineHie1 = productLineDim1 .createHierarchy("dimension2"); porductLineHie1.createLevel("level21"); IEdgeDefinition columnEdge = cqd .createEdge(ICubeQueryDefinition.ROW_EDGE); IDimensionDefinition dateDim = columnEdge.createDimension("dimension1"); IHierarchyDefinition dateHier = dateDim.createHierarchy("dimension1"); dateHier.createLevel("level11"); dateHier.createLevel("level12"); dateHier.createLevel("level13"); dateHier.createLevel("level14"); IMeasureDefinition measure =cqd.createMeasure( "measure1" ); measure.setAggrFunction( "SUM" ); TimeFunction timeFunction = new TimeFunction(); TimePeriod timePeriod = new TimePeriod(0, TimePeriodType.YEAR); ReferenceDate referenceDate = new ReferenceDate(new GregorianCalendar( 1999, 7, 19).getTime()); timeFunction.setReferenceDate(referenceDate); timeFunction.setBaseTimePeriod(timePeriod); timeFunction.setTimeDimension("dimension1"); IBinding binding2 = new Binding("measure1"); binding2.setExpression(new ScriptExpression("measure[\"measure1\"]")); binding2.setTimeFunction(timeFunction); binding2.setAggrFunction(IBuildInAggregation.TOTAL_SUM_FUNC); binding2.addAggregateOn("dimension[\"dimension1\"][\"level11\"]"); binding2.addAggregateOn("dimension[\"dimension2\"][\"level21\"]"); cqd.addBinding(binding2); DateCube util = new DateCube(); DataEngineImpl engine = (DataEngineImpl) DataEngine .newDataEngine(createPresentationContext()); util.createCube(engine); ICube cube = util.getCube(DateCube.cubeName, engine); BirtCubeView cubeView = new BirtCubeView(new CubeQueryExecutor(null, cqd, engine.getSession(), new ImporterTopLevel(), engine.getContext()), cube, null, null); CubeCursor cursor = cubeView.getCubeCursor(new StopSign(), cube); List columnEdgeBindingNames = new ArrayList(); List rowEdgeBindingNames = new ArrayList(); this.printCube(cursor, columnEdgeBindingNames, rowEdgeBindingNames, "measure1"); engine.shutdown(); }
/** * test month to date function * * @throws Exception */ @Test public void testRelativeTimePeriod3() throws Exception { ICubeQueryDefinition cqd = new CubeQueryDefinition(DateCube.cubeName); IEdgeDefinition rowEdge = cqd .createEdge(ICubeQueryDefinition.COLUMN_EDGE); IDimensionDefinition productLineDim1 = rowEdge .createDimension("dimension2"); IHierarchyDefinition porductLineHie1 = productLineDim1 .createHierarchy("dimension2"); porductLineHie1.createLevel("level21"); IEdgeDefinition columnEdge = cqd .createEdge(ICubeQueryDefinition.ROW_EDGE); IDimensionDefinition dateDim = columnEdge.createDimension("dimension1"); IHierarchyDefinition dateHier = dateDim.createHierarchy("dimension1"); dateHier.createLevel("level11"); dateHier.createLevel("level12"); dateHier.createLevel("level13"); dateHier.createLevel("level14"); IMeasureDefinition measure =cqd.createMeasure( "measure1" ); measure.setAggrFunction( "SUM" ); TimeFunction timeFunction = new TimeFunction(); TimePeriod timePeriod = new TimePeriod(0, TimePeriodType.MONTH); ReferenceDate referenceDate = new ReferenceDate(new GregorianCalendar( 1998, 7, 19).getTime()); timeFunction.setReferenceDate(referenceDate); timeFunction.setBaseTimePeriod(timePeriod); timeFunction.setTimeDimension("dimension1"); IBinding binding2 = new Binding("measure1"); binding2.setExpression(new ScriptExpression("measure[\"measure1\"]")); binding2.setTimeFunction(timeFunction); binding2.setAggrFunction(IBuildInAggregation.TOTAL_SUM_FUNC); binding2.addAggregateOn("dimension[\"dimension1\"][\"level11\"]"); binding2.addAggregateOn("dimension[\"dimension1\"][\"level12\"]"); binding2.addAggregateOn("dimension[\"dimension1\"][\"level13\"]"); cqd.addBinding(binding2); DateCube util = new DateCube(); DataEngineImpl engine = (DataEngineImpl) DataEngine .newDataEngine(createPresentationContext()); util.createCube(engine); ICube cube = util.getCube(DateCube.cubeName, engine); BirtCubeView cubeView = new BirtCubeView(new CubeQueryExecutor(null, cqd, engine.getSession(), new ImporterTopLevel(), engine.getContext()), cube, null, null); CubeCursor cursor = cubeView.getCubeCursor(new StopSign(), cube); List columnEdgeBindingNames = new ArrayList(); List rowEdgeBindingNames = new ArrayList(); this.printCube(cursor, columnEdgeBindingNames, rowEdgeBindingNames, "measure1"); engine.shutdown(); }
/** * test week to date function * * @throws Exception */ @Test public void testRelativeTimePeriod12() throws Exception { ICubeQueryDefinition cqd = new CubeQueryDefinition(DateCube.cubeName); IEdgeDefinition rowEdge = cqd .createEdge(ICubeQueryDefinition.COLUMN_EDGE); IDimensionDefinition productLineDim1 = rowEdge .createDimension("dimension2"); IHierarchyDefinition porductLineHie1 = productLineDim1 .createHierarchy("dimension2"); porductLineHie1.createLevel("level21"); IEdgeDefinition columnEdge = cqd .createEdge(ICubeQueryDefinition.ROW_EDGE); IDimensionDefinition dateDim = columnEdge.createDimension("dimension1"); IHierarchyDefinition dateHier = dateDim.createHierarchy("dimension1"); dateHier.createLevel("level11"); dateHier.createLevel("level12"); dateHier.createLevel("level13"); dateHier.createLevel("level14"); IMeasureDefinition measure =cqd.createMeasure( "measure1" ); measure.setAggrFunction( "SUM" ); TimeFunction timeFunction = new TimeFunction(); TimePeriod timePeriod = new TimePeriod(0, TimePeriodType.WEEK); ReferenceDate referenceDate = new ReferenceDate(new GregorianCalendar( 1998, 7, 19).getTime()); timeFunction.setReferenceDate(referenceDate); timeFunction.setBaseTimePeriod(timePeriod); timeFunction.setTimeDimension("dimension1"); IBinding binding2 = new Binding("measure1"); binding2.setExpression(new ScriptExpression("measure[\"measure1\"]")); binding2.setTimeFunction(timeFunction); binding2.setAggrFunction(IBuildInAggregation.TOTAL_SUM_FUNC); binding2.addAggregateOn("dimension[\"dimension1\"][\"level11\"]"); binding2.addAggregateOn("dimension[\"dimension1\"][\"level12\"]"); binding2.addAggregateOn("dimension[\"dimension1\"][\"level13\"]"); cqd.addBinding(binding2); DateCube util = new DateCube(); DataEngineImpl engine = (DataEngineImpl) DataEngine .newDataEngine(createPresentationContext()); util.createCube(engine); ICube cube = util.getCube(DateCube.cubeName, engine); BirtCubeView cubeView = new BirtCubeView(new CubeQueryExecutor(null, cqd, engine.getSession(), new ImporterTopLevel(), engine.getContext()), cube, null, null); CubeCursor cursor = cubeView.getCubeCursor(new StopSign(), cube); List columnEdgeBindingNames = new ArrayList(); List rowEdgeBindingNames = new ArrayList(); this.printCube(cursor, columnEdgeBindingNames, rowEdgeBindingNames, "measure1"); engine.shutdown(); }
/** * test trailing function * * @throws Exception */ @Test public void testRelativeTimePeriod5() throws Exception { ICubeQueryDefinition cqd = new CubeQueryDefinition(DateCube.cubeName); IEdgeDefinition rowEdge = cqd .createEdge(ICubeQueryDefinition.COLUMN_EDGE); IDimensionDefinition productLineDim1 = rowEdge .createDimension("dimension2"); IHierarchyDefinition porductLineHie1 = productLineDim1 .createHierarchy("dimension2"); porductLineHie1.createLevel("level21"); IEdgeDefinition columnEdge = cqd .createEdge(ICubeQueryDefinition.ROW_EDGE); IDimensionDefinition dateDim = columnEdge.createDimension("dimension1"); IHierarchyDefinition dateHier = dateDim.createHierarchy("dimension1"); dateHier.createLevel("level11"); dateHier.createLevel("level12"); dateHier.createLevel("level13"); dateHier.createLevel("level14"); IMeasureDefinition measure =cqd.createMeasure( "measure1" ); measure.setAggrFunction( "SUM" ); TimeFunction timeFunction = new TimeFunction(); TimePeriod timePeriod = new TimePeriod(-3, TimePeriodType.MONTH); ReferenceDate referenceDate = new ReferenceDate(new GregorianCalendar( 1998, 10, 20).getTime()); timeFunction.setReferenceDate(referenceDate); timeFunction.setBaseTimePeriod(timePeriod); timeFunction.setTimeDimension("dimension1"); IBinding binding2 = new Binding("measure1"); binding2.setExpression(new ScriptExpression("measure[\"measure1\"]")); binding2.setTimeFunction(timeFunction); binding2.setAggrFunction(IBuildInAggregation.TOTAL_SUM_FUNC); binding2.addAggregateOn("dimension[\"dimension1\"][\"level11\"]"); cqd.addBinding(binding2); DateCube util = new DateCube(); DataEngineImpl engine = (DataEngineImpl) DataEngine .newDataEngine(createPresentationContext()); util.createCube(engine); ICube cube = util.getCube(DateCube.cubeName, engine); BirtCubeView cubeView = new BirtCubeView(new CubeQueryExecutor(null, cqd, engine.getSession(), new ImporterTopLevel(), engine.getContext()), cube, null, null); CubeCursor cursor = cubeView.getCubeCursor(new StopSign(), cube); List columnEdgeBindingNames = new ArrayList(); List rowEdgeBindingNames = new ArrayList(); this.printCube(cursor, columnEdgeBindingNames, rowEdgeBindingNames, "measure1"); engine.shutdown(); }
/** * test trailing function * * @throws Exception */ @Test public void testRelativeTimePeriod9() throws Exception { ICubeQueryDefinition cqd = new CubeQueryDefinition(DateCube.cubeName); IEdgeDefinition rowEdge = cqd .createEdge(ICubeQueryDefinition.COLUMN_EDGE); IDimensionDefinition productLineDim1 = rowEdge .createDimension("dimension2"); IHierarchyDefinition porductLineHie1 = productLineDim1 .createHierarchy("dimension2"); porductLineHie1.createLevel("level21"); IEdgeDefinition columnEdge = cqd .createEdge(ICubeQueryDefinition.ROW_EDGE); IDimensionDefinition dateDim = columnEdge.createDimension("dimension1"); IHierarchyDefinition dateHier = dateDim.createHierarchy("dimension1"); dateHier.createLevel("level11"); dateHier.createLevel("level12"); dateHier.createLevel("level13"); dateHier.createLevel("level14"); IMeasureDefinition measure =cqd.createMeasure( "measure1" ); measure.setAggrFunction( "SUM" ); TimeFunction timeFunction = new TimeFunction(); TimePeriod timePeriod = new TimePeriod(-3, TimePeriodType.DAY); ReferenceDate referenceDate = new ReferenceDate(new GregorianCalendar( 1998, 8, 20).getTime()); timeFunction.setReferenceDate(referenceDate); timeFunction.setBaseTimePeriod(timePeriod); timeFunction.setTimeDimension("dimension1"); IBinding binding2 = new Binding("measure1"); binding2.setExpression(new ScriptExpression("measure[\"measure1\"]")); binding2.setTimeFunction(timeFunction); binding2.setAggrFunction(IBuildInAggregation.TOTAL_SUM_FUNC); binding2.addAggregateOn("dimension[\"dimension1\"][\"level11\"]"); cqd.addBinding(binding2); DateCube util = new DateCube(); DataEngineImpl engine = (DataEngineImpl) DataEngine .newDataEngine(createPresentationContext()); util.createCube(engine); ICube cube = util.getCube(DateCube.cubeName, engine); BirtCubeView cubeView = new BirtCubeView(new CubeQueryExecutor(null, cqd, engine.getSession(), new ImporterTopLevel(), engine.getContext()), cube, null, null); CubeCursor cursor = cubeView.getCubeCursor(new StopSign(), cube); List columnEdgeBindingNames = new ArrayList(); List rowEdgeBindingNames = new ArrayList(); this.printCube(cursor, columnEdgeBindingNames, rowEdgeBindingNames, "measure1"); engine.shutdown(); }
/** * test trailing function * * @throws Exception */ @Test public void testRelativeTimePeriod6() throws Exception { ICubeQueryDefinition cqd = new CubeQueryDefinition(DateCube.cubeName); IEdgeDefinition rowEdge = cqd .createEdge(ICubeQueryDefinition.COLUMN_EDGE); IDimensionDefinition productLineDim1 = rowEdge .createDimension("dimension2"); IHierarchyDefinition porductLineHie1 = productLineDim1 .createHierarchy("dimension2"); porductLineHie1.createLevel("level21"); IEdgeDefinition columnEdge = cqd .createEdge(ICubeQueryDefinition.ROW_EDGE); IDimensionDefinition dateDim = columnEdge.createDimension("dimension1"); IHierarchyDefinition dateHier = dateDim.createHierarchy("dimension1"); dateHier.createLevel("level11"); dateHier.createLevel("level12"); dateHier.createLevel("level13"); dateHier.createLevel("level14"); IMeasureDefinition measure =cqd.createMeasure( "measure1" ); measure.setAggrFunction( "SUM" ); TimeFunction timeFunction = new TimeFunction(); TimePeriod timePeriod = new TimePeriod(-2, TimePeriodType.YEAR); ReferenceDate referenceDate = new ReferenceDate(new GregorianCalendar( 1999, 10, 20).getTime()); timeFunction.setReferenceDate(referenceDate); timeFunction.setBaseTimePeriod(timePeriod); timeFunction.setTimeDimension("dimension1"); IBinding binding2 = new Binding("measure1"); binding2.setExpression(new ScriptExpression("measure[\"measure1\"]")); binding2.setTimeFunction(timeFunction); binding2.setAggrFunction(IBuildInAggregation.TOTAL_SUM_FUNC); binding2.addAggregateOn("dimension[\"dimension2\"][\"level21\"]"); cqd.addBinding(binding2); DateCube util = new DateCube(); DataEngineImpl engine = (DataEngineImpl) DataEngine .newDataEngine(createPresentationContext()); util.createCube(engine); ICube cube = util.getCube(DateCube.cubeName, engine); BirtCubeView cubeView = new BirtCubeView(new CubeQueryExecutor(null, cqd, engine.getSession(), new ImporterTopLevel(), engine.getContext()), cube, null, null); CubeCursor cursor = cubeView.getCubeCursor(new StopSign(), cube); List columnEdgeBindingNames = new ArrayList(); List rowEdgeBindingNames = new ArrayList(); this.printCube(cursor, columnEdgeBindingNames, rowEdgeBindingNames, "measure1"); engine.shutdown(); }
/** * test trailing function * * @throws Exception */ @Test public void testRelativeTimePeriod13() throws Exception { ICubeQueryDefinition cqd = new CubeQueryDefinition(DateCube.cubeName); IEdgeDefinition rowEdge = cqd .createEdge(ICubeQueryDefinition.COLUMN_EDGE); IDimensionDefinition productLineDim1 = rowEdge .createDimension("dimension2"); IHierarchyDefinition porductLineHie1 = productLineDim1 .createHierarchy("dimension2"); porductLineHie1.createLevel("level21"); IEdgeDefinition columnEdge = cqd .createEdge(ICubeQueryDefinition.ROW_EDGE); IDimensionDefinition dateDim = columnEdge.createDimension("dimension1"); IHierarchyDefinition dateHier = dateDim.createHierarchy("dimension1"); dateHier.createLevel("level11"); dateHier.createLevel("level12"); dateHier.createLevel("level13"); dateHier.createLevel("level14"); IMeasureDefinition measure =cqd.createMeasure( "measure1" ); measure.setAggrFunction( "SUM" ); TimeFunction timeFunction = new TimeFunction(); TimePeriod timePeriod = new TimePeriod(3, TimePeriodType.YEAR); ReferenceDate referenceDate = new ReferenceDate(new GregorianCalendar( 1998, 11, 20).getTime()); timeFunction.setReferenceDate(referenceDate); timeFunction.setBaseTimePeriod(timePeriod); timeFunction.setTimeDimension("dimension1"); IBinding binding2 = new Binding("measure1"); binding2.setExpression(new ScriptExpression("measure[\"measure1\"]")); binding2.setTimeFunction(timeFunction); binding2.setAggrFunction(IBuildInAggregation.TOTAL_SUM_FUNC); binding2.addAggregateOn("dimension[\"dimension2\"][\"level21\"]"); cqd.addBinding(binding2); DateCube util = new DateCube(); DataEngineImpl engine = (DataEngineImpl) DataEngine .newDataEngine(createPresentationContext()); util.createCube(engine); ICube cube = util.getCube(DateCube.cubeName, engine); BirtCubeView cubeView = new BirtCubeView(new CubeQueryExecutor(null, cqd, engine.getSession(), new ImporterTopLevel(), engine.getContext()), cube, null, null); CubeCursor cursor = cubeView.getCubeCursor(new StopSign(), cube); List columnEdgeBindingNames = new ArrayList(); List rowEdgeBindingNames = new ArrayList(); this.printCube(cursor, columnEdgeBindingNames, rowEdgeBindingNames, "measure1"); engine.shutdown(); }
/** * test year to date function using latest date in cube * * @throws Exception */ @Test public void testRelativeTimePeriod18() throws Exception { ICubeQueryDefinition cqd = new CubeQueryDefinition(DateCube.cubeName); IEdgeDefinition rowEdge = cqd .createEdge(ICubeQueryDefinition.COLUMN_EDGE); IDimensionDefinition productLineDim1 = rowEdge .createDimension("dimension2"); IHierarchyDefinition porductLineHie1 = productLineDim1 .createHierarchy("dimension2"); porductLineHie1.createLevel("level21"); IEdgeDefinition columnEdge = cqd .createEdge(ICubeQueryDefinition.ROW_EDGE); IDimensionDefinition dateDim = columnEdge.createDimension("dimension1"); IHierarchyDefinition dateHier = dateDim.createHierarchy("dimension1"); dateHier.createLevel("level11"); dateHier.createLevel("level12"); dateHier.createLevel("level13"); dateHier.createLevel("level14"); IMeasureDefinition measure =cqd.createMeasure( "measure1" ); measure.setAggrFunction( "SUM" ); TimeFunction timeFunction = new TimeFunction(); TimePeriod timePeriod = new TimePeriod(0, TimePeriodType.YEAR); timeFunction.setReferenceDate( null ); timeFunction.setBaseTimePeriod(timePeriod); timeFunction.setTimeDimension("dimension1"); IBinding binding2 = new Binding("measure1"); binding2.setExpression(new ScriptExpression("measure[\"measure1\"]")); binding2.setTimeFunction(timeFunction); binding2.setAggrFunction(IBuildInAggregation.TOTAL_SUM_FUNC); binding2.addAggregateOn("dimension[\"dimension1\"][\"level11\"]"); binding2.addAggregateOn("dimension[\"dimension1\"][\"level12\"]"); binding2.addAggregateOn("dimension[\"dimension1\"][\"level13\"]"); binding2.addAggregateOn("dimension[\"dimension1\"][\"level14\"]"); binding2.addAggregateOn("dimension[\"dimension2\"][\"level21\"]"); cqd.addBinding(binding2); DateCube util = new DateCube(); DataEngineImpl engine = (DataEngineImpl) DataEngine .newDataEngine(createPresentationContext()); util.createCube(engine); ICube cube = util.getCube(DateCube.cubeName, engine); BirtCubeView cubeView = new BirtCubeView(new CubeQueryExecutor(null, cqd, engine.getSession(), new ImporterTopLevel(), engine.getContext()), cube, null, null); CubeCursor cursor = cubeView.getCubeCursor(new StopSign(), cube); List columnEdgeBindingNames = new ArrayList(); List rowEdgeBindingNames = new ArrayList(); this.printCube(cursor, columnEdgeBindingNames, rowEdgeBindingNames, "measure1"); engine.shutdown(); }
/** * test previous N period function * * @throws Exception */ @Test public void testRelativeTimePeriod7() throws Exception { ICubeQueryDefinition cqd = new CubeQueryDefinition(DateCube.cubeName); IEdgeDefinition rowEdge = cqd .createEdge(ICubeQueryDefinition.COLUMN_EDGE); IDimensionDefinition productLineDim1 = rowEdge .createDimension("dimension2"); IHierarchyDefinition porductLineHie1 = productLineDim1 .createHierarchy("dimension2"); porductLineHie1.createLevel("level21"); IEdgeDefinition columnEdge = cqd .createEdge(ICubeQueryDefinition.ROW_EDGE); IDimensionDefinition dateDim = columnEdge.createDimension("dimension1"); IHierarchyDefinition dateHier = dateDim.createHierarchy("dimension1"); dateHier.createLevel("level11"); dateHier.createLevel("level12"); dateHier.createLevel("level13"); dateHier.createLevel("level14"); IMeasureDefinition measure =cqd.createMeasure( "measure1" ); measure.setAggrFunction( "SUM" ); TimeFunction timeFunction = new TimeFunction(); TimePeriod timePeriod = new TimePeriod(0, TimePeriodType.YEAR); ReferenceDate referenceDate = new ReferenceDate(new GregorianCalendar( 1998, 10, 20).getTime()); timeFunction.setReferenceDate(referenceDate); timeFunction.setBaseTimePeriod(timePeriod); timeFunction.setTimeDimension("dimension1"); timePeriod = new TimePeriod(-3, TimePeriodType.MONTH); timeFunction.setRelativeTimePeriod(timePeriod); IBinding binding2 = new Binding("measure1"); binding2.setExpression(new ScriptExpression("measure[\"measure1\"]")); binding2.setTimeFunction(timeFunction); binding2.setAggrFunction(IBuildInAggregation.TOTAL_SUM_FUNC); binding2.addAggregateOn("dimension[\"dimension1\"][\"level11\"]"); cqd.addBinding(binding2); DateCube util = new DateCube(); DataEngineImpl engine = (DataEngineImpl) DataEngine .newDataEngine(createPresentationContext()); util.createCube(engine); ICube cube = util.getCube(DateCube.cubeName, engine); BirtCubeView cubeView = new BirtCubeView(new CubeQueryExecutor(null, cqd, engine.getSession(), new ImporterTopLevel(), engine.getContext()), cube, null, null); CubeCursor cursor = cubeView.getCubeCursor(new StopSign(), cube); List columnEdgeBindingNames = new ArrayList(); List rowEdgeBindingNames = new ArrayList(); this.printCube(cursor, columnEdgeBindingNames, rowEdgeBindingNames, "measure1"); engine.shutdown(); }
/** * test previous N period function * * @throws Exception */ @Test public void testRelativeTimePeriod10() throws Exception { ICubeQueryDefinition cqd = new CubeQueryDefinition(DateCube.cubeName); IEdgeDefinition rowEdge = cqd .createEdge(ICubeQueryDefinition.COLUMN_EDGE); IDimensionDefinition productLineDim1 = rowEdge .createDimension("dimension2"); IHierarchyDefinition porductLineHie1 = productLineDim1 .createHierarchy("dimension2"); porductLineHie1.createLevel("level21"); IEdgeDefinition columnEdge = cqd .createEdge(ICubeQueryDefinition.ROW_EDGE); IDimensionDefinition dateDim = columnEdge.createDimension("dimension1"); IHierarchyDefinition dateHier = dateDim.createHierarchy("dimension1"); dateHier.createLevel("level11"); dateHier.createLevel("level12"); dateHier.createLevel("level13"); dateHier.createLevel("level14"); IMeasureDefinition measure =cqd.createMeasure( "measure1" ); measure.setAggrFunction( "SUM" ); TimeFunction timeFunction = new TimeFunction(); TimePeriod timePeriod = new TimePeriod(0, TimePeriodType.YEAR); ReferenceDate referenceDate = new ReferenceDate(new GregorianCalendar( 1999, 9, 9).getTime()); timeFunction.setReferenceDate(referenceDate); timeFunction.setBaseTimePeriod(timePeriod); timeFunction.setTimeDimension("dimension1"); timePeriod = new TimePeriod(-3, TimePeriodType.WEEK); timeFunction.setRelativeTimePeriod(timePeriod); IBinding binding2 = new Binding("measure1"); binding2.setExpression(new ScriptExpression("measure[\"measure1\"]")); binding2.setTimeFunction(timeFunction); binding2.setAggrFunction(IBuildInAggregation.TOTAL_SUM_FUNC); binding2.addAggregateOn("dimension[\"dimension1\"][\"level11\"]"); cqd.addBinding(binding2); DateCube util = new DateCube(); DataEngineImpl engine = (DataEngineImpl) DataEngine .newDataEngine(createPresentationContext()); util.createCube(engine); ICube cube = util.getCube(DateCube.cubeName, engine); BirtCubeView cubeView = new BirtCubeView(new CubeQueryExecutor(null, cqd, engine.getSession(), new ImporterTopLevel(), engine.getContext()), cube, null, null); CubeCursor cursor = cubeView.getCubeCursor(new StopSign(), cube); List columnEdgeBindingNames = new ArrayList(); List rowEdgeBindingNames = new ArrayList(); this.printCube(cursor, columnEdgeBindingNames, rowEdgeBindingNames, "measure1"); engine.shutdown(); }