BIRT:如何以编程方式删除数据集参数

时间:2014-02-20 12:38:40

标签: java birt

我想修改现有的* .rptdesign文件并以新名称保存。 现有文件包含Data Set,其中包含模板SQL select语句和多个DS参数。 我想使用一个实际的SQL select语句,它只使用部分DS参数。

但是,以下代码会导致异常:

Exception in thread "main" `java.lang.RuntimeException`: *The structure is floating, and its handle is invalid!*
at org.eclipse.birt.report.model.api.StructureHandle.getStringProperty(StructureHandle.java:207)
at org.eclipse.birt.report.model.api.DataSetParameterHandle.getName(DataSetParameterHandle.java:143)
at org.eclipse.birt.report.model.api.DataSetHandle$DataSetParametersPropertyHandle.removeParamBindingsFor(DataSetHandle.java:851)
at org.eclipse.birt.report.model.api.DataSetHandle$DataSetParametersPropertyHandle.removeItems(DataSetHandle.java:694)

-

    OdaDataSetHandle dsMaster = (OdaDataSetHandle) report.findDataSet("Master");

    HashSet<String> bindVarsUsed = new HashSet<String>();
    ...
    // find out which DS parameters are actually used
    HashSet<String> bindVarsUsed = new HashSet<String>();
    ...
    ArrayList<OdaDataSetParameterHandle> toRemove = new ArrayList<OdaDataSetParameterHandle>();
    for (Iterator iter = dsMaster.parametersIterator(); iter.hasNext(); ) {
        OdaDataSetParameterHandle dsPara = (OdaDataSetParameterHandle)iter.next();
        String name = dsPara.getName();
        if (name.startsWith("param_")) {
            String bindVarName = name.substring(6);
            if (!bindVarsUsed.contains(bindVarName)) {
                toRemove.add(dsPara);
            }
        }
    }
    PropertyHandle paramsHandle = dsMaster.getPropertyHandle( OdaDataSetHandle.PARAMETERS_PROP );
    paramsHandle.removeItems(toRemove);

这里有什么问题? 有没有人使用DE API从现有数据集中删除参数?

1 个答案:

答案 0 :(得分:1)

我有类似的问题。通过多次调用'removeItem'解决了这个问题,并且每次都必须重新评估parametersIterator。

protected void updateDataSetParameters(OdaDataSetHandle dataSetHandle) throws SemanticException {
    int countMatches = StringUtils.countMatches(dataSetHandle.getQueryText(), "?");
    int paramIndex = 0;
    do {
        paramIndex = 0;
        PropertyHandle odaDataSetParameterProp = dataSetHandle.getPropertyHandle(OdaDataSetHandle.PARAMETERS_PROP);   
        Iterator parametersIterator = dataSetHandle.parametersIterator();
        while(parametersIterator.hasNext()) {
            Object next = parametersIterator.next();
            paramIndex++;
            if(paramIndex > countMatches) {
                odaDataSetParameterProp.removeItem(next);
                break;
            }
        }
        if(paramIndex < countMatches) {
            paramIndex++;
            OdaDataSetParameter dataSetParameter = createDataSetParameter(paramIndex);
            odaDataSetParameterProp.addItem(dataSetParameter);
        }
    } while(countMatches != paramIndex);
}

private OdaDataSetParameter createDataSetParameter(int paramIndex) {
    OdaDataSetParameter dataSetParameter = StructureFactory.createOdaDataSetParameter();
    dataSetParameter.setName("param_" + paramIndex);
    dataSetParameter.setDataType(DesignChoiceConstants.PARAM_TYPE_INTEGER);
    dataSetParameter.setNativeDataType(1);
    dataSetParameter.setPosition(paramIndex);
    dataSetParameter.setIsInput(true);
    dataSetParameter.setIsOutput(false);
    dataSetParameter.setExpressionProperty("defaultValue", new Expression("<evaluation script>", ExpressionType.JAVASCRIPT));
    return dataSetParameter;
}