1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.example.demo;
21
22 import org.apache.myfaces.tobago.internal.component.AbstractUICommand;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import javax.enterprise.context.SessionScoped;
27 import javax.faces.event.AjaxBehaviorEvent;
28 import javax.inject.Named;
29 import java.io.Serializable;
30 import java.lang.invoke.MethodHandles;
31 import java.util.Currency;
32
33 @SessionScoped
34 @Named
35 public class GroupController implements Serializable {
36
37 private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
38
39 private String chatlog;
40 private String newMessage;
41 private String sendTo;
42 private double value;
43 private double valueInEuro;
44 private Currency currency;
45 private static final Currency[] CURRENCIES;
46 private String firstName;
47 private String lastName;
48
49 static {
50 CURRENCIES = new Currency[]{
51 Currency.getInstance("JPY"),
52 Currency.getInstance("TTD"),
53 Currency.getInstance("USD"),
54 Currency.getInstance("EUR")};
55 }
56
57 public GroupController() {
58 chatlog = "Peter: Hi, how are you?";
59 newMessage = "I'm fine.";
60 sendTo = "";
61 value = 1000.0;
62 currency = Currency.getInstance("EUR");
63 firstName = "Bob";
64 lastName = "Marley";
65 compute();
66 }
67
68 public String getChatlog() {
69 return chatlog;
70 }
71
72 public String getNewMessage() {
73 return newMessage;
74 }
75
76 public void setNewMessage(final String newMessage) {
77 this.newMessage = newMessage;
78 }
79
80 public void sendChat() {
81 if (newMessage.equals("delete chat")) {
82 deleteChat();
83 } else {
84 if (!chatlog.isEmpty()) {
85 chatlog += "\n";
86 }
87 chatlog += "User Two: " + newMessage;
88 newMessage = "";
89 }
90 }
91
92 public void deleteChat() {
93 chatlog = "";
94 }
95
96 public String getSendTo() {
97 return sendTo;
98 }
99
100 public void setSendTo(final String sendTo) {
101 this.sendTo = sendTo;
102 }
103
104 public void sendToListener(final AjaxBehaviorEvent event) {
105
106 LOG.info("AjaxBehaviorEvent called.");
107
108 if (event != null && event.getComponent() instanceof AbstractUICommand) {
109 final AbstractUICommand command = (AbstractUICommand) event.getComponent();
110 sendTo = command.getLabel();
111 LOG.info("AjaxBehaviorEvent called. Current label: '{}'", sendTo);
112 }
113 }
114
115 public void compute(final AjaxBehaviorEvent event) {
116 LOG.info("AjaxBehaviorEvent called.");
117 compute();
118 }
119
120 private void compute() {
121 switch (currency.getCurrencyCode()) {
122 case "JPY":
123 valueInEuro = value * 0.00884806667;
124 break;
125 case "TTD":
126 valueInEuro = value * 0.133748824;
127 break;
128 case "USD":
129 valueInEuro = value * 0.896097495;
130 break;
131 case "EUR":
132 valueInEuro = value;
133 break;
134 default:
135 throw new RuntimeException("Unsupported Currency: '" + currency.getCurrencyCode() + "'");
136 }
137 LOG.info("euro value: '{}', value: '{}', currency: '{}'", valueInEuro, value, currency.getCurrencyCode());
138 }
139
140 public double getValue() {
141 return value;
142 }
143
144 public void setValue(final double value) {
145 this.value = value;
146 }
147
148 public double getValueInEuro() {
149 return valueInEuro;
150 }
151
152 public Currency getCurrency() {
153 return currency;
154 }
155
156 public void setCurrency(final Currency currency) {
157 this.currency = currency;
158 }
159
160 public Currency[] getCurrencies() {
161 return CURRENCIES;
162 }
163
164 public String getFirstName() {
165 return firstName;
166 }
167
168 public void setFirstName(String firstName) {
169 this.firstName = firstName;
170 }
171
172 public String getLastName() {
173 return lastName;
174 }
175
176 public void setLastName(String lastName) {
177 this.lastName = lastName;
178 }
179 }