1
2
3
4
5 package org.directdemocracyportal.democracy.service;
6
7 import java.util.ArrayList;
8 import java.util.Date;
9 import java.util.List;
10
11 import org.directdemocracyportal.democracy.model.application.Action;
12 import org.directdemocracyportal.democracy.model.application.Event;
13 import org.directdemocracyportal.democracy.model.application.GovernmentPortal;
14 import org.directdemocracyportal.democracy.model.application.OnlinePoliticalParty;
15 import org.directdemocracyportal.democracy.model.application.Portal;
16 import org.directdemocracyportal.democracy.model.application.User;
17 import org.directdemocracyportal.democracy.model.application.UserRole;
18 import org.directdemocracyportal.democracy.model.application.UserSession;
19 import org.directdemocracyportal.democracy.model.core.GroupAgent;
20 import org.directdemocracyportal.democracy.model.core.Role;
21 import org.directdemocracyportal.democracy.model.world.Country;
22 import org.directdemocracyportal.democracy.model.world.Election;
23 import org.directdemocracyportal.democracy.model.world.ElectionType;
24 import org.directdemocracyportal.democracy.model.world.Government;
25 import org.directdemocracyportal.democracy.model.world.GovernmentType;
26 import org.directdemocracyportal.democracy.model.world.Issue;
27 import org.directdemocracyportal.democracy.model.world.Organisation;
28 import org.directdemocracyportal.democracy.model.world.OrganisationType;
29 import org.directdemocracyportal.democracy.model.world.Person;
30 import org.directdemocracyportal.democracy.model.world.PoliticalParty;
31 import org.directdemocracyportal.democracy.model.world.Region;
32 import org.directdemocracyportal.democracy.model.world.RegionType;
33 import org.directdemocracyportal.democracy.model.world.Resolution;
34 import org.directdemocracyportal.democracy.model.world.VoteResult;
35 import org.directdemocracyportal.democracy.model.world.Organisation.OrganisationRoles;
36 import org.directdemocracyportal.democracy.model.world.Resolution.ResolutionState;
37 import org.directdemocracyportal.democracy.service.command.CreatePartyCommand;
38 import org.directdemocracyportal.democracy.service.dao.AgentDAO;
39 import org.directdemocracyportal.democracy.service.dao.CountryDAO;
40 import org.directdemocracyportal.democracy.service.dao.DocumentDAO;
41 import org.directdemocracyportal.democracy.service.dao.EventDAO;
42 import org.directdemocracyportal.democracy.service.dao.GovernmentDAO;
43 import org.directdemocracyportal.democracy.service.dao.PoliticalPartyDAO;
44 import org.directdemocracyportal.democracy.service.dao.PortalDAO;
45 import org.directdemocracyportal.democracy.service.dao.RegionDAO;
46 import org.directdemocracyportal.democracy.service.dao.RoleDAO;
47 import org.directdemocracyportal.democracy.service.dao.UserDAO;
48 import org.directdemocracyportal.democracy.service.dao.UserSessionDAO;
49 import org.springframework.dao.DataAccessException;
50 import org.springframework.transaction.annotation.Propagation;
51 import org.springframework.transaction.annotation.Transactional;
52
53 /***
54 * The Class PortalServiceImpl.
55 */
56 @Transactional(propagation = Propagation.REQUIRED)
57 public class PortalServiceImpl implements PortalService
58 {
59
60 /*** The user dao. */
61 final private UserDAO userDAO;
62
63 /*** The agent dao. */
64 final private AgentDAO agentDAO;
65
66 /*** The event dao. */
67 final private EventDAO eventDAO;
68
69 /*** The portal dao. */
70 final private PortalDAO portalDAO;
71
72 /*** The country dao. */
73 final private CountryDAO countryDAO;
74
75 /*** The user session dao. */
76 final private UserSessionDAO userSessionDAO;
77
78 /*** The government dao. */
79 private final GovernmentDAO governmentDAO;
80
81 /*** The political party dao. */
82 private final PoliticalPartyDAO politicalPartyDAO;
83
84 /*** The document dao. */
85 private final DocumentDAO documentDAO;
86
87 /*** The role dao. */
88 private final RoleDAO roleDAO;
89
90 /*** The region dao. */
91 private final RegionDAO regionDAO;
92
93 /***
94 * Instantiates a new portal service impl.
95 *
96 * @param userDAO the user dao
97 * @param eventDAO the event dao
98 * @param portalDAO the portal dao
99 * @param countryDAO the country dao
100 * @param userSessionDAO the user session dao
101 * @param politicalPartyDAO the political party dao
102 * @param governmentDAO the government dao
103 * @param agentDAO the agent dao
104 * @param documentDAO the document dao
105 * @param roleDAO the role dao
106 * @param regionDAO the region dao
107 */
108 public PortalServiceImpl(UserDAO userDAO, EventDAO eventDAO,
109 PortalDAO portalDAO, CountryDAO countryDAO,
110 UserSessionDAO userSessionDAO, PoliticalPartyDAO politicalPartyDAO,
111 GovernmentDAO governmentDAO, AgentDAO agentDAO,DocumentDAO documentDAO,RoleDAO roleDAO,RegionDAO regionDAO) {
112 this.userDAO = userDAO;
113 this.eventDAO = eventDAO;
114 this.portalDAO = portalDAO;
115 this.countryDAO = countryDAO;
116 this.userSessionDAO = userSessionDAO;
117 this.politicalPartyDAO = politicalPartyDAO;
118 this.governmentDAO = governmentDAO;
119 this.agentDAO = agentDAO;
120 this.documentDAO = documentDAO;
121 this.roleDAO = roleDAO;
122 this.regionDAO = regionDAO;
123 }
124
125
126
127
128
129
130 public List<Event> getEvents() {
131 return this.eventDAO.getAll();
132 }
133
134
135
136
137
138
139 public List<Election> getElections() {
140 List<Election> elections = new ArrayList<Election>();
141
142 for (Government government : this.governmentDAO.getAll()) {
143 elections.addAll(government.getElections());
144 }
145 return elections;
146 }
147
148
149
150
151
152
153 public List<Government> getGovernments() {
154 return this.governmentDAO.getAllNationalGovernments();
155 }
156
157
158
159
160
161
162 public List<Portal> getPortals() {
163 return this.portalDAO.getAll();
164 }
165
166
167
168
169
170
171 public List<Country> getCountries() {
172 return countryDAO.getAll();
173 }
174
175
176
177
178
179
180
181 public PoliticalParty createParty(CreatePartyCommand command, Long userId)
182 throws PartyAlreadyExistException {
183 if (politicalPartyDAO.findByName(command.getName()) != null)
184 throw new PartyAlreadyExistException();
185
186 Country country = countryDAO.load(command.getCountryId());
187 PoliticalParty politicalParty = new OnlinePoliticalParty();
188 politicalParty.setCountry(country);
189 politicalParty.setName(command.getName());
190 politicalParty.setOrganisationType(OrganisationType.National);
191 politicalParty.setRegion(country);
192 agentDAO.save(politicalParty);
193
194 UserRole role = new UserRole();
195 role.setName(OrganisationRoles.Member.toString());
196 role.setGroupAgent(politicalParty);
197 politicalParty.getDefinedRoles().add(role);
198
199 politicalPartyDAO.save(politicalParty);
200
201 UserSession userSession = this.userSessionDAO.findByUserId(userId);
202 if (userSession != null) {
203 userSession.getUser().getEvents().add(
204 new Event(userSession.getUser(),
205 Action.Create_Political_Party, new Date()));
206 this.userSessionDAO.save(userSession);
207 }
208
209 return politicalParty;
210 }
211
212
213
214
215
216
217 public List<PoliticalParty> getPoliticalParties() {
218 return politicalPartyDAO.getAll();
219 }
220
221
222
223
224
225
226 public PoliticalParty getPoliticalParty(Long partyId) {
227 return politicalPartyDAO.load(partyId);
228 }
229
230
231
232
233
234
235
236 public OnlinePoliticalParty joinParty(Long partyId, Long userId) {
237 PoliticalParty party = politicalPartyDAO.load(partyId);
238
239 User user = userDAO.load(userId);
240
241 Role partyMember = party.findDefinedRoleByName(OrganisationRoles.Member.toString());
242
243 user.getPerson().getRoles().add(partyMember);
244
245 partyMember.getPlayers().add(user.getPerson());
246 agentDAO.save(party);
247 userDAO.save(user);
248
249 UserSession userSession = this.userSessionDAO.findByUserId(userId);
250 if (userSession != null) {
251 userSession.getUser().getEvents().add(
252 new Event(userSession.getUser(),
253 Action.Join_Political_Party, new Date()));
254 this.userSessionDAO.save(userSession);
255 }
256
257 return (OnlinePoliticalParty) party;
258 }
259
260
261
262
263
264
265 public Government getGovernment(Long governmentId) {
266 return governmentDAO.load(governmentId);
267 }
268
269
270
271
272
273
274 public List<UserSession> getActiveUserSessions() {
275 return userSessionDAO.getActiveSessions();
276 }
277
278
279
280
281
282
283 public List<User> getUsers() {
284 return this.userDAO.getAll();
285 }
286
287
288
289
290
291
292 @Override
293 public GroupAgent getOrganisation(Long organisationId) {
294 return (GroupAgent) this.agentDAO.load(organisationId);
295 }
296
297
298
299
300
301
302 @Override
303 public List<OnlinePoliticalParty> getOnlinePoliticalParties() {
304 return politicalPartyDAO.getAllOnline();
305 }
306
307
308
309
310
311
312
313
314
315 @Override
316 public PoliticalParty createPoliticalParty(String name, String shortCode,
317 Country country, Region region) {
318 PoliticalParty politicalParty = new PoliticalParty();
319 politicalParty.setName(name);
320 politicalParty.setShortCode(shortCode);
321 politicalParty.setOrganisationType(OrganisationType.National);
322 politicalParty.setCountry(country);
323 politicalParty.setRegion(region);
324 agentDAO.save(politicalParty);
325
326 Role role = new Role();
327 role.setGroupAgent(politicalParty);
328 role.setName(Organisation.OrganisationRoles.Member.toString());
329 politicalParty.getDefinedRoles().add(role);
330 agentDAO.save(politicalParty);
331
332 return politicalParty;
333 }
334
335
336
337
338
339
340
341 @Override
342 public void addMember(Organisation organisation, Person person) {
343 organisation.addMember(person);
344 agentDAO.save(person);
345 agentDAO.save(organisation);
346 }
347
348
349
350
351
352
353
354 @Override
355 public void addRolePlayed(Role role, Person person) {
356 role.getPlayers().add(person);
357 person.getRoles().add(role);
358 agentDAO.save(person);
359 agentDAO.save(role.getGroupAgent());
360 }
361
362
363
364
365
366
367
368 @Override
369 public Role createRoleInOrg(String roleName, Organisation organisation) {
370 Role newRole = new Role();
371 newRole.setGroupAgent(organisation);
372 newRole.setName(roleName);
373 organisation.getDefinedRoles().add(newRole);
374 agentDAO.save(organisation);
375 return newRole;
376 }
377
378
379
380
381
382
383
384
385
386
387 @Override
388 public Organisation createOrganisation(String name, String abbr,
389 Country country, Region region,
390 OrganisationType organisationType, Organisation parent) throws OrganisationAlreadyExistException {
391 if (parent != null) {
392
393 if (parent.findOrgByName(name) != null) {
394 throw new OrganisationAlreadyExistException();
395 }
396
397 Organisation newOrg = new Organisation();
398 newOrg.setParent(parent);
399 newOrg.setName(name);
400 newOrg.setAbbreviation(abbr);
401 newOrg.setCountry(country);
402 newOrg.setRegion(region);
403 newOrg.setOrganisationType(organisationType);
404 agentDAO.save(newOrg);
405 Role role = new Role();
406 role.setGroupAgent(newOrg);
407 role.setName(Organisation.OrganisationRoles.Member.toString());
408 newOrg.getDefinedRoles().add(role);
409 agentDAO.save(newOrg);
410 parent.getChildren().add(newOrg);
411 agentDAO.save(parent);
412 return newOrg;
413 } else {
414 Organisation newOrg = new Organisation();
415 newOrg.setName(name);
416 newOrg.setAbbreviation(abbr);
417 newOrg.setCountry(country);
418 newOrg.setRegion(region);
419 newOrg.setOrganisationType(organisationType);
420 agentDAO.save(newOrg);
421 Role role = new Role();
422 role.setGroupAgent(newOrg);
423 role.setName(Organisation.OrganisationRoles.Member.toString());
424 newOrg.getDefinedRoles().add(role);
425 agentDAO.save(newOrg);
426 return newOrg;
427 }
428 }
429
430
431
432
433
434
435
436
437 @Override
438 public Government createGovernment(String name,Long countryId ,
439 GovernmentType governmentType,String headOfstate) {
440
441 Country country = countryDAO.load(countryId);
442 Government government = new Government();
443 government.setName(name);
444 government.setCountry(country);
445 government.setRegion(country);
446 government.setGovernmentType(governmentType);
447 government.setOrganisationType(OrganisationType.National);
448 agentDAO.save(government);
449 Role governmentRole = new Role();
450 governmentRole.setGroupAgent(government);
451 governmentRole
452 .setName(Organisation.OrganisationRoles.Member.toString());
453 government.getDefinedRoles().add(governmentRole);
454 agentDAO.save(government);
455
456 Role headofstaterole = createRoleInOrg(
457 Government.GovernmentRoles.Head_of_State.toString(),
458 government);
459
460 Person headOfState = new Person();
461 headOfState.setName(headOfstate);
462 agentDAO.save(headOfState);
463 addMember(government, headOfState);
464 addRolePlayed(headofstaterole, headOfState);
465
466 Portal portal = portalDAO.findByName("Direct Democracy Portal");
467
468 GovernmentPortal governmentPortal = new GovernmentPortal();
469 governmentPortal
470 .setName("Direct Democracy Portal " + country.getName());
471 governmentPortal.setParent(portal);
472 governmentPortal.setCountry(country);
473 governmentPortal.setRegion(country);
474 governmentPortal.setOrganisationType(OrganisationType.National);
475 governmentPortal.setGovernment(government);
476 agentDAO.save(governmentPortal);
477 portal.getChildren().add(governmentPortal);
478 agentDAO.save(portal);
479
480 UserRole governmentPortalRole = new UserRole();
481 governmentPortalRole.setGroupAgent(governmentPortal);
482 governmentPortalRole.setName(Organisation.OrganisationRoles.Member
483 .toString());
484 governmentPortal.getDefinedRoles().add(governmentPortalRole);
485 governmentPortalRole.getRoleActions();
486 agentDAO.save(governmentPortal);
487
488 return government;
489 }
490
491
492
493
494 public Portal getGlobalPortal() {
495 Portal portal = portalDAO.findByName("Direct Democracy Portal");
496 portal.personsActive();
497
498 return portal;
499 }
500
501
502
503
504
505
506
507
508
509
510 @Override
511 public Government createRegionalGovernment(String name, Country country,
512 Region region, OrganisationType organisationType,
513 Government parentGovernment) {
514
515 Government government = new Government();
516 government.setParent(parentGovernment);
517 government.setName(name);
518 government.setCountry(country);
519 government.setRegion(region);
520 government.setGovernmentType(parentGovernment.getGovernmentType());
521 government.setOrganisationType(organisationType);
522 agentDAO.save(government);
523 Role role = new Role();
524 role.setGroupAgent(government);
525 role.setName(Organisation.OrganisationRoles.Member.toString());
526 government.getDefinedRoles().add(role);
527 agentDAO.save(government);
528 parentGovernment.getChildren().add(government);
529 agentDAO.save(parentGovernment);
530
531 GovernmentPortal parentGovernmentPortal = portalDAO
532 .findGovernmentPortal(parentGovernment);
533
534 GovernmentPortal governmentPortal = new GovernmentPortal();
535 governmentPortal.setName("Direct Democracy Portal "
536 + government.getName());
537 governmentPortal.setParent(parentGovernmentPortal);
538 governmentPortal.setCountry(country);
539 governmentPortal.setRegion(region);
540 governmentPortal.setOrganisationType(organisationType);
541 governmentPortal.setGovernment(government);
542 agentDAO.save(governmentPortal);
543 parentGovernmentPortal.getChildren().add(governmentPortal);
544 agentDAO.save(parentGovernmentPortal);
545
546 UserRole governmentPortalRole = new UserRole();
547 governmentPortalRole.setGroupAgent(governmentPortal);
548 governmentPortalRole.setName(Organisation.OrganisationRoles.Member
549 .toString());
550 governmentPortal.getDefinedRoles().add(governmentPortalRole);
551 governmentPortalRole.getRoleActions();
552 agentDAO.save(governmentPortal);
553
554 return government;
555 }
556
557
558
559
560 @Override
561 public void createResolution(Resolution resolution) {
562 documentDAO.save(resolution);
563 }
564
565
566
567
568 @Override
569 public void setResolutionDecidedDate(Resolution resolution, Date parseDate) {
570 System.out.println("Resolution decied date set " + parseDate);
571 if(!ResolutionState.Decided.equals(resolution.getResolutionState())) {
572 resolution.setResolutionState(ResolutionState.Decided);
573 resolution.setDecidedDate(parseDate);
574 documentDAO.save(resolution);
575 }
576 }
577
578
579
580
581 public List<Resolution> getDecidedResolutions() {
582 return documentDAO.getDecidedResolutionsInDateOrder();
583 }
584
585
586
587
588 @Override
589 public void addResolutionIssue(Resolution resolution, Issue issue,
590 VoteResult voteResult) {
591 if (!resolution.containsIssue(issue.getName())) {
592
593 issue.setVoteResult(voteResult);
594 voteResult.setIssue(issue);
595
596 issue.setResolution(resolution);
597 resolution.getIssues().add(issue);
598
599 documentDAO.save(resolution);
600 System.out.println("issue saved " + issue.getName());
601 } else {
602 System.out.println("issue ignored " + issue.getName());
603 }
604 }
605
606
607
608
609 @Override
610 public void updateVoteResult(VoteResult voteResult) {
611 documentDAO.save(voteResult);
612 }
613
614
615
616
617 @Override
618 public Organisation createOrgUnit(String orgName, String shortName,
619 OrganisationType organisationType, Long orgId) throws OrganisationAlreadyExistException {
620 Organisation org = (Organisation) agentDAO.load(orgId);
621
622 return createOrganisation(orgName, shortName, org.getCountry(), org.getRegion(), organisationType, org);
623 }
624
625
626
627
628 @Override
629 public Organisation findOrgByName(String name) {
630 return (Organisation) agentDAO.findByName(name);
631 }
632
633
634
635
636 @Override
637 public Person findMemberByNameInOrg(Long orgId, String name) {
638 Organisation organisation = (Organisation) agentDAO.load(orgId);
639 return organisation.findMemberByName(name);
640 }
641
642
643
644
645 @Override
646 public void addMemberById(Long orgId, Long personId) {
647 Organisation organisation = (Organisation) agentDAO.load(orgId);
648 Person person = (Person) agentDAO.load(personId);
649 addMember(organisation, person);
650 }
651
652
653
654
655 @Override
656 public void addRolePlayedById(Long roleId, Long agentId) {
657 Role role = roleDAO.load(roleId);
658 Person person = (Person) agentDAO.load(agentId);
659 addRolePlayed(role, person);
660 }
661
662
663
664
665 @Override
666 public Country createCountry(String name) {
667 Region world = countryDAO.findRegionByName("World");
668
669 Country country = new Country();
670 country.setName(name);
671 country.setParent(world);
672 country.setRegionType(RegionType.National);
673 countryDAO.save(country);
674 world.getParts().add(country);
675
676 countryDAO.saveRegion(world);
677 return country;
678 }
679
680
681
682
683 @Override
684 public Country findCountryByName(String name) {
685 return countryDAO.findByName(name);
686 }
687
688
689
690
691 public void createElection(String name, ElectionType electionType,Long governmentid) {
692 Government government = governmentDAO.load(governmentid);
693 Election election = new Election();
694 election.setName(name);
695 election.setType(electionType);
696 election.setGovernment(government);
697 government.getElections().add(election);
698 agentDAO.save(government);
699 }
700
701
702
703
704 @Override
705 public PoliticalParty createPoliticalPartyById(String name,
706 String shortCode, Long countryId) {
707 Country country = countryDAO.load(countryId);
708
709 return createPoliticalParty(name, shortCode, country, country);
710 }
711
712
713
714
715 @Override
716 public void createOrganisationById(String name, String abbr,
717 Long countryId,Long regionId, OrganisationType organisationType, Long orgId) throws DataAccessException, OrganisationAlreadyExistException {
718
719 Country country = countryDAO.load(countryId);
720 Region region = regionDAO.load(regionId);
721
722 createOrganisation(name, abbr, country, region, organisationType,(Organisation) agentDAO.load(orgId));
723 }
724
725
726
727
728 @Override
729 public PoliticalParty findPartyByShortCode(String shortCode) {
730 return politicalPartyDAO.findByShortCode(shortCode);
731 }
732
733
734
735
736 @Override
737 public Person createPerson(String name) {
738 Person person= new Person();
739 person.setName(name);
740 agentDAO.save(person);
741 return person;
742 }
743
744
745
746
747 @Override
748 public Region findRegionByName(String name) {
749 return regionDAO.findByName(name);
750 }
751
752 }