Rev 1396 | Rev 1404 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1332 | jmachado | 1 | package pt.estgp.estgweb.domain.dao.impl; |
2 | |||
1338 | jmachado | 3 | import jomm.dao.impl.AbstractDao; |
1399 | jmachado | 4 | import org.apache.log4j.Logger; |
1338 | jmachado | 5 | import org.hibernate.Query; |
1380 | jmachado | 6 | import org.hibernate.criterion.Projections; |
1333 | jmachado | 7 | import pt.estgp.estgweb.domain.QuestionarioPedagogicoCursoAfeto; |
8 | |||
9 | import java.util.List; |
||
10 | |||
11 | import static org.hibernate.criterion.Restrictions.eq; |
||
12 | |||
1332 | jmachado | 13 | /** |
14 | * Created by jorgemachado on 24/03/16. |
||
15 | */ |
||
16 | public class QuestionarioPedagogicoCursoAfetoDaoImpl extends QuestionarioPedagogicoCursoAfetoDao{ |
||
17 | |||
1399 | jmachado | 18 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(QuestionarioPedagogicoCursoAfetoDaoImpl.class); |
19 | |||
1332 | jmachado | 20 | public static QuestionarioPedagogicoCursoAfetoDaoImpl getInstance() { |
21 | if (myInstance == null) |
||
22 | myInstance = new QuestionarioPedagogicoCursoAfetoDaoImpl(); |
||
23 | return (QuestionarioPedagogicoCursoAfetoDaoImpl) myInstance; |
||
24 | } |
||
1333 | jmachado | 25 | |
26 | public List<QuestionarioPedagogicoCursoAfeto> find(long questionarioId,1.5.0/docs/api/java/lang/String.html">String cursoCode) |
||
27 | { |
||
28 | return createCriteria() |
||
29 | .add(eq("questionario.id", questionarioId)) |
||
30 | .add(eq("codigoCurso", cursoCode)) |
||
31 | .list(); |
||
32 | } |
||
33 | public List<QuestionarioPedagogicoCursoAfeto> find(long questionarioId) |
||
34 | { |
||
35 | return createCriteria() |
||
36 | .add(eq("questionario.id", questionarioId)) |
||
37 | .list(); |
||
38 | } |
||
1338 | jmachado | 39 | |
1380 | jmachado | 40 | public List<Long> findCursosAfetosIds(long questionarioId) |
41 | { |
||
42 | return createCriteria() |
||
43 | .setProjection(Projections.distinct(Projections.property("id"))) |
||
44 | .add(eq("questionario.id", questionarioId)) |
||
45 | .list(); |
||
46 | } |
||
1338 | jmachado | 47 | |
48 | |||
49 | |||
1380 | jmachado | 50 | |
1338 | jmachado | 51 | public static class CourseStatsVars |
52 | { |
||
53 | public long unidades; |
||
54 | public long marcadas; |
||
55 | public long observadas; |
||
1378 | jmachado | 56 | public long porVerProfs; |
1338 | jmachado | 57 | public long porVer; |
58 | public long naoUsadas; |
||
59 | } |
||
60 | |||
1399 | jmachado | 61 | public static class QuestionarioStatsVars |
62 | { |
||
63 | public long cursos; |
||
64 | public long unidades; |
||
65 | public long profs; |
||
66 | public long alunos; |
||
67 | public long turmas; |
||
68 | public long alunosDeUsadas; |
||
69 | public long profsDeUsadas; |
||
70 | public long unidadesUsadas; |
||
71 | } |
||
72 | |||
1338 | jmachado | 73 | public CourseStatsVars loadStats(long cursoAfetoId) |
74 | { |
||
75 | CourseStatsVars courseStatsVars = new CourseStatsVars(); |
||
76 | 1.5.0/docs/api/javax/management/Query.html">Query q = AbstractDao.getCurrentSession().createQuery("SELECT " + |
||
77 | " sum (case when u.vista = false then 1 else 0 end) AS porVer " + |
||
1378 | jmachado | 78 | ",sum (case when u.vistaProfs = false then 1 else 0 end) AS porVerProfs " + |
1338 | jmachado | 79 | ",sum (case when u.usar = false then 1 else 0 end) AS naoUsadas " + |
80 | ",sum (case when u.marked = true then 1 else 0 end) AS marked " + |
||
81 | ",sum (case when u.obs is not NULL and u.obs != '' then 1 else 0 end) AS observadas " + |
||
82 | ",count(distinct u.id) AS unidades " + |
||
83 | "from " + QuestionarioPedagogicoCursoAfeto.class.getName() + " c " + |
||
84 | "JOIN c.unidadesAfetas u " + |
||
85 | "WHERE c.id = :id " + |
||
86 | "GROUP BY c.id"); |
||
87 | |||
88 | q.setLong("id",cursoAfetoId); |
||
89 | 5+0%2Fdocs%2Fapi+Object">Object[] results = (5+0%2Fdocs%2Fapi+Object">Object[]) q.uniqueResult(); |
||
90 | |||
1396 | jmachado | 91 | if(results != null && results[0] != null) |
1395 | jmachado | 92 | { |
93 | courseStatsVars.porVer = (1.5.0/docs/api/java/lang/Long.html">Long) results[0]; |
||
94 | courseStatsVars.porVerProfs = (1.5.0/docs/api/java/lang/Long.html">Long) results[1]; |
||
95 | courseStatsVars.naoUsadas = (1.5.0/docs/api/java/lang/Long.html">Long) results[2]; |
||
96 | courseStatsVars.marcadas = (1.5.0/docs/api/java/lang/Long.html">Long) results[3]; |
||
97 | courseStatsVars.observadas = (1.5.0/docs/api/java/lang/Long.html">Long) results[4]; |
||
98 | courseStatsVars.unidades = (1.5.0/docs/api/java/lang/Long.html">Long) results[5]; |
||
99 | } |
||
100 | |||
1338 | jmachado | 101 | return courseStatsVars; |
102 | } |
||
1399 | jmachado | 103 | |
104 | public QuestionarioStatsVars loadQuestionarioStats(long questionarioId) |
||
105 | { |
||
106 | QuestionarioStatsVars questionarioStatsVars = new QuestionarioStatsVars(); |
||
107 | 1.5.0/docs/api/javax/management/Query.html">Query q = AbstractDao.getCurrentSession().createQuery("SELECT " + |
||
108 | " count(distinct c.id) AS cursos " + |
||
109 | ",count(distinct u.id) AS unidades " + |
||
110 | ",sum (u.profs) AS profs " + |
||
111 | ",sum (u.turmas) AS turmas " + |
||
112 | ",sum (u.students) AS students " + |
||
113 | ",sum (case when u.usar = true then u.students else 0 end) AS alunosDeUsadas " + |
||
114 | ",sum (case when u.usar = true then u.profs else 0 end) AS profsDeUsadas " + |
||
115 | ",sum (case when u.usar = true then 1 else 0 end) AS unidadesUsadas " + |
||
116 | ",sum (case when u.usar = true then 1 else 1 end) AS unidadesTodasCheck " + |
||
117 | "from " + QuestionarioPedagogicoCursoAfeto.class.getName() + " c " + |
||
118 | "JOIN c.questionario q " + |
||
119 | "JOIN c.unidadesAfetas u " + |
||
120 | "WHERE q.id = :id " + |
||
121 | "GROUP BY u.id"); |
||
122 | |||
123 | q.setLong("id",questionarioId); |
||
124 | 5+0%2Fdocs%2Fapi+Object">Object[] results = (5+0%2Fdocs%2Fapi+Object">Object[]) q.uniqueResult(); |
||
125 | |||
126 | if(results != null && results[0] != null) |
||
127 | { |
||
128 | questionarioStatsVars.cursos = (1.5.0/docs/api/java/lang/Long.html">Long) results[0]; |
||
129 | questionarioStatsVars.unidades = (1.5.0/docs/api/java/lang/Long.html">Long) results[1]; |
||
130 | questionarioStatsVars.profs = (1.5.0/docs/api/java/lang/Long.html">Long) results[2]; |
||
131 | questionarioStatsVars.turmas = (1.5.0/docs/api/java/lang/Long.html">Long) results[3]; |
||
132 | questionarioStatsVars.alunos = (1.5.0/docs/api/java/lang/Long.html">Long) results[4]; |
||
133 | questionarioStatsVars.alunosDeUsadas = (1.5.0/docs/api/java/lang/Long.html">Long) results[5]; |
||
134 | questionarioStatsVars.profsDeUsadas = (1.5.0/docs/api/java/lang/Long.html">Long) results[6]; |
||
135 | questionarioStatsVars.unidadesUsadas = (1.5.0/docs/api/java/lang/Long.html">Long) results[7]; |
||
136 | |||
137 | if(((1.5.0/docs/api/java/lang/Long.html">Long) results[8]) != questionarioStatsVars.unidades) |
||
138 | logger.error("TEM UM ERRO NA QUERY, a contagem de usadas com o distinct da diferente de com o SUM"); |
||
139 | |||
140 | } |
||
141 | |||
142 | return questionarioStatsVars; |
||
143 | } |
||
1332 | jmachado | 144 | } |