Sunday, July 12, 2009

Graphics - Format Selection

The Problem
So, you decided to edit an picture from you camera. Most likely, it came from the camera as a JPG or JPEG file. However, if you edit it and save, and especially repeat this process couple times, your image will look different. Wherever there was a smooth transition from one color, or shade, to another color or shade, you will notice strange lines, like contour lines on a map.

The Mystery Revealed
The reason is, each time your editing software saves a file in JPEG format, it compresses the image and introduces small changes to your picture. These changes are small and they are not noticeable. Because JPEG compression algorithm is allowed to perform these small changes, it is able to compress the file so nicely. So, it all works great, unless you save it not once but 3, 4 or 8 times. Then these small changes accumulate and the deformations are very well visible and not really pretty. Your picture is ruined.

The Solution
So, what, shall you avoid the JPEG format? Not at all. However, if you decide to edit the picture, save it in a lossless format. You can open it, edit and save as many times as you want, and you picture will not deteriorate. Than, when you are ready to post the picture to your website or print it, save it as a JPEG file. But what is a lossless format? Well, there are two good candidates: TIFF and PNG. TIFF is better, in a way, because it can save more than just the image. It can also save paths and layers (I'm not sure of layers, though). Just make sure, that you don't let the TIFF formatter use JPEG as compression. You would get into the same problem as described above. PNG may be even a bit smaller, but it will not save anything more than the image itself.

Tool
GIMP image editor will allow you to open and save in all these formats without problems.

Sunday, July 5, 2009

Date, Time, SQLite and Android

This post summarizes my recent experience (problem, finally solved) with storing and then reading and displaying the date and time in Android application.

Storing the current time/date in SQLite
The most common way is to do:
database.execSQL("update TABLE_NAME set COLUMN_NAME = datetime('now') where ...");
As a result, SQLite stores a string representing the current time in UTC (GMT), using the ISO8601 date/time format. This format (YYYY-MM-DD HH:MM:SS), is by the way suitable for date/time comparisons. The fact that the value stored is in UTC and not in a local time zone, is actually nice. More about it below.

Retrieving a time/date and displaying it
To retrieve the value, follow the recommended Android practice:
Cursor row = databaseHelper.query(true, TABLE_NAME, new String[] {
COLUMN_INDEX}, ID_COLUMN_INDEX + "=" + rowId,
null, null, null, null, null);
String dateTime = row.getString(row.getColumnIndexOrThrow(COLUMN_INDEX));
This, returns a string, parse it and reformat to your local format and time zone:
DateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
date = iso8601Format.parse(dateTime);
} catch (ParseException e) {
Log.e(TAG, "Parsing ISO8601 datetime failed", e);
}

long when = date.getTime();
int flags = 0;
flags |= android.text.format.DateUtils.FORMAT_SHOW_TIME;
flags |= android.text.format.DateUtils.FORMAT_SHOW_DATE;
flags |= android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
flags |= android.text.format.DateUtils.FORMAT_SHOW_YEAR;

String finalDateTime = android.text.format.DateUtils.formatDateTime(context,
when + TimeZone.getDefault().getOffset(when), flags);
Note the when + TimeZone.getDefault().getOffset(when) above. This does the trick to convert the UTC time/date to local time. The DateUtils.formatDateTime() seem to be supposed to do this, but it doesn't, or I wasn't able to find a way to achieve it.

And that's it.

Thursday, June 18, 2009

Interfaces, Inheritence and Hibernate

How to choose: Inheritence or Interface?
  • Idea 1: If the relationship is more an "is a", use inheritance. If it is more a "can be", use interface. Examples: TextBox "is-a" Control, ArrayList "can-be" enumerated.
  • Idea 2: If your object is a specialization of another object, use inheritance. But if the feature that distinguishes the derived class from the base class is something that other classes may need to support as well, an interface is a better choice.

Monday, June 15, 2009

Flash Builder 4, Hibernate and Eclipse: Working Together

So, we at OLCC decided to give Flash Builder 4 (beta) a try. So far, we have been working only with a small sample using the JEE for its server tier, but already have learned a few valuable lessons:

Creating a New Flex Project
  • Accept the folder names flex_src, bin-debug; the FB4 doesn't deal correctly with non-default values.
  • After creating a project, go to .flexProperties and set the serverContextRoot="/YourProjectName". After opening Properties->Flex Server window, reset this value of serverContextRoot.
  • Instead of using mx:Text, there is now s:SimpleText control.
Error 404, when trying to Debug the application

When we got this, we saw the URL that FlashBuilder is trying to use to start the HTML wrapper is http://localhost:8080/ProjectNamebin-debug/Main.html. The bin-debug definitely shouldn't be there. We also noticed, that this same URL shows in the project properties on Flex Build Path page as the Server Root URL. It can't be fixed there, but we fixed it in the Debug run configuration. Trying to use Run instead of Debug was even more messy. For some reason, it tried to connect the debugger to the FlashPlayer (yes, in Run mode).

PermGen Issue

Also, recently, we had problem with running out of the space in PermGen. The only solution we found, was to reduce its size (yes, reduce!) from the default 256M to 128M.

The funny thing is, we had eclipse running fine for a long time. Then, when we installed a 2nd instance of eclipse, using a separate workspace, we started experiencing this problem. When we used the 256M, and any serious action crashed the Eclipse, we ran the eclipsec.exe which showed an error message "not able to find enough memory to start jvm with these options" or smth like this. That's why we have reduced the memory. We haven't had the PermGen issue since then. I don't understand this issue really, it's more a trial-and-error solution, but it works.


Hibernate

For Hibernate, we use:
  • Database Development perspective provided by eclipse
  • Hibernate installed via GlassFish admin console (we pointed eclipse compiler at it)
  • Hibernate Tools from JBoss Tools (installed just the hibernate tools through the eclipse update site http://download.jboss.org/jbosstools/updates/stable/)
  • JavaDB bundled with GlassFish server
This configuration seems a good mix. That's it for now.

Tuesday, June 9, 2009

GlassFish Application Server: Introduction and More

This blog summarizes what I have found interesting or not-obvious about the Sun's open source enterprise application server called GlassFish. If you think, any of the info in this blog is not precise or just incorrect, feel free to comment.

  • Version 2.1 of GlassFish is the final version that implements Java EE 5. Version 3.0 of GlassFish is the first one that implements Java EE 6. It's a preview, but ready for production.
  • Server Domain: a new concept introduced into GlassFish that means one or more server instances that can be managed as a whole by an administrator. For developer, domain=server instance. For administrator, it can comprise of multiple, clustered server instances. Each domain, even if all domains are local, runs in its own JVM (there is also one JVM instance associated with the server administration/control, it seems). More info.
  • Virtual Server: to be finished...

Wednesday, May 20, 2009

Using Unit Testing to Test GWT RCP Calls

This is a short note. I'm working on a GWT application and ran into a problem with an RPC call which gets some objects from the database using Hibernate and passes them back to the client. I'm going to solve the problem using Dozer, but I needed to confirm this was the problem and simplify the testing of this in future. So, I decided I want to unit test this service from the client's perspective. Here are my notes from this exercise:
  • The easiest way to create a GWTTestCase is using the GWT's jUnitCreator. Creating it by hand is possible but tricky.
  • The asynchronous test needs to use delayTestFinish(delayMs) and finishTest(). In my case, I had to set 5000 Ms. For some reason, 500Ms, which I thought would be more than enough was timing out.
  • The good news is, that when using Eclipse plugin for GWT, starting initiating Run as JUnit Test on the GWTTestCase test starts for you also the server. And doing the same with Debug as JUnit Test allows to debug the client as well as server code.
Note: the similar test is supported in Flex application!

Tuesday, May 12, 2009

Developing Flex Applications with Java Middle-Tier

Intro
In my job we are using Flex with Java to develop web applications that support the activity of the agency I'm working for. It's my first experience with Flex and I'd say, I'm duly impressed. I had experience of developing using GWT and I must say, that Flex is an equally good tool.

A Simple Way
Just follow this tutorial.

Tools Used
We use the following toolset:

  • eclipse with FlexBuilder plugin (plugin is not free)
  • jBoss
  • Java for back-end
  • LCDS to communicate between Flex and Java (not free, but there are free alternatives: BlazeDS and the LCDS Express is also free, I think).
  • Cairngorm pattern for Flex client structure

Tips and Gotchas

  • A great intro to Cairngorm (and Flex itself) is one by David Tucker.
  • He is using ColdFusion for the back-end, so switch to this or this for how to do it with Java.
The Cairngorm pattern for calling a remote service is a bit complex. Here's how it goes:
  • In the main mxml file (the one where you define mx:Application): instantiate the ServiceLocator: . The id "services" is irrelevant, it seems. And instantiate the FrontController.
  • Let say, the command LOGIN is to be handled by the back-end. Dispatch the command by initiating the LoginEvent and calling dispatch() on it.
  • Each event is mapped onto a command in the FrontController. Let say, in this case, onto a LoginCommand.
  • In loginCommand, instantiate a delegate, say LoginDelegate and call the login(event)
  • In LoginDelegate, obtain the service proxy using:
    ServiceLocator.getInstance().getRemoteObject("myRemoteObject");
    and call the login(args) on it.
  • Now, two additional files need to be setup: Services.mxml and remoting-config.xml
  • In the first one, Services.mxml, define a mx:RemoteObject with id="myRemoteObject" and the destination="myRemoteClass".
  • In the second, the remoting-config.xml, define a destination with id="myRemoteClass" and the Java class package+classname in the source tag.
  • In the Flex Server properties (access through project properties), set the Root Context as the /YourProjectName.
If you need to pass an object between client and the server, create two corresponding value objects (usually named *VO, like LoginVO), one in ActionScript in flex branch and one in Java branch:

  • The ActionScript version, needs to have an annotation above the class definition: [RemoteClass(alias=org.sjb.LoginVO)].
  • The Java version needs to be public and have public setters.
If you want to debug not only Flex (which is done the usual "eclipse" way), stop the JBoss server and start it in Debug mode. That's all that is needed.

Conclusion
Altogether, using Flex with Java is very simple. Enjoy it!