Search in sources :

Example 6 with LogicalColumn

use of org.pentaho.metadata.model.LogicalColumn in project pentaho-kettle by pentaho.

the class ConceptUtilTest method testGetAttributeType.

@Test
public void testGetAttributeType() throws Exception {
    final LogicalColumn logicalColumn = mock(LogicalColumn.class);
    final AttributeType attribute = AttributeType.ATTRIBUTE;
    when(logicalColumn.getProperty(DefaultIDs.LOGICAL_COLUMN_ATTRIBUTE_TYPE)).thenReturn(attribute.name());
    final AttributeType attributeType = ConceptUtil.getAttributeType(logicalColumn);
    assertEquals(attribute, attributeType);
}
Also used : LogicalColumn(org.pentaho.metadata.model.LogicalColumn) Test(org.junit.Test)

Example 7 with LogicalColumn

use of org.pentaho.metadata.model.LogicalColumn in project pentaho-kettle by pentaho.

the class ConceptUtilTest method testFindFirstKeyColumn.

@Test
public void testFindFirstKeyColumn() throws Exception {
    final LogicalTable logicalTable = mock(LogicalTable.class);
    assertNull(ConceptUtil.findFirstKeyColumn(logicalTable));
    final LogicalColumn logicalColumn = mock(LogicalColumn.class);
    final LogicalColumn logicalColumnKey1 = mock(LogicalColumn.class);
    final LogicalColumn logicalColumnKey2 = mock(LogicalColumn.class);
    when(logicalColumnKey1.getFieldType()).thenReturn(FieldType.KEY);
    when(logicalColumnKey2.getFieldType()).thenReturn(FieldType.KEY);
    when(logicalTable.getLogicalColumns()).thenReturn(new LinkedList<LogicalColumn>() {

        {
            add(logicalColumn);
            add(logicalColumnKey1);
            add(logicalColumnKey2);
        }
    });
    final LogicalColumn firstKeyColumn = ConceptUtil.findFirstKeyColumn(logicalTable);
    assertEquals(logicalColumnKey1, firstKeyColumn);
}
Also used : LogicalColumn(org.pentaho.metadata.model.LogicalColumn) LogicalTable(org.pentaho.metadata.model.LogicalTable) Test(org.junit.Test)

Example 8 with LogicalColumn

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

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

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

the class MetadataServiceUtil method convertQuery.

/**
 * Converts a thin query model into a full query
 *
 * @param src
 * @return
 */
public org.pentaho.metadata.query.model.Query convertQuery(Query src) {
    IMetadataDomainRepository domainRepository = getMetadataRepository();
    Domain fullDomain = domainRepository.getDomain(src.getDomainName());
    LogicalModel logicalModel = fullDomain.findLogicalModel(src.getModelId());
    // create a new full query object
    org.pentaho.metadata.query.model.Query dest = new org.pentaho.metadata.query.model.Query(fullDomain, logicalModel);
    // now add the selections
    List<Selection> selections = dest.getSelections();
    for (Column column : src.getColumns()) {
        // get the objects needed for the selection
        LogicalColumn logicalColumn = logicalModel.findLogicalColumn(column.getId());
        org.pentaho.metadata.model.Category category = getCategory(column.getId(), logicalModel);
        AggregationType aggregationType = AggregationType.valueOf(column.getSelectedAggType());
        // create a selection and add it to the list
        Selection selection = new Selection(category, logicalColumn, aggregationType);
        selections.add(selection);
    }
    // now add the filters
    List<Constraint> constraints = dest.getConstraints();
    for (Condition condition : src.getConditions()) {
        org.pentaho.metadata.query.model.CombinationType combinationType = CombinationType.valueOf(condition.getCombinationType());
        LogicalColumn logicalColumn = logicalModel.findLogicalColumn(condition.getColumn());
        String paramName = null;
        for (Parameter parameter : src.getParameters()) {
            if (parameter.getColumn().equals(condition.getColumn())) {
                paramName = parameter.getName() == null ? parameter.getColumn() : parameter.getName();
            }
        }
        // condition.setParameterized(parameterized);
        String formula = condition.getCondition(logicalColumn.getDataType().name(), paramName);
        Constraint constraint = new Constraint(combinationType, formula);
        constraints.add(constraint);
    }
    // now set the disable distinct option
    if (src.getDisableDistinct() != null) {
        dest.setDisableDistinct(src.getDisableDistinct());
    }
    // now add the sorting information
    List<org.pentaho.metadata.query.model.Order> orders = dest.getOrders();
    for (Order order : src.getOrders()) {
        // find the selection
        for (Selection selection : selections) {
            if (selection.getLogicalColumn().getId().equals(order.getColumn())) {
                Type type = Type.valueOf(order.getOrderType());
                org.pentaho.metadata.query.model.Order fullOrder = new org.pentaho.metadata.query.model.Order(selection, type);
                orders.add(fullOrder);
            }
        }
    }
    // now add the parameter information
    List<org.pentaho.metadata.query.model.Parameter> parameters = dest.getParameters();
    for (Parameter parameter : src.getParameters()) {
        // find the column for this parameter
        LogicalColumn logicalColumn = logicalModel.findLogicalColumn(parameter.getColumn());
        DataType type = logicalColumn.getDataType();
        String[] value = parameter.getValue();
        org.pentaho.metadata.query.model.Parameter fullParam = new org.pentaho.metadata.query.model.Parameter(parameter.getColumn(), type, value[0]);
        parameters.add(fullParam);
    }
    return dest;
}
Also used : LogicalColumn(org.pentaho.metadata.model.LogicalColumn) Query(org.pentaho.platform.dataaccess.metadata.model.impl.Query) Constraint(org.pentaho.metadata.query.model.Constraint) Selection(org.pentaho.metadata.query.model.Selection) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) AggregationType(org.pentaho.metadata.model.concept.types.AggregationType) LogicalModel(org.pentaho.metadata.model.LogicalModel) LogicalColumn(org.pentaho.metadata.model.LogicalColumn) IPhysicalColumn(org.pentaho.metadata.model.IPhysicalColumn) Column(org.pentaho.platform.dataaccess.metadata.model.impl.Column) DataType(org.pentaho.metadata.model.concept.types.DataType) Condition(org.pentaho.platform.dataaccess.metadata.model.impl.Condition) Order(org.pentaho.platform.dataaccess.metadata.model.impl.Order) IMetadataDomainRepository(org.pentaho.metadata.repository.IMetadataDomainRepository) DataType(org.pentaho.metadata.model.concept.types.DataType) AggregationType(org.pentaho.metadata.model.concept.types.AggregationType) FieldType(org.pentaho.metadata.model.concept.types.FieldType) Type(org.pentaho.metadata.query.model.Order.Type) CombinationType(org.pentaho.metadata.query.model.CombinationType) Parameter(org.pentaho.platform.dataaccess.metadata.model.impl.Parameter) Domain(org.pentaho.metadata.model.Domain) CombinationType(org.pentaho.metadata.query.model.CombinationType)

Aggregations

LogicalColumn (org.pentaho.metadata.model.LogicalColumn)30 LocalizedString (org.pentaho.metadata.model.concept.types.LocalizedString)16 LogicalTable (org.pentaho.metadata.model.LogicalTable)12 ArrayList (java.util.ArrayList)10 LogicalModel (org.pentaho.metadata.model.LogicalModel)9 Domain (org.pentaho.metadata.model.Domain)8 Category (org.pentaho.metadata.model.Category)7 Test (org.junit.Test)5 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)4 SqlDataSource (org.pentaho.metadata.model.SqlDataSource)4 SqlPhysicalColumn (org.pentaho.metadata.model.SqlPhysicalColumn)4 SqlPhysicalModel (org.pentaho.metadata.model.SqlPhysicalModel)4 SqlPhysicalTable (org.pentaho.metadata.model.SqlPhysicalTable)4 AggregationType (org.pentaho.metadata.model.concept.types.AggregationType)4 Point (org.pentaho.di.core.gui.Point)3 StepMeta (org.pentaho.di.trans.step.StepMeta)3 List (java.util.List)2 Document (org.dom4j.Document)2 Element (org.dom4j.Element)2 TableItem (org.eclipse.swt.widgets.TableItem)2