Laur IVAN

HTML to PDF with iText 2.1.7

Posted by in Software, Tutorial

Sometimes you need to use an older version of a product die to e.g. licensing changes. It’s my case with iText. While the new version looks fantastic, their changes (and removal of e.g. RTF output) are deal breakers for the feature set we need to maintain. Therefore, 2.1.7 is the most recent version we can use. Heh. In maven, this would be:

The string resulted from the previous article can be printed out fairly easily. First, we create a document and a writer:

We write the document:

read more

Call an Oracle Function from Spring with CallableStatement

Posted by in Software, Tutorial

Following the previous post, I wondered if there’s a more elegant way to call a function. It turns out there is and it’s based on CallableStatement:

The CallableStatementCreator sets up the statement and the CallableStatementCallback executes the statement and returns the value up to the execute() call. Having a function:

We can build a query string like:

The CallableStatementCreator instance is then:

This states the first parameter is a result, the second and 3rd parameter are function parameters. NOTE: Be careful because you can specify indices…read more

Call an Oracle Function from Spring

Posted by in Software, Tutorial

The easiest way to call a function from Spring is using SELECT. For example, having a function HAS_PANEL_FLAG_OK with two parameters and returning an integer, the function call looks like:

This will return all flags for the panels matching the condition. Java Call The query string can be something like:

and the actual query will be:

The problem I’ve encountered is the ability to specify an argument which is not part of the query. Something like this:

I’ll investigate this later… HTH, A little experiment: If…read more

List Columns in a ResultSet

Posted by in Software, Tutorial

In order to list all columns in a JDBC ResultSet (e.g. for debug purposes), you need to iterate through its metadata:

Note: Column numbers start from 1, not 0 (hence the md.getColumnName(i + 1) above). I usually wrap this in a if(logger.isTraceEnabled()) so it doesn’t get triggered all the time. HTH, A little experiment: If you find this post and ad below useful, please check the ad out :-)

Pass Through a Corporate Proxy in Java

Posted by in Software, Tutorial

TL;DR: There’s no easy way to create a proxy :) I’ve tried for a while to build a way to request something through a proxy, for testing an emulated android app. The solution came from Octavian who kindly provided me a snippet to run once per app lifetime. It tackles the problem from two fronts: Sets proxy system properties (http.proxy* and https.proxy*) Sets the default Authenticator The code is:

One thing that can be improved would be a configuration-based (e.g. defined environment variable) bypass code, rather than a hostname…read more

Mock an Observable Interface

Posted by in Software, Tutorial

TL;DR: Use a doAnswer() with a new Answer<Observable<Type>>() if you have to mock observables. Today I wrote my first test where I had to mock an observable interface:

where the User model is:

Solution In my test class (named UserDatabaseTest), I first mocked the interface:

` Then, my setup() method is:

where USER_NAME is a String. To use it, you need to attach an observer and assert:

This is quite simple. Alternative Note: This I haven’t tried yet, but here it goes… If you want…read more

Load Glyphs with LibGDX

Posted by in Software

TL;DR: Remember to give the font loader the list of characters to render from the font! I’m trying to get a more resolution-independent mechanism to load simple shapes in place. After investigating SVG loading for several days I got nowhere. Then, I found out that libGDX actually supports vector fonts (TTF). Woo-hoo! Internally, it’ll convert the loaded font to a BitmapFont, for a specific size (if you want to use the same font with different sizes, you’ll have to load and store it accordingly). Preliminary stuff It is important (to…read more

About Java Exceptions in SE 7

Posted by in Software

TL;DR: JDK 7 improves on exception handling (less code, base exception class…). Base exception class Reflective operations exceptions now have a base class ReflectiveOperationException. This allows you to to a global catch rather than one for each exception. Now you can write:

Note: Reflective operations are ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchFieldException, NoSuchMethodException. Multi-catch Previously, you’d had to write code like this:

Now, you can write:

Woo-hoo! Less lines of code! Final catch Normally, when you re-throw form a catch, you only re-throw the caught exception (or a…read more

TIL: Lambda Expressions

Posted by in Software

TL;DR: A lambda expression is a shortcut notation to anonymous inner classes containing a single method. Usually, they define implementations for functional interfaces (interfaces declaring a single method). Today I finally found out what lambda expressions are (formally) by watching this tutorial. Lambda expressions are a concise way of passing pieces of code around. You can look at lambda expressions as shorthand for anonymous inner classes. There is a catch though: They are valid for functional interfaces. Functional interfaces Functional interfaces are interfaces defining a single method, like this:

read more