<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Terence hacks</title>
	<atom:link href="http://terencehacks.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://terencehacks.wordpress.com</link>
	<description>Life as a wannabe programmer</description>
	<lastBuildDate>Sun, 01 Jan 2012 05:03:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='terencehacks.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Terence hacks</title>
		<link>http://terencehacks.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://terencehacks.wordpress.com/osd.xml" title="Terence hacks" />
	<atom:link rel='hub' href='http://terencehacks.wordpress.com/?pushpress=hub'/>
		<item>
		<title>2011 in review</title>
		<link>http://terencehacks.wordpress.com/2012/01/01/2011-in-review/</link>
		<comments>http://terencehacks.wordpress.com/2012/01/01/2011-in-review/#comments</comments>
		<pubDate>Sun, 01 Jan 2012 05:03:52 +0000</pubDate>
		<dc:creator>Terence Ponce</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://terencehacks.wordpress.com/?p=303</guid>
		<description><![CDATA[The WordPress.com stats helper monkeys prepared a 2011 annual report for this blog. Here&#8217;s an excerpt: A San Francisco cable car holds 60 people. This blog was viewed about 1,700 times in 2011. If it were a cable car, it would take about 28 trips to carry that many people. Click here to see the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terencehacks.wordpress.com&amp;blog=17292332&amp;post=303&amp;subd=terencehacks&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The WordPress.com stats helper monkeys prepared a 2011 annual report for this blog.</p>
<div style="background:url('/wp-content/mu-plugins/annual-reports/img/emailteaser.jpg') no-repeat center center;height:300px;"></div>
<p>Here&#8217;s an excerpt:</p>
</p>
<blockquote><p>A San Francisco cable car holds 60 people.  This blog was viewed about <strong>1,700</strong> times in 2011.  If it were a cable car, it would take about 28 trips to carry that many people.</p></blockquote>
<p><a href="/2011/annual-report/">Click here to see the complete report.</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terencehacks.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terencehacks.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terencehacks.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terencehacks.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/terencehacks.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/terencehacks.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/terencehacks.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/terencehacks.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terencehacks.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terencehacks.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terencehacks.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terencehacks.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terencehacks.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terencehacks.wordpress.com/303/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terencehacks.wordpress.com&amp;blog=17292332&amp;post=303&amp;subd=terencehacks&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://terencehacks.wordpress.com/2012/01/01/2011-in-review/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fdd669d69fc537eaadf826104075af96?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">eeyorexd</media:title>
		</media:content>
	</item>
		<item>
		<title>Facebook Image Publisher Tracker</title>
		<link>http://terencehacks.wordpress.com/2011/08/25/facebook-image-publisher-tracker/</link>
		<comments>http://terencehacks.wordpress.com/2011/08/25/facebook-image-publisher-tracker/#comments</comments>
		<pubDate>Thu, 25 Aug 2011 08:57:21 +0000</pubDate>
		<dc:creator>Terence Ponce</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[google-app-engine]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://terencehacks.wordpress.com/?p=296</guid>
		<description><![CDATA[Hi there! It&#8217;s been a while since I made an article. I got pretty busy and kinda lazy over the past few months so I completely forgot that I have a blog going on. So the last article that I made was about using Google App Engine. I stopped that project because it got pretty [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terencehacks.wordpress.com&amp;blog=17292332&amp;post=296&amp;subd=terencehacks&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hi there! It&#8217;s been a while since I made an article. I got pretty busy and kinda lazy over the past few months so I completely forgot that I have a blog going on.</p>
<p>So the last article that I made was about <a title="A noob’s attempt on Google App Engine using Java part 2" href="http://terencehacks.wordpress.com/2011/03/26/a-noobs-attempt-on-google-app-engine-using-java-part-2/">using Google App Engine</a>. I stopped that project because it got pretty complicated the moment I tried using the Datastore. So now, I decided to build something simple, something that doesn&#8217;t require a database to run.</p>
<p>Recently, I found out that you can trace a Facebook image back to its publisher based on the image&#8217;s URL. Obviously, the image should come from Facebook&#8217;s content deliver network or CDN as people usually describe it. It&#8217;s no secret, really. I found out how immediately after my first Google search. I&#8217;m pretty sure revealing how is kind of unethical and on a moral gray area, but hey, the information is already out there. It&#8217;s not like I can make it any worse by posting it here. Besides, I trust that any reader of this blog (if there&#8217;s really anyone reading this) would be responsible enough not to use it for evil.</p>
<p>Let&#8217;s say, for example, I posted this <a href="https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-snc6/63442_472293192932_636557932_5746749_150701_n.jpg" title="My Facebook profile picture">Facebook picture of me</a> somewhere on the Internet. At the 3rd level of the image link, there&#8217;s a set of numbers separated by an underscore (63442_472293192932_636557932_5746749_150701_n.jpg). These numbers are actually time stamps, but among those time stamps is the profile ID of the publisher of the image. What you want to do is to get the 3rd one (636557932), which is the profile ID, and type the link in your browser (http://facebook.com/profile.php?id=&lt;insert profile ID here&gt;/). That wasn&#8217;t so hard right? I&#8217;m pretty sure anyone persistent enough would be able to figure it out on their own.</p>
<p>After I learned how to do that, I thought it would be fun to make a simple tool to automate the process since I&#8217;m too lazy to type it in the address bar every time. Then I realized that it&#8217;s pretty easy to make it into a Google App Engine app, so I did.</p>
<p>Since I didn&#8217;t really want this article to be lengthy and cause people to get bored, I decided to skip with the code discussion and just get on with showing you the actual software itself. The tool can be found <a href="http://aquamodena.appspot.com" title="Facebook Image Publisher Tracker" target="_blank">here</a> and the source code for the whole project can be found <a href="https://bitbucket.org/terenceponce/facebook-image-publisher-tracker/src" title="Source" target="_blank">here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terencehacks.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terencehacks.wordpress.com/296/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terencehacks.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terencehacks.wordpress.com/296/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/terencehacks.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/terencehacks.wordpress.com/296/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/terencehacks.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/terencehacks.wordpress.com/296/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terencehacks.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terencehacks.wordpress.com/296/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terencehacks.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terencehacks.wordpress.com/296/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terencehacks.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terencehacks.wordpress.com/296/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terencehacks.wordpress.com&amp;blog=17292332&amp;post=296&amp;subd=terencehacks&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://terencehacks.wordpress.com/2011/08/25/facebook-image-publisher-tracker/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fdd669d69fc537eaadf826104075af96?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">eeyorexd</media:title>
		</media:content>
	</item>
		<item>
		<title>A noob&#8217;s attempt on Google App Engine using Java part 2</title>
		<link>http://terencehacks.wordpress.com/2011/03/26/a-noobs-attempt-on-google-app-engine-using-java-part-2/</link>
		<comments>http://terencehacks.wordpress.com/2011/03/26/a-noobs-attempt-on-google-app-engine-using-java-part-2/#comments</comments>
		<pubDate>Sat, 26 Mar 2011 03:11:06 +0000</pubDate>
		<dc:creator>Terence Ponce</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[google-app-engine]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://terencehacks.wordpress.com/?p=270</guid>
		<description><![CDATA[In my last entry, I modified my Maven project so it can be deployed to Google App Engine. This time, I&#8217;m going to work on my actual application so I can finally have something to show to other people. Right now, my Spring web application works fine on Google App Engine. Let me go over [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terencehacks.wordpress.com&amp;blog=17292332&amp;post=270&amp;subd=terencehacks&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In my <a href="/2011/03/12/a-noobs-attempt-on-google-app-engine-using-java/">last entry</a>, I modified my Maven project so it can be deployed to Google App Engine. This time, I&#8217;m going to work on my actual application so I can finally have something to show to other people.</p>
<p>Right now, my Spring web application works fine on Google App Engine. Let me go over the code that I have so far. I first made a bean called Manufacturer:</p>
<p><pre class="brush: java;">
package com.jenanderic.domain;

public class Manufacturer {
	private int id;
	private String name;
	private String address;
	
	// Constructors
	
	// Getters and Setters
}
</pre></p>
<p>Next, I made an implementation class of a service interface for the bean that I just made:</p>
<p><pre class="brush: java;">
package com.jenanderic.service;

// Imports

public class ManufacturerServiceImpl implements ManufacturerService {
	
	@Autowired
	private List&lt;Manufacturer&gt; manufacturers;
	
	// Getters and Setters
}
</pre></p>
<p>I made it autowired so Spring will do the injection for me. I then made the controller to handle the requests:</p>
<p><pre class="brush: java;">
package com.jenanderic.web;

// Imports

@Controller
@RequestMapping(&quot;/manufacturer&quot;)
public class ManufacturerController {

	private ManufacturerService manufacturerService;
	
	@Autowired
	public void setManufacturerService(ManufacturerService manufacturerService) {
		this.manufacturerService = manufacturerService;
	}
	
	@RequestMapping(method = RequestMethod.GET)
	public String getManufacturerList(Model model) {
		model.addAttribute(&quot;manufacturers&quot;, manufacturerService.getAllManufacturers());
		return &quot;manufacturer_list&quot;;
	}
}
</pre></p>
<p>Then, I made the template page since the controller returns a page called manufacturer_list.jsp:</p>
<p><pre class="brush: xml;">
&lt;%@ include file=&quot;/WEB-INF/jsp/include.jsp&quot; %&gt;

&lt;html&gt;
	&lt;head&gt;
		&lt;title&gt;&lt;spring:message code=&quot;manufacturer.title&quot; /&gt;&lt;/title&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;h2&gt;&lt;spring:message code=&quot;manufacturer.heading&quot; /&gt;&lt;/h2&gt;
		&lt;c:forEach items=&quot;${manufacturers}&quot; var=&quot;manufacturer&quot;&gt;
			&lt;p&gt;&lt;c:out value=&quot;${manufacturer.id}&quot;/&gt; - 
			&lt;c:out value=&quot;${manufacturer.name}&quot;/&gt; - &lt;c:out value=&quot;${manufacturer.address}&quot;/&gt;&lt;/p&gt;
		&lt;/c:forEach&gt;
	&lt;/body&gt;
&lt;/html&gt;
</pre></p>
<p>Now that everything&#8217;s almost done, it&#8217;s time to make an actual bean to inject into the controller. I placed it in the Application&#8217;s context:</p>
<p><pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
	xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
	xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
	xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd&quot;&gt;
	
	&lt;bean id=&quot;manufacturerService&quot; class=&quot;com.jenanderic.service.ManufacturerServiceImpl&quot;&gt;
		&lt;property name=&quot;manufacturers&quot;&gt;
			&lt;list&gt;
				&lt;ref local=&quot;manufacturer1&quot; /&gt;
				&lt;ref local=&quot;manufacturer2&quot; /&gt;
				&lt;ref local=&quot;manufacturer3&quot; /&gt;
			&lt;/list&gt;
		&lt;/property&gt;
	&lt;/bean&gt;
	
	&lt;bean id=&quot;manufacturer1&quot; class=&quot;com.jenanderic.domain.Manufacturer&quot;&gt;
		&lt;property name=&quot;id&quot; value=&quot;1&quot; /&gt;
		&lt;property name=&quot;name&quot; value=&quot;Manufacturer #1&quot; /&gt;
		&lt;property name=&quot;address&quot; value=&quot;Address1&quot; /&gt;
	&lt;/bean&gt;
	
	&lt;bean id=&quot;manufacturer2&quot; class=&quot;com.jenanderic.domain.Manufacturer&quot;&gt;
		&lt;property name=&quot;id&quot; value=&quot;2&quot; /&gt;
		&lt;property name=&quot;name&quot; value=&quot;Manufacturer #2&quot; /&gt;
		&lt;property name=&quot;address&quot; value=&quot;Address2&quot; /&gt;
	&lt;/bean&gt;
	
	&lt;bean id=&quot;manufacturer3&quot; class=&quot;com.jenanderic.domain.Manufacturer&quot;&gt;
		&lt;property name=&quot;id&quot; value=&quot;3&quot; /&gt;
		&lt;property name=&quot;name&quot; value=&quot;Manufacturer #3&quot; /&gt;
		&lt;property name=&quot;address&quot; value=&quot;Address3&quot; /&gt;
	&lt;/bean&gt;
	
&lt;/beans&gt;
</pre></p>
<p>Since I have it autowired, I don&#8217;t need to explicitly inject it into the controller. Pretty cool, right?</p>
<p>Anyway, if I build and run the web app in GAE&#8217;s development server, it will run smoothly with no problems, so I&#8217;m sure that my web app runs 100% fine at this point.</p>
<p>Of course, I need to add persistence in the application since web apps are mostly about persistence. This is where it gets complicated. Even though I have plenty of experience working with database-driven applications, Google App Engine makes it seem otherwise.</p>
<p>Google App Engine works in a manner very different from traditional platforms. The most noticeable is the way it handles persistence; it doesn&#8217;t support relational databases. Google App Engine uses a non-relational database called the datastore. At the moment, I don&#8217;t have a full grasp of the pros and cons of not being able to use a relational database, but I&#8217;m pretty sure it has a great impact with the way you will build your application. Working with a non-relational database means that you have to manually maintain indexes with hand-written code. Aside from that, you have to manually write code for merging the results of multiple queries. Bummer.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terencehacks.wordpress.com/270/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terencehacks.wordpress.com/270/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terencehacks.wordpress.com/270/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terencehacks.wordpress.com/270/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/terencehacks.wordpress.com/270/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/terencehacks.wordpress.com/270/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/terencehacks.wordpress.com/270/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/terencehacks.wordpress.com/270/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terencehacks.wordpress.com/270/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terencehacks.wordpress.com/270/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terencehacks.wordpress.com/270/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terencehacks.wordpress.com/270/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terencehacks.wordpress.com/270/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terencehacks.wordpress.com/270/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terencehacks.wordpress.com&amp;blog=17292332&amp;post=270&amp;subd=terencehacks&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://terencehacks.wordpress.com/2011/03/26/a-noobs-attempt-on-google-app-engine-using-java-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fdd669d69fc537eaadf826104075af96?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">eeyorexd</media:title>
		</media:content>
	</item>
		<item>
		<title>A noob&#8217;s attempt on Google App Engine using Java</title>
		<link>http://terencehacks.wordpress.com/2011/03/12/a-noobs-attempt-on-google-app-engine-using-java/</link>
		<comments>http://terencehacks.wordpress.com/2011/03/12/a-noobs-attempt-on-google-app-engine-using-java/#comments</comments>
		<pubDate>Sat, 12 Mar 2011 04:29:53 +0000</pubDate>
		<dc:creator>Terence Ponce</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[google-app-engine]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://terencehacks.wordpress.com/?p=243</guid>
		<description><![CDATA[Since I solved most of the problems that I encountered with Spring MVC, I decided to take it up a notch by using Google App Engine. If you haven&#8217;t heard about GAE, it&#8217;s basically a host for web applications that are built in Java or Python. Also, it&#8217;s an engine for building web applications (as [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terencehacks.wordpress.com&amp;blog=17292332&amp;post=243&amp;subd=terencehacks&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Since I solved most of the problems that I encountered with Spring MVC, I decided to take it up a notch by using <a href="http://code.google.com/appengine/">Google App Engine</a>. If you haven&#8217;t heard about GAE, it&#8217;s basically a host for web applications that are built in Java or Python. Also, it&#8217;s an engine for building web applications (as you can see from the name). One of the benefits that you get from GAE is that your web application gets to be hosted by the same servers that power Google. Best of all, it&#8217;s free! Well, not really. It&#8217;s free when you avail the basic package, which already has everything a startup company needs anyway. You&#8217;ll only need to pay if you need more hosting power or other services like custom domains for your web application.</p>
<p>To be honest, this isn&#8217;t my first attempt on GAE. A few months ago, I made a web application using <a href="http://www.python.org/">Python</a> and <a href="http://www.allbuttonspressed.com/projects/django-nonrel">Django-nonrel</a>, a modification of the Django framework for Google App Engine. It was all good but I stopped the development after a few weeks because I lack knowledge, references, and guidance at that time.</p>
<p>With that said, this is my second attempt at using GAE. I decided to use Java and Spring 3.0 this time since I&#8217;ve been focusing a lot on Java lately. Hopefully, I&#8217;ll be able to do it successfully this time. </p>
<p>Of course, the first thing I did was read through the <a href="http://code.google.com/appengine/docs/java/gettingstarted/">documentation</a>. Unfortunately, I didn&#8217;t really get much from the documentation because it didn&#8217;t have enough information about the tools that I&#8217;ll use for the project like Maven 2, and Spring 3.0. I didn&#8217;t use Ant for my project because I found Maven to be more convenient to use. I already have an existing project built with Maven 2 and Spring 3.0 that I want to migrate to GAE, so I had to find a way to get Maven to do all the build, dependency management, and deployment task for me. Luckily, I found a <a href="http://code.google.com/p/maven-gae-plugin/">plugin for Maven</a> that already does what I need.</p>
<p>To start, let me show you the contents of the pom.xml of my existing Maven project:</p>
<p><pre class="brush: xml;">
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
		xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;&gt;
	&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
	&lt;groupId&gt;com.jenanderic&lt;/groupId&gt;
	&lt;artifactId&gt;jenanderic&lt;/artifactId&gt;
	&lt;packaging&gt;war&lt;/packaging&gt;
	&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
	
	&lt;name&gt;Jen&amp;amp;Eric Drugstore&lt;/name&gt;
	&lt;description&gt;POS System for Jen&amp;amp;Eric Drugstore&lt;/description&gt;
	
	&lt;dependencies&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;junit&lt;/groupId&gt;
			&lt;artifactId&gt;junit&lt;/artifactId&gt;
			&lt;version&gt;3.8.1&lt;/version&gt;
		&lt;/dependency&gt;
		
		&lt;dependency&gt;
			&lt;groupId&gt;commons-logging&lt;/groupId&gt;
			&lt;artifactId&gt;commons-logging&lt;/artifactId&gt;
			&lt;version&gt;1.1.1&lt;/version&gt;
		&lt;/dependency&gt;
		
		&lt;dependency&gt;
			&lt;groupId&gt;javax.servlet&lt;/groupId&gt;
			&lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
			&lt;version&gt;2.5&lt;/version&gt;
		&lt;/dependency&gt;
		
		&lt;dependency&gt;
			&lt;groupId&gt;taglibs&lt;/groupId&gt;
			&lt;artifactId&gt;standard&lt;/artifactId&gt;
			&lt;version&gt;1.1.2&lt;/version&gt;
		&lt;/dependency&gt;
		
		&lt;dependency&gt;
			&lt;groupId&gt;jstl&lt;/groupId&gt;
			&lt;artifactId&gt;jstl&lt;/artifactId&gt;
			&lt;version&gt;1.1.2&lt;/version&gt;
		&lt;/dependency&gt;
		
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework&lt;/groupId&gt;
			&lt;artifactId&gt;spring-aop&lt;/artifactId&gt;
			&lt;version&gt;3.0.5.RELEASE&lt;/version&gt;
		&lt;/dependency&gt;
		
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework&lt;/groupId&gt;
			&lt;artifactId&gt;spring-web&lt;/artifactId&gt;
			&lt;version&gt;3.0.5.RELEASE&lt;/version&gt;
		&lt;/dependency&gt;
		
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework&lt;/groupId&gt;
			&lt;artifactId&gt;spring-context-support&lt;/artifactId&gt;
			&lt;version&gt;3.0.5.RELEASE&lt;/version&gt;
		&lt;/dependency&gt;
		
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework&lt;/groupId&gt;
			&lt;artifactId&gt;spring-context&lt;/artifactId&gt;
			&lt;version&gt;3.0.5.RELEASE&lt;/version&gt;
		&lt;/dependency&gt;
		
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework&lt;/groupId&gt;
			&lt;artifactId&gt;spring-beans&lt;/artifactId&gt;
			&lt;version&gt;3.0.5.RELEASE&lt;/version&gt;
		&lt;/dependency&gt;
		
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework&lt;/groupId&gt;
			&lt;artifactId&gt;spring-core&lt;/artifactId&gt;
			&lt;version&gt;3.0.5.RELEASE&lt;/version&gt;
		&lt;/dependency&gt;
		
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework&lt;/groupId&gt;
			&lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
			&lt;version&gt;3.0.5.RELEASE&lt;/version&gt;
		&lt;/dependency&gt;
		
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework&lt;/groupId&gt;
			&lt;artifactId&gt;spring-web&lt;/artifactId&gt;
			&lt;version&gt;3.0.5.RELEASE&lt;/version&gt;
		&lt;/dependency&gt;
		
	&lt;/dependencies&gt;
	
	&lt;build&gt;
		&lt;finalName&gt;JenAndEric&lt;/finalName&gt;
		
		&lt;plugins&gt;

			&lt;plugin&gt;
				&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
				&lt;version&gt;2.1&lt;/version&gt;
				&lt;configuration&gt;
					&lt;source&gt;1.6&lt;/source&gt;
					&lt;target&gt;1.6&lt;/target&gt;
				&lt;/configuration&gt;
			&lt;/plugin&gt;
			
			&lt;plugin&gt;
				&lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
				&lt;artifactId&gt;tomcat-maven-plugin&lt;/artifactId&gt;
				&lt;configuration&gt;
					&lt;url&gt;http://localhost:8080/manager&lt;/url&gt;
					&lt;username&gt;tomcat&lt;/username&gt;
					&lt;password&gt;s3cret&lt;/password&gt;
					&lt;path&gt;/JenAndEric&lt;/path&gt;
				&lt;/configuration&gt;
			&lt;/plugin&gt;

		&lt;/plugins&gt;
	&lt;/build&gt;
&lt;/project&gt;
</pre></p>
<p>As you can see from the dependencies, it&#8217;s a Spring 3.0 project. Don&#8217;t mind the server credentials you see from the bottom of the code. I was too lazy to encrypt it anyway so I don&#8217;t care if you see my Tomcat server&#8217;s credentials right now. Also, notice the name of the web application that I&#8217;m making. I&#8217;m trying to make a web application for my family&#8217;s drug store so I can finally help with the family business. I&#8217;m such a good son. I think I should be awarded for this! Kidding aside, I don&#8217;t mind releasing some of the source code here but the project&#8217;s repository on <a href="http://bitbucket.org">BitBucket</a> is marked as private so you won&#8217;t really see everything.</p>
<p>Of course, this project won&#8217;t be able to run on GAE as is. I need to use the <a href="http://code.google.com/p/maven-gae-plugin/">maven-gae-plugin</a> for my project to be able to run on Google App Engine. For starters, I need to add the location of the repositories where Maven will download the plugin and dependencies since it&#8217;s not hosted on the global repos of Maven:</p>
<p><pre class="brush: xml;">
&lt;repositories&gt;
	&lt;repository&gt;
		&lt;id&gt;maven-gae-plugin-repo&lt;/id&gt;
		&lt;url&gt;http://maven-gae-plugin.googlecode.com/svn/repository&lt;/url&gt;
		&lt;name&gt;Maven Google App Engine Repository&lt;/name&gt;
	&lt;/repository&gt;
&lt;/repositories&gt;

&lt;pluginRepositories&gt;
	&lt;pluginRepository&gt;
		&lt;id&gt;maven-gae-plugin-repo&lt;/id&gt;
		&lt;url&gt;http://maven-gae-plugin.googlecode.com/svn/repository/&lt;/url&gt;
		&lt;name&gt;Maven Google App Engine Repository&lt;/name&gt;
	&lt;/pluginRepository&gt;
&lt;/pluginRepositories&gt;
</pre></p>
<p>Now that Maven knows where to download the plugins and dependencies that the project needs, it&#8217;s time to add the actual plugins themselves:</p>
<p><pre class="brush: xml;">
&lt;build&gt;
	&lt;plugins&gt;
	
		&lt;!-- Other plugins ommitted for brevity --&gt;
		
		&lt;plugin&gt;
			&lt;groupId&gt;net.kindleit&lt;/groupId&gt;
			&lt;artifactId&gt;maven-gae-plugin&lt;/artifactId&gt;
			&lt;version&gt;${gae.pluginVersion}&lt;/version&gt;
			&lt;dependencies&gt;
				&lt;dependency&gt;
					&lt;groupId&gt;net.kindleit&lt;/groupId&gt;
					&lt;artifactId&gt;gae-runtime&lt;/artifactId&gt;
					&lt;version&gt;${gae.version}&lt;/version&gt;
					&lt;type&gt;pom&lt;/type&gt;
				&lt;/dependency&gt;
			&lt;/dependencies&gt;
		&lt;/plugin&gt;
		
		&lt;plugin&gt;
			&lt;artifactId&gt;maven-release-plugin&lt;/artifactId&gt;
			&lt;configuration&gt;
				&lt;goals&gt;gae:deploy&lt;/goals&gt;
			&lt;/configuration&gt;
		&lt;/plugin&gt;

	&lt;/plugins&gt;
&lt;/build&gt;
</pre></p>
<p>Adding these plugins to your pom.xml allows you to use the gae:run and gae:deploy goals in your Maven project. The first one, <strong>gae:run</strong>, builds your project and deploys it to the development server that the GAE SDK comes with. The second one, <strong>gae:deploy</strong>, builds your project and deploys it to the actual server of your application inside Google App Engine.</p>
<p>Next up, we need to add the JAR files that the project needs to run on GAE. Of course, instead of adding them manually to the <strong>lib</strong> folder of the project, we&#8217;ll let Maven handle the dependency management for us:</p>
<p><pre class="brush: xml;">
&lt;dependencies&gt;
	
	&lt;!-- Other dependencies ommitted for brevity --&gt;
	
	&lt;dependency&gt;
		&lt;groupId&gt;com.google.appengine.orm&lt;/groupId&gt;
		&lt;artifactId&gt;datanucleus-appengine&lt;/artifactId&gt;
		&lt;version&gt;1.0.7.final&lt;/version&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;org.datanucleus&lt;/groupId&gt;
		&lt;artifactId&gt;datanucleus-core&lt;/artifactId&gt;
		&lt;version&gt;1.1.5&lt;/version&gt;
		&lt;exclusions&gt;
			&lt;exclusion&gt;
				&lt;groupId&gt;javax.transaction&lt;/groupId&gt;
				&lt;artifactId&gt;transaction-api&lt;/artifactId&gt;
			&lt;/exclusion&gt;
		&lt;/exclusions&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;com.google.appengine&lt;/groupId&gt;
		&lt;artifactId&gt;datanucleus-jpa&lt;/artifactId&gt;
		&lt;version&gt;1.1.5&lt;/version&gt;
		&lt;scope&gt;runtime&lt;/scope&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;com.google.appengine&lt;/groupId&gt;
		&lt;artifactId&gt;geronimo-jpa_3.0_spec&lt;/artifactId&gt;
		&lt;version&gt;1.1.1&lt;/version&gt;
		&lt;scope&gt;runtime&lt;/scope&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;com.google.appengine&lt;/groupId&gt;
		&lt;artifactId&gt;appengine-api-1.0-sdk&lt;/artifactId&gt;
		&lt;version&gt;${gae.version}&lt;/version&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;com.google.appengine&lt;/groupId&gt;
		&lt;artifactId&gt;appengine-tools-api&lt;/artifactId&gt;
		&lt;version&gt;${gae.version}&lt;/version&gt;
	&lt;/dependency&gt;

&lt;/dependencies&gt;
</pre></p>
<p>If you read the code thoroughly, you&#8217;ll notice that I used property references for some of the values. I did that so upgrading to a different version won&#8217;t be much of a hassle. Finally, I added the actual values for the property references that I made earlier:</p>
<p><pre class="brush: xml;">
&lt;properties&gt;
	&lt;gae.home&gt;C:\devtools\eclipse\plugins\com.google.appengine.eclipse.sdkbundle.1.4.2_1.4.2.v201102111811\appengine-java-sdk-1.4.2&lt;/gae.home&gt;
	&lt;gae.applicationName&gt;&lt;/gae.applicationName&gt;
	&lt;gae.pluginVersion&gt;0.7.1&lt;/gae.pluginVersion&gt;
	&lt;gae.version&gt;1.3.7&lt;/gae.version&gt;
&lt;/properties&gt;
</pre></p>
<p>As you can see, I haven&#8217;t given any name for my application yet. Why is it so hard to come up with names?</p>
<p>Now that we&#8217;re finished modifying the pom.xml of the project, it&#8217;s time to test if the project will indeed run on Google App Engine:</p>
<blockquote><p>$ mvn gae:run</p></blockquote>
<p>As I said earlier, <strong>gae:run</strong> builds and deploys the project to the development server. Aside from that, it also starts the development server so you can access it from your browser. Anyway, I&#8217;m too lazy to post the output, but I assure you that the project will indeed run when you go to <a href="http://localhost:8080">http://localhost:8080/</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terencehacks.wordpress.com/243/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terencehacks.wordpress.com/243/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terencehacks.wordpress.com/243/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terencehacks.wordpress.com/243/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/terencehacks.wordpress.com/243/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/terencehacks.wordpress.com/243/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/terencehacks.wordpress.com/243/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/terencehacks.wordpress.com/243/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terencehacks.wordpress.com/243/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terencehacks.wordpress.com/243/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terencehacks.wordpress.com/243/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terencehacks.wordpress.com/243/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terencehacks.wordpress.com/243/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terencehacks.wordpress.com/243/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terencehacks.wordpress.com&amp;blog=17292332&amp;post=243&amp;subd=terencehacks&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://terencehacks.wordpress.com/2011/03/12/a-noobs-attempt-on-google-app-engine-using-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fdd669d69fc537eaadf826104075af96?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">eeyorexd</media:title>
		</media:content>
	</item>
		<item>
		<title>Bad start with Spring part 2 &#8211; Resolved</title>
		<link>http://terencehacks.wordpress.com/2011/03/11/bad-start-with-spring-part-2-resolved/</link>
		<comments>http://terencehacks.wordpress.com/2011/03/11/bad-start-with-spring-part-2-resolved/#comments</comments>
		<pubDate>Fri, 11 Mar 2011 09:53:05 +0000</pubDate>
		<dc:creator>Terence Ponce</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://terencehacks.wordpress.com/?p=236</guid>
		<description><![CDATA[Last time, I was kind of frustrated as well as lazy about my problem. As a recap, I had a problem with manually injecting beans in Spring 3.0 because I tried doing it the old fashioned way. After a long time of reading and thinking, I realized that I&#8217;m having problems because I didn&#8217;t embrace [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terencehacks.wordpress.com&amp;blog=17292332&amp;post=236&amp;subd=terencehacks&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="/2011/03/07/bad-start-with-spring-part-2/">Last time</a>, I was kind of frustrated as well as lazy about my problem. As a recap, I had a problem with manually injecting beans in Spring 3.0 because I tried doing it the old fashioned way. After a long time of reading and thinking, I realized that I&#8217;m having problems because I didn&#8217;t embrace the sudden transition I had with annotations.</p>
<p>Since I&#8217;m the kind of person who embraces change, I decided to stop whining and use the auto wiring feature instead. I added the @Autowired annotation to the setter method inside the controller:</p>
<p><pre class="brush: java;">
package com.jenanderic.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.Model;

import java.util.Date;
import java.util.List;

import com.jenanderic.domain.Manufacturer;
import com.jenanderic.service.ManufacturerService;

@Controller
@RequestMapping(&quot;/manufacturer_list&quot;)
public class ManufacturerListController {

	private ManufacturerService manufacturerService;

	@RequestMapping(method = RequestMethod.GET)
	public String manufacturerList(Model model) {
		Date today = new Date();
		model.addAttribute(&quot;today&quot;, today);
		model.addAttribute(&quot;manufacturers&quot;, manufacturerService.getManufacturers());
		return &quot;manufacturer_list&quot;;
	}
	
	@Autowired
	public void setManufacturerService(ManufacturerService manufacturerService) {
		this.manufacturerService = manufacturerService;
	}
}
</pre></p>
<p>Actually, now that I&#8217;ve thought about it already, I find the use of annotations much better and cleaner than the old method. It takes a while to learn for it to be readable enough though. It took me about half a day of reading to actually realize how cool annotations are. I even used multiple references to learn this.</p>
<p>As of now, I&#8217;m learning how Spring works little by little. I even finished a few chapters on the book that I&#8217;m reading. As of now, I&#8217;m trying out how to make Spring-MVC work with Google App Engine, but I think I&#8217;ll cover that on another entry.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terencehacks.wordpress.com/236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terencehacks.wordpress.com/236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terencehacks.wordpress.com/236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terencehacks.wordpress.com/236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/terencehacks.wordpress.com/236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/terencehacks.wordpress.com/236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/terencehacks.wordpress.com/236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/terencehacks.wordpress.com/236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terencehacks.wordpress.com/236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terencehacks.wordpress.com/236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terencehacks.wordpress.com/236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terencehacks.wordpress.com/236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terencehacks.wordpress.com/236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terencehacks.wordpress.com/236/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terencehacks.wordpress.com&amp;blog=17292332&amp;post=236&amp;subd=terencehacks&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://terencehacks.wordpress.com/2011/03/11/bad-start-with-spring-part-2-resolved/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fdd669d69fc537eaadf826104075af96?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">eeyorexd</media:title>
		</media:content>
	</item>
		<item>
		<title>Bad start with Spring part 2</title>
		<link>http://terencehacks.wordpress.com/2011/03/07/bad-start-with-spring-part-2/</link>
		<comments>http://terencehacks.wordpress.com/2011/03/07/bad-start-with-spring-part-2/#comments</comments>
		<pubDate>Mon, 07 Mar 2011 03:15:40 +0000</pubDate>
		<dc:creator>Terence Ponce</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://terencehacks.wordpress.com/?p=229</guid>
		<description><![CDATA[Recently, I realized that Spring 2.5 was really old already. I was even nitpicked for it: @Terence why do you start with Spring 2.5 now when Spring 3 is already over a year old? – Sean Patrick Floyd Feb 24 at 10:50 The quote came from a comment in my question on StackOverflow. Anyway, I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terencehacks.wordpress.com&amp;blog=17292332&amp;post=229&amp;subd=terencehacks&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently, I realized that Spring 2.5 was really old already. I was even <a href="http://stackoverflow.com/questions/5102514/how-to-retrieve-a-row-in-a-database-and-convert-it-into-an-object-in-spring">nitpicked for it</a>:</p>
<blockquote><p>
@Terence why do you start with Spring 2.5 now when Spring 3 is already over a year old? – <a href="http://stackoverflow.com/users/342852/sean-patrick-floyd">Sean Patrick Floyd</a> Feb 24 at 10:50
</p></blockquote>
<p>The quote came from a comment in my question on StackOverflow.</p>
<p>Anyway, I decided to try and use Spring 3.0 because of that. One of the new things that I encountered was the way Spring 3.0 maps the request to controllers. The dispatcher servlet doesn&#8217;t handle the URL mapping to controllers now. Instead, you use annotations to handle it automatically.</p>
<p><strong>**UPDATE**</strong><br />
Apparently, I was wrong about the annotations. Spring already has annotation support in 2.5. I just didn&#8217;t get a chance encounter it back then which is why I thought it was completely new.<br />
<strong>**/UPDATE**</strong></p>
<p>So I tried making another simple web application to try it out. First of all, I made a bean class:</p>
<p><em>Manufacturer.java</em><br />
<pre class="brush: java;">
package com.jenanderic.domain;

public class Manufacturer {
	
	private int id;
	private String name;
	private String address;
	
	public Manufacturer() {}
	
	public Manufacturer(String name, String address) {
		this.name = name;
		this.address = address;
	}
	
	public Manufacturer(int id, String name, String address) {
		this(name, address);
		this.id = id;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getAddress() {
		return address;
	}
	
	public void setAddress(String address) {
		this.address = address;
	}
}
</pre></p>
<p>I then made an interface for managing the bean:</p>
<p><pre class="brush: java;">
package com.jenanderic.service;

import java.io.Serializable;
import java.util.List;

import com.jenanderic.domain.Manufacturer;

public interface ManufacturerService extends Serializable {
	
	public List&lt;Manufacturer&gt; getManufacturers();
	
	public void setManufacturers(List&lt;Manufacturer&gt; manufacturers);
}
</pre></p>
<p>After that, I made an implementation class for the interface I just created:</p>
<p><em>ManufacturerServiceImpl.java</em><br />
<pre class="brush: java;">
package com.jenanderic.service;

import java.util.List;

import com.jenanderic.domain.Manufacturer;

public class ManufacturerServiceImpl implements ManufacturerService {
	
	private List&lt;Manufacturer&gt; manufacturers;
	
	public List&lt;Manufacturer&gt; getManufacturers() {
		return manufacturers;
	}
	
	public void setManufacturers(List&lt;Manufacturer&gt; manufacturers) {
		this.manufacturers = manufacturers;
	}
}
</pre></p>
<p>Now that I have the classes that I need, I decided to make a controller class for it:</p>
<p><em>ManufacturerListController.java</em><br />
<pre class="brush: java;">
package com.jenanderic.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.Model;

import java.util.Date;
import java.util.List;

import com.jenanderic.domain.Manufacturer;
import com.jenanderic.service.ManufacturerService;

@Controller
@RequestMapping(&quot;/manufacturer_list&quot;)
public class ManufacturerListController {

	private ManufacturerService manufacturerService;
	
	@RequestMapping(method = RequestMethod.GET)
	public String manufacturerList(Model model) {
		Date today = new Date();
		model.addAttribute(&quot;today&quot;, today);
		model.addAttribute(&quot;manufacturers&quot;, manufacturerService.getManufacturers());
		return &quot;manufacturer_list&quot;;
	}
	
	public void setManufacturerService(ManufacturerService manufacturerService) {
		this.manufacturerService = manufacturerService;
	}
}
</pre></p>
<p>Notice the @Controller and @RequestMapping annotation. Putting the @Controller annotation in your class automatically makes it a controller class in Spring 3.0. Due to this, you don&#8217;t need to declare it in your dispatcher servlet anymore. I guess that&#8217;s pretty cool.</p>
<p>Oh wait, since I can&#8217;t declare the URL mapping of the controller in the dispatcher servlet, how do I inject my beans to the controller manually now? This is what my dispatcher servlet looks like right now:</p>
<p><pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
	xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
	xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
	xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd&quot;&gt;

	&lt;context:component-scan base-package=&quot;com.jenanderic.web&quot; /&gt;
	
	&lt;bean class=&quot;org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping&quot; /&gt;
	
	&lt;bean class=&quot;org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter&quot; /&gt;
	
	&lt;bean class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;&gt;
		&lt;property name=&quot;prefix&quot; value=&quot;/WEB-INF/jsp/&quot; /&gt;
		&lt;property name=&quot;suffix&quot; value=&quot;.jsp&quot; /&gt;
	&lt;/bean&gt;
	
	&lt;!--
	&lt;bean name=&quot;/manufacturer_list&quot; class=&quot;com.jenanderic.web.ManufacturerListController&quot;&gt;
		&lt;property name=&quot;manufacturerService&quot; ref=&quot;manufacturerService&quot; /&gt;
	&lt;/bean&gt;
	--&gt;
	
	&lt;bean id=&quot;manufacturerService&quot; class=&quot;com.jenanderic.service.ManufacturerServiceImpl&quot;&gt;
		&lt;property name=&quot;manufacturers&quot;&gt;
			&lt;list&gt;
				&lt;ref local=&quot;manufacturer1&quot; /&gt;
				&lt;ref local=&quot;manufacturer2&quot; /&gt;
				&lt;ref local=&quot;manufacturer3&quot; /&gt;
			&lt;/list&gt;
		&lt;/property&gt;
	&lt;/bean&gt;
	
	&lt;bean id=&quot;manufacturer1&quot; class=&quot;com.jenanderic.domain.Manufacturer&quot;&gt;
		&lt;property name=&quot;id&quot; value=&quot;1&quot; /&gt;
		&lt;property name=&quot;name&quot; value=&quot;Manufacturer #1&quot; /&gt;
		&lt;property name=&quot;address&quot; value=&quot;Address1&quot; /&gt;
	&lt;/bean&gt;
	
	&lt;bean id=&quot;manufacturer2&quot; class=&quot;com.jenanderic.domain.Manufacturer&quot;&gt;
		&lt;property name=&quot;id&quot; value=&quot;1&quot; /&gt;
		&lt;property name=&quot;name&quot; value=&quot;Manufacturer #2&quot; /&gt;
		&lt;property name=&quot;address&quot; value=&quot;Address2&quot; /&gt;
	&lt;/bean&gt;
	
	&lt;bean id=&quot;manufacturer3&quot; class=&quot;com.jenanderic.domain.Manufacturer&quot;&gt;
		&lt;property name=&quot;id&quot; value=&quot;1&quot; /&gt;
		&lt;property name=&quot;name&quot; value=&quot;Manufacturer #3&quot; /&gt;
		&lt;property name=&quot;address&quot; value=&quot;Address3&quot; /&gt;
	&lt;/bean&gt;
&lt;/beans&gt;
</pre></p>
<p>Notice the part that I commented out. That&#8217;s what I would have done if I wasn&#8217;t using annotations. Unfortunately, Spring 3.0 frowns upon not using annotations.</p>
<p>Anyway, running the program as is would result in a null pointer exception since this method won&#8217;t be getting any bean from the dispatcher servlet:</p>
<p><pre class="brush: java;">
@RequestMapping(method = RequestMethod.GET)
public String manufacturerList(Model model) {
	Date today = new Date();
	model.addAttribute(&quot;today&quot;, today);
	model.addAttribute(&quot;manufacturers&quot;, manufacturerService.getManufacturers());
	return &quot;manufacturer_list&quot;;
}
</pre></p>
<p>If I use the auto-wiring feature of Spring 3.0, I guess my problem would be resolved by now, but this excerpt from the book that I&#8217;m reading is giving me second thoughts of using the auto-wiring feature:</p>
<blockquote><p>Although the auto-wiring feature is very powerful, the cost is that it will reduce the readability of your bean configurations. Because auto-wiring is performed by Spring at runtime, you cannot derive how your beans are wired from the bean configuration file. In practice, we recommend applying autowiring only in applications whose component dependencies are not complicated.</p></blockquote>
<p>This was taken from the book, Spring Recipes &#8211; A problem-solution approach, 2nd edition by Gary Mak, Josh Long, and Daniel Rubio.</p>
<p>Yes, I know. I can&#8217;t really call my application complicated yet. I guess I&#8217;m just too lazy or too stupid to fix this problem on my own right now. Either way, I lost the motivation to learn Spring as of the moment. I can&#8217;t find a good introductory resource to Spring 3.0 that&#8217;s why I&#8217;m having a hard time right now. I really hope I get accepted at the code camp that I applied for. They&#8217;re going to teach Spring there. That might be my chance to finally have a good start with Spring.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terencehacks.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terencehacks.wordpress.com/229/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terencehacks.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terencehacks.wordpress.com/229/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/terencehacks.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/terencehacks.wordpress.com/229/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/terencehacks.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/terencehacks.wordpress.com/229/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terencehacks.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terencehacks.wordpress.com/229/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terencehacks.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terencehacks.wordpress.com/229/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terencehacks.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terencehacks.wordpress.com/229/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terencehacks.wordpress.com&amp;blog=17292332&amp;post=229&amp;subd=terencehacks&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://terencehacks.wordpress.com/2011/03/07/bad-start-with-spring-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fdd669d69fc537eaadf826104075af96?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">eeyorexd</media:title>
		</media:content>
	</item>
		<item>
		<title>Bad start with Spring &#8211; Resolved</title>
		<link>http://terencehacks.wordpress.com/2011/03/06/bad-start-with-spring-resolved/</link>
		<comments>http://terencehacks.wordpress.com/2011/03/06/bad-start-with-spring-resolved/#comments</comments>
		<pubDate>Sun, 06 Mar 2011 07:22:35 +0000</pubDate>
		<dc:creator>Terence Ponce</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://terencehacks.wordpress.com/?p=224</guid>
		<description><![CDATA[In my previous entry, I started to learn how to do simple CRUD operations using Spring-MVC&#8217;s JDBC feature. Unfortunately, it didn&#8217;t turn out to be as simple as I thought. So my problem was with this unit test that was failing because it returns an empty result set: This is getCompany()&#8217;s logic: Of course, I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terencehacks.wordpress.com&amp;blog=17292332&amp;post=224&amp;subd=terencehacks&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In my <a href="/2011/03/01/bad-start-with-spring/">previous entry</a>, I started to learn how to do simple CRUD operations using Spring-MVC&#8217;s JDBC feature. Unfortunately, it didn&#8217;t turn out to be as simple as I thought.</p>
<p>So my problem was with this unit test that was failing because it returns an empty result set:</p>
<p><pre class="brush: java;">
public void testGetCompany() {
	Company company = companyDao.getCompany(1);

	assertEquals(&quot;Benjo&quot;, company.getName());
}
</pre></p>
<p>This is getCompany()&#8217;s logic:</p>
<p><pre class="brush: java;">
public Company getCompany(int id) {
	logger.info(&quot;Getting company with id = &quot; + id);
	Company company = getSimpleJdbcTemplate().queryForObject(
			&quot;SELECT id, name FROM companies WHERE id = ?&quot;,
			new CompanyMapper(), id);
	return company;
}
</pre></p>
<p>Of course, I was certain that there was really an entry in the database with the company name, &#8220;Benjo&#8221; with the id, 1, so I couldn&#8217;t figure out where the problem is coming from.</p>
<p>A few days later, I got a chance to talk to my <a href="http://blog.dondiimperial.com/">mentor</a>. I described the problem to him as best as I can and I showed him my code as well. After a few minutes of discussion, he finally figured out where the problem was coming from.</p>
<p>Apparently, the problem had something to do with how I created the table in the database:</p>
<p><pre class="brush: sql;">
CREATE TABLE companies (
	id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
	name varchar(100) NOT NULL
);
</pre></p>
<p>The problem had something to do with the <strong>id</strong> field being an auto-incrementing field, but this wasn&#8217;t actually the root of the problem. There&#8217;s nothing wrong with using auto-incrementing fields. The problem was caused by how I insert values into the database:</p>
<p><pre class="brush: sql;">
INSERT INTO companies (name) VALUES ('Benjo');
INSERT INTO companies (name) VALUES ('Lasam');
INSERT INTO companies (name) VALUES ('Juffy');
</pre></p>
<p>The reason why my unit test retrieves an empty result set is because I didn&#8217;t hard code the value for id when inserting a value into the database. That&#8217;s why it can&#8217;t find &#8220;Benjo&#8221;, which supposedly should have an id of 1. After realizing that, I changed the query into this:</p>
<p><pre class="brush: sql;">
INSERT INTO companies (id, name) VALUES (1, 'Benjo');
INSERT INTO companies (id, name) VALUES (2, 'Lasam');
INSERT INTO companies (id, name) VALUES (3, 'Juffy');
</pre></p>
<p>For some reason, the previous query doesn&#8217;t assign the value of id as 1 when I let the auto-increment feature do the assigning for me. My mentor says it had something to do with the internal counter of the auto-increment feature. Honestly, I didn&#8217;t understand why that was. I don&#8217;t know how MySQL works internally. If you ask me, I don&#8217;t see anything wrong with the previous queries that I used.</p>
<p>Either way, my problem&#8217;s resolved already. As of now, I still can&#8217;t understand the reason behind the problem. Hopefully, I&#8217;ll be able to know about it in the future. At least I know how to deal with this kind of problem now.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terencehacks.wordpress.com/224/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terencehacks.wordpress.com/224/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terencehacks.wordpress.com/224/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terencehacks.wordpress.com/224/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/terencehacks.wordpress.com/224/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/terencehacks.wordpress.com/224/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/terencehacks.wordpress.com/224/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/terencehacks.wordpress.com/224/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terencehacks.wordpress.com/224/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terencehacks.wordpress.com/224/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terencehacks.wordpress.com/224/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terencehacks.wordpress.com/224/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terencehacks.wordpress.com/224/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terencehacks.wordpress.com/224/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terencehacks.wordpress.com&amp;blog=17292332&amp;post=224&amp;subd=terencehacks&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://terencehacks.wordpress.com/2011/03/06/bad-start-with-spring-resolved/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fdd669d69fc537eaadf826104075af96?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">eeyorexd</media:title>
		</media:content>
	</item>
		<item>
		<title>Bad start with Spring</title>
		<link>http://terencehacks.wordpress.com/2011/03/01/bad-start-with-spring/</link>
		<comments>http://terencehacks.wordpress.com/2011/03/01/bad-start-with-spring/#comments</comments>
		<pubDate>Tue, 01 Mar 2011 13:57:02 +0000</pubDate>
		<dc:creator>Terence Ponce</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://terencehacks.wordpress.com/?p=207</guid>
		<description><![CDATA[Recently, one of my good friends asked me to make a simple web application for them. Since I&#8217;ve been wanting to learn Spring for quite some time now, I decided to take on his request because I&#8217;m such a good person. Just kidding, I really just wanted to have an excuse to learn Spring. After [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terencehacks.wordpress.com&amp;blog=17292332&amp;post=207&amp;subd=terencehacks&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently, one of my good friends asked me to make a simple web application for them. Since I&#8217;ve been wanting to learn Spring for quite some time now, I decided to take on his request because I&#8217;m such a good person. Just kidding, I really just wanted to have an excuse to learn Spring.</p>
<p>After a few minutes of Google searching, I came across the <a href="http://static.springsource.org/docs/Spring-MVC-step-by-step/">official tutorial for Spring MVC</a>. I noticed that the tutorial was a bit outdated and had a few errors, but I still managed to finish the tutorial in spite of that.</p>
<p>Now that I already did all the necessary stuff to make a simple web application in Spring, I decided to add a bit more to get started with the project that my friend asked me to do. The first thing that I tried to do was learn how to do CRUD operations in Spring. I started by making a Java bean called company:</p>
<p><em>Company.java</em><br />
<pre class="brush: java;">
package gpspeedometer.domain;

import java.io.Serializable;

public class Company implements Serializable {
	private int id;
	private String name;
	
	public Company() {
	
	}
	
	public Company(String name) {
		setName(name);
	}
	
	public Company(Company company) {
		this(company.getName());
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append(&quot;Name: &quot; + name);
		return buffer.toString();
	}
	
	@Override
	public boolean equals(Object object) {
		if (object != null &amp;&amp; object instanceof Company) {
			Company company = (Company) object;
			if (company.getId() == getId() &amp;&amp; company.getName().equals(getName())) {
				return true;
			}
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		return getId() ^ getName().hashCode();
	}
}
</pre></p>
<p>I&#8217;m not sure if I&#8217;ve overridden hashCode() and equals() right, but that&#8217;s not really the issue here. Since the tutorial was all about TDD, I decided to make a unit test for the bean that I just created:</p>
<p><em>CompanyTests.java</em><br />
<pre class="brush: java;">
package gpspeedometer.domain;

import gpspeedometer.domain.Company;

import junit.framework.TestCase;

public class CompanyTests extends TestCase {
	private Company company;
	
	protected void setUp() throws Exception {
		company = new Company();
	}
	
	public void testGetAndSetName() {
		String testName = &quot;random name&quot;;
		assertNull(company.getName());
		company.setName(testName);
		assertEquals(testName, company.getName());
	}
	
	public void testOverrideToString() {
		String testToString = &quot;Name: Random&quot;;
		company.setName(&quot;Random&quot;);
		assertEquals(testToString, company.toString());
	}
	
	public void testOverrideEquals() {
		company.setId(1);
		company.setName(&quot;Lasam&quot;);
		Company company2 = new Company();
		company2.setId(1);
		company2.setName(&quot;Lasam&quot;);
		assertEquals(company.equals(company2), true);
	}
	
	public void testOverrideHashCode() {
		company.setId(1);
		company.setName(&quot;Lasam&quot;);
		Company company2 = new Company();
		company2.setId(1);
		company2.setName(&quot;Lasam&quot;);
		assertEquals(company.hashCode(), company2.hashCode());
	}
}
</pre></p>
<p>Of course, a test as simple as this would most likely pass. Just kidding. To be honest, I&#8217;m not really sure about the tests on hashCode and equals, but again, that&#8217;s not the issue here. After making the bean, I made an interface to do handle the bean services:</p>
<p><em>CompanyManager.java</em><br />
<pre class="brush: java;">
package gpspeedometer.service;

import java.io.Serializable;
import java.util.List;

import gpspeedometer.domain.Company;
import gpspeedometer.repository.CompanyDao;

public interface CompanyManager extends Serializable {
	
	public List&lt;Company&gt; getCompanies();
	
	public void setCompanies(List&lt;Company&gt; companies);
	
	public void setCompanyDao(CompanyDao companyDao);
}
</pre></p>
<p>I then made an implementation class for the interface that I just made:</p>
<p><em>SimpleCompanyManager.java</em><br />
<pre class="brush: java;">
package gpspeedometer.service;

import java.util.ArrayList;
import java.util.List;

import gpspeedometer.domain.Company;
import gpspeedometer.repository.CompanyDao;

public class SimpleCompanyManager implements CompanyManager {
	private CompanyDao companyDao;
	private List&lt;Company&gt; companies;

	public List&lt;Company&gt; getCompanies() {
		return companyDao.getCompanyList();
	}
	
	public void setCompanies(List&lt;Company&gt; companies) {
		this.companies = companies;
	}
	
	public void setCompanyDao(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}
}
</pre></p>
<p>After that, I made a unit test for the class that I just made:</p>
<p><em>SimpleCompanyManagerTests.java</em><br />
<pre class="brush: java;">
package gpspeedometer.service;

import java.util.ArrayList;
import java.util.List;

import gpspeedometer.domain.Company;
import gpspeedometer.repository.CompanyDao;
import gpspeedometer.repository.InMemoryCompanyDao;

import junit.framework.TestCase;

public class SimpleCompanyManagerTests extends TestCase {
	private SimpleCompanyManager companyManager;
	private List&lt;Company&gt; companies;
	
	private static int COMPANY_COUNT = 2;
	
	private static String COMPANY_ONE = &quot;Bus Company #1&quot;;
	private static String COMPANY_TWO = &quot;Bus Company #2&quot;;
	
	protected void setUp() throws Exception {
		companyManager = new SimpleCompanyManager();
		companies = new ArrayList&lt;Company&gt;();
		
		Company company = new Company();
		company.setName(&quot;Bus Company #1&quot;);
		companies.add(company);
		
		company = new Company();
		company.setName(&quot;Bus Company #2&quot;);
		companies.add(company);
		
		CompanyDao companyDao = new InMemoryCompanyDao(companies);
		companyManager.setCompanyDao(companyDao);
	}
	
	public void testGetCompaniesWithNoCompanies() {
		companyManager = new SimpleCompanyManager();
		companyManager.setCompanyDao(new InMemoryCompanyDao(null));
		assertNull(companyManager.getCompanies());
	}
	
	public void testGetCompanies() {
		List&lt;Company&gt; companies = companyManager.getCompanies();
		assertNotNull(companies);
		assertEquals(COMPANY_COUNT, companyManager.getCompanies().size());
		
		Company company = companies.get(0);
		assertEquals(COMPANY_ONE, company.getName());
		
		company = companies.get(1);
		assertEquals(COMPANY_TWO, company.getName());
	}
}
</pre></p>
<p>Well, hey, the tests passed again. Lucky me. At this point, I&#8217;m pretty sure you&#8217;re already bored reading this entry. I wouldn&#8217;t be surprised if you skipped over what I said over here. I&#8217;m feeling sad about this, so bear with me for a while. Anyway, let&#8217;s move on to the database part of the program. Again, I made another interface to handle the DAO of Company:</p>
<p><em>CompanyDao.java</em><br />
<pre class="brush: java;">
package gpspeedometer.repository;

import java.util.List;

import gpspeedometer.domain.Company;

public interface CompanyDao {
	public List&lt;Company&gt; getCompanyList();
	
	public Company getCompany(int id);
	
	public void addCompany(Company company);
	
	public void editCompany(Company company);
}
</pre></p>
<p>I was on a roll until this happened. I made the implementation class for CompanyDao:</p>
<p><em>JdbcCompanyDao.java</em><br />
<pre class="brush: java;">
package gpspeedometer.repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import gpspeedometer.domain.Company;

public class JdbcCompanyDao extends SimpleJdbcDaoSupport implements CompanyDao {
	protected final Log logger = LogFactory.getLog(getClass());
	
	public List&lt;Company&gt; getCompanyList() {
		logger.info(&quot;Getting companies.&quot;);
		List&lt;Company&gt; companies = getSimpleJdbcTemplate().query(
				&quot;SELECT id, name FROM companies&quot;,
				new CompanyMapper());
		return companies;
	}
	
	public Company getCompany(int id) {
		logger.info(&quot;Getting company with id = &quot; + id);
		Company company = getSimpleJdbcTemplate().queryForObject(
				&quot;SELECT id, name FROM companies WHERE id = ?&quot;,
				new CompanyMapper(), id);
		return company;
	}
	
	public void addCompany(Company company) {
		logger.info(&quot;Inserting company: &quot; + company.getName());
		int count = getSimpleJdbcTemplate().update(
				&quot;INSERT INTO companies (name) VALUES (:name)&quot;,
				new MapSqlParameterSource()
					.addValue(&quot;name&quot;, company.getName()));
		logger.info(&quot;Rows affected: &quot; + count);
	}
	
	public void editCompany(Company company) {
		logger.info(&quot;Editing company: &quot; + company.getName());
		int count = getSimpleJdbcTemplate().update(
				&quot;UPDATE companies SET name = :name WHERE id = :id&quot;,
				new MapSqlParameterSource()
					.addValue(&quot;name&quot;, company.getName())
					.addValue(&quot;id&quot;, company.getId()));
		logger.info(&quot;Rows affected: &quot; + count);
	}
	
	private static class CompanyMapper implements ParameterizedRowMapper&lt;Company&gt; {
		public Company mapRow(ResultSet rs, int rowNum) throws SQLException {
			Company company = new Company();
			company.setId(rs.getInt(&quot;id&quot;));
			company.setName(rs.getString(&quot;name&quot;));
			return company;
		}
	}
}
</pre></p>
<p>If you look at the code at first glance, you wouldn&#8217;t see anything wrong with it. Unfortunately, that wasn&#8217;t the case. I learned that from the unit test that I made for the previous class:</p>
<p><em>JdbcCompanyDaoTests.java</em><br />
<pre class="brush: java;">
package gpspeedometer.repository;

import java.util.List;

import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;

import gpspeedometer.domain.Company;

public class JdbcCompanyDaoTests extends AbstractTransactionalDataSourceSpringContextTests {
	private CompanyDao companyDao;
	
	public void setCompanyDao(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}
	
	@Override
	protected String[] getConfigLocations() {
		return new String[] {&quot;classpath:test-context.xml&quot;};
	}
	
	@Override
	protected void onSetUpInTransaction() throws Exception {
		super.deleteFromTables(new String[] {&quot;companies&quot;});
		super.executeSqlScript(&quot;file:db/load_data.sql&quot;, true);
	}
	
	public void testGetCompanyList() {
		List&lt;Company&gt; companies = companyDao.getCompanyList();
		
		assertEquals(&quot;Wrong number of companies?&quot;, 3, companies.size());
	}
	
	public void testGetCompany() {
		Company company = companyDao.getCompany(1);
		
		assertEquals(&quot;Benjo&quot;, company.getName());
	}
	
	public void testAddCompany() {
		Company company = new Company();
		company.setName(&quot;Ponce Inc.&quot;);
		companyDao.addCompany(company);
		
		company = companyDao.getCompany(4);
		assertEquals(&quot;Wrong name of company?&quot;, &quot;Ponce Inc.&quot;, company.getName());
	}
	
	public void testEditCompany() {
		List&lt;Company&gt; companies = companyDao.getCompanyList();
		
		for (Company company : companies) {
			company.setName(&quot;Lasam Inc.&quot;);
			companyDao.editCompany(company);
		}
		
		List&lt;Company&gt; updatedCompanies = companyDao.getCompanyList();
		for (Company company : updatedCompanies) {
			assertEquals(&quot;Wrong name of company?&quot;, &quot;Lasam Inc.&quot;, company.getName());
		}
	}
}
</pre></p>
<p>The tests failed at lines 33-37 at JdbcCompanyDaoTests.java, specifically, testGetCompany(). Here&#8217;s the error that I get from JUnit in Ant:</p>
<blockquote><p>[junit] Testcase: testAddCompany(gpspeedometer.repository.JdbcCompanyDaoTests):     Caused an ERROR<br />
[junit] Incorrect result size: expected 1, actual 0<br />
[junit] org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0<br />
[junit]     at org.springframework.dao.support.DataAccessUtils.requiredSingleResult(DataAccessUtils.java:71)<br />
[junit]     at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:722)<br />
[junit]     at org.springframework.jdbc.core.simple.SimpleJdbcTemplate.queryForObject(SimpleJdbcTemplate.java:169)<br />
[junit]     at gpspeedometer.repository.JdbcCompanyDao.getCompany(JdbcCompanyDao.java:29)<br />
[junit]     at gpspeedometer.repository.JdbcCompanyDaoTests.testAddCompany(JdbcCompanyDaoTests.java:44)<br />
[junit]     at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76)<br />
[junit]<br />
[junit]<br />
[junit] Test gpspeedometer.repository.JdbcCompanyDaoTests FAILED</p></blockquote>
<p>For some reason, the query returns nothing, which results in an empty result set. The development came to a screeching halt the moment this error appeared. The reason why this sucked is because I&#8217;m still working my way around Spring and how it works. I haven&#8217;t got a clue as to how this happened since I don&#8217;t really know what&#8217;s happening behind the scenes with my code. Let&#8217;s take a look at the faulty methods here:</p>
<p><pre class="brush: java;">
public Company getCompany(int id) {
	logger.info(&quot;Getting company with id = &quot; + id);
	Company company = getSimpleJdbcTemplate().queryForObject(
			&quot;SELECT id, name FROM companies WHERE id = ?&quot;,
			new CompanyMapper(), id);
	return company;
}
</pre></p>
<p><pre class="brush: java;">
public void testGetCompany() {
	Company company = companyDao.getCompany(1);
	
	assertEquals(&quot;Benjo&quot;, company.getName());
}
</pre></p>
<p>On my database, there&#8217;s a table called company that has &#8220;Benjo&#8221; as its name on the first row, so I checked if the test data really made it into the database:</p>
<p><pre class="brush: java;">
@Override
protected void onSetUpInTransaction() throws Exception {
	super.deleteFromTables(new String[] {&quot;companies&quot;});
	super.executeSqlScript(&quot;file:db/load_data.sql&quot;, true);
}
</pre></p>
<p>The method loads the sql file called load_data before starting the tests. Here are its contents:</p>
<p><em>load_data.sql</em><br />
<pre class="brush: sql;">
INSERT INTO companies (name) VALUES ('Benjo');
INSERT INTO companies (name) VALUES ('Lasam');
INSERT INTO companies (name) VALUES ('Juffy');
</pre></p>
<p>If this is the case, the method, getCompany(), should be able to retrieve a row from the database, but unfortunately, that didn&#8217;t happen. It always returned an empty result set.</p>
<p>Since I couldn&#8217;t handle the problem by myself anymore, I turned to <a href="http://stackoverflow.com">StackOverflow</a> for answers. My problem is still vague for me, so I decided to ask <a href="http://stackoverflow.com/q/5102514/395972">a very generic question</a> about my problem. My question didn&#8217;t receive any good feedback. All I got was a guy asking me why I was still using Spring 2.5 instead of 3.0, which was out of the question. Honestly, I don&#8217;t even know how that is relevant to my problem. I&#8217;m pretty sure migrating to 3.0 won&#8217;t solve my problem since I&#8217;ll most likely be writing the same code if that happens.</p>
<p>Anyway, since I still haven&#8217;t received any help from anyone, and I&#8217;m still clueless as to how Spring works, I decided to blog about it right now. This really made me sad because I can&#8217;t even do simple CRUD operations. What more if I needed to do something more complex? Hopefully, someone will be patient enough to read this and help me with my problem.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terencehacks.wordpress.com/207/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terencehacks.wordpress.com/207/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terencehacks.wordpress.com/207/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terencehacks.wordpress.com/207/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/terencehacks.wordpress.com/207/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/terencehacks.wordpress.com/207/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/terencehacks.wordpress.com/207/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/terencehacks.wordpress.com/207/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terencehacks.wordpress.com/207/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terencehacks.wordpress.com/207/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terencehacks.wordpress.com/207/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terencehacks.wordpress.com/207/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terencehacks.wordpress.com/207/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terencehacks.wordpress.com/207/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terencehacks.wordpress.com&amp;blog=17292332&amp;post=207&amp;subd=terencehacks&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://terencehacks.wordpress.com/2011/03/01/bad-start-with-spring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fdd669d69fc537eaadf826104075af96?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">eeyorexd</media:title>
		</media:content>
	</item>
		<item>
		<title>Inheritance in Java</title>
		<link>http://terencehacks.wordpress.com/2011/01/29/inheritance-in-java/</link>
		<comments>http://terencehacks.wordpress.com/2011/01/29/inheritance-in-java/#comments</comments>
		<pubDate>Sat, 29 Jan 2011 12:16:42 +0000</pubDate>
		<dc:creator>Terence Ponce</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[oop]]></category>

		<guid isPermaLink="false">http://terencehacks.wordpress.com/?p=176</guid>
		<description><![CDATA[DISCLAIMER: This article is a bit hard to swallow so I&#8217;ll try to explain it to you as simple as I can. I also need to mention that there are other concepts that will also be introduced in this chapter aside from Inheritance. This is my first post for 2011, so happy new year! Anyway, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terencehacks.wordpress.com&amp;blog=17292332&amp;post=176&amp;subd=terencehacks&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>DISCLAIMER:</strong> This article is a bit hard to swallow so I&#8217;ll try to explain it to you as simple as I can. I also need to mention that there are other concepts that will also be introduced in this chapter aside from Inheritance.</p>
<p>This is my first post for 2011, so happy new year! Anyway, as a preparation for my upcoming preliminary exam in Java, I decided to write about Inheritance in this article.</p>
<p>Let&#8217;s say you&#8217;re at work right now. Your boss asked you to make an application that records your company&#8217;s employees&#8217; information.</p>
<p>He mentioned the things you need to record:</p>
<ol>
<li>Name</li>
<li>Salary ($500 being the default salary, unless specified)</li>
<li>Employee Number</li>
<li>Department Handled (For managers only)</li>
</ol>
<p>Since you&#8217;re already familiar with classes, you decided to start immediately by creating classes that models the employees of the company. You started by making a class for the regular employee:</p>
<p><em>Employee.java</em><br />
<pre class="brush: java;">
public class Employee {
	private String name;
	private double salary;
	private int empNo;
	
	public Employee(String name, double salary, int empNo) {
		setName(name);
		setSalary(salary);
		setEmpNo(empNo);
	}
	
	public String getName() {
		return this.name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public double getSalary() {
		return this.salary;
	}
	
	public void setSalary(double salary) {
		this.salary = salary;
	}
	
	public int getEmpNo() {
		return this.empNo;
	}
	
	public void setEmpNo(int empNo) {
		this.empNo = empNo;
	}
	
	public void getDetails() {
		System.out.println(&quot;Name: &quot; + getName());
		System.out.println(&quot;Salary: &quot; + getSalary());
		System.out.println(&quot;EmpNo: &quot; + getEmpNo());
	}
}
</pre></p>
<p>Now that you&#8217;re finished creating a class for the regular employee, you decided to make another class for the manager, since your boss specifically told you that there are 2 types of employees at the company:</p>
<p><em>Manager.java</em><br />
<pre class="brush: java;">
public class Manager {
	private String name;
	private double salary;
	private int empNo;
	private String department;
	
	public Manager(String name, double salary, int empNo, String department) {
		setName(name);
		setSalary(salary);
		setEmpNo(empNo);
		setDepartment(department);
	}
	
	public String getName() {
		return this.name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public double getSalary() {
		return this.salary;
	}
	
	public void setSalary(double salary) {
		this.salary = salary;
	}
	
	public int getEmpNo() {
		return this.empNo;
	}
	
	public void setEmpNo(int empNo) {
		this.empNo = empNo;
	}
	
	public String getDepartment() {
		return this.department;
	}
	
	public void setDepartment(String department) {
		this.department = department;
	}
	
	public void getDetails() {
		System.out.println(&quot;Name: &quot; + getName());
		System.out.println(&quot;Salary: &quot; + getSalary());
		System.out.println(&quot;EmpNo: &quot; + getEmpNo());
		System.out.println(&quot;Department: &quot; + getDepartment();
	}
}
</pre></p>
<p>Wait, what&#8217;s this? Did you notice how similar the 2 classes are? They&#8217;re so similar, you can&#8217;t even tell how different they are at first glance (Well, aside from the file name of course). This may seem fine at first, but what if your company introduced several new types of employee? It would be pretty ridiculous to have to write almost the same thing everytime a new type of employee is introduced, right? If only there was a way to turn all those redundant code into something we can reuse so we wouldn&#8217;t have to write them every single time.</p>
<p>Fortunately, we can! One of the main features of Object-Oriented Programming is Inheritance. Let me quote <a href="http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29">Wikipedia</a> on this one:</p>
<blockquote><p>In Object-Oriented Programming (OOP), <strong>Inheritance</strong> is a way to compartmentalize and reuse code by creating collections of attributes and behaviors called objects which can be based on previously created objects. In classical inheritance where objects are defined by classes, classes can inherit other classes. The new classes, known as subclasses (or derived classes), inherit attributes and behavior of the pre-existing classes, which are referred to as superclasses (or ancestor classes). The inheritance relationships of classes gives rise to a hierarchy.</p></blockquote>
<p>Basically, what we do with Inheritance is we make a base class (super class) that contains the most common attributes and methods among all the classes, and make other classes (sub classes), which inherits from the base class, to customize and extend the functionality of the base class into their own. We do this to prevent code redundancy and promote code reuse. Lesser code redundancy = faster, and more efficient code.</p>
<p>Now that we&#8217;re familiar with Inheritance, let&#8217;s try to incorporate it in our code. Based from the boss&#8217; specifications, it&#8217;s safe to say that Employee is the super class while Manager is the sub class. Since Employee is the base class, we don&#8217;t need to modify it in any way. Manager is the one containing the redundant code so let&#8217;s modify that instead:</p>
<p><em>Manager.java</em> Version 2<br />
<pre class="brush: java;">
public class Manager extends Employee {
	private String department;
	
	public Manager(String name, double salary, int empNo, String department) {
		super(name, salary, empNo);
		setDepartment(department);
	}
	
	public String getDepartment() {
		return this.department;
	}
	
	public void setDepartment(String department) {
		this.department = department;
	}
	
	public void getDetails() {
		super.getDetails();
		System.out.println(&quot;Department: &quot; + getDepartment());
	}
}
</pre></p>
<p>ZOMG! We just shrunk Manager.java from 52 lines down to 21. Just because this version has less lines than the previous one doesn&#8217;t mean it&#8217;s simpler. In fact, it&#8217;s so complicated, it&#8217;s hard to read&#8230; at first. Let me demistify the code one by one:</p>
<p><pre class="brush: java;">
public class Manager extends Employee {
</pre></p>
<p>We used the word <strong>extends</strong> to make the Manager class inherit from the Employee class. This creates a relationship and a hierarchy between the two classes. It&#8217;s like saying that Manager is an Employee, but not the other way around. From this point, Manager will inherit everything from Employee, and by everything, I mean the attributes, and methods, but not the constructors.</p>
<hr />
<p><pre class="brush: java;">
private String department;
</pre></p>
<p>Noticed how we only declared one attribute here? Since Manager inherits from Employee, it already possesses all the attributes and methods from Employee, so there&#8217;s no need to declare them anymore.</p>
<hr />
<p><pre class="brush: java;">
public Manager(String name, double salary, int empNo, String department) {
	super(name, salary, empNo);
	setDepartment(department);
}
</pre></p>
<p>We made a constructor for the manager class, but this time our constructor has an additional argument, department, since managers has to handle a department in the company.</p>
<p>Line 2 calls the super class&#8217; constructor, which returns the created Employee object.</p>
<p>Line 3 sets a value to the Manager&#8217;s department attribute using <strong>setDepartment()</strong>.</p>
<p>After all that is done, a Manager object is now created with values for the name, salary, employee number, and department attributes.</p>
<hr />
<p><pre class="brush: java;">
public String getDepartment() {
	return this.department;
}

public void setDepartment(String department) {
	this.department = department;
}
</pre></p>
<p>As I said earlier, since Manager inherits from Employee, we only need to make a getter and setter method for department.</p>
<hr />
<p><pre class="brush: java;">
public void getDetails() {
	super.getDetails();
	System.out.println(&quot;Department: &quot; + getDepartment());
}
</pre></p>
<p>Since I kept repeating how we don&#8217;t need to remake methods that we already made in the super class, shouldn&#8217;t this be illegal or something? Of course not! Actually, what we did here is a technique called <em><strong>Method Overriding</strong></em>. Again, let me quote <a href="http://en.wikipedia.org/wiki/Method_overriding">Wikipedia</a> on this one:</p>
<blockquote><p><strong>Method Overriding</strong>, in Object Oriented Programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super classes or parent classes. The implementation in the subclass overrides (replaces) the implementation in the super class by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class. The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, or If an object of the subclass is used to invoke the method, then the version in the child class will be executed.</p></blockquote>
<p>Since <strong>getDetails()</strong> from Employee only prints the name, salary, and the employee number, we need to override it inside the Manager class so it prints the department as well.</p>
<p>At line 2, we call the <strong>getDetails()</strong> method of Employee, which prints the name, salary, and employee number. After that, we print the department at line 3.</p>
<hr />
<p>Now that we&#8217;re done creating our model classes, let&#8217;s make another class to demonstrate what we learned in this article:</p>
<p><pre class="brush: java;">
public class EmployeeDemo {
	public static void main(String args[]) {
		Employee e = new Employee(&quot;Your name&quot;, 25000.00, 0001);
		Manager m = new Manager(&quot;Terence&quot;, 100000.00, 0002, &quot;Administration&quot;);
		
		e.getDetails();
		m.getDetails();
	}
}
</pre></p>
<p>Isn&#8217;t it cool how I&#8217;m your boss even though you&#8217;re employee #1? Kidding aside, this will be the output of the class:</p>
<blockquote><p>Name: Your name<br />
Salary: 25000.00<br />
EmpNo: 0001</p>
<p>Name: Terence<br />
Salary: 100000.00<br />
EmpNo: 0002<br />
Department: Administration</p></blockquote>
<p>Isn&#8217;t Inheritance neat? I&#8217;m pretty sure you still don&#8217;t have any solid idea where to use this, but by the time you&#8217;re designing a large or complex software, you&#8217;ll find that Inheritance makes a lot of things easier in a design point of view.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terencehacks.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terencehacks.wordpress.com/176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terencehacks.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terencehacks.wordpress.com/176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/terencehacks.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/terencehacks.wordpress.com/176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/terencehacks.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/terencehacks.wordpress.com/176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terencehacks.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terencehacks.wordpress.com/176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terencehacks.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terencehacks.wordpress.com/176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terencehacks.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terencehacks.wordpress.com/176/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terencehacks.wordpress.com&amp;blog=17292332&amp;post=176&amp;subd=terencehacks&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://terencehacks.wordpress.com/2011/01/29/inheritance-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fdd669d69fc537eaadf826104075af96?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">eeyorexd</media:title>
		</media:content>
	</item>
		<item>
		<title>Code Refactoring</title>
		<link>http://terencehacks.wordpress.com/2010/12/15/code-refactoring/</link>
		<comments>http://terencehacks.wordpress.com/2010/12/15/code-refactoring/#comments</comments>
		<pubDate>Wed, 15 Dec 2010 01:35:58 +0000</pubDate>
		<dc:creator>Terence Ponce</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://terencehacks.wordpress.com/?p=152</guid>
		<description><![CDATA[In the real world, you&#8217;re not a one-man team like you once were during college (because apparently, it&#8217;s cheating in your professor&#8217;s eyes). You will work with other people when developing software. If that&#8217;s the case, it&#8217;s inevitable that those other people you&#8217;re working with will also be looking at your code. Not only that, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terencehacks.wordpress.com&amp;blog=17292332&amp;post=152&amp;subd=terencehacks&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In the real world, you&#8217;re not a one-man team like you once were during college (because apparently, it&#8217;s cheating in your professor&#8217;s eyes). You will work with other people when developing software. If that&#8217;s the case, it&#8217;s inevitable that those other people you&#8217;re working with will also be looking at your code. Not only that, they&#8217;ll also be working with the code that you will write.</p>
<p>One of the things a developer must strive for is better code. What do I mean by better code? Take a look at the code you&#8217;re working on right now. If you show it to other programmers, will they be able to figure out how your code works immediately without having to look at it extensively?</p>
<p>Does your code have too much comments? Do most of the comments just explain the &#8220;what&#8221; and not the &#8220;why&#8221; of the code? It&#8217;s not that I have anything against using comments. I simply believe that comments should only be used to indicate why you did the following approach in writing the code to which to comment points to instead of what the code does.</p>
<p>Do you find yourself repeating a lot of code in your software?</p>
<p>Take a look at some of the codes that you made in the past. Do you still understand how everything works?</p>
<p>If you answered No to the first and last question and Yes to the rest of the questions, then you, dude, have bad code. It&#8217;s time to give your code a makeover a.k.a. code refactoring.</p>
<p><a href="http://en.wikipedia.org/wiki/Code_refactoring">Code Refactoring</a>, by definition of Wikipedia, is the process of changing a program&#8217;s source code without affecting its external functional behavior to improve some of the non-functional aspects of the program. You usually change it for the better.</p>
<p>Code Refactoring has the following benefits:</p>
<ol>
<li>You make your code more readable to yourself and others as well.</li>
<li>Since it makes your code more readable, it makes your software easier to maintain and extend.</li>
<li>More often than not, refactoring results to lesser lines of code, so it actually saves you some bytes of memory when refactoring</li>
<li>It results to lesser comments in your code since the code is pretty much self-explanatory already.</li>
<li>It saves you from being screamed at by your fellow programmers for showing them bad code.</li>
</ol>
<p>To further my discussion on the concept of code refactoring, let me show you a basic example of how refactoring works.</p>
<p>We&#8217;ll use the code that we made in <a href="/2010/12/12/access-modifiers-in-java/">my last entry</a>. Here are both the source files we made:</p>
<p><em>Car.java</em><br />
<pre class="brush: java;">
public class Car {
	private String brand;
	private String color;
	private String name;
	private int speed = 0;
	private int gear = 1;

	public String getBrand() {
		return this.brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public String getColor() {
		return this.color;
	}

	public void setColor(String color) {
		color = color.replaceAll(&quot;[0-9]&quot;, &quot;&quot;);
		this.color = color;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getSpeed() {
		return this.speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public int getGear() {
		return this.gear;
	}

	public void setGear(int gear) {
		this.gear = gear;
	}
}
</pre></p>
<p><em>CarDemo.java</em><br />
<pre class="brush: java;">
public class CarDemo {
	public static void main(String[]args) {
		Car myCar = new Car();

		myCar.setBrand(&quot;Porsche&quot;);
		myCar.setColor(&quot;White123&quot;);
		myCar.setName(&quot;911 GT3&quot;);
		myCar.setSpeed(120);
		myCar.setGear(2);

		System.out.println(&quot;The car is a &quot; + myCar.getColor() + &quot; &quot; + myCar.getBrand() + &quot; &quot; myCar.getName() + &quot;.&quot;);
		System.out.println(&quot;The car is runs at &quot; + myCar.getSpeed() + &quot; miles per hour on gear &quot; + myCar.getGear());
	}
}
</pre></p>
<p>Let&#8217;s focus our attention to <em>CarDemo.java</em>. Look at how we invoked the Car class in this example. You first declare and instantiate an object then you have to set values to them one by one. Isn&#8217;t it too much of a hassle to create objects with this approach? I&#8217;m pretty lazy to have to write all these so it&#8217;s only natural to find a way to do them in shorthanded way.</p>
<p>Speaking of laziness, did I tell it&#8217;s one of the virtues of a good programmer? No, not that kind of laziness where you bum around and do nothing. Here&#8217;s the definition of laziness according to the book, <a href="http://c2.com/cgi/wiki?LazinessImpatienceHubris">ProgrammingPerl</a>:</p>
<blockquote><p><strong>Laziness</strong>: The quality that makes you go to great effort to reduce overall energy expenditure. It makes you write labor-saving programs that other people will find useful, and document what you wrote so you don&#8217;t have to answer so many questions about it. Hence, the first great virtue of a programmer. Also hence, this book.</p></blockquote>
<p>Now that we know how being lazy is a good trait for a programmer, we need to do something about how we create Car objects.</p>
<p>Let&#8217;s say one of your co-workers, Bob, who also happens to be your team mate in the program you&#8217;re working on, wanted Car objects to have specific default values for their attributes when he creates them so he can mass produce them (Well, mass producing is a bit of an exaggeration but just go with it). He told you that he only wants to specify the brand and name of the car and he wants every car that he makes to have it&#8217;s default color set to &#8220;White&#8221;, its speed set to 0 (obviously) and its gear set to 1. How will you do this?</p>
<p>When we talk about creating objects, we&#8217;re talking about the constructor, specifically. Since we didn&#8217;t explicitly make a constructor for the Car class in the last entry, let&#8217;s make one for Bob:</p>
<p><pre class="brush: java;">
public Car (String brand, String name) {
	setBrand(brand);
	setColor(&quot;White&quot;);
	setName(name);
}
</pre></p>
<p>What this constructor does is get the values of its brand and name argument and assign them to the object using its setter methods. If you looked at it closely, you&#8217;ll notice that we didn&#8217;t call the setter for speed and gear. Why? Take a look at <em>Car.java</em> again. We already assigned default values for the attributes, speed and gear.</p>
<p><pre class="brush: java;">
private int speed = 0;
private int gear = 1;
</pre></p>
<p>Every instance of Car will possess these assigned values by default when you create them.</p>
<p>If you still remember Bob&#8217;s requirements, you&#8217;ll see that it&#8217;s exactly what he wanted. If we go to <em>CarDemo.java</em> right now, we can now create objects in this fashion:</p>
<p><pre class="brush: java;">
Car bobsCar = new Car(&quot;Mitsubishi&quot;, &quot;Lancer EX&quot;);
</pre></p>
<p>Isn&#8217;t this convenient? We can now create objects and assign values to their attributes in a single line. If we want to change them, we can just invoke the setter methods and be done with it. This will now be our new <em>CarDemo.java</em>:</p>
<p><pre class="brush: java;">
public class CarDemo {
	public static void main(String[]args) {
		Car bobsCar = new Car(&quot;Mitsubishi&quot;, &quot;Lancer EX&quot;);

		System.out.println(&quot;The car is a &quot; + bobsCar.getColor() + &quot; &quot; + bobsCar.getBrand() + &quot; &quot; bobsCar.getName() + &quot;.&quot;);
		System.out.println(&quot;The car is runs at &quot; + bobsCar.getSpeed() + &quot; miles per hour on gear &quot; + bobsCar.getGear());
	}
}
</pre></p>
<p>This will be the output:</p>
<blockquote><p>The car is a White Mitsubishi Lancer EX.<br />
The car runs at 0 miles per hour on gear 1.</p></blockquote>
<p>Will you look at that? We shrunk that long 6-liner code into just one line. See how convenient code refactoring is?</p>
<p>We can still shrink the main method of <em>CarDemo.java</em> into just 2 lines. Look at the rest of the code, did you notice how long it is to write the code to print the values of the object to the screen? Well, not really, but what if you made a million objects and you have to print all of their values to the screen? Wouldn&#8217;t that be a nightmare?</p>
<p>If you find yourself typing the same code multiple times, you can turn all those repeated code into methods. That way, you only have to type the code once and just call it when you need it.</p>
<p>Let&#8217;s cut and paste our printing code to <em>Car.java</em>, put it inside a method, and tweak it a little bit so it would suit our needs:<br />
<pre class="brush: java;">
public void printInformation() {
	System.out.println(&quot;The car is a &quot; + getColor() + &quot; &quot; + getBrand() + &quot; &quot; getName() + &quot;.&quot;);
	System.out.println(&quot;The car is runs at &quot; + getSpeed() + &quot; miles per hour on gear &quot; + getGear());
}
</pre></p>
<p>I&#8217;m pretty sure you&#8217;ll have a good idea of how <em>CarDemo.java</em> would look right now, but I&#8217;ll still show it to you:</p>
<p><pre class="brush: java;">
public class CarDemo {
	public static void main(String[]args) {
		Car bobsCar = new Car(&quot;Mitsubishi&quot;, &quot;Lancer EX&quot;);

		bobsCar.printInformation();
	}
}
</pre></p>
<p>We shrunk it into 2 lines (Well, 3, actually, because of the whitespace)! It will still produce the same output as last time, but now it takes less effort to do the same thing. Isn&#8217;t code refactoring wonderful? Now you will be saved from getting screamed at by your co-workers.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/terencehacks.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/terencehacks.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/terencehacks.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/terencehacks.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/terencehacks.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/terencehacks.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/terencehacks.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/terencehacks.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/terencehacks.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/terencehacks.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/terencehacks.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/terencehacks.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/terencehacks.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/terencehacks.wordpress.com/152/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=terencehacks.wordpress.com&amp;blog=17292332&amp;post=152&amp;subd=terencehacks&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://terencehacks.wordpress.com/2010/12/15/code-refactoring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fdd669d69fc537eaadf826104075af96?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">eeyorexd</media:title>
		</media:content>
	</item>
	</channel>
</rss>
