?? 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 + -