1 19 20 package com.sslexplorer.wizard.forms; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import java.util.Locale ; 25 26 import javax.servlet.http.HttpServletRequest ; 27 28 import org.apache.struts.Globals; 29 import org.apache.struts.action.ActionErrors; 30 import org.apache.struts.action.ActionMapping; 31 import org.apache.struts.action.ActionMessage; 32 import org.apache.struts.taglib.TagUtils; 33 34 import com.sslexplorer.boot.PropertyClass; 35 import com.sslexplorer.boot.PropertyDefinition; 36 import com.sslexplorer.boot.PropertyDefinitionCategory; 37 import com.sslexplorer.core.CoreException; 38 import com.sslexplorer.core.CoreUtil; 39 import com.sslexplorer.properties.PropertyItem; 40 import com.sslexplorer.properties.forms.PropertiesForm; 41 import com.sslexplorer.tabs.TabModel; 42 import com.sslexplorer.wizard.AbstractWizardSequence; 43 44 45 49 public abstract class AbstractWizardPropertiesForm extends AbstractWizardForm implements PropertiesForm, TabModel { 50 51 public final static String ATTR_PROPERTY_ITEMS = "propertItems"; 52 public final static String ATTR_PROPERTY_ITEM_VALUES = "propertItemValues"; 53 public final static String ATTR_CATEGORY_DEFINITIONS = "categoryDefinitions"; 54 55 private PropertyItem[] propertyItems; 56 private List <PropertyDefinitionCategory> categoryDefinitions; 57 private List <PropertyClass> propertyClasses; 58 private int selectedCategory = -1; 59 private List <String > tabTitles; 60 private HttpServletRequest request; 61 private String selectedTab; 62 private int categoryIdx; 63 64 public AbstractWizardPropertiesForm(List <PropertyClass> propertyClasses) { 65 super(); 66 setPropertyClasses(propertyClasses); 67 } 68 69 public List <PropertyClass> getPropertyClasses() { 70 return propertyClasses; 71 } 72 73 public void setPropertyClasses(List <PropertyClass> propertyClasses) { 74 this.propertyClasses = propertyClasses; 75 } 76 77 80 public void apply(AbstractWizardSequence sequence) throws Exception { 81 for(int i = 0 ; i < propertyItems.length ; i++) { 82 sequence.putAttribute(getKeyForForm(ATTR_PROPERTY_ITEM_VALUES) + "." + propertyItems[i].getName(), propertyItems[i].getValue()); 83 } 84 } 85 86 89 public void init(AbstractWizardSequence sequence, HttpServletRequest request) throws Exception { 90 91 this.request = request; 92 93 96 sequence.putAttribute(getKeyForForm(ATTR_PROPERTY_ITEMS), propertyItems); 97 if(sequence.getAttribute(getKeyForForm(ATTR_CATEGORY_DEFINITIONS), null) == null) { 98 sequence.putAttribute(getKeyForForm(ATTR_CATEGORY_DEFINITIONS), categoryDefinitions); 99 } 100 else { 101 for(int i = 0 ; i < propertyItems.length; i++) { 102 String n = getKeyForForm(ATTR_PROPERTY_ITEM_VALUES) + "." + propertyItems[i].getName(); 103 Object val = sequence.getAttribute(n, null); 104 if(val != null) { 105 propertyItems[i].setValue(val); 106 } 107 } 108 } 109 } 110 111 112 115 public void reset(ActionMapping mapping, HttpServletRequest request) { 116 super.reset(mapping, request); 117 categoryIdx = 0; 118 AbstractWizardSequence seq = getWizardSequence(request); 119 propertyItems = seq == null ? null : (PropertyItem[])seq.getAttribute(getKeyForForm(ATTR_PROPERTY_ITEMS), null); 120 categoryDefinitions = seq == null ? null : (List )seq.getAttribute(getKeyForForm(ATTR_CATEGORY_DEFINITIONS), null); 121 if(propertyItems != null) { 122 for(int i = 0 ; i < propertyItems.length; i++) { 123 if(propertyItems[i].getType() == PropertyDefinition.TYPE_BOOLEAN) { 124 propertyItems[i].setValue("false"); 125 } 126 } 127 } 128 } 129 130 131 132 135 public List getCategoryDefinitions() { 136 return categoryDefinitions; 137 } 138 139 142 public boolean getEnabled() { 143 return true; 144 } 145 146 149 public abstract int getParentCategory(); 150 151 154 public PropertyItem getPropertyItem(int idx) { 155 return propertyItems[idx]; 156 } 157 158 161 public PropertyItem[] getPropertyItems() { 162 return propertyItems; 163 } 164 165 168 public void setCategoryDefinitions(List categoryDefinitions) { 169 this.categoryDefinitions = categoryDefinitions; 170 171 } 172 173 176 public void setPropertyItem(int idx, PropertyItem item) { 177 propertyItems[idx] = item; 178 179 } 180 181 184 public void setPropertyItems(PropertyItem[] propertyItems) { 185 this.propertyItems = propertyItems; 186 } 187 188 String getKeyForForm(String key) { 189 return getPageName() + "." + key; 190 } 191 192 195 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { 196 if(isCommiting()) { 197 ActionErrors errs = null; 198 PropertyItem[] items = getPropertyItems(); 199 for (int i = 0; i < items.length; i++) { 200 PropertyDefinition def = items[i].getDefinition(); 201 try { 202 def.validate(String.valueOf(items[i].getPropertyValue()), getClass().getClassLoader()); 203 } 204 catch(CoreException ce) { 205 if(errs == null) { 206 errs = new ActionErrors(); 207 } 208 ce.getBundleActionMessage().setArg3(CoreUtil.getMessageResources(request.getSession(), 209 def.getMessageResourcesKey()).getMessage((Locale ) request.getSession().getAttribute(Globals.LOCALE_KEY), 210 def.getName() + ".name")); 211 errs.add(Globals.ERROR_KEY, ce.getBundleActionMessage()); 212 } 213 catch (Exception e) { 214 errs.add(Globals.ERROR_KEY, new ActionMessage("properties.error.failedToValidate", e.getMessage())); 215 } 216 } 217 if(errs != null) 218 return errs; 219 220 } 221 return super.validate(mapping, request); 222 } 223 224 public void setSelectedCategory(int selectedCategory) { 225 this.selectedCategory = selectedCategory; 226 } 227 228 public int getSelectedCategory() { 229 return selectedCategory; 230 } 231 232 235 public String getSelectedTab() { 236 return selectedTab; 237 } 238 239 242 public String getTabBundle(int idx) { 243 return categoryDefinitions.get(idx).getBundle(); 244 } 245 246 249 public int getTabCount() { 250 return categoryDefinitions.size(); 251 } 252 253 256 public String getTabName(int idx) { 257 return "category." + categoryDefinitions.get(idx).getId(); 258 } 259 260 263 public String getTabTitle(int idx) { 264 PropertyDefinitionCategory cat = categoryDefinitions.get(idx); 265 return CoreUtil.getMessageResources( 266 request.getSession(), cat.getBundle()).getMessage((Locale )request.getSession().getAttribute(Globals.LOCALE_KEY), getTabName(idx) + ".name"); 267 } 268 269 272 public void setSelectedTab(String selectedTab) { 273 this.selectedTab = selectedTab; 274 } 275 276 283 public List <PropertyItem> getNextCategory() { 284 if(propertyItems.length == 0) { 285 return null; 286 } 287 List <PropertyItem> l = new ArrayList <PropertyItem>(); 288 int curCat = -1; 289 while(categoryIdx < propertyItems.length) { 290 if(curCat == -1) { 291 curCat = propertyItems[categoryIdx].getCategory(); 292 } 293 else if(curCat != propertyItems[categoryIdx].getCategory()) { 294 break; 295 } 296 l.add(propertyItems[categoryIdx]); 297 categoryIdx++; 298 } 299 return l; 300 } 301 } 302 | Popular Tags |