<?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>Articles</title>
	<atom:link href="http://dev.nirmalya.net/articles/feed" rel="self" type="application/rss+xml" />
	<link>http://dev.nirmalya.net/articles</link>
	<description>Random articles  to help others who might be facing similar situations/problems (previously faced by me) during development.</description>
	<lastBuildDate>Sat, 12 May 2012 19:20:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Writing Own N-Grams Analyzer Vs Using ShingleAnalyzerWrapper</title>
		<link>http://dev.nirmalya.net/articles/java/writing-own-n-grams-analyzer-vs-using-shingleanalyzerwrapper</link>
		<comments>http://dev.nirmalya.net/articles/java/writing-own-n-grams-analyzer-vs-using-shingleanalyzerwrapper#comments</comments>
		<pubDate>Sun, 29 Apr 2012 14:02:41 +0000</pubDate>
		<dc:creator>nirmalya</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Lucene]]></category>

		<guid isPermaLink="false">http://dev.nirmalya.net/articles/?p=503</guid>
		<description><![CDATA[When extracting word-level N-grams, subclassing Apache Lucene&#8217;s Analyzer seems (at first glance) a better choice than using their ShingleAnalyzerWrapper. Lets say you wanted to extract word-level N-Grams for the following quote, &#8220;The brick walls are there for a reason. The brick walls are not there to keep us out; the brick walls are there to [...]]]></description>
			<content:encoded><![CDATA[<p>When extracting word-level <a href="http://en.wikipedia.org/wiki/N-gram" target="_blank">N-grams</a>, subclassing Apache Lucene&#8217;s <code><a href="http://lucene.apache.org/core/3_6_0/api/all/org/apache/lucene/analysis/Analyzer.html" target="_blank">Analyzer</a></code> seems (at first glance) a better choice than using their <code><a href="http://lucene.apache.org/core/3_6_0/api/all/org/apache/lucene/analysis/shingle/ShingleAnalyzerWrapper.html" target="_blank">ShingleAnalyzerWrapper</a></code>.<br />
<span id="more-503"></span><br />
Lets say you wanted to extract word-level N-Grams for the following quote, &#8220;<i>The brick walls are there for a reason. The brick walls are not there to keep us out; the brick walls are there to give us a chance to show how badly we want something.</i>&#8221; (by <a href="http://en.wikiquote.org/wiki/Randy_Pausch" target="_blank">Randy Pausch</a>).</p>
<p>
<b>Using the <code>ShingleAnalyzerWrapper</code></b>,</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">boolean</span> outputUnigrams <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span>, outputUnigramsIfNoShingles <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> minShingleSize <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span>, maxShingleSize <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
ShingleAnalyzerWrapper wrapper <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ShingleAnalyzerWrapper<span style="color: #009900;">&#40;</span>
    <span style="color: #000000; font-weight: bold;">new</span> StandardAnalyzer<span style="color: #009900;">&#40;</span>Version.<span style="color: #006633;">LUCENE_36</span><span style="color: #009900;">&#41;</span>, 
    minShingleSize, maxShingleSize, tokenSeparator, 
    outputUnigrams, outputUnigramsIfNoShingles<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
TokenStream tokenStream <span style="color: #339933;">=</span> wrapper.<span style="color: #006633;">tokenStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;contents&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringReader</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The code above yields <b>20</b> word-level 2-grams : <i>_ brick, brick walls, walls _, _ reason, reason _, _ keep, keep us, us out, out _, _ give, give us, us _, _ chance, chance _, _ show, show how, how badly, badly we, we want, want something</i>.</p>
<p>Notice the (unwanted) underscores.</p>
<p>
<b>Using own subclass</b> of Apache Lucene&#8217;s <code>Analyzer</code>, (let&#8217;s call it) <code>NGramAnalyzer</code>,</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NGramAnalyzer <span style="color: #000000; font-weight: bold;">extends</span> Analyzer <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> TokenStream tokenStream<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> fieldName, <span style="color: #003399;">Reader</span> reader<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Version v <span style="color: #339933;">=</span> Version.<span style="color: #006633;">LUCENE_36</span><span style="color: #339933;">;</span>
    Tokenizer tokenizer <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">convertToLowerCase</span> 
      <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> LowerCaseTokenizer<span style="color: #009900;">&#40;</span>v, reader<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> StandardTokenizer<span style="color: #009900;">&#40;</span>v, reader<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    ShingleFilter ts <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ShingleFilter<span style="color: #009900;">&#40;</span>tokenizer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ts.<span style="color: #006633;">setOutputUnigrams</span><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>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> StopFilter<span style="color: #009900;">&#40;</span>v, ts, StopAnalyzer.<span style="color: #006633;">ENGLISH_STOP_WORDS_SET</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The code above yields <b>26</b> word-level 2-grams : <i>the brick, brick walls, walls are, are there, there for, for a, a reason, reason the, are not, not there, there to, to keep, keep us, us out, out the, to give, give us, us a, a chance, chance to, to show, show how, how badly, badly we, we want, want something</i>.</p>
<p>
Notice the inclusion of 2-grams such as &#8220;<i>are there</i>&#8220;, &#8220;<i>a reason</i>&#8220;, &#8220;<i>reason the</i>&#8221; which are either missing or partially substituted by underscores in the 2-grams output by the <code>ShingleAnalyzerWrapper</code> (because they contain stop words which are part of the <code>ENGLISH_STOP_WORDS_SET</code>).</p>
<p>
So, it <b>might be better to use the former</b> (<code>ShingleAnalyzerWrapper</code>), <b>with minor modifications</b>,</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">CharTermAttribute termAttribute <span style="color: #339933;">=</span> 
  <span style="color: #009900;">&#40;</span>CharTermAttribute<span style="color: #009900;">&#41;</span> tokenStream.<span style="color: #006633;">getAttribute</span><span style="color: #009900;">&#40;</span>CharTermAttribute.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
LinkedHashSet<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>String<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> tokens <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkedHashSet<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>String<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</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;">char</span> underscore <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;_&quot;</span>.<span style="color: #006633;">charAt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>stream.<span style="color: #006633;">incrementToken</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">String</span> n_gram <span style="color: #339933;">=</span> termAttribute.<span style="color: #006633;">toString</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;">int</span> ln <span style="color: #339933;">=</span> n_gram.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>n_gram.<span style="color: #006633;">charAt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> underscore<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>outputUnigrams<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> tokens.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>n_gram.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>n_gram.<span style="color: #006633;">charAt</span><span style="color: #009900;">&#40;</span>ln <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> underscore<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>outputUnigrams<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> tokens.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>n_gram.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, ln <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">else</span>
      tokens.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>n_gram<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// .. handle exception</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>which now yields <b>9</b> word-level 2-grams, <i>brick walls, keep us, us out, give us, show how, how badly, badly we, we want, want something</i>.</p>
<p>Hope this helps.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.nirmalya.net/articles/java/writing-own-n-grams-analyzer-vs-using-shingleanalyzerwrapper/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Equivalent Of &#8220;Like&#8221; For MongoDB</title>
		<link>http://dev.nirmalya.net/articles/java/equivalent-of-like-for-mongodb</link>
		<comments>http://dev.nirmalya.net/articles/java/equivalent-of-like-for-mongodb#comments</comments>
		<pubDate>Sun, 25 Mar 2012 09:10:47 +0000</pubDate>
		<dc:creator>nirmalya</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[Spring Data]]></category>

		<guid isPermaLink="false">http://dev.nirmalya.net/articles/?p=493</guid>
		<description><![CDATA[Command db.foo.find({"firstName":/ss/}) on the Mongo shell returns results similar to &#123; &#34;firstName&#34; : &#34;Issac&#34;, &#34;lastName&#34; : &#34;Newton&#34;, &#34;yearOfBirth&#34; : 1642 &#125; Repeating the same using Spring Data for MongoDB did not go smoothly. I kept encountering JSON parse exceptions for the JSON string {"firstName":/ss/}. Referring to MongoDB manual, the $regex clause should help. Spring Data&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Command <code>db.foo.find({"firstName":/ss/})</code> on the Mongo shell returns results similar to</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;firstName&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Issac&quot;</span>, <span style="color: #0000ff;">&quot;lastName&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Newton&quot;</span>, <span style="color: #0000ff;">&quot;yearOfBirth&quot;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1642</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Repeating the same using <a href="http://www.springsource.org/spring-data/mongodb" target="_blank">Spring Data for MongoDB</a> did not go smoothly. I kept encountering JSON parse exceptions for the JSON string <code>{"firstName":/ss/}</code>.</p>
<p>Referring to MongoDB manual, <a href="http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions" target="_blank">the $regex clause</a> should help. Spring Data&#8217;s Criteria class has 2 related methods (<code><a href="http://static.springsource.org/spring-data/data-document/docs/current/api/org/springframework/data/mongodb/core/query/Criteria.html#regex(java.lang.String)" target="_blank">Criteria.regex(String)</a></code> and <code><a href="http://static.springsource.org/spring-data/data-document/docs/current/api/org/springframework/data/mongodb/core/query/Criteria.html#regex(java.lang.String, java.lang.String)" target="_blank">Criteria.regex(String, String)</a></code>). However, I was unable to get them to work either. So I resorted to using the <a href="http://www.mongodb.org/display/DOCS/Java+Tutorial" target="_blank">MongoDB Java Driver</a> directly. A snippet of the working code,<br />
<span id="more-493"></span></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> findPeopleHavingFirstNameContaining<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> searchString<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> people <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    Mongo m <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
      m <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Mongo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span>, <span style="color: #cc66cc;">27017</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// simplified for this post</span>
      DB db <span style="color: #339933;">=</span> m.<span style="color: #006633;">getDB</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      BasicDBObject query <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BasicDBObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      DBCollection coll <span style="color: #339933;">=</span> db.<span style="color: #006633;">getCollection</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      query.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;firstName&quot;</span>, 
        <span style="color: #000000; font-weight: bold;">new</span> BasicDBObject<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;$regex&quot;</span>, <span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.*((?i)%s).*&quot;</span>, searchString<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      DBCursor cur <span style="color: #339933;">=</span> coll.<span style="color: #006633;">find</span><span style="color: #009900;">&#40;</span>query<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">int</span> numRecords <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>cur <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> cur.<span style="color: #006633;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>numRecords <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        people <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>cur.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          DBObject o <span style="color: #339933;">=</span> cur.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          people.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span>
                       <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> o.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;firstName&quot;</span><span style="color: #009900;">&#41;</span>, 
                       <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> o.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;lastName&quot;</span><span style="color: #009900;">&#41;</span>, 
                       <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span><span style="color: #009900;">&#41;</span> o.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;yearOfBirth&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">intValue</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><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">/* handle exception .. */</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>m <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> m.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">return</span> people<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>And here is the domain object,</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> firstName, lastName<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> yearOfBirth<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> Person<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> fName, <span style="color: #003399;">String</span> lName, <span style="color: #000066; font-weight: bold;">int</span> year<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    setFirstName<span style="color: #009900;">&#40;</span>fName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    setLastName<span style="color: #009900;">&#40;</span>lName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    setYearOfBirth<span style="color: #009900;">&#40;</span>year<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #666666; font-style: italic;">// getter/setter methods follow ..</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://dev.nirmalya.net/articles/java/equivalent-of-like-for-mongodb/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quickly Debugging Your Hadoop Program</title>
		<link>http://dev.nirmalya.net/articles/java/quickly-debugging-your-hadoop-program</link>
		<comments>http://dev.nirmalya.net/articles/java/quickly-debugging-your-hadoop-program#comments</comments>
		<pubDate>Thu, 15 Mar 2012 10:30:34 +0000</pubDate>
		<dc:creator>nirmalya</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Hadoop]]></category>

		<guid isPermaLink="false">http://dev.nirmalya.net/articles/?p=505</guid>
		<description><![CDATA[This quick tip applies to situations when you are trying to debug your Hadoop program and the log messages (or System.outs) are not appearing on file or console. Edit the /usr/local/hadoop/conf/log4j.properties file (path relevant to the vanilla Apache Hadoop distribution). Update the log4j.rootLogger to have console appended to it, similar to: log4j.rootLogger=$&#123;hadoop.root.logger&#125;, EventCounter, console The [...]]]></description>
			<content:encoded><![CDATA[<p>This quick tip applies to situations when you are trying to debug your Hadoop program and the log messages (or <code>System.out</code>s) are not appearing on file or console.</p>
<p>Edit the <code>/usr/local/hadoop/conf/log4j.properties</code> file (path relevant to the vanilla Apache Hadoop distribution). Update the <code>log4j.rootLogger</code> to have <code>console</code> appended to it, similar to:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">log4j.<span style="color: #006633;">rootLogger</span><span style="color: #339933;">=</span>$<span style="color: #009900;">&#123;</span>hadoop.<span style="color: #006633;">root</span>.<span style="color: #006633;">logger</span><span style="color: #009900;">&#125;</span>, EventCounter, console</pre></div></div>

<p>The log files are under subdirectories under <code>/usr/local/hadoop/logs/userlogs</code> similar to <code>job_201203151712_000x</code>. Select the most recent (the one with largest <code>x</code>).</p>
<p>Under <code>/usr/local/hadoop/logs/userlogs</code> there are subdirectories similar to</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">attempt_201203151712_0008_m_000000_0  attempt_201203151712_0008_m_000002_0  
attempt_201203151712_0008_r_000000_0  attempt_201203151712_0008_m_000001_0
attempt_201203151712_0008_m_000003_0  job-acls.xml</pre></div></div>

<p>The naming convention is pretty obvious, i.e. the <code>_m_</code> ones are for the Mappers, <code>_r_</code> for Reducers. Each of these <code>_m_</code>/<code>_r_</code> subdirectories have 4 files,</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">log.index  stderr  stdout  syslog</pre></div></div>

<p>Your log messages appear inside the <code>stderr</code> file.<br />
Your <code>System.out</code>s appear inside the <code>stdout</code> file.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.nirmalya.net/articles/java/quickly-debugging-your-hadoop-program/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Social Media Counter Using MongoDB And Spring Data</title>
		<link>http://dev.nirmalya.net/articles/java/social-media-counter-using-mongodb-and-spring-data</link>
		<comments>http://dev.nirmalya.net/articles/java/social-media-counter-using-mongodb-and-spring-data#comments</comments>
		<pubDate>Thu, 23 Feb 2012 11:30:52 +0000</pubDate>
		<dc:creator>nirmalya</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Setup]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[Spring Data]]></category>

		<guid isPermaLink="false">http://dev.nirmalya.net/articles/?p=406</guid>
		<description><![CDATA[About a month ago, I created a simple project to better manage reading list (fed by an ever growing number of RSS feeds) by prioritizing which articles to read based on attributes such as URLs’ popularity, age, tags, etc.. The popularity of an URL can be determined based on its social media counters (Facebook likes, [...]]]></description>
			<content:encoded><![CDATA[<p>About a month ago, I created a simple project to better manage reading list (fed by an ever growing number of RSS feeds) by prioritizing which articles to read based on attributes such as URLs’ popularity, age, tags, etc.. </p>
<p>The popularity of an URL can be determined based on its social media counters (Facebook likes, Tweet count, Google +1′s, LinkedIn shares, etc.), number of comments, etc.</p>
<p>Information stored for one URL is likely to be very different from that stored for another. For example,</p>
<ul>
<li>URL1 only has a Tweet count and the number of LinkedIn shares,</li>
<li>URL2 has Facebook likes, Tweet count, LinkedIn shares and a set of tags</li>
<li>URL3 has Facebook likes, Tweet count, LinkedIn shares and Google +1′s (introduced later in the project),</li>
</ul>
<p><a href="http://www.mongodb.org/" target="_blank">MongoDB</a> is <strong><a href="http://www.mongodb.org/display/DOCS/Production+Deployments" title="MongoDB in production deployments" target="_blank">excellent</a></strong> for handling schemaless data similar to the simple example above. <a href="http://www.springsource.org/spring-data/mongodb" target="_blank">Spring Data</a> makes it very easy to integrate MongoDB into a new/existing project. In this article, I describe the project’s setup, the relevant sections of the project’s <code>pom.xml</code>, part of the DAO code and a simple aggregation done by making use of MongoDB’s <a href="http://www.mongodb.org/display/DOCS/MapReduce" target="_blank">Map Reduce feature</a>. Specifics of trending algorithms, the UIs will be covered in later articles.<br />
<span id="more-406"></span><br />
<big><strong>Project Directory Layout</strong></big><br />
I have used the <a href="http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html" target="_blank">standard directory layout</a> common to most Maven projects. I have the used the common DAO pattern which allows replacing of the actual persitence layer (in this example, MongoDB) with any other NoSQL or relational database. Code responsible for the social media counters (such as the FacebookWorker, GooglePlusOneWorker, TwitterWorker, etc.) are grouped under the worker package. Code for parsing RSS feeds is based on <a href="http://www.vogella.de/articles/RSSFeed/article.html" target="_blank">http://www.vogella.de/articles/RSSFeed/article.html</a>.<br />
<code></p>
<p>¦   pom.xml<br />
+---src<br />
¦   +---main<br />
¦   ¦   +---java<br />
¦   ¦   ¦   +---de<br />
¦   ¦   ¦   ¦   +---vogella<br />
¦   ¦   ¦   ¦       +---rss<br />
¦   ¦   ¦   +---net<br />
¦   ¦   ¦       +---nirmalya<br />
¦   ¦   ¦           +---urlpopularitycounter<br />
¦   ¦   ¦               +---dao<br />
¦   ¦   ¦               ¦   +---mongo<br />
¦   ¦   ¦               +---worker<br />
¦   ¦   +---resources<br />
¦   ¦   ------applicationContext.xml<br />
¦   ¦   ------quartz.properties<br />
¦   +---test<br />
¦       +---java<br />
¦       ¦   +---dev<br />
¦       ¦       +---nirmalya<br />
¦       ¦           +---net<br />
¦       ¦               +---urlpopularitycounter<br />
¦       +---resources<br />
</code></p>
<p><big><strong>pom.xml</strong></big><br />
Pasted below are relevant sections of the project&#8217;s <code>pom.xml</code> to get started with Spring Data and MongoDB. First, the <code>repositories</code> section,</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;repositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;repository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-milestone<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Spring Maven MILESTONE Repository<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://maven.springframework.org/milestone<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/repository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/repositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Relevant parts of the <code>dependencies</code> section,</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>commons-collections<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>commons-collections<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3.2.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>commons-logging<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>commons-logging<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.1.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <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.springframework<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-core<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3.0.6.RELEASE<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <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.springframework<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-context-support<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3.1.0.RELEASE<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <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.springframework.data<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-data-mongodb<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.1.RELEASE<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>I used <a href="http://code.google.com/p/json-path/" target="_blank">JsonPath</a> (think of it as XPath for JSON) to simplify parsing the JSON strings returned by some of the social media counters. Relevant <code>dependency</code>,</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.jayway.jsonpath<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>json-path<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.5.5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Relevant <code>dependency</code> for the RSS feed parsing code,</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax.xml.stream<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>stax-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>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0-2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <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.json<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>json<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>20090211<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>I used the Quartz Scheduler for scheduling the RSS feed checking jobs. Relevant <code>dependency</code></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <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.opensymphony.quartz<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>quartz<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.6.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><big><strong>applicationContext.xml</strong></big><br />
Relevant sections of the Spring configuration file,</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">xmlns:context</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/context&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">xmlns:mongo</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/data/mongo&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">xmlns:util</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/util&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">xsi:schemaLocation</span>=</span>
<span style="color: #009900;">          <span style="color: #ff0000;">&quot;http://www.springframework.org/schema/context</span>
<span style="color: #009900;">          http://www.springframework.org/schema/context/spring-context-3.0.xsd</span>
<span style="color: #009900;">          http://www.springframework.org/schema/data/mongo</span>
<span style="color: #009900;">          http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd</span>
<span style="color: #009900;">          http://www.springframework.org/schema/beans</span>
<span style="color: #009900;">          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;context:annotation-config</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mongo:mongo</span> <span style="color: #000066;">host</span>=<span style="color: #ff0000;">&quot;127.0.0.1&quot;</span> <span style="color: #000066;">port</span>=<span style="color: #ff0000;">&quot;27017&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;mongoTemplate&quot;</span> </span>
<span style="color: #009900;">          <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.data.mongodb.core.MongoTemplate&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor-arg</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;mongo&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor-arg</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;databaseName&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;test&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;urlStatsDao&quot;</span> </span>
<span style="color: #009900;">          <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;net.nirmalya.urlpopularitycounter.dao.mongo.UrlStatsDaoImpl&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">autowire</span>=<span style="color: #ff0000;">&quot;byType&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- other bean definitions omitted for sake of brevity --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><big><strong>Some Code</strong></big><br />
The DAO,</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> UrlStatsDao <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> UrlStats find<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>UrlStats<span style="color: #339933;">&gt;</span> findLeastPopular<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>UrlStats<span style="color: #339933;">&gt;</span> findMostPopular<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>UrlStats<span style="color: #339933;">&gt;</span> findMostRecent<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>UrlStats<span style="color: #339933;">&gt;</span> findOldest<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> insert<span style="color: #009900;">&#40;</span>UrlStats urlStats<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> insertOrUpdate<span style="color: #009900;">&#40;</span>UrlStats urlStats<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> remove<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> Map<span style="color: #339933;">&lt;</span>Integer, Integer<span style="color: #339933;">&gt;</span> retrieveNumUrlsGroupBy<span style="color: #009900;">&#40;</span>GroupByTime groupByTime<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// .. other methods </span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Partial implementation of <code>UrlStatsDaoImpl</code>,</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">springframework</span>.<span style="color: #006633;">data</span>.<span style="color: #006633;">mongodb</span>.<span style="color: #006633;">core</span>.<span style="color: #006633;">query</span>.<span style="color: #006633;">Criteria</span>.<span style="color: #006633;">where</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">net.nirmalya.urlpopularitycounter.UrlStats</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">net.nirmalya.urlpopularitycounter.dao.UrlStatsDao</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.beans.factory.annotation.Autowired</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.data.mongodb.core.MongoOperations</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.data.mongodb.core.query.Order</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.data.mongodb.core.query.Query</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.data.mongodb.core.query.Sort</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.data.mongodb.core.query.Update</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UrlStatsDaoImpl <span style="color: #000000; font-weight: bold;">implements</span> UrlStatsDao <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// .. other methods omitted for sake of brevity </span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> insertOrUpdate<span style="color: #009900;">&#40;</span>UrlStats urlStats<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        Update u <span style="color: #339933;">=</span> update<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;facebookComments&quot;</span>, urlStats.<span style="color: #006633;">getFacebookComments</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>
        u <span style="color: #339933;">=</span> update<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;facebookShares&quot;</span>, urlStats.<span style="color: #006633;">getFacebookShares</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, u<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        u <span style="color: #339933;">=</span> update<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;googlePlusOneCount&quot;</span>, urlStats.<span style="color: #006633;">getGooglePlusOneCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, u<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        u <span style="color: #339933;">=</span> update<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;linkedInCount&quot;</span>, urlStats.<span style="color: #006633;">getLinkedInCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, u<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        u <span style="color: #339933;">=</span> update<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;twitterCount&quot;</span>, urlStats.<span style="color: #006633;">getTwitterCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, u<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        u <span style="color: #339933;">=</span> update<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;total&quot;</span>, urlStats.<span style="color: #006633;">getTotal</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, u<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        u <span style="color: #339933;">=</span> update<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;dateAdded&quot;</span>, urlStats.<span style="color: #006633;">getDateAdded</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, u<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>urlStats.<span style="color: #006633;">getTotal</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            u.<span style="color: #006633;">addToSet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;totalHistory&quot;</span>, urlStats.<span style="color: #006633;">getTotal</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        upsert<span style="color: #009900;">&#40;</span>urlStats, u<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> Update update<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> key, <span style="color: #003399;">Object</span> v, Update u<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>v <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span>
                <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span>v.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #003399;">Integer</span>.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span><span style="color: #009900;">&#41;</span> v<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">intValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
                <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span>v.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #003399;">Date</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>u <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                u <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Update<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            u.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span>key, v<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> u<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> upsert<span style="color: #009900;">&#40;</span>UrlStats urlStats, Update u<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>u <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">Date</span> lastUpdated <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            u.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;lastUpdated&quot;</span>, lastUpdated<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            u.<span style="color: #006633;">addToSet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;lastUpdatedHistory&quot;</span>, lastUpdated<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            mongoTemplate.<span style="color: #006633;">upsert</span><span style="color: #009900;">&#40;</span>getQuery<span style="color: #009900;">&#40;</span>urlStats.<span style="color: #006633;">getUrl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>, u, UrlStats.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            mongoTemplate.<span style="color: #006633;">save</span><span style="color: #009900;">&#40;</span>urlStats, collectionName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Partial implementation of <code>UrlStatsWorker</code>,</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UrlStatsWorker <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Runnable</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">volatile</span> <span style="color: #000066; font-weight: bold;">boolean</span> stopRequested <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> threadPoolSize <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> Log logger <span style="color: #339933;">=</span> LogFactory.<span style="color: #006633;">getLog</span><span style="color: #009900;">&#40;</span>UrlStatsWorker.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> UrlStatsDao urlStatsDao<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// .. other methods omitted for sake of brevity </span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> updateStats<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> trackedUrl, ExecutorService execSrvc<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        LinkedHashMap<span style="color: #339933;">&lt;</span>String, Integer<span style="color: #339933;">&gt;</span> counts <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkedHashMap<span style="color: #339933;">&lt;</span>String, Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            Collection<span style="color: #339933;">&lt;</span>Callable<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;&gt;</span> tasks <span style="color: #339933;">=</span> 
              <span style="color: #000000; font-weight: bold;">new</span> LinkedList<span style="color: #339933;">&lt;</span>Callable<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> names <span style="color: #339933;">=</span> 
              <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;FaceBookShares&quot;</span>, <span style="color: #0000ff;">&quot;GooglePlusOneCount&quot;</span>, 
                <span style="color: #0000ff;">&quot;LinkedInCount&quot;</span>, <span style="color: #0000ff;">&quot;TwitterCount&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
            tasks.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> FacebookWorker<span style="color: #009900;">&#40;</span>trackedUrl<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            tasks.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> GooglePlusOneWorker<span style="color: #009900;">&#40;</span>trackedUrl<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            tasks.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> LinkedInWorker<span style="color: #009900;">&#40;</span>trackedUrl<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            tasks.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> TwitterWorker<span style="color: #009900;">&#40;</span>trackedUrl<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            List<span style="color: #339933;">&lt;</span>Future<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;&gt;</span> list <span style="color: #339933;">=</span> execSrvc.<span style="color: #006633;">invokeAll</span><span style="color: #009900;">&#40;</span>tasks<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">int</span> index <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Future<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> f <span style="color: #339933;">:</span> list<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                counts.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>names<span style="color: #009900;">&#91;</span>index<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span>, f.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            logger.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Saving / updating ..</span>
        UrlStats urlStatsB4 <span style="color: #339933;">=</span> urlStatsDao.<span style="color: #006633;">find</span><span style="color: #009900;">&#40;</span>trackedUrl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        UrlStats urlStatsAfter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UrlStats<span style="color: #009900;">&#40;</span>trackedUrl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        urlStatsAfter.<span style="color: #006633;">setFacebookComments</span><span style="color: #009900;">&#40;</span>counts.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;FaceBookShares&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        urlStatsAfter.<span style="color: #006633;">setGooglePlusOneCount</span><span style="color: #009900;">&#40;</span>counts.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;GooglePlusOneCount&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        urlStatsAfter.<span style="color: #006633;">setLinkedInCount</span><span style="color: #009900;">&#40;</span>counts.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;LinkedInCount&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        urlStatsAfter.<span style="color: #006633;">setTwitterCount</span><span style="color: #009900;">&#40;</span>counts.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;TwitterCount&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        urlStatsAfter.<span style="color: #006633;">setDateAdded</span><span style="color: #009900;">&#40;</span>urlStatsB4.<span style="color: #006633;">getDateAdded</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>urlStatsAfter.<span style="color: #006633;">getDateAdded</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> <span style="color: #009900;">&#123;</span>
            urlStatsAfter.<span style="color: #006633;">setDateAdded</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        urlStatsDao.<span style="color: #006633;">insertOrUpdate</span><span style="color: #009900;">&#40;</span>urlStatsAfter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Partial implementation of one of the social media counter classes (<code>GooglePlusOneWorker</code>),</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> GooglePlusOneWorker <span style="color: #000000; font-weight: bold;">implements</span> Callable<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> Log logger <span style="color: #339933;">=</span> LogFactory.<span style="color: #006633;">getLog</span><span style="color: #009900;">&#40;</span>GooglePlusOneWorker.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> trackedUrl<span style="color: #339933;">;</span>
&nbsp;
    GooglePlusOneWorker<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> trackedUrl<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">trackedUrl</span> <span style="color: #339933;">=</span> trackedUrl<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Integer</span> call<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">int</span> count <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> urlForStats <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot; .. &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> data <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot; .. &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Do a HTTP Post ..</span>
            <span style="color: #666666; font-style: italic;">// Read the reply from the server</span>
            <span style="color: #666666; font-style: italic;">// Parse the JSON string returned - use JsonPath.read(jsonString, &quot;path&quot;)</span>
            <span style="color: #003399;">Double</span> countd <span style="color: #339933;">=</span> JsonPath.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>replyFromServer, <span style="color: #0000ff;">&quot;.. xpath ..&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            count <span style="color: #339933;">=</span> countd.<span style="color: #006633;">intValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            logger.<span style="color: #006633;">warn</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> count<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The representation of the &#8220;document&#8221; (<i>think of it as a table record for those new to NoSQL</i>)</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UrlStats <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> dateAdded, lastUpdated<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> facebookComments, facebookShares, linkedInCount,
      googlePlusOneCount, total, twitterCount<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> url, urlTitle<span style="color: #339933;">;</span>  
  <span style="color: #666666; font-style: italic;">// .. getter/setter methods omitted for sake of brevity</span></pre></div></div>

<p><big><strong>&#8220;Documents&#8221;</strong></big><br />
Command <code>db.urlStats.find({}).limit(2).pretty();</code> on the Mongo shell returns results similar to</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">&quot;_id&quot;</span> <span style="color: #339933;">:</span> ObjectId<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;4f39bf6af466eb9eb443e3cb&quot;</span><span style="color: #009900;">&#41;</span>,
    <span style="color: #0000ff;">&quot;dateAdded&quot;</span> <span style="color: #339933;">:</span> ISODate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2012-02-14T01:56:58.321Z&quot;</span><span style="color: #009900;">&#41;</span>,
    <span style="color: #0000ff;">&quot;facebookComments&quot;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">2196</span>,
    <span style="color: #0000ff;">&quot;googlePlusOneCount&quot;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">36</span>,
    <span style="color: #0000ff;">&quot;lastUpdated&quot;</span> <span style="color: #339933;">:</span> ISODate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2012-02-23T06:27:34.651Z&quot;</span><span style="color: #009900;">&#41;</span>,
    <span style="color: #0000ff;">&quot;lastUpdatedHistory&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
        ISODate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2012-02-14T01:57:01.178Z&quot;</span><span style="color: #009900;">&#41;</span>, ISODate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2012-02-15T09:39:01.981Z&quot;</span><span style="color: #009900;">&#41;</span>,
        ISODate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2012-02-16T08:02:31.182Z&quot;</span><span style="color: #009900;">&#41;</span>, ISODate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2012-02-17T10:34:31.260Z&quot;</span><span style="color: #009900;">&#41;</span>,
        ISODate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2012-02-18T16:18:35.770Z&quot;</span><span style="color: #009900;">&#41;</span>, ISODate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2012-02-19T21:40:32.719Z&quot;</span><span style="color: #009900;">&#41;</span>,
        ISODate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2012-02-20T00:06:32.823Z&quot;</span><span style="color: #009900;">&#41;</span>, ISODate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2012-02-21T23:31:30.684Z&quot;</span><span style="color: #009900;">&#41;</span>,
        ISODate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2012-02-22T20:54:32.098Z&quot;</span><span style="color: #009900;">&#41;</span>, ISODate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2012-02-23T06:27:34.651Z&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#93;</span>,
    <span style="color: #0000ff;">&quot;linkedInCount&quot;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">163</span>,
    <span style="color: #0000ff;">&quot;total&quot;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">4258</span>,
    <span style="color: #0000ff;">&quot;totalHistory&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">4119</span>, <span style="color: #cc66cc;">4203</span>, <span style="color: #cc66cc;">4223</span>, <span style="color: #cc66cc;">4240</span>, <span style="color: #cc66cc;">4250</span>, <span style="color: #cc66cc;">4254</span>, <span style="color: #cc66cc;">4255</span>, <span style="color: #cc66cc;">4256</span>, <span style="color: #cc66cc;">4257</span>, <span style="color: #cc66cc;">4258</span> <span style="color: #009900;">&#93;</span>,
    <span style="color: #0000ff;">&quot;twitterCount&quot;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1863</span>,
    <span style="color: #0000ff;">&quot;url&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;http://www.bbc.co.uk/nature/16944753&quot;</span>,
    <span style="color: #0000ff;">&quot;urlTitle&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Why Zebra evolved stripes?&quot;</span>
<span style="color: #009900;">&#125;</span>,
<span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">&quot;_id&quot;</span> <span style="color: #339933;">:</span> ObjectId<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;4f32408d88d848ee8340aaed&quot;</span><span style="color: #009900;">&#41;</span>,
    <span style="color: #0000ff;">&quot;dateAdded&quot;</span> <span style="color: #339933;">:</span> ISODate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2012-02-01T00:00:00Z&quot;</span><span style="color: #009900;">&#41;</span>,
    <span style="color: #0000ff;">&quot;facebookComments&quot;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">31</span>,
    <span style="color: #0000ff;">&quot;googlePlusOneCount&quot;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">19</span>,
    <span style="color: #0000ff;">&quot;lastUpdated&quot;</span> <span style="color: #339933;">:</span> ISODate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2012-02-11T20:08:37.609Z&quot;</span><span style="color: #009900;">&#41;</span>,
    <span style="color: #0000ff;">&quot;lastUpdatedHistory&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
        ISODate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2012-02-08T09:30:01.186Z&quot;</span><span style="color: #009900;">&#41;</span>, ISODate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2012-02-09T10:29:04.646Z&quot;</span><span style="color: #009900;">&#41;</span>,
        ISODate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2012-02-10T09:43:31.200Z&quot;</span><span style="color: #009900;">&#41;</span>, ISODate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2012-02-11T20:08:37.609Z&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#93;</span>,
    <span style="color: #0000ff;">&quot;linkedInCount&quot;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">10</span>,
    <span style="color: #0000ff;">&quot;total&quot;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">240</span>,
    <span style="color: #0000ff;">&quot;totalHistory&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">221</span>, <span style="color: #cc66cc;">229</span>, <span style="color: #cc66cc;">230</span>, <span style="color: #cc66cc;">240</span> <span style="color: #009900;">&#93;</span>,
    <span style="color: #0000ff;">&quot;twitterCount&quot;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">180</span>,
    <span style="color: #0000ff;">&quot;url&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;http://www.engadget.com/2012/02/01/german-court-denies-apple-request-for-preliminary-ban-on-galaxy/&quot;</span>,
    <span style="color: #0000ff;">&quot;urlTitle&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;German court denies Apple request for preliminary ban on Galaxy Tab 10.1N and Galaxy Nexus sales&quot;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><big><strong>Map Reduce</strong></big></p>
<p>Using MongoDB&#8217;s <a href="http://www.mongodb.org/display/DOCS/MapReduce" target="_blank">Map Reduce feature</a> to count number of documents grouped by a unit of time (years, months, days, hours, minutes, seconds, milliseconds). Added the following method to the <code>UrlStatsDaoImpl</code>,</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> Map<span style="color: #339933;">&lt;</span>Integer, Integer<span style="color: #339933;">&gt;</span> retrieveNumUrlsGroupBy<span style="color: #009900;">&#40;</span>GroupByTime groupByTime<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span> mapFunction <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;function() { &quot;</span> <span style="color: #339933;">+</span>
          <span style="color: #0000ff;">&quot;var ts_x = this.ts.&quot;</span> <span style="color: #339933;">+</span> groupByTime.<span style="color: #006633;">getMethodName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;;&quot;</span> <span style="color: #339933;">+</span> 
          <span style="color: #0000ff;">&quot;emit(ts_x, 1); }&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> reduceFunction <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;function(key, values) { &quot;</span> <span style="color: #339933;">+</span>
          <span style="color: #0000ff;">&quot; var total = 0; &quot;</span> <span style="color: #339933;">+</span> 
          <span style="color: #0000ff;">&quot; for(var i = 0; i &lt; values.length; i++) { &quot;</span> <span style="color: #339933;">+</span> 
          <span style="color: #0000ff;">&quot;   total += values[i]; } &quot;</span> <span style="color: #339933;">+</span>
          <span style="color: #0000ff;">&quot; return total; }&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        TreeMap<span style="color: #339933;">&lt;</span>Integer, Integer<span style="color: #339933;">&gt;</span> mrResults <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TreeMap<span style="color: #339933;">&lt;</span>Integer, Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        @SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;rawtypes&quot;</span><span style="color: #009900;">&#41;</span>
        MapReduceResults<span style="color: #339933;">&lt;</span>Map<span style="color: #339933;">&gt;</span> results <span style="color: #339933;">=</span> mongoTemplate.<span style="color: #006633;">mapReduce</span><span style="color: #009900;">&#40;</span>
          collectionName, 
          mapFunction, reduceFunction,
          <span style="color: #003399;">Map</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>results.<span style="color: #006633;">getRawResults</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> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">Object</span> obj <span style="color: #339933;">=</span> results.<span style="color: #006633;">getRawResults</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;results&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                BasicDBList list <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>BasicDBList<span style="color: #009900;">&#41;</span> obj<span style="color: #339933;">;</span>
                Iterator<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span> it <span style="color: #339933;">=</span> list.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>it.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    BasicDBObject lItem <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>BasicDBObject<span style="color: #009900;">&#41;</span> it.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    @SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
                    Map<span style="color: #339933;">&lt;</span>String, Double<span style="color: #339933;">&gt;</span> lItemMap <span style="color: #339933;">=</span> lItem.<span style="color: #006633;">toMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lItemMap <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        mrResults.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>lItemMap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;_id&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">intValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, 
                          lItemMap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">intValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> mrResults<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>and added a new Enum <code>GroupByTime</code>,</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">enum</span> GroupByTime <span style="color: #009900;">&#123;</span>
    YEARS<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;getYear()&quot;</span><span style="color: #009900;">&#41;</span>, MONTHS<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;getMonth()&quot;</span><span style="color: #009900;">&#41;</span>, DAYS<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;getDate()&quot;</span><span style="color: #009900;">&#41;</span>, HOURS<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;getHours()&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> methodName<span style="color: #339933;">;</span>
&nbsp;
    GroupByTime<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> methodNameSpecified<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">methodName</span> <span style="color: #339933;">=</span> methodNameSpecified<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getMethodName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> methodName<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A simple JUnit test helps verify</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    @Test
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">void</span> testRetrieveNumUrlsGroupByXYZ<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        ApplicationContext ctx <span style="color: #339933;">=</span> 
          <span style="color: #000000; font-weight: bold;">new</span> GenericXmlApplicationContext<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;classpath:/applicationContext.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        UrlStatsDao dao <span style="color: #339933;">=</span> 
          <span style="color: #009900;">&#40;</span>UrlStatsDao<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ctx<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> ctx.<span style="color: #006633;">getBean</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;urlStatsDao&quot;</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><span style="color: #339933;">;</span>
        assertNotNull<span style="color: #009900;">&#40;</span>dao<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Map<span style="color: #339933;">&lt;</span>Integer, Integer<span style="color: #339933;">&gt;</span> numUrlsGroupedByDays <span style="color: #339933;">=</span> 
          dao.<span style="color: #006633;">retrieveNumUrlsGroupBy</span><span style="color: #009900;">&#40;</span>GroupByTime.<span style="color: #006633;">DAYS</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        assertNotNull<span style="color: #009900;">&#40;</span>numUrlsGroupedByDays<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;numUrlsGroupedByDays = &quot;</span> <span style="color: #339933;">+</span> numUrlsGroupedByDays<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The Map Reduce results look similar to <code>{1=1, 2=21, 8=788, .. , 21=272, 22=299, 23=68}</code> where the keys represent the day of the month and the number of URLs added on that particular day.</p>
<p>Hope the snippets helps you in your project.</p>
<p>References</p>
<ul>
<li><a href="http://static.springsource.org/spring-data/data-document/docs/current/reference/html/" target="_blank">http://static.springsource.org/spring-data/data-document/docs/current/reference/html/</a></li>
<li><a href="http://www.vogella.de/articles/RSSFeed/article.html" target="_blank">http://www.vogella.de/articles/RSSFeed/article.html</a></li>
<li><a href="http://code.google.com/p/json-path/" target="_blank">http://code.google.com/p/json-path/</a></li>
</ul>
<p><font color="red">[Update #1, Date : 15 April 2012]</font><br />
Previously <code>spring-data-mongodb</code> version 1.0.0.RC1 worked with <code>spring-context-support</code> version 3.0.6.RELEASE. It should now be <code>spring-data-mongodb</code> version 1.0.1.RELEASE with <code>spring-context-support</code> version 3.1.0.RELEASE.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.nirmalya.net/articles/java/social-media-counter-using-mongodb-and-spring-data/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing Tomcat 7 On Ubuntu And Accessing It Remotely</title>
		<link>http://dev.nirmalya.net/articles/setup/tomcat-7-ubuntu-remote-access</link>
		<comments>http://dev.nirmalya.net/articles/setup/tomcat-7-ubuntu-remote-access#comments</comments>
		<pubDate>Wed, 22 Feb 2012 07:23:24 +0000</pubDate>
		<dc:creator>nirmalya</dc:creator>
				<category><![CDATA[Setup]]></category>
		<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://dev.nirmalya.net/articles/?p=364</guid>
		<description><![CDATA[I installed Tomcat 7.0.26 on a standard.xsmall instance with a Ubuntu 11.04 image following some pretty good instructions on Diego Benna&#8217;s blog. After updating the required configuration files, I was able to access the server locally, but unable to access remotely. For future reference, listed below are steps I followed to resolve it. Installing Java [...]]]></description>
			<content:encoded><![CDATA[<p>I installed Tomcat 7.0.26 on a <a href="http://www.hpcloud.com/products/cloud-compute" target="_blank">standard.xsmall instance</a> with a Ubuntu 11.04 image following some <a href="http://diegobenna.blogspot.com/2011/01/install-tomcat-7-in-ubuntu-1010.html" target="_blank">pretty good instructions on Diego Benna&#8217;s blog</a>. After updating the required configuration files, I was able to access the server locally, but unable to access remotely. For future reference, listed below are steps I followed to resolve it.<br />
<span id="more-364"></span><br />
<big><strong>Installing Java 7</strong></big><br />
Please refer to the excellent instructions at <a href="http://brunoreis.com/tech/intalling-java-ubuntu-natty/" target="_blank">http://brunoreis.com/tech/intalling-java-ubuntu-natty/</a>.</p>
<p><big><strong>Installing Tomcat 7.0.26</strong></big><br />
Preparation</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">tar</span> xvzf apache-tomcat-7.0.26.tar.gz
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> apache-tomcat-7.0.26<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>tomcat7</pre></div></div>

<p>Next, edit <code>/etc/environment</code>. Contents of this file</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">JAVA_HOME</span>=<span style="color: #ff0000;">&quot;/usr/lib/jvm/java-7-sun/&quot;</span>
<span style="color: #007800;">JRE_HOME</span>=<span style="color: #ff0000;">&quot;/usr/lib/jvm/java-7-sun/jre&quot;</span>
<span style="color: #007800;">PATH</span>=<span style="color: #ff0000;">&quot;/usr/local/sbin:/usr/games:<span style="color: #007800;">$JAVA_HOME</span>:<span style="color: #007800;">$JRE_HOME</span>&quot;</span></pre></div></div>

<p>Next, edit <code>/usr/share/tomcat7/bin/catalina.sh</code>. Add in these 2 lines just after the <code>#!/bin/sh</code></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">JAVA_HOME</span>=<span style="color: #ff0000;">&quot;/usr/lib/jvm/java-7-sun/&quot;</span>
<span style="color: #007800;">JRE_HOME</span>=<span style="color: #ff0000;">&quot;/usr/lib/jvm/java-7-sun/jre&quot;</span></pre></div></div>

<p>Next, edit <code>/usr/share/tomcat7/conf/tomcat-users.xml</code>.<br />
Add the following just before the end of the <code>tomcat-users</code> element.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;user</span> <span style="color: #000066;">username</span>=<span style="color: #ff0000;">&quot;yourAdminUsername&quot;</span> <span style="color: #000066;">password</span>=<span style="color: #ff0000;">&quot;yourAdminPassword&quot;</span></span>
<span style="color: #009900;"> <span style="color: #000066;">roles</span>=<span style="color: #ff0000;">&quot;manager-gui,admin-gui,manager,admin,manager-script,admin-script&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p><big><strong>Starting up Tomcat</strong></big></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>tomcat7<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>startup.sh</pre></div></div>

<p>Since I do not have local access to my instance, I used wget to check the local access.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>tmp
$ <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>127.0.0.1:<span style="color: #000000;">8080</span></pre></div></div>

<p>The contents are written to <code>index.html</code>. You can then try to <code>wget http://your-instance-public-ip:8080</code>, but most likely it will not work at this point.</p>
<p><big><strong>Configuring The Firewall</strong></big><br />
Please refer to the excellent instructions at <a href="http://chiralsoftware.com/linux-system-administration/ubuntu-firewall-iptables.seam" target="_blank">http://chiralsoftware.com/linux-system-administration/ubuntu-firewall-iptables.seam</a>. Summarized below</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ iptables <span style="color: #660033;">-I</span> INPUT <span style="color: #000000;">1</span> <span style="color: #660033;">-i</span> lo <span style="color: #660033;">-j</span> ACCEPT
$ iptables <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-m</span> state <span style="color: #660033;">--state</span> ESTABLISHED,RELATED <span style="color: #660033;">-j</span> ACCEPT
$ iptables <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">80</span> <span style="color: #660033;">-j</span> ACCEPT
$ iptables <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">22</span> <span style="color: #660033;">-j</span> ACCEPT
$ iptables <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-j</span> DROP
$ iptables <span style="color: #660033;">-t</span> nat <span style="color: #660033;">-A</span> PREROUTING <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">80</span> <span style="color: #660033;">-j</span> REDIRECT <span style="color: #660033;">--to-port</span> <span style="color: #000000;">8080</span>
$ iptables <span style="color: #660033;">-I</span> INPUT <span style="color: #000000;">3</span> <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">8080</span> <span style="color: #660033;">-j</span> ACCEPT
$ iptables <span style="color: #660033;">-t</span> nat <span style="color: #660033;">-A</span> OUTPUT <span style="color: #660033;">-o</span> lo <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">80</span> <span style="color: #660033;">-j</span> REDIRECT <span style="color: #660033;">--to-port</span> <span style="color: #000000;">8080</span>
$ iptables-save <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>iptables.rules</pre></div></div>

<p>Next, reboot your instance, restart Tomcat and you should now be able to access http://your-instance-public-ip</p>
<p>Hope this helps.</p>
<p><big><strong>References</strong></big></p>
<ul>
<li><a href="http://brunoreis.com/tech/intalling-java-ubuntu-natty/" target="_blank">http://brunoreis.com/tech/intalling-java-ubuntu-natty/</a></li>
<li><a href="http://diegobenna.blogspot.com/2011/01/install-tomcat-7-in-ubuntu-1010.html" target="_blank">http://diegobenna.blogspot.com/2011/01/install-tomcat-7-in-ubuntu-1010.html</a></li>
<li><a href="http://chiralsoftware.com/linux-system-administration/ubuntu-firewall-iptables.seam" target="_blank">http://chiralsoftware.com/linux-system-administration/ubuntu-firewall-iptables.seam</a></li>
<li><a href="http://learnedstuffs.wordpress.com/2011/07/16/enabling-remote-access-on-apache-tomcat/" target="_blank">http://learnedstuffs.wordpress.com/2011/07/16/enabling-remote-access-on-apache-tomcat/</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://dev.nirmalya.net/articles/setup/tomcat-7-ubuntu-remote-access/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SimpleJPA + Maven + Dependencies</title>
		<link>http://dev.nirmalya.net/articles/java/simplejpa-maven-dependencies</link>
		<comments>http://dev.nirmalya.net/articles/java/simplejpa-maven-dependencies#comments</comments>
		<pubDate>Sun, 01 Jan 2012 19:44:28 +0000</pubDate>
		<dc:creator>nirmalya</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Setup]]></category>
		<category><![CDATA[AWS]]></category>
		<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[SimpleDB]]></category>
		<category><![CDATA[SimpleJPA]]></category>

		<guid isPermaLink="false">http://dev.nirmalya.net/articles/?p=342</guid>
		<description><![CDATA[Whilst I am very grateful to the folks who developed SimpleJPA, I found it a bit problematic building (with Maven) a project having SimpleJPA as of its dependencies. In this article, I attempt to make it easier for others who intend to use Maven for building their project which uses SimpleJPA. The Need To Add SimpleJPA To [...]]]></description>
			<content:encoded><![CDATA[<p>Whilst I am very grateful to the folks who developed <a href="http://code.google.com/p/simplejpa/" target="_blank">SimpleJPA</a>, I found it a bit problematic building (with <a href="http://maven.apache.org/" target="_blank">Maven</a>) a project having SimpleJPA as of its dependencies. In this article, I attempt to make it easier for others who intend to use Maven for building their project which uses SimpleJPA.</p>
<p><span id="more-342"></span><br />
<big><strong>The Need To Add SimpleJPA To Maven Local Repository</strong></big><br />
To begin with, (as of now) one cannot just add in a dependency similar to the one below and expect the build to proceed smoothly.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.spaceprogram<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>simplejpa<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>I had to download the SimpleJPA JAR file and it to my maven local repository. To do so, you may use the command,</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">mvn install:install-file
 -Dfile=PathToJARFile\simplejpa-1.5.jar
 -DgroupId=com.spaceprogram
 -DartifactId=simplejpa -Dversion=1.5 -Dpackaging=jar -DgeneratePom=true</pre></div></div>

<p><big><strong>The Need To Manually Manage SimpleJPA&#8217;s Dependencies</strong></big><br />
SimpleJPA&#8217;s<a href="http://code.google.com/p/simplejpa/wiki/GettingStarted" target="_blank">GettingStarted</a> Wiki page lists its dependencies. Since I couldn&#8217;t find a working Maven <code>pom.xml</code> for SimpleJPA, I created one and pasted below is the <code>dependencies</code> section which you can just copy into your project&#8217;s <code>pom.xml</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>cglib<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>cglib-nodep<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.2.2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.amazonaws<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>aws-java-sdk<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.002<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.spaceprogram<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>kitty-cache<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>commons-codec<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>commons-codec<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>commons-collections<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>commons-collections<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3.2.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>commons-lang<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>commons-lang<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>commons-logging<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>commons-logging<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.1.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>junit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>junit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3.8.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <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>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>net.sf.ehcache<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>ehcache-core<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.5.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>net.sf.scannotation<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>scannotation<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <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.apache.httpcomponents<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>httpclient<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.1.2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <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.hibernate<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>ejb3-persistence<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.2.GA<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <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.javassist<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javassist<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3.15.0-GA<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- SimpleJPA --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.spaceprogram<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>simplejpa<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Please take note</p>
<ul>
<li>GroupIds, ArtifcatIds, versions determined using <a href="http://search.maven.org/#search" target="_blank">http://search.maven.org/#search</a></li>
<li>You will also need to download <span style="color: #3366ff;"><strong><code>ejb3-persistence-1.0.2.GA.jar</code></strong></span> from <a href="http://mirrors.ibiblio.org/pub/mirrors/maven2/org/hibernate/ejb3-persistence/1.0.2.GA/" target="_blank">here</a> and add to maven local repository using the command

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">mvn install:install-file
 -Dfile=PathToJARFile\ejb3-persistence-1.0.2.GA.jar
 -DgroupId=org.hibernate
 -DartifactId=ejb3-persistence
 -Dversion=1.0.2.GA -Dpackaging=jar
 -DgeneratePom=false</pre></div></div>

</li>
<li>You will also need to download <span style="color: #3366ff;"><strong><code>KittyCache-1.2.jar</code></strong></span> from <a href="http://code.google.com/p/kitty-cache/downloads/list" target="_blank">here</a> and add to maven local repository using the command

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">mvn install:install-file
 -Dfile=PathToJARFile\KittyCache-1.2.jar
 -DgroupId=com.spaceprogram
 -DartifactId=kitty-cache
 -Dversion=1.2 -Dpackaging=jar -DgeneratePom=true</pre></div></div>

</li>
</ul>
<p><big><strong>References:</strong></big></p>
<ul>
<li><a title="Add a jar to maven local repository, link opens in a new tab or window" href="http://wrongdoor.wordpress.com/2010/11/28/add-a-jar-to-maven-local-repository/" target="_blank">http://wrongdoor.wordpress.com/2010/11/28/add-a-jar-to-maven-local-repository/</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://dev.nirmalya.net/articles/java/simplejpa-maven-dependencies/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Regex Pattern In XSD For Comma-Separated Attribute Value</title>
		<link>http://dev.nirmalya.net/articles/uncategorized/regex-pattern-in-xsd-for-comma-separated-attribute-value</link>
		<comments>http://dev.nirmalya.net/articles/uncategorized/regex-pattern-in-xsd-for-comma-separated-attribute-value#comments</comments>
		<pubDate>Wed, 16 Jun 2010 07:40:14 +0000</pubDate>
		<dc:creator>nirmalya</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[xsd]]></category>

		<guid isPermaLink="false">http://dev.nirmalya.net/articles/?p=338</guid>
		<description><![CDATA[Relevant Section Of The XML &#60;my_element xyz=&#34;ABC3&#34;/&#62; &#60;my_element xyz=&#34;ABC21,a1&#34;/&#62; Relevant Section Of The XSD 1 2 3 4 5 6 7 8 9 10 11 &#60;xs:element name=&#34;my_element&#34; maxOccurs=&#34;unbounded&#34; minOccurs=&#34;1&#34;&#62; &#60;xs:complexType&#62; &#60;xs:attribute name=&#34;xyz&#34; use=&#34;required&#34;&#62; &#60;xs:simpleType&#62; &#60;xs:restriction base=&#34;xs:string&#34;&#62; &#60;xs:pattern value=&#34;[a-zA-Z0-9]+[,a-zA-Z0-9]*&#34;/&#62; &#60;/xs:restriction&#62; &#60;/xs:simpleType&#62; &#60;/xs:attribute&#62; &#60;/xs:complexType&#62; &#60;/xs:element&#62;]]></description>
			<content:encoded><![CDATA[<p><strong>Relevant Section Of The XML</strong></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;my_element</span> <span style="color: #000066;">xyz</span>=<span style="color: #ff0000;">&quot;ABC3&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;my_element</span> <span style="color: #000066;">xyz</span>=<span style="color: #ff0000;">&quot;ABC21,a1&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p><strong>Relevant Section Of The XSD</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;my_element&quot;</span> <span style="color: #000066;">maxOccurs</span>=<span style="color: #ff0000;">&quot;unbounded&quot;</span> <span style="color: #000066;">minOccurs</span>=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xs:complexType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xs:attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;xyz&quot;</span> <span style="color: #000066;">use</span>=<span style="color: #ff0000;">&quot;required&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xs:simpleType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xs:restriction</span> <span style="color: #000066;">base</span>=<span style="color: #ff0000;">&quot;xs:string&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> 
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xs:pattern</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;[a-zA-Z0-9]+[,a-zA-Z0-9]*&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xs:restriction<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xs:simpleType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xs:attribute<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xs:complexType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xs:element<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://dev.nirmalya.net/articles/uncategorized/regex-pattern-in-xsd-for-comma-separated-attribute-value/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Chaining 2 jQuery UI Datepickers</title>
		<link>http://dev.nirmalya.net/articles/javascript/chaining-2-jquery-ui-datepickers</link>
		<comments>http://dev.nirmalya.net/articles/javascript/chaining-2-jquery-ui-datepickers#comments</comments>
		<pubDate>Thu, 10 Jun 2010 07:49:51 +0000</pubDate>
		<dc:creator>nirmalya</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://dev.nirmalya.net/articles/?p=331</guid>
		<description><![CDATA[A simple example to chain 2 jQuery UI datepickers such that when a date is selected in the first datepicker (a.k.a. the &#8216;from date&#8216;, date1), the minimum date in the second datepicker (a.k.a. the &#8216;to date&#8216;, date2) is set accordingly. Similarly it handles the case where the &#8216;to date&#8216; is selected before the &#8216;from date&#8216;, [...]]]></description>
			<content:encoded><![CDATA[<p>A simple example to chain 2 jQuery UI datepickers such that when a date is selected in the first datepicker (a.k.a. the &#8216;<i>from date</i>&#8216;, <code>date1</code>), the minimum date in the second datepicker (a.k.a. the &#8216;<i>to date</i>&#8216;, <code>date2</code>) is set accordingly. Similarly it handles the case where the &#8216;<i>to date</i>&#8216; is selected before the &#8216;<i>from date</i>&#8216;, in which case the maximum date in the &#8216;<i>from date</i>&#8216; is lesser than the &#8216;<i>to date</i>&#8216; but before current date. </p>
<p><strong>Relevant HTML For Datepicker Placeholders</strong></p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;date1&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;date1&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;date2&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;date2&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span></pre></div></div>

<p><strong>Relevant jQuery Setup Code</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
  $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#date1'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">datepicker</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
      autoSize<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> 
      constrainInput<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> 
      dateFormat<span style="color: #339933;">:</span> <span style="color: #3366CC;">'ddMyy'</span><span style="color: #339933;">,</span> 
      minDate<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> 
      maxDate<span style="color: #339933;">:</span> <span style="color: #3366CC;">'+90D'</span><span style="color: #339933;">,</span> 
      onSelect<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>dateText<span style="color: #339933;">,</span> inst<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#date2'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">datepicker</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;option&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;minDate&quot;</span><span style="color: #339933;">,</span>dateText<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
      <span style="color: #009900;">&#125;</span> 
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#date2'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">datepicker</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
      autoSize<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> 
      constrainInput<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> 
      dateFormat<span style="color: #339933;">:</span> <span style="color: #3366CC;">'ddMyy'</span><span style="color: #339933;">,</span> 
      minDate<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> 
      maxDate<span style="color: #339933;">:</span> <span style="color: #3366CC;">'+90D'</span><span style="color: #339933;">,</span> 
      onSelect<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>dateText<span style="color: #339933;">,</span> inst<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#date1'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">datepicker</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;option&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;maxDate&quot;</span><span style="color: #339933;">,</span>dateText<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
      <span style="color: #009900;">&#125;</span> 
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">//Other setup</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>In the jQuery setup code above, I have also set the following</p>
<ul>
<li>Do not allow selection of dates before current date (<code>minDate: 0</code>)</li>
<li>Do not allow selection of dates 90 days after current date (<code>maxDate: '+90D'</code>)</li>
<li>Date format similar to &#8216;<i>17Jun2010</i>&#8216;</li>
<li>The &#8216;<code>constrainInput: true</code>&#8216; indicates that the input field is constrained to those characters allowed by the current dateFormat.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://dev.nirmalya.net/articles/javascript/chaining-2-jquery-ui-datepickers/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Spring + JPA + Hibernate + Tomcat + EHCache</title>
		<link>http://dev.nirmalya.net/articles/java/spring-jpa-hibernate-tomcat-ehcache</link>
		<comments>http://dev.nirmalya.net/articles/java/spring-jpa-hibernate-tomcat-ehcache#comments</comments>
		<pubDate>Sat, 01 May 2010 17:27:09 +0000</pubDate>
		<dc:creator>nirmalya</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[EHCache]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://dev.nirmalya.net/articles/?p=305</guid>
		<description><![CDATA[Having @ManyToMany(fetch=FetchType.EAGER) attributes can slow down retrieval quite significantly (up to 40 times slower). I recall reading somewhere that FetchType.EAGER is the default for @ManyToMany associations. Also, from experience I noticed that setting FetchType.LAZY caused a org.hibernate.LazyInitializationException thrown with exception message similar to, failed to lazily initialize a collection of role: com.xyz.domain.EntityOne.images, no session or session [...]]]></description>
			<content:encoded><![CDATA[<p>Having <code>@ManyToMany(fetch=FetchType.EAGER)</code> attributes can slow down retrieval quite significantly (up to 40 times slower).</p>
<p>I recall reading somewhere that <code>FetchType.EAGER</code> is the default for <code>@ManyToMany</code> associations. Also, from experience I noticed that setting <code>FetchType.LAZY</code> caused a <code>org.hibernate.LazyInitializationException</code> thrown with exception message similar to, <code><span style="color: #ff0000;">failed to lazily initialize a collection of role</span>: com.xyz.domain.EntityOne.images, <span style="color: #ff0000;">no session or session was closed</span></code>.</p>
<p>So, it seemed that the only other way to quickly reduce the time it took (for the retrieval) was to look into the caching options such as query caching, second-level caching, both of which are supported by Hibernate (the webapp&#8217;s JPA provider). For more information on second-level caching, please refer to <a href="http://www.javalobby.org/java/forums/t48846.html" target="_blank" title="Opens in a new window or tab">this article</a>.</p>
<p>Integrating Spring + JPA + Hibernate + Tomcat + EHCache took me a few hours this afternoon, but the effort paid off. The retrieval is now 40 times faster!</p>
<p>This post is summarizing the setup involved in getting them all to work together.</p>
<p><strong>Relevant Section Of <code>persistence.xml</code>:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;provider<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.hibernate.ejb.HibernatePersistence<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/provider<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;shared-cache-mode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>ENABLE_SELECTIVE<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/shared-cache-mode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernate.cache.provider_class&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;org.hibernate.cache.SingletonEhCacheProvider&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernate.cache.provider_configuration&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/ehcache.xml&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernate.cache.use_second_level_cache&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernate.cache.use_query_cache&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Notes:</p>
<ul>
<li>The <code>persistence.xml</code> is located at <code>{tomcat}/webapps/{your-webapp}/META-INF</code>.</li>
<li>I used <code>org.hibernate.cache.SingletonEhCacheProvider</code> instead of <code>org.hibernate.cache.EhCacheProvider</code>. I was getting a WARN message if I used <code>org.hibernate.cache.EhCacheProvider</code>. I referred to <a href="http://blog.cherouvim.com/singleton-ehcache-cachemanager-warning-fix/" target="_blank" title="Opens in a new window or tab">this post</a> for a fix.</li>
</ul>
<p><strong>Relevant Section Of <code>ehcache.xml</code>:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;diskStore</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;user.dir/mywebapp-special-cache-folder&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;defaultCache</span> <span style="color: #000066;">eternal</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000066;">overflowToDisk</span>=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">maxElementsInMemory</span>=<span style="color: #ff0000;">&quot;1000&quot;</span> <span style="color: #000066;">timeToIdleSeconds</span>=<span style="color: #ff0000;">&quot;30&quot;</span> <span style="color: #000066;">timeToLiveSeconds</span>=<span style="color: #ff0000;">&quot;60&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cache</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;com.xyz.domain.EntityTwo&quot;</span> <span style="color: #000066;">eternal</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000066;">overflowToDisk</span>=<span style="color: #ff0000;">&quot;true&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">maxElementsInMemory</span>=<span style="color: #ff0000;">&quot;1000&quot;</span> <span style="color: #000066;">timeToIdleSeconds</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">timeToLiveSeconds</span>=<span style="color: #ff0000;">&quot;600&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">diskPersistent</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">diskExpiryThreadIntervalSeconds</span>=<span style="color: #ff0000;">&quot;300&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cache</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;com.xyz.domain.EntityOne&quot;</span> <span style="color: #000066;">eternal</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000066;">overflowToDisk</span>=<span style="color: #ff0000;">&quot;true&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">maxElementsInMemory</span>=<span style="color: #ff0000;">&quot;1000&quot;</span> <span style="color: #000066;">timeToIdleSeconds</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">timeToLiveSeconds</span>=<span style="color: #ff0000;">&quot;600&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">diskPersistent</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">diskExpiryThreadIntervalSeconds</span>=<span style="color: #ff0000;">&quot;300&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cache</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;com.xyz.domain.EntityOne.images&quot;</span> <span style="color: #000066;">eternal</span>=<span style="color: #ff0000;">&quot;false&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">overflowToDisk</span>=<span style="color: #ff0000;">&quot;true&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">maxElementsInMemory</span>=<span style="color: #ff0000;">&quot;1000&quot;</span> <span style="color: #000066;">timeToIdleSeconds</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">timeToLiveSeconds</span>=<span style="color: #ff0000;">&quot;600&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">diskPersistent</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">diskExpiryThreadIntervalSeconds</span>=<span style="color: #ff0000;">&quot;300&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>Notes:</p>
<ul>
<li>The <code>ehcache.xml</code> is located at <code>{tomcat}/webapps/{your-webapp}/WEB-INF/classes</code>.</li>
<li>The <code>diskStore</code> element indicates where the files to be used for caching will be stored for the entities I wish to be made persistent to disk. In my web application, the files are stored under <code>{tomcat}/bin/{mywebapp-special-cache-folder}</code></li>
<li><code>diskPersistent="true"</code> indicates that the disk store (for the specific entity) is persistent between cache and VM restarts. Please refer to the <a href="http://ehcache.org/documentation/storage_options.html" target="_blank" title="Opens in a new window or tab">EHCache documentation on disk storage</a> for more information.</li>
</ul>
<p><strong>Relevant JARs:</strong> under <code>{tomcat}/webapps/{your-webapp}/WEB-INF/lib</code></p>
<ul>
<li><code>ehcache-core-2.0.1.jar</code></li>
<li><code>hibernate3.jar</code></li>
<li><code>hibernate-jpa-2.0-api-1.0.0.Final.jar</code></li>
<li><code>slf4j-api-1.5.8.jar</code></li>
<li><code>slf4j-log4j12-1.5.6.jar</code></li>
<li>Spring 3.0.2 JARs..</li>
</ul>
<p><strong>Relevant Section Of Spring Configuration:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- there should be a way out of hardcoding the location of the properties file --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> </span>
<span style="color: #009900;">  <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;location&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/WEB-INF/spring-hes-db.properties&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- ENTITY MANAGER FACTORY --&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;!-- LocalEntityManagerFactoryBean did not work for me --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;emf-p&quot;</span> </span>
<span style="color: #009900;">  <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;persistenceUnitManager&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;pum&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;persistenceUnitName&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;pu1&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;dataSource-p&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jpaVendorAdapter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;database&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;MYSQL&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;showSql&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;generateDdl&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;databasePlatform&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;org.hibernate.dialect.MySQLDialect&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- TRANSACTION MANAGER --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;transactionManager&quot;</span> </span>
<span style="color: #009900;">  <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.orm.jpa.JpaTransactionManager&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;entityManagerFactory&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;emf-p&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;dataSource-p&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- DATA SOURCES --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;dataSource-p&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.jdbc.datasource.DriverManagerDataSource&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;driverClassName&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;com.mysql.jdbc.Driver&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;url&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;jdbc:mysql://${db.host}:${db.port}/${db.name}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${db.username}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${db.password}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- JPA TEMPLATE --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;jpaTemplate&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.orm.jpa.JpaTemplate&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;entityManagerFactory&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;emf-p&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- DAOs --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;entityOneDao&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;hes.db.impl.EntityOneDAOImpl&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jpaTemplate&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;jpaTemplate&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- PERSISTENCE UNIT --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;pum&quot;</span> </span>
<span style="color: #009900;">  <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;persistenceXmlLocations&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>META-INF/persistence.xml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dataSources&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;map<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;remoteDataSource&quot;</span> <span style="color: #000066;">value-ref</span>=<span style="color: #ff0000;">&quot;dataSource-p&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/map<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;defaultDataSource&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;dataSource-p&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Some of this may be redundant and will be cleaned up later.</p>
<p>If you have some better ideas, please do share. Thanks.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.nirmalya.net/articles/java/spring-jpa-hibernate-tomcat-ehcache/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>AnnotationException : Entity X References An Unknown Entity Y</title>
		<link>http://dev.nirmalya.net/articles/java/annotationexception-entity-x-references-an-unknown-entity-y</link>
		<comments>http://dev.nirmalya.net/articles/java/annotationexception-entity-x-references-an-unknown-entity-y#comments</comments>
		<pubDate>Thu, 29 Apr 2010 18:33:43 +0000</pubDate>
		<dc:creator>nirmalya</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[JPA]]></category>

		<guid isPermaLink="false">http://dev.nirmalya.net/articles/?p=296</guid>
		<description><![CDATA[When the persistence provider complains about "unknown entity" when creating a one-to-one (or many-to-one) relationship between 2 entities, it helps to check the persistence.xml.]]></description>
			<content:encoded><![CDATA[<p><strong>Problem:</strong> I was getting an org.hibernate.AnnotationException when creating a bidirectional one-to-one relationship between 2 entities (EntityOne and EntityTwo).</p>
<p><strong>Relevant Section Of StackTrace:</strong><br />
<code><strong>Caused by: </strong><strong><span style="color: #ff0000;">org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.xyz.domain.EntityOne.anImage references an unknown entity: com.xyz.domain.EntityTwo</span></strong><br />
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:103)<br />
at org.hibernate.cfg.AnnotationConfiguration.processEndOfQueue(AnnotationConfiguration.java:541)<br />
at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:523)<br />
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:380)<br />
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1206)<br />
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1449)</code></p>
<p><strong>Relevant Section Of EntityOne&#8217;s Code:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@<span style="color: #003399;">Entity</span>
@Table<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;entity1&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EntityOne <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span> <span style="color: #009900;">&#123;</span>
  @Id
  @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>
  @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;e1_id&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> id<span style="color: #339933;">;</span>
&nbsp;
  @OneToOne<span style="color: #009900;">&#40;</span>fetch <span style="color: #339933;">=</span> FetchType.<span style="color: #006633;">LAZY</span>, optional <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
  @JoinTable<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;entity1_entity2_map&quot;</span>,
    joinColumns <span style="color: #339933;">=</span> @JoinColumn<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;e1_id&quot;</span><span style="color: #009900;">&#41;</span>,
    inverseJoinColumns <span style="color: #339933;">=</span> @JoinColumn<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;e2_id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> EntityTwo anImage<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//Other code</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Relevant Section Of EntityTwo&#8217;s Code:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@<span style="color: #003399;">Entity</span>
@Table<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;entity2&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EntityTwo <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span> <span style="color: #009900;">&#123;</span>
  @Id
  @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>
  @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;e2_id&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> id<span style="color: #339933;">;</span>
&nbsp;
  @OneToOne<span style="color: #009900;">&#40;</span>mappedBy <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;anImage&quot;</span>, optional <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span> EntityOne e1<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//Other code</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Solution That Worked For Me:</strong><br />
Check the <code>persistence.xml</code> to ensure that the 2 entities (EntityOne and EntityTwo) are listed. Previously I had not included EntityTwo, which is why I kept getting the message <em>com.xyz.domain.EntityOne.anImage references an unknown entity: com.xyz.domain.EntityTwo</em></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    com.xyz.domain.EntityOne
    com.xyz.domain.EntityTwo
    <span style="color: #808080; font-style: italic;">&lt;!-- other configurations --&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://dev.nirmalya.net/articles/java/annotationexception-entity-x-references-an-unknown-entity-y/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 3.191 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2012-05-18 12:26:56 -->

