Example #1
0
  private static byte[] getCsv(CellDataSet table, String delimiter, String enclosing) {
    if (table != null) {
      AbstractBaseCell[][] rowData = table.getCellSetBody();
      AbstractBaseCell[][] rowHeader = table.getCellSetHeaders();

      boolean offset = rowHeader.length > 0;
      String[][] result = new String[(offset ? 1 : 0) + rowData.length][];
      if (offset) {
        List<String> cols = new ArrayList<String>();
        for (int x = 0; x < rowHeader[0].length; x++) {
          String col = null;
          for (int y = rowHeader.length - 1; y >= 0; y--) {
            String value = rowHeader[y][x].getFormattedValue();
            if (value == null || "null".equals(value)) // $NON-NLS-1$
            {
              value = ""; // $NON-NLS-1$
            }
            if (col == null && StringUtils.isNotBlank(value)) {
              col = value;
            } else if (col != null && StringUtils.isNotBlank(value)) {
              col = value + "/" + col;
            }
          }
          cols.add(enclosing + col + enclosing);
        }
        result[0] = cols.toArray(new String[cols.size()]);
      }
      String[] lastKnownHeader = null;
      for (int x = 0; x < rowData.length; x++) {
        int xTarget = (offset ? 1 : 0) + x;
        if (lastKnownHeader == null) {
          lastKnownHeader = new String[rowData[x].length];
        }
        List<String> cols = new ArrayList<String>();
        for (int y = 0; y < rowData[x].length; y++) {
          String value = rowData[x][y].getFormattedValue();
          if (!SaikuProperties.webExportCsvUseFormattedValue) {
            if (rowData[x][y] instanceof DataCell
                && ((DataCell) rowData[x][y]).getRawNumber() != null) {
              value = ((DataCell) rowData[x][y]).getRawNumber().toString();
            }
          }
          if (rowData[x][y] instanceof MemberCell
              && StringUtils.isNotBlank(value)
              && !"null".equals(value)) {
            lastKnownHeader[y] = value;
          } else if (rowData[x][y] instanceof MemberCell
              && (StringUtils.isBlank(value) || "null".equals(value))) {
            value = (StringUtils.isNotBlank(lastKnownHeader[y]) ? lastKnownHeader[y] : null);
          }

          if (value == null || "null".equals(value)) {
            value = "";
          }
          value = value.replace("\"", "\"\"");
          value = enclosing + value + enclosing;
          cols.add(value);
        }
        result[xTarget] = cols.toArray(new String[cols.size()]);
      }
      return export(result, delimiter);
    }
    return new byte[0];
  }
Example #2
0
  public CellDataSet execute(String queryName, ICellSetFormatter formatter) {
    String runId = "runId:" + ID_GENERATOR.getAndIncrement();
    try {
      //			System.out.println("Execute: ID " + Thread.currentThread().getId() + " Name: " +
      // Thread.currentThread().getName());
      IQuery query = getIQuery(queryName);
      OlapConnection con =
          olapDiscoverService.getNativeConnection(query.getSaikuCube().getConnectionName());

      Long start = (new Date()).getTime();
      if (query.getScenario() != null) {
        log.info(
            runId
                + "\tQuery: "
                + query.getName()
                + " Setting scenario:"
                + query.getScenario().getId());
        con.setScenario(query.getScenario());
      }

      if (query.getTag() != null) {
        query = applyTag(query, con, query.getTag());
      }

      String mdx = query.getMdx();
      log.info(runId + "\tType:" + query.getType() + ":\n" + mdx);

      CellSet cellSet = query.execute();
      Long exec = (new Date()).getTime();

      if (query.getScenario() != null) {
        log.info("Query (" + queryName + ") removing scenario:" + query.getScenario().getId());
        con.setScenario(null);
      }

      CellDataSet result = OlapResultSetUtil.cellSet2Matrix(cellSet, formatter);
      Long format = (new Date()).getTime();
      log.info(
          runId
              + "\tSize: "
              + result.getWidth()
              + "/"
              + result.getHeight()
              + "\tExecute:\t"
              + (exec - start)
              + "ms\tFormat:\t"
              + (format - exec)
              + "ms\t Total: "
              + (format - start)
              + "ms");
      result.setRuntime(new Double(format - start).intValue());
      getIQuery(queryName).storeCellset(cellSet);
      return result;
    } catch (Exception e) {
      if (log.isInfoEnabled()) {
        String error = ExceptionUtils.getRootCauseMessage(e);
        log.info(runId + "\tException: " + error);
      }
      throw new SaikuServiceException(runId + "\tCan't execute query: " + queryName, e);
    } catch (Error e) {
      if (log.isInfoEnabled()) {
        String error = ExceptionUtils.getRootCauseMessage(e);
        log.info(runId + "\tError: " + error);
      }
      throw new SaikuServiceException(runId + "\tCan't execute query: " + queryName, e);
    }
  }