June 2007


Nature and Job and Social26 Jun 2007 10:17 am

Last Saturday we had a picnic with co-workers from Sonopia. It was my first time when I was on the Kiev Sea, moreover it was opening of the swimming season for me. There were lost of sports activity, delicious meals (especially grilled meat) and social chatting.
The most thing I liked at that day, that there were no any commandments from anybody, and people were relaxed and were doing things they liked. At the same time, It seems that everybody was involved almost in at least couple of activities like playing football, swimming or supporting the campfire. Excellent rest for the excellent team :)

playing football
Bike and Nature21 Jun 2007 03:06 am

There are lots of bikers in Kiev, and most of them are communication through the local bike forum. Amongst of topics about bike equipment and maintenance techniques there are topics where bikers can arrange joint bike journey. The journey can be long or short, cross-country or in the local park. Somebody takes responsibility to set the route and timeline and invites other bikers to take part.
I found there a very interesting and unusual early morning journey and recently took part in it. The trip starts at 4.30AM every Thursday and takes about 24km with one stop near the river to watch the sunrise, then eat some snack and drink tea. The one-way route without way to home is here. The average speed was about 17 kmh which is quite hight for the cross-country.
I’d like to thank everybody who was patient to me as a newbie and didn’t steam ahead. Some photos from the trip are here.

Desna River fog
Perl and Code snippets and Java20 Jun 2007 02:08 am

Introduction

Almost every business application requires a country list as a dictionary data. Even simple registration form might contain country input field. And if you’re going to store billing or shipping information beyond one country you definitely have to have this important dictionary in your system. Firstly, you should decide which countries will be in your list. Depends on your goals it might be short list of the largest countries in the world or full list with all Islands, Territories and even Antarctica. Let’s review common sources for filling your country list dictionary.

Existing country lists

If you have billing or shipping integration with 3rd parties like PayPal, you can get this list from the register page html source code. Open a page with the registration information on the any trusted web portal, then find in the source code country list data (e.g. “View -> Page Source” in FF):

<option value="AL">Albania</option>
<option value="DZ">Algeria</option>
<option value="AD">Andorra</option>
<option value="AO">Angola</option>
<option value="AI">Anguilla</option>
………
 

Now you can copy&paste this information and then parse/use it in any convenient way. As you can see, the value in this list is a two character identifier which is called ‘ISO 3166-1 2 Letter Code‘ . It is very useful as a unique identifier and can work as a primary key in the countries DB table.
However there are lot’s of sites which already have well-proven lists, I’d suggest take this list directly from the original source - United Nations published
official list, which is republished in the many places like List of countries and territories or ISO country codes.

Generating SQL for country table

Let’s take one of that list and paste everything to the Excel sheet, then save it in the CSV format. The result should be similar to ISOCodes.csv and contains data separated by commas:

ABW,AW,Aruba
AFG,AF,Afghanistan
AGO,AO,Angola
AIA,AI,Anguilla
ALA,AX,Aland Islands
………
 

Once we have data stored in Perl readable format, we can parse it and generate SQL code:

#!/usr/bin/perl

# PERL MODULE
use Text::CSV::Simple;
   
# This script generetes sql code for country table

print <<SQL_END;
DROP TABLE IF EXISTS country;
CREATE TABLE country (
  short_code varchar(2) NOT NULL COMMENT ‘ISO 3166-1 2 Letter Code’,
  name varchar(100) NOT NULL,
  PRIMARY KEY  (short_code),
  UNIQUE KEY UNQ_COUNTRY_NAME (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
SQL_END

my $tplSQL = "INSERT INTO country (short_code, name) VALUES (\"%s\",\"%s\");\n";
my $tplXML = "<country short_code=\"%s\" name=\"%s\" />\n";
my $csvFile="H:/workspace/update/data/ISOCodes.csv";

my $parser = Text::CSV::Simple->new();
my @data = $parser->read_file($csvFile);

print "– SQL DUMP\n";
foreach(@data) {
        printf $tplSQL, @$_[1], @$_[2];
};
print "– Data for DBUnit tests\n";
foreach(@data) {
        printf $tplXML, @$_[1], @$_[2];
};
 

I’d prefer not to populate data directly to DB, but generate a script which can be edited and invoked many times. This script doesn’t work with command line parameters and use hard coded path because I’m not a Perl programmer who develops a multi-purpose tool:) I’m just a Java developer who needed a valid country list. After executing this script you’ll get SQL code and DBUnit xml snippets.

For those who are curious

Unfortunately, there are no one common countries list, however there are some official lists supported by different organizations like UN, bank communities, post offices, phone companies. When you’re choosing a proper list, take in account countries which are disappeared like USSR or was divided like Serbia and Montenegro. Choose a primary key from large variety of existent (ISO 2,3 characters, number, phone code,…) which satisfies your needs. If you have a unique key, you can get another useful information about chosen country from other sources afterwards. To get answers on those questions, I’d recommend to skim through How many countries are there in the world? and then google on interested keywords.

This is a part of Perl for Java Programmer series. Previous post was Perl for Java programmer: Installation. Stay tuned!

Perl and Java18 Jun 2007 12:42 pm

Introduction

Let’s assume that you are a Java server side programmer. Your current web project involves working with server side specific data, such as lots of media files which are stored in file storages, DB data, XML files, configuration files etc. All files are stored under proper sub-directories, your objects are beautifully mapped to DB using your preferable ORM like Hibernate. Another bunch of data is serialized to XML using something like xstream. Everything works like a charm, until the data storage format or convention is changed. For example, you decide to move all your pictures to another sub-folder. If you have references to these files in your DB you have to modify it too. Or, let’s say, in the next release of your product the textual data stored in the XMLs have to be updated to conform new POJOs which has new properties. What I’m trying to say here, is that if you develop an application which works with lots of non homogeneous data, you have to have an approach to migrate it from one version to another.
I think, that the best solution is using a script language which supports string parsing, SQL executing, file I/O. I chose Perl because of it well-known excellent work with regexps and good reputation between system administrators.

Installing/Configuring Perl

If you are a lucky user of the *nix OS, I believe, you don’t need any additional installation of the Perl, because it is already included in your distributive. For folks who are using Windows, I would recommend installing ActivePerl - Perl binary distribution for Win32 platform. Basically, I don’t know any other, and this one seems to be the best. Installing is a well-known “Next-Next-Next” joy.
My current IDE for Java is Eclipse. And one of great advantages of this famous IDE is it plugin system. There are lots plugins developed by different people almost for every purpose. The first plugin I found was EPIC - open source Perl IDE. Using this plugin you obtain simple but powerful facilities for Perl programming. It is a code highlighting and intellisense while typing, running a program by clicking right mouse-click on a perl source code file and choosing “Run as perl”. Output of the program is directed to Eclipse console. And of course, you can debug the program if output is helpless.
Finally, I’d like to emphasize the very useful tool which is supplied with ActivePerl - Perl Package Manager. Once you need any additional Perl package, you can easily install it in your system from the global repository. Perl Package Manager
For instance, you write a simple script which use B connection to a MySQL instance. But there is an error:

install_driver(mysql) failed: Can't locate DBD/mysql.pm in @INC
Perhaps the DBD::mysql perl module hasn't been fully installed,
or perhaps the capitalisation of 'mysql' isn't right.

The only thing you have to do, is to open Perl Package Manager, find missed in standard configuration package DBD::mysql, and install it.

Roadmap of this guide

Today I decided that writing less but more frequently is easier and using his approach I don’t need keeping this important post still in drafts. I’d rather ship every day part by part than wait for another month to complete it.
Therefore, I’m planning to cover in the near weeks these topics:

  • String manipulation
  • Files manipulation
  • DB work
  • XML work
  • SQL generating

Stay tuned!

Bike11 Jun 2007 12:22 pm

I’ve done my first long ride today. It was 24.5km from right bank to the left and back. The route is here. Today was a thought working day, because I didn’t have any interesting things to do, only unimportant and irrelevant little annoying tasks. After all of that I had a nervous meeting with irritated people. I was looking frward to the evening ride to have a relief. And now I’m sure that riding a bike is a great antidepressant! Now I’m calm and optimistic :)

Bike and Friends10 Jun 2007 10:28 am

I’ve eventually bought my bike of the dream! I’d been choosing what to buy at least for a week. I couldn’t work properly, because all my thoughts were about the bike. I was talking to all my friends who had a bike and could advice anything on it. I skimmed through lots of Kiev’s bike on-line resources like the local club to become more advanced in all that particularities. And today, thanked to my class-mate, and close friend WhiteShadow I chose and bought this excellent toy with an extra discount. I had to ride from the shop to my home, and that trip was just excellent! I did 12km and 5 more kilometers with my wife around our district this evening. Many thanks WhiteShadow!

TREK 4400
Books07 Jun 2007 12:39 pm

Before I read this book I’d heard only a little about Emile Zola. When I saw this book standing on a parent’s bookshelf I decided to read it. The novel appeared a rude and realistic, however have plenty things to think about. (more…)