1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
package org.directdemocracyportal.democracy.service.dao.hibernate; |
6 | |
|
7 | |
import java.util.List; |
8 | |
|
9 | |
import org.directdemocracyportal.democracy.model.application.GovernmentPortal; |
10 | |
import org.directdemocracyportal.democracy.model.application.Portal; |
11 | |
import org.directdemocracyportal.democracy.model.world.Country; |
12 | |
import org.directdemocracyportal.democracy.model.world.Government; |
13 | |
import org.directdemocracyportal.democracy.model.world.OrganisationType; |
14 | |
import org.directdemocracyportal.democracy.model.world.Region; |
15 | |
import org.directdemocracyportal.democracy.service.dao.PortalDAO; |
16 | |
import org.hibernate.criterion.DetachedCriteria; |
17 | |
import org.hibernate.criterion.Restrictions; |
18 | |
import org.springframework.dao.DataAccessException; |
19 | |
import org.springframework.transaction.annotation.Propagation; |
20 | |
import org.springframework.transaction.annotation.Transactional; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
@Transactional(propagation = Propagation.MANDATORY) |
26 | |
public class HibernatePortalDAO extends GenericHibernateDAO<Portal, Long> |
27 | |
implements PortalDAO |
28 | |
{ |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public HibernatePortalDAO() { |
34 | 1 | super(Portal.class); |
35 | 1 | } |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
@Transactional(readOnly = true) |
43 | |
public Portal findByName(String name) throws DataAccessException { |
44 | 0 | DetachedCriteria findByName = DetachedCriteria.forClass(Portal.class); |
45 | 0 | findByName.add(Restrictions.eq("name", name)); |
46 | |
|
47 | |
@SuppressWarnings("unchecked") |
48 | 0 | List<Portal> result = this.getHibernateTemplate().findByCriteria( |
49 | |
findByName); |
50 | |
|
51 | 0 | return result.isEmpty() ? null : result.get(0); |
52 | |
} |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
@Override |
60 | |
public GovernmentPortal findGovernmentPortal(Government government) { |
61 | 0 | DetachedCriteria findByGovernment = DetachedCriteria |
62 | |
.forClass(GovernmentPortal.class); |
63 | 0 | findByGovernment.add(Restrictions.eq("government.id", government |
64 | |
.getId())); |
65 | |
|
66 | |
@SuppressWarnings("unchecked") |
67 | 0 | List<GovernmentPortal> result = this.getHibernateTemplate() |
68 | |
.findByCriteria(findByGovernment); |
69 | |
|
70 | 0 | return result.isEmpty() ? null : result.get(0); |
71 | |
} |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
@Override |
79 | |
public Portal findNationalByCountry(Country country) { |
80 | 0 | DetachedCriteria findByCountry = DetachedCriteria |
81 | |
.forClass(GovernmentPortal.class); |
82 | 0 | findByCountry.add(Restrictions.eq("country.id", country.getId())); |
83 | 0 | findByCountry.add(Restrictions.eq("organisationType", |
84 | |
OrganisationType.National)); |
85 | |
|
86 | |
@SuppressWarnings("unchecked") |
87 | 0 | List<GovernmentPortal> result = this.getHibernateTemplate() |
88 | |
.findByCriteria(findByCountry); |
89 | |
|
90 | 0 | return result.isEmpty() ? null : result.get(0); |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
@Override |
99 | |
public Portal findRegionalByRegion(Region region) { |
100 | 0 | DetachedCriteria findByRegion = DetachedCriteria |
101 | |
.forClass(GovernmentPortal.class); |
102 | 0 | findByRegion.add(Restrictions.eq("region.id", region.getId())); |
103 | 0 | findByRegion.add(Restrictions.eq("organisationType", |
104 | |
OrganisationType.Regional)); |
105 | |
|
106 | |
@SuppressWarnings("unchecked") |
107 | 0 | List<GovernmentPortal> result = this.getHibernateTemplate() |
108 | |
.findByCriteria(findByRegion); |
109 | |
|
110 | 0 | return result.isEmpty() ? null : result.get(0); |
111 | |
} |
112 | |
} |