OpenFlashChartComponent extends ComponentBase { protected boolean executeAction() { log.debug( "start to run open flash chart component......" ); IPentahoResultSet data = (IPentahoResultSet) getInputValue( CHART_DATA ); if ( !data.isScrollable() ) { getLogger().debug( "ResultSet is not scrollable. Copying into memory" ); IPentahoResultSet memSet = data.memoryCopy(); data.close(); data = memSet; } String chartWidth = null; String inputWidth = getInputStringValue( CHART_WIDTH ); if ( inputWidth == null ) { chartWidth = DEFAULT_WIDTH; } else { chartWidth = inputWidth; } String chartHeight = null; String inputHeight = getInputStringValue( CHART_HEIGHT ); if ( null == inputHeight ) { chartHeight = DEFAULT_HEIGHT; } else { chartHeight = inputHeight; } String ofcURL = getInputStringValue( OFC_URL ); if ( ofcURL == null || "".equals( ofcURL ) ) { ofcURL = PentahoRequestContextHolder.getRequestContext().getContextPath() + DEFAULT_FLASH_LOC; } String ofclibname = getInputStringValue( OFC_LIB_NAME ); if ( ofclibname == null || "".equals( ofclibname ) ) { ofclibname = DEFAULT_FLASH_SWF; } String chartAttributeString = null; if ( getInputNames().contains( CHART_ATTRIBUTES ) ) { chartAttributeString = getInputStringValue( CHART_ATTRIBUTES ); } else if ( isDefinedResource( CHART_ATTRIBUTES ) ) { IActionSequenceResource resource = getResource( CHART_ATTRIBUTES ); chartAttributeString = getResourceAsString( resource ); } Node chartNode = null; if ( chartAttributeString != null ) { chartAttributeString = applyInputsToFormat( chartAttributeString ); try { Document chartDocument = XmlDom4JHelper.getDocFromString( chartAttributeString, new PentahoEntityResolver() ); chartNode = chartDocument.selectSingleNode( CHART_NODE_LOC ); if ( chartNode == null ) { chartNode = chartDocument.selectSingleNode( CHART_ATTRIBUTES ); } } catch ( XmlParseException e ) { getLogger().error( Messages.getInstance().getErrorString( "OpenFlashChartComponent.ERROR_0001_CANT_DOCUMENT_FROM_STRING" ), e ); return false; } } else { chartNode = getComponentDefinition( true ).selectSingleNode( CHART_ATTRIBUTES ); } if ( chartNode == null ) { getLogger().error( Messages.getInstance().getErrorString( "OpenFlashChartComponent.ERROR_0002_CHART_DEFINITION_NOT_FOUND" ) ); return false; } boolean byRow = false; if ( getInputStringValue( BY_ROW_PROP ) != null ) { byRow = Boolean.valueOf( getInputStringValue( BY_ROW_PROP ) ).booleanValue(); } String chartJson = generateChartJson( data, chartNode, byRow ); String chartId = UUIDUtil.getUUIDAsString().replaceAll( "[^\\w]", "" ); Properties props = new Properties(); props.setProperty( "chartId", chartId ); props.setProperty( "dataFunction", "getData" + chartId ); props.setProperty( "chart-width", chartWidth ); props.setProperty( "chart-height", chartHeight ); props.setProperty( "ofc-url", ofcURL ); props.setProperty( "ofc-libname", ofclibname ); props.setProperty( "chartJson", chartJson.replaceAll( "\"", "\\\\\"" ) ); String flashContent = TemplateUtil.applyTemplate( getFlashFragment(), props, null ); log.debug( "html_fragment=" + flashContent ); if ( this.isDefinedOutput( "html_fragment" ) ) { this.setOutputValue( "html_fragment", flashContent ); } if ( this.isDefinedOutput( "image-tag" ) ) { this.setOutputValue( "image-tag", flashContent ); } return true; } OpenFlashChartComponent(); void done(); boolean init(); boolean validateSystemSettings(); Log getLogger(); @Override void inputMissingError( String paramName ); static String CHART_NODE_LOC; }
@Test public void testExecuteAction() throws Exception { OpenFlashChartComponent fcc = spy( flashChartComponent ); doReturn( resultSet ).when( fcc ).getInputValue( "chart-data" ); when( resultSet.isScrollable() ).thenReturn( false ); when( resultSet.memoryCopy() ).thenReturn( memoryResultSet ); doReturn( "20" ).when( fcc ).getInputStringValue( "width" ); doReturn( "30" ).when( fcc ).getInputStringValue( "height" ); doReturn( "http: doReturn( "open-flash-chart-full-embedded-font.swf" ).when( fcc ).getInputStringValue( "ofc_lib_name" ); doReturn( null ).when( fcc ).getInputStringValue( "chart-attributes" ); doReturn( componentDefinitionNode ).when( fcc ).getComponentDefinition( true ); when( componentDefinitionNode.selectSingleNode( "chart-attributes" ) ).thenReturn( chartAttributesNode ); doReturn( "" ).when( fcc ).generateChartJson( memoryResultSet, chartAttributesNode, false ); boolean action = fcc.executeAction(); assertTrue( action ); verify( resultSet ).close(); }