Coverage Report - pt.digitalis.dif.dem.objects.parameters.types.BigDecimalParameter
 
Classes in this File Line Coverage Branch Coverage Complexity
BigDecimalParameter
0%
0/31
0%
0/16
1,889
BigDecimalParameter$1
0%
0/3
N/A
1,889
 
 1  0
 /**
 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.types;
 6  
 
 7  
 import java.math.BigDecimal;
 8  
 import java.util.ArrayList;
 9  
 import java.util.Date;
 10  
 import java.util.List;
 11  
 
 12  
 import pt.digitalis.dif.controller.interfaces.IDIFContext;
 13  
 import pt.digitalis.dif.dem.interfaces.IStageInstance;
 14  
 import pt.digitalis.dif.dem.objects.parameters.constraints.IParameterConstraint;
 15  
 import pt.digitalis.dif.dem.objects.parameters.errors.ParameterError;
 16  
 import pt.digitalis.dif.dem.objects.parameters.errors.ParameterErrorList;
 17  
 import pt.digitalis.dif.dem.objects.parameters.errors.ParameterErrorType;
 18  
 import pt.digitalis.dif.exception.objects.ParameterException;
 19  
 import pt.digitalis.dif.startup.DIFGeneralConfigurationParameters;
 20  
 
 21  
 /**
 22  
  * This class will define a numeric Parameter.
 23  
  * <p>
 24  
  * It will hold information relative to the Parameter value, ID key and validation constraints.
 25  
  * 
 26  
  * @author Luis Pinto <a href="mailto:lpinto@digitalis.pt">pviegas@digitalis.pt</a>
 27  
  * @created Feb 07, 2013
 28  
  */
 29  0
 public class BigDecimalParameter extends AbstractParameter<BigDecimal> {
 30  
 
 31  
     /** The list of supported classes to define in the concrete implementations */
 32  
     @SuppressWarnings("serial")
 33  0
     final static private List<String> supportedClasses = new ArrayList<String>() {
 34  
 
 35  
         {
 36  0
             add(BigDecimal.class.getCanonicalName());
 37  
         }
 38  
     };
 39  
 
 40  
     /**
 41  
      * @see pt.digitalis.dif.dem.objects.parameters.types.AbstractParameter#automaticConstraints()
 42  
      */
 43  
     @Override
 44  
     protected String automaticConstraints()
 45  
     {
 46  0
         return "numeric";
 47  
     }
 48  
 
 49  
     /**
 50  
      * @see pt.digitalis.dif.dem.objects.parameters.IParameter#getSupportedClasses()
 51  
      */
 52  
     public List<String> getSupportedClasses()
 53  
     {
 54  0
         return supportedClasses;
 55  
     }
 56  
 
 57  
     /**
 58  
      * @see pt.digitalis.dif.dem.objects.parameters.IParameter#getValueAsBigDecimal(pt.digitalis.dif.controller.interfaces.IDIFContext)
 59  
      */
 60  
     public BigDecimal getValueAsBigDecimal(IDIFContext context) throws ParameterException
 61  
     {
 62  0
         return getValue(context);
 63  
     }
 64  
 
 65  
     /**
 66  
      * @see pt.digitalis.dif.dem.objects.parameters.IParameter#getValueAsBoolean(IDIFContext)
 67  
      */
 68  
     public boolean getValueAsBoolean(IDIFContext context) throws ParameterException
 69  
     {
 70  0
         return getValue(context).compareTo(BigDecimal.ZERO) != 0;
 71  
     }
 72  
 
 73  
     /**
 74  
      * @see pt.digitalis.dif.dem.objects.parameters.IParameter#getValueAsDate(IDIFContext)
 75  
      */
 76  
     public Date getValueAsDate(IDIFContext context)
 77  
     {
 78  0
         return null;
 79  
     }
 80  
 
 81  
     /**
 82  
      * @see pt.digitalis.dif.dem.objects.parameters.IParameter#getValueAsDouble(IDIFContext)
 83  
      */
 84  
     public Double getValueAsDouble(IDIFContext context) throws ParameterException
 85  
     {
 86  0
         return getValue(context).doubleValue();
 87  
     }
 88  
 
 89  
     /**
 90  
      * @see pt.digitalis.dif.dem.objects.parameters.IParameter#getValueAsLong(IDIFContext)
 91  
      */
 92  
     public Long getValueAsLong(IDIFContext context) throws ParameterException
 93  
     {
 94  0
         return getValue(context).longValue();
 95  
     }
 96  
 
 97  
     /**
 98  
      * @see pt.digitalis.dif.dem.objects.parameters.IParameter#isNumeric()
 99  
      */
 100  
     public boolean isNumeric()
 101  
     {
 102  0
         return true;
 103  
     }
 104  
 
 105  
     /**
 106  
      * @see pt.digitalis.dif.dem.objects.parameters.IParameter#setValueFromString(java.lang.String,
 107  
      *      pt.digitalis.dif.dem.interfaces.IStageInstance, boolean)
 108  
      */
 109  
     public ParameterErrorList setValueFromString(String value, IStageInstance stageInstance,
 110  
             boolean initializationInProgress)
 111  
     {
 112  
 
 113  0
         ParameterErrorList list = new ParameterErrorList(this, value);
 114  
         ParameterError error;
 115  
         // TODO: Create a floating point constraint. This will not pick up the floating point errors
 116  0
         IParameterConstraint constraint = getConstraints().get("numeric");
 117  
         IDIFContext context;
 118  
 
 119  0
         if (stageInstance == null)
 120  0
             context = null;
 121  
         else
 122  0
             context = stageInstance.getContext();
 123  
 
 124  
         // If the numeric constraint does not validate...
 125  0
         if (!constraint.validateConstraint(value, stageInstance))
 126  
         {
 127  
             String language;
 128  
 
 129  0
             if (context == null)
 130  0
                 language = DIFGeneralConfigurationParameters.getInstance().getDefaultLanguage();
 131  
             else
 132  0
                 language = context.getLanguage();
 133  
 
 134  0
             error = new ParameterError(constraint.validationErrorMessage(language), ParameterErrorType.CONSTRAINT);
 135  0
             error.setConstraint("numeric", constraint);
 136  
 
 137  0
             list.addError(error);
 138  
         }
 139  
         else
 140  
         {
 141  
             try
 142  
             {
 143  0
                 if (value == null || "".equals(value))
 144  0
                     return setValue(null, stageInstance, initializationInProgress);
 145  
                 else
 146  
                 {
 147  0
                     value = value.replace(',', '.').trim();
 148  
 
 149  0
                     return setValue(new BigDecimal(value), stageInstance, initializationInProgress);
 150  
                 }
 151  
             }
 152  0
             catch (Exception e)
 153  
             {
 154  
                 /*
 155  
                  * Unreachable exception: to get here Double#parseDouble() would have to throw a NumberFormatException.
 156  
                  * This exception is raised if the string does not contain a parsable double. This would happen if a bad
 157  
                  * value is passed. This situation is avoided by the constraint validation mechanism. So if a bad value
 158  
                  * is passed the code never enters the 'else' branch and as such is never possible to get here.
 159  
                  */
 160  0
                 error = new ParameterError("Error parsing value", ParameterErrorType.OTHER);
 161  0
                 error.setException(e);
 162  0
                 list.addError(error);
 163  
             }
 164  
         }
 165  0
         return list;
 166  
     }
 167  
 }