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 }