čtvrtek 6. října 2011

Simple Javascript SCORM 2004 API

Just a very very simple implementation of SCORM 2004 API for LMS (RTS) developers. It implements just basic SCORM 2004 features. Hope, someone else may find it useful, see http://sites.google.com/site/xmedeko/code/misc/simple-javascript-scorm-2004-api.

úterý 31. května 2011

Java zsync

The very first implementation of zsync in Java: http://sourceforge.net/projects/jazsync/. It has no release yet, check out the source code from SVN.

středa 11. května 2011

ZK-DL Released!

ZK-DL, an extension to the ZKoss framework (ZK 5), has been released under LGPL just a few weeks ago!. ZK_DL provides especially:

  1. Integration with Spring.
  2. Flexible listbox, combobox and lovbox (a kind of combobox) components with automatic sorting and searching build upon the Hibernate Criteria API.
  3. And much more, see http://zk.datalite.cz/zk-dl.

The documentation is at http://zk.datalite.cz/zk-dl and the source code can be found at http://code.google.com/p/zk-dl/.

I have used DL-ZK with EJB3 on JBoss 5.1 successfully. So I had not taken the advantage of ZK-DL Spring integration (IMHO better than original ZK Spring integration). Anyway the ZK-DL listobox, combobox and lovbox components has greatly simplified our development and provided a rich yet simple interface to our users.

Many thanks to Jiří Bubník and Karel Čemus for their valuable help and cooperation. I wish DL-ZK long life and have many users!

čtvrtek 10. března 2011

JPA Queries - A Few Methods to Simplify the Life with JPA/JPQL

The JPA Query.getSingleResult() method is usable only when the JPQL has exactly one result. Yeah, you can catch NoResultException or NonUniqueResultException exceptions, but the try ... catch block make the code less readable. So I have coded three static method to overcome this JPA deficiency:
public class Queries {
    /**
     * @param query
     * @return {@code true}, the query has one or more results, otherwise returns {@code false}.
     */
    public static boolean hasAnyResult(Query query) {
        query.setMaxResults(1);
        final List<?> list = query.getResultList();
        return list.size() > 0;
    }

    /**
     * @param query
     * @return The only one result or {@code null}, when the query has no result.
     * @throws NonUniqueResultException
     *             when the query has more than one result.
     */
    public static Object singleResultOrNull(Query query) {
        query.setMaxResults(2);
        final List<?> list = query.getResultList();
        final int size = list.size();
        if (size <= 0) {
            return null;
        }
        if (size > 1) {
            throw new NonUniqueResultException("result returns more than one element");
        }

        // return first element from the list
        return list.get(0);
    }

    /**
     * @param query
     * @return The first result or {@code null}, when the query has no result.
     */
    public static Object firstResultOrNull(Query query) {
        query.setMaxResults(1);
        final List<?> list = query.getResultList();
        final int size = list.size();
        if (size <= 0) {
            return null;
        }
        if (size > 1) { // should not happened, Hibernate bug?
            Logger.getLogger(Queries.class).error("firstResultOrNull more rows returned. setMaxResults(1) does not work?");
        }

        return list.get(0);
    }

    /**
     * Constructor. No instances.
     */
    private Queries() {
    }
}
The hasAnyResult is the boolean query method - you may test if something exists in database. The best is to use it with some JPQL query returning only id of an entity, e.g.:
Queries.hasAnyResult(em.createQuery("SELECT id FROM Person WHERE ..."));
I think the usage of the singleResultOrNull is obvious. Use it, when you are sure the result returns one or no result. The exception NonUniqueResultException should warn you that something is wrong, wither the query or the data.

The firstResultOrNull returns just the first result of the query. Typically, use it with queries with "ORDER BY" statement, like "get the Person with lowest/highest salary."

úterý 15. února 2011

Oracle Like Decode in JasperReports

Oracle SQL has a nice function DECODE. I am using Java Beans data source for JasperReports and sometimes I have missed such a function in JasperReports expressions. One can achieve the same functionality by a ternary operator ? :. But it is not nice for more complicated conditions. So I have created my own Java decode:
public static Object decode(Object value, Object... arg) {
  if (arg == null) {
    return value;
  }
  int n = arg.length;
  for (int i = 0; i < n; i+=2) {
    int j = i + 1;
    if (j >= n) {
      // only the default remains
      return arg[i];
    }
    if (equal(value, arg[i])) {
      return arg[j];
    }
  }
  // no match found, no default is specified
  return value;
}
Then import it into JasperReports *.jrxml report by:
<import value="static com.mypackage.MyClass.decode"/>
And use it in the report like:
"" + decode(count, 0, "no one", 1, "one", 2, "two", "too many: " + count);

úterý 25. ledna 2011

ZK and Converting to a Negative Number

When I was working with ZK Web Framework, I have needed to convert some number to the negatives for the databinding. I have created a new type converter NumberNegativeConverter. Hope, someone else may find it useful, see http://sites.google.com/site/xmedeko/code/zk-web-framework/numbernegativeconverter.