|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| TableView.java | 0% | 2,6% | 12,5% | 3,3% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* Replica is published under the terms
|
|
| 3 |
* of the Apache Software License.
|
|
| 4 |
*/
|
|
| 5 |
package replica.server.mvc;
|
|
| 6 |
|
|
| 7 |
import java.sql.ResultSet;
|
|
| 8 |
import java.sql.ResultSetMetaData;
|
|
| 9 |
import java.sql.SQLException;
|
|
| 10 |
import java.util.logging.Level;
|
|
| 11 |
import java.util.logging.Logger;
|
|
| 12 |
|
|
| 13 |
/**
|
|
| 14 |
*
|
|
| 15 |
* @author Pedro Costa
|
|
| 16 |
* @author Helder Silva
|
|
| 17 |
* @since 20/Jan/2004
|
|
| 18 |
*/
|
|
| 19 |
public class TableView implements View { |
|
| 20 |
|
|
| 21 |
static Logger logger = Logger.getLogger(TableView.class.getName()); |
|
| 22 |
|
|
| 23 |
final static char SPACE = ' '; |
|
| 24 |
final static char DASH = '-'; |
|
| 25 |
|
|
| 26 |
/**
|
|
| 27 |
*
|
|
| 28 |
*/
|
|
| 29 | 3 |
public TableView() {
|
| 30 | 3 |
super();
|
| 31 |
} |
|
| 32 |
|
|
| 33 |
/* (non-Javadoc)
|
|
| 34 |
* @see replica.server.mvc.View#render(java.lang.Object)
|
|
| 35 |
*/
|
|
| 36 | 0 |
public Object render(Object model) {
|
| 37 | 0 |
return render((ResultSet)model);
|
| 38 |
} |
|
| 39 |
|
|
| 40 |
/**
|
|
| 41 |
* Render a ResultSet as a text string.
|
|
| 42 |
*
|
|
| 43 |
* @param model
|
|
| 44 |
* @return
|
|
| 45 |
*/
|
|
| 46 | 0 |
private String render(ResultSet model){
|
| 47 |
|
|
| 48 | 0 |
try{
|
| 49 | 0 |
StringBuffer res = new StringBuffer();
|
| 50 |
|
|
| 51 | 0 |
ResultSetMetaData metaData = model.getMetaData(); |
| 52 |
|
|
| 53 | 0 |
int columnCount = metaData.getColumnCount();
|
| 54 |
|
|
| 55 | 0 |
writeHeader(res, columnCount, metaData); |
| 56 |
|
|
| 57 | 0 |
writeDividerLine( res, columnCount, metaData, DASH); |
| 58 |
|
|
| 59 | 0 |
while( model.next() )
|
| 60 | 0 |
writeLine(res, columnCount, metaData, model); |
| 61 |
|
|
| 62 | 0 |
return res.toString();
|
| 63 |
} |
|
| 64 |
catch(SQLException e){
|
|
| 65 | 0 |
logger.log(Level.SEVERE, e.getMessage(), e); |
| 66 | 0 |
return "Problem returning data : [" + e.getMessage() +"]."; |
| 67 |
} |
|
| 68 |
finally{
|
|
| 69 | 0 |
try{
|
| 70 | 0 |
model.getStatement().close(); |
| 71 |
} |
|
| 72 |
catch(Throwable t){
|
|
| 73 |
} |
|
| 74 |
} |
|
| 75 |
} |
|
| 76 |
|
|
| 77 | 0 |
private void writeHeader(StringBuffer res, int columnCount, ResultSetMetaData metaData) throws SQLException{ |
| 78 |
|
|
| 79 | 0 |
res.append("|");
|
| 80 | 0 |
for (int i = 1; i <= columnCount; i++) { |
| 81 | 0 |
res.append( fillSpaces( metaData.getColumnLabel(i), metaData.getColumnDisplaySize(i), true ) )
|
| 82 |
.append("|");
|
|
| 83 |
} |
|
| 84 | 0 |
res.append("\n");
|
| 85 |
} |
|
| 86 |
|
|
| 87 | 0 |
private void writeDividerLine(StringBuffer res, int columnCount, ResultSetMetaData metaData, char fillChar) throws SQLException{ |
| 88 |
|
|
| 89 | 0 |
res.append("|");
|
| 90 |
|
|
| 91 | 0 |
for (int i = 1; i <= columnCount; i++) { |
| 92 | 0 |
res.append( fill( "", metaData.getColumnDisplaySize(i), true, fillChar) ); |
| 93 | 0 |
if( i == columnCount )
|
| 94 | 0 |
res.append("|");
|
| 95 |
else
|
|
| 96 | 0 |
res.append("+");
|
| 97 |
} |
|
| 98 |
|
|
| 99 | 0 |
res.append("\n");
|
| 100 |
} |
|
| 101 |
|
|
| 102 | 0 |
private void writeLine(StringBuffer res, int columnCount, ResultSetMetaData metaData, ResultSet model) throws SQLException{ |
| 103 |
|
|
| 104 | 0 |
res.append("|");
|
| 105 | 0 |
for (int i = 1; i <= columnCount; i++) { |
| 106 | 0 |
Object value = model.getObject(i); |
| 107 | 0 |
res.append( fillSpaces( value!=null?value.toString():"", |
| 108 |
metaData.getColumnDisplaySize(i), |
|
| 109 |
metaData.getColumnClassName(i).equals( String.class.getName() ) ) )
|
|
| 110 |
.append("|");
|
|
| 111 |
} |
|
| 112 | 0 |
res.append("\n");
|
| 113 |
} |
|
| 114 |
|
|
| 115 |
/**
|
|
| 116 |
* Return the received string filled with spaces until the returned string has the indicated
|
|
| 117 |
* number of characters.
|
|
| 118 |
*
|
|
| 119 |
* @param value the string to be formated
|
|
| 120 |
* @param size the required size for the string
|
|
| 121 |
* @param appendRight if true spaces are appended to the right of the string, else, appended to the left
|
|
| 122 |
* @return the formated string
|
|
| 123 |
*/
|
|
| 124 | 0 |
private String fillSpaces(String value, int size, boolean appendRight){ |
| 125 |
|
|
| 126 | 0 |
return fill( value, size, appendRight, SPACE);
|
| 127 |
} |
|
| 128 |
|
|
| 129 |
/**
|
|
| 130 |
* Return the received string filled with the given char until the returned string has the indicated
|
|
| 131 |
* number of characters.
|
|
| 132 |
*
|
|
| 133 |
* @param value the string to be formated
|
|
| 134 |
* @param size the required size for the string
|
|
| 135 |
* @param appendRight if true spaces are appended to the right of the string, else, appended to the left
|
|
| 136 |
* @param theChar the char to use to fill
|
|
| 137 |
* @return the formated string
|
|
| 138 |
*/
|
|
| 139 | 0 |
private String fill(String value, int size, boolean appendRight, char theChar){ |
| 140 |
|
|
| 141 | 0 |
if( value.length() > size )
|
| 142 | 0 |
return value.substring(0, size);
|
| 143 |
|
|
| 144 | 0 |
StringBuffer fillChars = new StringBuffer();
|
| 145 |
|
|
| 146 | 0 |
int numChars = size - value.length();
|
| 147 | 0 |
for (int i = 0; i < numChars; i++) { |
| 148 | 0 |
fillChars.append(theChar); |
| 149 |
} |
|
| 150 |
|
|
| 151 | 0 |
return appendRight ? value + fillChars.toString() : fillChars.toString() + value;
|
| 152 |
} |
|
| 153 |
} |
|
| 154 |
|
|
||||||||||