1 23 package interceptors; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 28 import org.dbforms.config.ConfigLoader; 29 import org.dbforms.config.DbFormsConfig; 30 import org.dbforms.config.FieldValues; 31 import org.dbforms.config.Table; 32 import org.dbforms.config.ValidationException; 33 34 import org.dbforms.event.DbEventInterceptorSupport; 35 36 import java.sql.Connection ; 37 import java.sql.ResultSet ; 38 import java.sql.SQLException ; 39 import java.sql.Statement ; 40 41 import javax.servlet.http.HttpServletRequest ; 42 43 49 public class BookstoreWithInterceptorTest extends DbEventInterceptorSupport { 50 private static Log logCat = LogFactory.getLog(ConfigLoader.class); 51 52 71 public int preInsert(HttpServletRequest request, Table table, 72 FieldValues fieldValues, DbFormsConfig config, Connection con) 73 throws ValidationException { 74 logCat.info("preInsert called"); 75 76 Statement stmt; 77 ResultSet rs = null; 78 long new_id = 0; 79 String strSql = ""; 80 String strParentID = "AUTHOR_ID"; 81 String strID = "BOOK_ID"; 82 String strTbl = "BOOK"; 83 84 if (fieldValues.get(strID) == null) { 85 try { 86 stmt = con.createStatement(); 87 strSql = "select max(" + strID + ") from " + strTbl; 88 89 if (fieldValues.get(strParentID) != null) { 90 strSql = strSql + " where " + strParentID + "=" 91 + fieldValues.get(strParentID); 92 } 93 94 rs = stmt.executeQuery(strSql); 95 rs.next(); 96 new_id = rs.getLong(1) + 1; 97 stmt.close(); 98 } catch (SQLException e) { 99 e.printStackTrace(); 100 } 101 102 if (new_id == 0) { 103 throw new ValidationException("Error generating automatic IDs"); 104 } 105 106 fieldValues.remove(strID); 107 setValue(table, fieldValues, strID, Long.toString(new_id)); 108 setValue(table, fieldValues, strParentID, Long.toString(1)); 109 110 setValue(table, fieldValues, "TITLE", 112 "fixed title in new interceptor"); 113 } 114 115 return GRANT_OPERATION; 116 } 117 118 130 public int preSelect(HttpServletRequest request, DbFormsConfig config, 131 Connection con) { 132 logCat.info("preSelect called"); 133 134 return GRANT_OPERATION; 135 } 136 137 156 public int preUpdate(HttpServletRequest request, Table table, 157 FieldValues fieldValues, DbFormsConfig config, Connection con) 158 throws ValidationException { 159 logCat.info("preUpdate called"); 160 161 if ("42".equals(fieldValues.get("ISBN").getFieldValue())) { 162 return IGNORE_OPERATION; 163 } 164 165 fieldValues.remove("ISBN"); 166 167 return GRANT_OPERATION; 168 } 169 } 170
| Popular Tags
|