Search in sources :

Example 11 with LogicalModel

use of org.pentaho.metadata.model.LogicalModel in project pentaho-platform by pentaho.

the class PentahoMetadataDomainRepositoryTest method testStoreDomain.

@Test
public void testStoreDomain() throws Exception {
    try {
        domainRepositorySpy.storeDomain(null, true);
        fail("Invalid domain should throw exception");
    } catch (DomainIdNullException success) {
    // ignored
    }
    try {
        domainRepositorySpy.storeDomain(new MockDomain(null), true);
        fail("Null domain id should throw exception");
    } catch (DomainIdNullException success) {
    // ignored
    }
    try {
        domainRepositorySpy.storeDomain(new MockDomain(""), true);
        fail("Empty domain id should throw exception");
    } catch (DomainIdNullException success) {
    // ignored
    }
    try {
        domainRepositorySpy.storeDomain(null, null, true);
        fail("Null InputStream should throw an exception");
    } catch (IllegalArgumentException success) {
    // ignored
    }
    try {
        domainRepositorySpy.storeDomain(null, "", true);
        fail("Null InputStream should throw an exception");
    } catch (IllegalArgumentException success) {
    // ignored
    }
    try {
        domainRepositorySpy.storeDomain(null, "valid", true);
        fail("Null InputStream should throw an exception");
    } catch (IllegalArgumentException success) {
    // ignored
    }
    try {
        domainRepositorySpy.storeDomain(EMPTY_INPUT_STREAM, null, true);
        fail("Invalid domain id should throw exception");
    } catch (DomainIdNullException success) {
    // ignored
    }
    try {
        domainRepositorySpy.storeDomain(EMPTY_INPUT_STREAM, "", true);
        fail("Invalid domain id should throw exception");
    } catch (DomainIdNullException success) {
    // ignored
    }
    // Have the XmiParser fail
    try {
        domainRepositorySpy.storeDomain(new MockDomain("exception"), true);
        fail("An unexpected exception should throw a DomainStorageException");
    } catch (DomainStorageException success) {
    // ignored
    }
    try {
        domainRepositorySpy.storeDomain(new ByteArrayInputStream(null), "valid", true);
        fail("Error with byte array input stream should throw exception");
    } catch (Exception success) {
    // ignored
    }
    domainRepositorySpy.removeDomain("steel-wheels_test");
    domainRepositorySpy.removeDomain(STEEL_WHEELS);
    domainRepositorySpy.removeDomain(SAMPLE_DOMAIN_ID);
    final MockDomain sample = new MockDomain(SAMPLE_DOMAIN_ID);
    domainRepositorySpy.storeDomain(sample, false);
    doReturn(true).when(aclNodeHelper).canAccess(any(RepositoryFile.class), eq(EnumSet.of(RepositoryFilePermission.READ)));
    final Domain domain = domainRepositorySpy.getDomain(SAMPLE_DOMAIN_ID);
    assertNotNull(domain);
    final List<LogicalModel> logicalModels = domain.getLogicalModels();
    assertNotNull(logicalModels);
    assertEquals(0, logicalModels.size());
    try {
        domainRepositorySpy.storeDomain(sample, false);
        fail("A duplicate domain with overwrite=false should fail");
    } catch (DomainAlreadyExistsException success) {
    // ignored
    }
    sample.addLogicalModel("test");
    domainRepositorySpy.storeDomain(sample, true);
    assertEquals(1, domainRepositorySpy.getDomain(SAMPLE_DOMAIN_ID).getLogicalModels().size());
    MockDomain xmiExtensionSample = new MockDomain(SAMPLE_DOMAIN_ID + ".xmi");
    try {
        domainRepositorySpy.storeDomain(xmiExtensionSample, false);
        fail("A duplicate domain with overwrite=false should fail");
    } catch (DomainAlreadyExistsException success) {
    // ignored
    }
    xmiExtensionSample.addLogicalModel("test1");
    xmiExtensionSample.addLogicalModel("test2");
    domainRepositorySpy.storeDomain(xmiExtensionSample, true);
    assertEquals(2, domainRepositorySpy.getDomain(SAMPLE_DOMAIN_ID).getLogicalModels().size());
    final RepositoryFile folder = domainRepositorySpy.getMetadataDir();
    assertNotNull(folder);
    assertTrue(folder.isFolder());
    assertEquals(1, repository.getChildren(folder.getId()).size());
    final Domain steelWheelsDomain = loadDomain(STEEL_WHEELS, "./steel-wheels.xmi");
    domainRepositorySpy.storeDomain(steelWheelsDomain, true);
    assertEquals(2, repository.getChildren(folder.getId()).size());
}
Also used : LogicalModel(org.pentaho.metadata.model.LogicalModel) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) ByteArrayInputStream(java.io.ByteArrayInputStream) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) Domain(org.pentaho.metadata.model.Domain) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) IOException(java.io.IOException) Test(org.junit.Test)

Example 12 with LogicalModel

use of org.pentaho.metadata.model.LogicalModel in project pentaho-platform by pentaho.

the class SessionCachingMetadataDomainRepositoryIT method testRemoveModel.

public void testRemoveModel() throws Exception {
    // $NON-NLS-1$
    final String ID1 = "1";
    // $NON-NLS-1$
    final String ID2 = "2";
    MockSessionAwareMetadataDomainRepository mock = new MockSessionAwareMetadataDomainRepository();
    SessionCachingMetadataDomainRepository repo = new SessionCachingMetadataDomainRepository(mock);
    // $NON-NLS-1$ //$NON-NLS-2$
    PentahoSessionHolder.setSession(new StandaloneSession("Standalone Session", "1"));
    Domain domain = getTestDomain(ID1);
    LogicalModel model = new LogicalModel();
    // $NON-NLS-1$
    model.setId("test");
    domain.addLogicalModel(model);
    repo.storeDomain(domain, false);
    repo.storeDomain(getTestDomain(ID2), false);
    repo.getDomain(ID1);
    assertEquals(1, PentahoSystem.getCacheManager(null).getAllKeysFromRegionCache(CACHE_NAME).size());
    repo.getDomain(ID2);
    assertEquals(2, PentahoSystem.getCacheManager(null).getAllKeysFromRegionCache(CACHE_NAME).size());
    // $NON-NLS-1$ //$NON-NLS-2$
    PentahoSessionHolder.setSession(new StandaloneSession("Standalone Session", "2"));
    repo.getDomain(ID1);
    assertEquals(3, PentahoSystem.getCacheManager(null).getAllKeysFromRegionCache(CACHE_NAME).size());
    // $NON-NLS-1$
    repo.removeModel(ID1, "test");
    // Removing a model should remove all domains with that same id, leaving others intact
    assertEquals(1, PentahoSystem.getCacheManager(null).getAllKeysFromRegionCache(CACHE_NAME).size());
    // $NON-NLS-1$
    assertEquals(1, mock.getInvocationCount("removeModel"));
}
Also used : LogicalModel(org.pentaho.metadata.model.LogicalModel) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) SessionCachingMetadataDomainRepository(org.pentaho.platform.plugin.services.metadata.SessionCachingMetadataDomainRepository) Domain(org.pentaho.metadata.model.Domain)

Example 13 with LogicalModel

use of org.pentaho.metadata.model.LogicalModel in project pentaho-platform by pentaho.

the class PMDUIComponent method loadModel.

private Document loadModel() {
    // Create a document that describes the result
    Document doc = DocumentHelper.createDocument();
    // $NON-NLS-1$
    Element root = doc.addElement("metadata");
    if (domainName == null) {
        // we can't do this without a model
        // $NON-NLS-1$ //$NON-NLS-2$
        root.addElement("message").setText(Messages.getInstance().getString("PMDUIComponent.USER_NO_DOMAIN_SPECIFIED"));
        return doc;
    }
    if (modelId == null) {
        // we can't do this without a model
        // $NON-NLS-1$ //$NON-NLS-2$
        root.addElement("message").setText(Messages.getInstance().getString("PMDUIComponent.USER_NO_MODEL_SPECIFIED"));
        return doc;
    }
    // $NON-NLS-1$
    Element modelNode = root.addElement("model");
    // because it's lighter weight, check the thin model
    Domain domain = getMetadataRepository().getDomain(domainName);
    if (domain == null) {
        // $NON-NLS-1$ //$NON-NLS-2$
        root.addElement("message").setText(Messages.getInstance().getString("PMDUIComponent.USER_DOMAIN_LOADING_ERROR", domainName));
        return doc;
    }
    String locale = LocaleHelper.getClosestLocale(LocaleHelper.getLocale().toString(), domain.getLocaleCodes());
    LogicalModel model = domain.findLogicalModel(modelId);
    if (model == null) {
        // $NON-NLS-1$ //$NON-NLS-2$
        root.addElement("message").setText(Messages.getInstance().getString("PMDUIComponent.USER_MODEL_LOADING_ERROR", modelId));
        // $NON-NLS-1$
        error(Messages.getInstance().getString("PMDUIComponent.USER_MODEL_LOADING_ERROR", modelId));
        return doc;
    }
    // $NON-NLS-1$
    modelNode.addElement("domain_id").setText(domainName);
    if (model.getId() != null) {
        // $NON-NLS-1$
        modelNode.addElement("model_id").setText(model.getId());
    }
    if (model.getName(locale) != null) {
        // $NON-NLS-1$
        modelNode.addElement("model_name").setText(model.getName(locale));
    }
    if (model.getDescription(locale) != null) {
        // $NON-NLS-1$
        modelNode.addElement("model_description").setText(model.getDescription(locale));
    }
    Element tableNode;
    for (Category category : model.getCategories()) {
        // $NON-NLS-1$
        tableNode = modelNode.addElement("view");
        if (category.getId() != null) {
            // $NON-NLS-1$
            tableNode.addElement("view_id").setText(category.getId());
        }
        if (category.getName(locale) != null) {
            // $NON-NLS-1$
            tableNode.addElement("view_name").setText(category.getName(locale));
        }
        if (category.getDescription(locale) != null) {
            // $NON-NLS-1$
            tableNode.addElement("view_description").setText(category.getDescription(locale));
        }
        for (LogicalColumn column : category.getLogicalColumns()) {
            // $NON-NLS-1$
            Boolean hidden = (Boolean) column.getProperty("hidden");
            if (hidden != null && hidden) {
                continue;
            }
            addColumn(column, tableNode, locale);
        }
    }
    return doc;
}
Also used : LogicalModel(org.pentaho.metadata.model.LogicalModel) LogicalColumn(org.pentaho.metadata.model.LogicalColumn) Category(org.pentaho.metadata.model.Category) Element(org.dom4j.Element) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) Document(org.dom4j.Document) Domain(org.pentaho.metadata.model.Domain)

Example 14 with LogicalModel

use of org.pentaho.metadata.model.LogicalModel in project pentaho-platform by pentaho.

the class PMDUIComponent method getLookup.

public Document getLookup() {
    // Create a document that describes the result
    Document doc = DocumentHelper.createDocument();
    // $NON-NLS-1$
    Element root = doc.addElement("metadata");
    if (domainName == null) {
        // we can't do this without a model
        // $NON-NLS-1$ //$NON-NLS-2$
        root.addElement("message").setText(Messages.getInstance().getString("PMDUIComponent.USER_NO_DOMAIN_SPECIFIED"));
        return doc;
    }
    if (modelId == null) {
        // we can't do this without a view
        // $NON-NLS-1$ //$NON-NLS-2$
        root.addElement("message").setText(Messages.getInstance().getString("PMDUIComponent.USER_NO_MODEL_SPECIFIED"));
        return doc;
    }
    if (columnId == null) {
        // we can't do this without a view
        // $NON-NLS-1$ //$NON-NLS-2$
        root.addElement("message").setText(Messages.getInstance().getString("PMDUIComponent.USER_NO_COLUMN_SPECIFIED"));
        return doc;
    }
    Domain domain = getMetadataRepository().getDomain(domainName);
    String locale = LocaleHelper.getClosestLocale(LocaleHelper.getLocale().toString(), domain.getLocaleCodes());
    // This is the business view that was selected.
    LogicalModel model = domain.findLogicalModel(modelId);
    if (model == null) {
        // $NON-NLS-1$ //$NON-NLS-2$
        root.addElement("message").setText(Messages.getInstance().getString("PMDUIComponent.USER_MODEL_LOADING_ERROR", modelId));
        return doc;
    }
    LogicalColumn column = model.findLogicalColumn(columnId);
    if (column == null) {
        // $NON-NLS-1$ //$NON-NLS-2$
        root.addElement("message").setText(Messages.getInstance().getString("PMDUIComponent.USER_COLUMN_NOT_FOUND"));
        return doc;
    }
    // Temporary hack to get the BusinessCategory. When fixed properly, you should be able to interrogate the
    // business column thingie for it's containing BusinessCategory.
    Category view = null;
    for (Category category : model.getCategories()) {
        for (LogicalColumn col : category.getLogicalColumns()) {
            if (col.getId().equals(column.getId())) {
                view = category;
                break;
            }
        }
    }
    if (view == null) {
        // $NON-NLS-1$ //$NON-NLS-2$
        root.addElement("message").setText(Messages.getInstance().getString("PMDUIComponent.USER_VIEW_NOT_FOUND"));
        return doc;
    }
    String mql = // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    "<mql><domain_type>relational</domain_type><domain_id>" + domainName + "</domain_id><model_id>" + modelId + "</model_id>";
    if (column.getProperty("lookup") == null) {
        // $NON-NLS-1$
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        mql += "<selection><view>" + view.getId() + "</view><column>" + column.getId() + "</column></selection>";
        mql += // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        "<orders><order><direction>asc</direction><view_id>" + view.getId() + "</view_id><column_id>" + column.getId() + "</column_id></order></orders>";
    } else {
        // $NON-NLS-1$
        String lookup = (String) column.getProperty("lookup");
        // assume model and view are the same...
        // $NON-NLS-1$
        StringTokenizer tokenizer1 = new StringTokenizer(lookup, ";");
        while (tokenizer1.hasMoreTokens()) {
            // $NON-NLS-1$
            StringTokenizer tokenizer2 = new StringTokenizer(tokenizer1.nextToken(), ".");
            if (tokenizer2.countTokens() == 2) {
                String lookupViewId = tokenizer2.nextToken();
                String lookupColumnId = tokenizer2.nextToken();
                // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                mql += "<selection><view>" + lookupViewId + "</view><column>" + lookupColumnId + "</column></selection>";
            }
        }
    }
    // $NON-NLS-1$
    mql += "</mql>";
    ArrayList messages = new ArrayList();
    SimpleParameterProvider lookupParameters = new SimpleParameterProvider();
    // $NON-NLS-1$
    lookupParameters.setParameter("mql", mql);
    IRuntimeContext runtime = SolutionHelper.doAction("/system/metadata/PickList.xaction", "lookup-list", lookupParameters, getSession(), messages, // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    this);
    IPentahoResultSet results = null;
    if (runtime != null) {
        if (runtime.getStatus() == IRuntimeContext.RUNTIME_STATUS_SUCCESS) {
            if (runtime.getOutputNames().contains("data")) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                results = runtime.getOutputParameter("data").getValueAsResultSet();
                Object[][] columnHeaders = results.getMetaData().getColumnHeaders();
                boolean hasColumnHeaders = columnHeaders != null;
                Element rowElement;
                // $NON-NLS-1$
                Element dataElement = root.addElement("data");
                if (hasColumnHeaders) {
                    for (int rowNo = 0; rowNo < columnHeaders.length; rowNo++) {
                        // $NON-NLS-1$
                        rowElement = dataElement.addElement("COLUMN-HDR-ROW");
                        for (int columnNo = 0; columnNo < columnHeaders[rowNo].length; columnNo++) {
                            // $NON-NLS-1$
                            Object nameAttr = results.getMetaData().getAttribute(rowNo, columnNo, "name");
                            if ((nameAttr != null) && (nameAttr instanceof LocalizedString)) {
                                LocalizedString str = (LocalizedString) nameAttr;
                                String name = str.getLocalizedString(locale);
                                if (name != null) {
                                    // $NON-NLS-1$
                                    rowElement.addElement("COLUMN-HDR-ITEM").setText(name);
                                } else {
                                    // $NON-NLS-1$
                                    rowElement.addElement("COLUMN-HDR-ITEM").setText(columnHeaders[rowNo][columnNo].toString());
                                }
                            } else {
                                // $NON-NLS-1$
                                rowElement.addElement("COLUMN-HDR-ITEM").setText(columnHeaders[rowNo][columnNo].toString());
                            }
                        }
                    }
                }
                Object[] row = results.next();
                while (row != null) {
                    // $NON-NLS-1$
                    rowElement = dataElement.addElement("DATA-ROW");
                    for (Object element : row) {
                        if (element == null) {
                            // $NON-NLS-1$ //$NON-NLS-2$
                            rowElement.addElement("DATA-ITEM").setText("");
                        } else {
                            // $NON-NLS-1$
                            rowElement.addElement("DATA-ITEM").setText(element.toString());
                        }
                    }
                    row = results.next();
                }
            }
        }
    }
    return doc;
}
Also used : LogicalColumn(org.pentaho.metadata.model.LogicalColumn) Category(org.pentaho.metadata.model.Category) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) Document(org.dom4j.Document) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) LogicalModel(org.pentaho.metadata.model.LogicalModel) IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) StringTokenizer(java.util.StringTokenizer) Domain(org.pentaho.metadata.model.Domain) IRuntimeContext(org.pentaho.platform.api.engine.IRuntimeContext) SimpleParameterProvider(org.pentaho.platform.engine.core.solution.SimpleParameterProvider)

Example 15 with LogicalModel

use of org.pentaho.metadata.model.LogicalModel in project pentaho-platform by pentaho.

the class PMDUIComponent method addThinDomainModels.

private void addThinDomainModels(final String domain, final Element modelsNode, final Element root) {
    IMetadataDomainRepository repo = getMetadataRepository();
    Domain domainObject = repo.getDomain(domain);
    String locale = LocaleHelper.getClosestLocale(LocaleHelper.getLocale().toString(), domainObject.getLocaleCodes());
    Element modelNode;
    for (LogicalModel model : domainObject.getLogicalModels()) {
        String vis = (String) model.getProperty("visible");
        if (vis != null) {
            String[] visibleContexts = vis.split(",");
            boolean visibleToContext = false;
            for (String context : visibleContexts) {
                if ("adhoc".equals(context.trim())) {
                    visibleToContext = true;
                    break;
                }
            }
            if (!visibleToContext) {
                continue;
            }
        }
        // $NON-NLS-1$
        modelNode = modelsNode.addElement("model");
        // $NON-NLS-1$
        modelNode.addElement("domain_id").setText(domain);
        if (model.getId() != null) {
            // $NON-NLS-1$
            modelNode.addElement("model_id").setText(model.getId());
        }
        String modelName = model.getName(locale);
        if (modelName != null) {
            // $NON-NLS-1$
            modelNode.addElement("model_name").setText(modelName);
        }
        if (model.getDescription() != null) {
            String modelDescription = model.getDescription(locale);
            if (modelDescription != null) {
                // $NON-NLS-1$
                modelNode.addElement("model_description").setText(modelDescription);
            }
        }
    }
    return;
}
Also used : LogicalModel(org.pentaho.metadata.model.LogicalModel) Element(org.dom4j.Element) IMetadataDomainRepository(org.pentaho.metadata.repository.IMetadataDomainRepository) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) Domain(org.pentaho.metadata.model.Domain)

Aggregations

LogicalModel (org.pentaho.metadata.model.LogicalModel)53 Domain (org.pentaho.metadata.model.Domain)29 LocalizedString (org.pentaho.metadata.model.concept.types.LocalizedString)14 Test (org.junit.Test)13 ModelerWorkspace (org.pentaho.agilebi.modeler.ModelerWorkspace)11 LogicalTable (org.pentaho.metadata.model.LogicalTable)11 ArrayList (java.util.ArrayList)10 LogicalColumn (org.pentaho.metadata.model.LogicalColumn)9 Category (org.pentaho.metadata.model.Category)8 Matchers.anyString (org.mockito.Matchers.anyString)7 SqlPhysicalModel (org.pentaho.metadata.model.SqlPhysicalModel)7 SqlPhysicalTable (org.pentaho.metadata.model.SqlPhysicalTable)6 IMetadataDomainRepository (org.pentaho.metadata.repository.IMetadataDomainRepository)6 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)6 DomainIdNullException (org.pentaho.metadata.repository.DomainIdNullException)5 DomainStorageException (org.pentaho.metadata.repository.DomainStorageException)5 ConnectionServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException)5 DatasourceServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)5 IOException (java.io.IOException)4 SqlDataSource (org.pentaho.metadata.model.SqlDataSource)4