Basics – JasperReport on your Java Desktop App


Overview

Have you ever had a client that only wants a basic reporting screen that has the values in it? Good thing JasperReports was invented!

In this post, I’m going to share a small example of how you can create your own report in iReport, compile it to *.jasper and call them via your own Java Desktop App!

Step by Step

Create your report in iReport

You can actually code this in XML but that would be inefficient. iReport is an open source report editor that developers can use to design reports. Jasper Reports are basically like Java Source code that you can compile in different language, as of this writing, it supports Java (of course), Groovy and JavaScript. Obviously, if you want to create a report that can be viewed in the Java based report viewer, you should compile it in Java. If you want to view it in a Web Browser, it should be in Javascript (although, technically, you can wrap a JFrame in an Applet and place the JasperReport viewer on the Applet, but not getting ahead in here).

In this example, let’s just create a simple Report (a blank perhaps) and put a few parameters and fields.

Screenshot at Jun 15 22-20-20

Once done, compile it and locate your jasper file (*.jasper is the compiled source file).

Code it!

I wouldn’t dive much on how you where you should put your jasper files. It can be externally access or via resource classpath. My recommendation is of course, put it in your classpath, but we all have our own case of usage. If there will be a lot of adjustments to your report because of alignments, then I would go with putting it externally. This is so that you wouldn’t have to compile your project again just to do minor alignments.

Before we go to the snippet code, make sure you have the dependency below.

<dependency>
     <groupId>net.sf.jasperreports</groupId>
     <artifactId>jasperreports</artifactId>
     <version>5.5.0</version>
 </dependency>

If you’re not familiar with Maven, you should be, read here. 🙂

and here is a sample of code.

// Get the file, put it in a stream
FileInputStream fis = new FileInputStream(this.getClass().getResource("").getFile());
BufferedInputStream bufferedInputStream = new BufferedInputStream(fis);
// We do have parameters, so we create the map object to store values for them.
Map map = new HashMap();
map.put("SAMPLE1", "This is a sample 1 text");
map.put("SAMPLE2", "This is a sample 2 text");

// We create the jasper report object, load the report with JasperFillManager object and use the
// JasperViewer to display the report            
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(bufferedInputStream);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map,new JREmptyDataSource());
JasperViewer.viewReport(jasperPrint, false);

Output!
Screenshot at Jun 15 22-19-45

If you’re having issues, just let me know (via comments), I can help you out.

Python-ing


Over the last couple of weeks, I’ve tried learning Python. It’s not as far from Java as someone would say, I’d actually call it one of the closest to the language. 🙂 I learned the language for one primary reason, I want to automate a process. I have the logical design, process flow, but I don’t want to use my comfort level language which is Java cause… we’ll it’s to comfy.

1st off, when and where to learn. A great deal of tutorials are all over the web! There’s no excuse for not learning it, even the advance stuff. It’s all about using your creativity to use the language.  

http://www.learnpython.org/

2nd, the tools. I’m using PyDev plugin for Eclipse – so Eclipse IDE plus a a python interpreter.

PyDev_-_PyDevSample1_sample_package_sample_py_-_Eclipse_-__Users_alvinreyes_EclipseProjects_Python

3rd, learn a web framework (optional). Well this is not really required. I tried learning Django and made a simple registration application with it. It’s not as simple as using CakePHP or CI, but it does the job. It has a “bake” like command that generates model that can be used in your application, as well as controller and view level (modules) scaffoldings. Very neat, but not as neat as Cake.

 

I had fun using and trying to achieve something with it, especially when I’m trying to parse through an XML file to get the nodes and its values. I’m trying to create a automated process to deploy our apps in a Weblogic console given an XML formatted or JSON file as a parameter. Simple and Straight forward. I’ll roll it out when the right time comes.

Calling SH Unix command via Java


By far, one of the most simple, yet overlooked simple solution to batch and process executions.

Running an SH via Java isn’t really hard – there’s already an existing Object Utility for it, just need to call it correctly though. Java Code:

java_code_call_sh

the samplescript.sh is a custom script I created using nano. Basically, I pass alvin jay and reyes as parameters and get them by using $1,2 and 3.

nano_script

Console Output: I manage to get the output from the output stream object of the Process.

dir_sh

Download the source code here:

JavaFX Tutorial – Basics


JavaFX seem to be gaining ground on the RIA space. With the right tools and development support, it wil definitely have its toll on the next best technology “thing!”. I’m not writing any JavaFX review here since there is a lot of Technology critiques out there that probably reviewed it extensively, but instead, I will write a simple tutorial on how can you develop JavaFX application in your MacOSX lion. 🙂

First some pre-requisites:

  • JavaFX Runtime Environment
  • Java Runtime Environment
  • JavaFX SDK
  • JavaFX Scene Builder
  • JavaFX IDE – I had chosen NetBeans 7 as it already has support for JavaFX.

This can all be downloaded on the Oracle Website. You may google it. 🙂

Requirement: Create a simple application that accepts (you guess it) person details (simple registration), a custom web browser and some analytics.

Technology: JavaFX and JPA 🙂

Step 1: Create the Database and Tables

Simple Database and Table, download the SQL file: here

Step 2: Create the User Interface and specify the controller

Using the JavaFX Scene Builder, create the user interface.

Step 3: Development

Code the App!

NetBeans has its support for JPA – so I used it to interact with the database.

Download the source:  here

Its basically a very simple application, but I think this sample will give you a brief introduction or a head start on how to actually develop an application using the platform.

Not a bad alternative if you want to create Desktop Applications, which of course, .net offers much better solution. Though the main take away here is that JavaFX redefines Java Desktop Application Development – flexible enough to support the best of both worlds (Desktop and Web) and if thats not enough, it also supports mobile phones.

SVN File List – Listing SVN Files in Java


Requirement 1: Given an SVN Repository URL with Username and Password, list all Files inside the directory.

Initialize SVN First by declaring a repostiroy object:

The above method will initialize the repository and call the list of files located on the given path “deploy”. This means all files at: svn://localhost/branches/project/deploy

The above code will re-curse to get all the list of files. We need to do a recursion to exclude the Directories and consider only files. 🙂

Sample code: here

Download the SVNKIT (Java API for SVN): here
Enjoy!