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.StringUtil;
024
025/**
026 * 伝送要求に対してのデータをファイルに書込みします。
027 * 但し、書き込まれるデータについては、旧伝送システムの形式と互換性を持たせるため、
028 * データの前30Byteに空白で埋め、さらに全体で500Byteになるように行末にも空白埋めをします。
029 *
030 * 書込みするファイル名は、実行対象で指定します。ファイル名は絶対パスで指定して下さい。
031 * また、書込するテキストファイルのエンコードは書込パラメーターが指定することができます。
032 * 指定しない場合、システムリソースの"DB_ENCODE"で指定された値が適用されます。
033 *
034 * @og.group 伝送システム
035 *
036 * @version  5.0
037 * @author   Hiroki.Nakamura
038 * @since    JDK1.6
039 */
040public class TransferExec_SAMCB implements TransferExec {
041
042        /**
043         * ファイルに書込みします。
044         *
045         * @param vals 伝送データ(配列)
046         * @param config 伝送設定オブジェクト
047         * @param tran トランザクションオブジェクト
048         *
049         * @og.rev 5.5.3.3 (2012/06/15) close処理
050         */
051        @Override
052        public void execute( final String[] vals, final TransferConfig config, final Transaction tran ) {
053                // 書込ファイルオブジェクト
054                File fileWrite = new File( config.getExecObj() );
055
056                // 書込ファイルのエンコード
057                String fileEncode = config.getExecPrm();
058                if( fileEncode == null || fileEncode.length() == 0 ) {
059                        fileEncode = "UTF-8";
060                }
061
062                PrintWriter writer = FileUtil.getPrintWriter( fileWrite,fileEncode );
063                String line = null;
064                for( String s : vals ) {
065                        // 前30Byteを空白埋め
066                        String preSpace = StringUtil.stringFill( "", 30, fileEncode );
067                        // 全体で500Byteになるように後ろに空白埋め
068                        line = StringUtil.stringFill( preSpace + s, 500, fileEncode );
069                        writer.println( line );
070                }
071                writer.close();                         // 5.5.3.3 (2012/06/15)
072        }
073}