Tuesday, March 23, 2010

Alternate record coloring in jasper reports

Alternate record styles in jasper reports



in .jrxml file

 we need to write one style like this

     <style name="Row" isDefault="false" mode="Transparent">
        <conditionalStyle>
            <conditionExpression><![CDATA[$V{REPORT_COUNT}%2 == 0]]></conditionExpression>
            <style isDefault="false" style="Row" backcolor="#E6DAC3"/>
        </conditionalStyle>
    </style>



Need to give this style name  for each record


    <reportElement style="Row" mode="Opaque" x="0" y="0" width="555" height="15"/>



So my .jrxml file is like this

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="null" language="groovy" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <style name="Title" isDefault="false" fontName="Times New Roman" fontSize="50" isBold="true" pdfFontName="Times-Bold"/>
    <style name="SubTitle" isDefault="false" forecolor="#736343" fontName="Arial" fontSize="18"/>
    <style name="Column header" isDefault="false" forecolor="#666666" fontName="Arial" fontSize="12" isBold="true"/>
    <style name="Detail" isDefault="false" fontName="Arial" fontSize="12"/>
    <style name="Row" isDefault="false" mode="Transparent">
        <conditionalStyle>
            <conditionExpression><![CDATA[$V{REPORT_COUNT}%2 == 0]]></conditionExpression>
            <style isDefault="false" style="Row" backcolor="#E6DAC3"/>
        </conditionalStyle>
    </style>
    <queryString language="SQL">
        <![CDATA[SELECT
     bp_indent.`indent_id` AS bp_indent_indent_id,
     bp_indent.`indent_no` AS bp_indent_indent_no,
     bp_indent.`req_emp_id` AS bp_indent_req_emp_id,
     bp_indent.`indent_date` AS bp_indent_indent_date,
     bp_indent.`indent_status` AS bp_indent_indent_status
FROM
     `bp_indent` bp_indent]]>
    </queryString>
    <field name="bp_indent_indent_id" class="java.lang.Integer">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="bp_indent_indent_no" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="bp_indent_req_emp_id" class="java.lang.Integer">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="bp_indent_indent_date" class="java.sql.Timestamp">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="bp_indent_indent_status" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="31" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="16" splitType="Stretch">
            <line>
                <reportElement positionType="FixRelativeToBottom" x="0" y="15" width="555" height="1"/>
                <graphicElement>
                    <pen lineWidth="0.5" lineColor="#999999"/>
                </graphicElement>
            </line>
            <staticText>
                <reportElement style="Column header" x="0" y="0" width="111" height="15" forecolor="#736343"/>
                <textElement/>
               <text><![CDATA[bp_indent_indent_id]]></text>
            </staticText>
            <staticText>
                <reportElement style="Column header" x="111" y="0" width="111" height="15" forecolor="#736343"/>
                <textElement/>
                <text><![CDATA[bp_indent_indent_no]]></text>
            </staticText>
            <staticText>
                <reportElement style="Column header" x="222" y="0" width="111" height="15" forecolor="#736343"/>
                <textElement/>
                <text><![CDATA[bp_indent_req_emp_id]]></text>
            </staticText>
            <staticText>
                <reportElement style="Column header" x="333" y="0" width="111" height="15" forecolor="#736343"/>
                <textElement/>
                <text><![CDATA[bp_indent_indent_date]]></text>
            </staticText>
            <staticText>
                <reportElement style="Column header" x="444" y="0" width="111" height="15" forecolor="#736343"/>
                <textElement/>
                <text><![CDATA[bp_indent_indent_status]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="15" splitType="Stretch">
            <frame>
                <reportElement style="Row" mode="Opaque" x="0" y="0" width="555" height="15"/>
                <textField isStretchWithOverflow="true">
                    <reportElement style="Detail" x="0" y="0" width="111" height="15"/>
                    <textElement/>
                    <textFieldExpression class="java.lang.Integer"><![CDATA[$F{bp_indent_indent_id}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true">
                    <reportElement style="Detail" x="111" y="0" width="111" height="15"/>
                    <textElement/>
                    <textFieldExpression class="java.lang.String"><![CDATA[$F{bp_indent_indent_no}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true">
                    <reportElement style="Detail" x="222" y="0" width="111" height="15"/>
                    <textElement/>
                    <textFieldExpression class="java.lang.Integer"><![CDATA[$F{bp_indent_req_emp_id}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true">
                    <reportElement style="Detail" x="333" y="0" width="111" height="15"/>
                    <textElement/>
                    <textFieldExpression class="java.sql.Timestamp"><![CDATA[$F{bp_indent_indent_date}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true">
                    <reportElement style="Detail" x="444" y="0" width="111" height="15"/>
                    <textElement/>
                    <textFieldExpression class="java.lang.String"><![CDATA[$F{bp_indent_indent_status}]]></textFieldExpression>
                </textField>
            </frame>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch">
            <line>
                <reportElement positionType="FixRelativeToBottom" x="0" y="3" width="555" height="1"/>
                <graphicElement>
                    <pen lineWidth="0.5" lineColor="#999999"/>
                </graphicElement>
            </line>
        </band>
    </columnFooter>
    <pageFooter>
        <band height="25" splitType="Stretch">
            <frame>
                <reportElement mode="Opaque" x="-21" y="1" width="597" height="24" forecolor="#D0B48E" backcolor="#F2EBDF"/>
                <textField evaluationTime="Report">
                    <reportElement style="Column header" x="533" y="0" width="40" height="20" forecolor="#736343"/>
                    <textElement verticalAlignment="Middle">
                        <font size="10" isBold="false"/>
                    </textElement>
                    <textFieldExpression class="java.lang.String"><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement style="Column header" x="453" y="0" width="80" height="20" forecolor="#736343"/>
                    <textElement textAlignment="Right" verticalAlignment="Middle">
                        <font size="10" isBold="false"/>
                    </textElement>
                    <textFieldExpression class="java.lang.String"><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
                </textField>
                <textField pattern="EEEEE dd MMMMM yyyy">
                    <reportElement style="Column header" x="22" y="1" width="197" height="20" forecolor="#736343"/>
                    <textElement verticalAlignment="Middle">
                        <font size="10" isBold="false"/>
                    </textElement>
                    <textFieldExpression class="java.util.Date"><![CDATA[new java.util.Date()]]></textFieldExpression>
                </textField>
            </frame>
        </band>
    </pageFooter>
    <summary>
        <band splitType="Stretch"/>
    </summary>
</jasperReport>

Thursday, March 11, 2010

Returning multiple result sets from stored procedure

Handling multiple result sets from stored procedure as follows

My Procedure in Mysql



DELIMITER $$
DROP PROCEDURE IF EXISTS `mms`.`get_items`$$

CREATE DEFINER=`root`@`%` PROCEDURE `get_items`(IN pi_id int)
BEGIN
    /*    Call get_items(0); */
       
    /* ----------Result Set 1 ---------- */
    select     i_id, i_no,  date_format(i_date,'%d/%m/%Y') as i_date,comments,i_status
            from bindent    where     i_id = pi_id ;

        /* ----------Result Set 2 ---------- */
    select     i_id, line_no, item_no,(select item_name from b_items where item_no = b_i_items.item_no )
                 as item_name,floor(req_qty) as req_qty, uom, app_qty, date_format(priority_date,'%d/%m/%Y') as
                  priority_date , wm_approved, vp_approved, comments,
                 (select uom_desc from b_uom where uom = b_i_items.uom) as uom_desc
             from b_i_items where i_id = pi_id ;
    END$$

DELIMITER ;




My Java Code function is

public static InVO getItemDetails(String id)
{
        List IndentDetList = null;
        Connection connection = null;
        CallableStatement callStatement = null;
        ResultSet rs = null, rsItems = null;
        Boolean queryResult = false;
        InVO invo = new InVO();
        try {
            // get the connection to the DB

            connection = MDB.getConnection();
            IndentDetList = new ArrayList();
      

            String query = "{Call get_items(?)}";
            callStatement = connection.prepareCall(query);

            callStatement.setString(1, id);
            rs = callStatement.executeQuery();

            while (rs.next()) {
                invo.setIndentid(rs.getString(1));
                invo.setIndentno(rs.getString(2));
                invo.setSysDate(rs.getString(3));
                invo.setIndentComment(rs.getString(4));
                invo.setStatus(rs.getString(5));
            }

            ItemVO itemobject = null;

            if (callStatement.getMoreResults())
        {
                rsItems = callStatement.getResultSet();

                 while (rsItems.next())
                {
                    itemobject = new ItemVO();
                    itemobject.setLineno(rsItems.getInt(2));
                    itemobject.setItemId(rsItems.getString(3));
                    itemobject.setItemName(rsItems.getString(4));
                    itemobject.setUnits(rsItems.getString(5));
                    itemobject.setUom(rsItems.getString(6));
                    itemobject.setPrioritydate(rsItems.getString("priority_date"));
                    itemobject.setUomDesc(rsItems.getString("uom_desc"));
                    IndentDetList.add(itemobject);
                }
                invo.setItemsList(IndentDetList);

            }


        } catch (Exception e) {
            indentvo = null;
            if (logger.isEnabledFor(Level.ERROR)) {
                logger.error("Error while getting Connection from DB: " + e.getMessage());
                                                                         }
                                        }
           finally {
                        MDB.close(connection);
                    }

        return indentvo;


    }// function closed

Friday, March 5, 2010

Basic struts example implemented in NetBeans IDE


Hi friends,

    This is a demo for basic example of struts framework implementation in NetBeans IDE.

    It is helpful to the developers who are looking for basic struts implementation. I have a step by step procedure with screen shots.

Have a look.

I) Create a project. File -> New Project

II) Give the project name and add the server. If server is not configured already then add the server like this



Tools -> servers ->
"add server"
Add the server if server is configured to the new project.
Click finish button






























III)
Create a folder called “form” in “web pages” in the project
Create jsps  welcomeStruts .jsp , succ.jsp and fail.jsp.
Creating welcomeStruts.jsp

Code:

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>


<html:html locale="true">
<body>

   <html:form action="/register">

        User Name <html:text property="userName"/>
        Password <html:password property="password"/>
        <html:submit/>

    </html:form>

    </body>
</html:html>

 

Creating succ.jsp
 




Creating fail.jsp 
 
  

Creating Action form bean class
Right click on the package and select new action form bean class

 
  
  

Select those fields and right click on it. You can find like this.

 


Click “select all”  and refactor button to get getters and setters.

 


Now Action form bean is like this



 
Creating Action class



  


Click Next


















Click “Finish”

Type the action code like this. This


 


If u hit .  (dot) after the object it will show auto suggested methods like 

 


Final action class is code 

package com.myapp.struts;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
public class registerAction extends org.apache.struts.action.Action 
{
   
    private final static String SUCCESS = "success";
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
       registerForm rf=(registerForm)form;
       String username=rf.getUserName();
       String password=rf.getPassword();
       if(username.equals("sowjiavi") || password.equals("sowjiavi"))
       {
        return mapping.findForward(SUCCESS);
       }else
           return mapping.findForward("fail");
    }
}

 

Struts-Config.xml


Step 1: Open the struts-config .xml file
Step 2: Add following content


<?xml version="1.0" encoding="UTF-8" ?>
           <!DOCTYPE struts-config PUBLIC
                  "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
               "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">


<struts-config>
    <form-beans>
       <form-bean name="registerForm" type="com.myapp.struts.registerForm"/>
    </form-beans>


  <action-mappings>

   <action input="/" name="registerForm" path="/register" scope="session" type="com.myapp.struts.registerAction">
      <forward name="success" path="/form/succ.jsp"/>
<forward name="fail" path="/form/fail.jsp"/>
     </action>
     
    </action-mappings>
  
</struts-config>

Then your struts-config.xml file look like this


 

web.xml


Step 1: Open web.xml file
Step 2: Modify the file like this.

 


Web.xml   code is:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
           <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
       </init-param>
         <load-on-startup>2</load-on-startup>
        </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
       <url-pattern>*.do</url-pattern>
    </servlet-mapping>
  
    <welcome-file-list>
        <welcome-file>/form/welcomeStruts.jsp</welcome-file>
        </welcome-file-list>
   </web-app>


My directory structure is

 


Then compile and run the application

 
  

  


It automatically opens the browser if server is configured.