/**
   * Updates values in InputParameterAttributes to the given report parameter.
   *
   * @param dataAttrs the latest input parameter attributes
   * @param cachedDataAttrs the cached input parameter attributes
   * @param reportParam the report parameter
   * @param setHandle the ROM data set that has the corresponding data set parameter
   * @throws SemanticException
   */
  private void updateInputParameterAttrsToReportParam(
      InputParameterAttributes inputParamAttrs,
      InputParameterAttributes cachedInputParamAttrs,
      AbstractScalarParameterHandle reportParam,
      OdaDataSetHandle setHandle)
      throws SemanticException {
    if (inputParamAttrs == null) return;

    InputParameterUIHints paramUiHints = inputParamAttrs.getUiHints();
    if (paramUiHints != null && reportParam.getContainer() instanceof ParameterGroupHandle) {
      ParameterGroupHandle paramGroup = (ParameterGroupHandle) reportParam.getContainer();

      InputParameterUIHints cachedParamUiHints =
          cachedInputParamAttrs == null ? null : cachedInputParamAttrs.getUiHints();

      String cachedGroupPromptDisplayName =
          cachedParamUiHints == null ? null : cachedParamUiHints.getGroupPromptDisplayName();

      String groupPromptDisplayName = paramUiHints.getGroupPromptDisplayName();

      if (cachedGroupPromptDisplayName == null
          || !cachedGroupPromptDisplayName.equals(groupPromptDisplayName)) {
        paramGroup.setDisplayName(groupPromptDisplayName);

        paramGroup.setDisplayNameKey(paramUiHints.getGroupPromptDisplayNameKey());
      }
    }

    updateInputElementAttrsToReportParam(
        inputParamAttrs.getElementAttributes(),
        cachedInputParamAttrs == null ? null : cachedInputParamAttrs.getElementAttributes(),
        reportParam,
        setHandle);
  }
  /**
   * @param inputAttrs
   * @param inputParamAttrs
   * @param paramHandle
   */
  protected void updateDefaultStaticValues(
      InputElementAttributes inputAttrs, AbstractScalarParameterHandle paramHandle) {
    // update default values.

    StaticValues newValues = null;
    List<Expression> tmpValues = paramHandle.getDefaultValueList();
    if (tmpValues != null) {
      for (int i = 0; i < tmpValues.size(); i++) {
        if (newValues == null) newValues = designFactory.createStaticValues();

        Expression tmpExpr = tmpValues.get(i);
        String odaValue = null;

        // for the constant, there is no need to remove quotes
        if (ExpressionType.CONSTANT.equalsIgnoreCase(tmpExpr.getType())) {
          odaValue = tmpExpr.getStringExpression();
        } else {
          odaValue =
              ParameterValueUtil.toODAValue(
                  tmpValues.get(i).getStringExpression(), paramHandle.getDataType());
        }
        newValues.add(odaValue);
      }
    }
    inputAttrs.setDefaultValues(newValues);
  }
  /**
   * Creates an DataElementAttributes with the given ROM report parameter.
   *
   * @param paramHandle the ROM report parameter.
   * @return the created DataElementAttributes
   */
  private DataElementAttributes updateDataElementAttrs(
      DataElementAttributes dataAttrs, AbstractScalarParameterHandle paramHandle) {
    DataElementAttributes retDataAttrs = dataAttrs;

    if (retDataAttrs == null) retDataAttrs = designFactory.createDataElementAttributes();

    // retDataAttrs.setNullability( DataSetParameterAdapter
    // .newElementNullability( paramHandle.allowNll( ) ) );

    retDataAttrs.setNullability(
        DataSetParameterAdapter.newElementNullability(
            getReportParamAllowMumble(paramHandle, ALLOW_NULL_PROP_NAME)));

    DataElementUIHints uiHints = designFactory.createDataElementUIHints();

    String text = paramHandle.getPromptText();
    String textKey = paramHandle.getPromptTextID();

    if (text != null || textKey != null) {
      uiHints.setDisplayName(text);
      uiHints.setDisplayNameKey(textKey);
    }

    text = paramHandle.getHelpText();
    textKey = paramHandle.getHelpTextKey();

    if (text != null || textKey != null) {
      uiHints.setDescription(text);
      uiHints.setDescriptionKey(textKey);
    }

    retDataAttrs.setUiHints(uiHints);

    return retDataAttrs;
  }
  /**
   * Updates values in InputElementAttributes to the given report parameter.
   *
   * @param elementAttrs the latest input element attributes
   * @param cachedElementAttrs the cached input element attributes
   * @param reportParam the report parameter
   * @param setHandle the ROM data set that has the corresponding data set parameter
   * @throws SemanticException
   */
  protected void updateInputElementAttrsToReportParam(
      InputElementAttributes elementAttrs,
      InputElementAttributes cachedElementAttrs,
      AbstractScalarParameterHandle reportParam,
      OdaDataSetHandle setHandle)
      throws SemanticException {
    if (elementAttrs == null) return;

    // update default values.

    updateDefaultValueToReportParam(elementAttrs, cachedElementAttrs, reportParam);

    // update isOptional value
    Boolean isOptional = Boolean.valueOf(elementAttrs.isOptional());
    Boolean cachedIsOptional =
        cachedElementAttrs == null ? null : Boolean.valueOf(cachedElementAttrs.isOptional());
    if (!CompareUtil.isEquals(cachedIsOptional, isOptional))
      setReportParamIsRequired(reportParam, ALLOW_BLANK_PROP_NAME, isOptional.booleanValue());

    // update selection choices
    updateROMSelectionList(
        elementAttrs.getStaticValueChoices(),
        cachedElementAttrs == null ? null : cachedElementAttrs.getStaticValueChoices(),
        reportParam);

    // update dynamic list
    DynamicValuesQuery valueQuery = elementAttrs.getDynamicValueChoices();
    AdapterUtil.updateROMDyanmicList(
        valueQuery,
        cachedElementAttrs == null ? null : cachedElementAttrs.getDynamicValueChoices(),
        reportParam,
        setHandle);

    // for dynamic parameter, the flag is in DynamicValuesQuery is true
    // for static parameter, the DynamicValuesQuery is null or the flag is
    // false

    DynamicValuesQuery cachedValueQuery =
        cachedElementAttrs == null ? null : cachedElementAttrs.getDynamicValueChoices();

    boolean isEnabled = (valueQuery == null) ? false : valueQuery.isEnabled();

    if (reportParam.getContainer() != null
        && reportParam.getContainer() instanceof CascadingParameterGroupHandle) isEnabled = true;

    if (cachedValueQuery == null || cachedValueQuery.isEnabled() != isEnabled) {
      if (isEnabled) reportParam.setValueType(DesignChoiceConstants.PARAM_VALUE_TYPE_DYNAMIC);
      else reportParam.setValueType(DesignChoiceConstants.PARAM_VALUE_TYPE_STATIC);
    }
  }
 /**
  * Returns the boolean value of allowMumble properties. Only support "allowNull" and "allowBlank"
  * properties.
  *
  * <p>"allowMumble" properties has been removed ROM. However, to do conversion, still need to know
  * their values.
  *
  * @param param the parameter
  * @param obsoletePropName either "allowNull" or "allowBlank".
  * @param value
  * @throws SemanticException
  */
 protected void setReportParamIsRequired(
     AbstractScalarParameterHandle param, String obsoletePropName, boolean value)
     throws SemanticException {
   if (ALLOW_NULL_PROP_NAME.equalsIgnoreCase(obsoletePropName)
       || ALLOW_BLANK_PROP_NAME.equalsIgnoreCase(obsoletePropName)) param.setIsRequired(!value);
   else assert false;
 }
  /**
   * Returns the boolean value of allowMumble properties. Only support "allowNull" and "allowBlank"
   * properties.
   *
   * <p>"allowMumble" properties has been removed ROM. However, to do conversion, still need to know
   * their values.
   *
   * @param param the parameter
   * @param propName either "allowNull" or "allowBlank".
   * @return <code>true</code> if the parameter allows the value. Otherwise <code>false</code>.
   */
  protected boolean getReportParamAllowMumble(
      AbstractScalarParameterHandle param, String propName) {
    if (ALLOW_NULL_PROP_NAME.equalsIgnoreCase(propName)
        || ALLOW_BLANK_PROP_NAME.equalsIgnoreCase(propName)) return !param.isRequired();

    assert false;
    return false;
  }
 /**
  * Sets the default value for ROM data set parameter.
  *
  * @param setParam the ROM data set parameter
  * @param literalValue the value
  */
 private void setROMDefaultValue(AbstractScalarParameterHandle setParam, Object value)
     throws SemanticException {
   List<Expression> newValues = null;
   if (!AdapterUtil.isNullExpression(value)) {
     assert value instanceof Expression;
     newValues = new ArrayList<Expression>();
     newValues.add(
         new Expression(
             ((Expression) value).getExpression(), ((Expression) value).getUserDefinedType()));
   }
   setParam.setDefaultValueList(newValues);
 }
 public static void addParameterSortBy(
     QueryDefinition queryDefn, AbstractScalarParameterHandle parameter, IModelAdapter adapter) {
   String sortBy = parameter.getSortByColumn();
   if (sortBy != null) {
     String sortDirection = parameter.getSortDirection();
     if (sortDirection != null) {
       org.eclipse.birt.report.model.api.Expression mexpr =
           (org.eclipse.birt.report.model.api.Expression)
               parameter
                   .getExpressionProperty(AbstractScalarParameter.SORT_BY_COLUMN_PROP)
                   .getValue();
       ScriptExpression dexpr = adapter.adaptExpression(mexpr);
       SortDefinition sort = new SortDefinition();
       sort.setExpression(dexpr);
       boolean direction =
           DesignChoiceConstants.SORT_DIRECTION_ASC.equalsIgnoreCase(sortDirection);
       sort.setSortDirection(direction ? ISortDefinition.SORT_ASC : ISortDefinition.SORT_DESC);
       queryDefn.addSort(sort);
     }
   }
 }
  /**
   * Updates values in DataElementAttributes to the given report parameter.
   *
   * @param dataAttrs the latest data element attributes
   * @param cachedDataAttrs the cached data element attributes
   * @param reportParam the report parameter
   * @throws SemanticException
   */
  private void updateDataElementAttrsToReportParam(
      DataElementAttributes dataAttrs,
      DataElementAttributes cachedDataAttrs,
      AbstractScalarParameterHandle reportParam)
      throws SemanticException {

    if (dataAttrs == null) return;

    boolean allowsNull = dataAttrs.allowsNull();
    if (cachedDataAttrs == null || cachedDataAttrs.allowsNull() != allowsNull)
      setReportParamIsRequired(reportParam, ALLOW_NULL_PROP_NAME, dataAttrs.allowsNull());

    // reportParam.setAllowNull( dataAttrs.allowsNull( ) );

    DataElementUIHints dataUiHints = dataAttrs.getUiHints();
    DataElementUIHints cachedDataUiHints =
        (cachedDataAttrs == null ? null : cachedDataAttrs.getUiHints());
    if (dataUiHints != null) {
      String displayName = dataUiHints.getDisplayName();
      String cachedDisplayName =
          cachedDataUiHints == null ? null : cachedDataUiHints.getDisplayName();

      boolean isChanged = false;

      if (cachedDisplayName == null || !cachedDisplayName.equals(displayName)) {
        isChanged = true;
        reportParam.setPromptText(displayName);
        reportParam.setPromptTextID(dataUiHints.getDisplayNameKey());
      }
      if (!isChanged && cachedDataUiHints != null && cachedDisplayName != null) {
        reportParam.setPromptText(cachedDisplayName);
        reportParam.setPromptTextID(cachedDataUiHints.getDisplayNameKey());
      }

      isChanged = false;
      String description = dataUiHints.getDescription();
      String cachedDescription =
          cachedDataUiHints == null ? null : cachedDataUiHints.getDescription();
      if (cachedDescription == null || !cachedDescription.equals(description)) {
        isChanged = true;
        reportParam.setHelpText(description);
        reportParam.setHelpTextKey(dataUiHints.getDescriptionKey());
      }

      if (!isChanged && cachedDataUiHints != null && cachedDescription != null) {
        reportParam.setHelpText(cachedDescription);
        reportParam.setHelpTextKey(cachedDataUiHints.getDescriptionKey());
      }
    }
  }
  public ParameterHelper(AbstractScalarParameterHandle param, ULocale ulocale, TimeZone timezone) {
    this.labelColumnName = getLabelColumnName(param);
    this.valueColumnName = getValueColumnName(param);
    this.valueType = param.getDataType();
    this.alreadySorted = param.getSortByColumn() != null;
    this.distinct = param.distinct();

    String sortDirection = param.getSortDirection();
    boolean sortByLabel = "label".equalsIgnoreCase(param.getSortBy());
    if (param instanceof ScalarParameterHandle) {
      parameterType = SCALAR_PARAMETER;
      ScalarParameterHandle parameter = (ScalarParameterHandle) param;
      this.fixedOrder = parameter.isFixedOrder();
      if (param.getLabelExpr()
          == null) { // if no label expression was set, apply pattern to value column
        pattern = parameter.getPattern();
      }
    } else {
      parameterType = FILTER_PARAMETER;
    }

    if (!(parameterType == SCALAR_PARAMETER && fixedOrder)
        && !alreadySorted
        && sortDirection != null) {
      boolean sortDirectionValue =
          DesignChoiceConstants.SORT_DIRECTION_ASC.equalsIgnoreCase(sortDirection);
      Comparator choiceComparator =
          new SelectionChoiceComparator(sortByLabel, pattern, sortDirectionValue, ulocale);
      this.comparator = new DistinctComparatorDecorator(choiceComparator, distinct);
    }
    this.converter = new ReportParameterConverter(pattern, ulocale, timezone);
  }
  public static void addParameterBinding(
      QueryDefinition queryDefn, AbstractScalarParameterHandle parameter, IModelAdapter adapter)
      throws DataException {
    String labelColumnName = getLabelColumnName(parameter);
    String valueColumnName = getValueColumnName(parameter);
    if (labelColumnName != null) {
      org.eclipse.birt.report.model.api.Expression mexpr =
          (org.eclipse.birt.report.model.api.Expression)
              parameter.getExpressionProperty(AbstractScalarParameter.LABEL_EXPR_PROP).getValue();
      ScriptExpression dexpr = adapter.adaptExpression(mexpr);
      addBinding(queryDefn, labelColumnName, dexpr);
    }
    org.eclipse.birt.report.model.api.Expression mexpr =
        (org.eclipse.birt.report.model.api.Expression)
            parameter.getExpressionProperty(AbstractScalarParameter.VALUE_EXPR_PROP).getValue();

    String dataType = parameter.getDataType();
    if (DesignChoiceConstants.PARAM_TYPE_STRING.equals(dataType)) {
      dataType = DesignChoiceConstants.PARAM_TYPE_JAVA_OBJECT;
    }
    ScriptExpression dexpr = adapter.adaptExpression(mexpr, dataType);
    addBinding(queryDefn, valueColumnName, dexpr);
  }
  /**
   * Updates values in report parameter by given ROM data set parameter.
   *
   * @param reportParam the report parameter
   * @param dataSetParam the data set parameter
   * @param updateDefaultValue the flag which indicates if the default value need to be forced
   *     updated.
   * @throws SemanticException
   */
  protected void updateLinkedReportParameterFromROMParameter(
      AbstractScalarParameterHandle reportParam,
      OdaDataSetParameterHandle dataSetParam,
      boolean updateDefaultValue)
      throws SemanticException {
    assert reportParam != null;

    if (dataSetParam == null) return;

    // should not convert report parameter name here.

    Object defaultValue =
        dataSetParam.getExpressionProperty(OdaDataSetParameter.DEFAULT_VALUE_MEMBER).getValue();
    String paramName = dataSetParam.getParamName();

    if (StringUtil.isBlank(paramName)) {
      dataSetParam.setParamName(reportParam.getName());
    }
    if (updateDefaultValue) setROMDefaultValue(reportParam, defaultValue);
  }
  /**
   * Refreshes property values of the given report parameter by the given parameter definition and
   * cached parameter definition. If values in cached parameter definition is null or values in
   * cached parameter definition are not equal to values in parameter defnition, update values in
   * given report parameter.
   *
   * @param reportParam the report parameter
   * @param paramDefn the ODA parameter definition
   * @param cachedParamDefn the cached ODA parameter definition in designerValues
   * @param dataType the updated data type
   * @param setHandle the ROM data set that has the corresponding data set parameter
   * @throws SemanticException if value in the data set design is invalid
   */
  void updateLinkedReportParameter(
      AbstractScalarParameterHandle reportParam,
      ParameterDefinition paramDefn,
      ParameterDefinition cachedParamDefn,
      OdaDataSetHandle setHandle)
      throws SemanticException {
    if (paramDefn == null) return;

    CommandStack cmdStack = reportParam.getModuleHandle().getCommandStack();
    try {
      cmdStack.startTrans(null);

      updateAbstractScalarParameter(reportParam, paramDefn, cachedParamDefn, setHandle);

    } catch (SemanticException e) {
      cmdStack.rollback();
      throw e;
    }

    cmdStack.commit();
  }
  /**
   * Creates a ODA InputParameterAttributes with the given ROM report parameter.
   *
   * @param inputParamAttrs
   * @param paramHandle the ROM report parameter.
   * @param dataSetDesign
   * @return the created <code>InputParameterAttributes</code>.
   */
  protected InputParameterAttributes updateInputElementAttrs(
      InputParameterAttributes inputParamAttrs,
      AbstractScalarParameterHandle paramHandle,
      DataSetDesign dataSetDesign) {
    InputParameterAttributes retInputParamAttrs = inputParamAttrs;

    if (inputParamAttrs == null)
      retInputParamAttrs = designFactory.createInputParameterAttributes();

    InputElementAttributes inputAttrs = retInputParamAttrs.getElementAttributes();
    if (inputAttrs == null) inputAttrs = designFactory.createInputElementAttributes();

    // update default values.

    updateDefaultStaticValues(inputAttrs, paramHandle);

    // inputAttrs.setOptional( paramHandle.allowBlank( ) );
    inputAttrs.setOptional(getReportParamAllowMumble(paramHandle, ALLOW_BLANK_PROP_NAME));

    ScalarValueChoices staticChoices = null;
    Iterator selectionList = paramHandle.choiceIterator();
    while (selectionList.hasNext()) {
      if (staticChoices == null) staticChoices = designFactory.createScalarValueChoices();
      SelectionChoiceHandle choice = (SelectionChoiceHandle) selectionList.next();

      ScalarValueDefinition valueDefn = designFactory.createScalarValueDefinition();
      valueDefn.setValue(choice.getValue());

      String label = choice.getLabel();
      String labelKey = choice.getLabelKey();

      if (label != null || labelKey != null) {
        valueDefn.setDisplayName(label);
        valueDefn.setDisplayNameKey(labelKey);
      }

      staticChoices.getScalarValues().add(valueDefn);
    }
    inputAttrs.setStaticValueChoices(staticChoices);

    ExpressionHandle valueExpr =
        paramHandle.getExpressionProperty(IAbstractScalarParameterModel.VALUE_EXPR_PROP);
    ExpressionHandle labelExpr =
        paramHandle.getExpressionProperty(IAbstractScalarParameterModel.LABEL_EXPR_PROP);

    DynamicValuesQuery valueQuery =
        updateDynamicValueQuery(
            paramHandle.getDataSet(),
            valueExpr.getValue(),
            labelExpr.getValue(),
            dataSetDesign,
            DesignChoiceConstants.PARAM_VALUE_TYPE_DYNAMIC.equalsIgnoreCase(
                paramHandle.getValueType()));
    inputAttrs.setDynamicValueChoices(valueQuery);

    if (paramHandle.getContainer() instanceof ParameterGroupHandle) {
      ParameterGroupHandle groupHandle = (ParameterGroupHandle) paramHandle.getContainer();

      InputParameterUIHints paramUiHints = designFactory.createInputParameterUIHints();

      String text = groupHandle.getDisplayName();
      String textKey = groupHandle.getDisplayNameKey();

      if (text != null || textKey != null) {
        paramUiHints.setGroupPromptDisplayName(text);
        paramUiHints.setGroupPromptDisplayNameKey(textKey);
      }

      retInputParamAttrs.setUiHints(paramUiHints);
    }

    retInputParamAttrs.setElementAttributes(inputAttrs);
    return retInputParamAttrs;
  }
 public static String getLabelColumnName(AbstractScalarParameterHandle parameter) {
   if (parameter.getLabelExpr() == null) {
     return null;
   }
   return LABEL_PREFIX + "_" + parameter.getName();
 }
 public static String getValueColumnName(AbstractScalarParameterHandle parameter) {
   return VALUE_PREFIX + "_" + parameter.getName();
 }