Skip to Content
0

How update Crystal Report data from database

Dec 11, 2017 at 05:13 AM

31

avatar image
Former Member

The project use Java sping mvc, have bean call ‘report.rpt' file.

Whatever,same follow codeing , export PDF file not newest data.

I think, maybe rpt not refresh data from database.

And try refresh data by :reportClientDoc.getReportSource().refresh(); , but can not export file to web.

Up description,i need more help , thanks.

Java bean:

/**
 * 
 */
package com.te.spring.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.crystaldecisions.report.web.viewer.ReportExportControl;
import com.crystaldecisions.reports.sdk.ParameterFieldController;
import com.crystaldecisions.reports.sdk.ReportClientDocument;
import com.crystaldecisions.sdk.occa.report.data.Fields;
import com.crystaldecisions.sdk.occa.report.exportoptions.ExportOptions;
import com.crystaldecisions.sdk.occa.report.exportoptions.ReportExportFormat;
import com.te.bean.empschedule.THrmScheduleDept;

/**
 * @author chen-yu
 * @email mimi2008yu@163.com
 */
public class Schedulecurrentreportoutfile implements Controller {
    private SessionFactory sessionFactory;
    private Session session;
    private Transaction transaction;
    
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
    /* (non-Javadoc)
     * @see org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver#resolveModelAndView(java.lang.reflect.Method, java.lang.Class, java.lang.Object, org.springframework.ui.ExtendedModelMap, org.springframework.web.context.request.NativeWebRequest)
     */
    public ModelAndView handleRequest(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {
        // TODO Auto-generated method stub
        
        if (!(arg0.getParameter("deptoption")=="") && !(arg0.getParameter("deptoption")==null)) {
            
            ReportClientDocument reportClientDoc = new ReportClientDocument();
            //ReportDocument reportClientDoc = new ReportDocument();
            
            reportClientDoc.open("VIEW/RPT/Schedulereportcurrent.rpt", 0);
            reportClientDoc.getReportSource().dispose();
            
            System.out.println("Refresh...");
            //reportClientDoc.getReportSource().refresh();
            System.out.println("Refreshed");
            
                        
            
            String SelectionFormula = "";
            
            if (!(arg0.getParameter("deptoption")=="") && !(arg0.getParameter("deptoption")==null)) {
                SelectionFormula = "{t_hrm_schedule_current.SCHEDULE_PARENT_DEPT}='" + arg0.getParameter("deptoption") +"'";
                //System.out.println(SelectionFormula);
            }            
            
            if (!(arg0.getParameter("groupsselect")=="") && !(arg0.getParameter("groupsselect")==null)) {
                SelectionFormula = SelectionFormula + " and {t_hrm_schedule_current.SCHEDULE_GROUP_ID}='" + arg0.getParameter("groupsselect") +"'";
                //System.out.println(SelectionFormula);
            }
            
            if (!(arg0.getParameter("empsselect")=="") && !(arg0.getParameter("empsselect")==null)) {
                SelectionFormula = SelectionFormula + " and {t_hrm_schedule_current.EMP_ID}='" + arg0.getParameter("empsselect") +"'";
                //System.out.println(SelectionFormula);
            }
            
            if (SelectionFormula != "") {
                reportClientDoc.setRecordSelectionFormula(SelectionFormula);                
            }
            
            
            //UI Parameter: Department
            session = getSessionFactory().openSession();
            THrmScheduleDept dept = (THrmScheduleDept)session.createQuery("From THrmScheduleDept Where parentDeptId='" + Integer.parseInt(arg0.getParameter("deptoption")) + "'").uniqueResult();
            
            Fields fields = reportClientDoc.getDataDefController().getDataDefinition().getParameterFields();
            ParameterFieldController parameterFieldController = reportClientDoc.getDataDefController().getParameterFieldController();
            parameterFieldController.setCurrentValue("", "Department", dept.getParentDeptName());
            
            //reportClientDoc.getReportSource().refresh();
            
            //Export File RTF
            ReportExportControl exportControl=new ReportExportControl();
            ExportOptions exportOptions=new ExportOptions();
            exportOptions.setExportFormatType(ReportExportFormat.PDF);
            
            exportControl.setReportSource(reportClientDoc.getReportSource());
            exportControl.setExportOptions(exportOptions);
            exportControl.setExportAsAttachment(true);
            exportControl.setName("NextMonthScheduleReport");
            
            System.out.println("2");
            exportControl.processHttpRequest(arg0, arg1, arg0.getServletContext(), null);
            exportControl.dispose();
            
            reportClientDoc.close();
            System.out.println("1");
        }
        return null;
    }

}

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

avatar image
Former Member Dec 11, 2017 at 06:14 AM
0

Add two line code, can resolve data refresh.

            reportClientDoc.open("VIEW/RPT/Schedulereportcurrent.rpt",OpenReportOptions._openAsReadOnly);
            reportClientDoc.getDatabaseController().logon("user", "password");
            reportClientDoc.getReportSource().refresh();
Share
10 |10000 characters needed characters left characters exceeded