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.8.1.5 (2017/09/08) LOGGER.debug 情報の追加
049         *
050         * @param       path 処理するファイルパス
051         * @param       ge72Data GE72 テーブルデータ
052         * @return      処理件数(正は成功、マイナスは異常時の行番号)
053         */
054        public int exec( final Path path , final String[] ge72Data ) {
055                LOGGER.debug( () -> "⑧ Path=" + path + " , GE72Data=" + Arrays.toString( ge72Data ) );
056
057                final String encode     = StringUtil.nval( ge72Data[FILE_ENC.NO] , DEF_ENCODE );        // UTF-8 , Windows-31J;
058                final String table      = ge72Data[TABLE_NAME.NO];
059                final String clms72     = ge72Data[CLMS.NO];                    // CLMS (#NAMEの設定)
060                final String params     = ge72Data[PARAMS.NO];                  // 固定値の設定
061                final int    skipCnt= StringUtil.nval( ge72Data[SKIP_CNT.NO] , 0 );
062
063                if( table == null || table.isEmpty() ) {
064                        // MSG2003 = DBINでは、tableは、必須です。
065                        throw MsgUtil.throwException( "MSG2003" );
066                }
067
068                // 一旦すべてのデータを読み取ります。よって、大きなファイルには向きません。
069                final List<List<String>> dataList = new ArrayList<>();          // ファイルを読み取った行データごとの分割されたデータ
070                final LineSplitter split = new LineSplitter( encode , clms72 );
071                split.forEach( path , line -> dataList.add( line ) );           // 1行ごとに、カラムを分割されたListオブジェクト
072
073                final String[] clms = split.getColumns();                                       // ファイルの#NAME から、カラム列を取り出します。
074                if( clms == null || clms.length == 0 ) {
075                        // MSG2004 = DBINでは、カラム列は、必須です。
076                        throw MsgUtil.throwException( "MSG2004" );
077                }
078
079                final String INS_QUERY = DBUtil.getInsertSQL( table,clms,null,null );
080
081                final List<String[]> dbData = new ArrayList<>();
082                if( !dataList.isEmpty() ) {
083                        for( int i=skipCnt; i<dataList.size(); i++ ) {                  // skipCntの行から取り込む
084                                final List<String> line = dataList.get(i);
085                                dbData.add( line.toArray( new String[line.size()] ) );
086                        }
087                }
088
089                return DBUtil.execute( INS_QUERY , dbData );
090        }
091}