package com.vaadin.data.util.sqlcontainer;
+import java.io.IOException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.util.filter.UnsupportedFilterException;
import com.vaadin.data.util.sqlcontainer.query.OrderBy;
import com.vaadin.data.util.sqlcontainer.query.QueryDelegate;
-import com.vaadin.data.util.sqlcontainer.query.TableQuery;
import com.vaadin.data.util.sqlcontainer.query.QueryDelegate.RowIdChangeListener;
+import com.vaadin.data.util.sqlcontainer.query.TableQuery;
import com.vaadin.data.util.sqlcontainer.query.generator.MSSQLGenerator;
import com.vaadin.data.util.sqlcontainer.query.generator.OracleGenerator;
public class SQLContainer implements Container, Container.Filterable,
Container.Indexed, Container.Sortable, Container.ItemSetChangeNotifier {
- private static final long serialVersionUID = -3863564310693712511L;
+ private static final Logger logger = Logger.getLogger(SQLContainer.class
+ .getName());
+
/** Query delegate */
private QueryDelegate delegate;
/** Auto commit mode, default = false */
/** Cache flush notification system enabled. Disabled by default. */
private boolean notificationsEnabled;
- /** Enable to output possible stack traces and diagnostic information */
- private boolean debugMode;
-
/**
* Prevent instantiation without a QueryDelegate.
*/
if (notificationsEnabled) {
CacheFlushNotifier.notifyOfCacheFlush(this);
}
- debug(null, "Row added to DB...");
+ logger.log(Level.INFO, "Row added to DB...");
return itemId;
} catch (SQLException e) {
- debug(e, null);
+ logger.log(Level.WARNING,
+ "Failed to add row to DB. Rolling back.", e);
try {
delegate.rollback();
} catch (SQLException ee) {
- debug(ee, null);
+ logger.log(Level.SEVERE,
+ "Failed to roll back row addition", e);
}
return null;
}
return delegate.containsRowWithKey(((RowId) itemId).getId());
} catch (Exception e) {
/* Query failed, just return false. */
- debug(e, null);
+ logger.log(Level.INFO, "containsId query failed", e);
}
}
return false;
rs.close();
delegate.commit();
} catch (SQLException e) {
- debug(e, null);
+ logger.log(Level.WARNING, "getItemIds() failed, rolling back.", e);
try {
delegate.rollback();
} catch (SQLException e1) {
- debug(e1, null);
+ logger.log(Level.SEVERE, "Failed to roll back state", e1);
}
try {
rs.getStatement().close();
rs.close();
} catch (SQLException e1) {
- debug(e1, null);
+ logger.log(Level.WARNING, "Closing session failed", e1);
}
throw new RuntimeException("Failed to fetch item indexes.", e);
}
CacheFlushNotifier.notifyOfCacheFlush(this);
}
if (success) {
- debug(null, "Row removed from DB...");
+ logger.log(Level.INFO, "Row removed from DB...");
}
return success;
} catch (SQLException e) {
- debug(e, null);
+ logger.log(Level.WARNING, "Failed to remove row, rolling back",
+ e);
try {
delegate.rollback();
} catch (SQLException ee) {
/* Nothing can be done here */
- debug(ee, null);
+ logger.log(Level.SEVERE, "Failed to rollback row removal",
+ ee);
}
return false;
}
}
if (success) {
delegate.commit();
- debug(null, "All rows removed from DB...");
+ logger.log(Level.INFO, "All rows removed from DB...");
refresh();
if (notificationsEnabled) {
CacheFlushNotifier.notifyOfCacheFlush(this);
}
return success;
} catch (SQLException e) {
- debug(e, null);
+ logger.log(Level.WARNING,
+ "removeAllItems() failed, rolling back", e);
try {
delegate.rollback();
} catch (SQLException ee) {
/* Nothing can be done here */
- debug(ee, null);
+ logger.log(Level.SEVERE, "Failed to roll back", ee);
}
return false;
}
try {
asc = ascending[i];
} catch (Exception e) {
- debug(e, null);
+ logger.log(Level.WARNING, "", e);
}
sorters.add(new OrderBy((String) propertyId[i], asc));
}
*/
public void commit() throws UnsupportedOperationException, SQLException {
try {
- debug(null, "Commiting changes through delegate...");
+ logger.log(Level.INFO, "Commiting changes through delegate...");
delegate.beginTransaction();
/* Perform buffered deletions */
for (RowItem item : removedItems.values()) {
* @throws SQLException
*/
public void rollback() throws UnsupportedOperationException, SQLException {
- debug(null, "Rolling back changes...");
+ logger.log(Level.INFO, "Rolling back changes...");
removedItems.clear();
addedItems.clear();
modifiedItems.clear();
if (notificationsEnabled) {
CacheFlushNotifier.notifyOfCacheFlush(this);
}
- debug(null, "Row updated to DB...");
+ logger.log(Level.INFO, "Row updated to DB...");
} catch (SQLException e) {
- debug(e, null);
+ logger.log(Level.WARNING,
+ "itemChangeNotification failed, rolling back...", e);
try {
delegate.rollback();
} catch (SQLException ee) {
/* Nothing can be done here */
- debug(e, null);
+ logger.log(Level.SEVERE, "Rollback failed", e);
}
throw new RuntimeException(e);
}
try {
delegate.setFilters(filters);
} catch (UnsupportedOperationException e) {
- /* The query delegate doesn't support filtering. */
- debug(e, null);
+ logger.log(Level.INFO,
+ "The query delegate doesn't support filtering", e);
}
try {
delegate.setOrderBy(sorters);
} catch (UnsupportedOperationException e) {
- /* The query delegate doesn't support filtering. */
- debug(e, null);
+ logger.log(Level.INFO,
+ "The query delegate doesn't support filtering", e);
}
int newSize = delegate.getCount();
if (newSize != size) {
}
sizeUpdated = new Date();
sizeDirty = false;
- debug(null, "Updated row count. New count is: " + size);
+ logger.log(Level.INFO, "Updated row count. New count is: " + size);
} catch (SQLException e) {
throw new RuntimeException("Failed to update item set size.", e);
}
try {
type = Class.forName(rsmd.getColumnClassName(i));
} catch (Exception e) {
- debug(e, null);
+ logger.log(Level.WARNING, "Class not found", e);
/* On failure revert to Object and hope for the best. */
type = Object.class;
}
rs.getStatement().close();
rs.close();
delegate.commit();
- debug(null, "Property IDs fetched.");
+ logger.log(Level.INFO, "Property IDs fetched.");
} catch (SQLException e) {
- debug(e, null);
+ logger.log(Level.WARNING,
+ "Failed to fetch property ids, rolling back", e);
try {
delegate.rollback();
} catch (SQLException e1) {
- debug(e1, null);
+ logger.log(Level.SEVERE, "Failed to roll back", e1);
}
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e1) {
- debug(e1, null);
+ logger.log(Level.WARNING, "Failed to close session", e1);
}
throw e;
}
} catch (UnsupportedOperationException e) {
/* The query delegate doesn't support sorting. */
/* No need to do anything. */
- debug(e, null);
+ logger.log(Level.INFO,
+ "The query delegate doesn't support sorting", e);
}
delegate.beginTransaction();
rs = delegate.getResults(currentOffset, pageLength * CACHE_RATIO);
rs.getStatement().close();
rs.close();
delegate.commit();
- debug(null, "Fetched " + pageLength * CACHE_RATIO
+ logger.log(Level.INFO, "Fetched " + pageLength * CACHE_RATIO
+ " rows starting from " + currentOffset);
} catch (SQLException e) {
- debug(e, null);
+ logger.log(Level.WARNING, "Failed to fetch rows, rolling back", e);
try {
delegate.rollback();
} catch (SQLException e1) {
- debug(e1, null);
+ logger.log(Level.SEVERE, "Failed to roll back", e1);
}
try {
if (rs != null) {
}
}
} catch (SQLException e1) {
- debug(e1, null);
+ logger.log(Level.WARNING, "Failed to close session", e1);
}
throw new RuntimeException("Failed to fetch page.", e);
}
}
}
- public boolean isDebugMode() {
- return debugMode;
- }
-
- public void setDebugMode(boolean debugMode) {
- this.debugMode = debugMode;
- }
-
- /**
- * Output a debug message or a stack trace of an exception
- *
- * @param message
- */
- private void debug(Exception e, String message) {
- if (debugMode) {
- // TODO: Replace with the common Vaadin logging system once it is
- // available.
- if (message != null) {
- System.err.println(message);
- }
- if (e != null) {
- e.printStackTrace();
- }
- }
- }
-
/**
* Calling this will enable this SQLContainer to send and receive cache
* flush notifications for its lifetime.
r.getReferencedColumn()));
return true;
} catch (Exception e) {
- debug(e, "Setting referenced item failed.");
+ logger.log(Level.WARNING, "Setting referenced item failed.", e);
return false;
}
}
return refdCont.getItem(getReferencedItemId(itemId, refdCont));
}
+ private void writeObject(java.io.ObjectOutputStream out) throws IOException {
+ out.defaultWriteObject();
+ }
+
+ private void readObject(java.io.ObjectInputStream in) throws IOException,
+ ClassNotFoundException {
+ in.defaultReadObject();
+ if (notificationsEnabled) {
+ /*
+ * Register instance with CacheFlushNotifier after de-serialization
+ * if notifications are enabled
+ */
+ CacheFlushNotifier.addInstance(this);
+ }
+ }
+
}
\ No newline at end of file