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
6 package pt.digitalis.dif.dem.objects.messages;
7
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.Map;
11
12 import pt.digitalis.dif.startup.DIFGeneralConfigurationParameters;
13 import pt.digitalis.dif.utils.ObjectFormatter;
14 import pt.digitalis.utils.common.collections.CaseInsensitiveHashMap;
15
16 /**
17 * Represents a view message with language translations and persistence information
18 *
19 * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a><br/>
20 * @author Rodrigo Goncalves <a href="mailto:rgoncalves@digitalis.pt">rgoncalves@digitalis.pt</a><br/>
21 * @created Nov 2, 2007
22 */
23 public class MessageList {
24
25 /** The messages */
26 private Map<String, MessageTranslations> messages = new CaseInsensitiveHashMap<MessageTranslations>();
27
28 /**
29 * Adds a message to the list. Uses the default language
30 *
31 * @param id
32 * the message id
33 * @param message
34 * the message
35 */
36 public void addMessage(String id, Message message)
37 {
38 addMessageTranslation(id, DIFGeneralConfigurationParameters.getInstance().getDefaultLanguage(), message);
39 }
40
41 /**
42 * Adds a message to the list. Note: It overrides the current translations if they exist.
43 *
44 * @param id
45 * the message id
46 * @param translations
47 * the translations list
48 */
49 public void addMessage(String id, MessageTranslations translations)
50 {
51 messages.put(id, translations);
52 }
53
54 /**
55 * Adds a message to the list. Uses the default language and no custom message
56 *
57 * @param id
58 * the message id
59 * @param message
60 * the message
61 */
62 public void addMessage(String id, String message)
63 {
64 addMessage(id, new Message(message));
65 }
66
67 /**
68 * Adds a message list to the list. All identical ids will override the current ones.
69 *
70 * @param list
71 * the list of message translations to add
72 */
73 public void addMessageList(MessageList list)
74 {
75 for (String id: list.getMessageIDs())
76 {
77 messages.put(id, list.getMessageTranslations(id));
78 }
79 }
80
81 /**
82 * Adds a message translation to the list
83 *
84 * @param id
85 * @param language
86 * @param message
87 */
88 public void addMessageTranslation(String id, String language, Message message)
89 {
90 MessageTranslations translations = messages.get(id);
91 language = language.toLowerCase();
92
93 if (translations == null)
94 {
95 translations = new MessageTranslations();
96 translations.addTranslation(language, message);
97
98 messages.put(id, translations);
99
100 }
101 else
102 translations.addTranslation(language, message);
103 }
104
105 /**
106 * Checks if a given message is contained on the list.
107 *
108 * @param messageID
109 * the message ID
110 * @return T if the message with the given ID is contained within the list, F otherwise
111 */
112 public boolean containsMessageWithID(String messageID)
113 {
114 return this.messages.containsKey(messageID);
115 }
116
117 /**
118 * Gets the list of messages IDs
119 *
120 * @return the message translations
121 */
122 public List<String> getMessageIDs()
123 {
124 return new ArrayList<String>(messages.keySet());
125 }
126
127 /**
128 * Gets the list of messages and their default translation message
129 *
130 * @return the message map
131 */
132 public Map<String, String> getMessages()
133 {
134 return getMessages(DIFGeneralConfigurationParameters.getInstance().getDefaultLanguage());
135 }
136
137 /**
138 * Gets the list of messages and their default message translation to the given language
139 *
140 * @param language
141 * the language desired
142 * @return the message map
143 */
144 public Map<String, String> getMessages(String language)
145 {
146
147 Map<String, String> messageValues = new CaseInsensitiveHashMap<String>();
148
149 for (String id: getMessageIDs())
150 {
151 if ((messages.get(id) != null) && (messages.get(id).getMessage(language) != null))
152 messageValues.put(id, messages.get(id).getMessage(language).getMessage());
153 else
154 messageValues.put(id, null);
155
156 }
157
158 return messageValues;
159 }
160
161 /**
162 * Gets the translations for a given message
163 *
164 * @param id
165 * the id of the message to search
166 * @return the message translations for the given message id
167 */
168 public MessageTranslations getMessageTranslations(String id)
169 {
170 return messages.get(id);
171 }
172
173 /**
174 * @see java.lang.Object#toString()
175 */
176 @Override
177 public String toString()
178 {
179 ObjectFormatter formatter = new ObjectFormatter();
180
181 formatter.addItem("Messages", messages);
182
183 return formatter.getFormatedObject();
184 }
185 }