1 19 20 package com.sslexplorer.properties.attributes.forms; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 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.PropertyDefinition; 37 import com.sslexplorer.core.CoreException; 38 import com.sslexplorer.core.forms.CoreForm; 39 import com.sslexplorer.properties.attributes.AttributeValueItem; 40 import com.sslexplorer.tabs.TabModel; 41 42 48 public class UserAttributesForm extends CoreForm implements TabModel { 49 50 static Log log = LogFactory.getLog(UserAttributesForm.class); 51 52 54 private List <AttributeValueItem> userAttributeValueItems; 55 private String selectedTab; 56 private List <String > categoryIds; 57 private List <String > categoryTitles; 58 59 67 public void initialize(List <AttributeValueItem> userAttributeValueItems) throws Exception { 68 this.userAttributeValueItems = userAttributeValueItems; 69 categoryIds = new ArrayList <String >(); 70 categoryTitles = new ArrayList <String >(); 71 for (Iterator i = userAttributeValueItems.iterator(); i.hasNext();) { 72 AttributeValueItem item = (AttributeValueItem) i.next(); 73 int idx = categoryIds.indexOf(item.getCategoryId()); 74 if (idx == -1) { 75 categoryIds.add(item.getCategoryId()); 76 categoryTitles.add(item.getCategoryLabel()); 77 } 78 } 79 selectedTab = categoryIds.size() > 0 ? categoryIds.get(0) : ""; 80 } 81 82 87 public List getCategoryIds() { 88 return categoryIds; 89 } 90 91 97 public List getUserAttributeValueItems() { 98 return userAttributeValueItems; 99 } 100 101 106 public int getTabCount() { 107 return categoryIds.size(); 108 } 109 110 115 public String getTabName(int idx) { 116 return (String ) categoryIds.get(idx); 117 } 118 119 124 public String getSelectedTab() { 125 return selectedTab; 126 } 127 128 133 public void setSelectedTab(String selectedTab) { 134 this.selectedTab = selectedTab; 135 } 136 137 142 public String getTabTitle(int idx) { 143 return (String ) categoryTitles.get(idx); 144 } 145 146 151 public String getTabBundle(int idx) { 152 return null; 153 } 154 155 161 public void reset(ActionMapping mapping, HttpServletRequest request) { 162 super.reset(mapping, request); 163 if (userAttributeValueItems != null) { 164 for (Iterator i = userAttributeValueItems.iterator(); i.hasNext();) { 165 AttributeValueItem item = (AttributeValueItem) i.next(); 166 if (item.getDefinition().getType() == PropertyDefinition.TYPE_BOOLEAN) { 167 item.setSelected(false); 168 } 169 } 170 } 171 } 172 173 179 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { 180 if (isCommiting()) { 181 ActionErrors errs = null; 182 for (Iterator i = userAttributeValueItems.iterator(); i.hasNext();) { 183 AttributeValueItem item = (AttributeValueItem) i.next(); 184 PropertyDefinition def = item.getDefinition(); 185 try { 186 def.validate(item.getValue().toString(), getClass().getClassLoader()); 187 } catch (CoreException ce) { 188 ce.getBundleActionMessage().setArg3(item.getLabel()); 189 if (errs == null) { 190 errs = new ActionErrors(); 191 } 192 errs.add(Globals.ERROR_KEY, ce.getBundleActionMessage()); 193 } catch (Exception e) { 194 errs.add(Globals.ERROR_KEY, new ActionMessage("userAttributes.error.failedToValidate", e.getMessage())); 195 } 196 } 197 if (errs != null) 198 return errs; 199 } 200 return super.validate(mapping, request); 201 } 202 } 203 | Popular Tags |