private JRVariable createHelperVariable(
      JRVariable variable, String nameSuffix, byte calculation) {
    JRDesignVariable helper = new JRDesignVariable();
    helper.setName(variable.getName() + nameSuffix);
    helper.setValueClassName(variable.getValueClassName());
    helper.setIncrementerFactoryClassName(variable.getIncrementerFactoryClassName());
    helper.setResetType(variable.getResetType());
    helper.setResetGroup(variable.getResetGroup());
    helper.setIncrementType(variable.getIncrementType());
    helper.setIncrementGroup(variable.getIncrementGroup());
    helper.setCalculation(calculation);
    helper.setSystemDefined(true);
    helper.setExpression(variable.getExpression());

    return helper;
  }
  private JRVariable createDistinctCountHelperVariable(JRVariable variable) {
    JRDesignVariable helper = new JRDesignVariable();
    helper.setName(variable.getName() + "_DISTINCT_COUNT");
    helper.setValueClassName(variable.getValueClassName());
    helper.setIncrementerFactoryClassName(JRDistinctCountIncrementerFactory.class.getName());
    helper.setResetType(JRVariable.RESET_TYPE_REPORT);

    if (variable.getIncrementType() != JRVariable.RESET_TYPE_NONE)
      helper.setResetType(variable.getIncrementType());

    helper.setResetGroup(variable.getIncrementGroup());
    helper.setCalculation(JRVariable.CALCULATION_NOTHING);
    helper.setSystemDefined(true);
    helper.setExpression(variable.getExpression());

    return helper;
  }
  private String generateExpression(JRExpression expression, byte evaluationType) {
    JRParameter jrParameter = null;
    JRField jrField = null;
    JRVariable jrVariable = null;

    StringBuffer sbuffer = new StringBuffer();

    JRExpressionChunk[] chunks = expression.getChunks();
    JRExpressionChunk chunk = null;
    String chunkText = null;
    if (chunks != null && chunks.length > 0) {
      for (int i = 0; i < chunks.length; i++) {
        chunk = chunks[i];

        chunkText = chunk.getText();
        if (chunkText == null) {
          chunkText = "";
        }

        switch (chunk.getType()) {
          case JRExpressionChunk.TYPE_TEXT:
            {
              sbuffer.append(chunkText);
              break;
            }
          case JRExpressionChunk.TYPE_PARAMETER:
            {
              jrParameter = (JRParameter) parametersMap.get(chunkText);

              sbuffer.append("((");
              sbuffer.append(jrParameter.getValueClassName());
              sbuffer.append(")super.parameter_");
              sbuffer.append(JRStringUtil.getJavaIdentifier(chunkText));
              sbuffer.append(".getValue())");

              break;
            }
          case JRExpressionChunk.TYPE_FIELD:
            {
              jrField = (JRField) fieldsMap.get(chunkText);

              sbuffer.append("((");
              sbuffer.append(jrField.getValueClassName());
              sbuffer.append(")super.field_");
              sbuffer.append(JRStringUtil.getJavaIdentifier(chunkText));
              sbuffer.append(".get");
              sbuffer.append((String) fieldPrefixMap.get(new Byte(evaluationType)));
              sbuffer.append("Value())");

              break;
            }
          case JRExpressionChunk.TYPE_VARIABLE:
            {
              jrVariable = (JRVariable) variablesMap.get(chunkText);

              sbuffer.append("((");
              sbuffer.append(jrVariable.getValueClassName());
              sbuffer.append(")super.variable_");
              sbuffer.append(JRStringUtil.getJavaIdentifier(chunkText));
              sbuffer.append(".get");
              sbuffer.append((String) variablePrefixMap.get(new Byte(evaluationType)));
              sbuffer.append("Value())");

              break;
            }
          case JRExpressionChunk.TYPE_RESOURCE:
            {
              jrParameter = (JRParameter) parametersMap.get(chunkText);

              sbuffer.append("super.evaluator.str(\"");
              sbuffer.append(chunkText);
              sbuffer.append("\")");

              break;
            }
        }
      }
    }

    if (sbuffer.length() == 0) {
      sbuffer.append("null");
    }

    return sbuffer.toString();
  }