Thứ Tư, 16 tháng 5, 2012

Datatable with row editing and deleting in Primefaces 3

JSF Page

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:p="http://primefaces.org/ui">

    <h:head>
        <title>Summary Report Page</title>
    </h:head>
    <h:body>
        <h:form id="form">
            <p:growl id="messages" showDetail="true"/> 
            <p:dataTable id="basic" var="car" value="#{tableBean.carsSmall}" editable="true">
                <p:ajax event="rowEdit" listener="#{tableBean.updateItem(car)}"  />
                <p:column headerText="Model" style="width:125px">
                    <p:cellEditor>
                        <f:facet name="output">
                            <h:outputText value="#{car.model}" />
                        </f:facet>
                        <f:facet name="input">
                            <p:inputText value="#{car.model}" style="width:100%"/>
                        </f:facet>
                    </p:cellEditor>
                </p:column>

                <p:column headerText="Year" style="width:125px">
                    <p:cellEditor>
                        <f:facet name="output">
                            <h:outputText value="#{car.year}" />
                        </f:facet>
                        <f:facet name="input">
                            <p:inputText value="#{car.year}" style="width:100%" label="Year"/>
                        </f:facet>
                    </p:cellEditor>
                </p:column>

                <p:column>
                    <f:facet name="header">
                        Manufacturer
                    </f:facet>
                    <h:outputText value="#{car.manufacturer}" />
                </p:column>

                <p:column>
                    <f:facet name="header">
                        Color
                    </f:facet>
                    <h:outputText value="#{car.color}" />
                </p:column>
                <p:column>
                    <f:facet name="header">
                        Delete
                    </f:facet>
                    <p:commandButton icon="ui-icon-close" title="remove from cart"
                                     actionListener="#{tableBean.removeItem(car)}" update="@form"/>
                </p:column>
                <p:column>
                    <f:facet name="header">
                        Edit
                    </f:facet>
                    <p:rowEditor/>
                </p:column>
            </p:dataTable>
        </h:form>
    </h:body>
</html>
Managed Bean:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean(name = "tableBean")
@ViewScoped
public class TableBean implements Serializable {
   
    private final static String[] colors;
    private final static String[] manufacturers;
    private List<Car> carsSmall;
   
    static {
        colors = new String[10];
        colors[0] = "Black";
        colors[1] = "White";
        colors[2] = "Green";
        colors[3] = "Red";
        colors[4] = "Blue";
        colors[5] = "Orange";
        colors[6] = "Silver";
        colors[7] = "Yellow";
        colors[8] = "Brown";
        colors[9] = "Maroon";
       
        manufacturers = new String[10];
        manufacturers[0] = "Mercedes";
        manufacturers[1] = "BMW";
        manufacturers[2] = "Volvo";
        manufacturers[3] = "Audi";
        manufacturers[4] = "Renault";
        manufacturers[5] = "Opel";
        manufacturers[6] = "Volkswagen";
        manufacturers[7] = "Chrysler";
        manufacturers[8] = "Ferrari";
        manufacturers[9] = "Ford";
    }
   
    public TableBean() {
        carsSmall = new ArrayList<Car>();
        populateRandomCars(carsSmall, 9);
    }
   
    private void populateRandomCars(List<Car> list, int size) {
        for (int i = 0; i < size; i++) {
            list.add(new Car(getRandomModel(), getRandomYear(), getRandomManufacturer(), getRandomColor()));
        }
    }
   
    public void removeItem(Car car) {
        carsSmall.remove(car);
    }
   
    public void updateItem(Car car) {
        System.out.println("New value: " + car.getModel());
    }
   
    public List<Car> getCarsSmall() {
        return carsSmall;
    }
   
    private int getRandomYear() {
        return (int) (Math.random() * 50 + 1960);
    }
   
    private String getRandomColor() {
        return colors[(int) (Math.random() * 10)];
    }
   
    private String getRandomManufacturer() {
        return manufacturers[(int) (Math.random() * 10)];
    }
   
    private String getRandomModel() {
        return UUID.randomUUID().toString().substring(0, 8);
    }
}

web.xml

<context-param>
        <param-name>com.sun.faces.expressionFactory</param-name>
        <param-value>com.sun.el.ExpressionFactoryImpl</param-value>
    </context-param>

Note: using E.L 2.2 (el-api-2.2.jar ; el-impl-2.2.jar) in Tomcat 6 lib folder


Thứ Ba, 15 tháng 5, 2012

Set permission on Solaris

For any folder: chmod -R 777 <FolderName>
For any file in a specified folder: chmod 777 *

Thứ Năm, 10 tháng 5, 2012

How to Customize the Growl PrimeFaces Component

Overwrite CSS of <p:growl>
Add code in <ui:define>:
<style type="text/css">
 .ui-growl {
        position:fixed;
        top:20px;
        right:20px;
        width:301px;
        z-index:9999;
}
.ui-growl-item-container {
        position:relative;
        margin:0 0 10px 0;
        opacity:0.85;
        filter:alpha(opacity=85);
}
.ui-growl-item {
        display:block;
        padding:10px 15px;
}
.ui-growl-item p {
        padding:0;
        margin:0;
}
.ui-growl-icon-close {
        position:absolute;
        top:5px;
        left:3px;
        cursor:pointer;
}
.ui-growl-title {
        font-weight:bold;
        padding:0 0 7px 0;
        display:block;
}
.ui-growl-image {
        width:32px;
        height:32px;
        float:left;
}
.ui-growl-message {
        padding:0 0 5px 0;
        width:220px;
        float:right;
}

.ui-growl-message p {
        font-weight: normal;
}
</style>

Read and Write excel file with POI library

Here is the simple code for reading and writing excel file with POI library

public void processExcel() throws FileNotFoundException {
        InputStream is = this.getClass().getResourceAsStream("/testpoi/ExcelFile.xls");
        FileInputStream fis = new FileInputStream("D://ExcelFile.xls");
        FileOutputStream fos = null;
        try {
            //HSSFWorkbook workbook = new HSSFWorkbook(is);
            HSSFWorkbook workbook = new HSSFWorkbook(fis);
            HSSFSheet sheet = workbook.getSheetAt(0);
            HSSFCell cell = null;
            int rows = sheet.getLastRowNum();
            //System.out.println("Total rows: " + rows);

            for (int i = 0; i < rows; i++) {
                double total = 0;
                for (int j = 0; j < 3; j++) {
                    cell = sheet.getRow(i + 1).getCell(j);
                    if (cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK
                            || cell.getCellType() == HSSFCell.CELL_TYPE_ERROR) {
                        continue;
                    }
                    if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                        double value = cell.getNumericCellValue();
                        //System.out.println("Value: " + value);
                        total += value;
                    }
                }
                vtResult.add(total);
            }
           
            //out result
            for (int h = 0; h < vtResult.size(); h++) {
                cell = sheet.getRow(h + 1).getCell(3);
                cell.setCellValue(vtResult.get(h));
            }

            // write workbook
            fos = new FileOutputStream("e://output.xls");
            workbook.write(fos);
            fos.close();
            workbook = null;
        } catch (IOException ex) {
            Logger.getLogger(ExcelUtil.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                is.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

Thứ Hai, 7 tháng 5, 2012

How to Enable EL 2.2 on Tomcat 6

EL 2.2 allows you to pass in parameters in your method expression (i.e. actions and action listeners). See example below:

<h:commandButton id="testButton" value="Test Button"
    actionListener="#{testBean.someMethod('someString')}" />
Inside your bean, you could in turn get your passed in parameters as follows:
public void someMethod(String param) {
System.out.println("param: " + param);
}
Here’s the console output:

param: someString

The following stack has been used for testing:
Apache Tomcat 6.0.26
MyFaces 2.0.2
jdk1.6.0_18

Tomcat 6 doesn’t come with EL (Expression Language) 2.2 and to enable it, follow these steps:
Replace el-api.jar with GlassFish EL 2.2 implementation (el-api-2.2.jar and el-impl-2.2.jar) in the Tomcat “lib” folder. These could be downloaded from the following links:
http://download.java.net/maven/2/org/glassfish/web/el-impl/2.2/el-impl-2.2.jar

http://download.java.net/maven/2/javax/el/el-api/2.2/el-api-2.2.jar

In the web.xml, add the following depending on the implementation:
For MyFaces using Sun’s (GlassFish) EL 2.2, add:


<context-param>
    <param-name>org.apache.myfaces.EXPRESSION_FACTORY</param-name>
    <param-value>com.sun.el.ExpressionFactoryImpl</param-value>
</context-param>

For Mojarra, add:
<context-param>
    <param-name>com.sun.faces.expressionFactory</param-name>
    <param-value>com.sun.el.ExpressionFactoryImpl</param-value>
</context-param>


Basically, if you use Sun’s (GlassFish) EL 2.2 implementation, add this to the param-value: com.sun.el.ExpressionFactoryImpl
If you use Apache’s EL 2.2 implementation, add this to the param-value: org.apache.el.ExpressionFactoryImpl
References:
http://wiki.apache.org/myfaces/HowToEnableEl22
Source: http://code2inspire.wordpress.com/2010/11/05/how-to-enable-el-2-2-on-tomcat-6/