Jasperreports 5.6.0 で Java8 LocalDate を出力するには

Jasperreports 5.6.0 で、Java8 LocalDate を出力するには、Jassperreports がテンプレートを読んで実行するコンパイラ
を別に用意しないとならなに。もう、Jassperreports が Java 8 に対応しないからだ。
コミュニティを探した結果。Eclipse の jdt コンパイラを指定する方法を見つけた。

→ Jasperreports 6.5.1 以降では、もはやその必要はない!
Jasperreports でLocalDate をフォーマット出力 - Oboe吹きプログラマの黙示録


環境。。。

Maven pom.xml

<dependency>
   <groupId>net.sf.jasperreports</groupId>
   <artifactId>jasperreports</artifactId>
   <version>5.6.0</version>
   <exclusions>
       <exclusion>
          <groupId>eclipse</groupId>
          <artifactId>jdtcore</artifactId>
       </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.eclipse.jdt.core.compiler</groupId>
    <artifactId>ecj</artifactId>
    <version>4.4</version>
</dependency>

jrxml での設定、1つ1つの jrxml ファイルに、設定が必要になるのが難点だけど。

LocalDate の出力フォーマットを指定する為に、java.time.format.DateTimeFormatter を使うなら、
インポート設定をする。
f:id:posturan:20161023131216j:plain

つまりこれは

   <import value="java.time.format.DateTimeFormatter"/>

に相当して、XML ファイル上。。

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
 name="sample2_subreport1" pageWidth="555" pageHeight="802" columnWidth="555"
 leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="e7afe545-1b11-4653-a894-10c52167832f">
   <property name="ireport.zoom" value="2.5937424601000028"/>
   <property name="ireport.x" value="414"/>
   <property name="ireport.y" value="0"/>
   <import value="java.time.format.DateTimeFormatter"/>

のように、追加される。

Field および Parameter の指定では、クラス定義として。java.time.LocalDate を指定する必要がある。
フィールドの例
f:id:posturan:20161023131633j:plain

   <field name="at_date" class="java.time.LocalDate"/>

パラメータの例
f:id:posturan:20161023131804j:plain

   <parameter name="nowdate" class="java.time.LocalDate" isForPrompting="false"/>

これを出力する記述として、、

   $F{at_date}.format(DateTimeFormatter.ofPattern("yyyy年 M月 d日"))


   $P{nowdate}.format(DateTimeFormatter.ofPattern("yyyy年 M月 d日"))

を jrxml に記述する。

stopPropagationの例、checkbox を td セルのクリックでも制御

JavaScript stopPropagationの例、メモ。

テーブルタグに配置したcheckbox を td セルのクリックでも制御


class="sample" の 配下の tableで、、1列目に checkbox ある場合、、

$(".sample td").click(function(){
   if ($(this).parent().children("td:nth-child(1)").children("input[type='checkbox']").prop("checked")){
      $(this).parent().children("td:nth-child(1)").children("input[type='checkbox']").prop("checked", false);
   }else{
      $(this).parent().children("td:nth-child(1)").children("input[type='checkbox']").prop("checked", true);
   }
});
$(".sample td input[type='checkbox']").click(function(ev){
   ev.stopPropagation();
});

Jasperreports のコンパイル実行を、Stream 処理でまとめる

Jasperreports のコンパイル実行を、Stream 処理でまとめてみました。

/**
 * @param directoryPath jrxmlを置いたディレクトリPATH
 * @return Map<String, Throwable> key=コンパイル処理実行のjrxmlファイルPATH、value=コンパイルエラー発生のThroawble
 */
public static Map<String, Throwable> jasperCompileWithDirectory(String directoryPath){
   return  Arrays.stream(new File(directoryPath).listFiles()).filter(e->e.getName().endsWith(".jrxml"))
   .collect(HashMap::new,(r, t)->{
      Throwable error = null;
      try(InputStream in=new FileInputStream(t.getAbsolutePath());
          OutputStream out = new FileOutputStream(t.getAbsolutePath().replaceAll("\\.jrxml", ".jasper"))
      ){
         JasperCompileManager.compileReportToStream(in, out);
      }catch(Exception e){
         error = e;
      }finally{
         r.put(t.getAbsolutePath(), error);
      }
   }, (r, u)->r.putAll(u));
}

使い方は、、、

    Map<String, Throwable> map = jasperCompileWithDirectory(Thread.currentThread().getContextClassLoader()
                                                            .getResource("").getPath() + "../../template");

のようにして、map の value が null ならコンパイル成功