    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.io;
    017
    018import java.io.PrintWriter;
    019
    020import org.opengion.fukurou.util.StringUtil;
    021import org.opengion.hayabusa.db.DBTableModel;
    022
    023/**
    024 * TableWriter をJSON形式で出力する為の実装ｸﾗｽです。
    025 * DefaultTableWriter を継承していますので、ﾗﾍﾞﾙ、名前、ﾃﾞｰﾀの出力部のみ
    026 * ｵｰﾊﾞｰﾗｲﾄﾞして、JSON形式ﾌｧｲﾙの出力機能を実現しています。
    027 *
    028 * 出力のJSON形式(JavaScript Object Notation)は、通常版ではなく IOｒ形式のJSONです。
    029 *
    030 * {
    031 *   "status": 200,
    032 *   "message": "OK",
    033 *   "sessionInfo": "",
    034 *   "information": "",
    035 *   "data": {
    036 *     "headers": [
    037 *       { "display_label": "品目番号","display": "PN"     },
    038 *       { "display_label": "入出庫数","display": "SUNYSY" },
    039 *     ],
    040 *     "rows": [
    041 *       {"cols": [ "100-1A100-01", "T", 1,00+00"  ]},
    042 *       {"cols": [ "200-2A125-01", "T", 500,00+00"]},
    043 *       {"cols": [ "100-1A100-01", "T", 100,00+00"]},
    044 *       {"cols": [ "200-2A125-01", "T", 200,00+00"]}
    045 *     ]
    046 *   }
    047 * }
    048 *
    049 *
    050 * @og.rev 8.1.1.0 (2022/02/04) 新規作成
    051 * @og.group ﾌｧｲﾙ出力
    052 *
    053 * @version  8.1
    054 * @author   Kazuhiko Hasegawa
    055 * @since    JDK17,
    056 */
    057public class TableWriter_JSON2 extends TableWriter_Default {
    058        /** このﾌﾟﾛｸﾞﾗﾑのVERSION文字列を設定します。 {@value} */
    059        private static final String VERSION = "8.1.1.0 (2022/02/04)" ;
    060
    061        private static final String INFO = " \"status\":200,\"message\":\"OK\",\"sessionInfo\":\"\",\"information\":\"\"," ;
    062
    063        /**
    064         * ﾃﾞﾌｫﾙﾄｺﾝｽﾄﾗｸﾀｰ
    065         */
    066        public TableWriter_JSON2() { super(); }         // これも、自動的に呼ばれるが、空のﾒｿｯﾄﾞを作成すると警告されるので、明示的にしておきます。
    067
    068        /**
    069         * DBTableModel から ﾃﾞｰﾀを作成して、PrintWriter に書き出します。
    070         *
    071         * @param       writer PrintWriterｵﾌﾞｼﾞｪｸﾄ
    072         */
    073        @Override
    074        public void writeDBTable( final PrintWriter writer )  {
    075                writer.println( "{" );
    076                writer.println( INFO );
    077                writer.println( " \"data\":{\"" );
    078
    079                super.setHeaderSequence( "LD" );
    080                super.writeDBTable( writer );
    081
    082                writer.println( " }" );
    083                writer.println( "}" );
    084        }
    085
    086        /**
    087         * PrintWriter に DBTableModelのラベル情報を書き込みます。
    088         *
    089         * IORのﾍｯﾀﾞｰ情報は、
    090         *   "headers":[{"display_label":"≪ｶﾗﾑﾗﾍﾞﾙ≫","display":"≪ｶﾗﾑ≫",},…,],
    091         * 形式です。
    092         *
    093         * @param       table DBTableModelオブジェクト
    094         * @param       writer PrintWriterオブジェクト
    095         */
    096        @Override
    097        protected void writeLabel( final DBTableModel table,final PrintWriter writer ) {
    098                writer.println( "  \"headers\":[" );
    099
    100                for( int i=0; i<numberOfColumns; i++ ) {
    101                        final int clm = clmNo[i];
    102                        if( clm < 0 ) { continue; }                     // カラム飛ばし
    103                        final String val = StringUtil.tagCut( dbColumn[clm].getLabel() );
    104                        writer.print( "   {\"display_label\":\"" );
    105                        writer.print( val );
    106                        writer.print( "\",\"display\":\"" );
    107                        writer.print( dbColumn[clm].getName() );
    108                        writer.println( "\"}," );
    109                }
    110                writer.println( "  ]," );
    111        }
    112
    113        /**
    114         * PrintWriter に DBTableModelのﾃｰﾌﾞﾙ情報を書き込みます。
    115         *
    116         * IORのﾃﾞｰﾀ情報は、
    117         *   "rows": [
    118         *     {"cols": [ "≪ﾃﾞｰﾀ1≫", "≪ﾃﾞｰﾀ2≫",…]},
    119         *     {"cols": [ "≪ﾃﾞｰﾀ1≫", "≪ﾃﾞｰﾀ2≫",…]},
    120         *   ]
    121         * 形式です。
    122         *
    123         * @param       table  DBTableModelｵﾌﾞｼﾞｪｸﾄ
    124         * @param       writer PrintWriterｵﾌﾞｼﾞｪｸﾄ
    125         */
    126        @Override
    127        protected void writeData( final DBTableModel table,final PrintWriter writer ) {
    128                final int numberOfRows =  table.getRowCount();
    129
    130                writer.println( "  \"rows\":[" );
    131
    132                final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE );   // \(ﾊﾞｯｸｽﾗｯｼｭ) と "(ﾀﾞﾌﾞﾙｸｵｰﾄ)のｴｽｹｰﾌﾟ 処理用
    133                for( int row=0; row<numberOfRows; row++ ) {
    134                        writer.print( "   {\"cols\":[" );
    135                        for( int i=0; i<numberOfColumns; i++ ) {
    136                                final int clm = clmNo[i];
    137                                if( clm < 0 ) { continue; }                     // ｶﾗﾑ飛ばし
    138
    139                                String val = table.getValue(row,clm);
    140                                // \(ﾊﾞｯｸｽﾗｯｼｭ) と "(ﾀﾞﾌﾞﾙｸｵｰﾄ)のｴｽｹｰﾌﾟ
    141                                if( val.contains( "\\" ) || val.contains( "\"" ) ) {
    142                                        buf.setLength(0);
    143                                        for( int j=0; j<val.length(); j++ ) {
    144                                                final char ch = val.charAt( j );
    145                                                if( ch == '\\' || ch == '\"' ) { buf.append( '\\' ); }
    146                                                buf.append( ch );
    147                                        }
    148                                        val = buf.toString();
    149                                }
    150
    151                                writer.print( '"' );
    152                                writer.print( val );
    153                                writer.print( "\"," );
    154                        }
    155                        writer.println( "]}," );
    156                }
    157
    158                writer.println( "  ]" );
    159        }
    160}