Coverage Report - pt.digitalis.dif.dem.objects.messages.MessageTranslations
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageTranslations
0%
0/21
0%
0/8
2,4
 
 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  
 
 6  
 package pt.digitalis.dif.dem.objects.messages;
 7  
 
 8  
 import java.util.HashMap;
 9  
 import java.util.Map;
 10  
 
 11  
 import pt.digitalis.dif.startup.DIFGeneralConfigurationParameters;
 12  
 import pt.digitalis.dif.utils.ObjectFormatter;
 13  
 
 14  
 /**
 15  
  * The translations of a message
 16  
  * 
 17  
  * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a><br/>
 18  
  * @author Rodrigo Gonçalves <a href="mailto:rgoncalves@digitalis.pt">rgoncalves@digitalis.pt</a>
 19  
  * @created Nov 2, 2007
 20  
  */
 21  0
 public class MessageTranslations {
 22  
 
 23  
     /** The translations */
 24  0
     private Map<String, Message> translations = new HashMap<String, Message>();
 25  
 
 26  
     /**
 27  
      * Adds a new translation to the map.
 28  
      * 
 29  
      * @param language
 30  
      *            the language of the translation
 31  
      * @param message
 32  
      *            the message object
 33  
      * @return the updated object
 34  
      */
 35  
     public MessageTranslations addTranslation(String language, Message message)
 36  
     {
 37  
 
 38  0
         language = processLanguage(language);
 39  
 
 40  0
         translations.put(language.toLowerCase(), message);
 41  
 
 42  0
         return this;
 43  
     }
 44  
 
 45  
     /**
 46  
      * Returns a message translation for a given language.
 47  
      * 
 48  
      * @param language
 49  
      *            the language of the translation
 50  
      * @return the message of the given language
 51  
      */
 52  
     public Message getMessage(String language)
 53  
     {
 54  
 
 55  0
         language = processLanguage(language);
 56  
 
 57  0
         Message message = translations.get(language);
 58  
 
 59  0
         if (message == null)
 60  
         {
 61  0
             if (language != DIFGeneralConfigurationParameters.getInstance().getDefaultLanguage())
 62  0
                 return translations.get(DIFGeneralConfigurationParameters.getInstance().getDefaultLanguage());
 63  
             else
 64  
             {
 65  0
                 if (translations.keySet().size() > 0)
 66  0
                     return (Message) translations.values().toArray()[0];
 67  
             }
 68  
         }
 69  
 
 70  0
         return message;
 71  
     }
 72  
 
 73  
     /**
 74  
      * Returns all the translations for the message.
 75  
      * 
 76  
      * @return the message translations
 77  
      */
 78  
     public Map<String, Message> getTranslations()
 79  
     {
 80  0
         return this.translations;
 81  
     }
 82  
 
 83  
     /**
 84  
      * Takes a language as argument and processes it according to the following rule: - if null was passed use DiF's
 85  
      * default language - if not null the language is converted to lower-case
 86  
      * 
 87  
      * @param language
 88  
      *            the language
 89  
      * @return the lower-case version of the language or DiF's default language.
 90  
      */
 91  
     private String processLanguage(String language)
 92  
     {
 93  
         // If language wasn't pass, revert back to default
 94  0
         if (language == null)
 95  0
             return DIFGeneralConfigurationParameters.getInstance().getDefaultLanguage();
 96  
         // ...else use the passed language lower case version
 97  
         else
 98  0
             return language.toLowerCase();
 99  
     }
 100  
 
 101  
     /**
 102  
      * @see java.lang.Object#toString()
 103  
      */
 104  
     @Override
 105  
     public String toString()
 106  
     {
 107  0
         ObjectFormatter formatter = new ObjectFormatter();
 108  
 
 109  0
         formatter.addItem("Translations", translations);
 110  
 
 111  0
         return formatter.getFormatedObject();
 112  
     }
 113  
 }