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.fukurou.transfer;
017
018import java.io.File;
019import java.io.PrintWriter;
020
021import org.opengion.fukurou.db.Transaction;
022import org.opengion.fukurou.util.FileUtil;
023import org.opengion.fukurou.util.HybsDateUtil;
024import org.opengion.fukurou.util.StringUtil;
025
026/**
027 * 伝送要求に対してのデータをファイルに書込みします。
028 * 但し、書き込まれるデータについては、旧伝送システムの形式と互換性を持たせるため、
029 * データの前30Byteに空白で埋め、さらに全体で標準では500Byteになるように行末にも空白埋めをします。
030 * 500byte以外にしたい場合は、書き込みパラメータの第2引数に整数で指定してください。
031 * 
032 * 先頭データに限っては、スペース以外で埋める事も可能です。
033 * その場合は実行対象の第2引数から順番に
034 * 状況コード(1byte)、データコード(1byte)、送り先(2byte)、テキスト種別(4byte) 送り元(2byte)
035 * を指定してください。
036 * 送り元を指定した場合はテキストの後に8byteスペース+送り元+26byteスペース+連番 を付加した上で
037 * 行末の空白埋めを行います(後ろ70byteという指定ではないので注意)
038 *
039 * 書込みするファイル名は、実行対象で指定します。ファイル名は絶対パスで指定して下さい。
040 * また、書込するテキストファイルのエンコードは書込パラメーターの第一引数で指定することができます。
041 * 指定しない場合、UTF-8が適用されます。
042 * 
043 * 出力したファイルを扱う場合に異なるOSで改行コードが問題となる場合がある場合は第三引数で指定します。
044 * 文字列でCRLFかLFを指定してください。標準はOSの改行コードです。
045 * 
046 * 
047 * 第一引数:エンコード(UTF-8)
048 * 第二引数:バイト数(負数で標準の500を利用) 
049 * 第三引数:末尾改行コード(標準はOS依存 CRLForLF設定可)
050 * 
051 * @og.rev 5.10.13.1 (2019/07/12) 改行コード追加
052 *
053 * @og.group 伝送システム
054 *
055 * @version  5.0
056 * @author   Hiroki.Nakamura
057 * @since    JDK1.6
058 */
059public class TransferExec_SAMCB implements TransferExec {
060        private final int fill = 500;
061        private final  String BR = System.getProperty("line.separator"); 
062
063
064        // 書込ファイルオブジェクト
065//      private File fileWrite = null;                  // 5.5.2.4 (2012/05/16) ローカル変数化
066
067        // 書込ファイルのエンコード
068//      private String fileEncode = null;               // 5.5.2.4 (2012/05/16) ローカル変数化
069
070        /**
071         * ファイルに書込みします。
072         *
073         * @param vals 伝送データ(配列)
074         * @param config 伝送設定オブジェクト
075         * @param tran トランザクションオブジェクト
076         *
077         * @og.rev 5.5.3.3 (2012/06/15) close処理
078         * @og.rev 5.8.1.1 (2014/11/14) パラメータで桁数指定可能にする
079         * @og.rev 5.8.1.2 (2014/11/21) 前方,後方のスペース埋めの箇所の互換性を上げる
080         * @og.rev 5.10.13.1 (2019/07/12) 改行コード指定と第二引数の標準設定
081         */
082        @Override
083        public void execute( final String[] vals, final TransferConfig config, final Transaction tran ) {
084                String fileEncode = "UTF-8";
085                String outBR = BR;
086                
087                // 5.8.1.2
088                String[] obj = StringUtil.csv2Array( config.getExecObj(), ' ' );
089//              File fileWrite = new File( config.getExecObj() );
090                File fileWrite = new File( obj[0] );
091                String preText = "";
092                String suffText = "";
093                preText +=  ( obj.length > 1 ) ? obj[1] : ""; // 状況コード
094                preText +=  ( obj.length > 2 ) ? obj[2] : ""; // データコード
095                preText +=  ( obj.length > 3 ) ? StringUtil.stringFill( obj[3], 8, fileEncode ) : ""; // ホストNO
096                preText +=  ( obj.length > 4 ) ? StringUtil.stringFill( obj[4], 4, fileEncode ) : ""; // テキスト種別
097                preText = StringUtil.stringFill( preText, 14, fileEncode );
098                if(obj.length > 1){
099                        preText += HybsDateUtil.getDate( "yyMMddHHmmss" );
100                        preText += HybsDateUtil.getDate( "MMdd" );
101                }
102                preText +=  ( obj.length > 4 ) ? StringUtil.stringFill( obj[4], 4, fileEncode ) : ""; // テキスト種別
103                
104                suffText +=  ( obj.length > 5 ) ? StringUtil.stringFill( "", 8, fileEncode ) + StringUtil.stringFill( obj[5], 28, fileEncode ) : ""; // 送り元
105                
106                // 5.8.1.1 (2014/11/14) fillByte追加
107                int fillByte = fill;
108                String execPrm = config.getExecPrm();
109                if( execPrm != null && execPrm.length() > 0 ){
110                        String[] prm = StringUtil.csv2Array( execPrm, ' ' );
111                        fileEncode =  prm[0];
112                        if( prm.length > 1 ) {
113                                fillByte = Integer.parseInt( prm[1] );
114                                if( fillByte < 0) { fillByte=fill; } // 5.10.13.1 (2019/07/12) 改行コード指定
115                        }
116                        // 5.10.13.1 (2019/07/12) 改行コード指定
117                        if( prm.length > 2 ) {
118                                switch (prm[2]) {
119                                        case "CRLF"     : outBR = "\r\n"; break;
120                                        case "LF"       : outBR = "\n"; break;
121                                        default         : outBR=BR;                                     
122                                }
123                        }
124                }
125
126//              String fileEncode = config.getExecPrm();
127//              if( fileEncode == null || fileEncode.length() == 0 ) {
128//                      fileEncode = "UTF-8";
129//              }
130
131                PrintWriter writer = FileUtil.getPrintWriter( fileWrite,fileEncode );
132                String line = null;
133                // 5.8.1.2 (2014/11/21) iが欲しいので書き方を変更する
134//              for( String s : vals ) {
135                for (int i = 0; i < vals.length; i++){
136                        // 前30Byteを空白埋め 
137                        // 5.8.1.2 (2014/11/21) 前方をスペース以外で埋められるようにする
138//                      String preSpace = StringUtil.stringFill( "", 30, fileEncode );
139                        String preSpace = StringUtil.stringFill( preText, 30, fileEncode );
140                        // 全体で500Byteになるように後ろに空白埋め
141                        // 5.8.1.1 500Byte以外も指定可能とする
142//                      line = StringUtil.stringFill( preSpace + s, 500, fileEncode );
143//                      line = StringUtil.stringFill( preSpace + s, fillByte, fileEncode );
144                        // 5.8.1.2 (2014/11/21)
145                        if(obj.length > 5){
146                                line = StringUtil.stringFill( preSpace + vals[i] + suffText + StringUtil.intFill( Integer.toString( i ), 14 ), fillByte, fileEncode );
147                        }
148                        else{
149                                line = StringUtil.stringFill( preSpace + vals[i] + suffText, fillByte, fileEncode );
150                        }
151                        
152//                      writer.println( line );
153                        writer.print( line + outBR ); // 5.10.13.1 (2019/07/12) 改行コード指定
154                }
155                writer.close(); // 5.5.3.3 (2012/06/15)
156        }
157}