1 package ejava.examples.webtier.bl;
2
3 import java.util.List;
4 import java.util.Map;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8
9 import ejava.examples.webtier.bo.Grade;
10 import ejava.examples.webtier.bo.Student;
11 import ejava.examples.webtier.dao.DAOFactory;
12 import ejava.examples.webtier.dao.DAOTypeFactory;
13 import ejava.examples.webtier.dao.StudentDAO;
14
15 public class RegistrarImpl implements Registrar {
16 Log log = LogFactory.getLog(RegistrarImpl.class);
17 private static final String NEW_STUDENT_QUERY = "getNewStudents";
18 private static final String GRAD_QUERY = "getGraduatingStudents";
19
20 private StudentDAO dao = null;
21 private StudentDAO getDAO() throws RegistrarException {
22 if (dao == null) {
23 DAOTypeFactory daoType = DAOFactory.getDAOTypeFactory();
24 log.debug("RegistrarImpl got daoTypeFactory:" + daoType);
25 if (daoType != null) {
26 dao = daoType.getStudentDAO();
27 }
28 else {
29 throw new RegistrarException("unable to get DAO");
30 }
31 }
32 return dao;
33 }
34
35 public Student addStudent(Student student) throws RegistrarException {
36 try {
37 return getDAO().create(student);
38 }
39 catch (Throwable th) {
40 throw new RegistrarException(th);
41 }
42 }
43
44 public Student completeCourse(Student student, Grade grade)
45 throws RegistrarException {
46 try {
47 student.getGrades().add(grade);
48 return getDAO().update(student);
49 }
50 catch (Throwable th) {
51 throw new RegistrarException(th);
52 }
53 }
54
55 public Student dropStudent(Student student) throws RegistrarException {
56 try {
57
58
59 return getDAO().remove(student);
60 }
61 catch (Throwable th) {
62 throw new RegistrarException(th);
63 }
64 }
65
66 public List<Student> getStudents(int index, int count)
67 throws RegistrarException {
68 try {
69 return getDAO().find(index, count);
70 }
71 catch (Throwable th) {
72 throw new RegistrarException(th);
73 }
74 }
75
76 public List<Student> getGraduatingStudents(int index, int count)
77 throws RegistrarException {
78 try {
79 return getDAO().find(GRAD_QUERY, null, index, count);
80 }
81 catch (Throwable th) {
82 throw new RegistrarException(th);
83 }
84 }
85
86 public List<Student> getNewStudents(int index, int count)
87 throws RegistrarException {
88 try {
89 return getDAO().find(NEW_STUDENT_QUERY, null, index, count);
90 }
91 catch (Throwable th) {
92 throw new RegistrarException(th);
93 }
94 }
95
96 public Student getStudent(long id) throws RegistrarException {
97 try {
98 return getDAO().get(id);
99 }
100 catch (Throwable th) {
101 throw new RegistrarException(th);
102 }
103 }
104
105 public List<Student> getStudents(
106 String queryName, Map<String, Object> params, int index, int count)
107 throws RegistrarException {
108 try {
109 return getDAO().find(queryName, params, index, count);
110 }
111 catch (Throwable th) {
112 throw new RegistrarException(th);
113 }
114 }
115
116 }