1 19 20 package com.sslexplorer.properties.forms; 21 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Locale ; 27 28 import javax.servlet.http.HttpServletRequest ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.struts.Globals; 33 import org.apache.struts.action.ActionErrors; 34 import org.apache.struts.action.ActionMapping; 35 import org.apache.struts.action.ActionMessage; 36 37 import com.sslexplorer.boot.PropertyClass; 38 import com.sslexplorer.boot.PropertyDefinition; 39 import com.sslexplorer.core.CoreException; 40 import com.sslexplorer.core.CoreUtil; 41 import com.sslexplorer.core.EntryIterator; 42 import com.sslexplorer.core.forms.CoreForm; 43 import com.sslexplorer.properties.PropertyItem; 44 45 48 public class AbstractPropertiesForm extends CoreForm implements PropertiesForm { 49 50 static Log log = LogFactory.getLog(AbstractPropertiesForm.class); 51 52 private PropertyItem[] propertyItems; 53 private List <PropertyClass> propertyClasses; 54 private String forwardTo; 55 private int parentCategory; 56 private String input; 57 private boolean redirect; 58 private String updateAction; 59 private List categoryDefinitions; 60 private int selectedCategory; 61 private HashMap store; 62 private int newSelectedCategory; 63 private List subCategories; 64 private List path; 65 private boolean supportsReplacementVariables; 66 67 public AbstractPropertiesForm(List <PropertyClass> propertyClasses, boolean supportsReplacementVariables) { 68 this(); 69 this.propertyClasses = propertyClasses; 70 this.supportsReplacementVariables = supportsReplacementVariables; 71 } 72 73 public AbstractPropertiesForm() { 74 super(); 75 store = new HashMap (); 76 path = new ArrayList (); 77 } 78 79 public boolean isSupportsReplacementVariables() { 80 return supportsReplacementVariables; 81 } 82 83 public boolean getEnabled() { 84 return true; 85 } 86 87 public String getForwardTo() { 88 return forwardTo; 89 } 90 91 public void setForwardTo(String forwardTo) { 92 this.forwardTo = forwardTo; 93 } 94 95 public String getInput() { 96 return input; 97 } 98 99 public void setInput(String input) { 100 this.input = input; 101 } 102 103 public void reset(ActionMapping mapping, javax.servlet.http.HttpServletRequest request) { 104 if (log.isDebugEnabled()) 105 log.debug("Reseting properties form"); 106 super.reset(mapping, request); 107 if (propertyItems != null) { 108 for (int i = 0; i < propertyItems.length; i++) { 109 if (propertyItems[i].getDefinition().getType() == PropertyDefinition.TYPE_BOOLEAN) { 110 propertyItems[i].setValue(Boolean.FALSE.toString()); 111 } 112 else if (propertyItems[i].getDefinition().getType() == PropertyDefinition.TYPE_LIST) { 113 propertyItems[i].setValue(propertyItems[i].getDefinition().getDefaultValue()); 114 } 115 } 116 } 117 } 118 119 public String getUpdateAction() { 120 return updateAction; 121 } 122 123 public PropertyItem[] getPropertyItems() { 124 return propertyItems; 125 } 126 127 public void setCategoryDefinitions(List categoryDefinitions) { 128 this.categoryDefinitions = categoryDefinitions; 129 } 130 131 public void setPropertyItems(PropertyItem[] propertyItems) { 132 this.propertyItems = propertyItems; 133 ; 134 } 135 136 public void setPropertyItem(int idx, PropertyItem item) { 137 propertyItems[idx] = item; 138 } 139 140 public PropertyItem getPropertyItem(int idx) { 141 return propertyItems[idx]; 142 } 143 144 public void setPropertyClasses(List <PropertyClass> propertyClasses) { 145 this.propertyClasses = propertyClasses; 146 } 147 148 public List <PropertyClass> getPropertyClasses() { 149 return propertyClasses; 150 } 151 152 public void setParentCategory(int parentCategory) { 153 this.parentCategory = parentCategory; 154 } 155 156 public int getParentCategory() { 157 return parentCategory; 158 } 159 160 public void setRedirect(boolean redirect) { 161 this.redirect = redirect; 162 } 163 164 public boolean isRedirect() { 165 return redirect; 166 } 167 168 public void setUpdateAction(String updateAction) { 169 this.updateAction = updateAction; 170 } 171 172 public List getCategoryDefinitions() { 173 return categoryDefinitions; 174 } 175 176 public void setSelectedCategory(int selectedCategory) { 177 this.selectedCategory = selectedCategory; 178 } 179 180 public int getSelectedCategory() { 181 return selectedCategory; 182 } 183 184 public PropertyItem retrieveItem(String name, PropertyItem defaultValue) { 185 PropertyItem val = (PropertyItem) store.get(name); 186 return val == null ? defaultValue : val; 187 } 188 189 public void clearValues() { 190 store.clear(); 191 } 192 193 public int getNewSelectedCategory() { 194 return newSelectedCategory; 195 } 196 197 public void setNewSelectedCategory(int newSelectedCategory) { 198 this.newSelectedCategory = newSelectedCategory; 199 } 200 201 public List getSubCategories() { 202 return subCategories; 203 } 204 205 public void setSubCategories(List subCategories) { 206 this.subCategories = subCategories; 207 } 208 209 public void pushCategory(int parentCategory) { 210 path.add(new Integer (parentCategory)); 211 } 212 213 public int popCategory() { 214 if (path.size() > 0) { 215 int parentCategory = ((Integer ) path.remove(path.size() - 1)).intValue(); 216 return parentCategory; 217 } 218 return -1; 219 } 220 221 public void storeItems() { 222 223 PropertyItem[] items = getPropertyItems(); 224 for (int i = 0; i < items.length; i++) { 225 PropertyDefinition def = items[i].getDefinition(); 226 if (def.getCategory() == getSelectedCategory()) { 227 storeItem(items[i]); 228 } 229 } 230 231 } 232 233 public void storeItem(PropertyItem item) { 234 store.put(item.getName(), item); 235 } 236 237 public Iterator storedItems() { 238 return new EntryIterator(store); 239 } 240 241 244 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { 245 if (isCommiting()) { 246 ActionErrors errs = null; 247 storeItems(); 250 for (Iterator i = storedItems(); i.hasNext();) { 251 PropertyItem item = (PropertyItem) i.next(); 252 PropertyDefinition def = item.getDefinition(); 253 try { 255 def.validate(String.valueOf(item.getPropertyValue()), getClass().getClassLoader()); 256 } catch (CoreException ce) { 257 ce.getBundleActionMessage().setArg3(CoreUtil.getMessageResources(request.getSession(), 258 def.getMessageResourcesKey()).getMessage((Locale ) request.getSession().getAttribute(Globals.LOCALE_KEY), 259 def.getName() + ".name")); 260 if (errs == null) { 261 errs = new ActionErrors(); 262 } 263 errs.add(Globals.ERROR_KEY, ce.getBundleActionMessage()); 264 } catch (Exception e) { 265 errs.add(Globals.ERROR_KEY, new ActionMessage("properties.error.failedToValidate", e.getMessage())); 266 } 267 } 268 if (errs != null) 269 return errs; 270 271 } 272 return super.validate(mapping, request); 273 } 274 } | Popular Tags |