Titbits


11
Dec 11

Quick Shell

You can go straight forward to GitHub, download and start using quick-shell… then read further.

I still like PHP for its simplicity of deployment and management – just copy files and voila! And for simple webapps you need the cheapest web hosting like the one I have to host a WordPress installation for this blog. Everything simple and easy, but… sloooow! I mean, being restricted to FTP access (normally, you don’t get SSH access with a cheap PHP hosting) only, transfering files and later managing them can be tedious and ineffective. Fortunately, there’s a bunch of file management application (written in PHP) that can help you in performing file tasks. But they use your disk space, can be slow and require to much clicking with often page refreshes… So, ideally would be to have that silver bullet SSH access (though it’s not ideal as you may not be able to use SSH from everywhere). Just compare, what is faster: to type “cd …” and “rm …” (“unzip …”, “untar …” etc) and press ENTER, or to click through the tree of files with page refresh between each, or even worse – to perform file manipulations via FTP? For hackers like us, the answer is obvious – CLI. ;)
php-shellSo, I though it could be easy to create a web command line application that would execute system commands in my hosting environment… This thinking resulted in a simple and very lightweight PHP application that gives me all I need in terms of file management on my php hosting – it’s called quick-shell. Besides that it offers easy ajax-based file upload into a current directory (implemented with jQuery AjaxFileUpload plugin) – so you can do pretty much everything from your browser. Feel free to download/fork it from GitHub, use it and let me know how you find it.

P.S. Very important! Don’t forget to create .htpasswd file with your password to protect your files from anonymous access.


14
Aug 11

Hierarchical data fetched in one query

This is a very typical situation when you have a hierarchical data structure of the type like forum posts or comments:

class Comment {
    long id;
    String comment;
    Comment parent;
}

Let’s store it in a relational database (e.g. MySQL). For this we need to create a table:

CREATE TABLE Comment(
  id int PRIMARY KEY,
  comment text,
  parent_id int,
  FOREIGN KEY (parent_id) REFERENCES Comment(id)
);

And you need to show it in a tree view, e.g.:

/Comment#1
   /Comment#2 (Reply to Comment#1)
   /Comment#4 (Reply to Comment#1)
      /Comment#8 (Reply to Comment#4)
   /Comment#7 (Reply to Comment#1)
/Comment#3
/Comment#5
   /Comment#6 (Reply to Comment#5)
      /Comment#9 (Reply to Comment#6)

You can see that comments added in an arbitrary order (by looking at the comment id sequence).

So how are you going to select all the data to build the tree by SQL means? Standard SQL does not provide hierarchical select feature (like Oracle’s CONNECT BY). One would probably end up doing multiple queries, which sounds like a very inefficient thing to do (imagine fetching a 10-level depth tree of 1000 comments). Fortunately, there is a better idea. I would say even more: a solution with one simple select query! :) Excited? It’s very simple, actually.

The idea is to construct a select statement which will return results sorted in an order we need (say, in the tree above each node is a row in the query result set). On the other hand it is very similar to the structure of directories and files in the filesystem. So, we will use a property of files here, which is the path. When creating a new Comment instance we will set its path, and update when setting a parent Comment:

class Comment {
   String path;
   …
   Comment(long id) {
     this.id = id;
     this.path = "/" + id;
   }

   void setParent(Comment parent) {
      this.parent = parent;
      this.path = (parent == null ? "" : parent.path) + "/" + id;
   }
}

If we apply this transformation to the example above the data in the table will look like:

id  parent_id  path
————————————-
1   NULL       /1
2   1          /1/2
3   NULL       /3
4   1          /1/4
5   NULL       /5
6   5          /5/6
7   1          /1/7
8   4          /1/4/8
9   6          /5/6/9

Now we can use the path field to retrieve our comments ordered accordingly. By executing

SELECT * FROM comment ORDER BY path

we will get this:

id  parent_id  path
————————————-
1   NULL       /1
2   1          /1/2
4   1          /1/4
8   4          /1/4/8
7   1          /1/7
3   NULL       /3
5   NULL       /5
6   5          /5/6
9   6          /5/6/9

The rest is only a matter of formatting the results in order to get nicely looking comments tree output. (Note: the level can be computed as a number of slashes, ‘/’, in the path string).

Also, when using ORM like Hibernate (or JPA in general), you are not forced to use SQL and can get your job done with simple criteria-based or HQL queries.

As a free bonus, you get pagination working properly without extra tweaking as you would have to do in case of multiple queries.

P.S. One piece that is missing in the above example is that the sorting for strings based on numbers will be done in the alphabetical order, e.g. 11 will appear before 2 (which is obviously not what we want). So to fix this problem you can prefix each part of the path with a length of the numbers in characters. It will result in smth like: 12, 211 (or 1.2, 2.11)…


27
Apr 11

Empty img src or Freaking repeated request problem

Hi! This is a victorious and quick post I have to make. (: And here is why. It took me two days of debugging and googling to find out the cause of a freaking problem: browser kept up sending same request for one of the pages of a Grails app I am working on now. The reason is an empty ‘src’ attribute of an <img> element (actually, it can be any element referring to loaded resource like a .js or .css file or any media content). You can read more about the problem itself here: Empty image src can destroy your site. I myself would probably never face this problem if didn’t have captcha (generated for each request to the problematic URL) and an optional image.

And here’s the solution:
1) in server code

<img ${ !url?:’src=”$url”‘ }/>

(in Grails terms meaning: don’t render the ‘src’ attribute if the url is empty)

2) in client (javascript) code:

imgElement.src = null; // don’t set img.src to empty string, though null is fine

So first, never ever leave your ‘src’ attribute empty! And second, never ever set ‘src’ value to empty string programatically or by any other means. Third, it’s ok to do this when it’s officially fixed either html5 is fully supported by all major browsers (hopefully).

Have a good one!


24
Oct 10

Default property value in Spring 3

When using Spring all your configuration comes from properties and a mean to make it work is PropertyPlaceholderConfigurer (xml is no longer considered as a configuration, since Spring’s xml is something stable and considered to be part of codebase, in opposite to properties).

Nice it sounds, however, using placeholders has an annoying drawback, i.e. you can’t leave a property undefined if there is a reference to it in Spring xml, which basically makes a lot of sense and keeps you on the safe side. However, you would want to lessen your configuration maintenance efforts by specifying, where it may fit, default values for some of the properties. Before Spring 3.0 it was not easy to accomplish (PropertyPlaceholderConfigurer.setProperties overrides externally defined values).

Fortunately, guys in SpringSource came up with a neat way to do it, which makes it bish-bash-bosh now:

${propertyName:defaultValue}

Such placeholder commands the Spring’s PropertyPlaceholderConfigurer to use defaultValue if property named propertyName is not found.

This saved me from a burden of maintaining a new property over a few of dozens of server-specific configuration .properties files, where only one required to have new functionality provided by a new property setting.