1 19 20 package com.sslexplorer.properties.attributes; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.StringTokenizer ; 26 27 import javax.servlet.http.HttpServletRequest ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.struts.util.MessageResources; 32 33 import com.sslexplorer.boot.PropertyDefinition; 34 import com.sslexplorer.boot.TypeMetaListItem; 35 import com.sslexplorer.boot.Util; 36 import com.sslexplorer.core.CoreUtil; 37 import com.sslexplorer.properties.Pair; 38 import com.sslexplorer.properties.PairListDataSource; 39 40 45 public class AttributeValueItem implements Comparable { 46 47 final static Log log = LogFactory.getLog(AttributeValueItem.class); 48 49 private AttributeDefinition definition; 50 private String label; 51 private Object value; 52 private String categoryLabel; 53 private String categoryId; 54 private int rows, columns; 55 private Pair[] listItems; 56 57 64 public AttributeValueItem(AttributeDefinition definition, HttpServletRequest request, String value) { 65 super(); 66 67 MessageResources messageResources = null; 68 if (definition.getMessageResourcesKey() != null) { 69 messageResources = CoreUtil.getMessageResources(request.getSession(), definition.getMessageResourcesKey()); 70 } 71 72 this.definition = definition; 73 this.label = definition.getLabel(); 74 75 this.categoryLabel = definition.getCategoryLabel(); 77 String s = messageResources == null ? null : messageResources.getMessage("attributeCategory." + definition.getCategory() 78 + ".title"); 79 if (s != null && !s.equals("")) { 80 this.categoryLabel = s; 81 } else { 82 if (categoryLabel == null || categoryLabel.equals("")) { 83 this.categoryLabel = "Attributes"; 84 } 85 } 86 87 s = messageResources == null ? null : messageResources.getMessage("attribute." + definition.getName() + ".title"); 90 if (s != null && !s.equals("")) { 91 label = s; 92 } else { 93 if (label != null && label.equals("")) { 94 label = definition.getName(); 95 } 96 } 97 if (getCategoryLabel() != null) 98 categoryId = Util.makeConstantKey(getCategoryLabel()); 99 100 this.value = getDefinition().parseValue(value); 102 103 if (definition.getType() == PropertyDefinition.TYPE_TEXT_AREA) { 105 this.value = value; 106 StringTokenizer t = new StringTokenizer (definition.getTypeMeta().equals("") ? "25x5" : definition.getTypeMeta(), "x"); 107 try { 108 columns = Integer.parseInt(t.nextToken()); 109 rows = Integer.parseInt(t.nextToken()); 110 } catch (Exception e) { 111 } 112 } else if (definition.getType() == PropertyDefinition.TYPE_LIST) { 113 List <Pair> listItemsList = new ArrayList <Pair>(); 114 if (!definition.getTypeMeta().startsWith("!")) { 115 for (Iterator i = ((List ) definition.getTypeMetaObject()).iterator(); i.hasNext();) { 116 TypeMetaListItem item = (TypeMetaListItem) i.next(); 117 Pair pair = new Pair(item.getValue(), item.getValue()); 118 if (item.getValue().equals(value)) { 119 this.value = pair.getValue(); 120 } 121 listItemsList.add(pair); 122 } 123 } 124 else { 125 String className = definition.getTypeMeta().substring(1); 126 try { 127 Class clazz = Class.forName(className); 128 Object obj = clazz.newInstance(); 129 if(obj instanceof PairListDataSource) 130 listItemsList.addAll(((PairListDataSource)obj).getValues(request)); 131 else 132 throw new Exception ("Not a PairListDataSource."); 133 } 134 catch(Exception e) { 135 log.error("Failed to create list data source.", e); 136 } 137 this.value = value; 138 } 139 listItems = new Pair[listItemsList.size()]; 140 listItemsList.toArray(listItems); 141 } else if (definition.getType() == PropertyDefinition.TYPE_STRING) { 142 columns = 25; 143 if (!definition.getTypeMeta().equals("")) { 144 try { 145 columns = Integer.parseInt(definition.getTypeMeta()); 146 } catch (NumberFormatException nfe) { 147 } 148 } 149 } else if (definition.getType() == PropertyDefinition.TYPE_INTEGER) { 150 columns = 8; 151 if (!definition.getTypeMeta().equals("")) { 152 try { 153 columns = Integer.parseInt(definition.getTypeMeta()); 154 } catch (NumberFormatException e) { 155 } 156 } 157 } else if (definition.getType() == PropertyDefinition.TYPE_PASSWORD) { 158 columns = 25; 159 if (!definition.getTypeMeta().equals("")) { 160 try { 161 columns = Integer.parseInt(definition.getTypeMeta()); 162 } catch (NumberFormatException nfe) { 163 } 164 } 165 } 166 } 167 168 173 public Pair[] getListItems() { 174 return listItems; 175 } 176 177 183 184 public String getLabel() { 185 return label; 186 } 187 188 193 public AttributeDefinition getDefinition() { 194 return definition; 195 } 196 197 202 public Object getValue() { 203 return value; 204 } 205 206 211 public void setValue(Object value) { 212 this.value = value; 213 } 214 215 220 public String getCategoryLabel() { 221 return categoryLabel; 222 } 223 224 229 public String getCategoryId() { 230 return categoryId; 231 } 232 233 240 public int compareTo(Object arg0) { 241 int i = (categoryId == null ? "" : categoryId).compareTo(((AttributeValueItem) arg0).categoryId == null ? "" 242 : ((AttributeValueItem) arg0).categoryId); 243 if (i == 0) { 244 i = new Integer (getDefinition().getSortOrder()).compareTo(new Integer (((AttributeValueItem) arg0).getDefinition() 245 .getSortOrder())); 246 return i == 0 ? (getDefinition().getName().compareTo(((AttributeValueItem) arg0).getDefinition().getName())) : i; 247 } else { 248 return i; 249 } 250 } 251 252 260 public int getColumns() { 261 return columns; 262 } 263 264 271 public int getRows() { 272 return rows; 273 } 274 275 281 public boolean getSelected() { 282 return value.equals(Boolean.TRUE); 283 } 284 285 291 public void setSelected(boolean selected) { 292 this.value = Boolean.valueOf(selected); 293 } 294 } 295 | Popular Tags |