| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package net.sf.webphotos.gui.util; |
| 17 | |
|
| 18 | |
|
| 19 | |
import java.util.List; |
| 20 | |
import javax.sql.RowSetEvent; |
| 21 | |
import javax.sql.RowSetListener; |
| 22 | |
import javax.swing.JOptionPane; |
| 23 | |
import javax.swing.table.AbstractTableModel; |
| 24 | |
import net.sf.webphotos.dao.jpa.AlbumDAO; |
| 25 | |
import net.sf.webphotos.util.ApplicationContextResource; |
| 26 | |
import org.apache.log4j.Logger; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
public class TableModelAlbum extends AbstractTableModel implements RowSetListener { |
| 32 | |
|
| 33 | |
private static final long serialVersionUID = 8393087620197315052L; |
| 34 | 0 | private static final TableModelAlbum instancia = new TableModelAlbum(); |
| 35 | |
private String ultimoSQL; |
| 36 | |
|
| 37 | 0 | private List<Object[]> tableData = null; |
| 38 | 0 | private static Logger log = Logger.getLogger(TableModelAlbum.class); |
| 39 | 0 | private static AlbumDAO albunsDAO = (AlbumDAO) ApplicationContextResource.getBean("albunsDAO"); |
| 40 | |
|
| 41 | |
|
| 42 | |
private TableModelAlbum() { |
| 43 | 0 | super(); |
| 44 | 0 | } |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
public static TableModelAlbum getModel() { |
| 52 | 0 | return instancia; |
| 53 | |
} |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
public void update() { |
| 60 | 0 | update(getUltimoSQL()); |
| 61 | 0 | } |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
public void update(String sql) { |
| 71 | |
try { |
| 72 | 0 | ultimoSQL = sql; |
| 73 | 0 | log.debug("Executando - " + ultimoSQL); |
| 74 | 0 | tableData = albunsDAO.findByNativeQuery(ultimoSQL); |
| 75 | 0 | } catch (Exception ex) { |
| 76 | 0 | log.error("Ocorreu um erro durante a leitura do álbum no banco de dados", ex); |
| 77 | 0 | int selecao = JOptionPane.showConfirmDialog(null, |
| 78 | |
"ERRO durante leitura do álbum no banco de dados.\n\nTentar Novamente?", |
| 79 | |
"Aviso!", |
| 80 | |
JOptionPane.YES_NO_OPTION, |
| 81 | |
JOptionPane.WARNING_MESSAGE); |
| 82 | 0 | if (selecao == JOptionPane.YES_OPTION) { |
| 83 | 0 | update(sql); |
| 84 | |
} else { |
| 85 | 0 | log.error("Ocorreu um erro inexperado durante a leitura do álbum", ex); |
| 86 | 0 | JOptionPane.showMessageDialog(null, "ERRO inexperado durante leitura do álbum - " + ex.getMessage(), "Erro!", JOptionPane.ERROR_MESSAGE); |
| 87 | 0 | throw new RuntimeException("Ocorreu um erro inexperado durante a leitura do álbum", ex); |
| 88 | |
} |
| 89 | 0 | } |
| 90 | 0 | } |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
@Override |
| 98 | |
public String getColumnName(int col) { |
| 99 | |
try { |
| 100 | 0 | return albunsDAO.createNativeQuery(ultimoSQL).getParameter(col).getName(); |
| 101 | 0 | } catch (Exception e) { |
| 102 | 0 | log.error("Error trying to get column name", e); |
| 103 | 0 | return "Error"; |
| 104 | |
} |
| 105 | |
} |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
@Override |
| 113 | |
public int getColumnCount() { |
| 114 | |
try { |
| 115 | 0 | return albunsDAO.createNativeQuery(ultimoSQL).getParameters().size(); |
| 116 | 0 | } catch (Exception e) { |
| 117 | 0 | log.error("Error trying to get column count", e); |
| 118 | 0 | return 0; |
| 119 | |
} |
| 120 | |
} |
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
@Override |
| 128 | |
public int getRowCount() { |
| 129 | |
try { |
| 130 | 0 | return albunsDAO.createNativeQuery(ultimoSQL).getResultList().size(); |
| 131 | 0 | } catch (Exception e) { |
| 132 | 0 | log.error("Error trying to get row count", e); |
| 133 | 0 | return 0; |
| 134 | |
} |
| 135 | |
} |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
@Override |
| 145 | |
public Object getValueAt(int row, int col) { |
| 146 | |
try { |
| 147 | 0 | return tableData.get(row)[col]; |
| 148 | 0 | } catch (Exception e) { |
| 149 | 0 | log.error("Error trying to get value at (" + row + "," + col + ")", e); |
| 150 | 0 | return null; |
| 151 | |
} |
| 152 | |
} |
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
@Override |
| 163 | |
public boolean isCellEditable(int l, int c) { |
| 164 | 0 | log.debug("Coordinates(" + l + "," + c + ")"); |
| 165 | 0 | return false; |
| 166 | |
} |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
@Override |
| 177 | |
public Class<?> getColumnClass(int column) { |
| 178 | |
try { |
| 179 | 0 | return albunsDAO.createNativeQuery(ultimoSQL).getParameter(column).getParameterType(); |
| 180 | 0 | } catch (Exception e) { |
| 181 | 0 | log.warn("Error getting column class, returning SuperType information", e); |
| 182 | 0 | return super.getColumnClass(column); |
| 183 | |
} |
| 184 | |
} |
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
public String getUltimoSQL() { |
| 192 | 0 | return ultimoSQL; |
| 193 | |
} |
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
public void setUltimoSQL(String ultimoSQL) { |
| 201 | 0 | this.ultimoSQL = ultimoSQL; |
| 202 | 0 | } |
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
@Override |
| 211 | |
public void rowSetChanged(RowSetEvent event) { |
| 212 | 0 | fireTableStructureChanged(); |
| 213 | 0 | } |
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
@Override |
| 222 | |
public void rowChanged(RowSetEvent event) { |
| 223 | 0 | fireTableDataChanged(); |
| 224 | 0 | } |
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
@Override |
| 232 | |
public void cursorMoved(RowSetEvent event) { |
| 233 | 0 | } |
| 234 | |
} |