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

<channel>
	<title>Software Passion &#187; Learning Materials</title>
	<atom:link href="http://www.softwarepassion.com/category/learning_materials/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.softwarepassion.com</link>
	<description>by Krzysztof Grajek</description>
	<lastBuildDate>Fri, 23 Dec 2011 07:48:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Java EE 6 Testing with Arquillian Persistence Extension</title>
		<link>http://www.softwarepassion.com/java-ee-6-testing-with-arquillian-persistence-extension/</link>
		<comments>http://www.softwarepassion.com/java-ee-6-testing-with-arquillian-persistence-extension/#comments</comments>
		<pubDate>Thu, 22 Dec 2011 07:42:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[General Programming]]></category>
		<category><![CDATA[Learning Materials]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=721</guid>
		<description><![CDATA[Today I will describe my newly discovered library which is an extension to the great JBoss Arquillian project. The mission of the Arquillian project is to provide a simple test harness that abstracts away all container lifecycle and deployment from the test logic so developers can easily produce a broad range of integration tests for [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>Today I will describe my newly discovered library which is an extension to the great <a href="http://www.jboss.org/arquillian">JBoss Arquillian</a> project.</p>
<blockquote><p>The mission of the Arquillian project is to provide a simple test harness that abstracts away all container lifecycle and deployment from the test logic so developers can easily produce a broad range of integration tests for their enterprise Java applications.</p></blockquote>
<p>Arquillian persistence extension allows you to test JPA related code without filling up the database with test data. All you have to do is to populate .yml file with your data specified. That will be best described with an example.</p>
<p>First of all add arquillian persistence dependencies to your project:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.jboss.arquillian.extension<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>arquillian-persistence-api<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${version.arquillian_persistence}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.jboss.arquillian.extension<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>arquillian-persistence-impl<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${version.arquillian_persistence}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>where &#8216;${version.arquillian_persistence}&#8217;  is equal to &#8217;1.0.0.Alpha2&#8242;. I assume that you have the basic arquillian tests (for EJB&#8217;s and CDI) working, If not read on.</p>
<p>I have found myself setting up arquillian enabled project quite cumbersome, always ending up with some classpath dependency problems while trying to remote test my classes. For those of you new to Arquillian I would strongly recommend starting up with working example provided by <a href="https://github.com/bartoszmajsak">Bartosz Majsak</a> and available on github as a <a href="https://github.com/bartoszmajsak/Beer-Advisor">&#8216;Beer-Advisor</a>&#8216; project. </p>
<p>The &#8216;Beer-Advisor&#8217; project can be quickly refactored to suit your needs, it lack comments but its so clearly written that it shouldn&#8217;t be a problem to grasp the idea.</p>
<p>Ok then, lets get going with the arquillian persistence extension, we are going to use two nice annotations to help us testing our JPA layer: @UsingDataSet and @ShouldMatchDataSet.</p>
<p>We start off with doing some setup, I will copy part of the example from the Beer-Advisor project which you can easily clone and try for yourself:</p>
<p>Having an entity class Beer:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aentity+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Entity</span></a><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Beer <span style="color: #000000; font-weight: bold;">implements</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aserializable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Serializable</span></a><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 5892013208071126314L<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;@Id<br />
&nbsp; &nbsp;@GeneratedValue<span style="color: #009900;">&#40;</span>strategy <span style="color: #339933;">=</span> GenerationType.<span style="color: #006633;">AUTO</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Long</span></a> id<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;@Basic<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;@Basic<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abigdecimal+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BigDecimal</span></a> price<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;@Basic<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abigdecimal+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BigDecimal</span></a> alcohol<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;@Basic<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> code<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;@Enumerated<span style="color: #009900;">&#40;</span>EnumType.<span style="color: #006633;">STRING</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> Type type<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// If lazy then Glassfish Embedded is facing this problem: https://bugs.eclipse.org/bugs/show_bug.cgi?id=323403</span><br />
&nbsp; &nbsp;@ManyToOne<span style="color: #009900;">&#40;</span>optional <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span>, cascade <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>CascadeType.<span style="color: #006633;">PERSIST</span>, CascadeType.<span style="color: #006633;">MERGE</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> Brewery brewery<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;Beer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// to satisfy JPA</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">public</span> Beer<span style="color: #009900;">&#40;</span>Brewery brewery, Type type, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abigdecimal+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BigDecimal</span></a> price, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abigdecimal+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BigDecimal</span></a> alcohol<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">price</span> <span style="color: #339933;">=</span> price<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">alcohol</span> <span style="color: #339933;">=</span> alcohol<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">type</span> <span style="color: #339933;">=</span> type<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">brewery</span> <span style="color: #339933;">=</span> brewery<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">brewery</span>.<span style="color: #006633;">addBeer</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
........ <span style="color: #006633;">Getters</span> and Setters ...............</div></div>
<p>and simple DAO for managing Beer objects on the persistence layer:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@RequestScoped<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JpaBeerRepository <span style="color: #000000; font-weight: bold;">implements</span> BeerRepository<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;@PersistenceContext<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> EntityManager em<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;@Override<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">public</span> Beer getById<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Long</span></a> id<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> em.<span style="color: #006633;">find</span><span style="color: #009900;">&#40;</span>Beer.<span style="color: #000000; font-weight: bold;">class</span>, id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
..........<br />
<br />
@Override<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">public</span> Set<span style="color: #339933;">&lt;</span>Beer<span style="color: #339933;">&gt;</span> fetchAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; CriteriaBuilder criteriaBuilder <span style="color: #339933;">=</span> em.<span style="color: #006633;">getCriteriaBuilder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; CriteriaQuery<span style="color: #339933;">&lt;</span>Beer<span style="color: #339933;">&gt;</span> query <span style="color: #339933;">=</span> criteriaBuilder.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>Beer.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; Root<span style="color: #339933;">&lt;</span>Beer<span style="color: #339933;">&gt;</span> from <span style="color: #339933;">=</span> query.<span style="color: #006633;">from</span><span style="color: #009900;">&#40;</span>Beer.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; CriteriaQuery<span style="color: #339933;">&lt;</span>Beer<span style="color: #339933;">&gt;</span> select <span style="color: #339933;">=</span> query.<span style="color: #006633;">select</span><span style="color: #009900;">&#40;</span>from<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; Set<span style="color: #339933;">&lt;</span>Beer<span style="color: #339933;">&gt;</span> result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>Beer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; result.<span style="color: #006633;">addAll</span><span style="color: #009900;">&#40;</span>em.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>select<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResultList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp;@Override<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> save<span style="color: #009900;">&#40;</span>Beer beer<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>beer.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;em.<span style="color: #006633;">persist</span><span style="color: #009900;">&#40;</span>beer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;em.<span style="color: #006633;">merge</span><span style="color: #009900;">&#40;</span>beer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
..................</div></div>
<p>We can easily test our DAO by creating datasets in yml format and test our logic against it:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Beer:<br />
&nbsp; - id: <span style="color: #000000;">1</span><br />
&nbsp; &nbsp; alcohol: <span style="color: #000000;">4.5</span><br />
&nbsp; &nbsp; name: <span style="color: #ff0000;">&quot;Mocny Full&quot;</span><br />
&nbsp; &nbsp; price: <span style="color: #000000;">1.0</span><br />
&nbsp; &nbsp; type: LAGER<br />
&nbsp; &nbsp; brewery_id: <span style="color: #000000;">1</span><br />
&nbsp; - id: <span style="color: #000000;">2</span><br />
&nbsp; &nbsp; alcohol: <span style="color: #000000;">55.0</span><br />
&nbsp; &nbsp; name: <span style="color: #ff0000;">&quot;End of history&quot;</span><br />
&nbsp; &nbsp; price: <span style="color: #000000;">765</span><br />
&nbsp; &nbsp; type: BLOND_ALE<br />
&nbsp; &nbsp; brewery_id: <span style="color: #000000;">2</span><br />
&nbsp; - id: <span style="color: #000000;">3</span><br />
&nbsp; &nbsp; alcohol: <span style="color: #000000;">41.0</span><br />
&nbsp; &nbsp; name: <span style="color: #ff0000;">&quot;Sink The Bismarck!&quot;</span><br />
&nbsp; &nbsp; price: <span style="color: #000000;">64.0</span><br />
&nbsp; &nbsp; type: QUADRUPEL_IPA<br />
&nbsp; &nbsp; brewery_id: <span style="color: #000000;">2</span><br />
&nbsp; - id: <span style="color: #000000;">4</span><br />
&nbsp; &nbsp; alcohol: <span style="color: #000000;">8.5</span><br />
&nbsp; &nbsp; name: <span style="color: #ff0000;">&quot;Delirium Tremens&quot;</span><br />
&nbsp; &nbsp; price: <span style="color: #000000;">10.0</span><br />
&nbsp; &nbsp; type: PALE_ALE<br />
&nbsp; &nbsp; brewery_id: <span style="color: #000000;">3</span><br />
&nbsp; - id: <span style="color: #000000;">5</span><br />
&nbsp; &nbsp; alcohol: <span style="color: #000000;">8.4</span><br />
&nbsp; &nbsp; name: <span style="color: #ff0000;">&quot;Pauwel Kwak&quot;</span><br />
&nbsp; &nbsp; price: <span style="color: #000000;">4.0</span><br />
&nbsp; &nbsp; type: AMBER<br />
&nbsp; &nbsp; brewery_id: <span style="color: #000000;">4</span></div></div>
<p>You should put your .yml file into test/resources/datasets folder in you maven project</p>
<p>@UsingDataSet annotation will populate our database with the data specified in beers.yml file</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp;@Test<br />
&nbsp; &nbsp;@UsingDataSet<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;beers.yml&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> shouldReturnBeerByItsId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a><br />
&nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// given</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Long</span></a> beerId <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> expectedName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Mocny Full&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// when</span><br />
&nbsp; &nbsp; &nbsp; Beer beers <span style="color: #339933;">=</span> beerRepository.<span style="color: #006633;">getById</span><span style="color: #009900;">&#40;</span>beerId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// then</span><br />
&nbsp; &nbsp; &nbsp; assertThat<span style="color: #009900;">&#40;</span>beers.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isEqualTo</span><span style="color: #009900;">&#40;</span>expectedName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span></div></div>
<p>Now, lets say we want to add new Beer and see if the database will have a specific data in it, for this purpose we are going to use @ShouldMatchDataSet(..)</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@Test<br />
&nbsp; &nbsp; @ShouldMatchDataSet<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;expected-insert-beer.yml&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> shouldSaveNewBeer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//when</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Beer b <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Beer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//set the necessary fields on the beer object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; beerRepository.<span style="color: #006633;">save</span><span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertThat<span style="color: #009900;">&#40;</span>beerRepository<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isNotNull</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>after inserting our Beer object to the database through the EntityManager we should end up with a single beer record in our database, exactly like the one specified in &#8216;expected-insert-beer.yml&#8217;.</p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/java-ee-6-testing-with-arquillian-persistence-extension/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Oracle Certified Master, Java EE 5 Enterprise Architect</title>
		<link>http://www.softwarepassion.com/oracle-certified-master-java-ee-5-enterprise-architect/</link>
		<comments>http://www.softwarepassion.com/oracle-certified-master-java-ee-5-enterprise-architect/#comments</comments>
		<pubDate>Fri, 16 Dec 2011 13:07:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Learning Materials]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=711</guid>
		<description><![CDATA[Today I have received very long awaited email from Oracle: Congratulations! You have passed the Oracle Certified Master, Java EE 5 Enterprise Architect certification. You are among the elite 1% of certified Java professionals who have gone on to achieve the Java Enterprise Architect certification. I was waiting for the result for almost 4 months [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>Today I have received very long awaited email from Oracle:</p>
<blockquote><p>
Congratulations! You have passed the Oracle Certified Master, Java EE 5 Enterprise Architect certification. You are among the elite 1% of certified Java professionals who have gone on to achieve the Java Enterprise Architect certification.
</p></blockquote>
<p>I was waiting for the result for almost 4 months since I&#8217;ve finished the last part of the certification process and started to fear that I won&#8217;t pass while reading on JavaRanch about problems some people had with the diagrams.</p>
<p>Anyway, now I know what it is to be a certified architect <img src='http://www.softwarepassion.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  </p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/oracle-certified-master-java-ee-5-enterprise-architect/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Why you should join Stackoverflow</title>
		<link>http://www.softwarepassion.com/why-you-should-join-stackoverflow/</link>
		<comments>http://www.softwarepassion.com/why-you-should-join-stackoverflow/#comments</comments>
		<pubDate>Tue, 04 Oct 2011 19:09:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[General Programming]]></category>
		<category><![CDATA[Learning Materials]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=646</guid>
		<description><![CDATA[Back in May 2008 when I first heard about stackoverflow idea I was really skeptical about it. You can read my rants about the whole idea here. Since then, I have used stackoverflow more and more and I have to say that for the last couple of months I actually love it.This was a long [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>Back in May 2008 when I first heard about stackoverflow idea I was really skeptical about it. You can read my rants about the whole idea <a title="Why do we really need Stackoverflow" href="http://www.softwarepassion.com/do-we-really-need-stackoverflow/" target="_blank">here</a>. Since then, I have used stackoverflow more and more and I have to say that for the last couple of months I actually love it.This was a long process for me but I can assure you, that this post is going to be completely different the the one written in the middle of 2008.<br />
Once I started contributing and become a part of stackoverflow community I have discovered quite a few things about me and about the website itself.</p>
<p style="text-align: center;"><a title="Stackoverflow" href="http://stackoverflow.com/" target="_blank"><img class="aligncenter size-full wp-image-647" title="stackoverflow-logo" src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/stackoverflow-logo.png" alt="" width="250" height="70" /></a></p>
<p style="text-align: left;">I think many developers agree, that if you are into programming you are willing to learn all the time, learning is fun and if you have a source of learning repeatedly every day something new, you will be a happy developer. Of course many of us read books, blogs, community sites like reddit, dzone or digg, looking for something cool to read every day. Many of us do certifications just for fun, just to get some feeling of knowledge gaining. For me Stackoverflow is becoming one of the major sources of self development which I can do at my own peace.</p>
<p>Possibly, every person will find something different for himself at stackoverflow, and it all depends on our personal preferences and goals. If you are reading this and you still don&#8217;t use stackoverflow much, I urge you to try at least for a month.<strong> </strong></p>
<h3><strong>1. You can learn by teaching</strong></h3>
<p style="text-align: center;"><strong><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/learning.jpg" rel="lightbox[646]"><img class="aligncenter size-medium wp-image-648" title="learning" src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/learning-300x262.jpg" alt="" width="300" height="262" /></a><br />
</strong></p>
<p><strong> </strong>Everyone who has ever thought about improving his own learning efficiency knows this. When you try to teach what you have just learned, everything auto-magically get nicely organized in your head. All you have to do is to find a way to share your knowledge &#8211; now you can, use a stackoverflow of course.</p>
<p>You can read more about effective learning <a href="http://psychology.about.com/od/educationalpsychology/tp/effective-learning.htm">here</a>.</p>
<h3><strong>2. You can collect stuff</strong></h3>
<p><strong><br />
</strong></p>
<p>
<iframe frameborder="0" width="480" height="360" src="http://www.dailymotion.com/embed/video/xcbtqd"></iframe><br /><a href="http://www.dailymotion.com/video/xcbtqd_turbo-bubble-gum-pictures_auto" target="_blank">Turbo bubble gum pictures</a> <i> by <a href="http://www.dailymotion.com/KnightRiderDDR" target="_blank">KnightRiderDDR</a></i>
</p>
<p><em><br />
</em></p>
<p>The first and last thing in my life I was actually collecting, was a chewing gum stickers in the middle eighties, they were called <a href="http://www.dailymotion.com/video/xcbtqd_turbo-bubble-gum-pictures_auto">&#8216;Turbo&#8217;</a> and most of the people reading this won&#8217;t probably know them, as they were sold in eastern europe only(I can be wrong), it was fun though, but now I have stackoverflow <img src='http://www.softwarepassion.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .<br />
For all the people collecting all kind of stuff, well, by using a stackoverflow you can fullfill your desire and collect something new &#8211; stackoverflow points and different kinds of badges</p>
<h3><strong>3. You can compete</strong></h3>
<p>People like competition of many kinds and they do it for whatever reason. Whatever reason you have, you can now compete on stackoverflow when providing better answers than others, collecting more points etc.<br />
Remember, competition is a driving force of progress.</p>
<h3><strong>4. You can revise your knowledge</strong></h3>
<p>This is for me actually the most important point. Remember &#8220;forgetting curve&#8221;? No? Well, its because you have probably not revised that information and you have forgotten <img src='http://www.softwarepassion.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
This is simple, if you don&#8217;t repeat what you have learned you will finally forget it. By using a stackoverflow you can highly minimize or prevent totally your memory loss.</p>
<p style="text-align: center;"><a href="http://en.wikipedia.org/wiki/Forgetting_curve"><img class="aligncenter" title="Forgetting Curve - Wikipedia" src="http://upload.wikimedia.org/wikipedia/commons/4/4e/ForgettingCurve.svg" alt="Forgetting Curve - Wikipedia" width="277" height="237" /></a></p>
<h3><strong>5. You can expand your knowledge</strong></h3>
<p>If you&#8217;ll jump into a stackoverflow mania you will spend there a bit more time than just the time answering/asking questions. You will probably be  browsing the web app, reading notification of posted comments to your questions/answers, at the same time you notice other people answers, you&#8217;ll often find out a better approach of doing something than your own answer gave, links to an on-line articles, documentation, other interesting questions in your field of expertise etc, you&#8217;ll just find the best of the web posted by other people in one place, all related to whats the most interesting for you.</p>
<h3><strong>6. You can ask questions yourself</strong></h3>
<p>I almost forgot about this one. You can of course ask questions there and if they are not too difficult someone will probably answer them pretty fast <img src='http://www.softwarepassion.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3><strong>7. You can join Careers 2.0</strong></h3>
<p>I&#8217;ve just joined<a href="http://careers.stackoverflow.com/"> Careers 2.0</a> and I can say that it&#8217;s well done web app, unfortunately there is not a single company from Poland <img src='http://www.softwarepassion.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Anyway I guess it can be useful for others (especially in the US + UK state)</p>
<h3><strong>8. You can meet people at Stackoverflow meet-up days</strong></h3>
<p><strong><br />
</strong></p>
<p style="text-align: center;"><strong><a href="http://blog.stackoverflow.com/2011/03/stack-overflow-meetups-april-6/" target="_blank"><img class="aligncenter size-full wp-image-653" title="220x250_Meetups_Banner" src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/220x250_Meetups_Banner.jpg" alt="" width="220" height="250" /></a><br />
</strong></p>
<p>Haven&#8217;t tried that yet but It&#8217;s always good to have a good excuse to go for a beer, or isn&#8217;t it?</p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/why-you-should-join-stackoverflow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting started with Drools Flow</title>
		<link>http://www.softwarepassion.com/getting-started-with-drools-flow/</link>
		<comments>http://www.softwarepassion.com/getting-started-with-drools-flow/#comments</comments>
		<pubDate>Thu, 10 Feb 2011 12:07:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Jboss Drools]]></category>
		<category><![CDATA[Learning Materials]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=548</guid>
		<description><![CDATA[Today I will present my latest discovery called JBoss Drools and Drools Flow in particular. I am completely new to the drools framework and because I couldn&#8217;t find myself a quick and simple intro to the framework I will post it myself. Presented example is completely useless from the business perspective and it doesn&#8217;t describe [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>Today I will present my latest discovery called JBoss Drools and Drools Flow in particular. I am completely new to the drools framework and because I couldn&#8217;t find myself a quick and simple intro to the framework I will post it myself. Presented example is completely useless from the business perspective and it doesn&#8217;t describe any real world scenario.</p>
<p>According to the Drools Flow website itself:</p>
<blockquote><p>Drools Flow provides workflow or (business) process capabilities to the Drools platform. A business process or workflow describes the order in which a series of steps need to be executed, using a flow chart. This makes it much easier to describe a complex composition of various tasks. Processes are especially useful in describing state-based, long-running processes. Drools Flow allows end users to specify, execute and monitor (a part of) their business logic using these processes. The Drools Flow process framework is easily embeddable into any Java application (as a simple Java component) or can run standalone in a server environment.</p></blockquote>
<p>First of all you need to prepare your development environment. You will need the following:</p>
<ul>
<li>Eclipse IDE</li>
<li><a href="http://download.jboss.org/drools/release/5.1.1.34858.FINAL/drools-5.1.1-bin.zip">Drools Binaries v.5.1</a></li>
<li><a href="http://download.jboss.org/drools/release/5.1.1.34858.FINAL/drools-5.1.1-eclipse-all.zip">Drools Eclipse 3.5 Workbench</a></li>
</ul>
<p>For the purpose of this tutorial that&#8217;s enough, you can try to download full Drools stack using the installer (including JBoss AS, Eclipse IDE, Birt reporting, Guvnor etc) but I didn&#8217;t have any luck with this installer and I guess you will be better off once you do install everything by hand as you learn.</p>
<p>Unpack drools binaries somewhere on your machine and Drools Eclipse workbench into eclipse folder. Restart your IDE and you should be able to create new Drools projects by now.</p>
<p>Select File -> New Project from the Eclipse file menu:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_1.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_1-261x300.png" alt="" title="drools_1" width="261" height="300" class="aligncenter size-medium wp-image-551" /></a></p>
<p>Provide descriptive name for your project:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_2.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_2-287x300.png" alt="" title="drools_2" width="287" height="300" class="aligncenter size-medium wp-image-552" /></a></p>
<p>Un-click all the proposed examples which eclipse plugin wants to generate for you, you can do that later if you want to learn more on drools.</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_3.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_3-300x291.png" alt="" title="drools_3" width="300" height="291" class="aligncenter size-medium wp-image-553" /></a></p>
<p>Select Drools runtime, if you have no runtime configured, click &#8216;Configure Workspace settings&#8217; and point to the directory where you have downloaded your binaries.</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_4.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_4-300x291.png" alt="" title="drools_4" width="300" height="291" class="aligncenter size-medium wp-image-554" /></a></p>
<p>After you click finish, you should have a basic Drools project structure created for you:<br />
<span id="more-548"></span><br />
<a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_5.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_5-300x97.png" alt="" title="drools_5" width="300" height="97" class="aligncenter size-medium wp-image-555" /></a></p>
<p>Let&#8217;s create our application entry point with the main method we can execute to test our flow.</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_6.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_6-256x300.png" alt="" title="drools_6" width="256" height="300" class="aligncenter size-medium wp-image-556" /></a></p>
<p>You should end up with a simple java class and single main method:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.softwarepassion.droolsflow</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SPDroolsFlowExample <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* @param args<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Now lets create our Flow file for the Drools Flow engine, select New -> Other on the project node and under the Drools folder select &#8216;Flow File&#8217;:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_7.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_7-300x287.png" alt="" title="drools_7" width="300" height="287" class="aligncenter size-medium wp-image-557" /></a></p>
<p>Select the name for your flow file:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_8.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_8-270x300.png" alt="" title="drools_8" width="270" height="300" class="aligncenter size-medium wp-image-558" /></a></p>
<p>The last step when creating your flow file is to select the runtime compatibility with the drools engine:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_9.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_9-286x300.png" alt="" title="drools_9" width="286" height="300" class="aligncenter size-medium wp-image-559" /></a></p>
<p>Once your flow file is created make sure its under src/main/rules while in the package explorer window, this will ensure that we can find it easily on our class-path while executing our test application.<br />
Make sure you are in the &#8216;Drools Perspective&#8217;, open the rules file (*.rf) and drop the following onto the designer window:<br />
Assuming you have a start event already placed in your flow (green cirle)</p>
<ol>
<li>1 Gateway [diverge]</li>
<li>3 Logs (found under &#8216;service tasks&#8217;)</li>
<li>1 Gateway [converge]</li>
<li>1 End event</li>
</ol>
<p>Once you do that, place them on the screen similary to this screenshot below and connect all together using &#8216;Sequence Flow&#8217; arrow found at the top of the flow toolbox.</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_11.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_11-300x159.png" alt="" title="drools_11" width="300" height="159" class="aligncenter size-medium wp-image-560" /></a></p>
<p>When you have your diagram ready, click on the whitespace anywhere on the diagram designer and under the properties tab provide the ID attribute, for this example we use &#8216;com.softwarepassion.droolsflowexample&#8217;:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_12_new.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_12_new-300x87.png" alt="" title="drools_12_new" width="300" height="87" class="aligncenter size-medium wp-image-572" /></a></p>
<p>Now on the designer select the first Gateway (diverge) and under properties tab set its type to &#8216;OR&#8217;:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_13.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_13-300x152.png" alt="" title="drools_13" width="300" height="152" class="aligncenter size-medium wp-image-562" /></a></p>
<p>For the second Gateway provide the type &#8216;XOR&#8217;, this ensures that we have to get the flow only from one of the nodes to proceed, selecting &#8216;AND&#8217; for the second gateway would require two branches to be executed but we want to execute just one, either top or bottom log.</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_14.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_14-300x235.png" alt="" title="drools_14" width="300" height="235" class="aligncenter size-medium wp-image-563" /></a></p>
<p>Once you select OR for the first gateway you can set constraints describing what exactly has to happen to go to the top branch or the bottom branch.</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_15.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_15-300x103.png" alt="" title="drools_15" width="300" height="103" class="aligncenter size-medium wp-image-564" /></a></p>
<p>Click &#8216;&#8230;&#8217; on the Constraints row (inside properties tab):</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_16.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_16-300x158.png" alt="" title="drools_16" width="300" height="158" class="aligncenter size-medium wp-image-565" /></a></p>
<p>Select &#8216;Edit&#8217; button for the top branch (&#8216;To node Log Top&#8217;), change its type to &#8216;java&#8217;, dialect to &#8216;java&#8217; and type in:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span></div></div>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_17.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_17-300x223.png" alt="" title="drools_17" width="300" height="223" class="aligncenter size-medium wp-image-566" /></a></p>
<p>Similarly, for the other branch set the condition to return false:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_18.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_18-300x227.png" alt="" title="drools_18" width="300" height="227" class="aligncenter size-medium wp-image-567" /></a></p>
<p>Hardcoding &#8216;true&#8217; and &#8216;false&#8217; values is just for now, we will learn later on, how to insert some object into the flow and read its state inside a flow task.</p>
<p>Just for debugging purposes provide descriptive messages to our Log events, you can do that either from the Properties tab or by double clicking the node on the diagram itself.<br />
Because we are using Log events all of them have the name &#8216;Log&#8217;.</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_19.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_19-300x201.png" alt="" title="drools_19" width="300" height="201" class="aligncenter size-medium wp-image-568" /></a></p>
<p>Now lets do some wiring. Create a simple handler for our work items, &#8216;executeWorkItem&#8217; method will be called every time the &#8216;Log&#8217; task is executed within our workflow.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.softwarepassion.droolsflow</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.drools.runtime.process.WorkItem</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.drools.runtime.process.WorkItemHandler</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.drools.runtime.process.WorkItemManager</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SimpleWorkItemHandler <span style="color: #000000; font-weight: bold;">implements</span> WorkItemHandler<span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> executeWorkItem<span style="color: #009900;">&#40;</span>WorkItem workItem, WorkItemManager manager<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Executing work item &quot;</span> <span style="color: #339933;">+</span> workItem<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; manager.<span style="color: #006633;">completeWorkItem</span><span style="color: #009900;">&#40;</span>workItem.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> abortWorkItem<span style="color: #009900;">&#40;</span>WorkItem workItem, WorkItemManager manager<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Do nothing here</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Wiring it all together, we end up with the following code in our main class:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.softwarepassion.droolsflow</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.drools.KnowledgeBase</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.drools.builder.KnowledgeBuilder</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.drools.builder.KnowledgeBuilderFactory</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.drools.builder.ResourceType</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.drools.io.ResourceFactory</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.drools.runtime.StatefulKnowledgeSession</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SPDroolsFlowExample <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* @param args<br />
&nbsp; &nbsp; &nbsp;* @throws Exception <br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; KnowledgeBase knowledgeBase <span style="color: #339933;">=</span> readKnowledgeBase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; StatefulKnowledgeSession ksession <span style="color: #339933;">=</span> knowledgeBase.<span style="color: #006633;">newStatefulKnowledgeSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; SimpleWorkItemHandler handler <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleWorkItemHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; ksession.<span style="color: #006633;">getWorkItemManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">registerWorkItemHandler</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Log&quot;</span>, handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; ksession.<span style="color: #006633;">startProcess</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.softwarepassion.droolsflowexample&quot;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ksession.<span style="color: #006633;">fireAllRules</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> KnowledgeBase readKnowledgeBase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; KnowledgeBuilder kbuilder <span style="color: #339933;">=</span> KnowledgeBuilderFactory.<span style="color: #006633;">newKnowledgeBuilder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; kbuilder.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ResourceFactory.<span style="color: #006633;">newClassPathResource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;droolsflowexample.rf&quot;</span><span style="color: #009900;">&#41;</span>, ResourceType.<span style="color: #006633;">DRF</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> kbuilder.<span style="color: #006633;">newKnowledgeBase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Click on the class and select &#8216;Run  as..&#8217; -> &#8216;Java Application&#8217;, this will should print something similar to the following messages:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_20.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_20-300x53.png" alt="" title="drools_20" width="300" height="53" class="aligncenter size-medium wp-image-569" /></a></p>
<p>Source for the flow file at this stage of the tutorial is listed below:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #339933;">?&gt;</span> <br />
<span style="color: #339933;">&lt;</span>process xmlns<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://drools.org/drools-5.0/process&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xmlns<span style="color: #339933;">:</span>xs<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xs<span style="color: #339933;">:</span>schemaLocation<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://drools.org/drools-5.0/process drools-processes-5.0.xsd&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;RuleFlow&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;flow&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.softwarepassion.droolsflowexample&quot;</span> package<span style="color: #339933;">-</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.softwarepassion&quot;</span> <span style="color: #339933;">&gt;</span><br />
<br />
&nbsp; <span style="color: #339933;">&lt;</span>header<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;/</span>header<span style="color: #339933;">&gt;</span><br />
<br />
&nbsp; <span style="color: #339933;">&lt;</span>nodes<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>start id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Start&quot;</span> x<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;72&quot;</span> y<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;206&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;48&quot;</span> height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;48&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>end id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;7&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;End&quot;</span> x<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;802&quot;</span> y<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;196&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;48&quot;</span> height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;48&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>split id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;8&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Gateway&quot;</span> x<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;203&quot;</span> y<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;205&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;49&quot;</span> height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;49&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;3&quot;</span> <span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>constraints<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>constraint toNodeId<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;10&quot;</span> toType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;DROOLS_DEFAULT&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;constraint&quot;</span> priority<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;code&quot;</span> dialect<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java&quot;</span> <span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;&lt;/</span>constraint<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>constraint toNodeId<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;11&quot;</span> toType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;DROOLS_DEFAULT&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;constraint&quot;</span> priority<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;code&quot;</span> dialect<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java&quot;</span> <span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;&lt;/</span>constraint<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>constraints<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>split<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>join id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;9&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Gateway&quot;</span> x<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;537&quot;</span> y<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;197&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;49&quot;</span> height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;49&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;2&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>workItem id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;10&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Log Top&quot;</span> x<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;352&quot;</span> y<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;157&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;100&quot;</span> height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;48&quot;</span> <span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>work name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Log&quot;</span> <span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>parameter name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Message&quot;</span> <span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>type name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;org.drools.process.core.datatype.impl.type.StringDataType&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>value<span style="color: #339933;">&gt;</span>Message from log top...<span style="color: #339933;">&lt;/</span>value<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>parameter<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>work<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>workItem<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>workItem id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;11&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Log Bottom&quot;</span> x<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;354&quot;</span> y<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;251&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;100&quot;</span> height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;48&quot;</span> <span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>work name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Log&quot;</span> <span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>parameter name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Message&quot;</span> <span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>type name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;org.drools.process.core.datatype.impl.type.StringDataType&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>value<span style="color: #339933;">&gt;</span>Message from log bottom..<span style="color: #339933;">&lt;/</span>value<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>parameter<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>work<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>workItem<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>workItem id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;12&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Log End&quot;</span> x<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;657&quot;</span> y<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;196&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;100&quot;</span> height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;48&quot;</span> <span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>work name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Log&quot;</span> <span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>parameter name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Message&quot;</span> <span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>type name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;org.drools.process.core.datatype.impl.type.StringDataType&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>value<span style="color: #339933;">&gt;</span>Message from log end<span style="color: #339933;">&lt;/</span>value<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>parameter<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>work<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>workItem<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;/</span>nodes<span style="color: #339933;">&gt;</span><br />
<br />
&nbsp; <span style="color: #339933;">&lt;</span>connections<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>connection from<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;12&quot;</span> to<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;7&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>connection from<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span> to<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;8&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>connection from<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;11&quot;</span> to<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;9&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>connection from<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;10&quot;</span> to<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;9&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>connection from<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;8&quot;</span> to<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;10&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>connection from<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;8&quot;</span> to<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;11&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>connection from<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;9&quot;</span> to<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;12&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;/</span>connections<span style="color: #339933;">&gt;</span><br />
<br />
<span style="color: #339933;">&lt;/</span>process<span style="color: #339933;">&gt;</span></div></div>
<p>If all is fine and working we can continue to the next part of our tutorial showing you how to add data to our workflow which can be read/modified by the workflow tasks.<br />
For this example we will create a very simple POJO which we will pass from one task to the other and based on the properties contained within that POJO we will direct our flow to one of the two branches created earlier in the tutorial.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.softwarepassion.droolsflow</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SimpleTO <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> msg<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getMsg<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> msg<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setMsg<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">msg</span> <span style="color: #339933;">=</span> msg<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>As you can see this is really simple java class <img src='http://www.softwarepassion.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Now let&#8217;s create an instance of it and add it to the workflow parameters, inside our class with the main method add the following:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; KnowledgeBase knowledgeBase <span style="color: #339933;">=</span> readKnowledgeBase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; StatefulKnowledgeSession ksession <span style="color: #339933;">=</span> knowledgeBase.<span style="color: #006633;">newStatefulKnowledgeSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; Map<span style="color: #339933;">&lt;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>, Object<span style="color: #339933;">&gt;</span> parameterMap <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>, Object<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SimpleTO st <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleTO<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; st.<span style="color: #006633;">setMsg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;BOTTOM&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; parameterMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;st&quot;</span>, st<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; SimpleWorkItemHandler handler <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleWorkItemHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; ksession.<span style="color: #006633;">getWorkItemManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">registerWorkItemHandler</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Log&quot;</span>, handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; ksession.<span style="color: #006633;">startProcess</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.softwarepassion.droolsflowexample&quot;</span>, parameterMap<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ksession.<span style="color: #006633;">fireAllRules</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>Setting the &#8216;msg&#8217; field to &#8216;TOP&#8217; will cause to execute our top branch and &#8216;BOTTOM&#8217; our bottom branch respectively.</p>
<p>For all this to work we need to add a variable to our workflow and change our constraints in the first gateway.</p>
<p>Click somwhere on the workflow to get the properties, select the &#8216;variables&#8217; row and add a single variable called &#8216;st&#8217;:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_22.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_22-300x180.png" alt="" title="drools_22" width="300" height="180" class="aligncenter size-medium wp-image-577" /></a></p>
<p>Note that we have added our object using the key with the same name as the variable we are defining above. Once our variable is ready to be used in the workflow we can reference it in the Constraints dialogs of our diverging gateway:</p>
<p>Top constraint:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_23.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_23-300x225.png" alt="" title="drools_23" width="300" height="225" class="aligncenter size-medium wp-image-578" /></a></p>
<p>Bottom constraint:</p>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_24.png" rel="lightbox[548]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/drools_24-300x225.png" alt="" title="drools_24" width="300" height="225" class="aligncenter size-medium wp-image-579" /></a></p>
<p>Now you can test the application, changing the property of our injected object will cause going into one of the branches. </p>
<p>I hope it helps anyone trying to get started with the drools flow.</p>
<p>Enjoy!</p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/getting-started-with-drools-flow/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Design Patterns Series &#8211; Part One: Strategy Pattern</title>
		<link>http://www.softwarepassion.com/design-patterns-series-part-one-strategy-pattern/</link>
		<comments>http://www.softwarepassion.com/design-patterns-series-part-one-strategy-pattern/#comments</comments>
		<pubDate>Fri, 28 Jan 2011 21:04:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[General Programming]]></category>
		<category><![CDATA[Learning Materials]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=439</guid>
		<description><![CDATA[The Strategy Pattern is a first pattern described in a series of post I&#8217;m planning to make regarding this topic. The Strategy pattern allows us to select a specific algorithm at runtime and its one of the simples design patterns to grasp. The scenario described on the diagram below shows a simple usage of the [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>The Strategy Pattern is a first pattern described in a series of post I&#8217;m planning to make regarding this topic. The Strategy pattern allows us to select a specific algorithm at runtime and its one of the simples design patterns to grasp.<br />
The scenario described on the diagram below shows a simple usage of the strategy pattern.<br />
<a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/strategy_pattern.png" rel="lightbox[439]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/strategy_pattern.png" alt="" title="strategy_pattern" width="799" height="380" class="aligncenter size-full wp-image-441" /></a></p>
<p>Let&#8217;s say we have a HttpServlet which creates a report using data retrieved from the request parameters, or some other sources like database. and depending on which kind of report we want to create (PDF, DOC or HTML) chooses appropriate algorithm to do just that.<br />
So w end up with common interface for creating reports called &#8216;ReportGenerator&#8217; with a single method &#8216;generateReport(ReportData rd)&#8217;:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/*<br />
&nbsp;* Created by softwarepassion.com<br />
&nbsp;* Any information regarding license for this code snippets and<br />
&nbsp;* other copyright info can be found @softwarepassion.com<br />
&nbsp;* The author doesn't take any reposnsibility for the presented<br />
&nbsp;* code and design patterns solutions.<br />
&nbsp;* Please be advised that this code can contain errors!<br />
&nbsp;* <br />
&nbsp;*/</span><br />
<br />
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">strategydesignpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Common interface for generating reports using different formats<br />
&nbsp;* <br />
&nbsp;* @author Krzysztof Grajek<br />
&nbsp;* @version 28-01-2011<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> ReportGenerator <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Generates report using provided report data<br />
&nbsp; &nbsp; &nbsp;* @param rd ReportData object carrying all info needed to produce report<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> generateReport<span style="color: #009900;">&#40;</span>ReportData rd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>We have three different implementations of this interface, allowing us to generate report in a form of PDF, DOC or HTML format.<br />
PDF Report Generator:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/*<br />
&nbsp;* Created by softwarepassion.com<br />
&nbsp;* Any information regarding license for this code snippets and<br />
&nbsp;* other copyright info can be found @softwarepassion.com<br />
&nbsp;* The author doesn't take any reposnsibility for the presented<br />
&nbsp;* code and design patterns solutions.<br />
&nbsp;* Please be advised that this code can contain errors!<br />
&nbsp;*<br />
&nbsp;*/</span><br />
<br />
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">strategydesignpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Responsible for handling PDF reports<br />
&nbsp;* <br />
&nbsp;* @author Krzysztof Grajek<br />
&nbsp;* @version 28-01-2011<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PDFReportGenerator <span style="color: #000000; font-weight: bold;">implements</span> ReportGenerator <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> generateReport<span style="color: #009900;">&#40;</span>ReportData rd<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Generating PDF report...&quot;</span> <span style="color: #339933;">+</span> rd.<span style="color: #006633;">getTitle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Microsoft Word Generator:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/*<br />
&nbsp;* Created by softwarepassion.com<br />
&nbsp;* Any information regarding license for this code snippets and<br />
&nbsp;* other copyright info can be found @softwarepassion.com<br />
&nbsp;* The author doesn't take any reposnsibility for the presented<br />
&nbsp;* code and design patterns solutions.<br />
&nbsp;* Please be advised that this code can contain errors!<br />
&nbsp;*<br />
&nbsp;*/</span><br />
<br />
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">strategydesignpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Responsible for handling reports in MS Word format<br />
&nbsp;* <br />
&nbsp;* @author Krzysztof Grajek<br />
&nbsp;* @version 28-01-2011<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MsDOCReportGenerator <span style="color: #000000; font-weight: bold;">implements</span> ReportGenerator<span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> generateReport<span style="color: #009900;">&#40;</span>ReportData rd<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Generating MS Word report.. &quot;</span> <span style="color: #339933;">+</span> rd.<span style="color: #006633;">getTitle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>HTML Report Generator:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/*<br />
&nbsp;* Created by softwarepassion.com<br />
&nbsp;* Any information regarding license for this code snippets and<br />
&nbsp;* other copyright info can be found @softwarepassion.com<br />
&nbsp;* The author doesn't take any reposnsibility for the presented<br />
&nbsp;* code and design patterns solutions.<br />
&nbsp;* Please be advised that this code can contain errors!<br />
&nbsp;*<br />
&nbsp;*/</span><br />
<br />
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">strategydesignpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Responsible for generating reports in HTML format<br />
&nbsp;*<br />
&nbsp;* @author Krzysztof Grajek<br />
&nbsp;* @version 28-01-2011<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HTMLReportGenerator <span style="color: #000000; font-weight: bold;">implements</span> ReportGenerator<span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> generateReport<span style="color: #009900;">&#40;</span>ReportData rd<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Generating HTML report...&quot;</span> <span style="color: #339933;">+</span> rd.<span style="color: #006633;">getTitle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>All mentioned above generators use a single object (simple POJO class) containing the data needed to generate report ReportData</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/*<br />
&nbsp;* Created by softwarepassion.com<br />
&nbsp;* Any information regarding license for this code snippets and<br />
&nbsp;* other copyright info can be found @softwarepassion.com<br />
&nbsp;* The author doesn't take any reposnsibility for the presented<br />
&nbsp;* code and design patterns solutions.<br />
&nbsp;* Please be advised that this code can contain errors!<br />
&nbsp;*/</span><br />
<br />
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">strategydesignpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Used to transport data needed to produce report<br />
&nbsp;* in any form using {@link ReportGenerator}<br />
&nbsp;* <br />
&nbsp;* @author Krzysztof Grajek<br />
&nbsp;* @version 28-01-2011<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ReportData <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> title<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> description<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//...other report related stuff needed to produce report</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//..getters and setters for the class fields</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getDescription<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> description<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDescription<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> description<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">description</span> <span style="color: #339933;">=</span> description<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getTitle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> title<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setTitle<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> title<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">title</span> <span style="color: #339933;">=</span> title<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #009900;">&#125;</span></div></div>
<p>All of different kinds of generators can be used in our simple report servlet:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/*<br />
&nbsp;* Created by softwarepassion.com<br />
&nbsp;* Any information regarding license for this code snippets and<br />
&nbsp;* other copyright info can be found @softwarepassion.com<br />
&nbsp;* The author doesn't take any reposnsibility for the presented<br />
&nbsp;* code and design patterns solutions.<br />
&nbsp;* Please be advised that this code can contain errors!<br />
&nbsp;*<br />
&nbsp;*/</span><br />
<br />
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">strategydesignpattern</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Example class using {@code ReportGenerator} interface<br />
&nbsp;* This class mimmicks the behaviour of a http servlet, assuming that all needed<br />
&nbsp;* parameters for generating report are coming from the database or in the form<br />
&nbsp;* of servlet request parameters contained in a single {@code ReportData } object,<br />
&nbsp;* we choose one of the available report generators<br />
&nbsp;* to generate our report.<br />
&nbsp;* <br />
&nbsp;* @author Krzysztof Grajek<br />
&nbsp;* @version 28-01-2011<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MockReportServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> ReportGenerator reportGenerator<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onGet<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> request, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> response<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//1. get the data from the database, request parameters or any other source</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//in the form of ReportData object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ReportData reportData <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ReportData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; reportData.<span style="color: #006633;">setTitle</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Design Patterns Report for: Strategy Pattern&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//2. Using the request parameter decide on which one of three report generators</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//to use when generating the report</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//3. Assuming that PDF generator was selected by the user generate PDF report</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PDF&quot;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>request<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reportGenerator <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PDFReportGenerator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reportGenerator.<span style="color: #006633;">generateReport</span><span style="color: #009900;">&#40;</span>reportData<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The source code for this example (in the form of Netbeans project) can be found <a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/StrategyDesignPattern.zip">here</a></p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/design-patterns-series-part-one-strategy-pattern/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Jasper Reports Book</title>
		<link>http://www.softwarepassion.com/jasper-reports-book/</link>
		<comments>http://www.softwarepassion.com/jasper-reports-book/#comments</comments>
		<pubDate>Sun, 05 Sep 2010 15:29:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Learning Materials]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=396</guid>
		<description><![CDATA[One of the latest books on my bookshelve is &#8216;Jasper Reports 3.6 Development Cookbook&#8216; by Bilal Siddiqui. If you have ever wrote/maintained an application which has to gather some data into a report form using java technology this can be a book for you. The book is divided into 50 sections, each being a separate [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>One of the latest books on my bookshelve is &#8216;<em><strong>Jasper Reports 3.6 Development Cookbook</strong>&#8216; </em>by Bilal Siddiqui. If you have ever wrote/maintained an application which has to<br />
gather some data into a report form using java technology this can be a book for you.<br />
The book is divided into 50 sections, each being a separate recipe, describing how to build a particular part of the<br />
report like headers, footers, grids etc.<br />
Every recipe found in this book has a form of a well prepared tutorial with screen shots and &#8216;extensive&#8217; explanation about a<br />
particular topic.<br />
This book goes straight to the point which some of the developers will really appreciate, it&#8217;s a 50 quick recipes on how to<br />
use iReport software and Jasper Reports library effectively and with no fuss.<br />
You can find more info about the book <a href="http://www.packtpub.com/jasperreports-3-5-2-development-cookbook/book?utm_source=softwarepassion.com&amp;utm_medium=bookrev&amp;utm_content=blog&amp;utm_campaign=mdb_004586">here</a><br />
<strong>Hope you&#8217;ll like it too.</strong></p>
<div id="attachment_398" class="wp-caption aligncenter" style="width: 409px"><em><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/jasper.jpg" rel="lightbox[396]"><img class="size-full wp-image-398" title="jasper reports 3.6 development cookbook" src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/jasper.jpg" alt="" width="399" height="493" /></a></em><p class="wp-caption-text">jasper reports 3.6 development cookbook</p></div>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/jasper-reports-book/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Add &#8216;count #&#8217; column to JTable</title>
		<link>http://www.softwarepassion.com/add-count-column-to-jtable/</link>
		<comments>http://www.softwarepassion.com/add-count-column-to-jtable/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 14:18:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[General Programming]]></category>
		<category><![CDATA[Graphics And Design]]></category>
		<category><![CDATA[Learning Materials]]></category>
		<category><![CDATA[count column]]></category>
		<category><![CDATA[jxtable]]></category>

		<guid isPermaLink="false">http://www.softwarepassion.com/?p=336</guid>
		<description><![CDATA[<span style="max-width: 100px; display: block; float: left;"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/count_jtable_column-300x179.png" alt="jtable count column" title="jtable # column" width="100" height="100"/></span><span style="margin: 0px; padding-left: 10px; display: block; max-width: 370px; float: left;">How to add 'count#' column in JTable</span>]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>Important note first: This is mostly not my code, I was strugling with this problem for some time, I couldn&#8217;t google it at all, and one day, I&#8217;ve just found it. Now I use it in one of my projects but I cannot find the original post again, therefore I have decided to post it on my blog (slightly modified version of the original) so other people with similar problem will have a better chance of finding the solution.<br />
So, the idea is very simple. The whole point of displaying additional column which is kind of independent from the other columns is to simply use another table. This way you can sort one table by column and the number column will not sort with it. &#8211; thats exactly what I was looking for and I guess its kind of popular problem.<br />
Becouse you put two tables in one scroll pane the gui looks like its just one column!<br />
<script type="text/javascript"><!--
google_ad_client = "pub-3015706834207881";
/* 468x15, utworzono 09-11-19 */
google_ad_slot = "3912330401";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
I have created one simple method to apply this technique to any tables in my application (I use JXTable actually but JTable will do as well)</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> addLPColumn<span style="color: #009900;">&#40;</span>JXTable table, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajscrollpane+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JScrollPane</span></a> scp<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajtable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JTable</span></a> rowTable <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RowNumberTable<span style="color: #009900;">&#40;</span>table<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; scp.<span style="color: #006633;">setRowHeaderView</span><span style="color: #009900;">&#40;</span>rowTable<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; scp.<span style="color: #006633;">setCorner</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajscrollpane+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JScrollPane</span></a>.<span style="color: #006633;">UPPER_LEFT_CORNER</span>, rowTable.<span style="color: #006633;">getTableHeader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; scp.<span style="color: #006633;">repaint</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p><script type="text/javascript"><!--
google_ad_client = "pub-3015706834207881";
/* 468x15, utworzono 09-11-19 */
google_ad_slot = "3912330401";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>RowNumberTable is simple one column table displaying the &#8216;count&#8217; number of rows in the second table. </p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-3015706834207881";
/* 468x15, utworzono 09-11-19 */
google_ad_slot = "3912330401";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RowNumberTable <span style="color: #000000; font-weight: bold;">extends</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajtable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JTable</span></a><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">implements</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Achangelistener+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">ChangeListener</span></a>, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Apropertychangelistener+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">PropertyChangeListener</span></a><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajtable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JTable</span></a> main<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> RowNumberTable<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajtable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JTable</span></a> table<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; main <span style="color: #339933;">=</span> table<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; main.<span style="color: #006633;">addPropertyChangeListener</span><span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">this</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; setFocusable<span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; setAutoCreateColumnsFromModel<span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; setModel<span style="color: #009900;">&#40;</span> main.<span style="color: #006633;">getModel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; setSelectionModel<span style="color: #009900;">&#40;</span> main.<span style="color: #006633;">getSelectionModel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Atablecolumn+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">TableColumn</span></a> column <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Atablecolumn+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">TableColumn</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; column.<span style="color: #006633;">setHeaderValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Lp.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; addColumn<span style="color: #009900;">&#40;</span> column <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; column.<span style="color: #006633;">setCellRenderer</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> RowNumberRenderer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; getColumnModel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getColumn</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setPreferredWidth</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; setPreferredScrollableViewportSize<span style="color: #009900;">&#40;</span>getPreferredSize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addNotify<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">addNotify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acomponent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Component</span></a> c <span style="color: #339933;">=</span> getParent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// &nbsp;Keep scrolling of the row table in sync with the main table.</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c <span style="color: #000000; font-weight: bold;">instanceof</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajviewport+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JViewport</span></a><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajviewport+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JViewport</span></a> viewport <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajviewport+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JViewport</span></a><span style="color: #009900;">&#41;</span>c<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; viewport.<span style="color: #006633;">addChangeListener</span><span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">this</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/*<br />
&nbsp; &nbsp; &nbsp;* &nbsp;Delegate method to main table<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getRowCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>main <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> main.<span style="color: #006633;">getRowCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getRowHeight<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> row<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> main.<span style="color: #006633;">getRowHeight</span><span style="color: #009900;">&#40;</span>row<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/*<br />
&nbsp; &nbsp; &nbsp;* &nbsp;This table does not use any data from the main TableModel,<br />
&nbsp; &nbsp; &nbsp;* &nbsp;so just return a value based on the row parameter.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> getValueAt<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> row, <span style="color: #000066; font-weight: bold;">int</span> column<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainteger+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Integer</span></a>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span>row <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/*<br />
&nbsp; &nbsp; &nbsp;* &nbsp;Don't edit data in the main TableModel by mistake<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isCellEditable<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> row, <span style="color: #000066; font-weight: bold;">int</span> column<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #666666; font-style: italic;">//</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp;Implement the ChangeListener</span><br />
<span style="color: #666666; font-style: italic;">//</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> stateChanged<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Achangeevent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">ChangeEvent</span></a> e<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// &nbsp;Keep the scrolling of the row table in sync with main table</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajviewport+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JViewport</span></a> viewport <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajviewport+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JViewport</span></a><span style="color: #009900;">&#41;</span> e.<span style="color: #006633;">getSource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajscrollpane+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JScrollPane</span></a> scrollPane <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajscrollpane+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JScrollPane</span></a><span style="color: #009900;">&#41;</span>viewport.<span style="color: #006633;">getParent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; scrollPane.<span style="color: #006633;">getVerticalScrollBar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span>viewport.<span style="color: #006633;">getViewPosition</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #666666; font-style: italic;">//</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp;Implement the PropertyChangeListener</span><br />
<span style="color: #666666; font-style: italic;">//</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> propertyChange<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Apropertychangeevent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">PropertyChangeEvent</span></a> e<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// &nbsp;Keep the row table in sync with the main table</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;selectionModel&quot;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">getPropertyName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setSelectionModel<span style="color: #009900;">&#40;</span> main.<span style="color: #006633;">getSelectionModel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;model&quot;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">getPropertyName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setModel<span style="color: #009900;">&#40;</span> main.<span style="color: #006633;">getModel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/*<br />
&nbsp; &nbsp; &nbsp;* &nbsp;Borrow the renderer from JDK1.4.2 table header<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> RowNumberRenderer <span style="color: #000000; font-weight: bold;">extends</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adefaulttablecellrenderer+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">DefaultTableCellRenderer</span></a><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> RowNumberRenderer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setHorizontalAlignment<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajlabel+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JLabel</span></a>.<span style="color: #006633;">CENTER</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acomponent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Component</span></a> getTableCellRendererComponent<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajtable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JTable</span></a> table, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> value, <span style="color: #000066; font-weight: bold;">boolean</span> isSelected, <span style="color: #000066; font-weight: bold;">boolean</span> hasFocus, <span style="color: #000066; font-weight: bold;">int</span> row, <span style="color: #000066; font-weight: bold;">int</span> column<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>table <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajtableheader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JTableHeader</span></a> header <span style="color: #339933;">=</span> table.<span style="color: #006633;">getTableHeader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>header <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setForeground<span style="color: #009900;">&#40;</span>header.<span style="color: #006633;">getForeground</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setBackground<span style="color: #009900;">&#40;</span>header.<span style="color: #006633;">getBackground</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setFont<span style="color: #009900;">&#40;</span>header.<span style="color: #006633;">getFont</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isSelected<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setFont<span style="color: #009900;">&#40;</span> getFont<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">deriveFont</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afont+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Font</span></a>.<span style="color: #006633;">BOLD</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setText<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>value <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">:</span> value.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setBorder<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Auimanager+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">UIManager</span></a>.<span style="color: #006633;">getBorder</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;TableHeader.cellBorder&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p><a href="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/count_jtable_column.png" rel="lightbox[336]"><img src="http://softwarepassion.s3.amazonaws.com/wp-content/uploads/count_jtable_column-300x179.png" alt="count_jtable_column" title="count_jtable_column" width="300" height="179" class="aligncenter size-medium wp-image-337" /></a></p>
<p>In my case the &#8216;count&#8217; column is called &#8216;Lp.&#8217; &#8211; in polish </p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-3015706834207881";
/* 468x15, utworzono 09-11-19 */
google_ad_slot = "3912330401";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
I have created one simple method to apply this technique to any tables in my application (I use JXTable actually but JTable will do as well)</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> addLPColumn<span style="color: #009900;">&#40;</span>JXTable table, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajscrollpane+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JScrollPane</span></a> scp<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajtable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JTable</span></a> rowTable <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RowNumberTable<span style="color: #009900;">&#40;</span>table<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; scp.<span style="color: #006633;">setRowHeaderView</span><span style="color: #009900;">&#40;</span>rowTable<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; scp.<span style="color: #006633;">setCorner</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajscrollpane+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JScrollPane</span></a>.<span style="color: #006633;">UPPER_LEFT_CORNER</span>, rowTable.<span style="color: #006633;">getTableHeader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; scp.<span style="color: #006633;">repaint</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p><script type="text/javascript"><!--
google_ad_client = "pub-3015706834207881";
/* 468x15, utworzono 09-11-19 */
google_ad_slot = "3912330401";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/add-count-column-to-jtable/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Emergent Design: The Evolutionary Nature of Professional Software Development</title>
		<link>http://www.softwarepassion.com/emergent-design-the-evolutionary-nature-of-professional-software-development/</link>
		<comments>http://www.softwarepassion.com/emergent-design-the-evolutionary-nature-of-professional-software-development/#comments</comments>
		<pubDate>Thu, 04 Sep 2008 12:19:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Learning Materials]]></category>

		<guid isPermaLink="false">http://softwarepassion.com/?p=34</guid>
		<description><![CDATA[Well, I have just read another comment left on my blog and I had realized, well, I had realized that I have a blog . Completly forgot about its existence, and since last post I was writing as a student at University of Greenwich, London, now I&#8217;m writing as an owner of a really small [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_SjX7MIMF_HI/SL_Umfl5STI/AAAAAAAAALw/G8FHjOKqJBU/s1600-h/410rsT5XQmL._SL500_AA240_.jpg" rel="lightbox[34]"><img style="float:right; margin:0 0 10px 10px;cursor:pointer; cursor:hand;" src="http://4.bp.blogspot.com/_SjX7MIMF_HI/SL_Umfl5STI/AAAAAAAAALw/G8FHjOKqJBU/s320/410rsT5XQmL._SL500_AA240_.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5242142249063106866" /></a><br />Well, I have just read another comment left on my blog and I had realized, well, I had realized that I have a blog <img src='http://www.softwarepassion.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Completly forgot about its existence, and since last post I was writing as a student at University of Greenwich, London, now I&#8217;m writing as an owner of a really small startup software company in Elblag, Poland <img src='http://www.softwarepassion.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> , things change quickly.</p>
<p>Anyway, student or not, I still read a lot, maybe not that much as I used to do but stil quite a lot. I have decided to write a short note on the latest book I read as I&#8217;ve found it really interesting.</p>
<p>The title you know already, this book is not about programming but about more general concepts, about programming as a profession. It&#8217;s a nice book about software design and life as a software developer. Once you become proficient in any programming language and you start being interested in methodologies, patterns etc you should read this book!<br />Highly recommended!</p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/emergent-design-the-evolutionary-nature-of-professional-software-development/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Head First Software Development completed!</title>
		<link>http://www.softwarepassion.com/head-first-software-development-completed/</link>
		<comments>http://www.softwarepassion.com/head-first-software-development-completed/#comments</comments>
		<pubDate>Sat, 01 Mar 2008 21:00:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Learning Materials]]></category>

		<guid isPermaLink="false">http://softwarepassion.com/?p=28</guid>
		<description><![CDATA[Another example of the book proving that software books can be fun! Head First series books are usualy easy to read and thats what makes them most valuable for me. I have read a few of them, since I started learning how to program, and I was never disappointed, I think I have read HF [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp2.blogger.com/_SjX7MIMF_HI/R8nKLPGXcKI/AAAAAAAAAKA/ir7vGfmWiLc/s1600-h/hfsd.gif" rel="lightbox[28]"><img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer;" src="http://bp2.blogger.com/_SjX7MIMF_HI/R8nKLPGXcKI/AAAAAAAAAKA/ir7vGfmWiLc/s400/hfsd.gif" alt="" id="BLOGGER_PHOTO_ID_5172887941392330914" border="0" /></a>Another example of the book proving that software books can be fun! Head First series books are usualy easy to read and thats what makes them most valuable for me. I have read a few of them, since I started learning how to program, and I was never disappointed, I think I have read HF Servlets and JSP about 3 times <img src='http://www.softwarepassion.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  and I know that if I start a project with Servlets or JSPs I will read a few bits again <img src='http://www.softwarepassion.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .<br />HF Software Development is of course no exception. This book on my bookshelf is a result of my new subscription to Safari, I should actualy say it&#8217;s on my virtual bookshelf <img src='http://www.softwarepassion.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .<br />I belive there are experienced software developers which will find this book boring or maybe not bringing anything new to their &#8216;toolbox&#8217;.<br />For me this book is superb! In my opinion every CS student should read it before going into the wild and look for a job.<br />This is in short what you can get:
<ul>
<li>A feel of profesional software development process</li>
<p>
<li>Reason for manager&#8217;s daily headache <img src='http://www.softwarepassion.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<p>
<li>What is TDD and how it works</li>
<p>
<li>What is Continous Integration</li>
<p>
<li>How tos: gathering requirements, desing and implement </li>
<p>
<li>A few bits on proper design, UML and refactoring</li>
<p>
<li>Many others which I either considered common knowledge or haven&#8217;t notice <img src='http://www.softwarepassion.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </li>
<p></ul>
<p>Go to Safari books online or Amazon for detailed table of contents.<br />I would give it 5/5 mark.<br />Highly recommended</p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/head-first-software-development-completed/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Mythical Man Month On My Bookshelf</title>
		<link>http://www.softwarepassion.com/mythical-man-month-on-my-bookshelf/</link>
		<comments>http://www.softwarepassion.com/mythical-man-month-on-my-bookshelf/#comments</comments>
		<pubDate>Sun, 27 Jan 2008 05:43:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Learning Materials]]></category>

		<guid isPermaLink="false">http://softwarepassion.com/?p=25</guid>
		<description><![CDATA[I was reading so often about this one on the net and in other books that it was on top of my reading list quite a long time. I had no idea what to expect from this book and I haven&#8217;t found there anything what recently I like the most, like tips on software development, [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp2.blogger.com/_SjX7MIMF_HI/R5wePgcuK0I/AAAAAAAAAJY/fJ9xk5-jk-U/s1600-h/mythical_man_month.jpg" rel="lightbox[25]"><img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer;" src="http://bp2.blogger.com/_SjX7MIMF_HI/R5wePgcuK0I/AAAAAAAAAJY/fJ9xk5-jk-U/s400/mythical_man_month.jpg" alt="" id="BLOGGER_PHOTO_ID_5160032524817017666" border="0" /></a><br />I was reading so often about this one on the net and in other books that it was on top of my reading list quite a long time. I had no idea what to expect from this book and I haven&#8217;t found there anything what recently I like the most, like tips on software development, refactorings to patterns or code tunning. Well it&#8217;s non of those things but of course it&#8217;s not the author&#8217;s fault. Definietly I should do some more research before buying this one, as I think this book is not for startups in software industry like me. I belive, people who manage projects and people resources enjoy reading this book much more than those who write software only.<br />I cannot say I didn&#8217;t like it though, overall is always nice to read about such a huge project like soft for IBM mainframe.<br />Maybe not really what I&#8217;m looking for these days but definietly valuable read!<br />Recommended!</p>
<!--INFOLINKS_OFF-->]]></content:encoded>
			<wfw:commentRss>http://www.softwarepassion.com/mythical-man-month-on-my-bookshelf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

