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     */
016    package org.opengion.fukurou.transfer;
017    
018    import java.io.File;
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    import org.opengion.fukurou.db.Transaction;
023    import org.opengion.fukurou.util.ApplicationInfo;
024    import 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     */
044    public 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    }