001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.plugin.column;
017
018import org.opengion.hayabusa.common.HybsSystem;
019import org.opengion.hayabusa.db.AbstractRenderer;
020import org.opengion.hayabusa.db.CellRenderer;
021import org.opengion.hayabusa.db.DBColumn;
022import org.opengion.fukurou.util.StringUtil;
023
024/**
025 * BARCODE レンデラーは、カラムのデータからCODE39 バーコードを表示する場合に使用するクラスです。
026 * div 要素に、バーコードに変換する文字列を設定して、それを JsBarcode.code39.min.js を使用して バーコードに
027 * 変換します。QRコードと統一できなかったため、canvas や video をJavaScript内部で生成しています。
028 * バーコードは、img タグに適用されます。
029 *
030 * スタート/エンドキャラクタ(*)は、JavaScript側(JsBarcode)で付与されます。データに付けるとエラーになる。
031 * チェックデジットのモジュラス43 は、既存のバーコードフォント仕様との互換性から、付けません。
032 *
033 * 基本的な構造は、データ設定用の div 要素と、表示用のimg、起動用のscript です。
034 *
035 *    <div id='div≪カラム名≫' class='BARDATA' >≪値≫</div>
036 *    <img src='' id='img≪カラム名≫' class='BARIMG' ≪パラメータ≫ '></canvas>
037 *    <script>barView('≪カラム名≫');</script>
038 *
039 * script に CDNサービス を使うと、無線環境(iPad等)ではものすごく遅くなったため、ローカルに配置することにします。
040 * <script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.11.4/dist/barcodes/JsBarcode.code39.min.js"><!-- --></script>
041 *
042 * これらは、使用する画面に、組み込んでください。
043 * <script src="{@SYS.JSP}/option/JsBarcode.code39.min.js"><!-- --></script>
044 * <script src="{@SYS.JSP}/option/videocamera.js"><!-- --></script>
045 *
046 * @og.rev 7.4.2.2 (2021/05/28) 新規作成
047 * @og.group データ表示
048 *
049 * @version  7.4
050 * @author   Kazuhiko Hasegawa
051 * @since    JDK11.0,
052 */
053public class Renderer_BARCODE extends AbstractRenderer {
054        /** このプログラムのVERSION文字列を設定します。   {@value} */
055        private static final String VERSION = "7.4.2.2 (2021/05/28)" ;
056
057        private static final String BASE_HTML = "<div id='div≪カラム名≫' class='BARDATA'>≪値≫</div>"
058                                                                        + CR +  "<img src='' id='img≪カラム名≫' class='BARIMG' ≪パラメータ≫ >"
059                                                                        + CR +  "<script>barView('≪カラム名≫');</script>" ;
060
061        private  String         name;
062        private  String         param;
063
064        /**
065         * デフォルトコンストラクター
066         *
067         * @og.rev 7.4.2.2 (2021/05/28) 新規作成
068         */
069        public Renderer_BARCODE() { super(); }          // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
070
071        /**
072         * DBColumnオブジェクトを指定したprivateコンストラクター。
073         *
074         * @og.rev 7.4.2.2 (2021/05/28) 新規作成
075         *
076         * @param       clm     DBColumnオブジェクト
077         */
078        private Renderer_BARCODE( final DBColumn clm ) {
079                super();
080
081                name  = clm.getName();
082                param = StringUtil.nvalAdd( clm.getRendererParam() ,
083                                                                        clm.getRendererAttributes().get( "optionAttributes" ) );
084        }
085
086        /**
087         * 各オブジェクトから自分のインスタンスを返します。
088         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
089         * まかされます。
090         *
091         * @og.rev 7.4.2.2 (2021/05/28) 新規作成
092         *
093         * @param       clm     DBColumnオブジェクト
094         *
095         * @return      CellEditorオブジェクト
096         * @og.rtnNotNull
097         */
098        public CellRenderer newInstance( final DBColumn clm ) {
099                return new Renderer_BARCODE( clm );
100        }
101
102        /**
103         * データの表示用文字列を返します。
104         *
105         * @og.rev 7.4.2.2 (2021/05/28) 新規作成
106         *
107         * @param   value 入力値
108         *
109         * @return  データの表示用文字列
110         * @og.rtnNotNull
111         */
112        @Override
113        public String getValue( final String value ) {
114                return makeBarcode( name, value );
115        }
116
117        /**
118         * データの表示用文字列を返します。
119         *
120         * @og.rev 7.4.2.2 (2021/05/28) 新規作成
121         *
122         * @param   row   行番号
123         * @param   value 入力値
124         *
125         * @return  データ表示用の文字列
126         * @og.rtnNotNull
127         */
128        @Override
129        public String getValue( final int row,final String value ) {
130                final String newName = name + HybsSystem.JOINT_STRING + row;
131                return makeBarcode( newName, value );
132        }
133
134        /**
135         * データ出力用の文字列を作成します。
136         * ファイル等に出力する形式を想定しますので、HTMLタグを含まない
137         * データを返します。
138         * 基本は、#getValue( String ) をそのまま返します。
139         *
140         * @og.rev 7.4.2.2 (2021/05/28) 新規作成
141         *
142         * @param   value 入力値
143         *
144         * @return  データ出力用の文字列
145         * @og.rtnNotNull
146         * @see         #getValue( String )
147         */
148        @Override
149        public String getWriteValue( final String value ) {
150                return value == null ? "" : value;
151        }
152
153        /**
154         * データの表示用文字列を返します。
155         *
156         * @og.rev 7.4.2.2 (2021/05/28) 新規作成
157         *
158         * @param       name    カラム名
159         * @param   value       入力値 表示するファイルのアドレス
160         *
161         * @return  データ表示用の文字列
162         * @og.rtnNotNull
163         */
164        private String makeBarcode( final String name,final String value ) {
165
166                return BASE_HTML.replaceAll( "≪カラム名≫"  , name )
167                                                .replaceAll( "≪パラメータ≫" , param )
168                                                .replaceAll( "≪値≫"     , value );
169        }
170}