1 23 package org.mdarad.framework.util.struts.action; 24 25 import java.util.Collection ; 26 import java.util.List ; 27 import java.util.Vector ; 28 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 32 import org.mdarad.framework.util.ClassUtils; 33 import org.mdarad.framework.util.ClassUtilsException; 34 import org.mdarad.framework.util.struts.criteria.CriterionException; 35 import org.mdarad.framework.util.struts.criteria.FormCriterion; 36 37 44 public abstract class SearchActionForm extends MdaradActionForm { 45 46 53 public abstract FormCriterion[] getAllowableCriteria(); 54 55 56 60 private Collection concreteCriteria; 61 62 67 public List getConcreteCriteria() { 68 if (concreteCriteria == null) { 69 concreteCriteria = new Vector (); 70 } 71 return (List ) concreteCriteria; 72 } 73 74 78 public void setConcreteCriteria(List collection) { 79 concreteCriteria = collection; 80 } 81 82 86 private String selectedCriterion; 87 88 92 public String getSelectedCriterion() { 93 return selectedCriterion; 94 } 95 96 100 public void setSelectedCriterion(String criterion) { 101 selectedCriterion = criterion; 102 } 103 104 111 public FormCriterion getAllowableCriterion(String name) { 112 FormCriterion[] criteria = getAllowableCriteria(); 113 for (int i = 0; i < criteria.length; i++) { 114 if (criteria[i].getName().equals(name)) { 115 return criteria[i]; 116 } 117 } 118 return null; 119 } 120 121 129 public FormCriterion addCriterion(String key) { 130 FormCriterion criterion = (FormCriterion) getAllowableCriterion(key); 131 if (criterion == null) { 132 throw new CriterionException("The criterion doesn't exist"); 133 } 134 Collection concreteCriteria = getConcreteCriteria(); 135 137 Object object = null; 138 try { 139 object = ClassUtils.getInstance( 140 criterion.getClass(), 141 new Class [] { criterion.getClass() }, 142 new Object [] { criterion }); 143 } catch (ClassUtilsException cue) { 144 throw new FormException(cue); 145 } 146 FormCriterion concCriterion = (FormCriterion) object; 147 addCriterion(concCriterion); 148 return concCriterion; 149 } 150 151 160 public FormCriterion addCriterion(FormCriterion criterion) { 161 if (criterion == null) { 162 throw new CriterionException("The criterion to be added is null"); 163 } 164 getConcreteCriteria().add(criterion); 165 return (FormCriterion) criterion; 166 } 167 168 169 174 public void removeCriterion(Integer index) { 175 Collection concreteCriteria = getConcreteCriteria(); 176 ((List )concreteCriteria).remove(index.intValue()); 177 } 178 179 180 185 public void initialize(HttpServletRequest request, 186 HttpServletResponse response) { 187 setConcreteCriteria(new Vector ()); 188 } 189 190 } 191 | Popular Tags |