Rev 1399 | Rev 1521 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | fvelez | 1 | package pt.estgp.estgweb.domain.dao.impl; |
345 | jmachado | 2 | |
214 | jmachado | 3 | import org.hibernate.Criteria; |
345 | jmachado | 4 | import org.hibernate.Query; |
1496 | jmachado | 5 | import org.hibernate.criterion.Criterion; |
6 | import org.hibernate.criterion.Expression; |
||
7 | import org.hibernate.criterion.Order; |
||
8 | import org.hibernate.criterion.Projections; |
||
163 | jmachado | 9 | import pt.estgp.estgweb.domain.Course; |
10 | import pt.estgp.estgweb.domain.CourseImpl; |
||
682 | jmachado | 11 | import pt.estgp.estgweb.domain.CourseUnit; |
1327 | jmachado | 12 | import pt.estgp.estgweb.domain.CourseUnitTurma; |
995 | jmachado | 13 | import pt.estgp.estgweb.domain.dao.DaoFactory; |
214 | jmachado | 14 | import pt.estgp.estgweb.domain.dao.DaoUtils; |
680 | jmachado | 15 | import pt.estgp.estgweb.services.common.SearchTypeEnum; |
1 | fvelez | 16 | |
214 | jmachado | 17 | import java.util.List; |
18 | |||
995 | jmachado | 19 | import static org.hibernate.criterion.Restrictions.eq; |
20 | import static org.hibernate.criterion.Restrictions.not; |
||
680 | jmachado | 21 | |
1 | fvelez | 22 | /** |
23 | * @author Jorge Machado |
||
24 | * @date 28/Fev/2008 |
||
25 | * @time 2:51:06 |
||
26 | * @see pt.estgp.estgweb.domain.dao.impl |
||
27 | */ |
||
28 | public class CourseDaoImpl extends CourseDao |
||
29 | { |
||
30 | public static CourseDaoImpl getInstance() |
||
31 | { |
||
32 | if (myInstance == null) |
||
33 | myInstance = new CourseDaoImpl(); |
||
34 | return (CourseDaoImpl) myInstance; |
||
35 | } |
||
36 | |||
163 | jmachado | 37 | public CourseImpl findCourseByCode(1.5.0/docs/api/java/lang/String.html">String code) |
38 | { |
||
345 | jmachado | 39 | return (CourseImpl) createCriteria().add(eq("code", code)).uniqueResult(); |
163 | jmachado | 40 | } |
41 | |||
444 | jmachado | 42 | public CourseImpl findCourseByCodeAndYear(1.5.0/docs/api/java/lang/String.html">String code, 1.5.0/docs/api/java/lang/String.html">String year) |
43 | { |
||
44 | return (CourseImpl) createCriteria().add(eq("code", code)).add(eq("importYear", year)).uniqueResult(); |
||
45 | } |
||
46 | |||
47 | |||
214 | jmachado | 48 | public int countCoursesActualYear(1.5.0/docs/api/java/lang/String.html">String query, SearchTypeEnum searchType) |
49 | { |
||
345 | jmachado | 50 | Criterion c = DaoUtils.createSearchQuery(query, searchType, "name", "description"); |
214 | jmachado | 51 | return createCriteria() |
52 | .add(c) |
||
995 | jmachado | 53 | .add(eq("importYear", DaoFactory.getConfigurationDaoImpl().getInterfaceImportYear())) |
214 | jmachado | 54 | .list().size(); |
55 | } |
||
56 | |||
1198 | jmachado | 57 | public int countCoursesUnits(1.5.0/docs/api/java/lang/String.html">String importYear,long courseId) |
58 | { |
||
1327 | jmachado | 59 | /* return createCriteria() |
1198 | jmachado | 60 | .createAlias("courseUnits", "c") |
61 | .setProjection(Projections.property("c.id")) |
||
62 | .add(eq("c.importYear", importYear)) |
||
63 | .add(eq("id", courseId)) |
||
64 | .list().size(); |
||
1327 | jmachado | 65 | */ |
66 | return (1.5.0/docs/api/java/lang/Integer.html">Integer) createCriteria(CourseUnit.class) |
||
67 | .setProjection(Projections.rowCount()) |
||
68 | .add(eq("importYear",importYear)) |
||
69 | .add(eq("course.id",courseId)) |
||
70 | .uniqueResult(); |
||
71 | } |
||
72 | |||
1395 | jmachado | 73 | public int countCoursesUnits(1.5.0/docs/api/java/lang/String.html">String importYear,long courseId,1.5.0/docs/api/java/lang/String.html">String semestre) |
74 | { |
||
75 | /* return createCriteria() |
||
76 | .createAlias("courseUnits", "c") |
||
77 | .setProjection(Projections.property("c.id")) |
||
78 | .add(eq("c.importYear", importYear)) |
||
79 | .add(eq("id", courseId)) |
||
80 | .list().size(); |
||
81 | */ |
||
82 | return (1.5.0/docs/api/java/lang/Integer.html">Integer) createCriteria(CourseUnit.class) |
||
83 | .setProjection(Projections.rowCount()) |
||
84 | .add(eq("importYear",importYear)) |
||
85 | .add(eq("semestre",semestre)) |
||
86 | .add(eq("course.id",courseId)) |
||
87 | .uniqueResult(); |
||
88 | } |
||
1327 | jmachado | 89 | |
1395 | jmachado | 90 | |
1327 | jmachado | 91 | public int countCoursesUnitsTurmas(1.5.0/docs/api/java/lang/String.html">String importYear,long courseId) |
92 | { |
||
93 | return (1.5.0/docs/api/java/lang/Integer.html">Integer) createCriteria(CourseUnitTurma.class) |
||
94 | .setProjection(Projections.countDistinct("turma")) |
||
95 | .createAlias("courseUnit", "courseUnit") |
||
1333 | jmachado | 96 | .createAlias("students", "student") |
1327 | jmachado | 97 | .add(eq("courseUnit.importYear",importYear)) |
98 | .add(eq("courseUnit.course.id",courseId)) |
||
99 | .uniqueResult(); |
||
100 | } |
||
101 | |||
1198 | jmachado | 102 | public int countCoursesUnitsNot(1.5.0/docs/api/java/lang/String.html">String importYear,long courseId) |
103 | { |
||
104 | return createCriteria() |
||
105 | .createAlias("courseUnits", "c") |
||
106 | .setProjection(Projections.property("c.id")) |
||
107 | .add(not(eq("c.importYear", importYear))) |
||
108 | .add(eq("id", courseId)) |
||
109 | .list().size(); |
||
110 | } |
||
111 | |||
112 | public List<Long> findCoursesUnitsIds(1.5.0/docs/api/java/lang/String.html">String importYear,long courseId) |
||
113 | { |
||
114 | return createCriteria() |
||
115 | .createAlias("courseUnits", "c") |
||
116 | .setProjection(Projections.property("c.id")) |
||
117 | .add(eq("id", courseId)) |
||
118 | .add(eq("c.importYear", importYear)) |
||
119 | .addOrder(Order.asc("c.name")) |
||
120 | .list(); |
||
121 | } |
||
122 | |||
123 | public List<Long> findCoursesUnitsIds(long courseId) |
||
124 | { |
||
125 | return createCriteria() |
||
126 | .createAlias("courseUnits","c") |
||
127 | .setProjection(Projections.property("c.id")) |
||
128 | .add(eq("id", courseId)) |
||
129 | .addOrder(Order.asc("c.importYear")) |
||
130 | .addOrder(Order.asc("c.name")) |
||
131 | .list(); |
||
132 | } |
||
133 | |||
214 | jmachado | 134 | public List<Course> searchActualYear(1.5.0/docs/api/java/lang/String.html">String query, SearchTypeEnum searchTypeEnum, int max, int page) |
135 | { |
||
136 | Criterion c = DaoUtils.createSearchQuery(query, searchTypeEnum, "name", "description"); |
||
137 | Criteria criteria = createCriteria(); |
||
138 | criteria.add(c) |
||
995 | jmachado | 139 | .add(eq("importYear", DaoFactory.getConfigurationDaoImpl().getInterfaceImportYear())) |
214 | jmachado | 140 | .addOrder(Order.asc("name")) |
141 | .setMaxResults(max) |
||
142 | .setFirstResult(page * max); |
||
143 | return criteria.list(); |
||
144 | } |
||
145 | |||
146 | |||
147 | public List<Course> findAllOrderByName() |
||
148 | { |
||
249 | jmachado | 149 | return createCriteria() |
150 | .addOrder(Order.asc("name")) |
||
151 | .list(); |
||
214 | jmachado | 152 | } |
153 | |||
376 | jmachado | 154 | public List<Course> findAllImportYearOrderByName(1.5.0/docs/api/java/lang/String.html">String importYear, 1.5.0/docs/api/java/lang/String.html">String area) |
155 | { |
||
156 | Criteria c = createCriteria(); |
||
157 | if (importYear != null && importYear.length() > 0) |
||
158 | { |
||
159 | c.add(eq("importYear", importYear)); |
||
160 | } |
||
161 | if (area != null && area.length() > 0) |
||
162 | { |
||
163 | c.add(eq("area", area)); |
||
164 | } |
||
165 | return c.addOrder(Order.asc("name")) |
||
166 | .list(); |
||
167 | } |
||
168 | |||
249 | jmachado | 169 | public List<Course> findAllOrderByName(1.5.0/docs/api/java/lang/String.html">String importYear) |
170 | { |
||
171 | return createCriteria() |
||
680 | jmachado | 172 | .add(eq("cu.importYear", importYear)) |
249 | jmachado | 173 | .addOrder(Order.asc("name")) |
174 | .list(); |
||
175 | } |
||
176 | |||
695 | jmachado | 177 | public List<Course> findAllOrderByNameEvenWithoutCourseUnit(1.5.0/docs/api/java/lang/String.html">String importYear, 1.5.0/docs/api/java/lang/String.html">String area, 1.5.0/docs/api/java/lang/String.html">String type) |
178 | { |
||
179 | Criteria c = createCriteria(); |
||
180 | |||
181 | /* c.setProjection(Projections.projectionList() |
||
182 | .add(Projections.distinct(Projections.id())));*/ |
||
183 | if (importYear != null && importYear.length() > 0) |
||
184 | c.add(eq("importYear", importYear)); |
||
185 | if (area != null && area.length() > 0) |
||
186 | c.add(eq("area", area)); |
||
187 | if (type != null && type.length() > 0) |
||
188 | c.add(eq("degree", type)); |
||
189 | return c.addOrder(Order.asc("name")) |
||
190 | .list(); |
||
191 | } |
||
192 | |||
1496 | jmachado | 193 | public List<Course> findAllActiveOrderByNameEvenWithoutCourseUnit() |
194 | { |
||
195 | Criteria c = createCriteria(); |
||
196 | |||
197 | /* c.setProjection(Projections.projectionList() |
||
198 | .add(Projections.distinct(Projections.id())));*/ |
||
199 | c.add(eq("status", true)); |
||
200 | return c.addOrder(Order.asc("name")) |
||
201 | .list(); |
||
202 | } |
||
203 | |||
204 | public List<Course> findAllActiveOrderByNameEvenWithoutCourseUnit(1.5.0/docs/api/java/lang/String.html">String institutionalCode, 1.5.0/docs/api/java/lang/String.html">String degree) |
||
205 | { |
||
206 | Criteria c = createCriteria(); |
||
207 | |||
208 | c.add(eq("status", true)); |
||
209 | |||
210 | if (degree != null && degree.length() > 0) |
||
211 | c.add(eq("degree", degree)); |
||
212 | |||
213 | if (institutionalCode != null && institutionalCode.length() > 0) |
||
214 | c.add(eq("institutionalCode", institutionalCode)); |
||
215 | |||
216 | return c.addOrder(Order.asc("name")) |
||
217 | .list(); |
||
218 | } |
||
219 | |||
790 | jmachado | 220 | public List<Course> findAllActiveOrderByNameEvenWithoutCourseUnit(1.5.0/docs/api/java/lang/String.html">String importYear, 1.5.0/docs/api/java/lang/String.html">String area, 1.5.0/docs/api/java/lang/String.html">String type) |
221 | { |
||
222 | Criteria c = createCriteria(); |
||
223 | |||
224 | /* c.setProjection(Projections.projectionList() |
||
225 | .add(Projections.distinct(Projections.id())));*/ |
||
226 | if (importYear != null && importYear.length() > 0) |
||
227 | c.add(eq("status", true)); |
||
228 | if (area != null && area.length() > 0) |
||
229 | c.add(eq("area", area)); |
||
230 | if (type != null && type.length() > 0) |
||
231 | c.add(eq("degree", type)); |
||
232 | return c.addOrder(Order.asc("name")) |
||
233 | .list(); |
||
234 | } |
||
235 | |||
1326 | jmachado | 236 | public List<Course> findCoursesWithUnitsIn(1.5.0/docs/api/java/lang/String.html">String importYear) |
237 | { |
||
238 | return findAllOrderByName(importYear,null,null,null); |
||
239 | } |
||
790 | jmachado | 240 | |
1326 | jmachado | 241 | public List<String> loadInstitutionCodes(1.5.0/docs/api/java/lang/String.html">String importYear) |
242 | { |
||
243 | return |
||
244 | createCriteria() |
||
245 | .setProjection(Projections |
||
246 | .distinct(Projections.projectionList() |
||
247 | .add(Projections.property("institutionCode")))) |
||
248 | .add(1.5.0/docs/api/java/beans/Expression.html">Expression.isNotNull("institutionCode")) |
||
249 | .list(); |
||
250 | } |
||
1327 | jmachado | 251 | public List<Course> findCoursesWithUnitsIn(1.5.0/docs/api/java/lang/String.html">String importYear,1.5.0/docs/api/java/lang/String.html">String institutionCode, 1.5.0/docs/api/java/lang/String.html">String semestre) |
252 | { |
||
253 | return findAllOrderByName(importYear,null,null,institutionCode,semestre); |
||
254 | } |
||
1326 | jmachado | 255 | public List<Course> findCoursesWithUnitsIn(1.5.0/docs/api/java/lang/String.html">String importYear,1.5.0/docs/api/java/lang/String.html">String institutionCode) |
256 | { |
||
257 | return findAllOrderByName(importYear,null,null,institutionCode); |
||
258 | } |
||
259 | |||
417 | jmachado | 260 | public List<Course> findAllOrderByName(1.5.0/docs/api/java/lang/String.html">String importYear, 1.5.0/docs/api/java/lang/String.html">String area, 1.5.0/docs/api/java/lang/String.html">String type) |
376 | jmachado | 261 | { |
1312 | jmachado | 262 | return findAllOrderByName(importYear,area,type,null); |
263 | } |
||
1326 | jmachado | 264 | |
1312 | jmachado | 265 | public List<Course> findAllOrderByName(1.5.0/docs/api/java/lang/String.html">String importYear, 1.5.0/docs/api/java/lang/String.html">String area, 1.5.0/docs/api/java/lang/String.html">String type,1.5.0/docs/api/java/lang/String.html">String institutionCode) |
266 | { |
||
1327 | jmachado | 267 | return findAllOrderByName(importYear,area,type,institutionCode,null); |
268 | } |
||
269 | public List<Course> findAllOrderByName(1.5.0/docs/api/java/lang/String.html">String importYear, 1.5.0/docs/api/java/lang/String.html">String area, 1.5.0/docs/api/java/lang/String.html">String type,1.5.0/docs/api/java/lang/String.html">String institutionCode, 1.5.0/docs/api/java/lang/String.html">String semestre) |
||
270 | { |
||
682 | jmachado | 271 | |
1054 | jmachado | 272 | 1.5.0/docs/api/java/lang/String.html">String query = "select distinct c from c in class Course join c.courseUnits cu where "; |
273 | 1.5.0/docs/api/java/lang/String.html">String logic = " "; |
||
274 | if (importYear != null && importYear.length() > 0) |
||
275 | { |
||
276 | query += logic + "cu.importYear = :importYear "; |
||
277 | logic = " and "; |
||
278 | } |
||
1312 | jmachado | 279 | if (institutionCode != null && institutionCode.trim().length() > 0) |
280 | { |
||
1399 | jmachado | 281 | query += logic + "c.institutionalCode = :institutionCode "; |
1312 | jmachado | 282 | logic = " and "; |
283 | } |
||
376 | jmachado | 284 | if (area != null && area.length() > 0) |
1054 | jmachado | 285 | { |
286 | query += logic + "c.area = :area "; |
||
287 | logic = " and "; |
||
288 | } |
||
417 | jmachado | 289 | if (type != null && type.length() > 0) |
1054 | jmachado | 290 | { |
291 | query += logic + "c.degree = :degree "; |
||
292 | logic = " and "; |
||
293 | } |
||
1327 | jmachado | 294 | if (semestre != null && semestre.length() > 0) |
295 | { |
||
296 | query += logic + "cu.semestre = :semestre "; |
||
297 | logic = " and "; |
||
298 | } |
||
1054 | jmachado | 299 | query += " order by c.name asc"; |
300 | |||
301 | 1.5.0/docs/api/javax/management/Query.html">Query q = getCurrentSession().createQuery(query); |
||
302 | if (importYear != null && importYear.length() > 0) |
||
303 | { |
||
304 | q.setString("importYear",importYear); |
||
305 | } |
||
1312 | jmachado | 306 | if (institutionCode != null && institutionCode.trim().length() > 0) |
307 | { |
||
308 | q.setString("institutionCode",institutionCode); |
||
309 | } |
||
1054 | jmachado | 310 | if (area != null && area.length() > 0) |
311 | { |
||
312 | q.setString("area",area); |
||
313 | } |
||
314 | if (type != null && type.length() > 0) |
||
315 | { |
||
316 | q.setString("degree",type); |
||
317 | } |
||
1327 | jmachado | 318 | if (semestre != null && semestre.length() > 0) |
319 | { |
||
320 | q.setString("semestre",semestre); |
||
321 | } |
||
1054 | jmachado | 322 | /* c.setProjection(Projections.projectionList() |
323 | .add(Projections.distinct(Projections.id())));*/ |
||
324 | return q.list(); |
||
376 | jmachado | 325 | } |
326 | |||
417 | jmachado | 327 | |
328 | |||
345 | jmachado | 329 | public List<String> loadImportYears() |
330 | { |
||
682 | jmachado | 331 | 1.5.0/docs/api/javax/management/Query.html">Query q = createQuery("select distinct c.importYear from c in class " + CourseUnit.class.getName() + " order by c.importYear desc"); |
345 | jmachado | 332 | return q.list(); |
333 | } |
||
334 | |||
335 | public List<Course> loadAllCourses(long teacherId, 1.5.0/docs/api/java/lang/String.html">String importYear) |
||
336 | { |
||
337 | return |
||
338 | createCriteria() |
||
339 | .add(eq("importYear", importYear)) |
||
340 | .list(); |
||
341 | } |
||
342 | |||
343 | public List<Course> loadCoordinatedCourses(long teacherId, 1.5.0/docs/api/java/lang/String.html">String importYear) |
||
344 | { |
||
345 | return |
||
346 | createCriteria() |
||
347 | .add(eq("coordinator.id", teacherId)) |
||
348 | .add(eq("importYear", importYear)) |
||
349 | .list(); |
||
350 | } |
||
351 | |||
352 | public List<Course> loadDirectorCourses(long teacherId, 1.5.0/docs/api/java/lang/String.html">String importYear) |
||
353 | { |
||
354 | return |
||
355 | createCriteria() |
||
356 | .add(eq("director.id", teacherId)) |
||
357 | .add(eq("importYear", importYear)) |
||
358 | .list(); |
||
359 | } |
||
360 | |||
214 | jmachado | 361 | public List<Course> findAll(1.5.0/docs/api/java/lang/String.html">String importYear) |
362 | { |
||
1189 | jmachado | 363 | return createCriteria().add(eq("importYear", importYear)).addOrder(Order.asc("name")) |
249 | jmachado | 364 | .list(); |
214 | jmachado | 365 | } |
366 | |||
1171 | jmachado | 367 | public List<Course> findAllStatus(1.5.0/docs/api/java/lang/String.html">String importYear,boolean status) |
368 | { |
||
369 | return createCriteria() |
||
370 | .add(eq("importYear", importYear)) |
||
371 | .add(eq("status", status)) |
||
1183 | jmachado | 372 | .addOrder(Order.asc("name")) |
1171 | jmachado | 373 | .list(); |
374 | } |
||
375 | |||
376 | public List<Course> findActiveCourses(1.5.0/docs/api/java/lang/String.html">String importYear) |
||
377 | { |
||
378 | return findAllStatus(importYear,true); |
||
379 | } |
||
380 | |||
961 | jmachado | 381 | public List<Course> findAllNotIn(1.5.0/docs/api/java/lang/String.html">String importYear) |
382 | { |
||
383 | return createCriteria().add(not(eq("importYear", importYear))) |
||
384 | .list(); |
||
385 | } |
||
386 | |||
799 | jmachado | 387 | |
388 | |||
684 | jmachado | 389 | // Duarte Santos |
671 | jmachado | 390 | public List<String> loadAreas() |
391 | { |
||
392 | return |
||
393 | createCriteria() |
||
394 | .setProjection(Projections |
||
395 | .distinct(Projections.projectionList() |
||
396 | .add(Projections.property("area")))) |
||
397 | .add(1.5.0/docs/api/java/beans/Expression.html">Expression.isNotNull("area")) |
||
398 | .list(); |
||
399 | } |
||
1496 | jmachado | 400 | |
401 | |||
1 | fvelez | 402 | } |