Exemple #1
0
  public void testSqlQuery() throws Exception {

    // Define an outputStream
    OutputStream out = System.out;

    logger.info("Building CDA settings from sample file");

    final SettingsManager settingsManager = SettingsManager.getInstance();
    URL file = this.getClass().getResource("sample-sql.cda");
    File settingsFile = new File(file.toURI());
    final CdaSettings cdaSettings =
        settingsManager.parseSettingsFile(settingsFile.getAbsolutePath());
    logger.debug("Doing query on Cda - Initializing CdaEngine");
    final CdaEngine engine = CdaEngine.getInstance();

    QueryOptions queryOptions = new QueryOptions();
    queryOptions.setDataAccessId("1");
    queryOptions.addParameter("orderDate", "2003-04-01");
    queryOptions.setOutputType(ExporterEngine.OutputType.XML);
    // queryOptions.addParameter("status","In Process");

    logger.info("Doing first query");
    engine.doQuery(out, cdaSettings, queryOptions);

    logger.info("Doing query with different parameters");
    queryOptions = new QueryOptions();
    queryOptions.setDataAccessId("1");
    queryOptions.addParameter("orderDate", "2004-01-01");
    engine.doQuery(out, cdaSettings, queryOptions);

    // Querying 2nd time to test cache
    logger.info("Doing query using the initial parameters - Cache should be used");
    queryOptions = new QueryOptions();
    queryOptions.setDataAccessId("1");
    queryOptions.addParameter("orderDate", "2003-04-01");
    engine.doQuery(out, cdaSettings, queryOptions);

    // Querying 2nd time to test cache
    logger.info("Doing query again to see if cache expires");
    try {
      Thread.sleep(6000);
    } catch (InterruptedException e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    }
    engine.doQuery(out, cdaSettings, queryOptions);
  }
Exemple #2
0
  // @Exposed(accessLevel = AccessLevel.PUBLIC, outputType = MimeType.PLAIN_TEXT)
  public boolean writeCdaFile(
      final OutputStream out,
      final String path,
      final String solution,
      final String file,
      final String data)
      throws Exception {
    // TODO: Validate the filename in some way, shape or form!
    IRepositoryAccess repository = CdaEngine.getEnvironment().getRepositoryAccess();
    // final ICommonParameterProvider requestParams = requParam;
    // Check if the file exists and we have permissions to write to it
    String relativePath = getRelativePath(path, solution, file);

    if (repository.canWrite(relativePath) && data != null) {
      // TODO: this is really to write anything here
      switch (repository.publishFile(relativePath, data.getBytes(ENCODING), true)) {
        case OK:
          SettingsManager.getInstance().clearCache();
          writeOut(out, "File saved.");
          return true;
        case FAIL:
          writeOut(out, "Save unsuccessful!");
          logger.error("writeCdaFile: saving " + relativePath);
          break;
      }
    } else {
      throw new AccessDeniedException(relativePath, null);
    }
    return false;
  }
Exemple #3
0
  // @Exposed(accessLevel = AccessLevel.PUBLIC)
  public void listParameters(
      final OutputStream out,
      final String path,
      final String solution,
      final String file,
      final String outputType,
      final String dataAccessId)
      throws Exception {
    final CdaEngine engine = CdaEngine.getInstance();
    // final ICommonParameterProvider requestParams = requParam;
    final String relativePath = getRelativePath(path, solution, file);
    IRepositoryAccess repAccess = CdaEngine.getEnvironment().getRepositoryAccess();
    logger.debug("Do Query: getRelativePath:" + relativePath);
    final CdaSettings cdaSettings = SettingsManager.getInstance().parseSettingsFile(relativePath);

    // Handle the query itself and its output format...
    final DiscoveryOptions discoveryOptions = new DiscoveryOptions();
    discoveryOptions.setOutputType(outputType);
    discoveryOptions.setDataAccessId(dataAccessId);

    String mimeType =
        ExporterEngine.getInstance().getExporter(discoveryOptions.getOutputType()).getMimeType();
    setResponseHeaders(mimeType);

    engine.listParameters(out, cdaSettings, discoveryOptions);
  }
Exemple #4
0
  // @Exposed(accessLevel = AccessLevel.PUBLIC)
  public void unwrapQuery(
      final OutputStream out,
      final String path,
      final String solution,
      final String file,
      final String uuid)
      throws Exception {
    final CdaEngine engine = CdaEngine.getInstance();
    // final ICommonParameterProvider requestParams = requParam;
    final String relativePath = getRelativePath(path, solution, file);
    final CdaSettings cdaSettings = SettingsManager.getInstance().parseSettingsFile(relativePath);
    // String uuid = requestParams.getStringParameter("uuid", null);
    QueryOptions queryOptions = engine.unwrapQuery(uuid);
    if (queryOptions != null) {
      Exporter exporter =
          ExporterEngine.getInstance()
              .getExporter(queryOptions.getOutputType(), queryOptions.getExtraSettings());

      String attachmentName = exporter.getAttachmentName();
      String mimeType = (attachmentName == null) ? null : getMimeType(attachmentName);
      if (StringUtils.isEmpty(mimeType)) {
        mimeType = exporter.getMimeType();
      }

      if (relativePath != null && uuid != null) ; // XXX  ==  if (this.parameterProviders != null)
      {
        setResponseHeaders(mimeType, attachmentName);
      }
      engine.doQuery(out, cdaSettings, queryOptions);
    } else {
      logger.error("unwrapQuery: uuid " + uuid + " not found.");
    }
  }
  public void testFormulaCacheSql() throws Exception {
    // Define an outputStream
    OutputStream out = System.out;

    logger.info("Building CDA settings from sample file");

    final SettingsManager settingsManager = SettingsManager.getInstance();
    URL file = this.getClass().getResource("sample-sql-formula.cda");
    File settingsFile = new File(file.toURI());
    final CdaSettings cdaSettings =
        settingsManager.parseSettingsFile(settingsFile.getAbsolutePath());
    logger.debug("Doing query on Cda - Initializing CdaEngine");
    final CdaEngine engine = CdaEngine.getInstance();

    QueryOptions queryOptions = new QueryOptions();
    queryOptions.setDataAccessId("1");
    queryOptions.addParameter("orderDate", "${TODAY()}");
    queryOptions.setOutputType("csv");

    logger.info("Doing first query --> TODAY()");
    engine.doQuery(out, cdaSettings, queryOptions);

    logger.info("Doing query with different parameters");
    queryOptions = new QueryOptions();
    queryOptions.setDataAccessId("1");
    queryOptions.addParameter("orderDate", "${DATE(2004;1;1)}");
    engine.doQuery(out, cdaSettings, queryOptions);

    // Querying 2nd time to test cache (formula translated before cache check)
    logger.info("Doing query using manual TODAY - Cache should be used");
    queryOptions = new QueryOptions();
    queryOptions.setDataAccessId("1");
    Calendar cal = Calendar.getInstance();
    queryOptions.addParameter(
        "orderDate",
        "${DATE("
            + cal.get(Calendar.YEAR)
            + ";"
            + (cal.get(Calendar.MONTH) + 1)
            + ";"
            + cal.get(Calendar.DAY_OF_MONTH)
            + ")}");
    engine.doQuery(out, cdaSettings, queryOptions);
  }
Exemple #6
0
  public void testSqlQuery() throws Exception {

    // Define an outputStream
    OutputStream out = System.out;

    logger.info("Building CDA settings from sample file");

    final SettingsManager settingsManager = SettingsManager.getInstance();
    URL file = this.getClass().getResource("sample-mondrian.cda");
    File settingsFile = new File(file.toURI());
    final CdaSettings cdaSettings =
        settingsManager.parseSettingsFile(settingsFile.getAbsolutePath());
    logger.debug("Doing query on Cda - Initializing CdaEngine");
    final CdaEngine engine = CdaEngine.getInstance();

    QueryOptions queryOptions = new QueryOptions();
    queryOptions.setDataAccessId("2");
    queryOptions.setOutputType("json");
    queryOptions.addParameter("status", "Shipped");

    logger.info("Doing query");
    engine.doQuery(out, cdaSettings, queryOptions);
  }
Exemple #7
0
  // @Exposed(accessLevel = AccessLevel.PUBLIC, outputType = MimeType.JSON)
  public void listDataAccessTypes(final OutputStream out, final boolean refreshCache)
      throws Exception {
    // boolean refreshCache = Boolean.parseBoolean(requParam.getStringParameter("refreshCache",
    // "false"));

    DataAccessConnectionDescriptor[] data =
        SettingsManager.getInstance().getDataAccessDescriptors(refreshCache);

    StringBuilder output = new StringBuilder("");
    if (data != null) {
      output.append("{\n");
      for (DataAccessConnectionDescriptor datum : data) {
        output.append(datum.toJSON()).append(",\n");
      }
      writeOut(out, output.toString().replaceAll(",\n\\z", "\n}"));
    }
  }
Exemple #8
0
  public void doQuery(final OutputStream out, DoQueryParameters parameters) throws Exception {
    final CdaEngine engine = CdaEngine.getInstance();
    final QueryOptions queryOptions = new QueryOptions();

    final String path =
        getRelativePath(parameters.getPath(), parameters.getSolution(), parameters.getFile());
    final CdaSettings cdaSettings = SettingsManager.getInstance().parseSettingsFile(path);

    // Handle paging options
    // We assume that any paging options found mean that the user actively wants paging.
    final long pageSize = parameters.getPageSize();
    final long pageStart = parameters.getPageStart();
    final boolean paginate = parameters.isPaginateQuery();
    if (pageSize > 0 || pageStart > 0 || paginate) {
      if (pageSize > Integer.MAX_VALUE || pageStart > Integer.MAX_VALUE) {
        throw new ArithmeticException("Paging values too large");
      }
      queryOptions.setPaginate(true);
      queryOptions.setPageSize(pageSize > 0 ? (int) pageSize : paginate ? DEFAULT_PAGE_SIZE : 0);
      queryOptions.setPageStart(
          pageStart > 0 ? (int) pageStart : paginate ? DEFAULT_START_PAGE : 0);
    }

    // Support for bypassCache (we'll maintain the name we use in CDE
    /*if(requestParams.hasParameter("bypassCache")){
      queryOptions.setCacheBypass(Boolean.parseBoolean(requestParams.getStringParameter("bypassCache","false")));
    }*/

    queryOptions.setCacheBypass(parameters.isBypassCache());

    // Handle the query itself and its output format...
    queryOptions.setOutputType(parameters.getOutputType());
    queryOptions.setDataAccessId(parameters.getDataAccessId());
    try {
      queryOptions.setOutputIndexId(parameters.getOutputIndexId());
    } catch (NumberFormatException e) {
      logger.error("Illegal outputIndexId '" + parameters.getOutputIndexId() + "'");
    }

    final ArrayList<String> sortBy = new ArrayList<String>();
    String[] def = {};
    for (Object obj : parameters.getSortBy()) {
      if (!((String) obj).equals("")) {
        sortBy.add((String) obj);
      }
    }
    queryOptions.setSortBy(sortBy);

    // ... and the query parameters
    // We identify any pathParams starting with "param" as query parameters and extra settings
    // prefixed with "setting"
    @SuppressWarnings("unchecked")
    final Iterator settings = parameters.getExtraSettings().entrySet().iterator();
    while (settings.hasNext()) {
      Map.Entry<String, Object> pairs = (Map.Entry) settings.next();
      final String name = pairs.getKey();
      final Object parameter = pairs.getValue();
      queryOptions.addSetting(name, (String) parameter);
    }
    final Iterator params = parameters.getExtraParams().entrySet().iterator();
    while (params.hasNext()) {

      Map.Entry<String, Object> pairs = (Map.Entry) params.next();
      final String name = pairs.getKey();
      final Object parameter = pairs.getValue();
      queryOptions.addParameter(name, parameter);

      //      if(name.startsWith(PREFIX_PARAMETER)){
      //          queryOptions.addParameter(name.substring(PREFIX_PARAMETER.length()), parameter);
      //          logger.debug("##########"+name+"##############");

    }
    /*
    if (param.startsWith(PREFIX_PARAMETER))
    {
      queryOptions.addParameter(param.substring(PREFIX_PARAMETER.length()), requestParams.getParameter(param));
    }
    else if (param.startsWith(PREFIX_SETTING))
    {
      queryOptions.addSetting(param.substring(PREFIX_SETTING.length()), requestParams.getStringParameter(param, ""));
    }*/

    if (parameters.isWrapItUp()) {
      String uuid = engine.wrapQuery(out, cdaSettings, queryOptions);
      logger.debug("doQuery: query wrapped as " + uuid);
      writeOut(out, uuid);
      return;
    }

    // we'll allow for the special "callback" param to be used, and passed as settingcallback to
    // jsonp exports
    if (!parameters.getJsonCallback().equals("<blank>")) {
      queryOptions.addSetting(JSONP_CALLBACK, parameters.getJsonCallback());
    }
    Exporter exporter =
        ExporterEngine.getInstance()
            .getExporter(queryOptions.getOutputType(), queryOptions.getExtraSettings());

    String attachmentName = exporter.getAttachmentName();
    String mimeType = (attachmentName == null) ? null : getMimeType(attachmentName);
    if (StringUtils.isEmpty(mimeType)) {
      mimeType = exporter.getMimeType();
    }

    if (parameters != null) ; // XXX + FIXME ==  if (this.parameterProviders != null)
    {
      setResponseHeaders(mimeType, attachmentName);
    }
    // Finally, pass the query to the engine
    engine.doQuery(out, cdaSettings, queryOptions);
  }
Exemple #9
0
  // @Exposed(accessLevel = AccessLevel.ADMIN, outputType = MimeType.PLAIN_TEXT)
  public void clearCache(final OutputStream out) throws Exception {
    SettingsManager.getInstance().clearCache();
    AbstractDataAccess.clearCache();

    out.write("Cache cleared".getBytes());
  }