1 23 package org.dbforms.event.datalist; 24 import org.dbforms.config.DbEventInterceptor; 25 import org.dbforms.config.DbEventInterceptorData; 26 import org.dbforms.config.DbFormsConfig; 27 import org.dbforms.config.Field; 28 import org.dbforms.config.FieldValues; 29 import org.dbforms.config.GrantedPrivileges; 30 31 import org.dbforms.event.ValidationEvent; 32 import org.dbforms.event.datalist.dao.DataSourceFactory; 33 import org.dbforms.event.datalist.dao.DataSourceSessionList; 34 35 import org.dbforms.util.MessageResourcesInternal; 36 import org.dbforms.util.StringUtil; 37 38 import java.sql.Connection ; 39 import java.sql.SQLException ; 40 41 import java.util.Vector ; 42 43 import javax.servlet.http.HttpServletRequest ; 44 45 46 47 53 public class InsertEvent extends ValidationEvent { 54 62 public InsertEvent(Integer tableId, String keyId, 63 HttpServletRequest request, DbFormsConfig config) { 64 super(tableId.intValue(), keyId, request, config); 65 } 66 67 68 79 public InsertEvent(String action, HttpServletRequest request, 80 DbFormsConfig config) { 81 super(StringUtil.getEmbeddedStringAsInteger(action, 2, '_'), 82 StringUtil.getEmbeddedString(action, 3, '_'), request, config); 83 } 84 85 92 93 public FieldValues getFieldValues() { 95 return getFieldValues(true); 96 } 97 98 99 107 public void processEvent(Connection con) throws SQLException { 108 if (!hasUserPrivileg(GrantedPrivileges.PRIVILEG_INSERT)) { 111 String s = MessageResourcesInternal.getMessage("dbforms.events.insert.nogrant", 112 getRequest().getLocale(), new String [] { 113 getTable().getName() 114 }); 115 throw new SQLException (s); 116 } 117 118 FieldValues fieldValues = getFieldValues(); 119 120 if (fieldValues.size() == 0) { 121 throw new SQLException ("no parameters"); 122 } 123 124 DbEventInterceptorData interceptorData = new DbEventInterceptorData(getRequest(), 125 getConfig(), con, getTable()); 126 interceptorData.setAttribute(DbEventInterceptorData.FIELDVALUES, 127 fieldValues); 128 129 int operation = getTable().processInterceptors(DbEventInterceptor.PRE_INSERT, 131 interceptorData); 132 133 if ((operation == DbEventInterceptor.GRANT_OPERATION) 134 && (fieldValues.size() > 0)) { 135 if (!checkSufficentValues(fieldValues)) { 137 throw new SQLException ("unsufficent parameters"); 138 } 139 140 DataSourceSessionList ds = DataSourceSessionList.getInstance(getRequest()); 142 DataSourceFactory qry = ds.get(getTable(), getRequest()); 143 144 boolean own = false; 145 146 if (qry == null) { 147 qry = new DataSourceFactory((String ) interceptorData 148 .getAttribute(DbEventInterceptorData.CONNECTIONNAME), 149 interceptorData.getConnection(), getTable()); 150 own = true; 151 } 152 153 qry.doInsert(interceptorData, fieldValues); 154 155 if (own) { 156 qry.close(); 157 } else { 158 ds.remove(getTable(), getRequest()); 159 } 160 161 String firstPosition = getTable().getPositionString(fieldValues); 163 getRequest().setAttribute("firstpos_" + getTable().getId(), 164 firstPosition); 165 166 getTable().processInterceptors(DbEventInterceptor.POST_INSERT, 169 interceptorData); 170 } 171 } 172 173 174 196 private boolean checkSufficentValues(FieldValues fieldValues) 197 throws SQLException { 198 Vector fields = getTable().getFields(); 199 200 for (int i = 0; i < fields.size(); i++) { 201 Field field = (Field) fields.elementAt(i); 202 203 if (!field.hasAutoIncSet() && field.hasIsKeySet()) { 206 if (fieldValues.get(field.getName()) == null) { 207 throw new SQLException ("Field " + field.getName() 208 + " is missing"); 209 } 210 } 211 else if (field.hasAutoIncSet()) { 213 if (fieldValues.get(field.getName()) != null) { 214 throw new SQLException ("Field " + field.getName() 215 + " should be calculated by RDBMS, remove it from the form"); 216 } 217 } 218 219 } 221 222 return true; 223 } 224 } 225 | Popular Tags |