Package it.eng.spagobi.engines.qbe.query

Examples of it.eng.spagobi.engines.qbe.query.ReportRunner


    Vector extractedFields = null;
    Map params = null;
    TemplateBuilder templateBuilder = null;
    String templateContent = null;
    File reportFile = null;
    ReportRunner runner = null;
    boolean isFormEngineInstance = false;
   
    logger.debug("IN");
   
    try {
      super.service(request, response)
     
      mimeType = getAttributeAsString( MIME_TYPE );
      logger.debug(MIME_TYPE + ": " + mimeType);   
      responseType = getAttributeAsString( RESPONSE_TYPE );
      logger.debug(RESPONSE_TYPE + ": " + responseType);
         
      Assert.assertNotNull(getEngineInstance(), "It's not possible to execute " + this.getActionName() + " service before having properly created an instance of EngineInstance class");
     
      transaction = (getEngineInstance().getDataSource()).getTransaction()
      transaction.open();
     
      fileExtension = MimeUtils.getFileExtension( mimeType );
      writeBackResponseInline = RESPONSE_TYPE_INLINE.equalsIgnoreCase(responseType);
     
      isFormEngineInstance = getEngineInstance().getTemplate().getProperty("formJSONTemplate") != null;
      if (!isFormEngineInstance) {
        // case of standard QBE
       
        Assert.assertNotNull(getEngineInstance().getActiveQuery(), "Query object cannot be null in oder to execute " + this.getActionName() + " service");
        Assert.assertTrue(getEngineInstance().getActiveQuery().isEmpty() == false, "Query object cannot be empty in oder to execute " + this.getActionName() + " service");
           
        Assert.assertNotNull(mimeType, "Input parameter [" + MIME_TYPE + "] cannot be null in oder to execute " + this.getActionName() + " service");   
        Assert.assertTrue( MimeUtils.isValidMimeType( mimeType ) == true, "[" + mimeType + "] is not a valid value for " + MIME_TYPE + " parameter");
       
        Assert.assertNotNull(responseType, "Input parameter [" + RESPONSE_TYPE + "] cannot be null in oder to execute " + this.getActionName() + " service");   
        Assert.assertTrue( RESPONSE_TYPE_INLINE.equalsIgnoreCase(responseType) || RESPONSE_TYPE_ATTACHMENT.equalsIgnoreCase(responseType), "[" + responseType + "] is not a valid value for " + RESPONSE_TYPE + " parameter");
       
        statement = getEngineInstance().getDataSource().createStatement( getEngineInstance().getActiveQuery() );   
        //logger.debug("Parametric query: [" + statement.getQueryString() + "]");
       
        statement.setParameters( getEnv() );
        jpaQueryStr = statement.getQueryString();
        logger.debug("Executable HQL/JPQL query: [" + jpaQueryStr + "]");
       
        sqlQuery = statement.getSqlQueryString();
        Assert.assertNotNull(sqlQuery, "The SQL query is needed while exporting results.");
       
      } else {
        // case of FormEngine
       
        sqlQuery = this.getAttributeFromSessionAsString(ExecuteDetailQueryAction.LAST_DETAIL_QUERY);
        Assert.assertNotNull(sqlQuery, "The detail query was not found, maybe you have not execute the detail query yet.");
      }
      logger.debug("Executable SQL query: [" + sqlQuery + "]");
     
      logger.debug("Exctracting fields ...");
      fieldsReader = new SQLFieldsReader(sqlQuery, transaction.getSQLConnection());
      try {
        extractedFields = fieldsReader.readFields();
      } catch (Exception e) {
        logger.debug("Impossible to extract fields from query");
        throw new SpagoBIEngineException("Impossible to extract fields from query: " + jpaQueryStr, e);
      }
      logger.debug("Fields extracted succesfully");
     
      Assert.assertTrue(getEngineInstance().getActiveQuery().getDataMartSelectFields(true).size()+getEngineInstance().getActiveQuery().getInLineCalculatedSelectFields(true).size() == extractedFields.size(),
          "The number of fields extracted from query resultset cannot be different from the number of fields specified into the query select clause");
     
      decorateExtractedFields( extractedFields );
     
      params = new HashMap();
      params.put("pagination", getPaginationParamVaue(mimeType) );
     
     
      SourceBean config = (SourceBean)ConfigSingleton.getInstance();   
      SourceBean baseTemplateFileSB = (SourceBean)config.getAttribute("QBE.TEMPLATE-BUILDER.BASE-TEMPLATE");
      String baseTemplateFileStr = null;
      if(baseTemplateFileSB != null) baseTemplateFileStr = baseTemplateFileSB.getCharacters();
      File baseTemplateFile = null;
      if(baseTemplateFileStr != null) baseTemplateFile = new File(baseTemplateFileStr);
     
      templateBuilder = new TemplateBuilder(sqlQuery, extractedFields, params, baseTemplateFile);
      templateContent = templateBuilder.buildTemplate();
     
      if( !"text/jrxml".equalsIgnoreCase( mimeType ) ) {
        if( "application/vnd.ms-excel".equalsIgnoreCase( mimeType ) ) {
         
          IDataStore dataStore = null;
         
          if (!isFormEngineInstance) {
            // case of standard QBE
           
            IDataSet dataSet = null;
           
            Integer limit = 0;
            Integer start = 0;
            Integer maxSize = QbeEngineConfig.getInstance().getResultLimit()
            boolean isMaxResultsLimitBlocking = QbeEngineConfig.getInstance().isMaxResultLimitBlocking();
            dataSet = QbeDatasetFactory.createDataSet(statement);
            dataSet.setAbortOnOverflow(isMaxResultsLimitBlocking);
           
            Map userAttributes = new HashMap();
            UserProfile profile = (UserProfile)this.getEnv().get(EngineConstants.ENV_USER_PROFILE);
            Iterator it = profile.getUserAttributeNames().iterator();
            while(it.hasNext()) {
              String attributeName = (String)it.next();
              Object attributeValue = profile.getUserAttribute(attributeName);
              userAttributes.put(attributeName, attributeValue);
            }
            dataSet.addBinding("attributes", userAttributes);
            dataSet.addBinding("parameters", this.getEnv());
            logger.debug("Executing query ...");
            dataSet.loadData(start, limit, (maxSize == null? -1: maxSize.intValue()));
           
            dataStore = dataSet.getDataStore();
         
          } else {
            // case of FormEngine
           
            JDBCDataSet dataset = new JDBCDataSet();
            IDataSource datasource = (IDataSource) this.getEnv().get( EngineConstants.ENV_DATASOURCE );
            dataset.setDataSource(datasource);
            dataset.setUserProfileAttributes(UserProfileUtils.getProfileAttributes( (UserProfile) this.getEnv().get(EngineConstants.ENV_USER_PROFILE)));
            dataset.setQuery(sqlQuery);
            logger.debug("Executing query ...");
            dataset.loadData();
            dataStore = dataset.getDataStore();
          }
         
          Exporter exp = new Exporter(dataStore);
          exp.setExtractedFields(extractedFields);
         
          Workbook wb = exp.exportInExcel();
         
          File file = File.createTempFile("workbook", ".xls");
          FileOutputStream stream = new FileOutputStream(file);
          wb.write(stream);
          stream.flush();
          stream.close();
          try {       
            writeBackToClient(file, null, writeBackResponseInline, "workbook.xls", mimeType);
          } catch (IOException ioe) {
            throw new SpagoBIEngineException("Impossible to write back the responce to the client", ioe);
          finally{
            if(file != null && file.exists()) {
              try {
                file.delete();
              } catch (Exception e) {
                logger.warn("Impossible to delete temporary file " + file, e);
              }
            }
          }
     
        }else{
          try {
            reportFile = File.createTempFile("report", ".rpt");
          } catch (IOException ioe) {
            throw new SpagoBIEngineException("Impossible to create a temporary file to store the template generated on the fly", ioe);
          }
         
          setJasperClasspath();
          connection = transaction.getSQLConnection();
         
          runner = new ReportRunner( );
          Locale locale = this.getLocale();
          try {
            runner.run( templateContent, reportFile, mimeType, connection, locale);
          catch (Exception e) {
            throw new SpagoBIEngineException("Impossible compile or to export the report", e);
          }
         
          try {       
View Full Code Here

TOP

Related Classes of it.eng.spagobi.engines.qbe.query.ReportRunner

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.