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.json;
017
018
019import java.util.ArrayList;
020import java.util.Map;
021
022import org.opengion.fukurou.util.StringUtil;
023import org.opengion.hayabusa.common.HybsSystemException;
024import org.opengion.hayabusa.db.DBTableModelUtil;
025import org.opengion.hayabusa.html.AbstractJsonReader;
026
027
028/**
029 * JSONからテーブルモデルへの変換を行う標準クラスです。
030 * エンジンのVIEWタイプJSONから出力したデータ形式を入力とします。
031 * 
032 * http://qiita.com/kenichi_nakamura/items/be73e37ec870e5845ed9
033 * https://www.mkyong.com/java/how-to-convert-java-map-to-from-json-jackson/
034 *
035 * @og.group 画面表示
036 * @og.rev 5.9.21.0 (2017/06/02)
037 *
038 * @version  5.0
039 * @author       Takahashi Masakazu
040 * @since    JDK7.0,
041 */
042public class JsonReader_Default extends AbstractJsonReader {
043        //* このプログラムのVERSION文字列を設定します。   {@value} */
044        private static final String VERSION = "5.9.21.0 (2017/06/02)" ;
045        
046        private String datakey = "DATA"; // データ部分を示すキー名
047
048        /**
049         * デフォルトコンストラクター
050         *
051         */
052        public JsonReader_Default() {
053        }
054
055        /**
056         * JSONデータをDBTableModelに変換します。
057         * 
058         * このdefaultクラスではエンジンのViewForm_JSON形式に対応します。
059         * エンジンでの出力は以下のような形となっていますが、汎用性を上げるためにHEAD,INFOは利用せずにDATAだけを見るようにします。
060         * 
061         * { "DATA":[{"カラム名":"値(1行目)",...},{"カラム名":"値(2行目)",...}]
062         *       ,"HEAD":{"カラム名":"ラベル",...}
063         *       ,"INFO":{{"COUNT":"件数","COLUMN":"出力カラム名(カンマ区切り)"} }
064         *      }
065         *
066         *
067         * @param   json JSON
068         */
069        @Override
070        public void readDBTable( final String json ) {
071                String[] names = null;
072                String[] readData = null;
073                int numberOfRows = 0;
074
075                table = DBTableModelUtil.newDBTable();
076                
077                int skip = getSkipRowCount();
078                
079                Map<String,Object> jsonmap = parse( json );
080                
081                // datakeyで名称を変更できるようにしておく。
082                // DATAは配列なのでListで返ってくる
083                // これが行となる。
084                ArrayList<Map<String,Object>> dataList = (ArrayList<Map<String,Object>>)jsonmap.get( datakey );
085                
086                for(Map<String,Object> mp : dataList){
087            // 最初にカラム名をセットする
088                        if( names == null){
089                                names = mp.keySet().toArray( new String[mp.size()] );
090                                setTableDBColumn( names ) ;
091            }
092                        
093                        if( skip > 0 ) { skip--; continue; }
094                        
095                        // 行データを追加
096                        if( numberOfRows < getMaxRowCount() ) {
097                                readData = readData( mp, names );
098                                setTableColumnValues( readData );
099                                numberOfRows ++ ;
100                        }
101                        else {
102                                table.setOverflow( true );
103                        }
104        }
105                
106                if( names == null ) {
107                        String errMsg = "有効なデータが見つかりませんでした。";
108                        throw new HybsSystemException( errMsg );
109                }
110        }
111        
112        
113        /**
114         * テーブルに登録する配列のJSONキー値を設定します。
115         * 
116         * { "DATA":[{"カラム名":"値(1行目)", ...},{"カラム名":"値(2行目)",...}] }
117         * 通常、上記のDATAをキー値としてテーブルに変換します。(エンジン標準出力)
118         * エンジン以外が同じ用な形式の場合に、クラスを新規作成しなくても取り込めるようにするためキー値を外から設定可能にしておきます。
119         *
120         * @param strings dataKey設定
121         */
122        public void setOptions ( String... strings ){
123                datakey = StringUtil.nval(strings[0],datakey);
124        }
125}