Pages

Thursday, March 31, 2011

Guice Intro

Hi all.....
I recently came to learn about a new technology and frameworks which i  gonna share about here........
First tech is Guice.....
Guice is a DI(Dependency Injection) Framework. There are many DI frameworks, Guice is one of them which is used by google.
Advantage of using DI, increases the performance of the system and it's easy to learn.

Before explaining about Guice , first i'll explain why we go for DI.
In OOPs language we use classes, interface and concepts like inheritance to increase the performance but is it really increasing the performance of the system, the answer is partly no. but how?
lets see an example explaining this slow performance
Suppose we have four classes and name it A,B,C and D.
In which we inherited A in B, B in C, and C in D.
1.Here the class D may not use methods of A and B instead memory is allocated for all the methods inside D
2.Suppose class D wants to use class A's method, it has to move from bottom to top hirerachy.

because of these reasons , obviously reducing the performance we go for DI.

So now we see what is DI and using a simple DI usage.
Here we are creating a dependency for a class only if it is neccesary using interfaces. so unneccessary hirerachy is removed.
Which obviously increases the performance.

Now we see a simple example
1. Create an interface.
package add.service;

public interface Add {
 
    public int add(int a, int b);
 
}
2.Create a class which implements the interface
package add.service;
public class SimpleAdd implements Add{



    public int add(int a, int b) {
        return a + b;
    }

}
3. Now we are going to inject the dependency to the class by creating a Module
package add.service;
import com.google.inject.Binder;
import com.google.inject.Module;
public class AddModule implements Module{
    public void configure(Binder binder) {
        binder.bind(Add.class).to(SimpleAdd.class);
    }
}
4. Now we are creating the client and using the DI
package add.service;

import com.google.inject.Guice;
import com.google.inject.Injector;

public class AddClient {

    public static void main(String[] args) {

        Injector injector = Guice.createInjector(new AddModule());
        Add add = injector.getInstance(Add.class);
        System.out.println(add.add(10, 54));
    }
}
So the Add functionality is directly linked using the binder and doing so increaes the performance. But we can't bind a single interface to multiple classes as same a s above.So how to bind mulitple classes? will be posted in future.

Sunday, March 13, 2011

Multiple graph in a single page using JPGraph

Implementing multiple graph in a single page is easier than you think.
You can achieve it in two ways,
First way would be combining two graphs and displaying in a page, this is a normal/usual way.
Here you can you go to see how to use it.

Second way is using the image tag here i will explain how to use it

first you decide which graph you want to use. here i take the bar graph but it doesn't matter what graph you are going to draw/implement.

here you can find the sample bargraph and save it as bargraph.php

Now we are going to draw multiple graph, here we are going to draw two graphs.

first create a image tag were the source path refers to the bargraph.php like this

<html>
<head></head>
<body>
<img src="bargraph.php">
<img src="bargraph.php">
</body>
</html>

and save it as multipleGraph.php
thats it we have created a page containing two graphs in it.

Creating dynamic graph is also easier
simply pass the values for the variable "$data" in bargraph.php as below
( here we are using "-" to differentiate between values)

<img src="bargraph.php?data=1-2-3">

and change the variable "$data" in bargraph.php page as this
$part = explode("-",$_GET['data']);
$data = array();
for($i=0;$i<count($part);$i++)
{
$data[] = $part[$i];
}

thats it you have created dynamic graph.
Hope this is useful.

Thursday, March 3, 2011

Using javascript inside an ajax page

Hi all........

We normally use javascript to call ajax page to generate/formulate content and display the content using reponseText. But today i have tried to create a javascript function inside the ajax page to perform validation.
But this doesnot work fine even the responseText loaded with all the functions. but why, because all the javascript functions and inclusion of files are done when the page loads first time. since the javascript functions defined in ajax page it is not successfully loaded. So how to achieve this i'll tell you the simpler way. JUST load all the functions in the first page itself, and all the operations performed by javascript when loading the ajax page should be define inside the functions, you need to create and use inside the functions. i.e you should not set a value for control when loading a ajax page. The complete illustration is below

for eg:
function ajaxCall()
{
xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET','ajaxPage.php',false);
xmlhttp.send();
document.getElementById('div').innerHTML = xmlhttp.responseText;
}
ajaxPage.php
//setting a value like this doesn't work
<input id='date1' type='text'>
<button id="trigger1">...button>
Calendar.setup(
  {
   inputField  : "date1",
   ifFormat    : "%m/%d/%Y %I:%M %p",
   button      : "trigger1",        
showsTime   : "true",
   timeFormat  : "12"    
} );

//the button trigger1 doesn't trigger anything in the main page since the javascript function is not inside the main page, so the above functionality as below to make it work.

first create a function
function setDate(val)
{

Calendar.setup(
  {
   inputField  : "date"+val,
   ifFormat    : "%m/%d/%Y %I:%M %p",
   button      : "trigger"+val,        
showsTime   : "true",
   timeFormat  : "12"    
} );


}

and include this function in the main page
after including change the code in the ajax page as below


<input id="date1" type="text" />
<button id="trigger1" onclick='setDate(1)'>...button>

now this will work fine.

i hope this helps someone.

Tuesday, March 1, 2011

Including a php in symfony

Hi....
Including a php inside symfony framework is a very easy thing. Let me explain it can be done.

You can do this in two ways
i) First one is the normal and logical way is to use the templates and displaying using sfConfig
refer this Including PHP

ii) second one is simpler to use for which u need not use action or templates.
To achieve this u just need to call that particular php file inside an iframe, that would be the simpler way to include a php file.

Sometimes u may need to open this page in a thickbox, at this time u will be calling the page in body onload function but this may not workout sometimes, but u can the delay option and then use the iframe to display the page.This will work fine.