使用BIRT API创建交叉报表1
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
/******************************************************************************* * Copyright (c) 2004 Actuate Corporation. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Actuate Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.birt.report.item.crosstab.core; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import junit.framework.TestCase; import org.eclipse.birt.report.item.crosstab.core.de.CrosstabReportItemHandle; import org.eclipse.birt.report.item.crosstab.core.de.DimensionViewHandle; import org.eclipse.birt.report.item.crosstab.core.util.CrosstabExtendedItemFactory; import org.eclipse.birt.report.item.crosstab.core.util.CrosstabUtil; import org.eclipse.birt.report.model.api.DesignConfig; import org.eclipse.birt.report.model.api.DesignEngine; import org.eclipse.birt.report.model.api.ElementFactory; import org.eclipse.birt.report.model.api.IDesignEngine; import org.eclipse.birt.report.model.api.ModuleHandle; import org.eclipse.birt.report.model.api.PropertyHandle; import org.eclipse.birt.report.model.api.ReportDesignHandle; import org.eclipse.birt.report.model.api.SessionHandle; import org.eclipse.birt.report.model.api.activity.SemanticException; import org.eclipse.birt.report.model.api.olap.CubeHandle; import org.eclipse.birt.report.model.api.olap.DimensionHandle; import org.eclipse.birt.report.model.api.olap.HierarchyHandle; import org.eclipse.birt.report.model.api.olap.LevelHandle; import org.eclipse.birt.report.model.api.olap.MeasureGroupHandle; import org.eclipse.birt.report.model.api.olap.MeasureHandle; import org.eclipse.birt.report.model.elements.interfaces.IDimensionModel; import org.eclipse.birt.report.model.i18n.ThreadResources; import com.ibm.icu.util.ULocale; /** * Base test case. * */ public class BaseTestCase extends TestCase { /** * design handle */ protected ReportDesignHandle designHandle = null; private static final String INPUT_FOLDER = "input/";//$NON-NLS-1$ private static final String GOLDEN_FOLDER = "golden/";//$NON-NLS-1$ /** * Byte array output stream. */ protected ByteArrayOutputStream os = null; protected void setUp( ) throws Exception { super.setUp( ); } protected void tearDown( ) throws Exception { super.tearDown( ); if ( designHandle != null ) { designHandle.close( ); designHandle = null; } } /** * Opens report design. * * @param fileName * @throws Exception */ protected void openDesign( String fileName ) throws Exception { ThreadResources.setLocale( ULocale.ENGLISH ); IDesignEngine designEngine = new DesignEngine( new DesignConfig( ) ); // MetaDataDictionary.reset( ); // initialize the metadata. designEngine.getMetaData( ); SessionHandle sh = designEngine.newSessionHandle( ULocale.getDefault( ) ); designHandle = sh.openDesign( getResource( INPUT_FOLDER + fileName ) ); } private ReportDesignHandle loadGolden( String fileName ) throws Exception { ThreadResources.setLocale( ULocale.ENGLISH ); IDesignEngine designEngine = new DesignEngine( new DesignConfig( ) ); // MetaDataDictionary.reset( ); // initialize the metadata. designEngine.getMetaData( ); SessionHandle sh = designEngine.newSessionHandle( ULocale.getDefault( ) ); return sh.openDesign( getResource( GOLDEN_FOLDER + fileName ) ); } /** * Create report design. * * @throws Exception */ protected void createDesign( ) throws Exception { ThreadResources.setLocale( ULocale.ENGLISH ); IDesignEngine designEngine = new DesignEngine( new DesignConfig( ) ); // MetaDataDictionary.reset( ); // initialize the metadata. designEngine.getMetaData( ); SessionHandle sh = designEngine.newSessionHandle( ULocale.getDefault( ) ); designHandle = sh.createDesign( ); } /** * Gets resource file path. * * @param fileName * @return * @throws Exception */ private String getResource( String fileName ) throws Exception { if ( fileName == null ) return null; URL url = getClass( ).getResource( fileName ); if ( url == null ) return null; return url.toString( ); } /** * Gets the input stream of the given name resources. */ protected InputStream getResourceAStream( String name ) { return this.getClass( ).getResourceAsStream( name ); } /** * Compares the two text files. * * @param goldenReader * the reader for golden file * @param outputReader * the reader for output file * @return true if two text files are same. * @throws Exception * if any exception */ private boolean compareFile( Reader goldenReader, Reader outputReader ) throws Exception { StringBuffer errorText = new StringBuffer( ); BufferedReader lineReaderA = null; BufferedReader lineReaderB = null; boolean same = true; int lineNo = 1; try { lineReaderA = new BufferedReader( goldenReader ); lineReaderB = new BufferedReader( outputReader ); String strA = lineReaderA.readLine( ).trim( ); String strB = lineReaderB.readLine( ).trim( ); while ( strA != null ) { // ignore id part strA = strA.replaceAll( "id=\"\\d+\"", "" ); //$NON-NLS-1$ //$NON-NLS-2$ strB = strB.replaceAll( "id=\"\\d+\"", "" ); //$NON-NLS-1$ //$NON-NLS-2$ same = strA.trim( ).equals( strB.trim( ) ); if ( !same ) { StringBuffer message = new StringBuffer( ); message.append( "line=" ); //$NON-NLS-1$ message.append( lineNo ); message.append( " is different:\n" ); //$NON-NLS-1$ message.append( " The line from golden file: " );//$NON-NLS-1$ message.append( strA ); message.append( "\n" );//$NON-NLS-1$ message.append( " The line from output file: " );//$NON-NLS-1$ message.append( strB ); message.append( "\n" );//$NON-NLS-1$ throw new Exception( message.toString( ) ); } strA = lineReaderA.readLine( ); strB = lineReaderB.readLine( ); lineNo++; } same = strB == null; } finally { try { if ( lineReaderA != null ) lineReaderA.close( ); if ( lineReaderB != null ) lineReaderB.close( ); } catch ( Exception e ) { lineReaderA = null; lineReaderB = null; errorText.append( e.toString( ) ); throw new Exception( errorText.toString( ) ); } } return same; } /** * Compares two text file. The comparison will ignore the line containing * "modificationDate". * * @param goldenFileName * the 1st file name to be compared. * @param os * the 2nd output stream to be compared. * @return true if two text files are same char by char * @throws Exception * if any exception. */ protected boolean compareFile( String goldenFileName ) throws Exception { if ( os == null ) return false; // String tmpGoldenFileName = GOLDEN_FOLDER + goldenFileName; ByteArrayOutputStream baos = new ByteArrayOutputStream( ); ReportDesignHandle golden = loadGolden( goldenFileName ); golden.serialize( baos ); baos.flush( ); // InputStream streamA = getResourceAStream( tmpGoldenFileName ); InputStream streamA = new ByteArrayInputStream( baos.toByteArray( ) ); InputStream streamB = new ByteArrayInputStream( os.toByteArray( ) ); InputStreamReader readerA = new InputStreamReader( streamA ); InputStreamReader readerB = new InputStreamReader( streamB ); boolean ok = true; try { ok = compareFile( readerA, readerB ); } catch ( Exception e ) { throw e; } return ok; } /** * Eventually, this method will call * {@link ReportDesignHandle#serialize(java.io.OutputStream)}to save the * output file of some unit test. * * @param moduleHandle * the module to save, either a report design or a library * @throws IOException * if error occurs while saving the file. */ protected void save( ModuleHandle moduleHandle ) throws IOException { os = new ByteArrayOutputStream( ); if ( moduleHandle != null ) moduleHandle.serialize( os ); os.close( ); } protected void save( ModuleHandle moduleHandle, String path ) throws IOException { moduleHandle.saveAs( path ); } /** * Create simple cube handle. * * @param module * @return */ protected CubeHandle prepareCube( ) throws SemanticException { ElementFactory factory = designHandle.getElementFactory( ); // create cube CubeHandle cubeHandle = factory.newTabularCube( "Cube_Test_1" );//$NON-NLS-1$ designHandle.getCubes( ).add( cubeHandle ); DimensionHandle dimensionHandle = factory.newTabularDimension( "Customer" );//$NON-NLS-1$ cubeHandle.add( CubeHandle.DIMENSIONS_PROP, dimensionHandle ); HierarchyHandle hierarchyHandle = factory.newTabularHierarchy( "Hierarchy" );//$NON-NLS-1$ dimensionHandle.add( DimensionHandle.HIERARCHIES_PROP, hierarchyHandle ); LevelHandle levelHandle = factory.newTabularLevel( dimensionHandle, "CUSTOMER_SEX" );//$NON-NLS-1$ hierarchyHandle.add( HierarchyHandle.LEVELS_PROP, levelHandle ); levelHandle = factory.newTabularLevel( dimensionHandle, "CUSTOMER_REGION" );//$NON-NLS-1$ hierarchyHandle.add( HierarchyHandle.LEVELS_PROP, levelHandle ); DimensionHandle dimensionHandle2 = factory.newTabularDimension( "Product" );//$NON-NLS-1$ cubeHandle.add( CubeHandle.DIMENSIONS_PROP, dimensionHandle2 ); HierarchyHandle hierarchyHandle2 = factory.newTabularHierarchy( "Hierarchy2" );//$NON-NLS-1$ dimensionHandle2.add( DimensionHandle.HIERARCHIES_PROP, hierarchyHandle2 ); LevelHandle levelHandle2 = factory.newTabularLevel( dimensionHandle2, "PRODUCT_TYPE" );//$NON-NLS-1$ hierarchyHandle2.add( HierarchyHandle.LEVELS_PROP, levelHandle2 ); levelHandle2 = factory.newTabularLevel( dimensionHandle2, "PRODUCT_NAME" );//$NON-NLS-1$ hierarchyHandle2.add( HierarchyHandle.LEVELS_PROP, levelHandle2 ); MeasureGroupHandle groupHandle = factory.newTabularMeasureGroup( "measure group" );//$NON-NLS-1$ cubeHandle.add( CubeHandle.MEASURE_GROUPS_PROP, groupHandle ); MeasureHandle measureHandle = factory.newTabularMeasure( "QUANTITY_PRICE" );//$NON-NLS-1$ groupHandle.add( MeasureGroupHandle.MEASURES_PROP, measureHandle ); measureHandle = factory.newTabularMeasure( "QUANTITY" );//$NON-NLS-1$ groupHandle.add( MeasureGroupHandle.MEASURES_PROP, measureHandle ); measureHandle = factory.newTabularMeasure( "QUANTITY_NUMBER" );//$NON-NLS-1$ groupHandle.add( MeasureGroupHandle.MEASURES_PROP, measureHandle ); measureHandle = factory.newTabularMeasure( "QUANTITY_SIZE" );//$NON-NLS-1$ groupHandle.add( MeasureGroupHandle.MEASURES_PROP, measureHandle ); return cubeHandle; } protected CrosstabReportItemHandle createSimpleCrosstab( ModuleHandle module ) { assert designHandle != null; try { // create cube CubeHandle cubeHandle = prepareCube( ); DimensionHandle dimensionHandle = cubeHandle.getDimension( "Customer" );//$NON-NLS-1$ DimensionHandle dimensionHandle2 = cubeHandle.getDimension( "Product" );//$NON-NLS-1$ PropertyHandle propHandle = dimensionHandle.getPropertyHandle( IDimensionModel.HIERARCHIES_PROP ); HierarchyHandle hierarchyHandle = (HierarchyHandle) propHandle.get( 0 ); propHandle = dimensionHandle2.getPropertyHandle( IDimensionModel.HIERARCHIES_PROP ); HierarchyHandle hierarchyHandle2 = (HierarchyHandle) propHandle.get( 0 ); // create cross tab CrosstabReportItemHandle crosstabItem = (CrosstabReportItemHandle) CrosstabUtil.getReportItem( CrosstabExtendedItemFactory.createCrosstabReportItem( module, cubeHandle, null ) ); DimensionViewHandle dimensionViewHandle = crosstabItem.insertDimension( dimensionHandle, ICrosstabConstants.ROW_AXIS_TYPE, -1 ); dimensionViewHandle.insertLevel( hierarchyHandle.getLevel( 0 ), -1 ); dimensionViewHandle.insertLevel( hierarchyHandle.getLevel( 1 ), -1 ); DimensionViewHandle dimensionViewHandle2 = crosstabItem.insertDimension( dimensionHandle2, ICrosstabConstants.COLUMN_AXIS_TYPE, -1 ); dimensionViewHandle2.insertLevel( hierarchyHandle2.getLevel( 0 ), -1 ); dimensionViewHandle2.insertLevel( hierarchyHandle2.getLevel( 1 ), -1 ); crosstabItem.insertMeasure( cubeHandle.getMeasure( "QUANTITY_PRICE" ), -1 );//$NON-NLS-1$ crosstabItem.insertMeasure( cubeHandle.getMeasure( "QUANTITY" ), -1 );//$NON-NLS-1$ crosstabItem.insertMeasure( cubeHandle.getMeasure( "QUANTITY_NUMBER" ), -1 );//$NON-NLS-1$ crosstabItem.insertMeasure( cubeHandle.getMeasure( "QUANTITY_SIZE" ), -1 );//$NON-NLS-1$ return crosstabItem; } catch ( SemanticException e ) { e.printStackTrace( ); } return null; } } |
