use of org.pentaho.metastore.api.IMetaStoreElementType in project pentaho-kettle by pentaho.
the class DatabaseMetaStoreUtil method getDatabaseElements.
public static List<DatabaseMeta> getDatabaseElements(IMetaStore metaStore) throws MetaStoreException {
List<DatabaseMeta> databases = new ArrayList<DatabaseMeta>();
// If the data type doesn't exist, it's an empty list...
//
IMetaStoreElementType elementType = metaStore.getElementTypeByName(PentahoDefaults.NAMESPACE, PentahoDefaults.DATABASE_CONNECTION_ELEMENT_TYPE_NAME);
if (elementType == null) {
return databases;
}
List<IMetaStoreElement> elements = metaStore.getElements(PentahoDefaults.NAMESPACE, elementType);
for (IMetaStoreElement element : elements) {
try {
DatabaseMeta databaseMeta = loadDatabaseMetaFromDatabaseElement(metaStore, element);
databases.add(databaseMeta);
} catch (Exception e) {
throw new MetaStoreException("Unable to load database from element with name '" + element.getName() + "' and type '" + elementType.getName() + "'", e);
}
}
return databases;
}
use of org.pentaho.metastore.api.IMetaStoreElementType in project pentaho-kettle by pentaho.
the class DatabaseMetaStoreUtil method populateDatabaseElement.
public static IMetaStoreElement populateDatabaseElement(IMetaStore metaStore, DatabaseMeta databaseMeta) throws MetaStoreException {
if (!metaStore.namespaceExists(PentahoDefaults.NAMESPACE)) {
throw new MetaStoreException("Namespace '" + PentahoDefaults.NAMESPACE + "' doesn't exist.");
}
// If the data type doesn't exist, error out...
//
IMetaStoreElementType elementType = metaStore.getElementTypeByName(PentahoDefaults.NAMESPACE, PentahoDefaults.DATABASE_CONNECTION_ELEMENT_TYPE_NAME);
if (elementType == null) {
throw new MetaStoreException("Unable to find the database connection type");
}
elementType = populateDatabaseElementType(metaStore);
// generate a new database element and populate it with metadata
//
IMetaStoreElement element = metaStore.newElement(elementType, databaseMeta.getName(), null);
element.addChild(metaStore.newAttribute(MetaStoreConst.DB_ATTR_ID_PLUGIN_ID, databaseMeta.getPluginId()));
element.setName(databaseMeta.getName());
element.addChild(metaStore.newAttribute(MetaStoreConst.DB_ATTR_ID_DESCRIPTION, databaseMeta.getDescription()));
element.addChild(metaStore.newAttribute(MetaStoreConst.DB_ATTR_ID_ACCESS_TYPE, databaseMeta.getAccessTypeDesc()));
element.addChild(metaStore.newAttribute(MetaStoreConst.DB_ATTR_ID_HOSTNAME, databaseMeta.getHostname()));
element.addChild(metaStore.newAttribute(MetaStoreConst.DB_ATTR_ID_PORT, databaseMeta.getDatabasePortNumberString()));
element.addChild(metaStore.newAttribute(MetaStoreConst.DB_ATTR_ID_DATABASE_NAME, databaseMeta.getDatabaseName()));
element.addChild(metaStore.newAttribute(MetaStoreConst.DB_ATTR_ID_USERNAME, databaseMeta.getUsername()));
element.addChild(metaStore.newAttribute(MetaStoreConst.DB_ATTR_ID_PASSWORD, metaStore.getTwoWayPasswordEncoder().encode(databaseMeta.getPassword())));
element.addChild(metaStore.newAttribute(MetaStoreConst.DB_ATTR_ID_SERVERNAME, databaseMeta.getServername()));
element.addChild(metaStore.newAttribute(MetaStoreConst.DB_ATTR_ID_DATA_TABLESPACE, databaseMeta.getDataTablespace()));
element.addChild(metaStore.newAttribute(MetaStoreConst.DB_ATTR_ID_INDEX_TABLESPACE, databaseMeta.getIndexTablespace()));
IMetaStoreAttribute attributesChild = metaStore.newAttribute(MetaStoreConst.DB_ATTR_ID_ATTRIBUTES, null);
element.addChild(attributesChild);
// Now add a list of all the attributes set on the database connection...
//
Properties attributes = databaseMeta.getAttributes();
Enumeration<Object> keys = databaseMeta.getAttributes().keys();
while (keys.hasMoreElements()) {
String code = (String) keys.nextElement();
String attribute = (String) attributes.get(code);
// Add it to the attributes child
//
attributesChild.addChild(metaStore.newAttribute(code, attribute));
}
// Extra information for 3rd-party tools:
//
// The driver class
//
element.addChild(metaStore.newAttribute(MetaStoreConst.DB_ATTR_DRIVER_CLASS, databaseMeta.getDriverClass()));
//
try {
element.addChild(metaStore.newAttribute(MetaStoreConst.DB_ATTR_JDBC_URL, databaseMeta.getURL()));
} catch (Exception e) {
throw new MetaStoreException("Unable to assemble URL from database '" + databaseMeta.getName() + "'", e);
}
return element;
}
use of org.pentaho.metastore.api.IMetaStoreElementType in project pentaho-kettle by pentaho.
the class MetaStoreTestBase method testFunctionality.
public void testFunctionality(IMetaStore metaStore) throws MetaStoreException {
if (!metaStore.namespaceExists(namespace)) {
metaStore.createNamespace(namespace);
}
List<String> namespaces = metaStore.getNamespaces();
assertEquals(1, namespaces.size());
IMetaStoreElementType elementType = metaStore.newElementType(namespace);
elementType.setName(SHARED_DIMENSION_NAME);
elementType.setDescription(SHARED_DIMENSION_DESCRIPTION);
metaStore.createElementType(namespace, elementType);
assertNotNull(elementType.getId());
List<IMetaStoreElementType> elementTypes = metaStore.getElementTypes(namespace);
assertEquals(1, elementTypes.size());
try {
metaStore.createElementType(namespace, elementType);
fail("Duplicate creation error expected!");
} catch (MetaStoreElementTypeExistsException e) {
// OK!
} catch (MetaStoreException e) {
e.printStackTrace();
fail("Create exception needs to be MetaStoreDataTypesExistException");
}
//
try {
metaStore.deleteNamespace(namespace);
fail("Expected error while deleting namespace with content!");
} catch (MetaStoreDependenciesExistsException e) {
// OK!
List<String> dependencies = e.getDependencies();
assertNotNull(dependencies);
assertEquals(1, dependencies.size());
assertEquals(elementType.getId(), dependencies.get(0));
}
IMetaStoreElement customerDimension = generateCustomerDimensionElement(metaStore, elementType);
IMetaStoreElementOwner elementOwner = customerDimension.getOwner();
assertNotNull(elementOwner);
assertEquals("joe", elementOwner.getName());
assertEquals(MetaStoreElementOwnerType.USER, elementOwner.getOwnerType());
metaStore.createElement(namespace, elementType, customerDimension);
assertNotNull(customerDimension.getId());
List<IMetaStoreElement> elements = metaStore.getElements(namespace, elementType);
assertEquals(1, elements.size());
assertNotNull(elements.get(0));
assertEquals(CUSTOMER_DIMENSION_NAME, elements.get(0).getName());
//
try {
metaStore.deleteElementType(namespace, elementType);
fail("Expected error while deleting data type with content!");
} catch (MetaStoreDependenciesExistsException e) {
// OK!
List<String> dependencies = e.getDependencies();
assertNotNull(dependencies);
assertEquals(1, dependencies.size());
assertEquals(customerDimension.getId(), dependencies.get(0));
}
// Some lookup-by-name tests...
//
assertNotNull(metaStore.getElementTypeByName(namespace, SHARED_DIMENSION_NAME));
assertNotNull(metaStore.getElementByName(namespace, elementType, CUSTOMER_DIMENSION_NAME));
// Clean up shop!
//
metaStore.deleteElement(namespace, elementType, customerDimension.getId());
elements = metaStore.getElements(namespace, elementType);
assertEquals(0, elements.size());
metaStore.deleteElementType(namespace, elementType);
elementTypes = metaStore.getElementTypes(namespace);
assertEquals(0, elementTypes.size());
metaStore.deleteNamespace(namespace);
namespaces = metaStore.getNamespaces();
assertEquals(0, namespaces.size());
}
use of org.pentaho.metastore.api.IMetaStoreElementType in project pentaho-kettle by pentaho.
the class SharedDimensionMetaStoreUtilTest method testSaveSharedDimension.
@Test
public void testSaveSharedDimension() throws Exception {
final String locale = Locale.US.toString();
final IMetaStore metaStore = mock(IMetaStore.class);
final IMetaStoreElementType metaStoreElementType = mock(IMetaStoreElementType.class);
when(metaStore.newElementType(anyString())).thenReturn(metaStoreElementType);
final IMetaStoreElement metaStoreElement = mock(IMetaStoreElement.class);
final String sdId = "sdId";
when(metaStore.getElement(anyString(), eq(metaStoreElementType), eq(sdId))).thenReturn(metaStoreElement);
final LogicalTable sharedDimension = mock(LogicalTable.class);
final String sdName = "sdName";
when(sharedDimension.getName(eq(locale))).thenReturn(sdName);
when(sharedDimension.getId()).thenReturn(sdId);
SharedDimensionMetaStoreUtil.saveSharedDimension(metaStore, sharedDimension, locale);
verify(metaStoreElement, times(1)).setElementType(eq(metaStoreElementType));
verify(metaStoreElement, times(1)).setName(eq(sdName));
verify(metaStoreElement, times(2)).addChild(any(IMetaStoreAttribute.class));
verify(metaStore, times(1)).updateElement(anyString(), eq(metaStoreElementType), eq(sdId), eq(metaStoreElement));
verify(sharedDimension, times(1)).setId(anyString());
}
use of org.pentaho.metastore.api.IMetaStoreElementType in project pentaho-kettle by pentaho.
the class StarDomainMetaStoreUtilTest method testLoadStarDomain.
@Test
public void testLoadStarDomain() throws Exception {
final String id = "id";
final String msName = "MSName";
when(metaStore.getName()).thenReturn(msName);
final DelegatingMetaStore delegatingMetaStore = spy(new DelegatingMetaStore(metaStore));
delegatingMetaStore.setActiveMetaStoreName(msName);
doAnswer(new Answer<IMetaStoreElementType>() {
@Override
public IMetaStoreElementType answer(InvocationOnMock invocationOnMock) throws Throwable {
return metaStore.getElementTypeByName((String) invocationOnMock.getArguments()[0], (String) invocationOnMock.getArguments()[1]);
}
}).when(delegatingMetaStore).getElementTypeByName(anyString(), anyString());
assertNull(StarDomainMetaStoreUtil.loadStarDomain(delegatingMetaStore, id));
final IMetaStoreElement metaStoreElement = mock(IMetaStoreElement.class);
final String name = "name";
when(metaStoreElement.getName()).thenReturn(name);
doReturn(metaStoreElement).when(delegatingMetaStore).getElement(anyString(), eq(metaStoreElementType), eq(id));
final StarDomain starDomain = StarDomainMetaStoreUtil.loadStarDomain(delegatingMetaStore, id);
assertEquals(id, starDomain.getObjectId().getId());
assertEquals(name, starDomain.getName());
}
Aggregations