me.thoughts.map(_ => blog)

A Giter8 Template for Liftweb

I have created a giter8 template liftweb-twitter-bootstrap.g8 for liftweb applications with twitter-bootstrap.

Prerequisite

Usage

$ g8 rajeshpg/liftweb-twitter-bootstrap  

template for liftweb app with twitter-bootstrap

package [com.myliftwebapp]:
name [My Liftweb App]:
liftweb_version [2.4]:
scala_version [2.9.1]:

$ cd my-liftweb-app  

Eclipse users can run the following command and import the project.

$ mvn eclipse:eclipse  

IntelliJ users can run the following command and import the project.

$ mvn idea:idea  

Creating Custom Maven Archetype

Maven makes it easy to create a project from a skeleton using the archetypes. But most of the time many of the archetype does not work out of the box.

Having a skeleton project with most of the default configurations always comes in handy. It saves time creating/writing configuration files for Spring, Hibernate… etc.

How about creating an archetype for our project and have it in our local Maven repository or publish it in public repository or in github ?

[grab your favourite beverage, this is a little long post.]

Scala Class Mixin and Composition - a Practical Example, Contd…

In the previous post we saw how to a create validator with a policy and filter data read from an excel.

Lets create another validator to check for discount types of a sales order and validate the sales data using the two validators and filter the data.

What we need is a DiscountTypeValidator to check for discount types allowed(Policy)

here is the updated process method

in line 12 we create another anonymous class for DiscountTypeValidator with a Policy DiscountTypeAllowed with a list allowedDiscountTypes that holds a list of discount type codes.

DiscountTypeValidator takes column index of the discount type as Int parameter. The validator method checks whether the discount type mentioned in the excel is allowed or not and then returns a list of booleans.

So now we have list of data worksheet from excel and lists of booleans mfValidationResult and discTypeValidationResult that represents the validation result of Validators for each row in the excel.

In line 15 we create a list of tuple containing the row object and one of the validation result,say mfValidationResult using zip method of scala.List(actually trait IterableLike).

validationResult looks like this

List((org.apache.poi.hssf.usermodel.HSSFRow@2200d5,true), (org.apache.poi.hssf.usermodel.HSSFRow@64ab4d,false),...)  

We have to zip the discTypeValidationResult also with validationResult using zipResults method.

There is no convenient method in scala to zip a tuple say (1,2) with another value say 3 so that we get (1,2,3) instead of ((1,2),3).

The snippet zipResults(validationResult, discTypeValidationResult) combines the validation results and gives a simple tuple containing the row object the validation result on the object.

Finally validationResult.filter(_._2).map(_._1) filters the sales data based on the validation result and returns the valid data.

Scala mixin and compostion is very comprehensive and convenient compared to Spring IoC.

Scala Class Mixin and Composition - a Practical Example

I am porting a part of a module written in Java to Scala. Lets consider the following 2 requirements alone. The second requirement is the one we are going to focus on.

Requirement:

1. Read data from an excel file containing orders(material purchase order)
2. Validate the data   
    a. check for mandatory fields.  
    b. check for allowed discount types.

The Java version is built using Spring IOC. The Scala version is built using the cake pattern that is similar to the dependency injection, but very light weight since it is built on the features available in the core of the Scala language.

The implementation

what we need is a Validator that validates an object T based on a Policy. So what we have now is the following traits.

Considering the requirement 2.a, we need a validator that checks for mandatory fields in the excel sheet. We will create a MandatoryFieldsValidator that validates every Row for presence of the mandatory fields.
But there are n types of format for the order data in excel sheet. Every format has different mandatory fields.
For example type 1 sheet has columns 1, 3, 6, 8 as mandatory fields, type 2 sheet has columns 0, 2, 5, 10 as mandatory fields.

Hence we have MandatoryFieldsPolicy with index of the mandatory fields and the MandatoryFieldsValidator

and here is the actual mixin where we mix validator with policy based on the type of data in the excel sheet

In the next post we’ll see filtering data based on multiple validations.

HOW to Handle ORA-01795 in Hibernate

ORA-01795 maximum number of expressions in a list is 1000 is an error that one encounters very rarely.

But I did encounter recently, while querying a table using hibernate.

Lets’ assume an employee table needs to be queried for employees (note the plural) based on employee codes.

The query would be:

1
select * from tbl_employee where employee_code in (1001,1002...2003)

But the query will result in error ORA-01795 maximum number of expressions in a list is 1000,
particularly in Oracle(I havent tried in this other dbs’).

Thats’ the problem statement, and lets see the possible solutions.

  • Construct SQL/HQL(incase of hibernate) with or clause along with the in clause.
    The more or clausess the more it hits the performance. Thats’ what i heard many SQL gurus told in blogs and forums.
1
select * from tbl_employee where employee_code in (1001,1002,...2000) or employee_code in (2001,2002,...3000)
  • Construct Query using Hibernate Criteria(the reason for this post)
    The code creates SQL query thats similar to the one above.
    The Hibernate Disjunction(line no 12) is used to group expressions in a single disjunction,
    i.e. select * from ... where... in X or Y or Z
    X,Y,Z being conditions.

Learning a New Programming Language

Learn at least one new language every year. Different languages solve the same problems in different ways. By learning several different approaches, you can help broaden your thinking and avoid getting stuck in a rut.

Andy Hunt Dave Thomas - the Pragmatic Programmer

A Simple Todo List Manager Becoming Complex

Recently i started a todo list manager project as a way to learn ruby on rails. Though this is not my first rails project, am still in the learning phase. I wanted to create a simple to-do list manager with UI as simple as twitter. One textbox to write the task to be done and thats it. I did not want to have a calendar link to select due date , no options to select the priority… etc. I want to capture maximum details possible in the single text box. Later i can extract the details using regular expression in the controller First it was a little difficult for me to get the right regex for the string patterns,later with a (first)question posted in stackoverflow.com i got the solution for that.

thanks to @Jens and @sawa for providing the right regex for my need. Still i keep getting ideas to make it as easy as possible for a user to enter his/her todo task with all details in a single text box, but each idea is making the code more complex.

I am working on this project with a very little spare time that i get, so it may take sometime to make it online and publish the code in github.

Using Github Thru Dropbox

A simple setup to use dropbox as a git repository, that found in stackoverflow.

1
2
3
4
5
6
7
8
9
10
~/project $ git init
~/project $ git add .
~/project $ git commit -m "first commit"
~/project $ cd ~/Dropbox/git  
~/Dropbox/git $ mkdir project.git
~/Dropbox/git $ cd project.git
~/Dropbox/git $ git init --bare
~/Dropbox/git $ cd ~/project  
~/project $ git remote add origin ~/Dropbox/git/project.git
~/project $ git push origin master

Move Wordpress Blog From Root to Subdirectory

I am quite sure you will get a lot of tutorials for this topic.

try this [http://www.google.co.in/search?q=move+wordpress+blog+from+root+to+subdirectory] (http://www.google.co.in/search?q=move+wordpress+blog+from+root+to+subdirectory) but that didn solve one particular problem,redirecting the old url to new url.

Earlier i had my blog in the root directory on my domain http://www.rajeshpg.com.i moved my blog to a subdirectory http://www.rajeshpg.com/blog. i reinstalled wordpress in the subdirectory, and import my earlier posts.

The problem is that the old url for a post (probably indexed by google) should be redirected to new url.

eg:-
http://www.rajeshpg.com/2007/01/javaxservletservletexception-path-loginlayout-does-not-start-with-a-character/ should be redirected to
http://www.rajeshpg.com/**blog**/2007/01/javaxservletservletexception-path-loginlayout-does-not-start-with-a-character/

After googling for an hour i got the solution. Modify your .htaccess file(or create one) in the root directory with this code.

    RewriteEngine On RewriteBase / RewriteRule ^(\d*)/(\d*)/(.*)$ http://yourdomainname.com/blog/$1/$2/$3 [L,R=301]

(\d\*) represents a sequence of numbers.
(.\*) represents sequence of characters.

note:– this solution works only for the urls of /year/month/title pattern of wordpress blog.