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.BufferedReader;
019import java.io.File;
020import java.util.ArrayList;
021import java.util.List;
022
023import org.opengion.fukurou.db.Transaction;
024import org.opengion.fukurou.util.ApplicationInfo;
025import org.opengion.fukurou.util.Closer;
026import org.opengion.fukurou.util.FileUtil;
027import org.opengion.fukurou.util.LogWriter;
028import org.opengion.fukurou.util.StringUtil;
029
030/**
031 * 伝送要求に対して、テキストファイルからデータを読取します。
032 * 但し、読取されるデータについては、旧伝送システムの形式と互換性を持たせるため、
033 * 31Byteから430Byteまでの400Byteを取得します。
034 *
035 * 読取するファイル名は、読取対象で指定します。
036 * ファイル名は絶対パスで指定する必要があります。
037 *
038 * 読込及びその後の実行処理が正常終了した場合は、読取ファイルは削除されます。
039 * 但し、読取パラメーターに"UNDEL"という文字を設定した場合は、正常終了した場合でも
040 * ファイルは削除されません。
041 *
042 * また、読取するテキストファイルのエンコードは読取パラメーターが指定することができます。
043 * 指定しない場合、システムリソースの"DB_ENCODE"で指定された値が適用されます。
044 *
045 * @og.group 伝送システム
046 *
047 * @version  5.0
048 * @author   Hiroki.Nakamura
049 * @since    JDK1.6
050 */
051public class TransferRead_SAMCB implements TransferRead {
052
053        // 読取ファイルオブジェクト
054        private String fileName = null;
055
056        // 読取ファイルのエンコード
057//      private String fileEncode = null;               // 5.5.2.4 (2012/05/16) ローカル変数化
058
059        // 完了時に読取ファイルを削除するかどうか
060        private boolean fileDel = true;
061
062        /**
063         * ファイルからデータを読み取ります。
064         *
065         * @param config 伝送設定オブジェクト
066         * @param tran トランザクションオブジェクト
067         *
068         * @return 読み取りしたデータ(配列)
069         */
070        @Override
071        public String[] read( final TransferConfig config, final Transaction tran ) {
072                fileName = config.getReadObj();
073                File fileRead = new File( fileName );
074                if( !fileRead.exists() ) { return new String[0]; }
075
076                String readPrm = config.getReadPrm();
077                if( readPrm != null && readPrm.indexOf( "UNDEL" ) >= 0 ) {
078                        fileDel = false;
079                        readPrm = readPrm.replace( "UNDEL", "" ).trim();
080                }
081                String fileEncode = readPrm;
082                if( fileEncode == null || fileEncode.length() == 0 ) {
083                        fileEncode = "UTF-8";
084                }
085
086                List<String> valList = new ArrayList<String>();
087                BufferedReader reader = FileUtil.getBufferedReader( fileRead, fileEncode );
088                String line = null;
089                try {
090                        while( ( line = reader.readLine() ) != null ) {
091                                line = StringUtil.stringFill( line, 500, fileEncode );
092                                byte[] bytes = StringUtil.makeByte( line, fileEncode );
093                                line = StringUtil.makeString( bytes, 30, 400, fileEncode );
094                                valList.add( line );
095                        }
096                }
097                catch( Throwable ex ) {
098                        LogWriter.log( ex );
099                        String errMsg = "ファイル読取時にエラーが発生しました。[LINE=" + line + "]";
100                        throw new RuntimeException( errMsg, ex );
101                }
102                finally {
103                        Closer.ioClose( reader );
104                }
105
106//              return valList.toArray( new String[0] );
107                return valList.toArray( new String[valList.size()] );
108        }
109
110        /**
111         * 更新(削除)対象のファイル名(配列)を返します。
112         *
113         * @return ファイル名(配列)
114         */
115        @Override
116        public String[] getKeys() {
117//              String[] rtn = { fileName };
118//              return rtn;
119                return new String[] { fileName };               // 5.5.2.4 (2012/05/16)
120        }
121
122        /**
123         * 更新(削除)対象のファイル名をセットします。
124         *
125         * @param keys ファイル名(配列)
126         */
127        @Override
128        public void setKeys( final String[] keys ) {
129                if( keys == null || keys.length == 0 ) { return; }
130                fileName = keys[0];
131        }
132
133        /**
134         * 読取したデータに対して完了処理を行います。
135         * 具体的には、読取したテキストファイルを削除します。
136         *
137         * @param config 伝送設定オブジェクト
138         * @param tran トランザクションオブジェクト
139         */
140        @Override
141        public void complete( final TransferConfig config, final Transaction tran ) {
142                if( !fileDel ) { return; }
143
144                File fileRead = new File( fileName );
145                if( !fileRead.exists() ) {
146                        return;
147//                      String errMsg = "ファイルが存在しません。[FILE=" + fileRead.getAbsolutePath() + "]";
148//                      throw new RuntimeException( errMsg );
149                }
150
151                boolean rtn = fileRead.delete();
152                if( !rtn ) {
153                        String errMsg = "ファイルの削除に失敗しました。[FILE=" + fileRead.getAbsolutePath() + "]";
154                        throw new RuntimeException( errMsg );
155                }
156        }
157
158        /**
159         * 読取したデータに対してエラー処理を行います。
160         * (ここでは何も処理しません)
161         *
162         * @param config 伝送設定オブジェクト
163         * @param appInfo DB接続情報
164         */
165        @Override
166        public void error( final TransferConfig config, final ApplicationInfo appInfo ) {
167        }
168}