JavaEE 7 JCache for JasperReport’s compiled reports

Environment:

  • Payara 4.1.1.154 with Hazelcast enabled
  • addition in pom.xml:
<dependency>
   <groupId>javax.cache</groupId>
   <artifactId>cache-api</artifactId>
   <version>1.0.0</version>
</dependency>

Implementation:

package com.rs.pdfproducer.invoice.control;

import com.rs.pdfproducer.PdfProducerException;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.cache.annotation.CacheDefaults;
import javax.cache.annotation.CacheKey;
import javax.cache.annotation.CacheRemoveAll;
import javax.cache.annotation.CacheResult;
import javax.inject.Singleton;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.Renderable;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.renderers.BatikRenderer;

/**
 *
 * @author realsoft
 */
@Singleton
@CacheDefaults(cacheName = "jasperCache")
public class JasperReportProducer {

    public JasperReport getJasperReport(String fileName) throws PdfProducerException {

        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(getJasperFileContent(fileName));
            JasperReport report = (JasperReport) JRLoader.loadObject(bais);
            return report;
        } catch (JRException ex) {
            Logger.getLogger(JasperReportProducer.class.getName()).log(Level.SEVERE, null, ex);
            throw new PdfProducerException("Error Loading JasperReport File: " + fileName, ex);
        } catch (IOException ex) {
            Logger.getLogger(JasperReportProducer.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @CacheResult
    private byte[] getJasperFileContent(@CacheKey String fileName) throws IOException {
        // FOR Test purpose!!! Remove later
        System.out.println("==>Read jasperreport file");
        Path path = Paths.get(fileName);
        return Files.readAllBytes(path);
    }

}

Usage:

    @Inject
    JasperReportProducer reportProducer;
    
    reportAll = reportProducer.getJasperReport("all.jasper");



Comments:
In the first version @CasheResult was JasperReport class but that caused unexpected problems (missing page numbers of reports, generated from cache). In this version @CasheResult is a simple byte[]. Works perfect.

Netbeans Look and Feel Problem

How to fix Netbeans 7.4 (in my case) problems with Linux Mint Cinnamon:

Download JGoodies Looks and JGoodies Common and extract zip files somewhere.

Change your <NETBEANS-FOLDER>/etc/netbeans.conf by adding “–cp:p /home/ehurmuzov/java/jgoodies/jgoodies-looks-2.5.3/jgoodies-looks-2.5.3.jar –cp:p /home/ehurmuzov/java/jgoodies/jgoodies-common-1.6.0/jgoodies-common-1.6.0.jar  –laf com.jgoodies.looks.plastic.Plastic3DLookAndFeel” to the netbeans_default_options line:

netbeans_default_options=”-J-client -J-Xss2m -J-Xms32m -J-XX:PermSize=32m -J-Dapple.laf.useScreenMenuBar=true -J-Dapple.awt.graphics.UseQuartz=true -J-Dsun.java2d.noddraw=true -J-Dsun.java2d.dpiaware=true -J-Dsun.zip.disableMemoryMapping=true –cp:p /home/ehurmuzov/java/jgoodies/jgoodies-looks-2.5.3/jgoodies-looks-2.5.3.jar –cp:p /home/ehurmuzov/java/jgoodies/jgoodies-common-1.6.0/jgoodies-common-1.6.0.jar  –laf com.jgoodies.looks.plastic.Plastic3DLookAndFeel”

Here is the result:

Before:

nb-before-menu

 

After:

nb-after-menu

 

Much better, isn’t it?

My Linux Applications

Short list of what I’m using on my Linux Desktop:

Netbeans (a couple of versions)
SQL Developer – Oracle
Squirrel SQL Client
JEdit
Meld
VirtualBox

Filezilla
gSTM
FeeeMind
xCHM
yEd Graph Editor
mc
Remmina Remote Desktop Client
Skype
Wireshark
Rhythmbox
VLC Media Player

ClamTK
catfish
Darktable
Dia
Inkscape
MyPaint

Get Geo Location from Image File

 

public static GeoLocation getMetadata(String filename) {
    GeoLocation gl = null;

    try {
        File file = new File(filename);
        Metadata metadata = ImageMetadataReader.readMetadata(file);

        for (Directory directory : metadata.getDirectories()) {
            if (directory instanceof GpsDirectory) {
                GpsDirectory gpsd = (GpsDirectory)directory;
                gl = gpsd.getGeoLocation();
            }
        }

    } catch (ImageProcessingException | IOException ex) {
        Logger.getLogger(ImageHelper.class.getName()).log(Level.SEVERE, null, ex);
    }
    return gl;
}

 

Very simple.
The example uses metadata extractor in Java.
http://www.drewnoakes.com/code/exif/

JSF 2.0 Composite Components #1

Starting with something very easy – label. We’ll make it bold to separate from input/output text.

Usual directories in the web project:


<Web Pages>
<Web Pages>/WEB-INF
<Web Pages>/resources
<Web Pages>/resources/css
<Web Pages>/resources/images
<Web Pages>/resources/realsoft


In bold is the directory where I’ll put my custom components. Choose a suitable for you name. Below is label.xhtml file:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:h="http://java.sun.com/jsf/html">
<!-- INTERFACE -->
<cc:interface>
  <cc:attribute name="value" required="true" />
  <cc:attribute name="for" required="false" />
</cc:interface>
<!-- IMPLEMENTATION -->
<cc:implementation>
  <span id="#{cc.clientId}" class="label" >
    <h:outputLabel id="#{cc.clientId}label" value="#{cc.attrs.value}" for="#{cc.attrs.for}" />
  </span>
</cc:implementation>
</html>



Java SE 1.6.0_21 (6u21)

Java SE 1.6.0_21 released.

Release Notes

SQL COUNT(DISTINCT…) on multiple columns – syntax

MySQL

SELECT COUNT(DISTINCT column1, column2) FROM mytable;

ORACLE

SELECT COUNT(DISTINCT column1 || column2) FROM mytable;

I’ll check what is the syntax for Informix server.

JPA (Java Persistence API) do not support multiple columns in statements like these, so you should use createNativeQuery();

Hello world!

Starting today 🙂