|
Shiv Kumar Oodles

Shiv Kumar (Manager-Solutions Architect)

Experience:11+ yrs

Shiv is an experienced Java Developer with a strong background in multiple technologies. He specializes in defining system architectures to ensure reliable and resilient solutions. He possesses a comprehensive understanding of the latest technologies and has hands-on experience in Core Java, Spring Boot, Hibernate, Apache Kafka messaging queue, Redis, as well as relational databases like MySQL and PostgreSQL, and non-relational databases like MongoDB. He excels in API implementations, Microservices, Web Services development, testing, and deployments. Shiv actively contributes to code enhancements and consistently delivers valuable contributions to various client projects, including Fabtrack, Pando, Pandojo, Digikam, WhatsApp Integration, Croniz, Punchin Application, Script TV, Bhaasha, and more. He demonstrates strong analytical skills and a creative mindset. In addition, he has a passion for reading books and exploring new technologies and innovations.

Shiv Kumar Oodles
Shiv Kumar
(Solutions Architect)

Shiv is an experienced Java Developer with a strong background in multiple technologies. He specializes in defining system architectures to ensure reliable and resilient solutions. He possesses a comprehensive understanding of the latest technologies and has hands-on experience in Core Java, Spring Boot, Hibernate, Apache Kafka messaging queue, Redis, as well as relational databases like MySQL and PostgreSQL, and non-relational databases like MongoDB. He excels in API implementations, Microservices, Web Services development, testing, and deployments. Shiv actively contributes to code enhancements and consistently delivers valuable contributions to various client projects, including Fabtrack, Pando, Pandojo, Digikam, WhatsApp Integration, Croniz, Punchin Application, Script TV, Bhaasha, and more. He demonstrates strong analytical skills and a creative mindset. In addition, he has a passion for reading books and exploring new technologies and innovations.

LanguageLanguages

DotEnglish

Conversational

DotHindi

Fluent

Skills
Skills

DotTranscoding

60%

DotNetflix Open Connect

60%

DotMySQL

80%

DotAWS

80%

DotKafka

80%

DotFFmpeg

80%

DotOBS Studio

80%

DotAmazon Prime Video Direct

60%

DotTechnical Project Management

80%

DotRedis

80%

DotPostgres

80%

DotJava

80%

DotKaltura

40%

DotDASH

60%

DotEncoding

60%

DotAnt Media

60%

DotSpring Boot

80%

DotHLS

80%

DotDRM

60%

DotZoom

80%
ExpWork Experience / Trainings / Internship

Aug 2013-Present

Solution Architect

Gurgaon


Oodles Technologies

Gurgaon

Aug 2013-Present

Aug 2013-Present

Solution Architect

Gurgaon


Oodles Technologies

Gurgaon

Aug 2013-Present

EducationEducation

2008-2012

Dot

PTU

B. Tech-CSE

Top Blog Posts
Implementation of Microservice Architecture in Java and Nodejs

In my last blog, I have discussed the main basic components of the microservice architecture. In this blog, we will discuss about some implementation techniques of the microservice architecture using Java and node-js technologies. I have worked on one project which converts currency from one format to other.

 

Below diagram will describe the structure of the application. Zuul Gateway has an inbuilt Ribbon load balancer. Zuul Gateway is the entry point for all the clients and it connects with Eureka Server to get the location of deployed microservice instances. All the node-js microservices below registered themselves with Eureka Server using Client Side Discovery pattern.

 

Architecture Diagram of Microservice Implementation :

 

 

In this application we are using different microservices which are listed below with description:

1. User Microservice :

User microservice is written in node.js and is used to create users and to fetch user's information. It is also used as metadata service because we can create exchange data from this microservice also.

 

2. Forex Microservice :

Forex microservice is also written in node.js and is used to set the value of one currency against another currency. We can also get the value of one currency against another one.

 

3. Currency Conversion Microservice :

Currency conversion microservice is also written in node.js and is used to convert the value of one currency into another currency value. To convert the value it will first fetch the value of one currency against another one from Forex microservice and then do the manipulation to show the converted value. In this case, one microservice(Currency Microservice) calls another microservice(Forex microservice) and it is done by using Feign Client.

Feign Client: If you want to call any method of another application in our application then you have to do either rest or soap request. Currently, rest is used mostly due to its flexible nature. To do rest call to other application you have to use rest-template in which we have programmatically build proper URL of the target with all the URL parameters and then pass it to a method of rest-template to fetch the data. To do this developer has to take care of everything rather than focus on the business logic of the application.

 

To overcome this issue, Netflix provides an abstraction over the REST API calls in which developers don’t have to bother about the internal working of REST APIs. The developer has to only define interface against the rest API and then the original request will be constructed by Spring itself. It hides the internal working of REST APIs and developers have now more time to focus on business logic. Request encoding and response decoding are also provided by Netflix Feign Client. We have to only provide the serviceId of the microservice and then Feign on the fly provide the implementation of the defined interface by using the declarative principle to call another microservice. Feign ask the Eureka discovery about the actual location of requested microservice on the basis of provided service-id and call the actual instance with the desired URL. @FeignClient annotation is used on the interface which takes service-id as a parameter.

 

How to implement Feign Client in Java:

 

1. You have to first add Feign dependency in your project pom.xml like

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-feign</artifactId>
  <version>1.4.2.RELEASE</version>
</dependency>

 

 

2. Add @EnableFeignClients in the main class of the project which let the project know that Feign will be used and enable the project to scan the Feign related annotations.

 

3. Then you have to define an interface annotated with @FeignClient which take service-id of another microservice as the parameter for which call is made. In this interface, Methods are defined with a type of request with annotation @RequestMapping.

 

Example :

           	@FeignClient(name="forex-service")
		public interface ForexClient {

		@RequestMapping(method = RequestMethod.GET, value = "/currency-exchange/from/{from}/to/{to}")
		public ResponseEntity  convertCurrency(
			@PathVariable(value = "from") String from, @PathVariable(value = "to") String to );
		 }

 

In above code snippet Feign will provide the implementation of the interface on runtime but for this Feign has to know beforehand about microservice which he has to call. That is why service-id is provided with @FeignClient annotation. Feign connect with Eureka server to get the actual host location of that microservice using service-id and then call the actual instance with URL provided in @RequestMapping annotation.

 

4. Atlast we have to autowire the interface defined above into required controller or service to inject the implementation of the Interface at runtime and call the interface method. Example:

	@RestController
	public class CurrencyConversionController {
    	@Autowired
    	ForexClient forexClient;

    	@GetMapping("/currency-converter/from/{from}/to/{to}/quantity/{quantity}")
            public CurrencyConversionBean convertCurrency(
                             	@PathVariable String from, 
				@PathVariable String to, 
				@PathVariable BigDecimal quantity) {
            try {
		ResponseEntity responseEntity = 
     						forexClient.convertCurrency(from,to);
		}catch (Exception ex){
				   ex.printStackTrace();
		}
				  .........
	   }
	  }

 

How to implement Feign Client in Nodejs :

Below are the steps to implement Feign Client in nodejs :

We will use the feignjs module and its client feign-request to implement the Feign in node-js.

1. To install both use the below commands :

npm install feignjs
npm install feignjs-client

 

There are multiple feign clients :


feignjs-request - Request module for nodejs

feignjs-jquery - JQuery module for nodejs

feignjs-node - Node module for nodejs - use http of nodejs

feignjs-xhr - XHR module for nodejs which use plain xhr.

 

But we will use feignjs-request because it is more suitable for our case. We will discuss other clients in upcoming blogs.

 

1. To install it use the command below :

	 npm install feignjs-request

 

2. Import feign in your required file using below syntax :

import feign = require('feignjs');
import FeignRequest = require('feignjs-request');

 

3. Define Apis in a declarative manner for microservices which we call using Feign using below syntax :

	var apiDescription = {
 	   saveValue : {
           method : 'POST',
           uri : '/createExchange'
        },
        getAllValue : {
           method : 'get',
           uri : '/getAllExchangeValues'
        
       };

These are the apis of the the other microservices which we have to call to fetch data using Feign.

 

4. Instantiate feign client using the below syntax :

	var client = feign.builder()
                    .client(new FeignRequest()).target(apiDescription, 'url');

	url - URL of the another microservice to which call will be made.

 

5. Make an actual call to required microservice :

Client declared above has now methods with their endpoints to make calls. Syntax for invoking the methods is given below :

Syntax :

client.method(
   [path-parameters], 
   [body/query/post-parameter-object], 
   [callback-function]
);

 

Example:

client.saveValue({ 'conversionMultiple':req.body.conversionMultiple,
                   'currencyFrom':req.body.currencyFrom, 
                   'currencyTo':req.body.currencyTo}, 
                   {Xtoken:token}
                 ).then(function(result, error){
                     if(error){
                       res.send(JSON.stringify(error));
                     }else{
                       res.json(result);
                     }
                  });

In method call, we have passed the path-parameters and one body object required for the call.

Result will be rendered as json into response object and in case of error, an error message will be sent as string format.

In this way, we can implement Feign both in java and nodejs.

We will discuss how to register microservices with Eureka in nodejs and other stuff also.

 

Thanks

Shiv Kumar

How to secure Redis database

In previous blog I have explained how to set up a key value database Redis. After installing redis we can access redis database using command redis-cli, because redis security is not implemented yet.

 

If you run following command on redis cli :

127.0.0.1:6379> auth admin
(error) ERR Client sent AUTH, but no password is set  - It gives this error message means no security password is set yet.

 

We can secure our Redis database in many ways as described below :

1. Bind it to some private ip instead of localhost :

	1.1 Open redis.conf 
	1.2 Go to configuration # bind 127.0.0.1 and uncomment it.
	1.3 Change local ip to some private ip.
	1.4 Save the changes and restart redis server.

 

2. Configure a secure password :

	2.1 Open redis.conf 
	2.2 Go to configuration # requirepass foobared and uncomment it.
	2.3 Change password to some secure password.
	2.4 Save the changes and restart redis server. 
	Now each time you access redis database and run command you have to provide password, otherwise it will give error (error) 
	NOAUTH Authentication required.
	2.5 Run command auth password . It gives OK.
	Now you can access redis database using command redis-cli -h hostname -p portnumber -a securepassword

 

3. Rename dangerous commands :

There is a set of some commands which are very dangerous to execute , so they should be executed by some authorised user.

To secure redis in this perspective you can completelly kill or rename those command using following syntax :

	Go to redis.conf and do following :
	Completely kill command :
	rename-command FLUSHDB ""  -- renamed to empty string means no operation will be performd when this command is executed.

	Rename to some other name :
	rename-command FLUSHDB FLUSHDB_MANUAL  -- command is renamed to FLUSHDB_MANUAL

	If you run now FLUSHDB on redis console it will give following error:
	(error) ERR unknown command 'FLUSHDB'

 

4. Another way to secure redis is setting permission and ownership to the redis folder using following command :

	sudo chmod 700 redis folder

Thanks

Shiv Kumar

Ways to show conditional error messages in jquery validate plugin

Sometimes we are required to show validation messages depending upon some conditions and check. Jquery Validate plugin provides different ways as described below to do the same.

 

Way 1 : Using "depends". It is not a rule but the property which toggles the rules on or off. "depends" is used only in rules section not in messages section.

 

{
    eventName: {
        required: {
            depends: function(ele) {
                return $('#eventCheckBox').is(':checked');
            }
        },
        minlength: 3
    },
    ...
},
messages: {
    eventName: {
        required: "Event name is required",
        minlength:  "Event name should be atleast 3 characters long."
    },
    ....
},

 

Way 2 : Using Inline check inside the rule section :

 {
        eventName: {
            required: $('#eventCheckBox').is(':checked')? true : false,
            minlength: 3
        }
    },
     ...
    messages: {
        username: {
           required: "Event name is required",
           minlength:  "Event name should be atleast 3 characters long."
        }
	...
    },

 

Way 3 : conditional checks in addMethod:

   jQuery.validator.addMethod("isValidMemberType", function(value, element) {
        var order = $(element).attr('data-order');
        if(value == 'PEOPLE_AND_COMPANIES')
        	return true;
        else if(value == 'PEOPLE_ONLY' && $('#company'+order).val() != 0)
        	return false;
        else if(value == 'COMPANIES_ONLY' && $('#user'+order).val() != 0)
        	return false;
        else return true;
    }, function(value,element){
    	if($(element).val() == 'PEOPLE_ONLY')
        	return $L("people.only.setting.error");
        else if($(element).val() == 'COMPANIES_ONLY')
        	return $L("company.only.setting.error");
    });

Thanks

Difference between BeanFactory and ApplicationContext

BeanFactory is also called basic IOC and ApplicationContext is called Advanced IOC. Although BeanFactory and ApplicationContext both are used to get the beans from IOC container by using method getBean(String beanName). But they have some significant differences in their implementation which are described as below :

 

1. BeanFactory uses lazy initialization approach whereas ApplicationContext uses eager initialization approach.

i.e BeanFactory creates a singleton bean only when it is requested from it but ApplicationContext creates all singleton beans at the time of its own initialization.

 

2. ApplicationContext creates and manages resources objects on its own whereas BeanFactory used to be explicitly provided a resource object using the syntax :

	ClassPathResource resource = new ClassPathResource("beans.xml");
	XmlBeanFactory factory = new XmlBeanFactory(resource);	// Here resource object is provided explicitly

 

3. ApplicationContext supports internationalization but BeanFactory do not.

 

4. Annotation based dependency Injection is not supported by BeanFactory whereas ApplicationContext supports using annotation @PreDestroy, @Autowired.

 

Thanks

Difference between get and load method of hibernate session

get() and load() both methods have same syntax and is used to load an entity using its id.But they are internally different in their mechanism.

public Object load(Class entityClass, Serializable id);
public Object get(Class entityClass, Serializable id);

 

Differences :

1. get() method provides the implementation of eager loading(load state and relations initially) whereas load() method provides the implementation of lazy loading(load state and relations when actaully needed).

2. When requested object is not found in the database get() method returns nullPointerException whereas load method throws objectNotFoundException.

3. get() method can be used in attached(session is active) as well as detached(session is closed) mode whereas load method can only be used in attached mode. In case of load() , if session is closed and then we try to access state of any object , LazyInitializationException is thrown.

 

I have following Emp table with 3 records.

Emp Table 
id  name     job         salary
1   Rahul    Developer   20000
2   Gaurav   Tester      19000
3   Gagan    Designer    20000

get() method Example :

public static void main(String arr[]){
Session session = MyFactory.getSession();
Emp emp = (Emp) session.get(Emp.class, 1);
//Emp emp = (Emp) session.get(Emp.class,5); It will throw nullPointerException beacuse id 5 is not available in db.
session.close();
System.out.println("Session closed, Entity state is :");
// State will be printed here becuase object is loaded eagerly with its own state and relations.
System.out.println(emp.getName()+" "+emp.getJob()+" "+emp.getSalary());
}

load() method Example :

public static void main(String arr[]){
Session session = MyFactory.getSession();
Emp emp = (Emp) session.load(Emp.class, 1);
//Emp emp = (Emp) session.load(Emp.class,5); It will throw objectNotFoundException beacuse id 5 is not available in db.
session.close();
System.out.println("Session closed, Entity state is :");
// LazyInitialization error will be thrown as object is loaded in lazy mode.
System.out.println(emp.getName()+" "+emp.getJob()+" "+emp.getSalary());
}

THANKS

Know about Generic Validator

In grails, validation is checked with either using command objects or using domain level validators, but both of them have pre-defined validation to check.

If we have to check some different kind of validation then we have to prepare regex for them or create some custom validator to validate.

But Generic Validator class of org.apache.commons.validator provides some static methods to check different kind of validations which we have to customise in command objects and in domains.

 

Some of the methods of class are described here :

 

isBlankOrNull(String value) : Checks if the field isn't null and length of the field is greater than zero not including whitespace.

isInt(String value) : Checks if value can be converted to a int primitive type or not.

matchRegexp(String value, String regexp) : Checks if the value matches the given regular expression or not.

isInRange(int value, int min, int max) : Checks if a value is within a given range or not.

 

Other benefit of using these methods is your code will look neat and clean.

THANKS

How to partially search a word in solr

I have faced an issue while implementing searching using solr.

I have to search for partial word and its working fine for single word or multiple words but creates an issue in a specific scenario :

When I seached for

"shiv" ---- working fine
"shiv kumar" --- working fine
"shiv kum" --- working fine
"shiv k"  -- failed here

 

The issue is not with my code but with tokenizer(StandardTokenizerFactory) solr configuration in schema.xml.

In schema.xml, StandardTokenizerFactory has a filter EdgeNGramFilterFactory configured in different way like :

<filter class="solr.EdgeNGramFilterFactory" minGramSize="2" maxGramSize="30" side="front" luceneMatchVersion="4.3"/>

 

It means it will create n-gram for the input starting from front edge and grouping element in pair of two and more and upto 30.

minGramSize plays an importatnt role here and specifies the minimum numbers of characters for grouping. You can understand this by example :

The above configuration will give the following output for the word "higher" :
 higher ---> "hi", "hig", "high", highe", "higher"

By putting minGramSize ="1" solved my problem as it gives the following output:
 higher ---> "h", "hi", "hig", "high", highe", "higher"

 

In earlier configuration there was grouping of 2 that is why "shiv k" case was failing because no combination was there for single "k".

Setting minGramSize to 1, search for single "k" also works and solved the issue.

Hope this will surely help you.

Thanks

How to get most relevant result using solr boosting

Recently I have faced an issue while implementing suggestion dropdown in our app using solr.

We had to show the very most relevant result to the user according to the role of user and other conditions.

Solrj API is already integrated in the app and it helped me to solve the issue with the help of boosting feature of solr to get the most nearby result.

 

Solr provides two types of boosting mechanism :

1. Index Time Boosting mechanism : It is done when we have to index data into solr.It can be applied to whole document or on a specific field also.

2. Query/Search Time Boosting mechanism : It is done while searching from solr. It can be applied on different fields according to result requirement.

I prefered second mechanism as it was solving my problem without affecting other modules.

Boosts are applied on different fileds using caret symbol(^) along with weightage value on the searched text or on a specific query part like:

(eventName:"Grails Conf" AND location:Delhi)^2.0 OR eventName:"Grails Conference"^10

In the above query :

eventName:"Grails Conference"^10   --- boosting is applied on the searched text only and weightage is 10.
(eventName:"Grails Conf" AND location:Delhi)^2   ---boosting is applied on a query part and weightage is 2.

 

Higher the weightage , higher is the priority of execution.

 

According to my requirement I have to apply the boosting on query parts like :

solrQuery = "(type:(com.threebaysover.events.Event) AND event_privacy:Public AND (event_status:Published OR event_status:Cancelled) AND (start_date:[* TO NOW] OR end_date:[* TO NOW])  AND ((connection:(${user.uniqueKey()}))^10 OR (followers:(${user.uniqueKey()}))^5 OR  (company:(${currentCompanies}))^4  OR  (event_partner_company:(${currentCompanies}))^2 OR (venue_company:(${currentCompanies}))^1))"

In above query , priority of execution is connection > followers > company > event_partner_company > venue_company according to weightage defined.

All time we have not to search for those results which fulfill the desired criteria.Sometimes our requirement is to search for those elements which are out of the desired criteria or we can say odd results.

 

For such types of situation, there are two types of boosting provided.

1. Positive Boost : It always fetches those reults which fulfill the given condition in query. Solr and Lucene Library both provide support for positive boosts.

2. Negative Boost : It fetches those result which are negative. For example you have to search for people who are not attending an event.

Solr doesn't support negative bbost while lucene do. It is implemented using query like :

(eventName:"Grails Conf" AND location:Delhi)^2.0 OR -eventName:"Grails Conference"^10

Above query will fetch those results in which eventName is not "Grails Conference".

Hope this will help you also.

Thanks

GORM fetching mechanisms and issues

Gorm provides two type of fetching mechanism using Hibernate under the hood :

1. Eager Fetching (Instant Loading)

2. Lazy Fecthing (Lazy/Late Loading) Lazy fetching is default.

 

1. Eager Fetching : Using this fetching mechanism the required instance along with their associated properties immediately loads into the memory but with separate db select query for associations(Internally).

	static mapping={
 	   propertyName lazy : false
	}
	
	Another alternative to lazy : false

	static mapping={
 	   propertyName fetch : 'join'  // It will also load the associated objects in single query by left outer join.
	}

 

Pros : It avoids extra database query to fetch other associated properties and we can directly get them from the instance immediately.

Cons : It downgrade the performance of the application as the whole data is not required immediately in all the cases.

 

2. Lazy Fetching : Using this fetching mechanism, the associated data is fetched on the demand with the help of new separate query on database.

	static mapping={
 	   propertyName lazy : true   // default
	}

 

Pros : No overloaded data every time, as the required things are fetched on demand.

Cons : There are several issues with Lazy fetching.

 1. Separate query is required to fetch the associated data.

 2. It creates issue when object is deattached from session and throws LazyInitializationException.

I have faced the same issue while working with a module. The required object lost from the hibernate session and no other property was available then.

 

Ways to solve the problem :

1. Make the fetching mode "eager" but it is costly.

2. Again attach the object to the session using either of the following approach :


	I. merge() - Again attached the object to the session but persist the current changes to the database.
		User user = User.get(1)
		....
		....
		object deattched from session
		....
		....
		user = user.merge()

	II. attach() - Attach the object to the session again but regardless of the changes in the database.
		User user = User.get(1)
		....
		....
		object deattched from session
		....
		....
		user = user.attach()

 

Using the above mentioned appraoch I am able to attach the instance to the session and it solved my problem.

Hope this will also help you.

Thanks

How to show timezones in GMT format in java

We all required date and time along with timezones in our project at various stages like sign up, event management, time management and etc. Java provides class java.util.TimeZone to play with time zones.It provides various methods to do so.

In my project, the requirement was to show the time zones in GMT format along with city name.

TimeZone class provides method getAvailableIDs() which gives a string array in the following format:

Pacific/Midway
Pacific/Niue
America/Adak
Etc/GMT+10 and so on...

But what I required was all the times zones in GMT format along with city name only like :

 (GMT-11:00) Midway
 (GMT-11:00) Niue
 (GMT-10:00) Adak
 (GMT-10:00) Atka

After some work around with the available list provided by getAvailableIDs(), finally I got the list in desired manner with the below code snippet :

I created a custom taglib to show all the time zones in gsp :

def timeZones = { attrs,body ->
		def allTimeZones = TimeZone.getAvailableIDs();
		List timeZoneList = []
		allTimeZones?.each{
		def timeZone = TimeZone.getTimeZone(it) // fetch timezone object from string name
		long hours = TimeUnit.MILLISECONDS.toHours(timeZone?.getRawOffset());  // find hours from offset value to display
		// find minutes from offset value to display
		long minutes = TimeUnit.MILLISECONDS.toMinutes(timeZone?.getRawOffset()) - TimeUnit.HOURS.toMinutes(hours);
		minutes = Math.abs(minutes);
		String result = "";
		if (hours > 0) {
			result = String.format("(GMT+%d:%02d) %s", hours, minutes, timeZone?.getID());
		} else if (hours < 0){
			result = String.format("(GMT%d:%02d) %s", hours, minutes, timeZone?.getID());
		}else{
			result = "(GMT)"+' '+ timeZone?.getID();
		}
		timeZoneList.add(result)
		}
		out << body(timeZoneList:timeZoneList)
	}


def timeZoneDisplayName = { attrs->
		def zoneName = attrs?.timeZoneName.trim()
		def zoneParts = zoneName.split(' ') 
		def locationName =  ''
		if(zoneParts[1]?.indexOf('/') == -1)
		locationName = zoneParts[1]
		else 
		locationName = zoneParts[1].substring(zoneParts[1].lastIndexOf("/") + 1)
		def displayName = zoneParts[0]+' '+locationName.replace('_', ' ')
		out << displayName
	}

In gsp use taglib like :

		<select multiple="multiple" name="timeZone" id="addTimeZone">
                   <event:timeZones>
                       <g:each in="${timeZoneList}">
                          <option value="${it.toString().trim()}"><event:timeZoneDisplayName timeZoneName="${it}"/></option>
                       </g:each>
                   </event:timeZones>
                </select>

This workaround provides me the desired data.

Applying small changes you can also get the time zones in your required format if format is different from this one.

 

THANKS

How to internationalize javascript messages in grails Application

Internationalization is easy in grails gsp pages with the help of property file and g:message tag of grails.But sometimes we have to show the messages in javascript files according to locale. 

 

In that case we have two ways to do so :

1 - Each time we have to pass the i18n messages from controller to client side in response.

2 - Internationalize the required messages on the client side instead of server side.

First approach is not suggested in case of javascript as it is not a good practice to fetch the i18n messages each time from server.

Second approach is good and suggested as it makes the work easier as we have not to make the server call when require internationalized message most of the time.

So the question is how to localize messages in javascript?

There are plugins in grails to do so like JAWR and i18n-asset-pipeline plugin. As I went through both of them, found that JAWR requires lots of configuration and little bit tricky to use but i18n-asset-pipeline plugin is easy to understand and requires less effort and configuration.

 

Usage : i18n files in grails-app/i18n:

In messages.properties

user.title= "Username"
user.password = "Password"

In messages_fr.properties

user.title= "nom d'utilisateur"
user.password = "mot de passe"

 

How to use i18n-asset-pipeline ?

We have to do three simple steps to implement this.

1.In grails i18n property files are stored in grails-app/i18n. To implement localization in javascript we have to define same i18n files set in grails-app/assets/javascript like :

messages.i18n
messages_fr.i18n

2. In grails-app/i18n, files contains key value pair but in grails-app/assets/javascript, files will contain only keys that we want to internationalize not the values. In this case I want to localize only username so I added only username only in both above define files.

user.title

3. Now its time to use the things in javascript. In main layout gsp define grails tag to load the locale like :

<asset:i18n name="texts" locale="${locale}" />

locale - Locale that should be loaded

name - name of i18n file to be loaded.

Default name is messages. and in java script file use :

alert($L("user.title");

Done. Now you can localize any message in javascript.

 

THANKS

Use of grails service in views

Use of Grails service in views

Why not to use services in views directly:

Reason 1: Generally we don't prefer to use the services in grails view as they decrease the performance of application if they are transactional in nature.But we can use them in view in non transactional manner.

 

Reason2 : If we use services directly in view, then class loader will load that service only by autowiring but not the other services which are referenced in that service.

If services are transactional in nature then either we have to use taglibs in order to use the methods of service or make a controller call to invoke the service method.

How to use in view if necessary:

Way 1 :

 
<g:set var="user" bean="userService"/>

We can access any service or Spring managed bean in application with the help of this bean attribute in g:set tag of grails.

Way 2:

<%
    def myService = grailsApplication.mainContext.getBean("myService");
%>

Using this syntax we can get the reference of the service bean from the Application Container used by the framework.

Way 3:

<%@ page import="com.myproject.MyService" %>
<%
    def myService = grailsApplication.classLoader.loadClass('com.myproject.MyService').newInstance()
%>

It will give you new instance of service and completely lost all the autowired things.

Note : Best approach is access the service methods using taglibs.

 

THANKS

Introduction to GEB : Best way to Automation Testing

Geb is used for the browser automation testing. It provides the power of Web provider, Jquery and Groovy script in a unit. It is very useful for acceptance, web and functional testing as it supports testing frameworks like JUnit and Spock. It supports all those browsers whose webdriver we include in our application.

 

Installation : In your BuildConfig do the following configuration :

1. In grails.project.dependency.resolution section :
	def gebVersion = "0.10.0"
	def seleniumVersion = "2.45.0"

2. In dependencies section :
	test("org.seleniumhq.selenium:selenium-firefox-driver:$seleniumVersion")   // Include web driver for firefox
 	test "org.gebish:geb-spock:$gebVersion"  // Include support for spock
        test "org.gebish:geb-junit4:$gebVersion" // Include support for Junit

3. In plugin section :
	test ":geb:$gebVersion"   // GEB version to install.

Now clean and do refresh dependencies.Run the project.

 

Usage :

Step 1 : First we have to define page structure which we have to test using Geb with the help of Module and Page Obkect pattern of GEB. Create a file named "GoogleSearchModule.groovy" with the following content :

import geb.Module
import geb.Page
class GoogleSearchModule extends Module{
	// a parameterised value set when the module is included
	def buttonValue
 
	// the content DSL
	static content = {
 
		// name the search input control fields, defining it with the jQuery like navigator
		field { $("input", name: "q") }
 
		// the search button declares that it takes us to the results page, and uses the
		// parameterised buttonValue to define itself
		button(to: GoogleResultsPage) {
			$("input", value: buttonValue)
		}
	}
}

class GoogleHomePage extends Page {
	
	   // pages can define their location, either absolutely or relative to a base
	   static url = "http://google.com/ncr"
	
	   // allow verifying that the browser is at the expected page
	   static at = { title == "Google" }
	
	   static content = {
		   // include the previously defined module
		   search { module GoogleSearchModule, buttonValue: "Google Search" }
	   }
   }

class GoogleResultsPage extends Page {
	static at = { title.endsWith "Google" }
	static content = {
		// reuse our previously defined module
		search { module GoogleSearchModule, buttonValue: "Search" }
 
		// content definitions can compose and build from other definitions
		results(wait: true) {$('div.srg li').find('a')}
		dropdownResult {$('ul.sbsb_b li')}
		firstResultLink(wait: true) { $('div.srg li a') }
	}
}
 
class WikipediaPage extends Page {
	static at = { title == "Wikipedia" }
	static content = {
	englishkWikiLink (wait : true){
		$('div.lang1 a')
	}
	}
}
}

 

Step 2: Page definition is completed. Now we have to right the test case for the above defined page using Spock and Geb syntax. Create a file named "GoogleWikiSpec.groovy" with the content :

import geb.spock.GebReportingSpec
class GoogleWikiSpec extends GebReportingSpec {
	def "first result for wikipedia search should be wikipedia"() {
	given:
	to GoogleHomePage   // "to" syntax is used to direct the page to GoogleHomePage which is defined in page definition

	when:
	search.field.value("wikipedia")  // search is define in the page "GoogleResultsPage" to search any content on google page.

	then:
	waitFor(20) { at GoogleResultsPage} // waitFor is used to delay or wait for the ae response.

	and:
	dropdownResult.size() == 4
	firstResultLink.text() == 'Wikipedia'
}
}

 

Like extending the IntegrationSpec in spock testing , here we are extending GebReportingSpec to support GEB syntax like "to GoogleHomePage".

Testing spec is completed here. Now Save the things and do "test-app" to run the above spec.

Doing this automatically direct to google page then search for Wikipedia page and test the length of suggestion dropdown along with the first result title.

This is the demo version for GEB. Wait for the Geb exploration further.

 

THANKS

Introduction to data driven spock testing

Why Data driven Testing :

If we have to write test cases for different domain ,controller and services, it is fine to go with interaction based testing(which contains descriptive name of test cases).

Problem :

But problem with this it, if we have to write test case for a action or service method which takes different values and outputs different result every time based on the input given to it. Then we have to replicate the code for each input series, which seems too much handy and replicated code also spoils the look of test cases like :

class CalculationServiceSpec extends Specification {
    def "sum of two numbers"() {
        expect:

	// same line is repeated 3 times for variable input fields
        service.calculateSum(1, 3) == 4
        service.calculateSum(7, 4) == 12 //fails
       	service.calculateSum(0, 0) == 0
    }
}

Solution :

Spock framework data driven testing supports testing of same code multiple times with varying inputs and outputs with the help of data variables and data tables. Using the data driven testing above code can be refcatored like this :

class CalculationServiceSpec extends Specification {
    def "sum of two numbers"() {
        expect:
        service.calculateSum(value1, value2) == sum
       	
	where :
	value1 | value 2 | sum
	1 | 3 | 4
	7 | 4 | 12  //fails
	0 | 0 | 0
    }
}

In the above code where clause contains a datatable in which first row is table header which is used to declare the data variables(in this case value1, value2 and sum). Below the table header,there are table rows which contain corresponding value for each data variables.

For each table row, test case logic is iterated once. If an iteration fails, the remaining iterations will never be executed.

I am here testing validation on a domain in single test case using data driven syntax :

Domain :

class User {
	String username
	String password
	
	static constraints = {
		username (nullable: false,blank: false,email: true, maxSize:60)
		description (blank: false, minSize: 10, maxSize:600)
	}
}

Test case :

class UserSpec extends Specification {
def setup :{}
def cleanup() {}

@Unroll
def "test validation on #field for the error : #error"(){
	given :
	  mockForConstraintsTests User
	  User user = new User(username : '[email protected]',description: 'test description').save(flush :true)	

	when :
	  User testUser = new User()
	  user."${field}" = value

       	then :
  	  !testUser.validate()
	  testUser.hasErrors()
	  testUser.errors['$field'] == error  //mockForConstraintsTests gives the name of failed constraint

  	where:
 	 error| value | field
	  'nullable'| null | 'username'
	  'blank'| '' | 'username'
 	  'maxSize' | 'L'*61+'@gmail.com' | 'username'
 	  'email'| 'test'  | 'username'
	  null | '[email protected]' | 'username'
	  'nullable' | null | 'description'
 	  'blank' | '' | 'description'
          'minSize' | 't'*8 | 'description'
          'maxSize' | 't'*601 | 'description'
}
}

Use of @Unroll :

As you see in above test case variable '#field' and '#error' are used and @Unroll is used in top of test case.If it is not done so, then the output will be like this :

test validation on #field for the error : #error with no iteration

@Unroll annotation makes the test case name more descriptive and understandable using the value of field like :

test validation on username for the error : nullable
test validation on username for the error : blank

It also makes the identification of iteration failure very easy.

Thanks

Shiv Kumar

How to test domain validation in unit test cases

Get started with unit test case, the first thing to test is domain - the very basic and important building block for entire development process.

During domain unit testing, the main focus is on constraints validation.

Domain classes are tested by using DomainClassUnitTestMixin or the HibernateTestMixin.

 

Domain to test :

class User {
	String username
	String password
	
	static constraints = {
		username (nullable: false,blank: false, unique: true, email: true, maxSize:60)
		description (blank: false, minSize: 10, maxSize:600)
	}
}

 

Here I am discussing two ways of domain constraints validation testing by using GrailsUnitTestMixin:

 

Way 1 : By using mockForConstraintsTests

GrailsUnitTestMixin provides a method called mockForConstraintsTests used to mock validation support for classes.

mockForConstraintsTests makes the testing task easy by changing the behavior of the errors object and domainClass.errors[field] gives us the name of failed constraint.

If we dont use mockForConstraintsTests, the validation errors are of type org.springframework.validation.FieldError object like :

Property [{0}] of class [{1}] with value [{2}] must be unique
Property [{0}] of class [{1}] with value [{2}] exceeds the maximum size of [{3}]

 

Test case :

class UserSpec extends Specification {
def setup {}
def cleanup() {}

@Unroll
def "test validation on username for the error : #type"(){
	given :
	  mockForConstraintsTests User
	  User user = new User(username : '[email protected]',description: 'test description').save(flush :true)	

	when :
	  User testUser = new User(username : value)
	  if(type == 'unique') 
	  mockForConstraintsTests(User, [testUser, user])  // check for the uniqueness between two user objects

       	then :
  	  !testUser.validate()
	  testUser.hasErrors()
	  testUser.errors['username'] == type  //mockForConstraintsTests gives the name of failed constraint

  	where:
 	  type|value
 	  'maxSize'|'L'*61+'@gmail.com'
 	  'email'|'abc'
	  null|'[email protected]'
	  'unique'|"[email protected]"
}
}

 

Way 2: By defining custom abstract class and extend it instead of Specification.

Create your own abstract class like :

import spock.lang.Specification

abstract class ConstraintUnitSpec extends Specification {
    String getLongString(Integer length) {
        'b' * length
    }

    String getEmail(Boolean valid) {
        valid ? "[email protected]" : "testemail@g"
    }

    String getUrl(Boolean valid) {
        valid ? "http://www.google.com" : "http:/ww.google.com"
    }

    String getCreditCard(Boolean valid) {
        valid ? "4111111111111111" : "41014"
    }

    void validateConstraints(obj, field, error) {
        def validated = obj.validate()
        if (error && error != 'valid') {
            assert !validated
            assert obj.errors[field]
            assert error == obj.errors[field]?.code
        } else {
            assert !obj.errors[field]
        }
    }
}

Now you have to modify the above test case little bit like:

class UserSpec extends ConstraintUnitSpec {   // UserSpec extends ConstraintUnitSpec instead of Specification as done earlier
def setup {}
def cleanup() {}

@Unroll
def "test validation on #field for the error : #error"(){
	given :
	  User user = new User(username : '[email protected]',password: 'Admin@1234').save(flush :true)	

	when :
	  User testUser = new User()
	  user."${field}" = value
	 	

       	then :
  	   validateConstraints(testUser, field, error)

  	where:
 	  error| value | field
	  'nullable'| null | 'username'
	  'blank'| '' | 'username'
 	  'maxSize.exceeded' | getLongString(61)+'@gmail.com' | 'username'
 	  'email'| getEmail(false)  | 'username'
	  'valid' | getEmail(true) | 'username'
	  'nullable' | null | 'description'
 	  'blank' | '' | 'description'
          'minSize.notmet' | getLongString(8) | 'description'
          'maxSize.exceeded' | getLongString(601) | 'description'
}
}

The main concept of abstract class is to make the things reusable through out all the test cases and define custom getter for email and other things which maintain the code neat and clean.

Enjoy the stuff.

Thanks

Shiv Kumar

How to implement criteria queries in unit test cases

Intrdouction to Plastic criteria

As the name suggest Plastic Criteria means somehow it is related to criteria.It is grails plugin to mock Grails Criteria for Unit Test cases.

 

Understand the need of Plastic Criteria : Why Plastic criteria plugin is required ?

1. Criteria queries and String-based queries like [executeQuery] are currently not supported in unit tests.

2. There's no support for HQL queries in unit tests yet.

3. Criteria queries and String-based queries aren't provided by mockDomain also. We have to mock your criteria queries itself like :

In case of Criteria :

mockFor(domianName).demand.static.createCriteria = { ->
    [list: { closure -> [  ] } ]
}

 

In case of ExceuteQuery : Suppose this one is the query we have to test through test cases

UserCompanyRoleMapping.executeQuery("select distinct user from UserCompanyRoleMapping u where u.company = :company and :admin in elements(u.roles)", [user: user, company: company, admin: companyAdminRole])

 

When we have to implement such type of string based query then do something like this :

List userList = [new User(params), new User(params)]
UserCompanyRoleMapping.metaClass.static.findAllUsersByCompanyAndRoles = { String company, def role -> userList }

 

and put in domain UserCompanyRoleMapping.groovy

static List findAllUsersByCompanyAndRoles(String company, def role) {
      executeQuery('from UserCompanyRoleMapping u where ...') // put your required query here 
   }

 

Conclusion of above :

As we can see in both the above approaches, the response of Criteria queries and String-based queries is not the actual one but it is defined earlier and hard coded(userList).

 

Solution :

To overcome such situations and to implement Criteria queries upto certain extent we require Plastic Criteria plugin.

Installation :

Put the following dependecy in your buildConfig :
plugins{
    test ":plastic-criteria:1.5.1"
}

 

How to use in test cases :

1. Import in the test case in which you want to mock criteria like

   import static plastic.criteria.PlasticCriteria.* 

2. Use the following syntax to mock required domains criteria in test case setup :

   mockCriteria([user,UserCompanyRoleMapping....])

 

Pros :

1. With the help of this plugin we are able to implement criteria(createCriteria and withCriteria) in our test cases.

2. It provide support for maximum nodes defined in criteria documentation.

3. Easy to use and understand.

 

Cons :

1. This plugin does not provide support for following :

>> count distinct

>> count

>> firstResult and some more.

For more detailed information visit the link : https://github.com/fabiooshiro/plastic-criteria

Thanks

 

Different ways of service injection in spock test cases

Different ways to inject service in unit test case :

In my current project, I have assigned the spock test case for the modules.I have faced the issues mostly in injection of dependencies.After some work around, some tips are here which makes the work easier. Some ways to inject service in tresr cases :

Way 1 : Mock using annotation
	We can mock service using the @Mock annotation like : 
	@Mock([SomeService])

Way2 : If first way of mocking fails somehow then try the another way of mocking 
       def service = mockFor(SpringSecurityService)
       def control = service.createMock()
       (domain/controller/service).springSecurityService = control

Way 3 : If anyhow above defined methods of injection not work then inject the service using new object instance
	(domain/controller/service).userService = new UserService()

Here are the ways to inject springsecurityService current user in unit test case :

For springSecurityService current user:

Way 1 : user should be available at this time
	controller.springSecurityService = [currentUser:[id:user.id]]

Way 2 : user should be available at this time
	controller.springSecurityService = [currentUser:user]

Way 3 :
	def springSecurityService = [getCurrentUser: {-> return user }] as SpringSecurityService
	service/controller.springSecurityService = springSecurityService

 

THANKS

 

Multiple file upload in grails

Many times we have to upload multiple files along with form data in many registration forms or in other processes and then we have to process all of them at backend.

We can do this using HTML5 attribute "multiple" in this way :

1. First include the attribute enctype='multipart/form-data' in form tag otherwise you can't access/read the files at server end on submit.

<g:form enctype='multipart/form-data' method='POST' action='saveUserProfile' controller='user'> 
...    
</g:form>

2. Use HTML5 multiple attribute for file inpute tag :

<g:form enctype='multipart/form-data' method='POST' action='saveUserProfile' controller='user'>
    <input type='file' name='userFiles[]' multiple />    //Using this we have upload multiple files in one go. 
    <button type='submit'>Submit</button>
</g:form>

3. On server end we will read and process the files one by one in such a manner :

request.getFiles("userFiles[]").each { file ->
 log.debug(file.originalFilename) // persistence logic or logic as per requirement.
}

Simple and easy approach to upload multiple files at a time.

Thanks

Shiv Kumar

Git : The remote end hung up unexpectedly

While committing data to github, after commit if we push the data to github it sometimes gives the error :

fatal: The remote end hung up unexpectedly
Everything up-to-date

To solve this issue simple use one of the below mentioned solution.

Solution 1:

1. Run this command in your terminal 
git config http.postBuffer 524288000  //This will increase the buffer size to 500 Mb.

2. Close the terminal and then again push the data to github.

Solution 2:

1. Run the command in terminal :
git remote add origin [email protected]:username/project.git 
//username is the name of authorised github user.and project.git is your project git(i.e if your project is testApp then testApp.git)

2. Close the terminal and then again push the data to github.

Thanks

Shiv Kumar

How to provide dynamic validation in jQuery validate

While using jQuery validation, in most of the cases we have to provide static validation(i.e min/max value or digits only etc.).

But somtime we have to provide dynamic validation which will change every time.

Problem : Validation defination for height is:

height: { 
    required: true,
    number: true, 
    max: $('#maxHeight').val()
},

If we provide dynamic value like this then it will run only once as the .validate() method initializes only once and

assign the value of maxHeight once for validation which doesn't change every time.

Solution : We have to do little bit change to the above defined validation like this:

Way 1:

height: { 
    required: true,
    number: true, 
    max: function(){ return $('#maxHeight').val() }
},

Doing this,each time function is called for applying max validation which in turn return the dynamic value each time.

Way 2: We have to use the "rules" method which will update the rules dynamically like :

$('#maxHeight').rules("add", {
     max: $('#maxHeight').val();
});

Way 3 : Another way is define your custom rule for the field in this way :

jQuery.validator.addMethod("maxHeight", function (height, element) {
    var max_height = $('#maxHeight').val()
    return max_height == height;
}, "Invalid height");

then in validate method just use :

height: { 
    required: true,
    number: true, 
    maxHeight: true
},

Note : This custom rule must be define before the .validate() method .

Thanks

Shiv Kumar

Fix Upload Failed issue while using fine uploader

While uploading file using php fine uploader, we sometime face problem that

"Each uploaded file successfully uploaded in back-end but still front-end always indicates 'Upload Failed'".

It is not actually any bug but it depends upon our response which we serve to uploader after file upload.

If we return something other than "{ success : true }", it will continue in "Upload Failed".

So to avoid this problem always return "success : true" along with other required data.

Upto verion 3.x fine uploader is free, but upgraded version 4.x and 5.x are paid.

Thanks 

Shiv Kumar

How to fix service dependency injection issue

Sometimes we encounter problem in injecting services in domain and controller when service name is started with more than one capital letter in beginning like TEStService.

 

In this case you can't inject service using "def testService" and it gives null pointer exception for this service.

If you rename it as TestService it will work fine.

 

Reason behind this :

Actually naming the service like this violates the grails naming convention and

grails convert the name "TESTService" into "testService"(lowercased all the uppercase letter except last one) instead of "tESTService".

 

Solution :

To avoid this problem inject the service like "def TESTService".

It will solve your problem.

 

Thanks

Shiv Kumar

How to implement filter for index action Sometimes we have to do some pre-processing before request intercepts and post-processing after request process. We use filters for that as they are also easily manageable by plugin. Syntax to define filter :
def filters = { testFilter(controller: 'test', action: '*') { before = { if (actionName.equals('update')) { // do something } after = { Map model -> } } } }
It is easy to apply filter any action and controller.But for default action "index",this won't work :
filterForIndex(controller: 'test', action: 'index') { before = { // do something } } filterForIndex(controller: 'test', action: 'null') { before = { // do something } }
If you log actionName in action "index" like :
log.debug"actionName == "+actionName // it will give null
Now do whatever you want in filter for index action. Thanks Shiv Kumar
Implement default layout with configuration in grails

When we set up a new grails project, grails provide us a default layout(main.gsp) in views > layouts > main.gsp.

This layout is implemented on the required page by using meta tag like :

<meta name="layout" content="main"/>

If we have lots of pages and using the same line of code on each required page becomes cumbersome.

Here is a better way to avoid such situation by configuring it in config.groovy.

Yes grails provides an easiest way to configure default layout so that meta tag is not used on each page. By doing this, main.gsp layout will be available on each page. 

 

All you have to do is make this configuration in config.groovy :

grails.sitemesh.default.layout = 'main'

Advance Approach :

If you don't even want to define this configuration in config.groovy and want to include layout on your gsp view, smart grails is here for you.

Just rename your main.gsp to application.gsp and run the project with clean.

 

Grails will automatically provide the layout on each view page without using any meta tag or configuration.

 

Thanks

Shiv Kumar

Groovy Tips for date and list

Some useful groovy tips  : 

1. List Size : Often we used to find list is empty or not. For this we do something like :

	List numberList = [1,2,3]  // list in this case
	if(numberList.size() == 0){
	...
	}
	else{
	...
	}

Rather than doing this, we can simply use groovy advancement like :

	if(!numberList){   // checks is list empty?
	...
	}
	else{
	...
	}

2. Finding current day,month and year from the date : 

 As Date methods are deprecated to find out current day, month and year from date object and other information.

We can use Calender Object for this like (current date is 29/08/2014):

	Calendar now = Calendar.getInstance()   // Gets the current date and time.
	def year= now.get(Calendar.YEAR);       // year = 2014

	def month= now.get(Calendar.MONTH)+1 ; // month = (8+1) = 9
	// Reason for incrementing by 1 is that first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last
	depends on the number of months in a year.
	So if your current date is 29/08/2014 then it will return month as 8.

	def day = now.get(Calendar.DAY_OF_MONTH)  // day = 29

You can also find more information about zone, week of year,week of month and hour of day etc. from Calender object

Thanks

Shiv Kumar

Fixing .gitignore functional error during commit

Sometimes we add some files in our .gitignore to avoid them from commit but still they appeared in our changes to commit and violat .gitignore.

Here are four easy steps to make .gitignore functional again :

Caution : Be aware to commit all your changes before or backup them, otherwise you will loose control on all the changed files that may be important to you.

Step 1 : git rm -r --cached .       // This will remove any changed files from the index.

Step 2 : git add .		       // To add the changes file to commit

Step 3 : git commit -m "fixed .gitignore issue"  

Step 4 : git push origin master(your working branch)

Now added files to .gitignore will never appear in your changes to commit.

Thanks

Shiv Kumar

Validate US phone number using jQuery validation

I had a requirement to validate US phone number in (ddd)ddd-dddd format using jQuery.

I have solved this problem by using jQuery validation plugin :

$("#myform").validate({
rules: {
field: {
required: true,
phoneUS: true
}
}
});

Files required for this :

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script>

It sometimes doesn't work.

To get it working we have to include Jquery addtional-methods.min.js

If still it doesn't work then you have to use following script before validate method which contains regex to validate :

jQuery.validator.addMethod("phoneUS", function (phone_number, element) {
            phone_number = phone_number.replace(/\s+/g, "");
            return this.optional(element) || phone_number.length > 9 &&
                  phone_number.match(/\(?[\d\s]{3}\)[\d\s]{3}-[\d\s]{4}$/);
        }, "Invalid phone number");

It finally solve the problem. If you have required some other format then modify above regex according to need.

Thanks

Shiv Kumar

How to implement history plugin in single page application

I had a requirement to implement history plugin to support back button functionality in my project.

I have used HTML5 history plugin along with localStorage to store data as browser has not so much space to store and handle mass data.

 

Advantage of localStorage :

As I have to store the exact state of page so it was necessary for me to store the each update on the page so that on backtrack the exact state can be recieved and localStorage gave me that facilty.

 

Requirements:

1. History.js You can get it from https://github.com/browserstate/history.js/zipball/master

 

Way to use history plugin to push state to History :

History.pushState(data, "title", "url");

e.g : History.pushState({state:1}, "State 1", "?state=1"); 

My all action returns json data so I put each data in localStorage like :

$.localStorage('styles',data);  // here "style" is key and "data" is JSON content got in ajax response.

and way to recieve that data again is :

var State = History.getState();  // state stored
var data = $.localStorage(State.title)  // title in this cas is "styles".

Generally when state of page change then history plugin works using its "window.onstatechange" event. We can use this like :

History.Adapter.bind(window,'statechange',function(){ 
        var State = History.getState(); // Note: We are using History.getState() instead of event.state
    }); 

 

It also provides some function like :

1. History.back()   // Go back once through the history.

2. History.forward()   //Go forward once through the history.

3. History.go(X)    //If X is negative go back through history X times, if X is positive go forwards through history X times1.

 

ReplaceState in place of pushState :

Sometimes it is required that we have to perform some ajax operations of the crrent accessed page.In this case replacestate is used instead of puh state as it replace the currently pushed state with the new new so that on backtrack you can get exact state.It has the same syntaxlike pushState:

History.replaceState(data,title,url) 

Replaces the existing state with a new state to the browser. data can be null or an object, title can be null or a string, url must be a string

 

Thanks

Shiv Kumar

How to use handlebars in grails

Sometime we have required to update or insert some data in DOM after an ajax call and we do this by manually creating html markup to show the ajax response.

It looks very much handy if we have insert very much data in DOM. But using handlebars we can get out of this problem as handler provides the option of javascript templating.

It makes the task DOM updation and insertion by using the handlebars expression which are very each to use and the handlebar templates are mostly similar to html structure.

Structure to define a handlebar template is :

<script id="template_Id" type="text/x-handlebars-template">
  content to placed in template
</script>

Suppose I have to update a ul after the ajax call whose response contains a JSON data.

var container = $("#ul_id");
var dataToUpdate = "<li><span>" + data.name + "</span>" + "<div>" + data.jobDescription"</div></li>";
container.append(dataToUpdate);

Suppose my JSON response contains an array of data :

[
  records :[
 {
    name : "Jatin",
    jobDescription : "Software Engineer"
 }
 { 
    name : "Nikhil",
    jobDescription : "Designer"
 }
]
]

We can perform the same task using the handlebars and its expression very easily.

To do this we have to simple put the "contentToUpdate" in handlebars template like this :

<script id="template_Id" type="text/x-handlebars-template">
{{#each records}}      // loop to itrate through all the records
<li>
  <span>{{name}}</span>     
  <div>{{jobDescription}}</div>
</li>
{{/each}}
</script>

Compile a template in JavaScript by using Handlebars.compile

var source   = $("#template_Id").html();   // source now have content of handlerbars template
var data = Handlebars.compile(source);     // source code is compiled using this
$("#ul_id").append(template(data));        // now the target DOm in this case "ul_id" is updated with the ajax response.

It makes the DOM nsertion task easy and less efforts are required in this.

For more info , you can refer to http://handlebarsjs.com/

 

Thanks

 

Shiv Kumar

How to sort data on currency basis in datatable

I had a requirement to sort the currency in ascending and descending order while using datatables. I have sort out this problem by using the following code in my javascript file.

Code in javascript file where you have used the datatable :

jQuery.extend(jQuery.fn.dataTableExt.oSort,{
    "currency-pre": function (a) {
        a = (a === "-") ? 0 : a.replace(/[^\d\-\.]/g, "");
        return parseFloat(a);
    },
    "currency-asc": function (a, b) {
        return a - b;
    },
    "currency-desc": function (a, b) {
        return b - a;
    }
});
 

The above code snippet works on the given column by calculating the difference between the value in the column.

During datatable initialisation we have to give the target column to apply sorting like this :

var oTable = $('#table_ID').dataTable({
		 'aoColumnDefs': [
		        { 'sType': 'currency', 'aTargets': [4] }     // In this case 5th column will be sorted on currency basis.
		     ],
	        "bProcessing": true,
	        "sPaginationType": "full_numbers",
		"bDestroy": true,
		"bStateSave" : true,     
     });	    
} 

In the similar way we have also some in-built functions to sort column data on different basis.

 

Thanks

Shiv Kumar

How to convert null values to empty String in Datatable

 In my case,I have a datatable named "Item" with structure like

|Style# | ItemNumber | Size | Color | Quantity |

Size for any item in above table may be null.So in this situation I have to display empty string for null "size" in datatable.

I have solved this problem using mRender property of datatable which provide the facility to process the data before rendering on view.

Here is the way to use mRender property in gsp:

<html>
<head>
<script>
$( document ).ready(function() {
 var oTable = $('#items').dataTable({
	        "aoColumnDefs":[{
		        "aTargets":[ 2 ]  // Here we have to give the target column for which value conversion(manipulation) takes place
		       ,"mRender": function( data, type, row ) {
			// "data" refers to the data of the cell
			// "type" refers data requested - this will be 'filter', 'display', 'type' or 'sort'
			// "row" referes to complete row data 
		          return (row[2] != "null")
		                    ? row[2]: "" ;
		      } 
		  }],
		    "sPaginationType": "full_numbers",
		    "sAjaxSource": '${createLink (action:'items',controller:'admin')}', // From this action data is provided in JSON format
		});
});
</script>
</head>
<body>
<div>
<table id="items" width="100%">
 <thead>
  <tr>
   <th>Style #</th>
   <th>ItemNumber</th>
   <th>Size</th>
   <th>Color</th>
   <th>Quantity</th>
  </tr> 
 </thead>
 <tbody></tbody>
</table>
</div>
<body>
</html>

Same thing can be done for multiple columns at a time.

Thanks.

Shiv Kumar

Partially update parent page on child page event

I had a requirement that from one of my view another page is opened in new tab.After submitting form(or some other action) on child view, I had to update some part of parent/opener view simultaneously. To do this there are several grails plugin available like "ICE push plugin" and "Grails Event Push Plugin".But I used native javascript to do this as it is easy to use and doesn't require any plugin and dependency to work.

 

Solution :

Suppose I have a div with id="childMessage" in parent page which I want to update on button click in child page.

 

So on child Page :

<html>
<body>
<button type="button" onclick="update('Hello')">Click Here</button>
<script>
function update(data){
  window.onbeforeunload = reflectChanges(data);
}
       
function reflectChanges(data) { 
//this condition checks if the parent view is open or not.If it is then update operation is performed.
if (window.opener != null && !window.opener.closed) 
        window.opener.updateParentView(data);
}
</script>
</body>
</html>

On parent View we have to define above called function("updateParentView") like this :

<html>
<head>
<script type="text/javascript" charset="utf-8">
function updateParentView(data){  // In this function you can do anything according to your requirement.
$('#childMessage').html(data)
}  
</script>
</head>
<body>
<div id="childMessage"></div>
</body>
</html>

Thanks

Shiv Kumar

 

Action is called twice in single request in grails
Sometime it happens that controller's action/method is called twice back to back.At first time it is called with the right parameter you have passed in request but at second time it is either called with no parameter or with undefined parameters.I have faced this problem many times but atlast find out the hack for this.
 
Root Cause :
1. The main cause of problem is that if rendered view[other than default(i.e testAction.gsp)] from action lets it is "TestAction" contains some empty source fields then action is called by browser again to fetch the file from src field url path.
 
2. Same problem can also be occurred due to href="" and href="#" in rendered view.
 
Let testAction.gsp contains :
<img src="" alt="User Image" width="500" height="500">   // Causes twice call to rendering action. 
 

Solution:

Never leave such empty src in rendered view.Always fill some suitable text in it except empty string and "#".
 
 
Thanks 
Shiv Kuamr

 

Implementing jQuery DynaTree in Grails Application
Introduction to DynaTree :
 
DynaTree is used to represent dynamic JavaScript tree view control along with the 
support of checkboxes,Keyboard, drag'n'drop and lazy loading.
 
Options to perform operation with dynatree:
1. title: "Node"      // used for name of the node.
2. children: null     // To initialize tree structure or children nodes from this object array.
3. keyboard: true,    // used to support keyboard navigation.
4. persist: false,    // used to persist expand-status to a cookie.
5. onActivate: null,  // event is used when a node is activated.
etc.
 
The root node object is obtained as follow:
        var rootNode = $("#tree").dynatree("getRoot");
        
Here is an example to initialize dynatree using JSON:
From the controller jsonResponse(JSONObject) rendered is :
  [ 
  {title: "Node 1"},
  {title: "Node 2", isFolder: true, //isFolder : true is used to represent node as Folder
    children: [
               {title: "Child-node 2.1"},
               {title: "Child-node 2.2"}
              ]
   },
   {title: "Node 3"}
]
                
The Dynatree is initialized during the onload event of the document. 
In jQuery this is usually done by passing a function to $(document) :
                         
 

// div attached with dynatree

Sometimes our requirement is to reload the dynatree after some event and reflect the changes immediately.
In that case we have to destroy and re-initialize the dynatree as follow:
$("#tree").dynatree("destroy");
$("#tree").dynatree({
     […]
});
                                
Thanks
Shiv Kumar

 

Banner

Don't just hire talent,
But build your dream team

Our experience in providing the best talents in accordance with diverse industry demands sets us apart from the rest. Hire a dedicated team of experts to build & scale your project, achieve delivery excellence, and maximize your returns. Rest assured, we will help you start and launch your project, your way – with full trust and transparency!