예제 #1
0
  protected static String getUnitName(JRReport report, JRDataset dataset, String nameSuffix) {
    String className;
    if (dataset.isMainDataset()) {
      className = report.getName();
    } else {
      className = report.getName() + "_" + dataset.getName();
    }

    className = JRStringUtil.getJavaIdentifier(className) + nameSuffix;

    return className;
  }
  public JRField[] getFields(IReportConnection con, JRDataset reportDataset, Map parameters)
      throws JRException, UnsupportedOperationException {
    /** Copyright (C) 2006 CINCOM SYSTEMS, INC. All Rights Reserved www.cincom.com */
    if (con instanceof it.businesslogic.ireport.connection.JRXMLADataSourceConnection) {
      try {
        String query = reportDataset.getQuery().getText();

        java.util.ArrayList v_fields = getFields(query, (JRXMLADataSourceConnection) con);

        // Translate
        JRField[] final_fields = new JRField[v_fields.size()];
        for (int i = 0; i < v_fields.size(); ++i) {
          XmlaFieldNode f = (XmlaFieldNode) v_fields.get(i);
          JRDesignField field = new JRDesignField();
          field.setName(f.getName());
          field.setValueClassName(f.getClassType());
          field.setDescription(f.getDescription());
          final_fields[i] = field;
        }

        return final_fields;

      } catch (Exception ex) {
        throw new JRException(ex);
      }
    } else {
      throw new JRException(
          "The active connection is not of type XMLA. Activate an XMLA connection first.");
    }
  }
예제 #3
0
 private void setGroups(JRDataset dataset, JRFillObjectFactory factory) {
   JRGroup[] jrGroups = dataset.getGroups();
   if (jrGroups != null && jrGroups.length > 0) {
     groups = new JRFillGroup[jrGroups.length];
     for (int i = 0; i < groups.length; i++) {
       groups[i] = factory.getGroup(jrGroups[i]);
     }
   }
 }
예제 #4
0
 private void setFields(JRDataset dataset, JRFillObjectFactory factory) {
   JRField[] jrFields = dataset.getFields();
   if (jrFields != null && jrFields.length > 0) {
     fields = new JRFillField[jrFields.length];
     fieldsMap = new HashMap();
     for (int i = 0; i < fields.length; i++) {
       fields[i] = factory.getField(jrFields[i]);
       fieldsMap.put(fields[i].getName(), fields[i]);
     }
   }
 }
예제 #5
0
 private void setParameters(JRDataset dataset, JRFillObjectFactory factory) {
   JRParameter[] jrParameters = dataset.getParameters();
   if (jrParameters != null && jrParameters.length > 0) {
     parameters = new JRFillParameter[jrParameters.length];
     parametersMap = new HashMap();
     for (int i = 0; i < parameters.length; i++) {
       parameters[i] = factory.getParameter(jrParameters[i]);
       parametersMap.put(parameters[i].getName(), parameters[i]);
     }
   }
 }
예제 #6
0
  /**
   * Creates a fill dataset object.
   *
   * @param filler the filelr
   * @param dataset the template dataset
   * @param factory the fill object factory
   */
  protected JRFillDataset(JRBaseFiller filler, JRDataset dataset, JRFillObjectFactory factory) {
    factory.put(dataset, this);

    this.filler = filler;
    this.parent = dataset;
    this.isMain = dataset.isMainDataset();

    scriptletClassName = dataset.getScriptletClass();
    resourceBundleBaseName = dataset.getResourceBundle();
    whenResourceMissingType = dataset.getWhenResourceMissingType();

    query = dataset.getQuery();

    setParameters(dataset, factory);

    setFields(dataset, factory);

    setVariables(dataset, factory);

    setGroups(dataset, factory);
  }
예제 #7
0
  private void setVariables(JRDataset dataset, JRFillObjectFactory factory) {
    JRVariable[] jrVariables = dataset.getVariables();
    if (jrVariables != null && jrVariables.length > 0) {
      List variableList = new ArrayList(jrVariables.length * 3);

      variablesMap = new HashMap();
      for (int i = 0; i < jrVariables.length; i++) {
        addVariable(jrVariables[i], variableList, factory);
      }

      setVariables(variableList);
    }
  }
  public JRQueryExecuter createQueryExecuter(JRDataset jRDataset, Map map) throws JRException {
    JRQuery query = jRDataset.getQuery();
    String queryString = query.getText();

    return new CSVQueryExecuter(queryString);
  }
예제 #9
0
 public JRExpression getFilterExpression() {
   return parent.getFilterExpression();
 }
예제 #10
0
 public JRPropertiesMap getPropertiesMap() {
   return parent.getPropertiesMap();
 }
예제 #11
0
 public boolean hasProperties() {
   return parent.hasProperties();
 }
예제 #12
0
 public String getResourceBundle() {
   return parent.getResourceBundle();
 }
예제 #13
0
 public JRSortField[] getSortFields() {
   return parent.getSortFields();
 }
예제 #14
0
 public String getScriptletClass() {
   return parent.getScriptletClass();
 }
예제 #15
0
 public String getName() {
   return parent.getName();
 }
예제 #16
0
  public JRField[] getFields(IReportConnection irConn, JRDataset reportDataset, Map parameters)
      throws JRException, UnsupportedOperationException {

    if (irConn == null || !irConn.isJDBCConnection()) {
      throw new JRException(
          "The active connection is not of type JDBC. Activate a JDBC connection first.");
    }

    String query = reportDataset.getQuery().getText();
    String error_msg = "";
    Connection con = null;
    PreparedStatement ps = null;

    try {
      // look for parameters in the query and replace them with default values.
      // parameters look something like
      // $P{QuoteGroupID}
      // or
      // $P!{OrderByClause}
      java.util.List queryParams = new ArrayList();
      JRParameter[] jrParams = reportDataset.getParameters();
      for (int k = 0; k < jrParams.length; ++k) {
        JRParameter parameter = jrParams[k];

        String p1 = "$P{" + parameter.getName() + "}";
        String p2 = "$P!{" + parameter.getName() + "}";

        Object defValue = parameters.get(parameter.getName());

        int ip1 = query.indexOf(p1);
        while (ip1 != -1) {
          // add a query parameter
          if (defValue == null) {
            throw new IllegalArgumentException(
                "Please set a " + "default value for the parameter '" + parameter.getName() + "'");
          }

          String before = query.substring(0, ip1);
          String after = query.substring(ip1 + p1.length());
          query = before + " ? " + after;
          queryParams.add(defValue);
          ip1 = query.indexOf(p1);
        }

        int ip2 = query.indexOf(p2);
        while (ip2 != -1) {
          // String replacement, Altering the SQL statement.
          if (defValue == null) {
            throw new IllegalArgumentException(
                "Please set a " + "default value for the parameter '" + parameter.getName() + "'");
          }

          String before = query.substring(0, ip2);
          String after = query.substring(ip2 + p2.length());
          query = before + "" + defValue.toString() + "" + after;
          ip2 = query.indexOf(p2);
        }
      }

      con = irConn.getConnection();

      ps = con.prepareStatement(query);
      //                for(int pc=0; pc<queryParams.size(); pc++ ) {
      //                    ps.setObject(pc+1, queryParams.get(pc) );
      //                }

      for (int pc = 0; pc < queryParams.size(); pc++) {
        Class parameterType = queryParams.get(pc).getClass();

        if (java.lang.Boolean.class.isAssignableFrom(parameterType)) {
          if (queryParams.get(pc) == null) {
            ps.setNull(pc + 1, Types.BIT);
          } else {
            ps.setBoolean(pc + 1, ((Boolean) queryParams.get(pc)).booleanValue());
          }
        } else if (java.lang.Byte.class.isAssignableFrom(parameterType)) {
          if (queryParams.get(pc) == null) {
            ps.setNull(pc + 1, Types.TINYINT);
          } else {
            ps.setByte(pc + 1, ((Byte) queryParams.get(pc)).byteValue());
          }
        } else if (java.lang.Double.class.isAssignableFrom(parameterType)) {
          if (queryParams.get(pc) == null) {
            ps.setNull(pc + 1, Types.DOUBLE);
          } else {
            ps.setDouble(pc + 1, ((Double) queryParams.get(pc)).doubleValue());
          }
        } else if (java.lang.Float.class.isAssignableFrom(parameterType)) {
          if (queryParams.get(pc) == null) {
            ps.setNull(pc + 1, Types.FLOAT);
          } else {
            ps.setFloat(pc + 1, ((Float) queryParams.get(pc)).floatValue());
          }
        } else if (java.lang.Integer.class.isAssignableFrom(parameterType)) {
          if (queryParams.get(pc) == null) {
            ps.setNull(pc + 1, Types.INTEGER);
          } else {
            ps.setInt(pc + 1, ((Integer) queryParams.get(pc)).intValue());
          }
        } else if (java.lang.Long.class.isAssignableFrom(parameterType)) {
          if (queryParams.get(pc) == null) {
            ps.setNull(pc + 1, Types.BIGINT);
          } else {
            ps.setLong(pc + 1, ((Long) queryParams.get(pc)).longValue());
          }
        } else if (java.lang.Short.class.isAssignableFrom(parameterType)) {
          if (queryParams.get(pc) == null) {
            ps.setNull(pc + 1, Types.SMALLINT);
          } else {
            ps.setShort(pc + 1, ((Short) queryParams.get(pc)).shortValue());
          }
        } else if (java.math.BigDecimal.class.isAssignableFrom(parameterType)) {
          if (queryParams.get(pc) == null) {
            ps.setNull(pc + 1, Types.DECIMAL);
          } else {
            ps.setBigDecimal(pc + 1, (BigDecimal) queryParams.get(pc));
          }
        } else if (java.lang.String.class.isAssignableFrom(parameterType)) {
          if (queryParams.get(pc) == null) {
            ps.setNull(pc + 1, Types.VARCHAR);
          } else {
            ps.setString(pc + 1, queryParams.get(pc).toString());
          }
        } else if (java.sql.Timestamp.class.isAssignableFrom(parameterType)) {
          if (queryParams.get(pc) == null) {
            ps.setNull(pc + 1, Types.TIMESTAMP);
          } else {
            ps.setTimestamp(pc + 1, (java.sql.Timestamp) queryParams.get(pc));
          }
        } else if (java.sql.Time.class.isAssignableFrom(parameterType)) {
          if (queryParams.get(pc) == null) {
            ps.setNull(pc + 1, Types.TIME);
          } else {
            ps.setTime(pc + 1, (java.sql.Time) queryParams.get(pc));
          }
        } else if (java.util.Date.class.isAssignableFrom(parameterType)) {
          if (queryParams.get(pc) == null) {
            ps.setNull(pc + 1, Types.DATE);
          } else {
            ps.setDate(pc + 1, new java.sql.Date(((java.util.Date) queryParams.get(pc)).getTime()));
          }
        } else {
          if (queryParams.get(pc) == null) {
            ps.setNull(pc + 1, Types.JAVA_OBJECT);
          } else {
            ps.setObject(pc + 1, queryParams.get(pc));
          }
        }
      }

      // Some JDBC drivers don't supports this method...
      try {
        ps.setFetchSize(0);
      } catch (Exception e) {
      }

      ResultSet rs = ps.executeQuery();

      // if (in < num) return;

      ResultSetMetaData rsmd = rs.getMetaData();

      // if (in < num) return;

      List columns = new ArrayList();
      for (int i = 1; i <= rsmd.getColumnCount(); ++i) {
        JRDesignField field = new JRDesignField();
        field.setName(rsmd.getColumnLabel(i));
        field.setValueClassName(Misc.getJdbcTypeClass(rsmd, i));
        field.setDescription("");
        columns.add(field);
      }

      JRField[] final_fields = new JRField[columns.size()];
      for (int i = 0; i < final_fields.length; ++i) {
        final_fields[i] = (JRField) columns.get(i);
      }

      return final_fields;

    } catch (IllegalArgumentException ie) {
      throw new JRException(ie.getMessage());
    } catch (NoClassDefFoundError ex) {
      ex.printStackTrace();
      error_msg = "NoClassDefFoundError!!\nCheck your classpath!";
      throw new JRException(error_msg);
    } catch (java.sql.SQLException ex) {
      error_msg = "SQL problems:\n" + ex.getMessage();
      throw new JRException(error_msg);
    } catch (Exception ex) {
      ex.printStackTrace();
      error_msg =
          "General problem:\n"
              + ex.getMessage()
              + "\n\nCheck username and password; is the DBMS active ?!";
      throw new JRException(error_msg);
    } catch (Throwable t) {
      throw new JRException(t.getMessage());
    } finally {
      if (ps != null)
        try {
          ps.close();
        } catch (Exception e) {
        }
      if (con != null)
        try {
          con.close();
        } catch (Exception e) {
        }
    }
  }