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; }
/** * Generates the data table. This servlet assumes a special parameter that contains the CSV URL * from which to load the data. */ @Override public DataTable generateDataTable(Query query, HttpServletRequest request) throws DataSourceException { if (!this.isAuthorized(request)) return null; int user_id = (Integer) request.getSession().getAttribute(AuthorizedAction.USER_ID); String prj_id = request.getParameter(Env.PROJECT_PARAM_NAME); String sub_id = request.getParameter(Env.SUBJECT_PARAM_NAME); String date = request.getParameter(Env.DATE_PARAM_NAME); String time = request.getParameter(Env.TIME_PARAM_NAME); boolean includeExtraFields = "1".equals(request.getParameter(Env.TYPE_PARAM_NAME)); logger.info( "ActivityResultServlet.generateDataTable: " + prj_id + "/" + sub_id + "/" + date + "/" + time + "/" + includeExtraFields); DataTable dataTable = new DataTable(); List<ColumnDescription> cds = new LinkedList<ColumnDescription>(); cds.add(new ColumnDescription("TIME", ValueType.NUMBER, "TIME")); cds.add(new ColumnDescription("ACTIVITY", ValueType.NUMBER, "ACTIVITY")); // Below 3 columns are algorithm specific if (includeExtraFields) { cds.add(new ColumnDescription("SMA", ValueType.NUMBER, "SMA")); cds.add(new ColumnDescription("SMA-H", ValueType.NUMBER, "SMA-H")); cds.add(new ColumnDescription("SMA-L", ValueType.NUMBER, "SMA-L")); } dataTable.addColumns(cds); List<TableRow> rows = new LinkedList<TableRow>(); try { ActData ad = ReportManager.getAct(prj_id, sub_id, date, Integer.parseInt(time)); double sma_h = Double.parseDouble(SettingManager.getValue(user_id, "sma_h")); double sma_l = Double.parseDouble(SettingManager.getValue(user_id, "sma_l")); for (int i = 0; i < ad.getAct().length; ++i) { // expend to 100Hz data to sync with raw signal for (int j = 0; j < 100; ++j) { TableRow tr = new TableRow(); tr.addCell((ad.getStart_second() + i) * 100 + j); tr.addCell(ad.getAct()[i].num_filt); // Below 3 columns are algorithm specific if (includeExtraFields) { tr.addCell(ad.getAct()[i].sma); tr.addCell(sma_h); tr.addCell(sma_l); } rows.add(tr); } } } catch (Exception e) { logger.error(e.getMessage(), e); throw new DataSourceException(ReasonType.OTHER, e.getMessage()); } logger.info("Return data (rows): " + rows.size() + " x " + rows.get(0).getCells().size()); dataTable.addRows(rows); return dataTable; }