Lucene’s Field options (Store and Index), aka RTFM :)

To make a long story short, if you want to make a field act as a database primary key (so you could update/delete a document), create a field with this parameters:

document.add(new Field("id", object.getId(), Store.YES, Index.NOT_ANALYZED));

This was a big RTFM moment for me. I’ve lost a lot of time with this one, and it was really simple. So I guess you are wondering why I didn’t check this :) Well in another project, it was Index.NO instead of Index.NOT_ANALYZED, and I thought that was ok, and checked EVERYTHING else, except that line you see above this text.

So, let’s read the manual together, first the Store options

  • Store.YES – Value is stored in index, so it can be retrieved by an IndexReader.
  • Store.NO – Not stored :)

Now the Index options:

  • Index.No – not indexed, so not searchable (… and in my case, if you don’t index the “ID” field, than it can’t be searchable, which means it can’t be deleted and/or updated)
  • Index.ANALYZED – Field will be indexed, and it will be analyzed (saved as tokens that will be searchable)
  • Index.NOT_ANALYZED – Field will be indexed but in it’s original form (good for things that should be searchable in original form, ID anybody? :))

So, if I got everything right, here are some good combinations:

Store.YES + Index.ANALYZED = In index and analyzed, good for not so big content, such as a title and some short intro text (first few lines you see in a blog, before you go to that post)

Store.NO + Index.ANALYZED = Not stored in index, but analyzed and searchable, so pretty good for big text (content)

Store.YES + Index.NOT_ANALYZED = Stored in index in it’s original form, great for IDs :)

If I got something wrong, feel free to point that out (in comments or hate mail), also if you got lucene tips, please share :D

Advertisement

@Embeddable with hbm.xml

At work we are using hibernate with annotations. So in some places we use the @Embeddable & @Embedded annotation. What do they do? They are both standard annotations, @Embeddable means “this class will not have it’s own table, and it will be embedded in some other’s class table”, and @Embedded means “embed this field to my table”.

That’s all good, but I’ve returned to my old project (klopaj.com), and what do we have there? hbm.xml files. I confess, I’ve first started using hibernate annotations, and then hbm files, so I’m not a hbm guru (nor annotations, but I know a bit more :)). So, to do the same thing with hbm.xml:


<class name="Poi"...>
...
<component name="geoPoint" access="field" class="GeoPoint">
<property name="latitude" type="double" />
<property name="longitude" type="double"  />
...
</component>

Simple as that, now my Poi table has two additional columns “latitude” and “longitude”, but my Poi class has a reference to GeoPoint instance. :)
But still, I like the annotation solutions prettier, but this one is working as planned.

Introducing the “Toink” sessions

I’ve very pleased to announce something that I want to do in a very long time. I’ve heard a million times “this framework is much better than <insert name> framework”, “it’s cool, but to difficult to setup” ,”too many XMLs” etc.

So what will I do? Simple, try to test all the frameworks as much as I can on a very simple application. The mystery application name is “Toink”, so there’s the name “Toink Session”! And yeah, the name doesn’t mean anything, it’s windows default sound when something goes wrong :)

Every framework will use the same backend (core) that has been implemented in Hibernate + Guice combination. And I’ll with test to see how simple it is to setup some framework, use a “legacy” code, and try to use the old POJO objects. Maybe I will do some performance tests as well, because all the frameworks will you the same core.

All the code, examples, bugs and wiki will be @ code.google.com/p/toink. I still don’t know how often will I post here, but as soon as some framework is done, I’ll post it here.

Ready or now, here we go! The first framework will be Apache Wicket!

I would like to hear your thoughts and ideas in the comments :)

The Unbearable Lightness of Java – jodd

I was amazed when I first read about this library.  It’s almost unbelievable what is packed in less than 850KB. Without further ado, let me represent jodd!

jodd logo

As the author said himself:

Jodd = tools + ioc + mvc + db + aop + tx < 850KB

There are two main parts, one is the general utility library, and the second being a set of lightweightapplication frameworks.

  1. How many times you had to deal with date in java? You can still feel the pain? Here is the cure JDateTime. Ever had a need to copy entire bean? Use BeanUtil. Sending email is your thing? Yup, jodd has that covered. Auto-populating forms? Checked. Also there are StringUtil, Printf, special JSP tag library and bunch more. Very useful from time to time.
  2. Second part is interesting as well. There is a slick MVC framework named Madvoc (I’ve used it in one of my projects, simple as hell), Petite (DI container, no XMLs :)), DB access layer and more.

Check it up yourself at www.jodd.org. Documentation is great and there are planty of examples to get you started (much better than my examples anyway)

Also there you can find “Uphea“, application built on jodd framework.

Sonar – open source software quality tool

Wow, I don’t know how did I miss this awesome tool. Just go to http://www.sonarsource.org/ download it and install it. It will help you keep your code much cleaner and with less bugs (I didn’t say bugless :))

Installation is easy, UI is simple enough, a lot of features (different metrics, time machine, unit tests etc)… And one of the great things I love about it there is a Sonar plugin for Hudson, and you can find it http://wiki.hudson-ci.org/display/HUDSON/Sonar+Plugin. With that plugin you could analyze your code with every night build, compare it to the previous, see if you have any improvements.

Oh yeah, one more thing, it’s maven ready. And if you are not using Java, there is a PHP, Flex, .NET, Groovy… plugins, check it out here http://docs.codehaus.org/display/SONAR/Sonar+Plugin+Library

Wicket, Hibernate and filters (web.xml)

I guess everybody read about Open Session In View. So, as the jboss guys said there, we need something like SessionRequest filter. No problem with that. I’ve used almost the same code in my application, but I had issues with it.

What happend? Well, sometimes after saving something wicket would block, wait a minute and throw a “After 1 minute the Pagemap null is still locked by:” exception. I’ve read alot about that, but couldn’t find any solution!

Luckly, I found one mailing list, where somebody said that SessionRequest should be defined before WicketFilter (I’ve lost the link, but when I find it, I’ll post it).  Here is how my web.xml looks like now and it worked!

<filter>
 <filter-name>SessionRequestFilter</filter-name>
 <filter-class>com.vuknikolic.SessionRequestFilter</filter-class>
 </filter>
 <filter-mapping>
 <filter-name>SessionRequestFilter</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>

<filter>
 <filter-name>wicket.patientportal</filter-name>
 <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
 <init-param>
 <param-name>applicationClassName</param-name>
 <param-value>com.vuknikolic.WicketApplication</param-value>
 </init-param>
 </filter>
 

UPDATE: I need to find some plugin for posting source code…

UPDATE 2: Found one source code highlighter, yay!

Quick introduction

Ok, so I finally made up my mind, I’ll start using this blog. I will mainly write about awesome Apache Wicket framework that I’m using at work, Android platform and development and bunch of other java related stuff (Lucene, Guice, neo4j etc) as well as my other projects :)

So, stay tuned, new posts coming soon :)

Cepelin

Cepelin is an android app that I’ve worked on with my colleges from my (now ex) firm youngculture.  There was believe it or not, eight of us willing to learning something, create an app and have a lot of fun. Couple of meetings with beer, few days working on an “ueberly awesome” idea (cafe, restaurant and event guide) we actually won the golden award on VIP mobile’s Android Challenge.

Members of the awesome team are: Saša Slavnić, Dragan Marjanović, Marko Simić, Željko Gavrilović, Žarko Šušnjar, Igor Popov, Igor Spasić and yours truly.

You can see our icon in the official VIP mobile commercial (27-28th second) :)

And here are the screenshots:

Gradski Prevoz – android app

My third application for VIP Android Challenge 1.0. I’ve created this application with great friend of mine Milan Delibašić.

Gradski Prevoz (Public Transport) helps user in using public transport by providing information on lines, schedules and other useful stuff. Currently supports several lines of public transport in Belgrade, Serbia.

This application (although not fully finished, because we didn’t map all the lines and buses) won the bronze award (HTC Hero)

This application was featured in VIP mobile’s commercial :)

klopaj! for android

My second application for VIP Android Challenge 1.0. Klopaj! lets you search entire Belgrade for the best restaurants. Just enter type of food, or restaurant’s name or whatever you would like and let klopaj show you where it is. You can comment restaurants, added them to your favorites, love them or not. App is powered by klopaj.com website.

This application also won a HTC Tattoo phone, that I’m currently using :)