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    }