001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: ReadErrorTableImpl.java 11730 2008-01-30 09:27:25Z rhou $
023: */
024: package com.bostechcorp.cbesb.console.server;
025:
026: import java.sql.Connection;
027: import java.sql.ResultSet;
028: import java.sql.ResultSetMetaData;
029: import java.sql.Statement;
030: import java.util.ArrayList;
031: import java.util.List;
032: import java.util.Vector;
033:
034: import javax.servlet.http.HttpSession;
035:
036: import com.bostechcorp.cbesb.common.i18n.CoreMessageConstants;
037: import com.bostechcorp.cbesb.common.i18n.Messages;
038: import com.bostechcorp.cbesb.console.common.ExceptionUtil;
039: import com.bostechcorp.cbesb.console.common.ServerSideException;
040: import com.bostechcorp.cbesb.console.common.TimeoutException;
041: import com.bostechcorp.cbesb.console.common.errordb.DatabaseDataPageInfo;
042: import com.bostechcorp.cbesb.console.rpc.ReadErrorTable;
043:
044: public class ReadErrorTableImpl extends ConsoleRemoteServiceServlet
045: implements ReadErrorTable {
046:
047: private static final long serialVersionUID = -8785191107176833840L;
048: private static final int ID_COLUMN = 1;
049: private StringBuffer sb;
050: private Vector<String> rowList;
051: private int rowsPerPage = 20;
052: private int curPage = 0;
053: private int totalRows = 0;
054:
055: private void cycleResultSet(ResultSet rs,
056: List<String> colNameArray, List<List> rowArray)
057: throws Exception {
058: ResultSetMetaData rsm = rs.getMetaData();
059: int ncols = rsm.getColumnCount();
060: for (int i = 1; i <= ncols; i++) {
061: colNameArray.add(rsm.getColumnName(i));
062: }
063: int total = 0;
064: while (rs.next()) {
065: total++;
066: List<String> row = new ArrayList<String>();
067: for (int c = 1; c <= ncols; c++) {
068: row.add(rs.getString(c));
069: }
070: rowArray.add(row);
071: }
072: this .totalRows = total;
073: HttpSession session = this .getThreadLocalRequest().getSession();
074: session.setAttribute("ColNameArray", colNameArray);
075: session.setAttribute("RowArray", rowArray);
076: }
077:
078: private void displayResultSet(String label,
079: List<String> colNameArray, List<List> rowArray)
080: throws Exception {
081: sb.append("<table border=\"6\" valign=\"top\"><tr>");
082: for (int i = 0; i < colNameArray.size(); i++) {
083: sb.append("<th>\n");
084: sb.append(colNameArray.get(i));
085: sb.append("</th>");
086: }
087: sb.append("</tr>");
088: if (this .curPage < 0)
089: this .curPage = 0;
090: int j = 0;
091: for (int i = this .curPage * this .rowsPerPage; i < rowArray
092: .size(); i++) {
093: if (j > this .rowsPerPage)
094: break;
095: List row = rowArray.get(i);
096: sb.append("<tr>");
097: for (int c = 1; c <= colNameArray.size(); c++) {
098: if (c == ID_COLUMN) {
099: int id = Integer.parseInt((String) row
100: .get(ID_COLUMN - 1));
101: String rowLabel = "row" + id;
102: rowList.addElement(rowLabel);
103: sb.append("<td valign='top' id='");
104: sb.append(rowLabel);
105: sb.append("'/>");
106: } else {
107: sb.append("<td valign=\"top\"");
108: if (c == 1) {
109: sb.append(" id=\"row");
110: sb.append(c);
111: sb.append("\"/>");
112: } else if (c == 4) {
113: String stacktrace = row.get(c - 1).toString();
114: String detailInfo = row.get(c - 1).toString();
115: detailInfo = detailInfo.replace("\n", "\\n");
116: sb.append("><a onClick=\"alert('");
117: sb.append(detailInfo);
118: sb
119: .append("');\" style=\"cursor: pointer; cursor: hand;\"><u>");
120: sb.append(stacktrace.substring(0, stacktrace
121: .indexOf("\n")));
122: sb.append("</u></a></td>");
123: } else {
124: sb.append(">");
125: sb.append(row.get(c - 1));
126: sb.append("</td>");
127: }
128: }
129: }
130: sb.append("</tr>");
131: j++;
132: }
133: sb.append("</table>");
134: }
135:
136: private void processError(Exception e) {
137: sb
138: .append((Messages
139: .get(CoreMessageConstants.DATABASE_CONNECTION_EXCEPTION))
140: + ":" + e + "\n\n");
141: sb.append(ExceptionUtil.stackTraceString(e));
142: }
143:
144: private void readError(Connection dbc) {
145: try {
146: Statement stmt = dbc.createStatement();
147: ResultSet rs = stmt
148: .executeQuery("select * from error order by errorid");
149: List<String> colNameArray = new ArrayList<String>();
150: List<List> rowArray = new ArrayList<List>();
151:
152: cycleResultSet(rs, colNameArray, rowArray);
153: displayResultSet("Exchange Errors", colNameArray, rowArray);
154: } catch (Exception e) {
155: processError(e);
156: }
157: }
158:
159: public DatabaseDataPageInfo read() throws TimeoutException,
160: ServerSideException {
161: this .rowsPerPage = ConsoleSettingUtil.getErrorDbPageSize(super
162: .getCurrentUserId());
163: sb = new StringBuffer();
164: rowList = new Vector<String>();
165: HttpSession session = this .getThreadLocalRequest().getSession();
166: // session.setAttribute("RowsPerPage", String.valueOf(rowsPerPage));
167: Connection dbc = (Connection) session.getAttribute("DBC");
168: if (dbc == null) {
169: throw new ServerSideException((Messages
170: .get(CoreMessageConstants.NO_DATABASE_CONNECTION)));
171: }
172:
173: readError(dbc);
174:
175: DatabaseDataPageInfo result = new DatabaseDataPageInfo(
176: this .curPage, this .totalRows, this .rowsPerPage);
177: result.setData(new String(sb));
178:
179: String[] a = new String[0];
180: result.setRowList((String[]) (rowList.toArray(a)));
181: return result;
182: }
183:
184: public DatabaseDataPageInfo readPage(int curPage)
185: throws TimeoutException, ServerSideException {
186: HttpSession session = this .getThreadLocalRequest().getSession();
187: this .rowsPerPage = Integer.parseInt((String) session
188: .getAttribute("RowsPerPage"));
189: this .curPage = curPage;
190: List<String> colNameArray = (ArrayList) session
191: .getAttribute("ColNameArray");
192: List<List> rowArray = (ArrayList) session
193: .getAttribute("RowArray");
194: if (colNameArray == null || rowArray == null) {
195: return read();
196: }
197: sb = new StringBuffer();
198: rowList = new Vector<String>();
199:
200: Connection dbc = (Connection) session.getAttribute("DBC");
201: if (dbc == null) {
202: throw new ServerSideException((Messages
203: .get(CoreMessageConstants.NO_DATABASE_CONNECTION)));
204: }
205:
206: try {
207: displayResultSet("Exchange Errors", colNameArray, rowArray);
208: } catch (Exception e) {
209: throw new ServerSideException(e);
210: }
211:
212: DatabaseDataPageInfo result = new DatabaseDataPageInfo(
213: this .curPage, this .totalRows, this .rowsPerPage);
214: result.setData(new String(sb));
215:
216: String[] a = new String[0];
217: result.setRowList((String[]) (rowList.toArray(a)));
218:
219: return result;
220: }
221:
222: }
|