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.hayabusa.remote;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    import java.util.Map;
021    
022    import org.opengion.fukurou.db.Transaction;
023    import org.opengion.fukurou.db.TransactionReal;
024    import org.opengion.fukurou.transfer.TransferConfig;
025    import org.opengion.fukurou.transfer.TransferRead;
026    import org.opengion.fukurou.util.ApplicationInfo;
027    import org.opengion.fukurou.util.StringUtil;
028    import org.opengion.hayabusa.common.HybsSystem;
029    import 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     */
046    public 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,操?プログラ?D
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             * 対象パラメーターは?(??タ件数) と ②k1?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    }