1 19 20 package com.sslexplorer.properties.attributes.forms; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.List ; 25 26 import javax.servlet.http.HttpServletRequest ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.apache.struts.Globals; 31 import org.apache.struts.action.ActionErrors; 32 import org.apache.struts.action.ActionMapping; 33 import org.apache.struts.action.ActionMessage; 34 35 import com.sslexplorer.boot.CodedException; 36 import com.sslexplorer.boot.DefaultPropertyDefinition; 37 import com.sslexplorer.boot.PropertyClass; 38 import com.sslexplorer.boot.PropertyClassManager; 39 import com.sslexplorer.core.CoreException; 40 import com.sslexplorer.core.forms.CoreForm; 41 import com.sslexplorer.properties.attributes.AttributeDefinition; 42 import com.sslexplorer.properties.attributes.AttributesPropertyClass; 43 import com.sslexplorer.properties.impl.userattributes.UserAttributes; 44 45 46 53 public class AttributeDefinitionForm extends CoreForm { 54 55 final static Log log = LogFactory.getLog(AttributeDefinitionForm.class); 56 57 59 protected String name; 60 protected String label; 61 protected String category; 62 protected String typeMeta; 63 protected int type; 64 protected String validationString; 65 protected String description; 66 protected String defaultValue; 67 protected int visibility; 68 protected int sortOrder; 69 protected AttributeDefinition definition; 70 71 76 public void initialise(AttributeDefinition definition) { 77 this.definition = definition; 78 this.name = definition.getName(); 79 this.label = definition.getLabel(); 80 this.visibility = definition.getVisibility(); 81 this.defaultValue = definition.getDefaultValue(); 82 this.sortOrder = definition.getSortOrder(); 83 this.description = definition.getDescription(); 84 this.category = definition.getCategoryLabel(); 85 this.type = definition.getType(); 86 this.validationString = definition.getValidationString(); 87 this.typeMeta = definition.getTypeMeta(); 88 } 89 90 95 public Collection <AttributesPropertyClass> getAttributePropertyClasses() { 96 List <AttributesPropertyClass> l = new ArrayList <AttributesPropertyClass>(); 97 for(PropertyClass propertyClass : PropertyClassManager.getInstance().getPropertyClasses()) { 98 if(propertyClass instanceof AttributesPropertyClass) { 99 l.add((AttributesPropertyClass)propertyClass); 100 } 101 } 102 return l; 103 } 104 105 110 public String getDefaultValue() { 111 return defaultValue; 112 } 113 114 119 public void setDefaultValue(String defaultValue) { 120 this.defaultValue = defaultValue; 121 } 122 123 128 public String getLabel() { 129 return label; 130 } 131 132 137 public void setLabel(String label) { 138 this.label = label; 139 } 140 141 146 public String getDescription() { 147 return description; 148 } 149 150 155 public void setDescription(String description) { 156 this.description = description; 157 } 158 159 164 public String getTypeMeta() { 165 return typeMeta; 166 } 167 168 173 public void setTypeMeta(String typeMeta) { 174 this.typeMeta = typeMeta; 175 } 176 177 182 public String getCategory() { 183 return category; 184 } 185 186 191 public void setCategory(String category) { 192 this.category = category; 193 } 194 195 200 public String getName() { 201 return name; 202 } 203 204 209 public void setName(String name) { 210 this.name = name; 211 } 212 213 219 public int getVisibility() { 220 return visibility; 221 } 222 223 229 public void setVisibility(int visibility) { 230 this.visibility = visibility; 231 } 232 233 238 public void setSortOrder(int sortOrder) { 239 this.sortOrder = sortOrder; 240 } 241 242 247 public int getSortOrder() { 248 return sortOrder; 249 } 250 251 256 public int getType() { 257 return type; 258 } 259 260 265 public void setType(int type) { 266 this.type = type; 267 } 268 269 275 public String getValidationString() { 276 return validationString; 277 } 278 279 285 public void setValidationString(String validationString) { 286 this.validationString = validationString; 287 } 288 289 294 public AttributeDefinition getDefinition() { 295 return definition; 296 } 297 298 302 public void applyToDefinition() { 303 definition.setDefaultValue(getDefaultValue().trim()); 304 definition.setLabel(getLabel().trim()); 305 definition.setSortOrder(getSortOrder()); 306 definition.setCategoryLabel(getCategory().trim()); 307 definition.setDescription(getDescription().trim()); 308 if(!getEditing()) { 309 definition.setVisibility(getVisibility()); 310 definition.setName(getName().trim()); 311 definition.setType(getType()); 312 } 313 definition.setValidationString(getValidationString()); 314 definition.setTypeMeta(getTypeMeta()); 315 } 316 317 320 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { 321 if(isCommiting()) { 322 ActionErrors errs = new ActionErrors(); 323 if(getName().trim().equals("")) { 324 errs.add(Globals.ERROR_KEY, new ActionMessage("editAttributeDefinition.error.noName")); 325 } else if(getDescription().trim().equals("")) { 326 errs.add(Globals.ERROR_KEY, new ActionMessage("editAttributeDefinition.error.noDescription")); 327 } else { 328 if(!getEditing()) { 329 try { 330 PropertyClass propertyClass = PropertyClassManager.getInstance().getPropertyClass(UserAttributes.NAME); 331 AttributeDefinition def = (AttributeDefinition)propertyClass.getDefinition(getName()); 332 if(def != null) { 333 errs.add(Globals.ERROR_KEY, new ActionMessage("editAttributeDefinition.error.duplicateName", getName())); 334 } 335 } 336 catch(Exception e) { 337 log.error("Failed to test if attribute exists.", e); 338 errs.add(Globals.ERROR_KEY, new ActionMessage("editAttributeDefinition.error.duplicateName", getName())); 339 } 340 } 341 if (!getName().matches("^[a-zA-Z0-9_-]*$")) { 342 errs.add(Globals.ERROR_KEY, new ActionMessage("editAttributeDefinition.error.invalidName")); 343 } 344 } 345 if(!validationString.trim().equals("")) { 346 String className = null; 347 int idx = validationString.indexOf('('); 348 if(idx == -1) { 349 className = validationString; 350 } 351 else { 352 if(!validationString.endsWith(")")) { 353 errs = new ActionErrors(); 354 errs.add(Globals.ERROR_KEY, new ActionMessage("editAttributeDefinition.error.invalidValidationStringFormat")); 355 } 356 className = validationString.substring(0, idx); 357 } 358 359 try { 360 Class.forName(className); 361 } 362 catch(ClassNotFoundException cnfe) { 363 errs.add(Globals.ERROR_KEY, new ActionMessage("editAttributeDefinition.error.noSuchValidator", className)); 364 } 365 366 try { 367 369 String defaultValue = getDefaultValue().trim(); 370 if(!defaultValue.equals("")) { 371 AttributeDefinition def = ((AttributesPropertyClass)definition.getPropertyClass()).createAttributeDefinition(type, 372 name, 373 typeMeta, 374 -1, 375 category, 376 defaultValue, 377 visibility, 378 sortOrder, 379 null, 380 false, 381 label, 382 description, 383 false, 384 true, 385 validationString); 386 try { 387 def.validate(defaultValue, getClass().getClassLoader()); 388 } catch (CoreException ce) { 389 ce.getBundleActionMessage().setArg3(def.getLabel()); 390 if (errs == null) { 391 errs = new ActionErrors(); 392 } 393 errs.add(Globals.ERROR_KEY, ce.getBundleActionMessage()); 394 } 395 } 396 } 397 catch(Exception e) { 398 if(errs == null) { 399 errs = new ActionErrors(); 400 } 401 errs.add(Globals.ERROR_KEY, new ActionMessage("editAttributeDefinition.error.failedToValidate", className)); 402 } 403 } 404 return errs; 405 } 406 return null; 407 } 408 } 409 | Popular Tags |