Coverage Report - pt.digitalis.dif.dem.objects.parameters.types.LongParameter
 
Classes in this File Line Coverage Branch Coverage Complexity
LongParameter
0%
0/34
0%
0/20
2,111
LongParameter$1
0%
0/3
N/A
2,111
 
 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 Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a>
 27  
  * @author Rodrigo Gonçalves <a href="mailto:rgoncalves@digitalis.pt">rgoncalves@digitalis.pt</a>
 28  
  * @created Nov 23, 2007
 29  
  */
 30  0
 public class LongParameter extends AbstractParameter<Number> {
 31  
 
 32  
     /** The list of supported classes to define in the concrete implementations */
 33  
     @SuppressWarnings("serial")
 34  0
     final static private List<String> supportedClasses = new ArrayList<String>() {
 35  
 
 36  
         {
 37  0
             add(Long.class.getCanonicalName());
 38  
         }
 39  
     };
 40  
 
 41  
     /**
 42  
      * @see pt.digitalis.dif.dem.objects.parameters.types.AbstractParameter#automaticConstraints()
 43  
      */
 44  
     @Override
 45  
     protected String automaticConstraints()
 46  
     {
 47  0
         return "numeric";
 48  
     }
 49  
 
 50  
     /**
 51  
      * @see pt.digitalis.dif.dem.objects.parameters.IParameter#getSupportedClasses()
 52  
      */
 53  
     public List<String> getSupportedClasses()
 54  
     {
 55  0
         return supportedClasses;
 56  
     }
 57  
 
 58  
     /**
 59  
      * @see pt.digitalis.dif.dem.objects.parameters.IParameter#getValueAsBigDecimal(pt.digitalis.dif.controller.interfaces.IDIFContext)
 60  
      */
 61  
     public BigDecimal getValueAsBigDecimal(IDIFContext context) throws ParameterException
 62  
     {
 63  0
         return BigDecimal.valueOf(getValueAsLong(context));
 64  
     }
 65  
 
 66  
     /**
 67  
      * @see pt.digitalis.dif.dem.objects.parameters.IParameter#getValueAsBoolean(IDIFContext)
 68  
      */
 69  
     public boolean getValueAsBoolean(IDIFContext context) throws ParameterException
 70  
     {
 71  0
         return !getValueAsLong(context).equals(0L);
 72  
     }
 73  
 
 74  
     /**
 75  
      * @see pt.digitalis.dif.dem.objects.parameters.IParameter#getValueAsDate(IDIFContext)
 76  
      */
 77  
     public Date getValueAsDate(IDIFContext context)
 78  
     {
 79  0
         return null;
 80  
     }
 81  
 
 82  
     /**
 83  
      * @see pt.digitalis.dif.dem.objects.parameters.IParameter#getValueAsDouble(IDIFContext)
 84  
      */
 85  
     public Double getValueAsDouble(IDIFContext context) throws ParameterException
 86  
     {
 87  0
         return getValueAsLong(context).doubleValue();
 88  
     }
 89  
 
 90  
     /**
 91  
      * @see pt.digitalis.dif.dem.objects.parameters.IParameter#getValueAsLong(IDIFContext)
 92  
      */
 93  
     public Long getValueAsLong(IDIFContext context) throws ParameterException
 94  
     {
 95  0
         return (Long) getValue(context);
 96  
     }
 97  
 
 98  
     /**
 99  
      * @see pt.digitalis.dif.dem.objects.parameters.IParameter#isNumeric()
 100  
      */
 101  
     public boolean isNumeric()
 102  
     {
 103  0
         return true;
 104  
     }
 105  
 
 106  
     /**
 107  
      * @see pt.digitalis.dif.dem.objects.parameters.IParameter#setValueFromString(java.lang.String,
 108  
      *      pt.digitalis.dif.dem.interfaces.IStageInstance, boolean)
 109  
      */
 110  
     public ParameterErrorList setValueFromString(String value, IStageInstance stageInstance,
 111  
             boolean initializationInProgress)
 112  
     {
 113  
 
 114  0
         ParameterErrorList list = new ParameterErrorList(this, value);
 115  
         ParameterError error;
 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  0
             error = new ParameterError(constraint.validationErrorMessage(language), ParameterErrorType.CONSTRAINT);
 134  0
             error.setConstraint("numeric", constraint);
 135  
 
 136  0
             list.addError(error);
 137  
         }
 138  
         else
 139  
         {
 140  
             try
 141  
             {
 142  0
                 if (value == null || "".equals(value) || "undefined".equals(value))
 143  0
                     return setValue(null, stageInstance, initializationInProgress);
 144  
                 else
 145  0
                     return setValue(Long.parseLong(value.trim()), stageInstance, initializationInProgress);
 146  
 
 147  
             }
 148  0
             catch (Exception e)
 149  
             {
 150  
                 /*
 151  
                  * Unreachable exception: to get here Long#parseLong() would have to throw a NumberFormatException. This
 152  
                  * exception is raised if the string does not contain a parsable double. This would happen if a bad
 153  
                  * value is passed. This situation is avoided by the constraint validation mechanism. So if a bad value
 154  
                  * is passed the code never enters the 'else' branch and as such is never possible to get here.
 155  
                  */
 156  
                 String language;
 157  
 
 158  0
                 if (context == null)
 159  0
                     language = DIFGeneralConfigurationParameters.getInstance().getDefaultLanguage();
 160  
                 else
 161  0
                     language = context.getLanguage();
 162  
 
 163  0
                 error = new ParameterError(getMessages().getMessages(language).get("valueTooLong"),
 164  0
                         ParameterErrorType.OTHER);
 165  0
                 error.setException(e);
 166  0
                 list.addError(error);
 167  
             }
 168  
         }
 169  
 
 170  0
         return list;
 171  
     }
 172  
 }