Java Code Examples for org.pentaho.metadata.model.Domain#addLogicalModel()

The following examples show how to use org.pentaho.metadata.model.Domain#addLogicalModel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryXmlHelperTest.java    From pentaho-metadata with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Before
public void init() throws Exception {
  helper = new QueryXmlHelper();
  Domain domain = TestHelper.getBasicDomain();
  LogicalModel model = TestHelper.buildDefaultModel();
  domain.addLogicalModel( model );
  model.setId( "MODEL1" );
  query = new Query( domain, model );

  LogicalColumn column = new LogicalColumn();
  column.setId( "LC_Test_Column1" );
  LogicalColumn column2 = new LogicalColumn();
  column2.setId( "LC_Test_Column2" );
  Category category = new Category();
  category.getLogicalColumns().add( column );
  query.getLogicalModel().getCategories().add( category );

  LogicalTable lt = new LogicalTable();
  lt.getLogicalColumns().add( column );
  lt.getLogicalColumns().add( column2 );
  query.getLogicalModel().getLogicalTables().add( lt );

  documentBuilderFactory = DocumentBuilderFactory.newInstance();
  db = documentBuilderFactory.newDocumentBuilder();
  doc = db.newDocument();
  metadataDomainRepository = new InMemoryMetadataDomainRepository();
  metadataDomainRepository.storeDomain( domain, true );
}
 
Example 2
Source File: QueryXmlHelperTest.java    From pentaho-metadata with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testFromXML() throws Exception {
  Domain domain = TestHelper.getBasicDomain();
  LogicalModel model = TestHelper.buildDefaultModel();
  domain.addLogicalModel( model );
  model.setId( "MODEL2" );
  Query query = new Query( domain, model );
  String xml = helper.toXML( query );
  try {
    query = helper.fromXML( metadataDomainRepository, xml );
    fail();
  } catch ( PentahoMetadataException e ) {
    // expected
  }
}
 
Example 3
Source File: AutoModeler.java    From pentaho-metadata with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Domain generateDomain( final ImportStrategy importStrategy ) throws PentahoMetadataException {
  Domain domain = new Domain();
  domain.setId( modelName );

  List<LocaleType> locales = new ArrayList<LocaleType>();
  locales.add( new LocaleType( "en_US", "English (US)" ) ); //$NON-NLS-1$ //$NON-NLS-2$
  domain.setLocales( locales );

  SqlPhysicalModel physicalModel = new SqlPhysicalModel();
  physicalModel.setId( databaseMeta.getName() );
  physicalModel.setDatasource( ThinModelConverter.convertFromLegacy( databaseMeta ) );

  Database database = database();

  try {
    // Add the database connection to the empty schema...
    //
    domain.addPhysicalModel( physicalModel );

    // Also add a model with the same name as the model name...
    //
    String bmID = Util.getLogicalModelIdPrefix() + "_" + modelName.replaceAll( " ", "_" ).toUpperCase(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    LogicalModel logicalModel = new LogicalModel();
    logicalModel.setId( bmID );
    domain.addLogicalModel( logicalModel );

    // Connect to the database...
    //
    database.connect();

    // clear the cache
    DBCache.getInstance().clear( databaseMeta.getName() );

    for ( int i = 0; i < tableNames.length; i++ ) {
      SchemaTable schemaTable = tableNames[i];

      // Import the specified tables and turn them into PhysicalTable
      // objects...
      //
      SqlPhysicalTable physicalTable =
          PhysicalTableImporter.importTableDefinition( database, schemaTable.getSchemaName(), schemaTable
          .getTableName(), locale, importStrategy );
      physicalModel.addPhysicalTable( physicalTable );

      // At the same time, we will create a business table and add that to the
      // business model...
      //
      LogicalTable businessTable = createBusinessTable( physicalTable, locale );
      logicalModel.addLogicalTable( businessTable );
    }
  } catch ( Exception e ) {
    // For the unexpected stuff, just throw the exception upstairs.
    //
    throw new PentahoMetadataException( e );
  } finally {
    // Make sure to close the connection
    //
    database.disconnect();
  }

  return domain;
}
 
Example 4
Source File: ThinModelIT.java    From pentaho-metadata with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void testSerializeSqlPhysicalModel() {

  String locale = LocaleHelper.getLocale().toString();

  SqlPhysicalModel model = new SqlPhysicalModel();
  SqlDataSource dataSource = new SqlDataSource();
  dataSource.setDatabaseName( "SampleData" );
  model.setDatasource( dataSource );
  SqlPhysicalTable table = new SqlPhysicalTable( model );
  model.getPhysicalTables().add( table );
  table.setTargetTableType( TargetTableType.INLINE_SQL );
  table.setTargetTable( "select * from customers" );

  SqlPhysicalColumn column = new SqlPhysicalColumn( table );
  column.setTargetColumn( "customername" );
  column.setName( new LocalizedString( locale, "Customer Name" ) );
  column.setDescription( new LocalizedString( locale, "Customer Name Desc" ) );
  column.setDataType( DataType.STRING );

  table.getPhysicalColumns().add( column );

  LogicalModel logicalModel = new LogicalModel();
  model.setId( "MODEL" );
  model.setName( new LocalizedString( locale, "My Model" ) );
  model.setDescription( new LocalizedString( locale, "A Description of the Model" ) );

  LogicalTable logicalTable = new LogicalTable();
  logicalTable.setPhysicalTable( table );

  logicalModel.getLogicalTables().add( logicalTable );

  LogicalColumn logicalColumn = new LogicalColumn();
  logicalColumn.setId( "LC_CUSTOMERNAME" );
  logicalColumn.setPhysicalColumn( column );

  logicalTable.addLogicalColumn( logicalColumn );

  Category mainCategory = new Category();
  mainCategory.setId( "CATEGORY" );
  mainCategory.setName( new LocalizedString( locale, "Category" ) );
  mainCategory.addLogicalColumn( logicalColumn );

  logicalModel.getCategories().add( mainCategory );

  Domain domain = new Domain();
  domain.addPhysicalModel( model );
  domain.addLogicalModel( logicalModel );

  // basic tests
  SerializationService service = new SerializationService();

  String xml = service.serializeDomain( domain );

  // System.out.println(xml);

  Domain domain2 = service.deserializeDomain( xml );

  Assert.assertEquals( 1, domain2.getPhysicalModels().size() );
  SqlPhysicalModel model2 = (SqlPhysicalModel) domain2.getPhysicalModels().get( 0 );
  Assert.assertEquals( "SampleData", model2.getDatasource().getDatabaseName() );
  Assert.assertEquals( 1, model.getPhysicalTables().size() );
  Assert.assertEquals( TargetTableType.INLINE_SQL, model.getPhysicalTables().get( 0 ).getTargetTableType() );

  Assert.assertEquals( 1, domain.getLogicalModels().size() );
  Assert.assertEquals( 1, domain.getLogicalModels().get( 0 ).getCategories().size() );
  Assert.assertEquals( 1, domain.getLogicalModels().get( 0 ).getCategories().get( 0 ).getLogicalColumns().size() );
  Assert.assertEquals( domain.getLogicalModels().get( 0 ).getLogicalTables().get( 0 ).getLogicalColumns().get( 0 ),
      domain.getLogicalModels().get( 0 ).getCategories().get( 0 ).getLogicalColumns().get( 0 ) );
  Assert.assertEquals( "Customer Name", domain.getLogicalModels().get( 0 ).getCategories().get( 0 )
      .getLogicalColumns().get( 0 ).getName().getString( "en_US" ) );
  Assert.assertEquals( "Customer Name Desc", domain.getLogicalModels().get( 0 ).getCategories().get( 0 )
      .getLogicalColumns().get( 0 ).getDescription().getString( "en_US" ) );

}
 
Example 5
Source File: TestHelper.java    From pentaho-metadata with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static Domain getBasicDomain() {

    String locale = LocaleHelper.getLocale().toString();

    SqlPhysicalModel model = new SqlPhysicalModel();
    SqlDataSource dataSource = new SqlDataSource();
    dataSource.setDatabaseName( "SampleData" );
    model.setDatasource( dataSource );
    SqlPhysicalTable table = new SqlPhysicalTable( model );
    table.setId( "PT1" );
    model.getPhysicalTables().add( table );
    table.setTargetTableType( TargetTableType.INLINE_SQL );
    table.setTargetTable( "select * from customers" );

    SqlPhysicalColumn column = new SqlPhysicalColumn( table );
    column.setId( "PC1" );
    column.setTargetColumn( "customername" );
    column.setName( new LocalizedString( locale, "Customer Name" ) );
    column.setDescription( new LocalizedString( locale, "Customer Name Desc" ) );
    column.setDataType( DataType.STRING );
    table.getPhysicalColumns().add( column );

    LogicalModel logicalModel = new LogicalModel();
    logicalModel.setId( "MODEL" );
    logicalModel.setName( new LocalizedString( locale, "My Model" ) );
    logicalModel.setDescription( new LocalizedString( locale, "A Description of the Model" ) );

    LogicalTable logicalTable = new LogicalTable();
    logicalTable.setId( "LT" );
    logicalTable.setPhysicalTable( table );
    logicalTable.setLogicalModel( logicalModel );

    logicalModel.getLogicalTables().add( logicalTable );

    LogicalColumn logicalColumn = new LogicalColumn();
    logicalColumn.setId( "LC_CUSTOMERNAME" );
    logicalColumn.setPhysicalColumn( column );
    logicalColumn.setLogicalTable( logicalTable );
    logicalTable.addLogicalColumn( logicalColumn );

    Category mainCategory = new Category( logicalModel );
    mainCategory.setId( "CATEGORY" );
    mainCategory.setName( new LocalizedString( locale, "Category" ) );
    mainCategory.addLogicalColumn( logicalColumn );

    logicalModel.getCategories().add( mainCategory );

    Domain domain = new Domain();
    domain.setId( "DOMAIN" );
    domain.addPhysicalModel( model );
    domain.addLogicalModel( logicalModel );

    return domain;
  }
 
Example 6
Source File: RepositoryIT.java    From pentaho-metadata with GNU Lesser General Public License v2.1 2 votes vote down vote up
@Test
public void testFileBasedRepository() throws Exception {

  File file = new File( "target/test/DOMAIN.domain.xml" );

  if ( file.exists() ) {
    file.delete();
  }

  Assert.assertTrue( !file.exists() );

  Domain domain = TestHelper.getBasicDomain();
  LogicalModel model2 = (LogicalModel) domain.getLogicalModels().get( 0 ).clone();
  model2.setId( "MODEL2" );

  FileBasedMetadataDomainRepository repo = new FileBasedMetadataDomainRepository();
  repo.setDomainFolder( "target/test" );
  repo.storeDomain( domain, false );

  Assert.assertTrue( file.exists() );
  long fileSize = file.length();

  domain.addLogicalModel( model2 );

  try {
    repo.storeDomain( domain, false );
    Assert.fail();
  } catch ( Exception e ) {
    Assert.assertTrue( e instanceof DomainAlreadyExistsException );
  }

  repo.storeDomain( domain, true );

  long newFileSize = file.length();

  Assert.assertNotSame( fileSize, newFileSize );

  Domain domain2 = repo.getDomain( domain.getId() );
  Assert.assertEquals( 2, domain2.getLogicalModels().size() );

  repo.removeModel( domain.getId(), "MODEL" );

  long fileSize3 = file.length();

  Assert.assertNotSame( newFileSize, fileSize3 );

  Domain domain3 = repo.getDomain( domain.getId() );

  Assert.assertEquals( 1, domain3.getLogicalModels().size() );

  repo.removeModel( domain.getId(), "MODEL2" );

  Assert.assertTrue( !file.exists() );

  Domain nullDomain = repo.getDomain( domain.getId() );

  Assert.assertNull( nullDomain );

}