001/*
002 * Copyright (c) 2017 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.fukurou.fileexec;
017
018import java.nio.file.Path;
019import java.util.List ;                                                                                 // 
020import java.util.ArrayList ;                                                                    // 
021import java.util.Arrays ;                                                                               // 
022
023import static org.opengion.fukurou.fileexec.AppliExec.GE72.*;           // enum のショートカット
024
025/**
026 * RunExec_DBIN は、RunExec インターフェースの実装クラスで、ファイルをデータベースに登録します。
027 *
028 *<pre>
029 *
030 * GE72.RUNTYPEが、'1' の場合の処理を行います。
031 *
032 *</pre>
033 *
034 * @og.rev 7.0.0.0 (2017/07/07) 新規作成
035 *
036 * @version  7.0
037 * @author   Kazuhiko Hasegawa
038 * @since    JDK1.8,
039 */
040public class RunExec_DBIN implements RunExec {
041        private static final XLogger LOGGER= XLogger.getLogger( RunExec_DBIN.class.getName() );         // ログ出力
042
043        private static final String DEF_ENCODE = "Windows-31J" ;
044
045        /**
046         * デフォルトコンストラクター
047         *
048         * @og.rev 6.9.7.0 (2018/05/14) PMD Each class should declare at least one constructor
049         */
050        public RunExec_DBIN() { super(); }              // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
051
052        /**
053         * 実際に処理を実行するプログラムのメソッド。
054         *
055         * @og.rev 6.8.1.5 (2017/09/08) LOGGER.debug 情報の追加
056         * @og.rev 6.9.7.0 (2018/05/14) PMD encode,clms72,skipCnt unreferenced before a possible exit point.
057         *
058         * @param       path 処理するファイルパス
059         * @param       ge72Data GE72 テーブルデータ
060         * @return      処理件数(正は成功、マイナスは異常時の行番号)
061         */
062        public int exec( final Path path , final String[] ge72Data ) {
063                LOGGER.debug( () -> "⑧ Path=" + path + " , GE72Data=" + Arrays.toString( ge72Data ) );
064
065                // 6.9.7.0 (2018/05/14) PMD encode,clms72,skipCnt unreferenced before a possible exit point.
066//              final String encode     = StringUtil.nval( ge72Data[FILE_ENC.NO] , DEF_ENCODE );        // UTF-8 , Windows-31J;
067                final String table      = ge72Data[TABLE_NAME.NO];
068//              final String clms72     = ge72Data[CLMS.NO];                    // CLMS (#NAMEの設定)
069//              final String params     = ge72Data[PARAMS.NO];                  // 固定値の設定
070//              final int    skipCnt= StringUtil.nval( ge72Data[SKIP_CNT.NO] , 0 );
071
072                if( table == null || table.isEmpty() ) {
073                        // MSG2003 = DBINでは、tableは、必須です。
074                        throw MsgUtil.throwException( "MSG2003" );
075                }
076
077                final String encode     = StringUtil.nval( ge72Data[FILE_ENC.NO] , DEF_ENCODE );        // UTF-8 , Windows-31J;
078                final String clms72     = ge72Data[CLMS.NO];                    // CLMS (#NAMEの設定)
079
080                // 一旦すべてのデータを読み取ります。よって、大きなファイルには向きません。
081                final List<List<String>> dataList = new ArrayList<>();          // ファイルを読み取った行データごとの分割されたデータ
082                final LineSplitter split = new LineSplitter( encode , clms72 );
083                split.forEach( path , line -> dataList.add( line ) );           // 1行ごとに、カラムを分割されたListオブジェクト
084
085                final String[] clms = split.getColumns();                                       // ファイルの#NAME から、カラム列を取り出します。
086                if( clms == null || clms.length == 0 ) {
087                        // MSG2004 = DBINでは、カラム列は、必須です。
088                        throw MsgUtil.throwException( "MSG2004" );
089                }
090
091                final String INS_QUERY = DBUtil.getInsertSQL( table,clms,null,null );
092
093                final int skipCnt = StringUtil.nval( ge72Data[SKIP_CNT.NO] , 0 );
094                final List<String[]> dbData = new ArrayList<>();
095                if( !dataList.isEmpty() ) {
096                        for( int i=skipCnt; i<dataList.size(); i++ ) {                  // skipCntの行から取り込む
097                                final List<String> line = dataList.get(i);
098                                dbData.add( line.toArray( new String[line.size()] ) );
099                        }
100                }
101
102                return DBUtil.execute( INS_QUERY , dbData );
103        }
104}