Search in sources :

Example 1 with IMetadataDomainRepository

use of org.pentaho.metadata.repository.IMetadataDomainRepository in project pentaho-platform by pentaho.

the class MetadataPublisher method publish.

@Override
public String publish(final IPentahoSession session) {
    MetadataPublisher.numberUpdated = 0;
    List<String> messages = new ArrayList<String>();
    // refresh new metadata domains
    try {
        IMetadataDomainRepository repo = PentahoSystem.get(IMetadataDomainRepository.class, session);
        repo.reloadDomains();
        MetadataPublisher.numberUpdated = repo.getDomainIds().size();
        return Messages.getInstance().getString("MetadataPublisher.USER_METADATA_RELOADED", // $NON-NLS-1$
        Integer.toString(MetadataPublisher.numberUpdated));
    } catch (Exception e) {
        // $NON-NLS-1$
        logger.error(Messages.getInstance().getErrorString("MetadataPublisher.ERROR_0001_USER_IMPORT_META_FAILED"), e);
        // $NON-NLS-1$
        messages.add(Messages.getInstance().getString("MetadataPublisher.ERROR_0001_USER_IMPORT_META_FAILED"));
    }
    StringBuffer buffer = new StringBuffer();
    // $NON-NLS-1$
    buffer.append("<small>");
    for (String str : messages) {
        // $NON-NLS-1$
        buffer.append("<br/>" + str);
    }
    buffer.append(// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    "<br/><b>" + Messages.getInstance().getString("MetadataPublisher.INFO_0001_CHECK_LOG") + "</b></small>");
    return buffer.toString();
}
Also used : ArrayList(java.util.ArrayList) IMetadataDomainRepository(org.pentaho.metadata.repository.IMetadataDomainRepository)

Example 2 with IMetadataDomainRepository

use of org.pentaho.metadata.repository.IMetadataDomainRepository 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)

Example 3 with IMetadataDomainRepository

use of org.pentaho.metadata.repository.IMetadataDomainRepository in project pentaho-platform by pentaho.

the class MetadataQueryComponent method execute.

public boolean execute() {
    // get the xml parser
    QueryXmlHelper helper = null;
    try {
        helper = createQueryXmlHelper();
    } catch (Exception e) {
        // $NON-NLS-1$
        logger.error("error", e);
        return false;
    }
    // parse the metadata query
    IMetadataDomainRepository repo = PentahoSystem.get(IMetadataDomainRepository.class, null);
    if (queryObject == null) {
        // there is no query model, so create one from the query string
        // apply templates to the query
        String templatedQuery = null;
        if (inputs != null) {
            Properties properties = new Properties();
            for (String name : inputs.keySet()) {
                if (!(inputs.get(name) == null)) {
                    properties.put(name, inputs.get(name).toString());
                }
            }
            templatedQuery = TemplateUtil.applyTemplate(query, properties, null);
        } else {
            templatedQuery = query;
        }
        try {
            queryObject = helper.fromXML(repo, templatedQuery);
        } catch (Exception e) {
            // $NON-NLS-1$
            logger.error("error", e);
            return false;
        }
    }
    if (queryObject == null) {
        // $NON-NLS-1$
        logger.error("error query object null");
        return false;
    }
    // Can still be overridden in the action sequence
    if (timeout == null) {
        // $NON-NLS-1$
        Object timeoutProperty = queryObject.getLogicalModel().getProperty("timeout");
        if (timeoutProperty != null && timeoutProperty instanceof Number) {
            int timeoutVal = ((Number) timeoutProperty).intValue();
            this.setTimeout(timeoutVal);
        }
    }
    if (maxRows == null) {
        // $NON-NLS-1$
        Object maxRowsProperty = queryObject.getLogicalModel().getProperty("max_rows");
        if (maxRowsProperty != null && maxRowsProperty instanceof Number) {
            int maxRowsVal = ((Number) maxRowsProperty).intValue();
            this.setMaxRows(maxRowsVal);
        }
    }
    String queryExecName = queryObject.getLogicalModel().getPhysicalModel().getQueryExecName();
    String queryExecDefault = queryObject.getLogicalModel().getPhysicalModel().getDefaultQueryClassname();
    // String modelType = (String) inputs.get("modeltype");
    IMetadataQueryExec executor = PentahoSystem.get(IMetadataQueryExec.class, queryExecName, session);
    if (executor == null) {
        // get the executor from a plugin possibly?
        Class clazz;
        try {
            clazz = Class.forName(queryExecDefault, true, queryObject.getLogicalModel().getPhysicalModel().getClass().getClassLoader());
            executor = (IMetadataQueryExec) clazz.getConstructor(new Class[] {}).newInstance(new Object[] {});
        } catch (Exception e) {
            logger.warn(Messages.getInstance().getErrorString("MetadataQueryComponent.ERROR_0002_NO_EXECUTOR", // $NON-NLS-1$
            queryExecName));
        }
    }
    if (executor == null) {
        // the query exec class is not defined thru configuration, go with the default
        Class clazz;
        try {
            clazz = Class.forName(queryExecDefault);
            executor = (IMetadataQueryExec) clazz.getConstructor(new Class[] {}).newInstance(new Object[] {});
        } catch (Exception e) {
            logger.error(Messages.getInstance().getErrorString("MetadataQueryComponent.ERROR_0002_NO_EXECUTOR", // $NON-NLS-1$
            queryExecName));
            return false;
        }
    }
    // determine parameter values
    if (queryObject.getParameters() != null) {
        for (Parameter param : queryObject.getParameters()) {
            Object value = null;
            if (inputs != null) {
                value = inputs.get(param.getName());
            }
            executor.setParameter(param, value);
        }
    }
    try {
        executor.setDoQueryLog(logSql);
        executor.setForwardOnly(this.useForwardOnlyResultSet);
        executor.setMaxRows(this.maxRows);
        executor.setMetadataDomainRepository(repo);
        executor.setReadOnly(this.readOnly);
        executor.setTimeout(this.timeout);
        if (this.inputs != null) {
            executor.setInputs(this.inputs);
        }
        resultSet = executor.executeQuery(queryObject);
        if (resultSet != null && !live && executor.isLive()) {
            // read the results and cache them
            IPentahoResultSet cachedResultSet = resultSet.memoryCopy();
            resultSet.close();
            resultSet.closeConnection();
            resultSet = cachedResultSet;
        }
        return resultSet != null;
    } catch (Exception e) {
        // $NON-NLS-1$
        logger.error("error", e);
        throw new RuntimeException(e.getLocalizedMessage(), e);
    }
}
Also used : QueryXmlHelper(org.pentaho.metadata.query.model.util.QueryXmlHelper) IMetadataDomainRepository(org.pentaho.metadata.repository.IMetadataDomainRepository) Properties(java.util.Properties) IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) Parameter(org.pentaho.metadata.query.model.Parameter) IMetadataQueryExec(org.pentaho.metadata.model.IMetadataQueryExec)

Example 4 with IMetadataDomainRepository

use of org.pentaho.metadata.repository.IMetadataDomainRepository 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)

Example 5 with IMetadataDomainRepository

use of org.pentaho.metadata.repository.IMetadataDomainRepository in project data-access by pentaho.

the class MetadataServiceUtil method getDomainObject.

/**
 * Returns the full domain obejct for a XML MQL query
 *
 * @param query
 * @return
 * @throws PentahoMetadataException
 */
public org.pentaho.metadata.model.Domain getDomainObject(String query) throws PentahoMetadataException {
    QueryXmlHelper helper = new QueryXmlHelper();
    IMetadataDomainRepository domainRepository = getMetadataRepository();
    org.pentaho.metadata.query.model.Query fatQuery = helper.fromXML(domainRepository, query);
    return fatQuery.getDomain();
}
Also used : QueryXmlHelper(org.pentaho.metadata.query.model.util.QueryXmlHelper) IMetadataDomainRepository(org.pentaho.metadata.repository.IMetadataDomainRepository)

Aggregations

IMetadataDomainRepository (org.pentaho.metadata.repository.IMetadataDomainRepository)13 Domain (org.pentaho.metadata.model.Domain)5 LogicalModel (org.pentaho.metadata.model.LogicalModel)5 LocalizedString (org.pentaho.metadata.model.concept.types.LocalizedString)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Before (org.junit.Before)2 Test (org.junit.Test)2 LogicalColumn (org.pentaho.metadata.model.LogicalColumn)2 QueryXmlHelper (org.pentaho.metadata.query.model.util.QueryXmlHelper)2 IUnifiedRepository (org.pentaho.platform.api.repository2.unified.IUnifiedRepository)2 ModelInfo (org.pentaho.platform.dataaccess.metadata.model.impl.ModelInfo)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 BigDecimal (java.math.BigDecimal)1 Properties (java.util.Properties)1 StringTokenizer (java.util.StringTokenizer)1 Matcher (java.util.regex.Matcher)1