1 3 package com.sslexplorer.extensions; 4 5 import java.util.ArrayList ; 6 import java.util.List ; 7 import java.util.Locale ; 8 import java.util.StringTokenizer ; 9 10 import org.apache.struts.Globals; 11 import org.apache.struts.action.ActionErrors; 12 import org.apache.struts.action.ActionMessage; 13 14 import com.sslexplorer.boot.CodedException; 15 import com.sslexplorer.boot.PropertyDefinition; 16 import com.sslexplorer.boot.PropertyList; 17 import com.sslexplorer.core.BundleActionMessage; 18 import com.sslexplorer.core.CoreException; 19 import com.sslexplorer.core.CoreUtil; 20 21 27 public class ShortcutParameterItem implements Comparable <ShortcutParameterItem> { 28 29 32 public final static String DUMMY_PASSWORD = "******"; 33 34 private ApplicationParameterDefinition definition; 36 private Object value; 37 private Pair[] listItems; 38 private int rows, columns; 39 private ExtensionDescriptor app; 40 private Locale locale; 41 42 50 public ShortcutParameterItem(ExtensionDescriptor app, ApplicationParameterDefinition definition, String value, Locale locale) { 51 this.definition = definition; 52 this.app = app; 53 this.locale = locale; 54 55 rows = 0; 56 columns = 0; 57 58 if (definition.getType() == PropertyDefinition.TYPE_LIST) { 59 StringTokenizer t = new StringTokenizer (definition.getTypeMeta(), ","); 60 List <Pair> listItemsList = new ArrayList <Pair>(); 61 while (t.hasMoreTokens()) { 62 String n = t.nextToken(); 63 String k = "application." + app.getId() + "." + definition.getName() + ".value." + n; 64 String v = app.getMessageResources().getMessage(k); 65 Pair pair = new Pair(n, v); 66 if (n.equals(value)) { 67 this.value = pair.getValue(); 68 } 69 listItemsList.add(pair); 70 } 71 listItems = new Pair[listItemsList.size()]; 72 listItemsList.toArray(listItems); 73 } else if (definition.getType() == PropertyDefinition.TYPE_MULTI_ENTRY_LIST) { 74 this.value = new PropertyList(value).getAsTextFieldText(); 75 StringTokenizer t = new StringTokenizer (definition.getTypeMeta(), "x"); 76 try { 77 columns = Integer.parseInt(t.nextToken()); 78 rows = Integer.parseInt(t.nextToken()); 79 } catch (NumberFormatException nfe) { 80 81 } 82 } else if (definition.getType() == PropertyDefinition.TYPE_TEXT_AREA) { 83 this.value = value; 84 StringTokenizer t = new StringTokenizer (definition.getTypeMeta(), "x"); 85 try { 86 columns = Integer.parseInt(t.nextToken()); 87 rows = Integer.parseInt(t.nextToken()); 88 } catch (NumberFormatException nfe) { 89 90 } 91 } else if (definition.getType() == PropertyDefinition.TYPE_BOOLEAN) { 92 String trueVal = (String ) (((List ) definition.getTypeMetaObject()).get(0)); 93 this.value = value.equals(trueVal) ? Boolean.TRUE : Boolean.FALSE; 94 } else if (definition.getType() == PropertyDefinition.TYPE_STRING) { 95 try { 96 columns = Integer.parseInt(definition.getTypeMeta()); 97 } catch (NumberFormatException nfe) { 98 } 99 this.value = value; 100 } else if (definition.getType() == PropertyDefinition.TYPE_PASSWORD) { 101 try { 102 columns = Integer.parseInt(definition.getTypeMeta()); 103 } catch (NumberFormatException nfe) { 104 } 105 this.value = value; 106 } else if (definition.getType() == PropertyDefinition.TYPE_TIME_IN_MS) { 107 try { 108 int val = Integer.parseInt(definition.getDefaultValue()); 109 if (definition.getTypeMeta().equalsIgnoreCase("s")) { 110 this.value = String.valueOf(val / 1000); 111 } else if (definition.getTypeMeta().equalsIgnoreCase("m")) { 112 this.value = String.valueOf(val / 1000 / 60); 113 } else if (definition.getTypeMeta().equalsIgnoreCase("h")) { 114 this.value = String.valueOf(val / 1000 / 60 / 60); 115 } else if (definition.getTypeMeta().equalsIgnoreCase("d")) { 116 this.value = String.valueOf(val / 1000 / 60 / 60 / 24); 117 } 118 } catch (Exception e) { 119 } 120 } else { 121 this.value = value; 122 } 123 124 } 126 127 135 public String getPasswordValue() { 136 return getValue() == null || getValue().equals("") ? "" : DUMMY_PASSWORD; 137 } 138 139 146 public ActionMessage validateItem() throws Exception { 147 if (getType() == PropertyDefinition.TYPE_INTEGER) { 148 if (getValue().toString().trim().length() == 0) { 149 if (!definition.isOptional()) { 150 return new BundleActionMessage("properties", "error.integerRequired", app.getMessageResources() 151 .getMessage("application." + app.getId() + "." + getName() + ".name")); 152 } 153 return null; 154 } 155 } else if (getType() == PropertyDefinition.TYPE_STRING || getType() == PropertyDefinition.TYPE_TEXT_AREA 156 || getType() == PropertyDefinition.TYPE_PASSWORD) { 157 if (getValue().toString().trim().length() == 0) { 158 if (!definition.isOptional()) { 159 return new BundleActionMessage("properties", "error.requiredParameterEmpty", app.getMessageResources() 160 .getMessage("application." + app.getId() + "." + getName() + ".name")); 161 162 } 163 return null; 164 } 165 } 166 167 PropertyDefinition def = getDefinition(); 168 try { 169 def.validate(String.valueOf(getPropertyValue()), getClass().getClassLoader()); 170 } catch (CoreException ce) { 171 ce.getBundleActionMessage().setArg3(def.getName()); 172 return ce.getBundleActionMessage(); 173 } 174 return null; 175 } 176 177 182 public boolean getOptional() { 183 return definition.isOptional(); 184 } 185 186 191 public String getLocalisedName() { 192 String key = "application." + app.getId() + "." + getName() + ".name"; 193 return app.getMessageResources().getMessage(key); 194 } 195 196 201 public String getLocalisedDescription() { 202 String key = "application." + app.getId() + "." + getName() + ".description"; 203 return app.getMessageResources().isPresent(key) ? app.getMessageResources().getMessage(locale, key) : getLocalisedName(); 204 } 205 206 211 public String getLocalisedCategory() { 212 String key = "application." + app.getId() + ".category." + getCategory() + ".name"; 213 return app.getMessageResources().isPresent(key) ? app.getMessageResources().getMessage(locale, key) : ""; 214 } 215 216 222 public int getColumns() { 223 return columns; 224 } 225 226 232 public int getRows() { 233 return rows; 234 } 235 236 241 public PropertyDefinition getDefinition() { 242 return definition; 243 } 244 245 250 public void setDefinition(ApplicationParameterDefinition definition) { 251 this.definition = definition; 252 } 253 254 259 public String getName() { 260 return definition.getName(); 261 } 262 263 268 public int getCategory() { 269 return definition.getCategory(); 270 } 271 272 277 public String getDefaultValue() { 278 return definition.getDefaultValue(); 279 } 280 281 286 public String getDefaultText() { 287 String val = getDefaultValue(); 288 if (definition.getType() == PropertyDefinition.TYPE_PASSWORD) { 289 val = ""; 290 } else if (definition.getType() == PropertyDefinition.TYPE_MULTI_ENTRY_LIST) { 291 PropertyList list = new PropertyList(definition.getDefaultValue()); 292 val = list.size() > 0 ? list.getPropertyItem(0) : ""; 293 } else if (definition.getType() == PropertyDefinition.TYPE_LIST) { 294 try { 295 int defaultItem = Integer.parseInt(definition.getDefaultValue()); 296 String k = "application." + app.getId() + "." + definition.getName() + ".value." + defaultItem; 297 val = app.getMessageResources().getMessage(k); 298 } catch (Exception e) { 299 } 300 } else if (definition.getType() == PropertyDefinition.TYPE_TIME_IN_MS) { 301 try { 302 int defaultItem = Integer.parseInt(getDefaultValue()); 303 if (definition.getTypeMeta().equalsIgnoreCase("s")) { 304 val = String.valueOf(defaultItem / 1000); 305 } else if (definition.getTypeMeta().equalsIgnoreCase("m")) { 306 val = String.valueOf(defaultItem / 1000 / 60); 307 } else if (definition.getTypeMeta().equalsIgnoreCase("h")) { 308 val = String.valueOf(defaultItem / 1000 / 60 / 60); 309 } else if (definition.getTypeMeta().equalsIgnoreCase("d")) { 310 val = String.valueOf(defaultItem / 1000 / 60 / 60 / 24); 311 } else { 312 val = String.valueOf(val); 313 } 314 } catch (Exception e) { 315 val = String.valueOf(getDefaultValue()); 316 } 317 } 318 if (val.length() > 15) { 319 val = val.substring(0, 15); 320 } 321 return val; 322 } 323 324 329 public String getTypeMeta() { 330 return definition.getTypeMeta(); 331 } 332 333 339 public Pair[] getListItems() { 340 return listItems; 341 } 342 343 348 public Object getValue() { 349 return value; 350 } 351 352 357 public boolean getSelected() { 358 return value.equals(Boolean.TRUE); 359 } 360 361 366 public void setSelected(boolean selected) { 367 this.value = Boolean.valueOf(selected); 368 } 369 370 375 public void setValue(Object value) { 376 if (getType() != PropertyDefinition.TYPE_PASSWORD || !value.equals(DUMMY_PASSWORD)) { 377 this.value = value; 378 } 379 } 380 381 386 public int getType() { 387 return definition.getType(); 388 } 389 390 396 public Object getPropertyValue() { 397 if (definition.getType() == PropertyDefinition.TYPE_MULTI_ENTRY_LIST) { 398 PropertyList l = new PropertyList(); 399 l.setAsTextFieldText(getValue().toString()); 400 return l.getAsPropertyText(); 401 } else if (definition.getType() == PropertyDefinition.TYPE_BOOLEAN) { 402 String trueVal = (String ) (((List ) definition.getTypeMetaObject()).get(0)); 403 String falseVal = (String ) (((List ) definition.getTypeMetaObject()).get(1)); 404 return Boolean.TRUE.equals(getValue()) ? trueVal : falseVal; 405 } 406 return getValue(); 407 } 408 409 414 public int compareTo(ShortcutParameterItem obj) { 415 return definition.compareTo(obj.getDefinition()); 416 } 417 418 423 public class Pair { 424 Object value; 425 String label; 426 427 433 public Pair(Object value, String label) { 434 this.value = value; 435 this.label = label; 436 } 437 438 443 public Object getValue() { 444 return value; 445 } 446 447 452 public String getLabel() { 453 return label; 454 } 455 456 461 public void setValue(Object value) { 462 this.value = value; 463 } 464 465 470 public String toString() { 471 return "pair[label=" + label + ",value=" + value + "]"; 472 } 473 } 474 475 } | Popular Tags |