Example #1
0
  private String generateDataTableJson(TimeChartData chartData) throws DataSourceException {
    DataTable data = new DataTable();
    ArrayList<ColumnDescription> cd = new ArrayList<ColumnDescription>();
    cd.add(new ColumnDescription("date", ValueType.DATETIME, "Date"));

    for (String serie : chartData.getSeries()) {
      cd.add(new ColumnDescription(serie, ValueType.NUMBER, serie));
    }
    data.addColumns(cd);
    try {

      for (TimeChartDataValue timeSerie : chartData.getValues()) {

        TableRow row = new TableRow();
        GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
        calendar.setTimeInMillis(
            TimeZone.getDefault().getOffset(timeSerie.getTimestamp().getTime())
                + timeSerie.getTimestamp().getTime());
        row.addCell(new TableCell(new DateTimeValue(calendar)));
        for (Double value : timeSerie.getValues()) {
          if (value != null) {
            row.addCell(value);
          } else {
            row.addCell(Value.getNullValueFromValueType(ValueType.NUMBER));
          }
        }
        data.addRow(row);
      }

    } catch (TypeMismatchException e) {
      System.out.println("Invalid type!");
    }
    Query query = new Query();
    DataSourceParameters parameters = new DataSourceParameters("");
    DataSourceRequest request = new DataSourceRequest(query, parameters, ULocale.UK);
    String generateResponse = DataSourceHelper.generateResponse(data, request);

    return generateResponse;
  }
 public DataTable generateDataTable(Query query, HttpServletRequest request)
     throws DataSourceException {
   String url = request.getParameter("url");
   System.out.println(
       new Timestamp((new java.util.Date()).getTime())
           + "\t--\tAZ\tRead datasource from "
           + url
           + "\treadCSVdatasource");
   if (StringUtils.isEmpty(url)) {
     System.out.println(
         new Timestamp((new java.util.Date()).getTime())
             + "\t--\tERR\tURL parameter not provided\treadCSVdatasource");
     url = getServletContext().getRealPath("data1.csv");
     // throw new DataSourceException(ReasonType.INVALID_REQUEST, "url parameter not provided");
   }
   // System.out.println("reading file "+ url);
   Reader reader;
   try {
     reader = new BufferedReader(new InputStreamReader(new URL(url).openStream()));
   } catch (MalformedURLException e) {
     System.out.println(
         new Timestamp((new java.util.Date()).getTime())
             + "\t--\tERR\tURL is malformed: "
             + url
             + "\treadCSVdatasource");
     throw new DataSourceException(ReasonType.INVALID_REQUEST, "url is malformed: " + url);
   } catch (IOException e) {
     System.out.println(
         new Timestamp((new java.util.Date()).getTime())
             + "\t--\tERR\tCouldn't open URL: "
             + url
             + "  "
             + e.getMessage()
             + "\treadCSVdatasource");
     throw new DataSourceException(ReasonType.INVALID_REQUEST, "Couldn't read from url: " + url);
   }
   DataTable dataTable = null;
   ULocale requestLocale = DataSourceHelper.getLocaleFromRequest(request);
   try {
     // Note: We assume that all the columns in the CSV file are text columns. In cases where the
     // column types are known in advance, this behavior can be overridden by passing a list of
     // ColumnDescription objects specifying the column types. See CsvDataSourceHelper.read() for
     // more details.
     dataTable = CsvDataSourceHelper.read(reader, null, true, requestLocale);
   } catch (IOException e) {
     System.out.println(
         new Timestamp((new java.util.Date()).getTime())
             + "\t--\tERR\tCouldn't read from URL: "
             + url
             + "  "
             + e.getMessage()
             + "\treadCSVdatasource");
     throw new DataSourceException(ReasonType.INVALID_REQUEST, "Couldn't read from url: " + url);
   }
   System.out.println(
       new Timestamp((new java.util.Date()).getTime())
           + "\t--\tAZ\tRead from "
           + url
           + " FINISHED.\treadCSVdatasource");
   return dataTable;
 }