📄 studentsserviceimpl.java
字号:
/*
* 创建日期 2005-7-20
*
*/
package limq.spring.service;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.*;
import net.sf.hibernate.Hibernate;
import net.sf.hibernate.Criteria;
import net.sf.hibernate.expression.*;
import net.sf.hibernate.Query;
import limq.hibernate.dao.*;
import limq.hibernate.vo.*;
import limq.common.*;
public class StudentsServiceImpl implements IStudentsService {
private Logger log = Logger.getLogger(this.getClass());
private IStudents studentsDao;
private ICourses coursesDao;
private IClasses classesDao;
private IDepartment departmentsdao;
/**
* 验证用户名密码
*
* @param username
* @param password
*/
public boolean validate(String username, String password) {
String password2 = studentsDao.getPasswordFromUsername(username);
if (password.equals(password2))
return true;
else
return false;
}
/**
* 分页显示所有课程
*
* @param pageinfo
*/
public HashMap getCourse(PageInfo pageinfo) throws Exception {
HashMap hp = new HashMap();
String hsql = "select c from Courses as c order by c.id";
Query query = coursesDao.getQuery(hsql);
int totalCount = pageinfo.getTatalCount();
int totalPage = pageinfo.getTotalpage();
int start = pageinfo.getStart();
totalCount = totalCount == -1 ? coursesDao.getTotalCount(hsql)
: totalCount;
totalPage = totalPage == -1 ? coursesDao.getTotalPage(totalCount,
pageinfo.getPageSize()) : totalPage;
query.setFirstResult(start);
query.setMaxResults(pageinfo.getPageSize());
List list = query.list();
hp.put("courses", (Courses[]) list.toArray(new Courses[0]));
hp.put("totalCount", new Integer(totalCount));
hp.put("totalPage", new Integer(totalPage));
return hp;
}
/**
* 分页显示所有选课历史
* @param pageinfo
* @param stu_name
*/
public HashMap getStudentHistory(PageInfo pageinfo, String stu_name)
throws Exception {
HashMap hp = new HashMap();
Students stu = this.getStudetFromName(stu_name);
Integer stu_id = stu.getId();
Criteria criteria = coursesDao.getCriteria(History.class);
criteria.createCriteria("student").add(Expression.eq("name", stu_name));
int totalCount = pageinfo.getTatalCount();
int totalPage = pageinfo.getTotalpage();
int start = pageinfo.getStart();
totalCount = totalCount == -1 ? criteria.list().size() : totalCount;
totalPage = totalPage == -1 ? studentsDao.getTotalPage(totalCount,
pageinfo.getPageSize()) : totalPage;
criteria.setFirstResult(start);
criteria.setMaxResults(pageinfo.getPageSize());
criteria.addOrder(Order.asc("id"));
// criteria.addOrder(Order.desc("id"));
List list = criteria.list();
hp.put("history", (History[]) list.toArray(new History[0]));
hp.put("totalCount", new Integer(totalCount));
hp.put("totalPage", new Integer(totalPage));
return hp;
}
/**
* 根据主键查找系
*
* @param id
* 主键ID
*/
public Department getDepFromID(Integer id) {
return (Department) departmentsdao
.loadByKey(Department.class, "id", id);
}
/**
* 根据主键查找课程
*
* @param id
* 主键ID
*/
public Courses getCourseFromID(Integer id) {
return (Courses) coursesDao.loadByKey(Courses.class, "id", id);
}
/**
* 根据主键查找班级
*
* @param id
* 主键ID
*/
public Classes getClassFromID(Integer id) {
return (Classes) classesDao.loadByKey(Classes.class, "id", id);
}
/**
* 根据姓名查找学生
*
* @param name
*
*/
public Students getStudetFromName(String name) {
return (Students) studentsDao.loadByKey(Students.class, "name", name);
}
/**
* 检查学生是否选报了同一课程的班级
*
* @param clazz
* 所选报的班级
* @param stu
* 学生实体
* @return true 该生选报同一课程的班级
* @return false 没有报过该课程的班级,可以选报
*
*/
public boolean ifEnrolSameCourse(Classes clazz, Students stu) {
Courses cour = clazz.getCourse();
Classes[] classes = (Classes[]) stu.getClasses()
.toArray(new Classes[0]);
for (int i = 0; i < classes.length; i++) {
Courses c1 = classes[i].getCourse();
if (c1.getId().equals(cour.getId()))
return true;
}
return false;
}
/**
* 检查课程的目前人数
*
* @param clazz
* 检查班级人数是否已满
* @param clazz
* 班级实体
* @return true 班级人数已满
* @return false 班级人数未满
*
*/
public boolean ifMoreThanCap(Classes clazz) {
Integer capacity = clazz.getCapacity();
Integer maxcapacity = clazz.getMaxcapacity();
if (capacity.intValue() < maxcapacity.intValue()) {
clazz.setCapacity(Integer.valueOf(capacity.intValue() + 1));
//classesDao.update(clazz);
return false;
} else
return true;
}
/**
* 相数据库插入一条学生选择班级的记录
*
* @param stu
* 学生
* @param clazz
* 所选择的班级
*/
public void selectClasses(Students stu, Classes clazz, Date date)
{
stu.getClasses().add(clazz);
clazz.getStudents().add(stu);
History his = new History();
his.setEnrolTime(date);
his.setStudent(stu);
his.setClasses(clazz);
his.setScore(clazz.getCourse().getScore());
his.setMarking(new Double(0));
try{
String cour_name=new String(clazz.getCourse().getName().getBytes("GBK"));
his.setCourseName(cour_name);
}catch( java.io.UnsupportedEncodingException e){e.getStackTrace();}
stu.getHistory().add(his);
}
public void updateSudent(Students stu,Contact contact){
studentsDao.update(stu);
studentsDao.update(contact);
}
/**
* @return 返回 studentsDao。
*/
public IStudents getStudentsDao() {
return studentsDao;
}
/**
* @param studentsDao
* 要设置的 studentsDao。
*/
public void setStudentsDao(IStudents studentsDao) {
this.studentsDao = studentsDao;
}
/**
* @return 返回 classesDao。
*/
public IClasses getClassesDao() {
return classesDao;
}
/**
* @param classesDao
* 要设置的 classesDao。
*/
public void setClassesDao(IClasses classesDao) {
this.classesDao = classesDao;
}
/**
* @return 返回 coursesDao。
*/
public ICourses getCoursesDao() {
return coursesDao;
}
/**
* @param coursesDao
* 要设置的 coursesDao。
*/
public void setCoursesDao(ICourses coursesDao) {
this.coursesDao = coursesDao;
}
/**
* @return 返回 departmentdao。
*/
public IDepartment getDepartmentsdao() {
return departmentsdao;
}
/**
* @param departmentdao
* 要设置的 departmentdao。
*/
public void setDepartmentsdao(IDepartment departmentdao) {
this.departmentsdao = departmentdao;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -