1
2
3
4
5 package org.directdemocracyportal.democracy.web.controller;
6
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Locale;
10 import java.util.Map;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.directdemocracyportal.democracy.model.application.Event;
15 import org.directdemocracyportal.democracy.model.application.OnlinePoliticalParty;
16 import org.directdemocracyportal.democracy.model.application.User;
17 import org.directdemocracyportal.democracy.model.world.Government;
18 import org.directdemocracyportal.democracy.model.world.Organisation;
19 import org.directdemocracyportal.democracy.model.world.Person;
20 import org.directdemocracyportal.democracy.model.world.PoliticalParty;
21 import org.directdemocracyportal.democracy.model.world.Vote;
22 import org.directdemocracyportal.democracy.service.PartyAlreadyExistException;
23 import org.directdemocracyportal.democracy.service.PortalService;
24 import org.directdemocracyportal.democracy.service.UserAlreadyExistException;
25 import org.directdemocracyportal.democracy.service.UserService;
26 import org.directdemocracyportal.democracy.service.command.CreatePartyCommand;
27 import org.directdemocracyportal.democracy.service.command.RegisterCommand;
28 import org.directdemocracyportal.democracy.service.dao.AgentDAO;
29 import org.directdemocracyportal.democracy.service.dao.DocumentDAO;
30 import org.directdemocracyportal.democracy.web.ApplicationMessageHolder;
31 import org.directdemocracyportal.democracy.web.BeanLocator;
32 import org.directdemocracyportal.democracy.web.ImageConstants;
33 import org.directdemocracyportal.democracy.web.StaticContextLoaderListener;
34 import org.directdemocracyportal.democracy.web.UserState;
35 import org.directdemocracyportal.democracy.web.ApplicationMessageHolder.MessageConstans;
36 import org.directdemocracyportal.democracy.web.action.ControllerAction;
37 import org.directdemocracyportal.democracy.web.action.CreatePoliticalPartyAction;
38 import org.directdemocracyportal.democracy.web.action.JoinPoliticalPartyAction;
39 import org.directdemocracyportal.democracy.web.action.LoginAction;
40 import org.directdemocracyportal.democracy.web.action.LogoutAction;
41 import org.directdemocracyportal.democracy.web.action.NewApplicationInstanceAction;
42 import org.directdemocracyportal.democracy.web.action.RegisterAction;
43 import org.directdemocracyportal.democracy.web.action.ShowAllEvents;
44 import org.directdemocracyportal.democracy.web.action.ShowApplicationHome;
45 import org.directdemocracyportal.democracy.web.action.ShowGovernmentAction;
46 import org.directdemocracyportal.democracy.web.action.ShowGovernmentsAction;
47 import org.directdemocracyportal.democracy.web.action.ShowMembersAction;
48 import org.directdemocracyportal.democracy.web.action.ShowMyProfileAction;
49 import org.directdemocracyportal.democracy.web.action.ShowOnlinePoliticalPartyAction;
50 import org.directdemocracyportal.democracy.web.action.ShowOrganisationAction;
51 import org.directdemocracyportal.democracy.web.action.ShowPersonAction;
52 import org.directdemocracyportal.democracy.web.action.ShowPoliticalPartiesAction;
53 import org.directdemocracyportal.democracy.web.action.ShowUserAction;
54 import org.directdemocracyportal.democracy.web.views.ActiveUserView;
55 import org.directdemocracyportal.democracy.web.views.EventsPanel;
56 import org.directdemocracyportal.democracy.web.views.ExplorerPanel;
57 import org.directdemocracyportal.democracy.web.views.FooterPanel;
58 import org.directdemocracyportal.democracy.web.views.GovernmentOverviewPanel;
59 import org.directdemocracyportal.democracy.web.views.GovernmentsPanel;
60 import org.directdemocracyportal.democracy.web.views.HeaderPanel;
61 import org.directdemocracyportal.democracy.web.views.MembersPanel;
62 import org.directdemocracyportal.democracy.web.views.OnlinePoliticalPartyOverviewPanel;
63 import org.directdemocracyportal.democracy.web.views.OrganisationOverviewPanel;
64 import org.directdemocracyportal.democracy.web.views.PersonProfilePanel;
65 import org.directdemocracyportal.democracy.web.views.PoliticalPartiesPanel;
66 import org.directdemocracyportal.democracy.web.views.StartContentPanel;
67 import org.directdemocracyportal.democracy.web.views.UserProfilePanel;
68 import org.directdemocracyportal.democracy.web.views.dialogs.CreatePartyDialog;
69 import org.directdemocracyportal.democracy.web.views.dialogs.LoginDialog;
70 import org.directdemocracyportal.democracy.web.views.dialogs.RegisterUserDialog;
71 import org.directdemocracyportal.democracy.web.views.dialogs.AbstractActionDialog.DialogResult;
72 import org.directdemocracyportal.democracy.web.views.header.UserPanel;
73 import org.directdemocracyportal.democracy.web.views.header.WelcomePanel;
74 import org.springframework.context.i18n.LocaleContextHolder;
75 import org.springframework.transaction.annotation.Propagation;
76 import org.springframework.transaction.annotation.Transactional;
77
78 import thinwire.ui.Component;
79 import thinwire.ui.Frame;
80 import thinwire.ui.Menu;
81 import thinwire.ui.Panel;
82 import thinwire.ui.layout.TableLayout;
83 import thinwire.ui.style.Color;
84
85 /***
86 * The Class FrontController.
87 */
88 @Transactional(propagation = Propagation.REQUIRED)
89 public class FrontController implements Controller
90 {
91
92 /*** The log. */
93 private static Log log = LogFactory.getLog(FrontController.class);
94
95 /*** The user service. */
96 private final UserService userService;
97
98 /*** The portal service. */
99 private final PortalService portalService;
100
101 /*** The agent dao. */
102 private final AgentDAO agentDAO;
103
104 /*** The document dao. */
105 private final DocumentDAO documentDAO;
106
107
108 /***
109 * Instantiates a new front controller.
110 *
111 * @param userService the user service
112 * @param portalService the portal service
113 * @param agentDAO the agent dao
114 * @param documentDAO the document dao
115 */
116 public FrontController(UserService userService,
117 PortalService portalService, AgentDAO agentDAO,
118 DocumentDAO documentDAO) {
119 super();
120 this.userService = userService;
121 this.portalService = portalService;
122 this.agentDAO = agentDAO;
123 this.documentDAO = documentDAO;
124 }
125
126
127
128
129 public void handleAction(ControllerAction action) throws Exception {
130 if (action instanceof NewApplicationInstanceAction) {
131 handleNewApplicationInstanceAction((NewApplicationInstanceAction) action);
132 }
133 if (action instanceof LoginAction) {
134 handleLoginAction((LoginAction) action);
135 } else if (action instanceof RegisterAction) {
136 handleRegisterAction((RegisterAction) action);
137 } else if (action instanceof LogoutAction) {
138 handleLogoutAction((LogoutAction) action);
139 } else if (action instanceof ShowMyProfileAction) {
140 handleShowMyProfileAction((ShowMyProfileAction) action);
141 } else if (action instanceof ShowGovernmentAction) {
142 handleShowGovernmentAction((ShowGovernmentAction) action);
143 } else if (action instanceof ShowMembersAction) {
144 handleShowMembersAction((ShowMembersAction) action);
145 } else if (action instanceof ShowPoliticalPartiesAction) {
146 handleShowPoliticalPartiesAction((ShowPoliticalPartiesAction) action);
147 } else if (action instanceof ShowGovernmentsAction) {
148 handleShowGovernmentsAction((ShowGovernmentsAction) action);
149 } else if (action instanceof ShowApplicationHome) {
150 handleShowApplicationHome((ShowApplicationHome) action);
151 } else if (action instanceof ShowAllEvents) {
152 handleShowAllEvents((ShowAllEvents) action);
153 } else if (action instanceof CreatePoliticalPartyAction) {
154 handleCreatePoliticalPartyAction((CreatePoliticalPartyAction) action);
155 } else if (action instanceof JoinPoliticalPartyAction) {
156 handleJoinPoliticalPartyAction((JoinPoliticalPartyAction) action);
157 } else if (action instanceof ShowOrganisationAction) {
158 handleShowOrganisationAction((ShowOrganisationAction) action);
159 } else if (action instanceof ShowUserAction) {
160 handleShowUserAction((ShowUserAction) action);
161 } else if (action instanceof ShowOnlinePoliticalPartyAction) {
162 handleShowOnlinePoliticalPartyAction((ShowOnlinePoliticalPartyAction) action);
163 } else if (action instanceof ShowPersonAction) {
164 handleShowPersonAction((ShowPersonAction) action);
165 }
166 }
167
168 /***
169 * Handle new application instance action.
170 *
171 * @param action the action
172 */
173 private void handleNewApplicationInstanceAction(
174 NewApplicationInstanceAction action) {
175 Frame frame = action.getFrame();
176
177 setupUserSettings(action.getArgs());
178 frame.setTitle(ApplicationMessageHolder
179 .getMessage(MessageConstans.APPLICATION_NAME));
180 frame.setLayout(new TableLayout(new double[][] { { 150, 0 },
181
182 { 0.1, 0.8, 0.1 } },
183 1,
184 0));
185
186 Menu menu = createMenu();
187 frame.setMenu(menu);
188
189 HeaderPanel headerPanel = new HeaderPanel();
190 headerPanel.setLimit("0,0,2,1");
191 frame.getChildren().add(headerPanel);
192
193 ExplorerPanel explorerPanel = new ExplorerPanel();
194 explorerPanel.setLimit("0,1,1,1");
195 frame.getChildren().add(explorerPanel);
196
197 PortalService portalService = (PortalService) StaticContextLoaderListener.getWebApplicationContext().getBean("portalService");
198 Panel contentView = new StartContentPanel(portalService.getGlobalPortal());
199 contentView.setLimit("1,1,1,1");
200 frame.getChildren().add(contentView);
201 ActiveUserView.activeContentView.set(contentView);
202
203 FooterPanel footerPanel = new FooterPanel();
204 footerPanel.setLimit("0,2,2,1");
205 frame.getChildren().add(footerPanel);
206
207 }
208
209 /***
210 * Sets the up user settings.
211 *
212 * @param args the new up user settings
213 */
214 private void setupUserSettings(String[] args) {
215 UserState.userSettings.set(createUserSettingMap(args));
216
217 if (UserState.getAcceptLanguage().contains("sv")) {
218 UserState.locale.set(new Locale("sv"));
219 } else {
220 UserState.locale.set(LocaleContextHolder.getLocale());
221 }
222 }
223
224 /***
225 * Creates the user setting map.
226 *
227 * @param args the args
228 * @return the map
229 */
230 private Map<String, String> createUserSettingMap(String[] args) {
231 Map<String, String> userSetting = new HashMap<String, String>();
232
233 for (String arg : args) {
234 String[] split = arg.split("=", 2);
235
236 String key = split[0];
237 String value = split[1];
238 userSetting.put(key, value);
239 }
240 return userSetting;
241 }
242
243 /***
244 * Creates the menu.
245 *
246 * @return the menu
247 */
248 private Menu createMenu() {
249 Menu menu = new Menu();
250 menu.getStyle().getFont().setColor(Color.WHITE);
251 menu.getStyle().getBackground().setColor(Color.valueOf("rgb(0,51,153)"));
252
253 Menu.Item mainPortalItem = new Menu.Item(ApplicationMessageHolder
254 .getMessage(MessageConstans.APPLICATION_NAME));
255 menu.getRootItem().getChildren().add(mainPortalItem);
256
257 Menu.Item membersPortalItem = new Menu.Item(ApplicationMessageHolder
258 .getMessage(MessageConstans.GROUPS));
259 membersPortalItem.setImage(ImageConstants.GROUP_HOME_ICON);
260 menu.getRootItem().getChildren().add(membersPortalItem);
261
262 Menu.Item portalItem = new Menu.Item(ApplicationMessageHolder
263 .getMessage(MessageConstans.RECENT_ACTIONS));
264 membersPortalItem.getChildren().add(portalItem);
265
266 Menu.Item showAllItem = new Menu.Item(ApplicationMessageHolder
267 .getMessage(MessageConstans.GROUPS));
268 showAllItem.setUserObject(new ShowAllEvents());
269 portalItem.getChildren().add(showAllItem);
270
271 Menu.Item partyItem = new Menu.Item(ApplicationMessageHolder
272 .getMessage(MessageConstans.POLITICAL_PARTY));
273 partyItem.setImage(ImageConstants.POLITICAL_PARTY_ICON);
274 menu.getRootItem().getChildren().add(partyItem);
275
276 Menu.Item createItem = new Menu.Item(ApplicationMessageHolder
277 .getMessage(MessageConstans.CREATE_POLITICAL_PARTY));
278 createItem.setUserObject(new CreatePoliticalPartyAction());
279
280 if (UserState.user.get() == null) {
281 createItem.setEnabled(false);
282 }
283 partyItem.getChildren().add(createItem);
284
285 Menu.Item governmentPortalItem = new Menu.Item(ApplicationMessageHolder
286 .getMessage(MessageConstans.GOVERNMENTS));
287 governmentPortalItem.setImage(ImageConstants.GOVERNMENT_ICON);
288 menu.getRootItem().getChildren().add(governmentPortalItem);
289
290 menu.addActionListener(Component.ACTION_CLICK, BeanLocator
291 .getApplicationActionListener());
292 return menu;
293 }
294
295
296 /***
297 * Handle show person action.
298 *
299 * @param action the action
300 */
301 private void handleShowPersonAction(ShowPersonAction action) {
302 Person person = (Person) agentDAO.load(action.getPersonId());
303
304 List<Vote> votes = documentDAO.getVotesForPerson(person);
305
306 ActiveUserView.changeContentView(new PersonProfilePanel(person,votes));
307 }
308
309 /***
310 * Handle show online political party action.
311 *
312 * @param action the action
313 */
314 private void handleShowOnlinePoliticalPartyAction(
315 ShowOnlinePoliticalPartyAction action) {
316 Organisation organisation = (Organisation) portalService
317 .getOrganisation(action.getPartyId());
318
319 ActiveUserView.changeContentView(new OnlinePoliticalPartyOverviewPanel(
320 (OnlinePoliticalParty) organisation));
321 }
322
323 /***
324 * Handle show user action.
325 *
326 * @param action the action
327 */
328 private void handleShowUserAction(ShowUserAction action) {
329 User user = userService.getUser(action.getUserId());
330 ActiveUserView.changeContentView(new UserProfilePanel(user,portalService.getPoliticalParties()));
331 }
332
333 /***
334 * Handle show organisation action.
335 *
336 * @param action the action
337 */
338 private void handleShowOrganisationAction(ShowOrganisationAction action) {
339 Organisation organisation = (Organisation) portalService
340 .getOrganisation(action.getOrganisationId());
341
342 ActiveUserView.changeContentView(new OrganisationOverviewPanel(
343 organisation));
344 }
345
346 /***
347 * Handle join political party action.
348 *
349 * @param action the action
350 */
351 private void handleJoinPoliticalPartyAction(JoinPoliticalPartyAction action) {
352 ActiveUserView.changeContentView(new OnlinePoliticalPartyOverviewPanel(
353 portalService.joinParty(action.getPartyId(),
354 UserState.user.get().getId())));
355 }
356
357 /***
358 * Handle create political party action.
359 *
360 * @param action the action
361 */
362 private void handleCreatePoliticalPartyAction(
363 CreatePoliticalPartyAction action) {
364 CreatePartyDialog createPartyDialog = new CreatePartyDialog(portalService.getCountries());
365 DialogResult dialogResult = createPartyDialog
366 .getDialogResult();
367
368 if (dialogResult.equals(DialogResult.Do_Action)) {
369 CreatePartyCommand createPartyCommand = new CreatePartyCommand();
370 createPartyCommand.setName(createPartyDialog.getName());
371 createPartyCommand.setCountryId(createPartyDialog
372 .getCountryId());
373 try {
374 PoliticalParty politicalParty = portalService
375 .createParty(createPartyCommand,
376 UserState.user.get().getId());
377 ActiveUserView
378 .changeContentView(new OnlinePoliticalPartyOverviewPanel(
379 (OnlinePoliticalParty) politicalParty));
380 } catch (PartyAlreadyExistException e) {
381
382 e.printStackTrace();
383 }
384 }
385 }
386
387 /***
388 * Handle show all events.
389 *
390 * @param action the action
391 */
392 private void handleShowAllEvents(ShowAllEvents action) {
393 List<Event> events = portalService.getEvents();
394 ActiveUserView.changeContentView(new EventsPanel(events));
395 }
396
397 /***
398 * Handle show application home.
399 *
400 * @param action the action
401 */
402 private void handleShowApplicationHome(ShowApplicationHome action) {
403 ActiveUserView.changeContentView(new StartContentPanel(portalService.getGlobalPortal()));
404 }
405
406 /***
407 * Handle show governments action.
408 *
409 * @param action the action
410 */
411 private void handleShowGovernmentsAction(ShowGovernmentsAction action) {
412 ActiveUserView.changeContentView(new GovernmentsPanel(portalService.getGovernments()));
413 }
414
415 /***
416 * Handle show political parties action.
417 *
418 * @param action the action
419 */
420 private void handleShowPoliticalPartiesAction(ShowPoliticalPartiesAction action) {
421 ActiveUserView.changeContentView(new PoliticalPartiesPanel(portalService.getOnlinePoliticalParties()));
422 }
423
424 /***
425 * Handle show members action.
426 *
427 * @param action the action
428 */
429 private void handleShowMembersAction(ShowMembersAction action) {
430 ActiveUserView.changeContentView(new MembersPanel(portalService.getUsers()));
431 }
432
433 /***
434 * Handle show government action.
435 *
436 * @param action the action
437 */
438 private void handleShowGovernmentAction(ShowGovernmentAction action) {
439 Government government = portalService.getGovernment(action.getGovernmentId());
440 ActiveUserView.changeContentView(new GovernmentOverviewPanel(government,portalService.getPoliticalParties()));
441 }
442
443 /***
444 * Handle show my profile action.
445 *
446 * @param action the action
447 */
448 private void handleShowMyProfileAction(ShowMyProfileAction action) {
449 ActiveUserView.changeContentView(new UserProfilePanel(UserState.user
450 .get(),portalService.getPoliticalParties()));
451 }
452
453 /***
454 * Handle logout action.
455 *
456 * @param action the action
457 */
458 private void handleLogoutAction(LogoutAction action) {
459 userService.logout(UserState.user.get());
460 UserState.user.set(null);
461 ActiveUserView.changeRightHeaderPanel(new WelcomePanel());
462 ActiveUserView.changeContentView(new StartContentPanel(portalService.getGlobalPortal()));
463 ActiveUserView.changeActiveMenu(createMenu());
464 }
465
466 /***
467 * Handle register action.
468 *
469 * @param action the action
470 */
471 private void handleRegisterAction(RegisterAction action) {
472 RegisterUserDialog registerDlg = new RegisterUserDialog(portalService.getPortals());
473 DialogResult dialogResult = registerDlg.getDialogResult();
474
475 while (DialogResult.Do_Action.equals(dialogResult)) {
476 RegisterCommand registerCommand = new RegisterCommand();
477 registerCommand.setName(registerDlg.getName());
478 registerCommand.setEmail(registerDlg.getEmail());
479 registerCommand.setUsername(registerDlg.getUsername());
480 registerCommand.setPassword(registerDlg.getPassword());
481
482 registerCommand.setPortalId(registerDlg.getPortalId());
483
484 try {
485 User user = userService.register(registerCommand, UserState.getSessionId());
486 UserState.user.set(user);
487 ActiveUserView.changeContentView(new UserProfilePanel(user,portalService.getPoliticalParties()));
488 ActiveUserView.changeRightHeaderPanel(new UserPanel(user));
489 ActiveUserView.changeActiveMenu(createMenu());
490 dialogResult = null;
491 } catch (UserAlreadyExistException e) {
492 registerDlg.setErrorMessage(ApplicationMessageHolder.getMessage(MessageConstans.USER_EXIST_CHOOSE_DIFFERENT));
493 dialogResult = registerDlg.getDialogResult();
494 }
495 }
496 }
497
498 /***
499 * Handle login action.
500 *
501 * @param loginAction the login action
502 * @throws Exception the exception
503 */
504 public void handleLoginAction(LoginAction loginAction) throws Exception {
505 LoginDialog loginDialog = new LoginDialog();
506 DialogResult dialogResult = loginDialog.getDialogResult();
507 if (dialogResult.equals(DialogResult.Do_Action)) {
508 User user = userService.login(
509 loginDialog.getName(), loginDialog.getPassword(),
510 UserState.getSessionId());
511 UserState.user.set(user);
512 ActiveUserView.changeContentView(new UserProfilePanel(user,portalService.getPoliticalParties()));
513 ActiveUserView.changeRightHeaderPanel(new UserPanel(user));
514 ActiveUserView.changeActiveMenu(createMenu());
515 }
516 }
517 }