Coverage Report - pt.digitalis.dif.dem.objects.parameters.rules.AbstractParameterRule
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractParameterRule
0%
0/77
0%
0/32
1,842
 
 1  0
 /**
 2  
  * 2008, 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  
 
 6  
 package pt.digitalis.dif.dem.objects.parameters.rules;
 7  
 
 8  
 import java.util.ArrayList;
 9  
 import java.util.Arrays;
 10  
 import java.util.List;
 11  
 import java.util.Map;
 12  
 
 13  
 import pt.digitalis.dif.dem.annotations.AnnotationTags;
 14  
 import pt.digitalis.dif.dem.interfaces.IStageInstance;
 15  
 import pt.digitalis.dif.dem.managers.IMessageManager;
 16  
 import pt.digitalis.dif.dem.objects.parameters.IParameter;
 17  
 import pt.digitalis.dif.exception.objects.ParameterException;
 18  
 import pt.digitalis.dif.ioc.DIFIoCRegistry;
 19  
 import pt.digitalis.dif.utils.ObjectFormatter;
 20  
 import pt.digitalis.utils.common.StringUtils;
 21  
 
 22  
 /**
 23  
  * A base implementation for all parameter rules
 24  
  * 
 25  
  * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a><br/>
 26  
  * @param <T>
 27  
  *            the parameter base type
 28  
  * @created Mar 31, 2009
 29  
  */
 30  0
 public abstract class AbstractParameterRule<T> implements IParameterRule<T> {
 31  
 
 32  
     /** The rule action */
 33  
     private ParameterRuleAction action;
 34  
 
 35  
     /** The description for end value */
 36  0
     private String descriptionEndValue = null;
 37  
 
 38  
     /** The description for start value */
 39  0
     private String descriptionStartValue = null;
 40  
 
 41  
     /** The description value */
 42  0
     private String descriptionValue = null;
 43  
 
 44  
     /** The end value for validations */
 45  0
     private String endValue = null;
 46  
 
 47  
     /** The parameter that this rule is associated to */
 48  
     protected String parameterID;
 49  
 
 50  
     /** the rule parameters */
 51  0
     private List<String> parameters = new ArrayList<String>();
 52  
 
 53  
     /** The start value for validations */
 54  0
     private String startValue = null;
 55  
 
 56  
     /** The value for validation */
 57  0
     private String value = null;
 58  
 
 59  
     /**
 60  
      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#getAction()
 61  
      */
 62  
     public ParameterRuleAction getAction()
 63  
     {
 64  0
         return action;
 65  
     }
 66  
 
 67  
     /**
 68  
      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#getDescriptionEndValue()
 69  
      */
 70  
     public String getDescriptionEndValue()
 71  
     {
 72  0
         return descriptionEndValue;
 73  
     }
 74  
 
 75  
     /**
 76  
      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#getDescriptionStartValue()
 77  
      */
 78  
     public String getDescriptionStartValue()
 79  
     {
 80  0
         return descriptionStartValue;
 81  
     }
 82  
 
 83  
     /**
 84  
      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#getDescriptionValue()
 85  
      */
 86  
     public String getDescriptionValue()
 87  
     {
 88  0
         return descriptionValue;
 89  
     }
 90  
 
 91  
     /**
 92  
      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#getEndValue()
 93  
      */
 94  
     public String getEndValue()
 95  
     {
 96  0
         return endValue;
 97  
     }
 98  
 
 99  
     /**
 100  
      * Returns the list of messages available for the parameter rule
 101  
      * 
 102  
      * @param stage
 103  
      *            the stage instance that the parameter is associated to
 104  
      * @return the parameter rule Messages
 105  
      */
 106  
     protected Map<String, String> getMessages(IStageInstance stage)
 107  
     {
 108  0
         return DIFIoCRegistry.getRegistry().getImplementation(IMessageManager.class)
 109  0
                 .collectEntityMessagesFromRepository(this.getClass()).getMessages(stage.getContext().getLanguage());
 110  
     }
 111  
 
 112  
     /**
 113  
      * Inspector for the 'parameter' attribute.
 114  
      * 
 115  
      * @param stage
 116  
      *            the stage instance that the parameter is associated to
 117  
      * @return the parameter value
 118  
      * @throws ParameterException
 119  
      *             if the parameter does not exist
 120  
      */
 121  
     @SuppressWarnings("unchecked")
 122  
     public IParameter<T> getParameter(IStageInstance stage) throws ParameterException
 123  
     {
 124  0
         return (IParameter<T>) stage.getParameters().getAllAvailableParameters().getParameter(parameterID);
 125  
     }
 126  
 
 127  
     /**
 128  
      * Get's the name of a given parameter from the stage messages. When missing the parameter ID is returned (camel
 129  
      * case interpreted)
 130  
      * 
 131  
      * @param stage
 132  
      *            the parameter's current stage
 133  
      * @param parameterID
 134  
      *            the parameter ID to fetch the name
 135  
      * @return the parameter name
 136  
      */
 137  
     public String getParameterName(IStageInstance stage, String parameterID)
 138  
     {
 139  0
         String message = stage.getMessage(parameterID);
 140  
 
 141  0
         if (message == null)
 142  0
             message = StringUtils.camelCaseToString(parameterID);
 143  
 
 144  0
         return message;
 145  
     }
 146  
 
 147  
     /**
 148  
      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#getParameters()
 149  
      */
 150  
     public List<String> getParameters()
 151  
     {
 152  0
         return parameters;
 153  
     }
 154  
 
 155  
     /**
 156  
      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#getStartValue()
 157  
      */
 158  
     public String getStartValue()
 159  
     {
 160  0
         return startValue;
 161  
     }
 162  
 
 163  
     /**
 164  
      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#getValue()
 165  
      */
 166  
     public String getValue()
 167  
     {
 168  0
         return value;
 169  
     }
 170  
 
 171  
     /**
 172  
      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#init(java.lang.String, java.lang.String,
 173  
      *      pt.digitalis.dif.dem.objects.parameters.rules.ParameterRuleAction, java.lang.String, java.lang.String,
 174  
      *      java.lang.String)
 175  
      */
 176  
     public AbstractParameterRule<T> init(String parameterID, String parameters, ParameterRuleAction action,
 177  
             String value, String first, String last)
 178  
     {
 179  0
         this.parameterID = parameterID;
 180  0
         this.action = action;
 181  
 
 182  0
         if (parameters != null && !"".equals(parameters))
 183  
         {
 184  0
             List<String> parametersPassed = Arrays.asList(parameters.split(","));
 185  
 
 186  0
             for (String parameterPassed: parametersPassed)
 187  0
                 this.parameters.add(parameterPassed.trim().toLowerCase());
 188  
         }
 189  
 
 190  0
         if (value != null && !value.contains(AnnotationTags.NONE.toString()) && value.contains(":"))
 191  
         {
 192  0
             String[] splitCommaValues = value.split(",");
 193  0
             for (String v: splitCommaValues)
 194  
             {
 195  0
                 String[] splitValue = v.split(":");
 196  0
                 if (this.value != null)
 197  
                 {
 198  0
                     this.value += ",";
 199  
                 }
 200  
                 else
 201  
                 {
 202  0
                     this.value = "";
 203  
                 }
 204  0
                 this.value += splitValue[0];
 205  
 
 206  0
                 if (this.descriptionValue != null)
 207  
                 {
 208  0
                     this.descriptionValue += ",";
 209  
                 }
 210  
                 else
 211  
                 {
 212  0
                     this.descriptionValue = "";
 213  
                 }
 214  0
                 this.descriptionValue += splitValue[1];
 215  
             }
 216  
         }
 217  
         else
 218  
         {
 219  0
             this.value = value;
 220  
         }
 221  
 
 222  0
         if (first != null && !first.contains(AnnotationTags.NONE.toString()) && first.contains(":"))
 223  
         {
 224  0
             String[] splitValue = first.split(":");
 225  0
             this.startValue = splitValue[0];
 226  0
             this.descriptionStartValue = splitValue[1];
 227  
         }
 228  
         else
 229  
         {
 230  0
             this.startValue = first;
 231  
         }
 232  
 
 233  0
         if (last != null && !last.contains(AnnotationTags.NONE.toString()) && last.contains(":"))
 234  
         {
 235  0
             String[] splitValue = last.split(":");
 236  0
             this.endValue = splitValue[0];
 237  0
             this.descriptionEndValue = splitValue[1];
 238  
         }
 239  
         else
 240  
         {
 241  0
             this.endValue = last;
 242  
         }
 243  
 
 244  0
         return this;
 245  
     }
 246  
 
 247  
     /**
 248  
      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#setDescriptionEndValue(java.lang.String)
 249  
      */
 250  
     public void setDescriptionEndValue(String descriptionEndValue)
 251  
     {
 252  0
         this.descriptionEndValue = descriptionEndValue;
 253  0
     }
 254  
 
 255  
     /**
 256  
      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#setDescriptionStartValue(java.lang.String)
 257  
      */
 258  
     public void setDescriptionStartValue(String descriptionStartValue)
 259  
     {
 260  0
         this.descriptionStartValue = descriptionStartValue;
 261  0
     }
 262  
 
 263  
     /**
 264  
      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#setDescriptionValue(java.lang.String)
 265  
      */
 266  
     public void setDescriptionValue(String descriptionValue)
 267  
     {
 268  0
         this.descriptionValue = descriptionValue;
 269  0
     }
 270  
 
 271  
     /**
 272  
      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#setEndValue(java.lang.String)
 273  
      */
 274  
     public AbstractParameterRule<T> setEndValue(String endValue)
 275  
     {
 276  0
         this.endValue = endValue;
 277  
 
 278  0
         return this;
 279  
     }
 280  
 
 281  
     /**
 282  
      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#setStartValue(java.lang.String)
 283  
      */
 284  
     public AbstractParameterRule<T> setStartValue(String startValue)
 285  
     {
 286  0
         this.startValue = startValue;
 287  
 
 288  0
         return this;
 289  
     }
 290  
 
 291  
     /**
 292  
      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#setValue(java.lang.String)
 293  
      */
 294  
     public AbstractParameterRule<T> setValue(String value)
 295  
     {
 296  0
         this.value = value;
 297  
 
 298  0
         return this;
 299  
     }
 300  
 
 301  
     /**
 302  
      * @see java.lang.Object#toString()
 303  
      */
 304  
     @Override
 305  
     public String toString()
 306  
     {
 307  0
         ObjectFormatter formatter = new ObjectFormatter();
 308  0
         formatter.addItem("Action", this.action);
 309  0
         formatter.addItem("End Value", this.endValue);
 310  0
         formatter.addItem("Parameter ID", this.parameterID);
 311  0
         formatter.addItem("Parameters", this.parameters);
 312  0
         formatter.addItem("Start Value", this.startValue);
 313  0
         formatter.addItem("Value", this.value);
 314  0
         formatter.addItem("Description Value", this.descriptionValue);
 315  0
         formatter.addItem("Description Start Value", this.descriptionStartValue);
 316  0
         formatter.addItem("Description End Value", this.descriptionEndValue);
 317  
 
 318  0
         return formatter.getFormatedObject();
 319  
     }
 320  
 }