^ Click Here

Tuesday, February 28, 2012

Spring Basic Example

I am providing a very basic example of Spring :-

Firstly I created a "Maven project"

Inside there is a person class person.java

package pkg;

public class Person {
    
    private String name;
    private IAnimal animal;
    
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public IAnimal getAnimal() {
        return animal;
    }
    public void setAnimal(IAnimal animal) {
        this.animal = animal;
    }
    public String toString() {
        return name+" has a "+animal.toString();
    }
}

the Interface IAnimal.java

package pkg;

public interface IAnimal {
    public void play();
}

the animal Dog.java

package pkg;

public class Dog implements IAnimal {
    private String name;
    public void play() {
        System.out.println("Playing with "+name);
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String toString() {
        return " Dog with name "+name;
    }

}

the animal Cat.java

Tuesday, February 21, 2012

Starting/Stopping a timer (stopwatch) in jQuery/javascript

First of all we need to have a HTML container where we can define hour, minute and seconds. the different counter i.e. the values of hour, minute or second are put in div with id counterHour, counterMin and counterSec as can be seen below.
 <div>
Counter : &nbsp;</div>
<div id="counterHour" style="float: left;">
0</div>
<div style="float: left;">
&nbsp; Hours &nbsp;</div>
<div id="counterMin" style="float: left;">
0</div>
<div style="float: left;">
&nbsp; minutes &nbsp;</div>
<div id="counterSec" style="float: left;">
0</div>
<div style="float: left;">
&nbsp; seconds &nbsp;</div>

Also we need a button to start and stop the timer :

<input type="button" id="timer" class="start" value="Start Timer" onclick="check_timer()">
As we can see there is an onclick attribute and check_timer() function is attached to it. Hence in script (<script></script>) tag we will define below functions. 

function check_timer(){
 if($('#timer').hasClass('start')){
  $('#counterSec').fadeOut(500).html(0).fadeIn(500);
  $('#counterMin').fadeOut(500).html(0).fadeIn(500);
  $('#counterHour').fadeOut(500).html(0).fadeIn(500);
  $('#timer').val("Stop Timer");
  timer = setInterval ( "increaseCounter()", 1000 );
  $('#timer').removeClass('start')
 }
 else{
  if(typeof timer != "undefined"){
   clearInterval(timer);  
  }
  $('#timer').val("Start Timer");
  $('#timer').addClass('start')
 }
}
 
function increaseCounter(){
 
 var secVal ;
 var minVal ;
 secVal = parseInt($('#counterSec').html(),10) 
 minVal = parseInt($('#counterMin').html(),10)
 if(secVal != 59)
 $('#counterSec').html((secVal+1));
 else{
  if(minVal != 59){
   $('#counterMin').html((minVal+1)); 
  }
  else{
   $('#counterHour').html((parseInt($('#counterHour').html(),10)+1));
   $('#counterMin').html(0);
  }
  $('#counterSec').html(0);
 }
} 
in check_timer function I am checking if it's start timer or stop timer with the help of class 'start'. and I am alternately starting the set interval and stopping the set interval respectively with changing the value of button.

the logic is pretty simple...set an interval of one second to increase the value of counterSec div. Simultaneously I am checking if it is 60th second to increase the minute counter also if it is 60th minute to increase the hour counter.

Try it here :


Counter :  
0
  Hours  
0
  Minutes  
0
  Seconds  

Friday, February 17, 2012

Spring Security custom login page infinite redirect problem [Solved]

In Spring security we have an option so that we can define our own custom login page instead of one provided by Spring application context framework, this login page is used for determining the ROLE and authentication of certain user using the web application.

However I got into a problem using that when I tried the below security XML configuration :-


    <http>
            <intercept-url pattern="/**" access="ROLE_USER"/>
            <form-login login-page="/login"
            login-processing-url="/static/j_spring_security_check"
            authentication-failure-url="/login?error=t"/>
        </http>
       
        <authentication-manager>
         <authentication-provider>
          <user-service>
           <user name="rahul" password="rahul123" authorities="ROLE_ADMIN,ROLE_USER"/>
          </user-service>
         </authentication-provider>
        </authentication-manager>
       
when I hit the URL it was getting into an infinite redirect request for the login page, Firefox gave an error message like "Firefox has detected that the server is redirecting the request for this address in a way that will never complete".

When I looked into it I could get the real reason of the problem. Actually since the pattern for intercept-url security tag was "/**". It simply meant that any request which start with "/" should be intercepted and authenticated for ROLE_USER. For this authentication purpose Application Context searches for "/login" page but "/login" also start with "/" and hence it is again intercepted and should be authenticated for ROLE_USER for which it would again go for "/login". So it actually is a perfect example of Catch-22 situation. the request was kept redirecting to itself.

Now since we have understood the problem the solution should not be difficult. personally I think giving a pattern as "/**" has dangerous conundrum so instead one should use something like "/home/**" (or any other pattern which should not cover "/login")  for intercept-url so that "/login" should not be authenticated. doing this solved my problem.
     

        <intercept-url pattern="/home/**" access="ROLE_USER"/>

Budding Developer's Ground: SOQL - Learning the parent-child queries for Salesforce 1

Budding Developer's Ground: SOQL - Learning the parent-child queries for Salesforce 1

SOQL - Learning the parent-child queries for Salesforce 1

Well... Lots of time the need arises to show all the opportunities for one contact ( or similar parent child relationships )

Considering it, there are two major part of this first is the query part where we would call all the opportunities for the contacts
 and second is the concept of wrapper class which is actually the mapping of two or more entities.

In salesforce to have a join in a query you must have a parent-child relationship....it's pretty easy to understand that one parent can have many childs.

So Actually there can be two ways of writing a query

(i) Make the child as the central object of the query.
     ex. - [SELECT NAME, Contact.Account.NAME , Contact.Account.somefield FROM Contact];

(ii)Make the parent as the central object of the query.
     ex. - [SELECT NAME, somefield, (SELECT NAME FROM Account.Contacts) FROM Account];

Both the queries above will retrieve same data but in different format and both are used according to the need or requirements (ex - For which entity sorting etc. is needed).

Noticeable things are
  1.  while referring to parent the dot(.) notation is used (contact.account.name in first query).
  2.  while referring to child a subquery is used with the fields it is retrieving .
  3.  in second query the child subquery consist Account.Contacts (and not Account.Contact). Actually here the Contacts does not represent a object type instead it represents the relationship between the Account object type and Contact Object type.
  
Hence one important thing to note is that while referring to a child subquery we must use the name of the relationship and not the object.

P.S you can get the name of the relationship from looking into the 'wsdl' file (type="tns:queryResult" represent child relationship name
to be used in subquery).

Things which wouldn't work :

a> [SELECT * FROM Contact] . There is nothing like '*' in soql, you have to write the fieldnames you want to retrieve.

b> [SELECT id, name, (select name from Account.Contact where createdDate < 2010-01-01), (select name from Account.Contact where createdDate > 2010-12-31) FROM Account]
    You can't use two subqueries for same child relationship while retrieving.


c> [SELECT name,(select name,(select name from Opportunities) from Contacts) FROM Account], the subquery inside another subquery i.e. nested subqueries are not allowed.


   
Due to these reason you would have to frame the query according to your need.
   

This should be enough for the start, We would dwell into parent-child relationship query more in the next part.