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 * @param valMap サーブレ?が受け取ったキーと値のマッ?
072 *
073 * @return XML形式?実行結果
074 */
075 @Override
076 public String remoteControl( final Map<String,String> valMap ) {
077 // パラメーターより伝?設定オブジェクトを生?します?
078 TransferConfig conf = new TransferConfig(
079 valMap.get( "KBREAD" )
080 , valMap.get( "READOBJ" )
081 , valMap.get( "READPRM" )
082 , valMap.get( "KBEXEC" )
083 , valMap.get( "EXECDBID" )
084 , valMap.get( "EXECOBJ" )
085 , valMap.get( "EXECPRM" )
086 , valMap.get( "ERROR_SENDTO" )
087 , valMap.get( "HFROM" )
088 , null, -1 );
089 Transaction tran = null;
090 String rtn = null;
091 try {
092 tran = new TransactionReal( appInfo );
093 TransferRead read = (TransferRead)StringUtil.newInstance( READ_CLASS_BASE + valMap.get( "KBREAD" ) );
094
095 // ??タ読?
096 String type = valMap.get( "type" );
097 if( "read".equals( type ) ) {
098 String[] data = read.read( conf, tran );
099 // 完?エラー処??ために更新キーを取得しXMLに埋め込?
100 String[] keys = read.getKeys();
101 rtn = makeXml( data, keys );
102 }
103 // 完???
104 else if( "complete".equals( type ) ) {
105 // パラメーターから更新キーを読み取る
106 String[] keys = getKeys( valMap );
107 read.setKeys( keys );
108 read.complete( conf, tran );
109 }
110 // エラー処?
111 else if( "error".equals( type ) ) {
112 // パラメーターから更新キーを読み取る
113 String[] keys = getKeys( valMap );
114 read.setKeys( keys );
115 read.error( conf, appInfo );
116 }
117 else {
118 String errMsg = "処?イプが不正です?[?可能タイ?read,complete,error][?されたタイ?" + type + "]";
119 throw new HybsSystemException( errMsg );
120 }
121 }
122 catch ( Throwable ex ) {
123 String msg = "伝?読取??HTTP経由)でエラーが発生しました?;
124 throw new HybsSystemException( msg, ex );
125 }
126 finally {
127 if( tran != null ) { tran.close(); }
128 }
129
130 return rtn;
131 }
132
133 /**
134 * 伝?読取???結果から??タ?及?キー?からXML??タを生成します?
135 *
136 * @og.rev 5.4.4.5 (2012/02/20) 特定文字をエスケープす?
137 * @param data ??タ?(配?)
138 * @param key 更新時に使用するキー?(配?)
139 *
140 * @return XML??タ
141 */
142 private String makeXml( final String[] data, final String[] key ) {
143 StringBuilder buf = new StringBuilder();
144 buf.append( "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" );
145 buf.append( "<root>" );
146 buf.append( " <dataList>" );
147 if( data != null ) {
148 for( String d : data ) {
149 // buf.append( " <data>" ).append( d ).append( "</data>" );
150 buf.append( " <data>" ).append( StringUtil.htmlFilter(d) ).append( "</data>" );
151 }
152 }
153 buf.append( " </dataList>" );
154 buf.append( " <keyList>" );
155 if( key != null ) {
156 for( String k : key ) {
157 // buf.append( " <key>" ).append( k ).append( "</key>" );
158 buf.append( " <key>" ).append( StringUtil.htmlFilter(k) ).append( "</key>" );
159 }
160 }
161 buf.append( " </keyList>" );
162 buf.append( "</root>" );
163 return buf.toString();
164 }
165
166 /**
167 * パラメーターより伝?読取オブジェクトに渡すキー?(配?)を生成します?
168 * 対象パラメーターは?(??タ件数) と ②k1?kn(??タ) です?
169 *
170 * @param valMap パラメーターMap
171 *
172 * @return 値?(配?)
173 */
174 private String[] getKeys( final Map<String,String> valMap ) {
175 int rows = 0;
176 String rno = valMap.get( "n" );
177 if( rno != null && rno.length() > 0 ) {
178 rows = Integer.valueOf( rno );
179 }
180 List<String> list = new ArrayList<String>();
181 for( int i=0; i<rows; i++ ) {
182 // String key = valMap.get( "k" + String.valueOf( i ) );
183 String key = valMap.get( "k" + i );
184 list.add( key );
185 }
186 // return list.toArray( new String[0] );
187 return list.toArray( new String[list.size()] );
188 }
189 }