Wednesday, August 12, 2009

[SQL] Why 1 = 1 in SQL query ?

I saw that interesting thread on StackOverflow here

Sometimes, you need to build dynamically the WHERE statement in a SQL query.
So you will concatenate some key/value seperated with a "AND" operator

If you run the following:


import java.util.HashMap;

public class Test {

public static void main(String[] args){

StringBuffer query = new StringBuffer("SELECT * FROM users WHERE ");

HashMap mp = new HashMap();
mp.put("firstname", "John");
mp.put("lastname", "Doe");
mp.put("login", "john.doe");

for (String k: mp.keySet()) {
query.append(" AND " + k + " = \"" + mp.get(k) + "\"");
}

System.out.println(query.toString());
}
}


The above will return:
SELECT * FROM users WHERE AND lastname = "Doe" AND login = "john.doe" AND firstname = "John"

The WHERE statement has been generated successfully except that now, we would need to remove the first occurence of the AND operator.

The trick is to add the following statement at the beginning of the query:
1 = 1

With this additionnal above statement, we wont need to look for the first occurence of the AND operator and our query will work since 1=1 will always be evaluated to true



import java.util.HashMap;

public class Test {

public static void main(String[] args){

StringBuffer query = new StringBuffer("SELECT * FROM users WHERE ");

HashMap mp = new HashMap();
mp.put("firstname", "John");
mp.put("lastname", "Doe");
mp.put("login", "john.doe");

query.append("1=1"); // This can also be moved when declaring the query in the StringBuffer

for (String k: mp.keySet()) {
query.append(" AND " + k + " = \"" + mp.get(k) + "\"");
}

System.out.println(query.toString());
}
}



The result will return:

SELECT * FROM users WHERE 1=1 AND lastname = "Doe" AND login = "john.doe" AND firstname = "John"

Tuesday, August 11, 2009

[SpringMVC] Spring Tool Suite 2.1.0 includes a Spring MVC template project

I have just installed Spring Tool Suite 2.1.0.
Spring Tool Suite (STS) is a tool from SpringSource based on the Eclipse IDE.
It includes most of the tools (Maven, Tomcat ...) you would need to do some Spring development.

I have tried STS 2.0.2 and one of the reason I stay with Netbeans (which is also a great product) is that creating a Spring MVC project was easier in Netbeans than in STS.
With STS 2.0.2, I had spend long hours to find out how to make a Spring MVC template work with no success (using maven2 archtype).

With STS 2.1.0, it's now easy to create a Spring MVC project to base your development on.

To create a Spring MVC project, go to:

1) File -> New -> Spring Template Project
2) Select the kind of template you want (MVC, Batch, Webflow...) and click Next
3) Enter your project name and top-level package name and click Finish
4) Et Voila, a Spring MVC project ready to be used.

Also, the great thing is that the configuration of this Spring MVC project is based on annotation instead of XML-only file (on Netbeans). The controller is then a simple POJO with @Controller.

Have a try, I look forward to playing around with it

Monday, August 10, 2009

[Python] Easily make available some files through HTTP

At work, our desktop computers run on different OSes, mainly Linux and Windows.

To be able to share files between Linux and Windows, a samba server will need to be installed and configured on the Linux box.

Since I dont need any files to be permanently shared, I dont have samba configured. If I need to make some files accessible, I use the below script to create a simple python-powered HTTP server, so that the files I want to share are easily accessible for download through HTTP.


#!/usr/bin/python

import SimpleHTTPServer
import SocketServer

# minimal web server. serves files relative to the
# current directory.

#Replace the port number if the port 8000 is in use already
PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT
httpd.serve_forever()



-Just save the below script (i.e. share_files.py) under the directory you want the World to have access to (i.e. $HOME/Pictures/)

-Go to $HOME/Pictures/ folder

-Give executable right to you script:
chmod u+x share_files.py

-Launch the script
./share_files.py

-Go to http://localhost:8000/
You should see the list of files you have under $HOME/Pictures/

To have the folder accessible for others, give away your IP address instead of localhost

Note:
The above script was found here

Monday, July 27, 2009

[vim] Send the output of a command straight into vim

Here is a pretty cool tip I learn from the Ubuntu groups on LinkedIn.

You are working with vim on a file and, for some reason, you need the output of a command (i.e. ls to get the list of files in folder).

1) First solution is to open a new shell and type 'ls', copy the result and paste it in the file that you are currently working on
2)You can use redirection by closing the file you are working on and type:

ls >> myfile.txt

3)This cool trick from vim (and vi).
Place your cursor where you want the output to be pasted
Type

:!r<command>

In our example, it will be

:!rls

and the result of the ls will be pasted where you have place the cursor.

Tip can be found here . You may need to join the Ubuntu group.

Sunday, July 19, 2009

[SpringMVC] Enabling annotation

The Spring MVC framework allows one to configure an application in 2 different ways :
- via a XML configuration file
- via annotations

I use the Netbeans IDE (6.7) for Spring development.
When you create a Spring MVC project, the template generated is using the XML configuration (to specify the mapping between URL patterns and Controllers)

To enable annotation:
-in ${project}-servlet.xml, remove/comment out the declarations regarding the
  • ControllerClassNameHandlerMapping
  • SimpleUrlHandlerMapping
  • ParameterizableViewController

-in the applicationContext.xml, add the following declarations:

*When declaring the <beans> root,
  • add the 'context' namespace declaration: xmlns:context="http://www.springframework.org/schema/context
  • Add the following to the xsi:schemaLocation:
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
*Add the below beans declarations:
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<
context:component-scan base-package="com.myapp"/>

From the documentation of Spring 2.5, DefaultAnnotationHandlerMapping is an
"implementation of the HandlerMapping interface that maps handlers based on HTTP paths expressed through the RequestMapping annotation at the type or method level. "

You will have to declare in your Java classes (POJO) a @RequestMapping which will represent an URL pattern.

The component-scan will tell Spring to scan the base-package for classes that have been annotated (@Controller, @RequestMapping...)

See more info on DefaultAnnotationHandlerMapping here

Wednesday, March 11, 2009

[OpenLDAP]How-to add a new tree in OpenLDAP

See below a short how-to I wrote as I had to create a new DIT (Directory Information Tree) in my OpenLDAP server.

1)Create, for example under /var/lib, a directory that will be used to
store the database file of the new tree i.e. in "ldap-mynewtree"
sudo mkdir -p /var/lib/ldap-mynewtree

2)Give the ownership of the directory to the openldap user (or whatever user the LDAP server is using)
sudo chown openldap:openldap /var/lib/ldap-mynewtree

3)Add a second database and suffix to /etc/ldap/slapd.conf
The below config has been taken from OpenLDAP default settings
######################
# Specific Directives for database #2, of type 'other' (can be bdb too):
# Database specific directives apply to this databasse until another
# 'database' directive occurs

database bdb

# The base of your directory in database #2
suffix "dc=mynewtree,dc=com"

# rootdn directive for specifying a superuser on the database.
#This is needed for syncrepl.
rootdn "cn=admin,dc=mynewtree,dc=com"
rootpw pwdadmin

# Where the database file are physically stored for database #2
directory "/var/lib/ldap-mynewtree"

#The above has been taken from the default settings of OpenLDAP
dbconfig set_cachesize 0 2097152 0
dbconfig set_lk_max_objects 1500
dbconfig set_lk_max_locks 1500
dbconfig set_lk_max_lockers 1500

# Indexing options for database #2
index objectClass eq

# Save the time that the entry gets modified, for database #2
lastmod on

access to *
by dn="cn=admin,dc=mynewtree,dc=com" write
by dn="cn=admin,dc=mynewtree,dc=com" read
by * read


4) sudo /etc/init.d/slapd stop

5) sudo /etc/init.d/slapd start

6) Create a ldif file containing the basic tree structure and save it
as mytreestructure.ldif

Example of ldif:

##############################
version: 1

dn: dc=mynewtree,dc=com
objectClass: dcObject
objectClass: organization
dc: mynewtree
o: Example Corporation
description: The Example Corporation

dn: cn=admin,dc=mynewtree,dc=com
objectClass: organizationalRole
cn: admin
description: Directory Manager

#####################################


7)Add the structure from the ldif file to the OpenLDAP server:
ldapadd -x -W -D cn=admin,dc=mynewtree,dc=com -f mytreestructure.ldif -c

As reference, http://www.linux.com/articles/113630?page=2 :
* -c: This means don't die on every error; list errors, but
continue and add those entries that did not contain errors
* -x: use simple authentication
* -W: prompt for the bind password
* -f filename: get entries from filename
* -D'binddn: Bind using binddn -- essentially a username expressed
in LDAP's language, using the full dn.
For admin functions, this will be the rootdn you specified in your
slapd.conf file.

8)Update config.php for phpldapadmin to display the new root
sudo vi /etc/phpldapadmin/config.php
Around line 86: We add the new root dn of the new tree in the array

$ldapservers->SetValue($i,'server','base',array('dc=thefirsttree,dc=com','dc=mynewtree,dc=com'));


9)You should now see the new tree you have just created under phpldapadmin beside the first tree.


Monday, February 23, 2009

[vim] Autocompletion/Commenting a block in vim

Here is a useful tip (IMHO) to use autocompletion in vim.
To make it short, just type the first few letters and hit Ctrl+p.
Tip found at here

Another tip to comment a block in vim.
Select a block in Visual Mode (Ctrl+V), press Shift+I and type your comment sign (# for perl)
Tip found at here