1 19 20 package com.sslexplorer.policyframework.forms; 21 22 import java.util.Iterator ; 23 24 import javax.servlet.http.HttpServletRequest ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.apache.struts.Globals; 29 import org.apache.struts.action.ActionErrors; 30 import org.apache.struts.action.ActionMapping; 31 32 import com.sslexplorer.boot.PropertyList; 33 import com.sslexplorer.core.BundleActionMessage; 34 import com.sslexplorer.core.forms.CoreForm; 35 import com.sslexplorer.input.MultiSelectSelectionModel; 36 import com.sslexplorer.policyframework.Resource; 37 import com.sslexplorer.security.LogonControllerFactory; 38 import com.sslexplorer.security.SessionInfo; 39 import com.sslexplorer.security.User; 40 import com.sslexplorer.tabs.TabModel; 41 42 49 public abstract class AbstractResourceForm extends CoreForm implements TabModel { 50 static Log log = LogFactory.getLog(AbstractResourceForm.class); 51 52 protected String resourceName, resourceDescription; 53 protected int resourceId; 54 protected MultiSelectSelectionModel policyModel; 55 protected PropertyList selectedPolicies; 56 protected User owner, user; 57 protected String originalName; 58 protected Resource resource; 59 protected boolean readOnly; 60 protected boolean assignOnly; 61 62 65 public AbstractResourceForm() { 66 selectedPolicies = new PropertyList(); 67 } 68 69 77 public abstract Resource getResourceByName(String resourceName, SessionInfo session) throws Exception ; 78 79 84 public int getResourceId() { 85 return resourceId; 86 } 87 88 93 public void setResourceID(int resourceId) { 94 this.resourceId = resourceId; 95 } 96 97 108 public void initialise(User user, Resource resource, boolean editing, MultiSelectSelectionModel policyModel, PropertyList selectedPolicies, User owner, boolean assignOnly) throws Exception { 109 this.resource = resource; 110 originalName = resource.getResourceName(); 111 setResourceID(resource.getResourceId()); 112 setResourceName(resource.getResourceName()); 113 setResourceDescription(resource.getResourceDescription()); 114 if(editing) { 115 setEditing(); 116 } 117 else { 118 setCreating(); 119 } 120 setPolicyModel(policyModel); 121 this.selectedPolicies = selectedPolicies; 122 this.owner = owner; 123 this.user = user; 124 this.assignOnly = assignOnly; 125 } 126 127 133 public User getOwner() { 134 return owner; 135 } 136 137 142 public User getUser() { 143 return user; 144 } 145 146 151 public Resource getResource() { 152 return resource; 153 } 154 155 160 public String getResourceDescription() { 161 return resourceDescription; 162 } 163 164 169 public void setResourceDescription(String resourceDescription) { 170 this.resourceDescription = resourceDescription.trim(); 171 } 172 173 178 public String getResourceName() { 179 return resourceName; 180 } 181 182 185 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { 186 ActionErrors errs = new ActionErrors(); 187 if (isCommiting()) { 188 if (getResourceName() == null || getResourceName().equals("")) { 189 errs.add(Globals.ERROR_KEY, new BundleActionMessage("policyframework", "error.createResource.missingName")); 190 } 191 if (getResourceName().length() > Resource.MAX_RESOURCE_NAME_LENGTH) { 192 errs.add(Globals.ERROR_KEY, new BundleActionMessage("policyframework", "error.createResource.resourceNameTooLong", String.valueOf(Resource.MAX_RESOURCE_NAME_LENGTH))); 193 } 194 if (getResourceDescription().equals("")) { 195 errs.add(Globals.ERROR_KEY, new BundleActionMessage("policyframework", "error.createResource.missingDescription", String.valueOf(Resource.MAX_RESOURCE_NAME_LENGTH))); 196 } 197 if(!getEditing() || !originalName.equals(getResourceName())) { 198 try { 199 Resource r = getResourceByName(getResourceName(), LogonControllerFactory.getInstance().getSessionInfo(request)); 200 if(r != null) { 201 errs.add(Globals.ERROR_KEY, new BundleActionMessage("policyframework", "error.createResource.resourceNameInUse", getResourceName())); 202 } 203 } 204 catch(Exception e) { 205 errs.add(Globals.ERROR_KEY, new BundleActionMessage("policyframework", "error.createResource.failedToDetermineIfResourceExists", e.getMessage())); 206 } 207 } 208 211 for(Iterator i = selectedPolicies.iterator(); i.hasNext(); ) { 212 String pol = (String )i.next(); 213 if(!policyModel.contains(pol)) { 214 throw new Error ("User doesn't have permission to select the policy '" + pol + "', this shouldn't happen."); 215 } 216 } 217 } 218 return errs; 219 } 220 221 226 public void setResourceName(String resourceName) { 227 this.resourceName = resourceName.trim(); 228 } 229 230 236 public String getSelectedPolicies() { 237 return selectedPolicies.getAsTextFieldText(); 238 } 239 240 246 public void setSelectedPolicies(String selectedPolicies) { 247 this.selectedPolicies.setAsTextFieldText(selectedPolicies); 248 } 249 250 255 public MultiSelectSelectionModel getPolicyModel() { 256 return policyModel; 257 } 258 259 264 public void setPolicyModel(MultiSelectSelectionModel policyModel) { 265 this.policyModel = policyModel; 266 } 267 268 273 public PropertyList getSelectedPoliciesList() { 274 return selectedPolicies; 275 } 276 277 282 public boolean getReadOnly() { 283 return readOnly; 284 } 285 286 290 public void setReadOnly() { 291 readOnly = true; 292 } 293 294 298 public void setWriteable() { 299 readOnly = false; 300 } 301 302 307 public void apply() throws Exception { 308 if(getReadOnly()) { 309 throw new Exception ("Read only"); 310 } 311 resource.setResourceName(getResourceName()); 312 resource.setResourceDescription(getResourceDescription()); 313 applyToResource(); 314 } 315 316 322 public abstract void applyToResource() throws Exception ; 323 324 327 public boolean isAssignOnly() { 328 return assignOnly; 329 } 330 331 334 public void setAssignOnly(boolean assignOnly) { 335 this.assignOnly = assignOnly; 336 } 337 } | Popular Tags |