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.util.ArrayList;
020import java.util.List;
021
022import org.opengion.fukurou.db.Transaction;
023import org.opengion.fukurou.util.ApplicationInfo;
024import org.opengion.fukurou.util.FileUtil;
025
026/**
027 * 伝送要求に対して、ファイルまたはディレクトリをスキャンし、それに含まれる
028 * ファイルの絶対パスのリストを取得します。
029 *
030 * 伝送定義マスタの読取対象は、スキャン対象のファイルまたはディレクトリを指定します。
031 * 処理実行後は、正常終了した場合は、スキャンしたファイルを削除します。
032 * 但し、読取パラメーターに"NODEL"を指定した場合、ファイルの削除は行われません。
033 * また、エラーが発生した場合は読取パラメーターの設定に関わらずファイルの削除は
034 * 行われません。
035 *
036 * 読取対象にディレクトリを指定した場合は、再起的にサブフォルダもスキャンされます。
037 *
038 * @og.group 伝送システム
039 *
040 * @version  5.0
041 * @author   Hiroki.Nakamura
042 * @since    JDK1.6
043 */
044public class TransferRead_FILELIST implements TransferRead {
045
046        // 更新(削除)対象のファイル名(配列)
047        private String[] fileNames = null;
048
049        /**
050         * ファイルまたはディレクトリをスキャンしファイルの絶対パスのリストを取得します。
051         *
052         * @og.rev 5.4.3.2 (2011/12/06) コピー中のファイル判定追加
053         * @og.rev 5.5.2.4 (2012/05/16) 配列を返す場合は、内部表現を暴露しないように、clone を返します。
054         *
055         * @param config 伝送設定オブジェクト
056         * @param tran トランザクションオブジェクト
057         *
058         * @return ファイル一覧(配列)
059         */
060        @Override
061        public String[] read( final TransferConfig config, final Transaction tran ) {
062                File file = new File( config.getReadObj() );
063                if( !file.exists() ) {
064                        String errMsg = "スキャン対象のファイル/ディレクトリ。[FILE=" + file.getAbsolutePath() + "]";
065                        throw new RuntimeException( errMsg );
066                }
067
068                List<String> list = new ArrayList<String>();
069                FileUtil.getFileList( file, false, list , false); // 5.4.3.2 コピー判定追加
070
071//              fileNames = list.toArray( new String[0] );
072                fileNames = list.toArray( new String[list.size()] );
073
074//              return fileNames;
075                return fileNames.clone();
076        }
077
078        /**
079         * 更新(削除)対象のファイル名(配列)を返します。
080         *
081         * @og.rev 5.5.2.4 (2012/05/16) 配列を返す場合は、内部表現を暴露しないように、clone を返します。
082         *
083         * @return ファイル名(配列)
084         */
085        public String[] getKeys() {
086//              return fileNames;
087                String[] rtn = null ;
088                if( fileNames != null ) { rtn = fileNames.clone(); }
089                return rtn ;
090        }
091
092        /**
093         * 更新(削除)対象のファイル名(配列)をセットします。
094         *
095         * @og.rev 5.5.2.4 (2012/05/16) 参照の格納には、System.arraycopy を使います。
096         *
097         * @param keys ファイル名(配列)
098         */
099        public void setKeys( final String[] keys ) {
100//              fileNames = keys;
101                if( keys != null ) {
102                        int size = keys.length ;
103                        fileNames = new String[size];
104                        System.arraycopy( keys,0,fileNames,0,size );
105                }
106                else {
107                        fileNames = null;
108                }
109        }
110
111        /**
112         * 読取した伝送データのヘッダーデータの状況を'2'(抜出済み)に更新します。
113         * 更新対象の通番NOについては、{@link #setKeys(String[])}で外部からセットすることもできます。
114         *
115         * @param config 伝送設定オブジェクト
116         * @param tran トランザクションオブジェクト
117         * @see #setKeys(String[])
118         */
119        @Override
120        public void complete( final TransferConfig config, final Transaction tran ) {
121                if( fileNames == null || fileNames.length == 0 ) { return; }
122                // 読取パラメーターに"NODEL"が指定されている場合は、スキャンしたファイルを削除しない。
123                if( "NODEL".equalsIgnoreCase( config.getReadPrm() ) ) { return; }
124
125                for( String fileName : fileNames ) {
126                        File file = new File( fileName );
127                        if( !file.exists() ) {
128                                String errMsg = "ファイルが存在しません。[FILE=" + file.getAbsolutePath() + "]";
129                                throw new RuntimeException( errMsg );
130                        }
131
132                        boolean rtn = file.delete();
133                        if( !rtn ) {
134                                String errMsg = "ファイルの削除に失敗しました。[FILE=" + file.getAbsolutePath() + "]";
135                                throw new RuntimeException( errMsg );
136                        }
137                }
138        }
139
140        /**
141         * (ここでは何もしません)
142         *
143         * @param config 伝送設定オブジェクト
144         * @param appInfo DB接続情報
145         */
146        @Override
147        public void error( final TransferConfig config, final ApplicationInfo appInfo ) {
148        }
149}