Coverage Report - pt.digitalis.dif.dem.objects.parameters.errors.ParameterErrors
 
Classes in this File Line Coverage Branch Coverage Complexity
ParameterErrors
0%
0/132
0%
0/106
5,385
 
 1  
 /**
 2  
  * 2007, Digitalis Informatica. All rights reserved. Distribuicao e Gestao de Informatica, Lda. Estrada de Paco de Arcos
 3  
  * num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999 http://www.digitalis.pt
 4  
  */
 5  
 package pt.digitalis.dif.dem.objects.parameters.errors;
 6  
 
 7  
 import java.util.ArrayList;
 8  
 import java.util.HashMap;
 9  
 import java.util.List;
 10  
 import java.util.Map;
 11  
 import java.util.Map.Entry;
 12  
 
 13  
 import pt.digitalis.dif.controller.http.HTTPConstants;
 14  
 import pt.digitalis.dif.controller.interfaces.IDIFContext;
 15  
 import pt.digitalis.dif.dem.interfaces.ICustomFormDefinition;
 16  
 import pt.digitalis.dif.dem.interfaces.IStageInstance;
 17  
 import pt.digitalis.dif.dem.managers.ICustomFormManager;
 18  
 import pt.digitalis.dif.dem.managers.IParameterManager;
 19  
 import pt.digitalis.dif.dem.objects.FeatureState;
 20  
 import pt.digitalis.dif.dem.objects.FormFieldCustomization;
 21  
 import pt.digitalis.dif.dem.objects.parameters.IEditableParameter;
 22  
 import pt.digitalis.dif.dem.objects.parameters.IParameter;
 23  
 import pt.digitalis.dif.dem.objects.parameters.types.AbstractParameter;
 24  
 import pt.digitalis.dif.exception.objects.ParameterException;
 25  
 import pt.digitalis.dif.ioc.DIFIoCRegistry;
 26  
 import pt.digitalis.dif.utils.ObjectFormatter;
 27  
 import pt.digitalis.dif.utils.extensions.ICaptcha;
 28  
 import pt.digitalis.utils.common.StringUtils;
 29  
 
 30  
 /**
 31  
  * A report object of any parameter errors that have occurred in the initialization of a stage. These will be injected
 32  
  * into a stage attribute for evaluation by the stage as the developer sees fit.
 33  
  * 
 34  
  * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a>
 35  
  * @author Rodrigo Gon�alves <a href="mailto:rgoncalves@digitalis.pt">rgoncalves@digitalis.pt</a>
 36  
  */
 37  0
 public class ParameterErrors {
 38  
 
 39  
     /** the captcha implementation */
 40  0
     private static ICaptcha captcha = null;
 41  
 
 42  
     /** A Map of all errors by parameter name. */
 43  0
     private final Map<String, ParameterErrorList> errors = new HashMap<String, ParameterErrorList>();
 44  
 
 45  
     /**
 46  
      * Liswt of parameters that the errors must be ignored, since a form customization (or any other feature that so
 47  
      * desires) has instructed the form to ignore these fields
 48  
      */
 49  0
     List<String> parametersTodiscardErrors = new ArrayList<String>();
 50  
 
 51  
     /**
 52  
      * keeps track of all refreshed parameter called by the refreshParameter method. this is used to know what
 53  
      * parameters must still be initialized for declared custom parameters
 54  
      */
 55  0
     private List<String> refreshedParameters = new ArrayList<String>();
 56  
 
 57  
     /** the current stage instance. */
 58  
     private final IStageInstance stageInstance;
 59  
 
 60  
     /**
 61  
      * Constructor.
 62  
      * 
 63  
      * @param stageInstance
 64  
      *            the current stage
 65  
      */
 66  0
     public ParameterErrors(IStageInstance stageInstance)
 67  
     {
 68  0
         this.stageInstance = stageInstance;
 69  0
     }
 70  
 
 71  
     /**
 72  
      * Adds a new parameter to.
 73  
      * 
 74  
      * @param parameterId
 75  
      *            the parameter to add the error to
 76  
      * @param error
 77  
      *            the error
 78  
      * @throws ParameterException
 79  
      *             if the parameter does not exist
 80  
      */
 81  
     public void addParameterError(String parameterId, ParameterError error) throws ParameterException
 82  
     {
 83  
         // Must ignore errors for parameters that the form configuration has determined as invisible!
 84  0
         if (!parametersTodiscardErrors.contains(parameterId))
 85  
         {
 86  0
             if (parameterId != null)
 87  0
                 parameterId = parameterId.toLowerCase();
 88  
 
 89  0
             ParameterErrorList errorsForParameter = getErrorsForParameter(parameterId);
 90  
 
 91  0
             if (errorsForParameter == null)
 92  
             {
 93  0
                 IParameter<?> parameter = null;
 94  0
                 Object parameterValue = null;
 95  
 
 96  0
                 if (parameterId != null && !"".equals(parameterId))
 97  
                 {
 98  
                     // Error for a specific parameter
 99  0
                     parameter = DIFIoCRegistry.getRegistry().getImplementation(IParameterManager.class)
 100  0
                             .getParameters(stageInstance).getParameter(parameterId);
 101  
 
 102  0
                     if (parameter == null)
 103  
                         // Changed so that custom stage instance parameters are picked up here...
 104  0
                         parameter = stageInstance.getParameters().getStageParameters().getParameter(parameterId);
 105  
 
 106  0
                     if (parameter == null)
 107  0
                         throw new ParameterException("Parameter \"" + parameterId + "\" does not exist in the stage \""
 108  0
                                 + stageInstance.getID() + "\" context.", parameter);
 109  
 
 110  
                     try
 111  
                     {
 112  0
                         parameterValue = parameter.getValue(stageInstance.getContext());
 113  
                     }
 114  0
                     catch (ParameterException e)
 115  
                     {
 116  
                         // Nothing to do!
 117  
                     }
 118  
                 }
 119  
 
 120  0
                 errorsForParameter = new ParameterErrorList(parameter, parameterValue);
 121  0
                 errors.put(parameterId, errorsForParameter);
 122  
             }
 123  
 
 124  0
             errorsForParameter.addError(error);
 125  
         }
 126  0
     }
 127  
 
 128  
     /**
 129  
      * Discards all errors.
 130  
      */
 131  
     public void discardAllErrors()
 132  
     {
 133  0
         errors.clear();
 134  0
         stageInstance.setParameterErrors(this);
 135  0
     }
 136  
 
 137  
     /**
 138  
      * Discards all errors for a specific parameter.
 139  
      * 
 140  
      * @param parameterId
 141  
      *            the parameter to discard errors
 142  
      */
 143  
     public void discardErrors(String parameterId)
 144  
     {
 145  0
         errors.remove(parameterId.toLowerCase());
 146  0
         stageInstance.setParameterErrors(this);
 147  0
     }
 148  
 
 149  
     /**
 150  
      * Discards all errors for a specific parameter.
 151  
      * 
 152  
      * @param type
 153  
      *            the parameter type of errors to discard
 154  
      */
 155  
     public void discardErrorsOfType(ParameterErrorType type)
 156  
     {
 157  0
         for (Entry<String, ParameterErrorList> listEntry: errors.entrySet())
 158  
         {
 159  
 
 160  0
             ParameterErrorList value = listEntry.getValue();
 161  0
             List<ParameterError> previousErrorList = value.getErrorList();
 162  0
             List<ParameterError> newErrorList = new ArrayList<ParameterError>();
 163  
 
 164  0
             for (ParameterError error: previousErrorList)
 165  0
                 if (!error.getErrorType().equals(type))
 166  0
                     newErrorList.add(error);
 167  
 
 168  0
             value.setErrorList(newErrorList);
 169  0
             errors.put(listEntry.getKey(), value);
 170  
         }
 171  
 
 172  0
         stageInstance.setParameterErrors(this);
 173  0
     }
 174  
 
 175  
     /**
 176  
      * Get all errors for all parameters.
 177  
      * 
 178  
      * @return the errors
 179  
      */
 180  
     public Map<String, ParameterErrorList> getAllParameterErrors()
 181  
     {
 182  0
         return errors;
 183  
     }
 184  
 
 185  
     /**
 186  
      * Retrieves the errors for a given parameter ID.
 187  
      * 
 188  
      * @param parameterID
 189  
      *            the desired parameter id
 190  
      * @return the parameter list
 191  
      */
 192  
     public ParameterErrorList getErrorsForParameter(String parameterID)
 193  
     {
 194  0
         if (parameterID == null)
 195  0
             return null;
 196  
         else
 197  0
             return errors.get(parameterID.toLowerCase());
 198  
     }
 199  
 
 200  
     /**
 201  
      * Checks if errors exist.
 202  
      * 
 203  
      * @return T if there are errors
 204  
      */
 205  
     public boolean hasErrors()
 206  
     {
 207  0
         for (ParameterErrorList list: errors.values())
 208  
         {
 209  0
             if (list.getErrorList().size() > 0)
 210  0
                 return true;
 211  
         }
 212  
 
 213  0
         return false;
 214  
     }
 215  
 
 216  
     /**
 217  
      * Checks if errors exist.
 218  
      * 
 219  
      * @param formName
 220  
      *            the form name
 221  
      * @return T if there are errors
 222  
      */
 223  
     public boolean hasErrorsForForm(String formName)
 224  
     {
 225  0
         for (ParameterErrorList list: errors.values())
 226  
         {
 227  0
             if (list.getParameter() != null && formName.equalsIgnoreCase(list.getParameter().getFormLinked())
 228  0
                     && list.getErrorList() != null && list.getErrorList().size() > 0)
 229  0
                 return true;
 230  
         }
 231  
 
 232  0
         return false;
 233  
     }
 234  
 
 235  
     /**
 236  
      * Checks if errors exist. Will ignore all errors related with partial submit of a form. Required fields not
 237  
      * submited. But validating errors in the fields that were filled.
 238  
      * 
 239  
      * @return T if there are errors
 240  
      */
 241  
     public boolean hasErrorsIgnoreParcialSubmitErrors()
 242  
     {
 243  0
         for (ParameterErrorList list: errors.values())
 244  0
             for (ParameterError error: list.getErrorList())
 245  
 
 246  
                 // If a non MISSING error is found, there are non-partioal-submit errors
 247  0
                 if (!ParameterErrorType.MISSING.equals(error.getErrorType()))
 248  0
                     return true;
 249  
 
 250  0
         return false;
 251  
     }
 252  
 
 253  
     /**
 254  
      * Refreshes the passed parameter and adds errors to the list if they are detected.
 255  
      * 
 256  
      * @param param
 257  
      *            the parameter to refresh
 258  
      * @param stageInstance
 259  
      *            the stage of the refresh request
 260  
      * @return the refreshed value
 261  
      */
 262  
     public Object refreshParameter(IParameter<?> param, IStageInstance stageInstance)
 263  
     {
 264  0
         Object result = null;
 265  0
         this.refreshedParameters.add(param.getId().toLowerCase());
 266  
 
 267  0
         if (stageInstance != null)
 268  
         {
 269  0
             IDIFContext context = stageInstance.getContext();
 270  
 
 271  
             // Get custom form configuration if present
 272  0
             ICustomFormDefinition customFormDef = null;
 273  0
             FormFieldCustomization customFormFieldDef = null;
 274  0
             Object currentFormNameObj = context.getRequest().getParameter(HTTPConstants.FORM_SUBMIT_STAGE);
 275  0
             boolean stageContainsForm = context.getStage().equalsIgnoreCase(
 276  0
                     currentFormNameObj == null ? null : currentFormNameObj.toString());
 277  
 
 278  0
             boolean parameterIsFromForm = false;
 279  
 
 280  0
             String paramFormName = param.getFormLinked();
 281  0
             String formSubmitName = (String) context.getRequest().getParameter(HTTPConstants.FORM_SUBMIT_NAME);
 282  0
             String formConfigID = (String) context.getRequest().getParameter(
 283  0
                     HTTPConstants.FORM_SUBMIT__CONFIG_BUSINESS_ID);
 284  
 
 285  0
             if (paramFormName != null && formSubmitName != null)
 286  0
                 parameterIsFromForm = paramFormName.equals(formSubmitName);
 287  
 
 288  
             // Form parameter that has a configuration active (as submited by the form)
 289  0
             if (parameterIsFromForm && param.isFormConfigurable())
 290  
             {
 291  0
                 customFormDef = DIFIoCRegistry.getRegistry().getImplementation(ICustomFormManager.class)
 292  0
                         .getConfiguration(stageInstance, formConfigID);
 293  
 
 294  0
                 if (customFormDef != null)
 295  
                 {
 296  0
                     customFormFieldDef = customFormDef.getCustomizedParameters().get(param.getId().toLowerCase());
 297  
 
 298  0
                     if (customFormFieldDef != null && customFormFieldDef.getMandatory() != FeatureState.DEFAULT)
 299  0
                         ((IEditableParameter) param).setRequired(customFormFieldDef.getMandatory() == FeatureState.ON);
 300  
                 }
 301  
             }
 302  
 
 303  
             ParameterErrorList list;
 304  
 
 305  
             // Refresh the value and collect any errors that may occur
 306  0
             list = param.refreshParameterValue(stageInstance);
 307  
 
 308  
             try
 309  
             {
 310  0
                 result = param.getValue(context);
 311  
 
 312  
             }
 313  0
             catch (ParameterException e)
 314  
             {
 315  0
                 ParameterError error = new ParameterError(e.getMessage(), ParameterErrorType.OTHER);
 316  0
                 list.addError(error);
 317  
             }
 318  
 
 319  
             // Form parameter that has a configuration active (as submited by the form)
 320  0
             if (customFormDef != null && customFormDef.getExcludedParameters().contains(param.getId()))
 321  
             {
 322  0
                 list.getErrorList().clear();
 323  0
                 parametersTodiscardErrors.add(param.getId());
 324  
             }
 325  
 
 326  0
             if (list.getErrorList().size() > 0)
 327  
             {
 328  
                 // If the parameter is not link to any form or is linked to form that was submitted, add errors if they
 329  
                 // exist
 330  0
                 if (param.getFormLinked() == null || stageContainsForm && parameterIsFromForm)
 331  
                 {
 332  0
                     errors.put(param.getId(), list);
 333  
                 }
 334  
                 else
 335  
                 {
 336  
                     // Search reported error to see which errors force reporting
 337  0
                     ParameterErrorList forcedErrors = new ParameterErrorList(list.getParameter(),
 338  0
                             list.getAttemptedValue());
 339  
 
 340  0
                     for (ParameterError error: list.getErrorList())
 341  0
                         if (error.isForceShowError())
 342  0
                             forcedErrors.addError(error);
 343  
 
 344  0
                     if (forcedErrors.getErrorList().size() > 0)
 345  
                     {
 346  0
                         errors.put(param.getId(), forcedErrors);
 347  
                     }
 348  
                 }
 349  
             }
 350  
         }
 351  
 
 352  0
         return result;
 353  
     }
 354  
 
 355  
     /**
 356  
      * Refreshes all remaining parameters and adds errors to the list if they are detected.
 357  
      * 
 358  
      * @param stageInstance
 359  
      *            the stage of the refresh request
 360  
      * @throws ParameterException
 361  
      */
 362  
     public void refreshParameters(IStageInstance stageInstance) throws ParameterException
 363  
     {
 364  0
         for (IParameter<?> parameter: stageInstance.getParameters().getStageParameters().getParameters().values())
 365  0
             if (!this.refreshedParameters.contains(parameter.getId().toLowerCase()))
 366  0
                 this.refreshParameter(parameter, stageInstance);
 367  
 
 368  0
         if (stageInstance.isFeatureEnabled(ICaptcha.CAPTCHA_PRESENT))
 369  
         {
 370  
             // Has captcha, must validate it for the given form is any was submited
 371  0
             Object submitStage = stageInstance.getContext().getRequest().getParameter(HTTPConstants.FORM_SUBMIT_STAGE);
 372  0
             Object submitForm = stageInstance.getContext().getRequest().getParameter(HTTPConstants.FORM_SUBMIT_NAME);
 373  
 
 374  0
             if (submitStage != null && stageInstance.getID().equalsIgnoreCase(submitStage.toString())
 375  0
                     && submitForm != null
 376  0
                     && stageInstance.isFeatureEnabled(ICaptcha.CAPTCHA_PRESENT + ":" + submitForm.toString()))
 377  
             {
 378  0
                 if (captcha == null)
 379  0
                     captcha = DIFIoCRegistry.getRegistry().getImplementation(ICaptcha.class);
 380  
 
 381  0
                 if (!captcha.isCaptchaValueValid(stageInstance.getContext()))
 382  
                 {
 383  
 
 384  0
                     if (StringUtils.isBlank(captcha.getCaptchaSubmitedValue(stageInstance.getContext())))
 385  
                     {
 386  0
                         if (this.getErrorsForParameter(ICaptcha.CAPTCHA_INPUT_ID).getErrorList().isEmpty())
 387  0
                             this.addParameterError(ICaptcha.CAPTCHA_INPUT_ID, new ParameterError(AbstractParameter
 388  0
                                     .getMessages().getMessages(stageInstance.getContext().getLanguage())
 389  0
                                     .get("required"), ParameterErrorType.MISSING));
 390  
                     }
 391  
                     else
 392  0
                         this.addParameterError(
 393  0
                                 ICaptcha.CAPTCHA_INPUT_ID,
 394  0
                                 new ParameterError(AbstractParameter.getMessages()
 395  0
                                         .getMessages(stageInstance.getContext().getLanguage()).get("captchaInvalid"),
 396  0
                                         ParameterErrorType.OTHER));
 397  
                 }
 398  
             }
 399  
         }
 400  0
     }
 401  
 
 402  
     /**
 403  
      * @see java.lang.Object#toString()
 404  
      */
 405  
     @Override
 406  
     public String toString()
 407  
     {
 408  0
         ObjectFormatter formatter = new ObjectFormatter();
 409  0
         formatter.addItem("Errors", errors);
 410  
 
 411  0
         return formatter.getFormatedObject();
 412  
     }
 413  
 }