And Todays’s Quote Would Be..

Life begins at the end of your comfort zone.

Neale Donald Walsch

Fixing Jasper Report Error, “Font ‘Arial’ is not available to the JVM.”

Yesterday, i had a weird error when deploying to my Red Hat machine. Somehow my pdf generated report is throwing exceptions, it said “Font ‘Arial’ is not available to the JVM.”. A little bit weird since on my development machine, everything is running well.

After i run this command,

fc-list | grep "Arial"

I found out that “Arial” font isnt installed on my RedHat server, a little bit tricky due to im unable to run “yum” command because the server is behind a network firewall. So my workaround is download a .rpm, and installed it manually. After several minutes googling, i found a very recommended .rpm installer.


http://www.mjmwired.net/resources/mjm-fedora-f11.html#ttf

I installed it using

 sudo rpm -ivh msttcore-fonts-2.0-3.noarch.rpm

And check my installed fonts

fc-list | grep "Arial"

Now i can see “Arial” fonts on my list of installed fonts on RedHat, restart my tomcat and my pdf reporting is running well again.

Here is the complete stacktrace of the error,

21:57:12,135 INFO  [stdout] (AsyncLogger-1) <2014-09-29 21:57:12,131>,[http-/0.0.0.0:80-94]>>[ERROR]Font 'Arial' is not available to the JVM. See the Javadoc for more details. net.sf.jasperreports.engine.util.JRFontNotFoundException: Font 'Arial' is not available to the JVM. See the Javadoc for more details.
21:57:12,135 INFO  [stdout] (AsyncLogger-1) 	at net.sf.jasperreports.engine.fonts.FontUtil.checkAwtFont(FontUtil.java:357)
21:57:12,135 INFO  [stdout] (AsyncLogger-1) 	at net.sf.jasperreports.engine.fill.SimpleTextLineWrapper.loadFont(SimpleTextLineWrapper.java:369)
21:57:12,135 INFO  [stdout] (AsyncLogger-1) 	at net.sf.jasperreports.engine.fill.SimpleTextLineWrapper.getGeneralFontInfo(SimpleTextLineWrapper.java:339)
21:57:12,135 INFO  [stdout] (AsyncLogger-1) 	at net.sf.jasperreports.engine.fill.SimpleTextLineWrapper.createFontInfo(SimpleTextLineWrapper.java:279)
21:57:12,135 INFO  [stdout] (AsyncLogger-1) 	at net.sf.jasperreports.engine.fill.SimpleTextLineWrapper.start(SimpleTextLineWrapper.java:241)
21:57:12,135 INFO  [stdout] (AsyncLogger-1) 	at net.sf.jasperreports.engine.fill.TextMeasurer.measure(TextMeasurer.java:537)
21:57:12,136 INFO  [stdout] (AsyncLogger-1) 	at net.sf.jasperreports.engine.fill.JRFillTextElement.chopTextElement(JRFillTextElement.java:623)
21:57:12,136 INFO  [stdout] (AsyncLogger-1) 	at net.sf.jasperreports.engine.fill.JRFillTextField.prepare(JRFillTextField.java:699)
Google+

Building A Runnable Maven JavaSE Desktop Application And Including Its Jar Dependencies

Usually it’s very easy to create a runnable jar using Netbean’s ant plugin, but on this project im trying to utilize maven. One painfull thing is somehow maven is unable to build create a runnable jar file which included its jar dependencies.

After several minutes googling, i’ve found a very good solution. Here is the content of my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.edw</groupId>
    <artifactId>Edw</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>        
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${targetdirectory}</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>                            
                            <classpathPrefix>lib/</classpathPrefix>                            
                            <mainClass>com.edw.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/customAssembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> 
                        <phase>package</phase> 
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

And dont forget, adding an xml file, customAssembly.xml which located on src/main/assembly/ folder

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>bin</id>
    <formats>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/dependency</directory>
            <outputDirectory>lib</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

Just build your application from Netbeans, and you will find a zip file on your Netbeans’ target folder, which consist of a runnable jar file and a lib folder containing your jar dependencies. ;-)

Google+

Regular Expression to Check Indonesian MobilePhone Provider

These are simple regex to check Indonesia’s Mobile phone provider based on its number,

telkomsel.regex = ^(\\+62|\\+0|0|62)8(1[123]|52|53|21|22|23)[0-9]{5,9}$

simpati.regex = ^(\\+62|\\+0|0|62)8(1[123]|2[12])[0-9]{5,9}$

as.regex = ^(\\+62|\\+0|0|62)8(52|53|23)[0-9]{5,9}$

indosat.regex= ^(\\+62815|0815|62815|\\+0815|\\+62816|0816|62816|\\+0816|\\+62858|0858|62858|\\+0814|\\+62814|0814|62814|\\+0814)[0-9]{5,9}$

im3.regex = ^(\\+62855|0855|62855|\\+0855|\\+62856|0856|62856|\\+0856|\\+62857|0857|62857|\\+0857)[0-9]{5,9}$

xl.regex = ^(\\+62817|0817|62817|\\+0817|\\+62818|0818|62818|\\+0818|\\+62819|0819|62819|\\+0819|\\+62859|0859|62859|\\+0859|\\+0878|\\+62878|0878|62878|\\+0877|\\+62877|0877|62877)[0-9]{5,9}$

Hope it can help others, ;-)

Google+

Using HashMaps as MyBatis’ Parameter and Return Values

Usually im using javabean as both parameter and return values for MyBatis. But actually, MyBatis also able to use HashMaps as both parameter and return values. Here is the example,

First is a simple pom file to load all the libraries needed,

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.edw</groupId>
    <artifactId>MyBatisSelectMap</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.2</version>
        </dependency>
    </dependencies>
</project>

And my configuration.xml file, to hold my MyBatis’ configuration

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="UNPOOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost/test"/>
                <property name="username" value="root"/>
                <property name="password" value="password"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/edw/mybatisselectmap/sqlmap/SelectMapper.xml" />
    </mappers>
</configuration>

An xml file, to hold all my queries, i named it SelectMapper.xml.
Please note the difference between using #{} and ${}. Hashtags sign means a prepared statement variables, while dollar sign means a simple string replace.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.edw.mybatisselectmap.mapper.SelectMapper" >    
    <select resultType="java.util.Map"  parameterType="java.util.Map" id="select" >
        SELECT
                *
        FROM
                testing 
        WERE  
                name LIKE #{myname}
        ORDER BY ${orderBy} 
    </select>    
</mapper>

A simple java file to map my xml queries,

package com.edw.mybatisselectmap.mapper;

import java.util.HashMap;
import java.util.List;

public interface SelectMapper {
    List<HashMap<Object, Object>> select(HashMap<Object, Object> hashMap);
}

and my java configuration file, to load all my MyBatis’ configurations

package com.edw.mybatisselectmap.config;

import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisSqlSessionFactory {

    private static final SqlSessionFactory FACTORY;

    static {
        try {
            Reader reader = Resources.getResourceAsReader("com/edw/mybatisselectmap/sqlmap/Configuration.xml");
            FACTORY = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            throw new RuntimeException("Fatal Error. Cause: " + e, e);
        }
    }

    public static SqlSessionFactory getSqlSessionFactory() {
        return FACTORY;
    }
}

And finally my main java file,

package com.edw.mybatisselectmap;

import com.edw.mybatisselectmap.config.MyBatisSqlSessionFactory;
import com.edw.mybatisselectmap.mapper.SelectMapper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

public class Main {

    private static Logger logger = Logger.getLogger(Main.class);

    public static void main(String[] args) {
        SqlSession sqlSession = null;
        try {
            
            HashMap<Object, Object> hashMapParameter = new HashMap<Object, Object>();
            hashMapParameter.put("orderBy", "id");
            hashMapParameter.put("myname", "%edw%");
            
            sqlSession = MyBatisSqlSessionFactory.getSqlSessionFactory().openSession(true);
            SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
            
            List<HashMap<Object, Object>> hashMapResults = mapper.select(hashMapParameter);
            
            for (HashMap<Object, Object> hashMapResult : hashMapResults) {
                logger.debug(hashMapResult);
            }
        } catch (Exception e) {
            logger.error(e, e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
}

You can find this post’s sourcecode at my github page here, https://github.com/edwinkun/MyBatisSelectMap.

Google+

A Simple Indonesian Geographic Coordinate Conversion Between Degrees to Degree-Minute-Second (DMS)

Recently i have a simple GIS project which force me to display an Indonesian DMS coordinate format, instead of the usual decimal format. This is a simple utility class i used to do convertion.

import java.text.DecimalFormat;

public class DegreeConverterTest {

    public static final String LONGITUDE = "longitude";
    public static final String LATITUDE = "latitude";

    public DegreeConverterTest(String testName) {
    }

    public static void main(String a[]) {
        System.out.println(degree2DMS(-8.857, LATITUDE));
        System.out.println(degree2DMS(10.645, LATITUDE));
        System.out.println(degree2DMS(135.696, LONGITUDE));
    }

    public static String degree2DMS(double coordinate, String mode) {
        String dms = "";

        char degree = '\u00B0';
        int degLong, minLongA;
        double minLong, secLong;
        DecimalFormat df = new DecimalFormat("#,###,####0.0000");

        if (LONGITUDE.equals(mode)) {
            // minus is BB (Bujur Barat or west longitude)
            // plus is BT (Bujur Timur of east longitude)
            degLong = (int) coordinate;
            minLong = (coordinate % 1) * 60;
            minLongA = (int) minLong;
            secLong = (minLong % 1) * 60;
            String secLongAs = String.valueOf(df.format(secLong));
            dms = degLong + degree + " " + minLongA + "' " + secLongAs + "\" BT";
        } else if (LATITUDE.equals(mode)) {
            // minus is LS (Lintang Selatan or south latitude)
            // plus is LU (Lintang Utara or north latitude)
            boolean isMinus = true;
            String minusDegree = "LS";
            degLong = (int) coordinate;
            String degLongS = String.valueOf(degLong);
            if (degLongS.contains("-")) {
                degLongS = degLongS.replace("-", "");
            } else {
                isMinus = false;
                minusDegree = "LU";
            }

            minLong = (coordinate % 1) * 60;
            minLongA = (int) minLong;
            String minLongAs = String.valueOf(minLongA);
            if (isMinus) {
                minLongAs = minLongAs.replace("-", "");
            }

            secLong = (minLong % 1) * 60;
            String secLongAs = String.valueOf(df.format(secLong));
            if (isMinus) {
                secLongAs = secLongAs.replace("-", "");
            }

            dms = degLongS + degree + " " + minLongAs + "' " + secLongAs + "\" " + minusDegree;
        }
        return dms;
    }
}

And this is the output i got on my netbeans’ console

8° 51' 25.2000" LS
10° 38' 42.0000" LU
311 41' 45.6000" BT
Google+