Search in sources :

Example 1 with Category

use of org.pentaho.metadata.model.Category 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 2 with Category

use of org.pentaho.metadata.model.Category 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 3 with Category

use of org.pentaho.metadata.model.Category in project data-access by pentaho.

the class WAQRTransport method createFromMetadata.

public static WAQRTransport createFromMetadata(Domain domain) {
    // this assumes a single logical model with a single logical category
    LogicalModel model = domain.getLogicalModels().get(0);
    Iterator<String> iter = model.getName().getLocales().iterator();
    String locale = iter.next();
    Category category = model.getCategories().get(0);
    String domainId = domain.getId();
    String modelId = model.getId();
    String modelName = model.getName() != null ? model.getName().getString(locale) : null;
    String categoryId = category.getId();
    String categoryName = category.getName() != null ? category.getName().getString(locale) : null;
    String schemaName = model.getName(locale);
    return createDomain(domainId, modelId, modelName, categoryId, categoryName, schemaName);
}
Also used : LogicalModel(org.pentaho.metadata.model.LogicalModel) Category(org.pentaho.metadata.model.Category)

Example 4 with Category

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

the class MetadataQueryComponentIT method getBasicDomain.

public Domain getBasicDomain() {
    SqlPhysicalModel model = new SqlPhysicalModel();
    SqlDataSource dataSource = new SqlDataSource();
    dataSource.setDatabaseName("SampleData");
    dataSource.setDialectType("HYPERSONIC");
    dataSource.setType(DataSourceType.JNDI);
    model.setDatasource(dataSource);
    SqlPhysicalTable table = new SqlPhysicalTable(model);
    table.setId("PT1");
    model.getPhysicalTables().add(table);
    table.setTargetTableType(TargetTableType.INLINE_SQL);
    table.setTargetTable("select distinct customername, salesrepemployeenumber from customers");
    SqlPhysicalColumn column = new SqlPhysicalColumn(table);
    column.setId("PC1");
    column.setTargetColumn("CUSTOMERNAME");
    column.setName(new LocalizedString("en_US", "Customer Name"));
    column.setDescription(new LocalizedString("en_US", "Customer Name Desc"));
    column.setDataType(DataType.STRING);
    table.getPhysicalColumns().add(column);
    SqlPhysicalColumn column2 = new SqlPhysicalColumn(table);
    column2.setId("PC2");
    column2.setTargetColumn("SALESREPEMPLOYEENUMBER");
    column2.setName(new LocalizedString("en_US", "Sales Rep"));
    column2.setDescription(new LocalizedString("en_US", "Sales Rep Employee Number"));
    column2.setDataType(DataType.NUMERIC);
    table.getPhysicalColumns().add(column2);
    LogicalModel logicalModel = new LogicalModel();
    logicalModel.setPhysicalModel(model);
    logicalModel.setId("MODEL");
    logicalModel.setName(new LocalizedString("en_US", "My Model"));
    logicalModel.setDescription(new LocalizedString("en_US", "A Description of the Model"));
    LogicalTable logicalTable = new LogicalTable();
    logicalTable.setId("LT");
    logicalTable.setPhysicalTable(table);
    logicalModel.getLogicalTables().add(logicalTable);
    LogicalColumn logicalColumn = new LogicalColumn();
    logicalColumn.setId("LC_CUSTOMERNAME");
    logicalColumn.setPhysicalColumn(column);
    logicalColumn.setLogicalTable(logicalTable);
    logicalTable.addLogicalColumn(logicalColumn);
    LogicalColumn logicalColumn2 = new LogicalColumn();
    logicalColumn2.setId("LC_SALESREP");
    logicalColumn2.setPhysicalColumn(column2);
    logicalColumn2.setLogicalTable(logicalTable);
    logicalTable.addLogicalColumn(logicalColumn2);
    Category mainCategory = new Category();
    mainCategory.setId("CATEGORY");
    mainCategory.setName(new LocalizedString("en_US", "Category"));
    mainCategory.addLogicalColumn(logicalColumn);
    mainCategory.addLogicalColumn(logicalColumn2);
    logicalModel.getCategories().add(mainCategory);
    Domain domain = new Domain();
    domain.setId("DOMAIN");
    domain.addPhysicalModel(model);
    domain.addLogicalModel(logicalModel);
    return domain;
}
Also used : LogicalModel(org.pentaho.metadata.model.LogicalModel) LogicalColumn(org.pentaho.metadata.model.LogicalColumn) Category(org.pentaho.metadata.model.Category) SqlDataSource(org.pentaho.metadata.model.SqlDataSource) SqlPhysicalColumn(org.pentaho.metadata.model.SqlPhysicalColumn) LogicalTable(org.pentaho.metadata.model.LogicalTable) SqlPhysicalModel(org.pentaho.metadata.model.SqlPhysicalModel) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) Domain(org.pentaho.metadata.model.Domain) SqlPhysicalTable(org.pentaho.metadata.model.SqlPhysicalTable)

Example 5 with Category

use of org.pentaho.metadata.model.Category in project data-access by pentaho.

the class DatasourceModelTest method contructRelationalModel.

private GuiStateModel contructRelationalModel(GuiStateModel guiStateModel) {
    IDatabaseConnection connection = new DatabaseConnection();
    connection.setAccessType(DatabaseAccessType.NATIVE);
    // connection.setDriverClass("org.hsqldb.jdbcDriver");
    connection.setName("SampleData");
    connection.setPassword("password");
    // connection.setUrl("jdbc:hsqldb:file:target/test-classes/solution1/system/data/sampledata");
    connection.setUsername("pentaho_user");
    List<IDatabaseConnection> connectionList = new ArrayList<IDatabaseConnection>();
    connectionList.add(connection);
    guiStateModel.setConnections(connectionList);
    guiStateModel.setPreviewLimit("10");
    guiStateModel.validateRelational();
    LogicalColumn logColumn = new LogicalColumn();
    logColumn.setDataType(DataType.NUMERIC);
    List<AggregationType> aggTypeList = new ArrayList<AggregationType>();
    aggTypeList.add(AggregationType.AVERAGE);
    logColumn.setAggregationList(aggTypeList);
    logColumn.setName(new LocalizedString("En", "Column1"));
    BusinessData businessData = new BusinessData();
    List<List<String>> dataSample = new ArrayList<List<String>>();
    List<String> rowData = new ArrayList<String>();
    rowData.add("Data1");
    rowData.add("Data2");
    rowData.add("Data3");
    rowData.add("Data4");
    dataSample.add(rowData);
    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);
    List<LocaleType> localeTypeList = new ArrayList<LocaleType>();
    localeTypeList.add(new LocaleType("Code", "Locale Description"));
    domain.setLocales(localeTypeList);
    businessData.setData(dataSample);
    businessData.setDomain(domain);
    guiStateModel.setLocaleCode("en");
    guiStateModel.setLogicalModels(domain.getLogicalModels());
    guiStateModel.validateRelational();
    return guiStateModel;
}
Also used : BusinessData(org.pentaho.platform.dataaccess.datasource.beans.BusinessData) LogicalColumn(org.pentaho.metadata.model.LogicalColumn) Category(org.pentaho.metadata.model.Category) ArrayList(java.util.ArrayList) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) SqlPhysicalColumn(org.pentaho.metadata.model.SqlPhysicalColumn) LogicalTable(org.pentaho.metadata.model.LogicalTable) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) SqlPhysicalModel(org.pentaho.metadata.model.SqlPhysicalModel) AggregationType(org.pentaho.metadata.model.concept.types.AggregationType) SqlPhysicalTable(org.pentaho.metadata.model.SqlPhysicalTable) LogicalModel(org.pentaho.metadata.model.LogicalModel) SqlDataSource(org.pentaho.metadata.model.SqlDataSource) LocaleType(org.pentaho.metadata.model.concept.types.LocaleType) DatabaseConnection(org.pentaho.database.model.DatabaseConnection) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) ArrayList(java.util.ArrayList) List(java.util.List) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) Domain(org.pentaho.metadata.model.Domain)

Aggregations

Category (org.pentaho.metadata.model.Category)8 LogicalModel (org.pentaho.metadata.model.LogicalModel)8 LogicalColumn (org.pentaho.metadata.model.LogicalColumn)7 Domain (org.pentaho.metadata.model.Domain)6 LocalizedString (org.pentaho.metadata.model.concept.types.LocalizedString)6 ArrayList (java.util.ArrayList)4 LogicalTable (org.pentaho.metadata.model.LogicalTable)3 SqlDataSource (org.pentaho.metadata.model.SqlDataSource)3 SqlPhysicalColumn (org.pentaho.metadata.model.SqlPhysicalColumn)3 SqlPhysicalModel (org.pentaho.metadata.model.SqlPhysicalModel)3 SqlPhysicalTable (org.pentaho.metadata.model.SqlPhysicalTable)3 List (java.util.List)2 Document (org.dom4j.Document)2 Element (org.dom4j.Element)2 DatabaseConnection (org.pentaho.database.model.DatabaseConnection)2 IDatabaseConnection (org.pentaho.database.model.IDatabaseConnection)2 AggregationType (org.pentaho.metadata.model.concept.types.AggregationType)2 LocaleType (org.pentaho.metadata.model.concept.types.LocaleType)2 BusinessData (org.pentaho.platform.dataaccess.datasource.beans.BusinessData)2 HashSet (java.util.HashSet)1