1 /***
2 * Copyright (c) 2002, Reuters America Inc. All rights reserved.<p>
3 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
4 * conditions are met:<p>
5 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
7 * in the documentation and/or other materials provided with the distribution. Neither the name of Reuters America Inc. nor the
8 * names of its contributors may be used to endorse or promote products derived from this software without specific prior written
9 * permission.<p>
10 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
11 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
12 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
14 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
15 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<p>
16 */
17
18 package com.reuters.rc.db.adb;
19
20 import com.reuters.rc.db.*;
21 import com.tibco.tibrv.*;
22
23
24 /***
25 * This class represents the reply sent by ADB in response to a request. Each
26 * response contains zero or more result sets, and each result set contains zero
27 * or more result rows.
28 * @see AdbRequest
29 * @author Jawaid Hakim.
30 */
31 public class AdbReply implements DbReply
32 {
33 /***
34 * Constructor.
35 * @param replyMsg Reply message from ADB.
36 */
37 public AdbReply(TibrvMsg replyMsg)
38 {
39 replyMsg_ = replyMsg;
40 try
41 {
42 valid_ = (getStatus() == 0);
43 }
44 catch (AdbBusinessException ex)
45 {
46 valid_ = false;
47 }
48 catch (AdbSystemException ex)
49 {
50 valid_ = false;
51 }
52 }
53
54 /***
55 * Check if the instance contains a valid result set.
56 * @return <code>true</code> if the object contains a valid
57 * result set. Returns <code>false</code> if the instance does
58 * not contain a valid result set.
59 * @see #getStatus()
60 */
61 public final boolean isValid()
62 {
63 return valid_;
64 }
65
66 /***
67 * Override toString().
68 * @return String representation of self.
69 */
70 public String toString()
71 {
72 return replyMsg_.toString();
73 }
74
75 /***
76 * Pretty Print the reply message.
77 * @return Pretty Printed value.
78 */
79 public final String toPrettyPrintString() throws AdbBusinessException, AdbSystemException
80 {
81 try
82 {
83 StringBuffer sb = new StringBuffer(256);
84 int resultSetCount = getResultSetCount();
85 for (int i = 0; i < resultSetCount; ++i)
86 {
87 sb.append("{\n");
88 int rowCount = getResultRowCount(i + 1);
89 for (int j = 0; j < rowCount; ++j)
90 {
91 sb.append(" row").append(String.valueOf(j)).append(" = ");
92 sb.append((((TibrvMsg)getResultRow(i+1,j))).toString()).append("\n");
93 }
94 sb.append("}\n");
95 }
96 return sb.toString();
97 }
98 catch (ArrayIndexOutOfBoundsException ex)
99 {
100 throw new AdbSystemException(ex);
101 }
102 }
103
104 /***
105 * Get the reply status.
106 * @return Reply status. <code>0</code> indicates success. A non-zero status
107 * indicates ADB error.
108 * @see #getError()
109 */
110 public final int getStatus() throws AdbBusinessException, AdbSystemException
111 {
112 try
113 {
114 if (replyMsg_ == null)
115 throw new AdbBusinessException("NULL reply");
116
117 return ((Integer)replyMsg_.get(ADB_STATUS)).intValue();
118 }
119 catch (TibrvException ex)
120 {
121 throw new AdbSystemException(ex);
122 }
123 }
124
125 /***
126 * Get the sql that caused the error.
127 * @return Sql statement that caused the error. <code>null</code> is
128 * returned if there is no error.
129 * @see #getStatus()
130 * @see #isValid()
131 * @see #getError()
132 */
133 public final String getSql() throws AdbBusinessException, AdbSystemException
134 {
135 try
136 {
137 if (isValid())
138 return (String)replyMsg_.get(ADB_SQL);
139 else
140 throw new AdbBusinessException(getError());
141 }
142 catch (TibrvException ex)
143 {
144 throw new AdbSystemException(ex);
145 }
146 }
147
148 /***
149 * Get a description of the error.
150 * @return Description of error.
151 * @see #getStatus()
152 * @see #getSql()
153 */
154 public final String getError() throws AdbBusinessException, AdbSystemException
155 {
156 try
157 {
158 if (! isValid())
159 {
160 if (replyMsg_ != null)
161 return (String)replyMsg_.get(ADB_ERROR);
162 else
163 throw new AdbBusinessException("NULL reply");
164 }
165 else
166 throw new AdbBusinessException("No error");
167 }
168 catch (TibrvException ex)
169 {
170 throw new AdbSystemException(ex);
171 }
172 }
173
174 /***
175 * Get the closure argument.
176 * @return Closure argument. <code>TibrvException</code> is thrown if
177 * there is no closure.
178 */
179 public final Object getClosure() throws AdbBusinessException, AdbSystemException
180 {
181 try
182 {
183 if (! isValid())
184 throw new AdbBusinessException(getError());
185
186 return replyMsg_.get(ADB_CLOSURE);
187 }
188 catch (TibrvException ex)
189 {
190 throw new AdbSystemException(ex);
191 }
192 }
193
194 /***
195 * Get the number of result sets.
196 * @return Number of result sets.
197 */
198 public final int getResultSetCount() throws AdbBusinessException, AdbSystemException
199 {
200 if (! isValid())
201 throw new AdbBusinessException(getError());
202
203 try
204 {
205 int numResultSet = 0;
206 while (replyMsg_.getFieldInstance(ADB_RESULTS, numResultSet + 1) != null)
207 ++numResultSet;
208
209 return numResultSet;
210 }
211 catch (TibrvException ex)
212 {
213 throw new AdbSystemException(ex);
214 }
215 }
216
217 /***
218 * Get the number of rows in the first result set.
219 * @return Number of rows in the first set.
220 * @see #getResultSetCount()
221 */
222 public final int getResultRowCount() throws AdbBusinessException, AdbSystemException
223 {
224 return getResultRowCount(1);
225 }
226
227 /***
228 * Get the number of rows in the specified result set.
229 * @param resultSet Result set number starting at <code>1</code>.
230 * @return Number of rows in the specified set.
231 * @see #getResultSetCount()
232 */
233 public final int getResultRowCount(int resultSet) throws AdbBusinessException, AdbSystemException
234 {
235 if (! isValid())
236 throw new AdbBusinessException(getError());
237
238 try
239 {
240 TibrvMsgField fld = replyMsg_.getFieldInstance(ADB_RESULTS, resultSet);
241 if (fld == null)
242 return 0;
243
244 return ((TibrvMsg)fld.data).getNumFields();
245 }
246 catch (TibrvException ex)
247 {
248 throw new AdbSystemException(getError());
249 }
250 }
251
252 /***
253 * Get a row from the first result set.
254 * @param index Result set row number. Row numbers start at <code>0</code>.
255 * @return Requested result row from the first result set. <code>null</code> is
256 * returned if the requested row is not found in the first result set.
257 */
258 public final TibrvMsg getResultRow(int index) throws AdbBusinessException, AdbSystemException
259 {
260 return getResultRow(1, index);
261 }
262
263 /***
264 * Get a row from the specified result set. Result set numbers start at <code>1</code>.
265 * @param resultSet Result set number. Result set numbers start at <code>1</code>.
266 * @param index Result set row number. Row numbers start at <code>0</code>.
267 * @return Requested result row from the first result set. <code>null</code> is
268 * returned if the requested row is not found in the specified result set.
269 * @see #getResultSetCount()
270 */
271 public final TibrvMsg getResultRow(int resultSet, int index) throws AdbBusinessException, AdbSystemException
272 {
273 if (! isValid())
274 throw new AdbBusinessException(getError());
275
276 try
277 {
278 TibrvMsgField fld = replyMsg_.getFieldInstance(ADB_RESULTS, resultSet);
279 if (fld == null)
280 throw new AdbBusinessException("Result row not found");
281
282 return (TibrvMsg)((TibrvMsg)fld.data).getFieldByIndex(index).data;
283 }
284 catch (TibrvException ex)
285 {
286 throw new AdbSystemException(ex);
287 }
288 catch (ArrayIndexOutOfBoundsException ex)
289 {
290 throw new AdbBusinessException(ex);
291 }
292 }
293
294
295 /***
296 * Return the underlying tibrvmsg. Has package scope.
297 *
298 * @return a <code>TibrvMsg</code> value
299 */
300 TibrvMsg getAsTibrvMsg() {
301 return replyMsg_;
302 }
303
304 /***
305 * Static data
306 */
307 private static final String ADB_STATUS = "status";
308 private static final String ADB_RESULTS = "results";
309 private static final String ADB_CLOSURE = "closure";
310
311 private static final String ADB_SQL = "sql";
312 private static final String ADB_ERROR = "error";
313 private static final String ADB_ROW = "row";
314
315 /***
316 * Reply from ADB.
317 */
318 private TibrvMsg replyMsg_;
319
320 /***
321 * Flag to indicate validity.
322 */
323 private boolean valid_;
324 }
This page was automatically generated by Maven