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.BufferedReader;
019    import java.io.File;
020    import java.util.ArrayList;
021    import java.util.List;
022    
023    import org.opengion.fukurou.db.Transaction;
024    import org.opengion.fukurou.util.ApplicationInfo;
025    import org.opengion.fukurou.util.Closer;
026    import org.opengion.fukurou.util.FileUtil;
027    import org.opengion.fukurou.util.LogWriter;
028    import 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     */
051    public 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    }