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.hayabusa.remote;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021
022import org.opengion.fukurou.db.Transaction;
023import org.opengion.fukurou.db.TransactionReal;
024import org.opengion.fukurou.transfer.TransferConfig;
025import org.opengion.fukurou.transfer.TransferRead;
026import org.opengion.fukurou.util.ApplicationInfo;
027import org.opengion.fukurou.util.StringUtil;
028import org.opengion.hayabusa.common.HybsSystem;
029import org.opengion.hayabusa.common.HybsSystemException;
030
031/**
032 * RemoteControllableインタフェイスを実装した
033 * サーブレット経由で遠隔伝送読取処理を行うためのクラスです。
034 *
035 * このクラスは、伝送読取処理のラッパークラスです。
036 * 引数のKBREADのパラメーターに基づき、伝送読取オブジェクトを生成し、伝送処理を実行します。
037 * 詳細については、{@link org.opengion.fukurou.transfer.TransferRead_HTTP}を参照して下さい。
038 *
039 * @og.rev 5.4.2.0 (2011/12/01) 新規作成
040 *
041 * @version  4.1
042 * @author   Hiroki Nakamura
043 * @since    JDK6.0,
044 *
045 */
046public class TransferReadWrapper implements RemoteControllable {
047
048        // 伝送読取クラスのベースクラス名
049        private static final String READ_CLASS_BASE = "org.opengion.fukurou.transfer.TransferRead_" ;
050
051        // コネクションにアプリケーション情報を追記するかどうか指定
052        private static final boolean USE_DB_APPLICATION_INFO  = HybsSystem.sysBool( "USE_DB_APPLICATION_INFO" ) ;
053
054        private static final ApplicationInfo appInfo;
055
056        static {
057                if( USE_DB_APPLICATION_INFO ) {
058                        appInfo = new ApplicationInfo();
059                        // ユーザーID,IPアドレス,ホスト名
060                        appInfo.setClientInfo( "TransferReadWrapper",HybsSystem.HOST_ADRS,HybsSystem.HOST_NAME );
061                        // 画面ID,操作,プログラムID
062                        appInfo.setModuleInfo( "TransferReadWrapper","TransferReadWrapper","TransferReadWrapper" );
063                }
064                else {
065                        appInfo = null;
066                }
067        }
068        /**
069         * RemoteControllableインタフェイスの実装メソッドです。
070         *
071         * @og.rev 5.7.1.2 (2013/12/20) msg ⇒ errMsg 変更
072         *
073         * @param       valMap   サーブレットが受け取ったキーと値のマップ
074         *
075         * @return      XML形式の実行結果
076         */
077        @Override
078        public String remoteControl( final Map<String,String> valMap ) {
079                // パラメーターより伝送設定オブジェクトを生成します。
080                TransferConfig conf = new TransferConfig(
081                                valMap.get( "KBREAD" )
082                                , valMap.get( "READOBJ" )
083                                , valMap.get( "READPRM" )
084                                , valMap.get( "KBEXEC" )
085                                , valMap.get( "EXECDBID" )
086                                , valMap.get( "EXECOBJ" )
087                                , valMap.get( "EXECPRM" )
088                                , valMap.get( "ERROR_SENDTO" )
089                                , valMap.get( "HFROM" )
090                                , null, -1 );
091                Transaction tran = null;
092                String rtn = null;
093                try {
094                        tran = new TransactionReal( appInfo );
095                        TransferRead read = (TransferRead)StringUtil.newInstance( READ_CLASS_BASE + valMap.get( "KBREAD" ) );
096
097                        // データ読取
098                        String type = valMap.get( "type" );
099                        if( "read".equals( type ) ) {
100                                String[] data = read.read( conf, tran );
101                                // 完了/エラー処理のために更新キーを取得しXMLに埋め込む
102                                String[] keys = read.getKeys();
103                                rtn = makeXml( data, keys );
104                        }
105                        // 完了処理
106                        else if( "complete".equals( type ) ) {
107                                // パラメーターから更新キーを読み取る
108                                String[] keys = getKeys( valMap );
109                                read.setKeys( keys );
110                                read.complete( conf, tran );
111                        }
112                        // エラー処理
113                        else if( "error".equals( type ) ) {
114                                // パラメーターから更新キーを読み取る
115                                String[] keys = getKeys( valMap );
116                                read.setKeys( keys );
117                                read.error( conf, appInfo );
118                        }
119                        else {
120                                String errMsg = "処理タイプが不正です。[指定可能タイプ=read,complete,error][指定されたタイプ=" + type + "]";
121                                throw new HybsSystemException( errMsg );
122                        }
123                }
124                catch ( Throwable ex ) {
125//                      String msg = "伝送読取処理(HTTP経由)でエラーが発生しました。";
126//                      throw new HybsSystemException( msg, ex );
127                        String errMsg = "伝送読取処理(HTTP経由)でエラーが発生しました。";
128                        throw new HybsSystemException( errMsg, ex );    // 5.7.1.2 (2013/12/20) msg ⇒ errMsg 変更
129                }
130                finally {
131                        if( tran != null ) { tran.close(); }
132                }
133
134                return rtn;
135        }
136
137        /**
138         * 伝送読取処理の結果からデータ一覧及びキー一覧からXMLデータを生成します。
139         *
140         * @og.rev 5.4.4.5 (2012/02/20) 特定文字をエスケープする
141         * @param data データ一覧(配列)
142         * @param key 更新時に使用するキー一覧(配列)
143         *
144         * @return XMLデータ
145         */
146        private String makeXml( final String[] data, final String[] key ) {
147                StringBuilder buf = new StringBuilder();
148                buf.append( "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" );
149                buf.append( "<root>" );
150                buf.append( " <dataList>" );
151                if( data != null ) {
152                        for( String d : data ) {
153//                              buf.append( "  <data>" ).append( d ).append( "</data>" );
154                                buf.append( "  <data>" ).append( StringUtil.htmlFilter(d) ).append( "</data>" );
155                        }
156                }
157                buf.append( " </dataList>" );
158                buf.append( " <keyList>" );
159                if( key != null ) {
160                        for( String k : key ) {
161//                              buf.append( "  <key>" ).append( k ).append( "</key>" );
162                                buf.append( "  <key>" ).append( StringUtil.htmlFilter(k) ).append( "</key>" );
163                        }
164                }
165                buf.append( " </keyList>" );
166                buf.append( "</root>" );
167                return buf.toString();
168        }
169
170        /**
171         * パラメーターより伝送読取オブジェクトに渡すキー一覧(配列)を生成します。
172         * 対象パラメーターは@n(データ件数) と Ak1〜kn(データ) です。
173         *
174         * @param valMap パラメーターMap
175         *
176         * @return 値一覧(配列)
177         */
178        private String[] getKeys( final Map<String,String> valMap ) {
179                int rows = 0;
180                String rno = valMap.get( "n" );
181                if( rno != null && rno.length() > 0 ) {
182                        rows = Integer.valueOf( rno );
183                }
184                List<String> list = new ArrayList<String>();
185                for( int i=0; i<rows; i++ ) {
186//                      String key = valMap.get( "k" + String.valueOf( i ) );
187                        String key = valMap.get( "k" + i );
188                        list.add( key );
189                }
190//              return list.toArray( new String[0] );
191                return list.toArray( new String[list.size()] );
192        }
193}