1 23 package org.dbforms.event; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 import org.dbforms.config.Constants; 28 import org.dbforms.config.DbFormsConfig; 29 import org.dbforms.config.Field; 30 import org.dbforms.config.FieldTypes; 31 import org.dbforms.config.FieldValue; 32 import org.dbforms.config.FieldValues; 33 import org.dbforms.config.MultipleValidationException; 34 35 import org.dbforms.event.eventtype.EventType; 36 37 import org.dbforms.util.MessageResources; 38 import org.dbforms.util.ParseUtil; 39 import org.dbforms.util.Util; 40 41 import java.io.UnsupportedEncodingException ; 42 43 import java.sql.Connection ; 44 import java.sql.SQLException ; 45 46 import java.util.Iterator ; 47 import java.util.Vector ; 48 49 import javax.servlet.ServletContext ; 50 import javax.servlet.http.HttpServletRequest ; 51 52 53 54 60 public abstract class DatabaseEvent extends WebEvent { 61 private static Log logCat = LogFactory.getLog(DatabaseEvent.class.getName()); 63 64 private String keyId; 65 66 74 public DatabaseEvent(int tableId, String keyId, HttpServletRequest request, 75 DbFormsConfig config) { 76 super(tableId, request, config); 77 this.keyId = keyId; 78 } 79 80 89 90 public abstract FieldValues getFieldValues(); 92 93 94 99 public String getKeyId() { 100 return keyId; 101 } 102 103 104 113 public void doValidation(String formValidatorName, ServletContext context) 114 throws MultipleValidationException { 115 } 116 117 118 126 public abstract void processEvent(Connection con) 127 throws SQLException , MultipleValidationException; 128 129 130 139 protected FieldValues getFieldValues(boolean insertMode) { 140 FieldValues result = new FieldValues(); 141 String paramStub = (Constants.FIELDNAME_PREFIX + getTable().getId() 142 + "_" 143 + (EventType.EVENT_DATABASE_INSERT.equals(getType()) 144 ? Constants.FIELDNAME_INSERTPREFIX : "") + keyId + "_"); 145 Vector params = ParseUtil.getParametersStartingWith(getRequest(), 146 paramStub); 147 148 boolean doIt = insertMode; 150 151 if (!doIt) { 153 Iterator e = params.iterator(); 154 155 while (e.hasNext()) { 156 String param = (String ) e.next(); 157 158 String value = ParseUtil.getParameter(getRequest(), param); 160 161 String oldValue = ParseUtil.getParameter(getRequest(), 163 Constants.FIELDNAME_OLDVALUETAG + param); 164 165 doIt = !value.equals(oldValue); 168 169 if (doIt) { 170 break; 171 } 172 } 173 } 174 175 if (doIt) { 177 Iterator e = params.iterator(); 178 179 while (e.hasNext()) { 180 String param = (String ) e.next(); 181 182 int iiFieldId = Integer.parseInt(param.substring( 183 paramStub.length())); 184 Field f = getTable().getField(iiFieldId); 185 186 String value = f.getEscaper().unescapeHTML(ParseUtil.getParameter( 187 getRequest(), param)); 188 FieldValue fv = new FieldValue(f, value); 189 190 fv.setOldValue(f.getEscaper().unescapeHTML(ParseUtil.getParameter( 191 getRequest(), Constants.FIELDNAME_OLDVALUETAG + param))); 192 fv.setPattern(ParseUtil.getParameter(getRequest(), 193 Constants.FIELDNAME_PATTERNTAG + param)); 194 fv.setLocale(MessageResources.getLocale(getRequest())); 195 196 if ((f.getType() == FieldTypes.BLOB) 197 || (f.getType() == FieldTypes.DISKBLOB)) { 198 fv.setFileHolder(ParseUtil.getFileHolder(getRequest(), 200 "f_" + getTable().getId() + "_" 201 + (insertMode ? Constants.FIELDNAME_INSERTPREFIX : "") 202 + keyId + "_" + iiFieldId)); 203 } 204 205 result.put(fv); 206 } 207 } 208 209 return result; 210 } 211 212 213 218 protected String getKeyValues() { 219 String key = null; 220 221 try { 222 key = ParseUtil.getParameter(getRequest(), 223 "k_" + getTable().getId() + "_" + keyId); 224 key = Util.decode(key, getRequest().getCharacterEncoding()); 225 logCat.info("::getKeyValues - key: " + key); 226 } catch (UnsupportedEncodingException e) { 227 logCat.error(e); 228 } 229 230 return key; 231 } 232 } 233 | Popular Tags |