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.util.ArrayList;
019import java.util.List;
020
021import org.opengion.fukurou.db.DBUtil;
022import org.opengion.fukurou.db.Transaction;
023import org.opengion.fukurou.util.ApplicationInfo;
024import 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 */
041public 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}