Results 1 to 3 of 3
  1. #1
    anavagomez is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default take code from java class put it in JSP page.

    hello,

    I am a java beginner and i have a task assigned that I need help with.

    I am testing the latest Crystal Reports for Java component.

    This is what I understand so far, the struts-config.xml file contains the actions of what class to call and what action to follow, either success or failure.


    struts:

    <action path="/getCrystalReport" type="com.struts.report.crystal.GetCrystalReportAc tion">
    <forward name="success" path="/CrystalReportViewer.jsp"/>
    <forward name="failure" path="/do/modalMessagePage" />
    </action>


    Then the Java class "GetCrystalReportAction" contains the functions that will be executed to get to the page, and return the value to be passed to the action on the struts file.


    I am aware I maight be violating the standards of MVC, but I would like to test something.


    I need to take the code that gets the report path in the java class (I think starting in line 92) and place it in the JSP page CrystalReportViewer.jsp

    This is just to test if I can get to the page and display the reports.


    I am including the code for the java class, and the JSP page.

    I hope someone can spare some time and lend me a hand. I have been trying to figure this out and have gotten nowhere.


    Thank you very much.
    Last edited by anavagomez; 04-26-2012 at 06:57 PM.

  2. #2
    anavagomez is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: take code from java class put it in JSP page.

    The class code

    Java Code:
    //Created by MyEclipse Struts
    // XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.0/xslt/JavaClass.xsl
    
    package com.struts.report.crystal;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionMessage;
    import org.apache.struts.action.ActionMessages;
    
    import com.crystaldecisions.reports.sdk.ReportClientDocument;
    import com.crystaldecisions.sdk.occa.report.data.ConnectionInfo;
    import com.crystaldecisions.sdk.occa.report.data.ConnectionInfos;
    import com.crystaldecisions.sdk.occa.report.lib.ReportSDKException;
    import com.exception.DAOException;
    import com.exception.SystemException;
    import com.report.Report;
    import com.report.ReportFactory;
    import com.security.Credential;
    import com.util.Constants;
    import com.util.WebUtils;
    
    public class GetCrystalReportAction extends Action {
    
    	ReportFactory fac = new ReportFactory();
    
    	public ActionForward execute(
    		ActionMapping mapping,
    		ActionForm form,
    		HttpServletRequest request,
    		HttpServletResponse response) 
    	throws SystemException{
    
    		// *** Security ***
    		HttpSession session = request.getSession();
    		
    		Credential credential = (Credential)session.getAttribute(Constants.CREDENTIAL);
    		if(credential == null || session.isNew()){
    			request.setAttribute(Constants.LOGIN_TARGET, WebUtils.getRequestInfo(request));
    			return mapping.findForward("loginPage");
    		}
    
    		String reportId = request.getParameter("reportId");
    		if(reportId==null)
    			reportId = (String)request.getAttribute("reportId");
    		String location = request.getParameter(Constants.RG_LOCATION);
    		if(location!=null){
    			session.setAttribute(Constants.RG_LOCATION, location);
    		}
    		
    		if(reportId==null || reportId.trim().length()==0){
    			ActionMessages messages = new ActionMessages();
    			messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.generic", "There was a problem getting you report. ReportId was missing", Constants.FORMAT_SQL_SERVER_DATE));
    			return mapping.findForward("failure");
    		}
    
    		int nReportId = 0;
    		try{
    			nReportId = Integer.parseInt(reportId);
    		}
    		catch(NumberFormatException e){
    			ActionMessages messages = new ActionMessages();
    			messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.generic", "There was a problem getting you report. ReportId was not a number", Constants.FORMAT_SQL_SERVER_DATE));
    			return mapping.findForward("failure");
    		}
    		
    		Report report;
    		try {
    			report = fac.getReport(nReportId);
    		} catch (DAOException e) {
    			throw new SystemException("Error getting report.", e);
    		}
    		if(report==null){
    			ActionMessages messages = new ActionMessages();
    			messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.generic", "No report found for your selection.", Constants.FORMAT_SQL_SERVER_DATE));
    			return mapping.findForward("failure");
    		}
    		
    		if("PDF".equals(report.getFormat())){
    			request.setAttribute("GO_TO_PDF", "Y");
    		}
    		
    		if (!credential.getPermissions().hasPermission(report.getPermissionId())) 
    			return mapping.findForward("accessDeniedPage");
    		
    		String reportPath = report.getPath();
    		reportPath = getLocationReportPath(reportPath, location);
    		
    		ReportClientDocument oReportClientDocument = new ReportClientDocument();
            try {
    			oReportClientDocument.open(reportPath, 0);
    		} catch (ReportSDKException e) {
    			throw new SystemException("Could not get report.", e);
    		}
            Object reportSource = oReportClientDocument.getReportSource();
            request.getSession().setAttribute("reportSource", reportSource);
            
            ConnectionInfos oConnectionInfos = new ConnectionInfos();
            ConnectionInfo oConnectionInfo = new ConnectionInfo();
    
            //Set username and password for the report's database
            oConnectionInfo.setUserName(report.getUserId());
            oConnectionInfo.setPassword(report.getPassword());
            oConnectionInfos.add(oConnectionInfo);
            
            request.getSession().setAttribute("oConnectionInfos", oConnectionInfos);
            return mapping.findForward("success");
    	}
    
    	public String getLocationReportPath(String reportPath, String location){
    		if(location!=null){
    			int index = reportPath.lastIndexOf("/");
    			String fileName = reportPath.substring(index+1);
    			reportPath = reportPath.substring(0, index);
    			reportPath += "/"+ location +  "/" + fileName;
    		}
    		return reportPath;
    	}
    
    }
    Last edited by anavagomez; 04-26-2012 at 06:58 PM.

  3. #3
    anavagomez is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: take code from java class put it in JSP page.

    the jsp code:

    Java Code:
    <%@page contentType="text/html"%>
    <%@page pageEncoding="UTF-8"%>
    <%@page import="com.crystaldecisions.report.web.viewer.*"%>
    <%@page import="com.crystaldecisions.sdk.occa.report.reportsource.*" %>
    <%@page import="com.crystaldecisions.sdk.occa.report.data.*" %>
    <%@page import="com.crystaldecisions.sdk.occa.report.exportoptions.*" %>
    <% 
    	String hideTree = request.getParameter("HIDE_GROUP_TREE");
    	String bolId = request.getParameter("BOL_ID");
    	String complaintId = request.getParameter("COMPL_ID");
    	String receiverId = request.getParameter("receiverId");
    	String coilId = request.getParameter("COIL_ID");
    	String sampleWorkORderId = request.getParameter("SAMPLE_ID");
    	String workOrder = request.getParameter("WORKORDER");
    	String orderLine = request.getParameter("LINE");
    	String invoiceId = request.getParameter("INVOICE_ID");
    	String ncrId = request.getParameter("NCR_ID");
    	String capaId = request.getParameter("CAPA_ID");
    
    	IReportSource reportSource = (IReportSource)session.getAttribute("reportSource");
    	ConnectionInfos oConnectionInfos = (ConnectionInfos)session.getAttribute("oConnectionInfos");
    
    	String goToPDF = request.getParameter("GO_TO_PDF");
    	if(goToPDF==null)
    		goToPDF = (String)request.getAttribute("GO_TO_PDF");
    	
    	if(goToPDF==null){
    		CrystalReportViewer oCrystalReportViewer = new CrystalReportViewer();
    		oCrystalReportViewer.setReportSource(reportSource);
    		oCrystalReportViewer.setOwnPage(true);
    		oCrystalReportViewer.setOwnForm(true);
    		oCrystalReportViewer.setPrintMode(CrPrintMode.PDF);
    		oCrystalReportViewer.setDatabaseLogonInfos(oConnectionInfos);
    
    
    		if(bolId!=null){
    			Fields fields = new Fields();
    			ParameterField pfield1 = new ParameterField();
    			Values vals1 = new Values();
    			ParameterFieldDiscreteValue pfieldDV1 = new ParameterFieldDiscreteValue();
    			pfield1.setName("@BOL_ID");
    			pfieldDV1.setValue(bolId);
    			vals1.add(pfieldDV1);
    			fields.add(pfield1);
    			pfield1.setCurrentValues(vals1);
    			pfield1.setReportName( "" );
    			oCrystalReportViewer.setParameterFields(fields);
    			oCrystalReportViewer.setEnableParameterPrompt(false);
    			oCrystalReportViewer.setDisplayGroupTree(false);
    			oCrystalReportViewer.refresh();
    		}
    		else if(ncrId!=null){
    			Fields fields = new Fields();
    			ParameterField pfield1 = new ParameterField();
    			Values vals1 = new Values();
    			ParameterFieldDiscreteValue pfieldDV1 = new ParameterFieldDiscreteValue();
    			pfield1.setName("@NCRId");
    			pfieldDV1.setValue(bolId);
    			vals1.add(pfieldDV1);
    			fields.add(pfield1);
    			pfield1.setCurrentValues(vals1);
    			pfield1.setReportName( "" );
    			oCrystalReportViewer.setParameterFields(fields);
    			oCrystalReportViewer.setEnableParameterPrompt(false);
    			oCrystalReportViewer.setDisplayGroupTree(false);
    			oCrystalReportViewer.refresh();
    		}
    		else if(workOrder!=null){
    			Fields fields = new Fields();
    			int index = -1;
    			if(workOrder.startsWith("%")){
    				String value = workOrder.substring(2);
    				index = value.indexOf("$");
    				workOrder = value.substring(0,index);
    				value = value.substring(index+1);
    				index = value.indexOf("$");
    				orderLine = value.substring(0,index);
    			}else{
    				index = workOrder.indexOf("-");
    				if(index > 0){
    					orderLine = workOrder.substring(index + 1);
    					workOrder = workOrder.substring(0,index);
    				}
    			}
    			/////////////////////////////////////////////////////////////////////////
    			ParameterFieldDiscreteValue pfieldDV1 = new ParameterFieldDiscreteValue();
    			pfieldDV1.setValue(workOrder);
    
    			Values vals1 = new Values();
    			vals1.add(pfieldDV1);
    
    			ParameterField pfield1 = new ParameterField();
    			pfield1.setName("WorkOrder");
    			pfield1.setCurrentValues(vals1);
    			pfield1.setReportName( "" );
    
    			fields.add(pfield1);
    			/////////////////////////////////////////////////////////////////////////
    			ParameterFieldDiscreteValue pfieldDV2 = new ParameterFieldDiscreteValue();
    			pfieldDV2.setValue(orderLine);
    
    			Values vals2 = new Values();
    			vals2.add(pfieldDV2);
    
    			ParameterField pfield2 = new ParameterField();
    			pfield1.setName("lineNo");
    			pfield1.setCurrentValues(vals2);
    			pfield1.setReportName( "" );
    
    			fields.add(pfield2);
    
    			oCrystalReportViewer.setParameterFields(fields);
    			oCrystalReportViewer.setEnableParameterPrompt(false);
    			oCrystalReportViewer.setDisplayGroupTree(false);
    			oCrystalReportViewer.refresh();
    		}
    		else if(coilId!=null){
    			Fields fields = new Fields();
    			ParameterField pfield1 = new ParameterField();
    			Values vals1 = new Values();
    			ParameterFieldDiscreteValue pfieldDV1 = new ParameterFieldDiscreteValue();
    			pfield1.setName("@CoilId");
    			pfieldDV1.setValue(coilId);
    			vals1.add(pfieldDV1);
    			fields.add(pfield1);
    			pfield1.setCurrentValues(vals1);
    			pfield1.setReportName( "" );
    			oCrystalReportViewer.setParameterFields(fields);
    			oCrystalReportViewer.setEnableParameterPrompt(false);
    			oCrystalReportViewer.setDisplayGroupTree(false);
    			oCrystalReportViewer.refresh();
    			
    		}
    		
    		else if(complaintId!=null){
    			Fields fields = new Fields();
    			ParameterField pfield1 = new ParameterField();
    			Values vals1 = new Values();
    			ParameterFieldDiscreteValue pfieldDV1 = new ParameterFieldDiscreteValue();
    			pfield1.setName("@complaintId");
    			pfieldDV1.setValue(complaintId);
    			vals1.add(pfieldDV1);
    			fields.add(pfield1);
    			pfield1.setCurrentValues(vals1);
    			pfield1.setReportName( "" );
    			oCrystalReportViewer.setParameterFields(fields);
    			oCrystalReportViewer.setEnableParameterPrompt(false);
    			oCrystalReportViewer.setDisplayGroupTree(false);
    			oCrystalReportViewer.refresh();
    		}
    		else if(receiverId!=null){
    			Fields fields = new Fields();
    			ParameterField pfield1 = new ParameterField();
    			Values vals1 = new Values();
    			ParameterFieldDiscreteValue pfieldDV1 = new ParameterFieldDiscreteValue();
    			pfield1.setName("@ReceiverId");
    			pfieldDV1.setValue(receiverId);
    			vals1.add(pfieldDV1);
    			fields.add(pfield1);
    			pfield1.setCurrentValues(vals1);
    			pfield1.setReportName( "" );
    			oCrystalReportViewer.setParameterFields(fields);
    			oCrystalReportViewer.setEnableParameterPrompt(false);
    			oCrystalReportViewer.setDisplayGroupTree(false);
    			oCrystalReportViewer.refresh();
    		}
    		if (session.getAttribute("refreshed") == null){
    			oCrystalReportViewer.refresh();
    			session.setAttribute("refreshed", "true");
    		}
    		oCrystalReportViewer.processHttpRequest(request, response, getServletConfig().getServletContext(), pageContext.getOut()); 
    	}
    	else{
    		String reportFormat = request.getParameter("reportFormat");
    		if(reportFormat==null) 
    			reportFormat="PDF";
    			
    	    ExportOptions oExportOptions = new ExportOptions();
    
    		if(reportFormat.equals("DOC"))
    			oExportOptions.setExportFormatType(ReportExportFormat.MSWord);
    		else if(reportFormat.equals("XLS"))
    			oExportOptions.setExportFormatType(ReportExportFormat.MSExcel);
    		else if(reportFormat.equals("PDF"))
    			oExportOptions.setExportFormatType(ReportExportFormat.PDF);
    		else if(reportFormat.equals("DATA"))
    			oExportOptions.setExportFormatType(ReportExportFormat.recordToMSExcel);
    		else
    			oExportOptions.setExportFormatType(ReportExportFormat.PDF);			
    
    	    ReportExportControl oReportExportControl = new ReportExportControl();
    	    oReportExportControl.setReportSource(reportSource);
    	    oReportExportControl.setExportOptions(oExportOptions);
    		oReportExportControl.setDatabaseLogonInfos(oConnectionInfos);
    
    		if(bolId!=null){
    			Fields fields = new Fields();
    			ParameterField pfield1 = new ParameterField();
    			Values vals1 = new Values();
    			ParameterFieldDiscreteValue pfieldDV1 = new ParameterFieldDiscreteValue();
    			pfield1.setName("@BOL_ID");
    			pfieldDV1.setValue(bolId);
    			vals1.add(pfieldDV1);
    			fields.add(pfield1);
    			pfield1.setCurrentValues(vals1);
    			pfield1.setReportName( "" );
    			oReportExportControl.setParameterFields(fields);
    		}
    		else if(ncrId!=null){
    			Fields fields = new Fields();
    			ParameterField pfield1 = new ParameterField();
    			Values vals1 = new Values();
    			ParameterFieldDiscreteValue pfieldDV1 = new ParameterFieldDiscreteValue();
    			pfield1.setName("@NCRId");
    			pfieldDV1.setValue(bolId);
    			vals1.add(pfieldDV1);
    			fields.add(pfield1);
    			pfield1.setCurrentValues(vals1);
    			pfield1.setReportName( "" );
    			oReportExportControl.setParameterFields(fields);
    		}
    		else if(invoiceId!=null){
    			Fields fields = new Fields();
    			ParameterField pfield1 = new ParameterField();
    			Values vals1 = new Values();
    			ParameterFieldDiscreteValue pfieldDV1 = new ParameterFieldDiscreteValue();
    			pfield1.setName("InvoiceID");
    			pfieldDV1.setValue(invoiceId);
    			vals1.add(pfieldDV1);
    			fields.add(pfield1);
    			pfield1.setCurrentValues(vals1);
    			pfield1.setReportName( "" );
    			oReportExportControl.setParameterFields(fields);
    		}
    		else if(workOrder!=null){
    			int index = -1;
    			if(workOrder.startsWith("%")){
    				String value = workOrder.substring(2);
    				index = value.indexOf("$");
    				workOrder = value.substring(0,index);
    				value = value.substring(index+1);
    				index = value.indexOf("$");
    				orderLine = value.substring(0,index);
    			}else{
    				index = workOrder.indexOf("-");
    				if(index > 0){
    					orderLine = workOrder.substring(index + 1);
    					workOrder = workOrder.substring(0,index);
    				}
    			}
    
    			Fields fields = new Fields();
    
    			/////////////////////////////////////////////////////////////////////////
    			ParameterFieldDiscreteValue pfieldDV1 = new ParameterFieldDiscreteValue();
    			pfieldDV1.setValue(workOrder);
    
    			Values vals1 = new Values();
    			vals1.add(pfieldDV1);
    
    			ParameterField pfield1 = new ParameterField();
    			pfield1.setName("@WorkOrder");
    			pfield1.setCurrentValues(vals1);
    
    			fields.add(pfield1);
    			/////////////////////////////////////////////////////////////////////////
    			
    			ParameterFieldDiscreteValue pfieldDV2 = new ParameterFieldDiscreteValue();
    			pfieldDV2.setValue(orderLine);
    
    			Values vals2 = new Values();
    			vals2.add(pfieldDV2);
    
    			ParameterField pfield2 = new ParameterField();
    			pfield2.setName("@LineNo");
    			pfield2.setCurrentValues(vals2);
    
    			fields.add(pfield2);
    			
    			oReportExportControl.setParameterFields(fields);
    		}
    
    		else if(complaintId!=null){
    			Fields fields = new Fields();
    			ParameterField pfield1 = new ParameterField();
    			Values vals1 = new Values();
    			ParameterFieldDiscreteValue pfieldDV1 = new ParameterFieldDiscreteValue();
    			pfield1.setName("@complaintId");
    			pfieldDV1.setValue(complaintId);
    			vals1.add(pfieldDV1);
    			fields.add(pfield1);
    			pfield1.setCurrentValues(vals1);
    			pfield1.setReportName( "" );
    			oReportExportControl.setParameterFields(fields);
    		}
    		
    		else if(receiverId!=null){
    			Fields fields = new Fields();
    			ParameterField pfield1 = new ParameterField();
    			Values vals1 = new Values();
    			ParameterFieldDiscreteValue pfieldDV1 = new ParameterFieldDiscreteValue();
    			pfield1.setName("@ReceiverId");
    			pfieldDV1.setValue(receiverId);
    			vals1.add(pfieldDV1);
    			fields.add(pfield1);
    			pfield1.setCurrentValues(vals1);
    			pfield1.setReportName( "" );
    			oReportExportControl.setParameterFields(fields);
    		}
    		else if(coilId!=null){
    			Fields fields = new Fields();
    			ParameterField pfield1 = new ParameterField();
    			Values vals1 = new Values();
    			ParameterFieldDiscreteValue pfieldDV1 = new ParameterFieldDiscreteValue();
    			pfield1.setName("@CoilId");
    			pfieldDV1.setValue(coilId);
    			vals1.add(pfieldDV1);
    			fields.add(pfield1);
    			pfield1.setCurrentValues(vals1);
    			pfield1.setReportName( "" );
    			oReportExportControl.setParameterFields(fields);
    		}
    		
    	    //ExportAsAtttachment(true) prompts for open or save;
    	    oReportExportControl.setExportAsAttachment(false);
    	    oReportExportControl.processHttpRequest(request, response, getServletConfig().getServletContext(), pageContext.getOut());
    	}%>

Similar Threads

  1. Replies: 5
    Last Post: 03-26-2012, 08:11 PM
  2. How to get the URL of current page in java class
    By Eswaraiah in forum New To Java
    Replies: 17
    Last Post: 03-16-2012, 01:43 PM
  3. Replies: 3
    Last Post: 07-26-2011, 02:36 AM
  4. jasper, set start on a new page thru java code
    By newbiejava in forum New To Java
    Replies: 0
    Last Post: 09-07-2010, 06:09 AM
  5. how to Import my Java Class in Jsp Page
    By pos in forum JavaServer Pages (JSP) and JSTL
    Replies: 20
    Last Post: 04-06-2010, 03:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •