use of org.pentaho.metadata.model.LogicalModel in project data-access by pentaho.
the class DSWDatasourceServiceImplTest method setUp.
@Before
public void setUp() throws Exception {
SqlDataSource dataSource = new SqlDataSource();
dataSource.setDatabaseName(CONNECTION_NAME);
SqlPhysicalTable sqlTable = new SqlPhysicalTable();
sqlTable.setTargetTable(VALID_QUERY);
SqlPhysicalModel sqlModel = new SqlPhysicalModel();
sqlModel.addPhysicalTable(sqlTable);
sqlModel.setDatasource(dataSource);
analysisModel = new LogicalModel();
analysisModel.setId(LOGICAL_MODEL_ID_ANALYSIS);
analysisModel.setProperty(DSWDatasourceServiceImpl.LM_PROP_VISIBLE, LOGICAL_MODEL_CONTEXTNAME);
reportingModel = new LogicalModel();
reportingModel.setId(LOGICAL_MODEL_ID_REPORTING);
domain2Models = new Domain();
domain2Models.setId(DOMAIN_ID_2MODELS);
domain2Models.addLogicalModel(analysisModel);
domain2Models.addLogicalModel(reportingModel);
domain2Models.setLocales(Arrays.asList(new LocaleType("en_US", "Test locale")));
domain2Models.addPhysicalModel(sqlModel);
Set<String> domains = new TreeSet<String>();
domains.add(DOMAIN_ID_2MODELS);
doReturn(domain2Models).when(domainRepository).getDomain(DOMAIN_ID_2MODELS);
doReturn(domains).when(domainRepository).getDomainIds();
doAnswer(new Answer<Object>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
final String modelId = (String) invocation.getArguments()[1];
final LogicalModel modelToRemove = domain2Models.findLogicalModel(modelId);
domain2Models.getLogicalModels().remove(modelToRemove);
return null;
}
}).when(domainRepository).removeModel(anyString(), anyString());
workspace2Models = mock(ModelerWorkspace.class);
when(workspace2Models.getLogicalModel(ModelerPerspective.ANALYSIS)).thenReturn(analysisModel);
when(workspace2Models.getLogicalModel(ModelerPerspective.REPORTING)).thenReturn(reportingModel);
dswService = spy(new DSWDatasourceServiceImpl(mock(ConnectionServiceImpl.class)));
doNothing().when(dswService).checkSqlQueriesSupported(anyString());
dswService.setMetadataDomainRepository(domainRepository);
Object[][] coumnHeaders = new Object[][] { columns };
SQLMetaData metadata = mock(SQLMetaData.class);
when(metadata.getColumnHeaders()).thenReturn(coumnHeaders);
when(metadata.getJDBCColumnTypes()).thenReturn(columnTypes);
IPentahoResultSet resultSet = mock(IPentahoResultSet.class);
when(resultSet.getMetaData()).thenReturn(metadata);
doReturn(resultSet).when(sqlConnection).executeQuery(matches("(.*" + VALID_QUERY + ".*)"));
when(sqlConnection.executeQuery(matches("(.*" + QUERY_COLUMN_ALREADY_EXIST + ".*)"))).thenThrow(new SQLException("Reason", "S0021", 21));
doReturn(nativeConnection).when(sqlConnection).getNativeConnection();
MondrianCatalog catalog = mock(MondrianCatalog.class);
doReturn(catalog).when(mondrianService).getCatalog(anyString(), any(IPentahoSession.class));
pentahoObjectFactory = mock(IPentahoObjectFactory.class);
when(pentahoObjectFactory.objectDefined(anyString())).thenReturn(true);
when(pentahoObjectFactory.get(this.anyClass(), anyString(), any(IPentahoSession.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
if (invocation.getArguments()[0].equals(IMondrianCatalogService.class)) {
return mondrianService;
}
if (invocation.getArguments()[0].equals(IPentahoConnection.class)) {
return sqlConnection;
}
if (invocation.getArguments()[0].equals(IMetadataDomainRepository.class)) {
return domainRepository;
}
return null;
}
});
PentahoSystem.registerObjectFactory(pentahoObjectFactory);
IPentahoSession pentahoSessionMock = mock(IPentahoSession.class);
when(pentahoSessionMock.getName()).thenReturn("sessionName");
PentahoSessionHolder.setSession(pentahoSessionMock);
}
use of org.pentaho.metadata.model.LogicalModel in project data-access by pentaho.
the class DSWDatasourceServiceImplTest method testDeleteLogicalModel_keepDomainIfLogicalModelExist.
@Test
public void testDeleteLogicalModel_keepDomainIfLogicalModelExist() throws Exception {
doReturn(true).when(dswService).hasDataAccessPermission();
doReturn(workspace2Models).when(dswService).createModelerWorkspace();
LogicalModel logicalModel = new LogicalModel();
logicalModel.setId(LOGICAL_MODEL_ID_DEFAULT);
domain2Models.getLogicalModels().add(logicalModel);
assertTrue(dswService.deleteLogicalModel(DOMAIN_ID_2MODELS, MODEL_NAME));
List<LogicalModel> logicalModels = domain2Models.getLogicalModels();
assertNotNull(logicalModels);
assertEquals(1, logicalModels.size());
verify(domainRepository, never()).removeDomain(domain2Models.getId());
}
use of org.pentaho.metadata.model.LogicalModel in project data-access by pentaho.
the class DSWDatasourceServiceImpl method prepareForSerializaton.
public void prepareForSerializaton(Domain domain) {
/*
* This method is responsible for cleaning up legacy information when
* changing datasource types and also manages CSV files for CSV based
* datasources.
*/
String relativePath = PentahoSystem.getSystemSetting("file-upload-defaults/relative-path", // $NON-NLS-1$
String.valueOf(FileUtils.DEFAULT_RELATIVE_UPLOAD_FILE_PATH));
String path = PentahoSystem.getApplicationContext().getSolutionPath(relativePath);
LogicalModel logicalModel = domain.getLogicalModels().get(0);
String modelState = (String) logicalModel.getProperty("datasourceModel");
if (modelState != null) {
XStream xs = new XStream();
DatasourceDTO datasource = (DatasourceDTO) xs.fromXML(modelState);
CsvFileInfo csvFileInfo = datasource.getCsvModelInfo().getFileInfo();
String csvFileName = csvFileInfo.getFilename();
if (csvFileName != null) {
// Cleanup logic when updating from CSV datasource to SQL
// datasource.
csvFileInfo.setFilename(null);
csvFileInfo.setTmpFilename(null);
csvFileInfo.setFriendlyFilename(null);
csvFileInfo.setContents(null);
csvFileInfo.setEncoding(null);
// Delete CSV file.
File csvFile = new File(path + File.separatorChar + csvFileName);
if (csvFile.exists()) {
csvFile.delete();
}
// Delete STAGING database table.
CsvTransformGenerator csvTransformGenerator = new CsvTransformGenerator(datasource.getCsvModelInfo(), AgileHelper.getDatabaseMeta());
try {
csvTransformGenerator.dropTable(datasource.getCsvModelInfo().getStageTableName());
} catch (CsvTransformGeneratorException e) {
logger.error(e);
}
}
// Update datasourceModel with the new modelState
modelState = xs.toXML(datasource);
logicalModel.setProperty("datasourceModel", modelState);
}
}
use of org.pentaho.metadata.model.LogicalModel in project data-access by pentaho.
the class MetadataService method getModelInfos.
/**
* Returns a list of ModelInfo objects for the specified domain. These objects are small and this list is intended to
* allow a client to provide a list of models to a user so the user can pick which one they want to work with.
*
* @param domain
* @param models
*/
private void getModelInfos(final String domain, List<ModelInfo> models) {
IMetadataDomainRepository repo = getMetadataRepository();
Domain domainObject = repo.getDomain(domain);
// find the best locale
String locale = LocaleHelper.getClosestLocale(LocaleHelper.getLocale().toString(), domainObject.getLocaleCodes());
// iterate over all of the models in this domain
for (LogicalModel model : domainObject.getLogicalModels()) {
// create a new ModelInfo object and give it the envelope information about the model
ModelInfo modelInfo = new ModelInfo();
modelInfo.setDomainId(domain);
modelInfo.setModelId(model.getId());
modelInfo.setModelName(model.getName(locale));
if (model.getDescription() != null) {
String modelDescription = model.getDescription(locale);
modelInfo.setModelDescription(modelDescription);
}
models.add(modelInfo);
}
return;
}
use of org.pentaho.metadata.model.LogicalModel in project data-access by pentaho.
the class MetadataService method loadModel.
/**
* Returns a Model object for the requested model. The model will include the basic metadata - categories and
* columns.
*
* @param domainId
* @param modelId
* @return
*/
public Model loadModel(String domainId, String modelId) {
if (domainId == null) {
// we can't do this without a model
// $NON-NLS-1$
error(Messages.getErrorString("MetadataService.ERROR_0003_NULL_DOMAIN"));
return null;
}
if (modelId == null) {
// we can't do this without a model
// $NON-NLS-1$
error(Messages.getErrorString("MetadataService.ERROR_0004_NULL_Model"));
return null;
}
// because it's lighter weight, check the thin model
Domain domain = getMetadataRepository().getDomain(domainId);
if (domain == null) {
// $NON-NLS-1$
error(Messages.getErrorString("MetadataService.ERROR_0005_DOMAIN_NOT_FOUND", domainId));
return null;
}
LogicalModel model = domain.findLogicalModel(modelId);
if (model == null) {
// the model cannot be found or cannot be loaded
// $NON-NLS-1$
error(Messages.getErrorString("MetadataService.ERROR_0006_MODEL_NOT_FOUND", modelId));
return null;
}
// create the thin metadata model and return it
MetadataServiceUtil util = getMetadataServiceUtil();
util.setDomain(domain);
Model thinModel = util.createThinModel(model, domainId);
return thinModel;
}
Aggregations