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.util.ArrayList;
019    import java.util.List;
020    
021    import org.opengion.fukurou.db.DBUtil;
022    import org.opengion.fukurou.db.Transaction;
023    import org.opengion.fukurou.util.ApplicationInfo;
024    import org.opengion.fukurou.util.StringUtil;
025    
026    /**
027     * 伝é?要求に対して、旧伝é?DBのãƒ??タを読取しますã?
028     *
029     * 伝é?定義マスタの読取対象は、以下ã?形式で定義するå¿?¦�がありますã?
030     *   (ãƒ??タコーãƒ? (送りå…? (ãƒ?‚­スト種別)   ä¾?:"3 D9 B119"
031     * 処ç�?®Ÿ行後ã?、読み取ったã?ãƒ?ƒ€ーãƒ??タの状況を'2'に更新しますã?
032     * ä½?�—、読取パラメーターに"NOUPDATE"を指定したå?合ã?処ç�?¾Œã?更新は行われませんã€?
033     * またã?エラーが発生したå?合ã?ヘッãƒ??ãƒ??タの状況を'9'に更新しますã?
034     *
035     * @og.group 伝é?シスãƒ?ƒ 
036     *
037     * @version  5.0
038     * @author   Hiroki.Nakamura
039     * @since    JDK1.6
040     */
041    public class TransferRead_CB01 implements TransferRead {
042    
043            // 更新対象の通番NO(配å?)
044            private String[] htcnoArr = null;
045    
046            /**
047             * 伝é?ãƒ??タを読み取りますã?
048             *
049             * @param config 伝é?設定オブジェクãƒ?
050             * @param tran トランザクションオブジェクãƒ?
051             *
052             * @return 読み取りしたãƒ??タ(配å?)
053             * @see #getKeys()
054             */
055            @Override
056            public String[] read( final TransferConfig config, final Transaction tran ) {
057                    htcnoArr = getHtcno( config, tran );
058                    return getData( htcnoArr, tran );
059            }
060    
061            /**
062             * 旧伝é?DBを検索しã?対象の通番NO(配å?)を返しますã?
063             *
064             * @param config 伝é?設定オブジェクãƒ?
065             * @param tran トランザクションオブジェクãƒ?
066             *
067             * @return 通番NO(配å?)
068             */
069            private String[] getHtcno( final TransferConfig config, final Transaction tran ) {
070                    String readObj = config.getReadObj();
071                    String[] obj = StringUtil.csv2Array( readObj, ' ' );
072                    if( obj.length < 3 ) {
073                            String errMsg = "読取対象はã€?ãƒ??タコーãƒ? (送りå…? (ãƒ?‚­スト種別) の形式でæŒ?®šして下さã�??[READOBJ=" + readObj + "]";
074                            throw new RuntimeException( errMsg );
075                    }
076                    String hcdd = obj[0];
077                    String hto = obj[1];
078                    String hsyu = obj[2];
079                    if( hcdd == null || hcdd.length() == 0
080                     || hto  == null || hto.length()  == 0
081                     || hsyu == null || hsyu.length() == 0 ) {
082                            String errMsg = "読取対象はã€?ãƒ??タコーãƒ? (送りå…? (ãƒ?‚­スト種別) はå¿??ですã?[READOBJ=" + readObj + "]";
083                            throw new RuntimeException( errMsg );
084                    }
085    
086                    StringBuilder buf = new StringBuilder();
087                    buf.append( "SELECT A.HTCNO" );
088                    buf.append( " FROM CB01 A" );
089                    buf.append( " WHERE A.HCDD = '" + hcdd + "'" );
090                    buf.append( " AND A.HTO = '" + hto + "'" );
091                    buf.append( " AND A.HSYU = '" + hsyu + "'" );
092                    buf.append( " AND A.HCDJ = '1'" );
093                    buf.append( " ORDER BY A.HTC" );
094    
095                    String[][] vals = DBUtil.dbExecute( buf.toString(),null,tran );
096                    List<String> hnoList = new ArrayList<String>();
097                    if( vals != null && vals.length > 0 ) {
098                            for( int row=0; row<vals.length; row++ ) {
099                                    hnoList.add( vals[row][0] );
100                            }
101                    }
102    
103    //              return hnoList.toArray( new String[0] );
104                    return hnoList.toArray( new String[hnoList.size()] );
105            }
106    
107            /**
108             * 対象の通番NOに対してCB01を読み込みãƒ??タをé?列で返しますã?
109             *
110             * @param htcnoArr 読取対象の通番NO(配å?)
111             * @param tran トランザクション
112             *
113             * @return ãƒ??タ(配å?)
114             */
115            private String[] getData( final String[] htcnoArr, final Transaction tran ) {
116                    if( htcnoArr == null || htcnoArr.length == 0 ) { return new String[0]; }
117    
118                    String htcnos = StringUtil.array2csv( htcnoArr );
119                    StringBuilder buf = new StringBuilder();
120                    buf.append( "SELECT A.HTEXT" );
121                    buf.append( " FROM CB01 A" );
122                    buf.append( " WHERE A.HCDJ = '5'" );
123                    buf.append( " AND A.HTCNO IN (" );
124                    buf.append( htcnos );
125                    buf.append( ") ORDER BY A.HTC, A.HTCNO" );
126    
127                    String[][] vals = DBUtil.dbExecute( buf.toString(),null,tran );
128                    String[] rtn = new String[vals.length];
129                    for( int i=0; i<vals.length; i++ ) {
130                            rtn[i] = vals[i][0];
131                    }
132                    return rtn;
133            }
134    
135            /**
136             * 更新対象の通番NO(配å?)を返しますã?
137             *
138             * @og.rev 5.5.2.4 (2012/05/16) 配å?を返す場合ã?、å?部表現を暴露しなã�?‚ˆã�?�«、clone を返しますã?
139             *
140             * @return 通番NO(配å?)
141             */
142            @Override
143            public String[] getKeys() {
144    //              return htcnoArr;
145                    String[] rtn = null ;
146                    if( htcnoArr != null ) { rtn = htcnoArr.clone(); }
147                    return rtn ;
148            }
149    
150            /**
151             * 更新対象の通番NO(配å?)をセãƒ?ƒˆしますã?
152             *
153             * @og.rev 5.5.2.4 (2012/05/16) 参ç?の格納には、System.arraycopy を使ã�?�¾すã?
154             *
155             * @param keys 通番NO(配å?)
156             */
157            @Override
158            public void setKeys( final String[] keys ) {
159    //              htcnoArr = keys;
160                    if( keys != null ) {
161                            int size = keys.length ;
162                            htcnoArr = new String[size];
163                            System.arraycopy( keys,0,htcnoArr,0,size );
164                    }
165                    else {
166                            htcnoArr = null;
167                    }
168            }
169    
170            /**
171             * 読取した伝é?ãƒ??タのヘッãƒ??ãƒ??タの状況を'2'(抜å?済み)に更新しますã?
172             * 更新対象の通番NOにつã�?�¦は、{@link #setKeys(String[])}で外部からセãƒ?ƒˆすることもできますã?
173             *
174             * @param config 伝é?設定オブジェクãƒ?
175             * @param tran トランザクションオブジェクãƒ?
176             * @see #setKeys(String[])
177             */
178            @Override
179            public void complete( final TransferConfig config, final Transaction tran ) {
180                    if( htcnoArr == null || htcnoArr.length == 0 ) { return; }
181                    // 読取パラメーターに"NOUPDATE"が指定されてã�?‚‹場合ã?、CB01の状況を更新しなã�?
182                    if( "NOUPDATE".equalsIgnoreCase( config.getReadPrm() ) ) { return; }
183    
184                    String htcnos = StringUtil.array2csv( htcnoArr );
185                    StringBuilder buf = new StringBuilder();
186                    buf.append( "UPDATE CB01 A" );
187                    buf.append( " SET A.HCDJ = '2'" );
188                    buf.append( " WHERE A.HCDJ = '1'" );
189                    buf.append( " AND A.HTCNO IN (" );
190                    buf.append( htcnos );
191                    buf.append( ")" );
192    
193                    DBUtil.dbExecute( buf.toString(),null,tran );
194            }
195    
196            /**
197             * 読取した伝é?ãƒ??タのヘッãƒ??ãƒ??タの状況を'9'(エラー)に更新しますã?
198             * 更新対象の通番NOにつã�?�¦は、{@link #setKeys(String[])}で外部からセãƒ?ƒˆすることもできますã?
199             *
200             * @param config 伝é?設定オブジェクãƒ?
201             * @param appInfo DB接続情報
202             * @see #setKeys(String[])
203             */
204            @Override
205            public void error( final TransferConfig config, final ApplicationInfo appInfo ) {
206                    if( htcnoArr == null || htcnoArr.length == 0 ) { return; }
207    
208                    String htcnos = StringUtil.array2csv( htcnoArr );
209                    StringBuilder buf = new StringBuilder();
210                    buf.append( "UPDATE CB01 A" );
211                    buf.append( " SET A.HCDJ = '9'" );
212                    buf.append( " WHERE A.HCDJ in ('1','2')" ); // 既に実行PGで抜å?済みに更新されてã�?‚‹可能性があã‚?
213                    buf.append( " AND A.HTCNO IN (" );
214                    buf.append( htcnos );
215                    buf.append( ")" );
216    
217                    DBUtil.dbExecute( buf.toString(),null,appInfo ); // エラー更新はトランザクションをå?けて処ç�?�™ã‚?
218            }
219    
220    }