<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Techie-Gyan &#187; interview</title>
	<atom:link href="http://www.techiegyan.com/category/interview/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.techiegyan.com</link>
	<description></description>
	<lastBuildDate>Fri, 25 Nov 2011 05:37:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>10x Performance Improvements &#8211; MySql</title>
		<link>http://www.techiegyan.com/2011/04/10/10x-performance-improvements-mysql/</link>
		<comments>http://www.techiegyan.com/2011/04/10/10x-performance-improvements-mysql/#comments</comments>
		<pubDate>Sun, 10 Apr 2011 06:55:55 +0000</pubDate>
		<dc:creator>Aditya</dc:creator>
				<category><![CDATA[basics]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[problem]]></category>
		<category><![CDATA[tool]]></category>
		<category><![CDATA[troubleshooting]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://www.techiegyan.com/?p=1718</guid>
		<description><![CDATA[10x Performance Improvements View more presentations from Ronald Bradford]]></description>
			<content:encoded><![CDATA[<div style="width:425px" id="__ss_3776503"> <strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/ronaldbradford/10x-performance-improvements" title="10x Performance Improvements">10x Performance Improvements</a></strong> <iframe src="http://www.slideshare.net/slideshow/embed_code/3776503" width="425" height="355" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
<div style="padding:5px 0 12px"> View more <a href="http://www.slideshare.net/">presentations</a> from <a href="http://www.slideshare.net/ronaldbradford">Ronald Bradford</a> </div>
</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.techiegyan.com/2011/04/10/10x-performance-improvements-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Application Events &#8211; Spring framework</title>
		<link>http://www.techiegyan.com/2011/04/08/using-application-events-spring-framework/</link>
		<comments>http://www.techiegyan.com/2011/04/08/using-application-events-spring-framework/#comments</comments>
		<pubDate>Fri, 08 Apr 2011 18:25:31 +0000</pubDate>
		<dc:creator>Aditya</dc:creator>
				<category><![CDATA[basics]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[j2ee]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[tool]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.techiegyan.com/?p=1700</guid>
		<description><![CDATA[Spring Application contexts support a simple form of event publishing and subscription. This event mechanism can not be compared with other message queues and JMS like systems but can be useful for certain use cases like following: 1. Sending async email: While executing a user registration flow, system may want to send and email and [...]]]></description>
			<content:encoded><![CDATA[<p>Spring Application contexts support a simple form of event publishing and subscription. This event mechanism can not be compared with other message queues and JMS like systems but can be useful for certain use cases like following:</p>
<p>1. Sending async email: While executing a user registration flow, system may want to send and email and sending this email should not stop the registration process to complete so it requires an asynchronous way to do that. Application Event can help in this situation.<br />
2. Auditing application actions: Another use case where you do not want to stop the execution of main thread and want to audit activities in an async way.</p>
<p>I am trying to list classes you will need to use your own Application Events and handle them:</p>
<p><strong>a) Your custom event :</strong> UserEvent, AbstractEvent. AbstractEvent is an abstract class which can be extended based on requirement. As you can see in the code, Event Id is used to call its super class which is ApplicationEvent class of Spring framework. Another variable eventContext is used to carry context parameters to the handler of event(Check different constructors of UserEvent). </p>
<pre class="brush: java; title: ; notranslate">
public class UserEvent extends AbstractEvent {
        public UserEvent(String eventId, Object eventContext) {
		super(eventId, eventContext);
	}
        public UserEvent(String eventId, User user, String tPassword) {
		super(eventId, new HashMap());
		Map&lt;String, Object&gt; params = (Map&lt;String, Object&gt;) this.getEventContext();
		params.put(&quot;user&quot;, user);
		params.put(&quot;tPassword&quot;, tPassword);
	}
}

public abstract class AbstractEvent extends ApplicationEvent {

        protected String eventId;
	protected Object eventContext;

	public AbstractEvent(String eventId, Object eventContext) {
		super(eventId);
		this.eventId = eventId;
		this.eventContext = eventContext;
	}

	public String getEventId() {
		return eventId;
	}

	public Object getEventContext() {
		return eventContext;
	}

	public Object getContextParams(String paramName){
		if(! (eventContext!= null &amp;&amp; eventContext instanceof Map)){
			return null;
		}
		return ((Map)eventContext).get(paramName);
	}
}
</pre>
<p><strong>b) Publish your custom event :</strong> You will like to publish your custom event generally from the flow of execution e.g. in this case UserService or RegistrationService. Spring gives a facility to make your class able to publish Application events by implementing ApplicationEventPublisherAware interface. </p>
<p>By implementing ApplicationEventPublisherAware, you need to implement setApplicationEventPublisher function and Spring will automatically call set function to inject ApplicationEventPublisher instance. This instance can be used for publishing the event.</p>
<p>This basically invokes all listeners waiting for this kind of Event in a separate thread to make it async.</p>
<pre class="brush: java; title: ; notranslate">
public class RegistrationService implements ApplicationEventPublisherAware{
        protected ApplicationEventPublisher applicationEventPublisher;
        public static final String Event_RegisterUser = &quot;registerUser&quot;;

        public void registerUser(User u) {
                .......
                .......
                try {
				applicationEventPublisher.publishEvent(new UserEvent(Event_RegisterUser, u));
			}
			catch(Exception e) {
				logger.error(e.getMessage(), e);
			}
                ..........
                ..........
        }
        public void setApplicationEventPublisher(
			ApplicationEventPublisher applicationEventPublisher) {
		this.applicationEventPublisher = applicationEventPublisher;
	}
}
</pre>
<p><strong>c) Handle your custom event :</strong> An event listener would be required to handle the event published by the service and handle it. By implementing ApplicationListener, class has to implement onApplicationEvent function where it gets the Event object with eventId and eventContext.</p>
<pre class="brush: java; title: ; notranslate">
public class UserEventListener implements ApplicationListener&lt;UserEvent&gt; {
        public void onApplicationEvent(UserEvent event) {
                String eventId = event.getEventId();
                User user = (User) event.getContextParams(&quot;user&quot;);
                String tempPassword = (String) event.getContextParams(&quot;tPassword&quot;);
                //Do your handling like send an email to newly added User
        }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.techiegyan.com/2011/04/08/using-application-events-spring-framework/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>i18n framework for HTML pages &#8211; jQuery</title>
		<link>http://www.techiegyan.com/2010/05/23/i18n-framework-for-html-pages-jquery/</link>
		<comments>http://www.techiegyan.com/2010/05/23/i18n-framework-for-html-pages-jquery/#comments</comments>
		<pubDate>Sun, 23 May 2010 13:26:15 +0000</pubDate>
		<dc:creator>Aditya</dc:creator>
				<category><![CDATA[addon]]></category>
		<category><![CDATA[basics]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://www.techiegyan.com/?p=1350</guid>
		<description><![CDATA[The problem is quite simple. You have HTML pages as your front end and you are not using any server side scripting to display your pages i.e. you are not using jsp or php or servlets to get HTML content. Application is HTML pages and javascript which is using Ajax to talk to back end [...]]]></description>
			<content:encoded><![CDATA[<p>The problem is quite simple. You have HTML pages as your front end and you are not using any server side scripting to display your pages i.e. you are not using jsp or php or servlets to get HTML content. Application is HTML pages and javascript which is using Ajax to talk to back end and getting and sending data to server according to user actions and on certain events and Multi-lingual interface is the requirement i.e. require i18n for front-end. </p>
<p>As most of the static content is coded in your HTML pages, one way to do this is figuring out user&#8217;s context and display him specific HTML page accordingly. That means maintaining different HTML pages for each language you want to support for your web application. This approach is little difficult because generally we  get changes very frequently on HTML pages so for maintaining it you also need to make changes in rest of HTML files for other languages. This requires testing for all languages as HTML files will also have some functional code with it and it may break for some languages while making changes. </p>
<p>We tried to solve this problem in slightly different manner. We tried to separate the display strings from HTML controls and tried to replace them with translations by figuring out the user&#8217;s language dynamically. In this way we have only one HTML page to maintain but we do have multiple resource files for language translations which keeps all static display strings translated for a certain language. As we need to do it on client side these resource files are basically Java script files having JSON objects with key and values. Keys are used to retrieve the display string value and replaced on HTML page. Following are the steps while loading the HTML page: </p>
<ul>
<li>Very first included javascript after library javascripts is i18n.js on the page.</li>
<li>i18n.js tries to load language from a cookie value, if it is not set then tries to get language setting of user from server using an Ajax request.</li>
<li>Once language is known, it downloads the translated javascript file from server e.g. if user language is French i.e. fr, it will load javascript file from http://server/contextpath/js/lang/&#8217; + _language + &#8216;/&#8217; +  getPageName() + &#8216;.js&#8217;. </li>
<li>All translated Java script files should reside in respective language folders and their names should match the page name i.e. the HTML page name so that a generic code can be written to retrieve correct translated Java script file.</li>
<li>In case there are some common strings used for all pages in web application, a common file _common.js can be created per language and can be downloaded along with page specific js.</li>
<li>Once you have all the resource ready. replace all display string placeholders from HTML page so that page can be viewed in user&#8217;s language.</li>
<li>Any other display message like Success and warning messages or any other content which is not the part of HTML page but getting generated dynamically using Java script should use the translated file to retrieve values against keys and display the translated text. </li>
</ul>
<p>Question is how to replace display strings in HTML page. Here is the way you should write your HTML page so that you can replace strings according to the language. </p>
<ul>
<li>Use span tag to enclose all Strings used on page as labels, texts etc. Have an id of each span and this id will be used to replace the language text for that span tag. </li>
<li>For controls like Select boxes and input tags use below code to replace texts</li>
<li>Use following geti18N() function to get translated text for the page while making dynamic strings</li>
</ul>
<pre class="brush: xml; title: ; notranslate">
&lt;span id=&quot;mylabel.id&quot; class=&quot;i18n&quot;&gt;&lt;/span&gt;
&lt;input type=&quot;button&quot; id=&quot;form.submit.button&quot; name=&quot;form.submit.button&quot; class=&quot;i18n&quot;&gt;&lt;/input&gt;
&lt;select id=&quot;someSelect&quot; class=&quot;i18n&quot;&gt;
    &lt;option id=&quot;select.option1&quot; value=&quot;1&quot;&gt;&lt;/option&gt;
    &lt;option id=&quot;select.option2&quot; value=&quot;2&quot;&gt;&lt;/option&gt;
&lt;/select&gt;
</pre>
<pre class="brush: jscript; title: ; notranslate">
function translate() {
	if (_translator) {
		$(&quot;span.i18n&quot;).each( function (i) {
			$(this).html(_translator[this.id]);
		});
		$(&quot;input.i18n&quot;).each( function (i) {
			$(this).val(_translator[this.id]);
		});
		$(&quot;select.i18n&quot;).each( function (i) {
			var opts = $(this)[0].options;
			if (opts)
				for(var i=0; i&lt;opts.length; i++) {
					opts[i].text = _translator[opts[i].id];
				}
		});
	}
}
</pre>
<p>You can see in the function above that class is used to for replacing translations in HTML file. Here _translator is the JSON object which is already loaded with translations as described earlier. Here is the example of translation file </p>
<pre class="brush: jscript; title: ; notranslate">
{
  &quot;key&quot;:&quot;value&quot;,
  &quot;mylabel.id&quot;:&quot;My Label&quot;,
  &quot;form.submit.button&quot;:&quot;Submit&quot;,
  &quot;select.option1&quot;:&quot;Option 1&quot;,
  &quot;select.option2&quot;:&quot;Option 2&quot;
}
</pre>
<p>For all dynamic Strings which are required while your getting success or error from server, you can use following function to get translated string from _translator object</p>
<pre class="brush: jscript; title: ; notranslate">
function getI18N(key) {
        var s = '';
	if(_translator[key]) {
		s = _translator[key];
	}
       return s;
}
</pre>
<p>Please post comments if you find it can be improved. Thanks</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techiegyan.com/2010/05/23/i18n-framework-for-html-pages-jquery/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Bubble Sort and Binary Search &#8211; Java</title>
		<link>http://www.techiegyan.com/2010/05/03/bubble-sort-and-binary-search-java/</link>
		<comments>http://www.techiegyan.com/2010/05/03/bubble-sort-and-binary-search-java/#comments</comments>
		<pubDate>Mon, 03 May 2010 17:41:34 +0000</pubDate>
		<dc:creator>Aditya</dc:creator>
				<category><![CDATA[basics]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://www.techiegyan.com/?p=1351</guid>
		<description><![CDATA[Just like that i was again brushing my core Java few days back and wrote bubble sort and binary search code in Java. Here i am sharing the code, please post your comments in case this can be further optimized: Bubble Sort: And once we have sorted array with us we can apply binary search [...]]]></description>
			<content:encoded><![CDATA[<p>Just like that i was again brushing my core Java few days back and wrote bubble sort and binary search code in Java. Here i am sharing the code, please post your comments in case this can be further optimized:</p>
<p>Bubble Sort:</p>
<pre class="brush: java; title: ; notranslate">
public static void main(String args[]) throws Exception{
    int[] newArr = new int[]{21,33,2,56,34,11,7,35,27,87,233,108,26,12,6,90,365,243,666,67};
	int n = newArr.length;
		boolean doMore = true;
		while (doMore){
			n--;
			doMore = false;
			for (int i=0; i &lt; n; i++) {
				if (newArr[i] &gt; newArr[i+1]) {
					// exchange elements
					int temp = newArr[i];  newArr[i] = newArr[i+1];  newArr[i+1] = temp;
					doMore = true;
				}
			}
		}
		printArray(newArr);
}

public static void printArray(int[] arr){
		System.out.println(&quot;===========START=============&quot;);
		for (int n=0; n &lt; arr.length;n++){
			System.out.println(&quot;Arr[&quot; + (n) + &quot;] :&quot; + arr[n]);
		}
		System.out.println(&quot;============END==============&quot;);
	}
</pre>
<p>And once we have sorted array with us we can apply binary search on it, Let say we want to search 11, here is the program which will do the binary searching :</p>
<pre class="brush: java; title: ; notranslate">
int x = 11;
int low = 0;
int high = newArr.length - 1;
int mid;
boolean breakLoop = true;
while(low &lt;= high &amp;&amp; breakLoop){
    mid = (low + high) / 2;
    if( newArr[mid] &lt; x )
        low = mid + 1;
    else if(newArr[mid]&gt; x )
        high = mid - 1;
    else{
        System.out.println(mid);
        breakLoop = false;
    }
}

System.out.println(&quot;NOT_FOUND&quot;);
</pre>
<p>To further optimize this we can remove one comparison and do it like this:</p>
<pre class="brush: java; title: ; notranslate">
int x = 11;
int low = 0;
int high = newArr.length - 1;
int mid;
while(low &lt; high){
    mid = low + (high -low) / 2;
    if( newArr[mid] &lt; x )
        low = mid + 1;
    else
        high = mid ;
}
if ((low &lt; newArr.length - 1) &amp;&amp; (newArr[low] == x))
           System.out.println(low);
       else
           System.out.println(&quot;NOT_FOUND&quot;);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.techiegyan.com/2010/05/03/bubble-sort-and-binary-search-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Convert timestamp between timezones : CONVERT_TZ() &#8211; MySql</title>
		<link>http://www.techiegyan.com/2009/11/05/convert-timestamp-between-timezones-convert_tz-mysql/</link>
		<comments>http://www.techiegyan.com/2009/11/05/convert-timestamp-between-timezones-convert_tz-mysql/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 12:46:39 +0000</pubDate>
		<dc:creator>Aditya</dc:creator>
				<category><![CDATA[basics]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[converter]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.techiegyan.com/?p=1114</guid>
		<description><![CDATA[Any application targeted for people in different countries or even in different cities requires to handle timezones for the users for application. One main requirement is to show data against user&#8217;s timezone e.g. creation date &#038; time of any message to the user. Many applications solve this by asking users their timezone at the time [...]]]></description>
			<content:encoded><![CDATA[<p>Any application targeted for people in different countries or even in different cities requires to handle timezones for the users for application. One main requirement is to show data against user&#8217;s timezone e.g. creation date &#038; time of any message to the user. </p>
<p>Many applications solve this by asking users their timezone at the time of registration and display part is more or less solved in this way. Most of the time these applications ask about your country and figure out by themselves about the timezone but there are some countries which are having multiple timezones and in that case time zone offset is used in GMT+- hh:mm format. </p>
<p>Problem arises when you also need to support the <a href="http://en.wikipedia.org/wiki/Daylight_saving_time">day light saving time</a>.  And it becomes more important when you need to do more than just displaying few values to the user like contacting certain people of certain timezone in certain time range. </p>
<p>There are ways to do support all different timezones with day light saving time and actually supported in many languages/platforms like Java and .Net. This is implemented using <a href="http://en.wikipedia.org/wiki/Zoneinfo">Zone info Database or Olsen Database</a> </p>
<p>Mysql also provides this functionality but not in very standard way and probably that is the reason for non-popularity of this function. Another reason is that it requires some extra arrangements to use this function i.e. populating some of the timezone tables in mysql schema. </p>
<p>Here is the usage of this function:</p>
<pre class="sql"><span style="color: #cc66cc;">1</span>. <span style="color: #993333; font-weight: bold;">SELECT</span> CONVERT_TZ<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'2004-01-01 12:00:00'</span>,<span style="color: #ff0000;">'GMT'</span>,<span style="color: #ff0000;">'MET'</span><span style="color: #66cc66;">&#41;</span>;</pre>
<pre class="sql"><span style="color: #cc66cc;">2</span>. <span style="color: #993333; font-weight: bold;">SELECT</span> CONVERT_TZ<span style="color: #66cc66;">&#40;</span>UTC_TIMESTAMP<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>,<span style="color: #ff0000;">'GMT'</span>,<span style="color: #ff0000;">'MST'</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>Before using this function, just make sure that you have populated some of the timezone tables in mysql schema. </p>
<p>a. time_zone<br />
b. time_zone_leap_second<br />
c. time_zone_name<br />
d. time_zone_transition<br />
e. time_zone_transition_type</p>
<p>You can get these tables at <a href="http://dev.mysql.com/downloads/timezones.html">http://dev.mysql.com/downloads/timezones.html</a> and see more information about MySql timezone support at <a href="http://dev.mysql.com/doc/refman/5.0/en/time-zone-support.html">http://dev.mysql.com/doc/refman/5.0/en/time-zone-support.html</a></p>
<p>Here is an example of this function with the use case, suppose you want to send an e-mail to the user of your application on his birthday and users belong to multiple timezones having day light saving time. If you have following information , you can actually greet your user for his birthday as soon as date changes to his B&#8217;Day. </p>
<p>You need to run a thread every 5 minutes to check if any of your users have birthday. </p>
<p>a. user&#8217;s timezone information<br />
b. user&#8217;s birth date.</p>
<pre class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span> DATE<span style="color: #66cc66;">&#40;</span>CONVERT_TZ<span style="color: #66cc66;">&#40;</span>UTC_TIMESTAMP<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>,<span style="color: #ff0000;">'GMT'</span>,<span style="color: #ff0000;">'users-time-zone'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>if result is equal to the user&#8217;s birth date, you can send him B&#8217;Day wishes. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.techiegyan.com/2009/11/05/convert-timestamp-between-timezones-convert_tz-mysql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

