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 * @og.group 伝送システム
044 *
045 * @version  5.0
046 * @author   Hiroki.Nakamura
047 * @since    JDK1.6
048 */
049public class TransferExec_SAMCB implements TransferExec {
050
051        // 書込ファイルオブジェクト
052//      private File fileWrite = null;                  // 5.5.2.4 (2012/05/16) ローカル変数化
053
054        // 書込ファイルのエンコード
055//      private String fileEncode = null;               // 5.5.2.4 (2012/05/16) ローカル変数化
056
057        /**
058         * ファイルに書込みします。
059         *
060         * @param vals 伝送データ(配列)
061         * @param config 伝送設定オブジェクト
062         * @param tran トランザクションオブジェクト
063         *
064         * @og.rev 5.5.3.3 (2012/06/15) close処理
065         * @og.rev 5.8.1.1 (2014/11/14) パラメータで桁数指定可能にする
066         * @og.rev 5.8.1.2 (2014/11/21) 前方,後方のスペース埋めの箇所の互換性を上げる
067         */
068        @Override
069        public void execute( final String[] vals, final TransferConfig config, final Transaction tran ) {
070                String fileEncode = "UTF-8";
071                
072                // 5.8.1.2
073                String[] obj = StringUtil.csv2Array( config.getExecObj(), ' ' );
074//              File fileWrite = new File( config.getExecObj() );
075                File fileWrite = new File( obj[0] );
076                String preText = "";
077                String suffText = "";
078                preText +=  ( obj.length > 1 ) ? obj[1] : ""; // 状況コード
079                preText +=  ( obj.length > 2 ) ? obj[2] : ""; // データコード
080                preText +=  ( obj.length > 3 ) ? StringUtil.stringFill( obj[3], 8, fileEncode ) : ""; // ホストNO
081                preText +=  ( obj.length > 4 ) ? StringUtil.stringFill( obj[4], 4, fileEncode ) : ""; // テキスト種別
082                preText = StringUtil.stringFill( preText, 14, fileEncode );
083                if(obj.length > 1){
084                        preText += HybsDateUtil.getDate( "yyMMddHHmmss" );
085                        preText += HybsDateUtil.getDate( "MMdd" );
086                }
087                preText +=  ( obj.length > 4 ) ? StringUtil.stringFill( obj[4], 4, fileEncode ) : ""; // テキスト種別
088                
089                suffText +=  ( obj.length > 5 ) ? StringUtil.stringFill( "", 8, fileEncode ) + StringUtil.stringFill( obj[5], 28, fileEncode ) : ""; // 送り元
090                
091                // 5.8.1.1 (2014/11/14) fillByte追加
092                int fillByte = 500;
093                String execPrm = config.getExecPrm();
094                if( execPrm != null && execPrm.length() > 0 ){
095                        String[] prm = StringUtil.csv2Array( execPrm, ' ' );
096                        fileEncode =  prm[0];
097                        if( prm.length > 1 ) {
098                                fillByte = Integer.parseInt( prm[1] );
099                        }
100                }
101
102//              String fileEncode = config.getExecPrm();
103//              if( fileEncode == null || fileEncode.length() == 0 ) {
104//                      fileEncode = "UTF-8";
105//              }
106
107                PrintWriter writer = FileUtil.getPrintWriter( fileWrite,fileEncode );
108                String line = null;
109                // 5.8.1.2 (2014/11/21) iが欲しいので書き方を変更する
110//              for( String s : vals ) {
111                for (int i = 0; i < vals.length; i++){
112                        // 前30Byteを空白埋め 
113                        // 5.8.1.2 (2014/11/21) 前方をスペース以外で埋められるようにする
114//                      String preSpace = StringUtil.stringFill( "", 30, fileEncode );
115                        String preSpace = StringUtil.stringFill( preText, 30, fileEncode );
116                        // 全体で500Byteになるように後ろに空白埋め
117                        // 5.8.1.1 500Byte以外も指定可能とする
118//                      line = StringUtil.stringFill( preSpace + s, 500, fileEncode );
119//                      line = StringUtil.stringFill( preSpace + s, fillByte, fileEncode );
120                        // 5.8.1.2 (2014/11/21)
121                        if(obj.length > 5){
122                                line = StringUtil.stringFill( preSpace + vals[i] + suffText + StringUtil.intFill( Integer.toString( i ), 14 ), fillByte, fileEncode );
123                        }
124                        else{
125                                line = StringUtil.stringFill( preSpace + vals[i] + suffText, fillByte, fileEncode );
126                        }
127                        
128                        writer.println( line );
129                }
130                writer.close(); // 5.5.3.3 (2012/06/15)
131        }
132}