Thursday, October 28, 2010

Dependency Injection in PHP vs Java

How to do Dependency Injection in PHP vs Java.

Plain PHP:

$helper_sales = new HelperSales();

Magento proprietary SPI:

// no type information!
$helper_sales = Mage::helper('sales');

Java / CDI :

@Inject
private HelperSales helperSales;

Java / get bean from Spring context :

HelperSales helperSales = appCtx.getBean(HelperSales.class);

The Java examples apply to Scala as well, of course.

Still envy PHP?

Sunday, October 24, 2010

PrimeFaces supports Bean Validation (JSR-303) in JSF 2.0

I've found (at least) one thing PrimeFaces / JSF 2.0 does something better than Vaadin when it comes to RIA (Rich Internet Web Applications), and that is Bean Validation (JSR-303) support.

The following Java code:
@NotNull

@Size(min=1) private String surname;
will automatically become validated JSF components in your XHTML/VDL file.

Cool!

Displaying AJAX Tables in PHP vs Java EE: ZFDataGrid and PrimeFaces DataTable

While developing with PHP + Zend Framework + Doctrine I missed an easy way to display/edit data using a grid/table.

A very useful component I found is ZFDataGrid.

Here's a sample code of how to use ZFDataGrid:

    function simpleAction()

    {         //Zend_Config         $config = new Zend_Config_Ini('./application/grids/grid.ini', 'production');                 //Grid Initialization         $grid = Bvb_Grid::factory('Bvb_Grid_Deploy_Table', $config, 'id');                 //Setting grid source         $grid->setSource(new Bvb_Grid_Source_Zend_Table(new Bugs()));                 //CRUD Configuration         $form = new Bvb_Grid_Form();         $form->setAdd(true)->setEdit(true)->setDelete(true);         $grid->setForm($form);                 //Pass it to the view         $this->view->pages = $grid;         $this->render('index');     }
It looks pretty good too.

Check the ZFDataGrid Live Demo here.

However, working with data grids using JSF 2.0 and PrimeFaces felt much more natural and easier.

Here's a sample code using PrimeFaces' DataTable :

<h:form>

    <p:dataTable var="car" value="#{tableBean.lazyModel}" paginator="true" rows="10" lazy="true"
                 paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
                 rowsPerPageTemplate="5,10,15"
                 selection="#{tableBean.selectedCar}" selectionMode="single"
                 onRowSelectComplete="carDialog.show()" onRowSelectUpdate="display">
        <f:facet name="header">
            Displaying 100,000,000 Cars
        </f:facet>
        <p:column headerText="Model">
            <h:outputText value="#{car.model}" />
        </p:column>
        <p:column headerText="Year">
            <h:outputText value="#{car.year}" />
        </p:column>
        <p:column headerText="Manufacturer">
            <h:outputText value="#{car.manufacturer}" />
        </p:column>
        <p:column headerText="Color">
            <h:outputText value="#{car.color}" />
        </p:column>
    </p:dataTable>

    <p:dialog header="Car Detail" widgetVar="carDialog" resizable="false"
              width="200" showEffect="explode" hideEffect="explode">
        <h:panelGrid id="display" columns="2" cellpadding="4">
            <f:facet name="header">
                <p:graphicImage value="/images/cars/#{tableBean.selectedCar.manufacturer}.jpg"/>
            </f:facet>
            <h:outputText value="Model:" />
            <h:outputText value="#{tableBean.selectedCar.model}"/>

            <h:outputText value="Year:" />
            <h:outputText value="#{tableBean.selectedCar.year}"/>

            <h:outputText value="Manufacturer:" />
            <h:outputText value="#{tableBean.selectedCar.manufacturer}"/>

            <h:outputText value="Color:" />
            <h:outputText value="#{tableBean.selectedCar.color}"/>
        </h:panelGrid>
    </p:dialog>

</h:form>

The above code may look verbose, but it packs a lot of functionality and it's very easy and intuitive to customize.
When you click a row it displays a nice dialog with a picture. Furthermore, it's actually lazy loading 100,000,000 rows!! (yes, ONE HUNDRED MILLION ROWS)

Here's how it looks:

You can see for real the PrimeFaces DataTable Lazy-loading Live Demo here.

It's very easy to add lazy-loading support to DataTable:

        lazyModel = new LazyDataModel<Car>() {

/**
* Dummy implementation of loading a certain segment of data.
* In a real application, this method should load data from a datasource
*/
@Override
public List<Car> load(int first, int pageSize, String sortField, boolean sortOrder, Map<String,String> filters) {
logger.log(Level.INFO, "Loading the lazy car data between {0} and {1}", new Object[]{first, (first+pageSize)});

                //Sorting and Filtering information are not used for demo purposes just random dummy data is returned

List<Car> lazyCars = new ArrayList<Car>();
populateLazyRandomCars(lazyCars, pageSize);

return lazyCars;
}
};

        /**
         * In a real application, this number should be resolved by a projection query
         */
        lazyModel.setRowCount(100000000);

Not to disrespect PHP or ZFDataGrid in anyway (I still need to use them for some of my work), but the experience with JSF 2.0 and PrimeFaces wins hands down. I think it's more because of PrimeFaces than JSF 2.0, but they're such a powerful combo (compared to if you use PrimeFaces with JSF 1.2).

I do hope that PrimeFaces provide a utility class that implements LazyDataModel for a Hibernate/HQL or JPA/JPQL query, but for now I can live with the above.

Vaadin on Google App Engine part 1: Setting up the Development Environment

Vaadin is such a nice Java web RIA application framework for building desktop-like apps, built on top of GWT AJAX Library. Vaadin uses Java to programmatically create UI components and there's very minimal CSS / HTML involved, much less JavaScript.

In this article series I will share my experiences on developing a Vaadin web application and deploying it to Google App Engine hosting.

Prepare the Vaadin + Google Development Environment


First you need to prepare your development environment.

  1. Install Eclipse IDE 3.6SR1 (Helios) - Java EE edition
  2. Install Vaadin Eclipse Plugin
  3. Install Google Plugin for Eclipse
  4. Download the latest Google App Engine SDK . This step is optional because Google Plugin for Eclipse can also download GAE SDK and GWT SDK for you.

Useful Reading

Vaadin on Google App Engine part 2: Creating the Web Application Project

To create a Vaadin web application on Google App Engine, in Eclipse IDE click File > New > Project... and create a new Vaadin project.

Make sure you choose Google App Engine as the Deployment configuration.

Then enable Google App Engine nature in your project:

  1. Right click your project > Properties...
  2. Go to Google > App Engine
  3. Check the Use Google App Engine checkbox
  4. Pick the Google App Engine SDK that you use or click Configure SDKs... if necessary

To ensure that JDO enhancement by DataNucleus access platform works correctly, do the following workaround:
  1. Right click your project > Properties...
  2. Go to Java Build Path
  3. Remove Web App Libraries
  4. Click Add JARs..., and select the war/WEB-INF/lib/vaadin-x.x.x.jar
Now your Vaadin application should run fine. You can also start adding JDO entities with proper DataNucleus JDO code enhancement.

Vaadin on Google App Engine part 1: Setting up the Development Environment

Vaadin is such a nice Java web RIA application framework for building desktop-like apps, built on top of GWT AJAX Library. Vaadin uses Java to programmatically create UI components and there's very minimal CSS / HTML involved, much less JavaScript.

In this article series I will share my experiences on developing a Vaadin web application and deploying it to Google App Engine hosting.

Prepare Vaadin + Google App Engine Development Environment


First you need to prepare your development environment.

  1. Install Eclipse IDE 3.6SR1 (Helios) - Java EE edition
  2. Install Vaadin Eclipse Plugin
  3. Install Google Plugin for Eclipse
  4. Download the latest Google App Engine SDK . This step is optional because Google Plugin for Eclipse can also download GAE SDK and GWT SDK for you.

Useful Reading

Eclipse Helios In Action: Modeling with Acceleo and Xtext

The Eclipse Modeling Project is one of the most active projects within the Eclipse community. Ed Merks will give a quick overview of the Modeling projects in Eclipse IDE 3.6 Helios. Then Cedric Brun will demo Acceleo and Sebastian Zarnekow will show Xtext.

This presentation was recorded as part of the Helios In Action virtual conference: eclipse.org/​helios/​heliosinaction.php.

Presented by Ed Merks, Cedric Brun of Obeo and Sebastian Zarnekow of itemis

See the webinar recording video here: Helios In Action: Modeling