/**
   * Renders the report for a given query to html
   *
   * @param sessionId
   * @param report
   * @param acceptedPage
   * @param template
   * @return
   * @throws Exception
   * @throws IOException
   * @throws ResourceException
   */
  public void renderReportHtml(
      String sessionId, String templateName, HtmlReport report, Integer acceptedPage)
      throws Exception {

    SaikuMasterModel model = sessionHolder.getModel(sessionId);

    if (model == null) {
      throw new SaikuAdhocException(
          Messages.getErrorString("ReportGeneratorService.ERROR_0001_MASTERMODEL_NOT_FOUND"));
    }

    sessionHolder.materializeModel(sessionId);
    String path = prptManager.getTemplatePath();
    String solution = prptManager.getSolution();

    if (templateName != null && !templateName.equals("default")) {
      ReportTemplate template = prptManager.getTemplate(path, solution, templateName);
      model.setReportTemplate(template);
    }

    MasterReport output = processReport(model);

    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    generateHtmlReport(
        output, stream, ParamUtils.getReportParameters("", model), report, acceptedPage);

    String string = stream.toString();

    //		if(log.isDebugEnabled()){
    //			writeHtmlFile(string);
    //		}

    report.setData(string);
  }
  public void saveCda(String sessionId, String path, String file)
      throws OperationNotSupportedException, IOException, TransformerFactoryConfigurationError,
          TransformerException {

    SaikuMasterModel model = sessionHolder.getModel(sessionId);

    if (!file.endsWith(".cda")) {
      file += ".cda";
    }

    String[] splits = ParamUtils.splitFirst(path.substring(1), "/");

    cdaManager.addDatasource(splits[0], splits[1], file, model.getCdaSettings().asXML());
    //		repository.writeFile(splits[0], splits[1], file, model.getCdaSettings().asXML());

  }
  /**
   * @param model
   * @param dataFactory
   * @param reportTemplate
   * @param output
   * @return
   * @throws ReportException
   * @throws ReportProcessingException
   * @throws SaikuAdhocException
   * @throws IOException
   * @throws ResourceException
   */
  protected MasterReport processReport(SaikuMasterModel model)
      throws ReportException, ReportProcessingException, SaikuAdhocException, ResourceException,
          IOException {

    CachingDataFactory dataFactory = null;

    try {

      model.deriveModels();

      final MasterReport reportTemplate = model.getDerivedModels().getReportTemplate();

      final WizardSpecification wizardSpecification = model.getWizardSpecification();

      reportTemplate.setDataFactory(model.getDerivedModels().getCdaDataFactory());
      reportTemplate.setQuery(model.getDerivedModels().getSessionId());

      reportTemplate.setAttribute(
          AttributeNames.Wizard.NAMESPACE, "wizard-spec", wizardSpecification);

      final ProcessingContext processingContext = new DefaultProcessingContext();
      final DataSchemaDefinition definition = reportTemplate.getDataSchemaDefinition();

      final ReportParameterValues parameterValues =
          StateUtilities.computeParameterValueSet(reportTemplate, getReportParameterValues(model));

      final ParameterDefinitionEntry[] parameterDefinitions =
          reportTemplate.getParameterDefinition().getParameterDefinitions();

      final DefaultFlowController flowController =
          new DefaultFlowController(
              processingContext, definition, parameterValues, parameterDefinitions, false);

      ensureSaikuPreProcessorIsAdded(reportTemplate, model);
      ensureHasOverrideWizardFormatting(reportTemplate, flowController);

      dataFactory = new CachingDataFactory(reportTemplate.getDataFactory(), false);
      dataFactory.initialize(
          processingContext.getConfiguration(),
          processingContext.getResourceManager(),
          processingContext.getContentBase(),
          processingContext.getResourceBundleFactory());

      final DefaultFlowController postQueryFlowController =
          flowController.performQuery(
              dataFactory,
              reportTemplate.getQuery(),
              reportTemplate.getQueryLimit(),
              reportTemplate.getQueryTimeout(),
              flowController.getMasterRow().getResourceBundleFactory());

      reportTemplate.setAttribute(
          AttributeNames.Wizard.NAMESPACE, AttributeNames.Wizard.ENABLE, Boolean.TRUE);

      ReportPreProcessor processor = new SaikuAdhocPreProcessor();
      ((SaikuAdhocPreProcessor) processor).setSaikuMasterModel(model);
      MasterReport output = processor.performPreProcessing(reportTemplate, postQueryFlowController);
      output.setAttribute(
          AttributeNames.Wizard.NAMESPACE, AttributeNames.Wizard.ENABLE, Boolean.FALSE);

      output.setAttribute(
          AttributeNames.Wizard.NAMESPACE, AttributeNames.Wizard.ENABLE, Boolean.FALSE);

      TemplateUtils.mergePageSetup(model, output);

      return output;

    } finally {
      dataFactory.close();
    }
  }