/*继续Action动作类*/
package action;
import inter.IOperation;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import entity.User;
import exception.MyException;
import factory.OperationFactory;
/***
* 动作:点击保存按钮后对应动作
* @author 猫哥
* @date 2017.2.9
* @modify 2017.2.19 to remove context
*/
public class SaveAction extends Action{
protected Boolean isAddOrEdit;
protected Object saveEntity=null;
protected User sessionUser=null;
public SaveAction(String entityType,String operationType,Boolean isAddOrEdit,Object saveEntity,User sessionUser){
super(entityType,operationType);
this.isAddOrEdit=isAddOrEdit;
this.saveEntity=saveEntity;
this.sessionUser=sessionUser;
}
@Override
public Map<String,Object> execute() throws MyException {
Map<String,Object> resultMap=new HashMap<String,Object>();
IOperation oper=OperationFactory.createOperation(entityType);
int affectedCount=-1;
if(isAddOrEdit){//添加
affectedCount=oper.add(saveEntity);
}else{//修改
affectedCount=oper.update(saveEntity);
}
if(affectedCount<1){//失败咯
throw new MyException(new Date(),"SaveAction执行失败,entityType:"+entityType,"更新失败");
}else{
resultMap.put("tipInfo", "更新成功");
actionUrl="tip.jsp";
return resultMap;
}
}
}
package action;
import java.util.Map;
import operation.JobOperation;
import util.Constant;
import entity.Job;
import entity.User;
import exception.MyException;
import factory.OperationFactory;
/***
* 动作:学生做作业/老师批阅作业
* @author 猫哥
* @date 2017.2.20
*/
public class SaveJobAction extends SaveAction{
public SaveJobAction(String entityType,String operationType,Boolean isAddOrEdit,Object saveEntity,User sessionUser){
super(entityType,operationType,isAddOrEdit,saveEntity,sessionUser);
}
@Override
public Map<String,Object> execute() throws MyException {
JobOperation jobOper=(JobOperation)OperationFactory.createOperation(entityType);
Job inputJob=(Job)saveEntity;
Job job=(Job)jobOper.selectById(inputJob.getJobId());
if(sessionUser.getUserRole().getRoleName().equals("学生"))//做作业
{
job.setJobScore("-1");//已做完,未阅
job.setJobContent(inputJob.getJobContent());
job.setJobTime(Constant.getDate());
}
else if(sessionUser.getUserRole().getRoleName().equals("教师"))//批阅
{
job.setJobScore(inputJob.getJobScore());//已做完,未阅
}
//新增完毕后跳转查看作业页面
saveEntity=job;
return super.execute();
}
}
package action;
import java.util.Map;
import entity.Lesson;
import entity.User;
import exception.MyException;
/***
* 动作:选课
* @author 猫哥
* @date 2017.2.20
*/
public class SaveLessonAction extends SaveAction{
public SaveLessonAction(String entityType,String operationType,Boolean isAddOrEdit,Object saveEntity,User sessionUser){
super(entityType,operationType,isAddOrEdit,saveEntity,sessionUser);
}
@Override
public Map<String,Object> execute() throws MyException {
Lesson lesson=(Lesson)saveEntity;
lesson.setLessonUser(sessionUser);
saveEntity=lesson;
return super.execute();
}
}
package action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import database.MySQLHandler;
import operation.LessonOperation;
import operation.WorkOperation;
import entity.Lesson;
import entity.User;
import entity.Work;
import exception.MyException;
import factory.OperationFactory;
/***
* 动作:保存作业(注意新增作业时需要事务)
* @author 猫哥
* @date 2017.2.17
* @modify 2017.2.19 to remove context
*/
public class SaveWorkAction extends SaveAction{
public SaveWorkAction(String entityType,String operationType,Boolean isAddOrEdit,Object saveEntity,User sessionUser){
super(entityType,operationType,isAddOrEdit,saveEntity,sessionUser);
}
@Override
public Map<String,Object> execute() throws MyException {
Map<String,Object> resultMap=new HashMap<String,Object>();
//获取最大work_id,加一为新id
WorkOperation workOper=(WorkOperation)OperationFactory.createOperation("Work");
int maxId=workOper.selectMaxId();
//获取新增的work信息,maxId+1是没问题的,如果正好别人插入maxId+1,大不了就失败了
Work one=(Work)saveEntity;
one.setWorkId(maxId+1);
//待执行的sql列表
ArrayList<String> sqlList=new ArrayList<String>();
//第一条语句system_Work中新增作业
sqlList.add("insert into system_Work(work_id,Work_title,work_time,work_course)"
+" values('"+one.getWorkId()+"','"+one.getWorkTitle()+"','"+one.getWorkTime()+"','"+one.getWorkCourse().getCourseId()+"')");
//第二条语句student_job中添加多条
LessonOperation lessonOper=(LessonOperation)OperationFactory.createOperation("Lesson");
ArrayList<Lesson> lessonList=(ArrayList<Lesson>)lessonOper.selectByCourseId(one.getWorkCourse().getCourseId());
StringBuilder sqlLesson=new StringBuilder();
sqlLesson.append("insert into student_Job(Job_score,job_user,job_work) values");
for(Lesson lesson:lessonList){
String sql="('-2','"+lesson.getLessonUser().getUserId()+"','"+one.getWorkId()+"'),";
sqlLesson.append(sql);
}
sqlLesson.deleteCharAt(sqlLesson.length()-1);
if(lessonList.size()>0)
sqlList.add(sqlLesson.toString());
//执行事务
MySQLHandler hand=new MySQLHandler();
hand.doTransaction(sqlList);
//新增完毕后跳转查看作业页面
resultMap.put("tipInfo", "更新成功");
actionUrl="tip.jsp";
return resultMap;
}
}
package action;
import inter.IOperation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import util.Constant;
import exception.MyException;
import factory.OperationFactory;
/***
* 动作:查看用户列表
* @author 猫哥
* @date 2017.2.9
* @modify 2017.2.x(忘记了哪天) add 分页 by 猫哥
* @modify 2017.2.19 to remove context
*/
public class ViewAction extends Action{
//分别代表要显示的页码,每页显示个数,偏移数,总个数,最大页数
protected int page,size,offset,total,maxPage;
public ViewAction(String entityType,String operationType,int page){
super(entityType,operationType);
this.page=page;
}
@Override
public Map<String,Object> execute() throws MyException {
//返回值对象
Map<String,Object> resultMap=new HashMap<String,Object>();
//获取数据库操作对象
IOperation oper=OperationFactory.createOperation(entityType);
//处理分页
total=oper.selectCount();
size=Constant.PageSize.get(entityType);
maxPage=total/size+((total%size)>0?1:0);
if(page>maxPage)//最后一页时点击下一页
page=maxPage;
if(page<1)//第一页时点击上一页
page=1;
offset=(page-1)*size;
//处理返回值
actionUrl=entityType.toLowerCase()+"Manage.jsp";
List result=oper.selectPage(offset, size);
resultMap.put(entityType.toLowerCase()+"s",result);
resultMap.put("currentPage", page);
resultMap.put("maxPage", maxPage);
return resultMap;
}
}
package action;
import java.util.HashMap;
import java.util.Map;
import operation.CourseOperation;
import util.Constant;
import entity.User;
import exception.MyException;
import factory.OperationFactory;
/***
* 动作:查看课程列表(根据登录用户角色不同)
* @author 猫哥
* @date 2017.2.15
* @modify 2017.2.19 to remove context
*/
public class ViewCourseAction extends ViewAction{
protected User sessionUser=null;
public ViewCourseAction(String entityType,String operationType,int page,User sessionUser){
super(entityType,operationType,page);
this.sessionUser=sessionUser;
}
@Override
public Map<String,Object> execute() throws MyException {
//返回值对象
Map<String,Object> resultMap=new HashMap<String,Object>();
//获取数据库操作对象
CourseOperation oper=(CourseOperation)OperationFactory.createOperation(entityType);
if(sessionUser.getUserRole().getRoleName().equals("校长")){//查看全部课程
return super.execute();
}else if(sessionUser.getUserRole().getRoleName().equals("教师")){
//教师特殊之处在于查看课程时,只能看自己创建的课程,注意分页也要重算
total=oper.selectCountByUserId(sessionUser.getUserId());
size=Constant.PageSize.get(entityType);
maxPage=total/size+((total%size)>0?1:0);
if(page>maxPage)//最后一页时点击下一页
page=maxPage;
if(page<1)//第一页时点击上一页
page=1;
offset=(page-1)*size;
resultMap.put(entityType.toLowerCase()+"s",oper.selectPageByUserId(offset, size, sessionUser.getUserId()));
resultMap.put("currentPage", page);
resultMap.put("maxPage", maxPage);
actionUrl="courseManageByTeacher.jsp";
}
else if(sessionUser.getUserRole().getRoleName().equals("学生")){//选课
//教师特殊之处在于查看课程时,只能看自己创建的课程,注意分页也要重算
total=oper.selectCountByStudentId(sessionUser.getUserId());
size=Constant.PageSize.get(entityType);
maxPage=total/size+((total%size)>0?1:0);
if(page>maxPage)//最后一页时点击下一页
page=maxPage;
if(page<1)//第一页时点击上一页
page=1;
offset=(page-1)*size;
resultMap.put(entityType.toLowerCase()+"s",oper.selectPageByStudentId(offset, size, sessionUser.getUserId()));
resultMap.put("currentPage", page);
resultMap.put("maxPage", maxPage);
actionUrl="courseManageByStudent.jsp";
}
return resultMap;
}
}
package action;
import java.util.HashMap;
import java.util.Map;
import operation.JobOperation;
import util.Constant;
import entity.User;
import exception.MyException;
import factory.OperationFactory;
/***
* 动作:查看作业列表(根据登录用户角色不同)
* @author 猫哥
* @date 2017.2.20
*/
public class ViewJobAction extends ViewAction{
protected User sessionUser=null;
public ViewJobAction(String entityType,String operationType,int page,User sessionUser){
super(entityType,operationType,page);
this.sessionUser=sessionUser;
}
@Override
public Map<String,Object> execute() throws MyException {
//返回值对象
Map<String,Object> resultMap=new HashMap<String,Object>();
//获取数据库操作对象
JobOperation oper=(JobOperation)OperationFactory.createOperation(entityType);
if(sessionUser.getUserRole().getRoleName().equals("校长")){//查看全部作业
return super.execute();
}else if(sessionUser.getUserRole().getRoleName().equals("教师")){
//教师负责查看自己课程已完成、未批阅的作业
total=oper.selectCountByTeacherId(sessionUser.getUserId(),"-1");
size=Constant.PageSize.get(entityType);
maxPage=total/size+((total%size)>0?1:0);
if(page>maxPage)//最后一页时点击下一页
page=maxPage;
if(page<1)//第一页时点击上一页
page=1;
offset=(page-1)*size;
resultMap.put(entityType.toLowerCase()+"s",oper.selectPageByTeacherId(offset, size, sessionUser.getUserId(),"-1"));
resultMap.put("currentPage", page);
resultMap.put("maxPage", maxPage);
actionUrl="jobManageByTeacher.jsp";
}
else if(sessionUser.getUserRole().getRoleName().equals("学生")){
//学生查看未做的作业
total=oper.selectCountByStudentId(sessionUser.getUserId(),"-2");
size=Constant.PageSize.get(entityType);
maxPage=total/size+((total%size)>0?1:0);
if(page>maxPage)//最后一页时点击下一页
page=maxPage;
if(page<1)//第一页时点击上一页
page=1;
offset=(page-1)*size;
resultMap.put(entityType.toLowerCase()+"s",oper.selectPageByStudentId(offset, size, sessionUser.getUserId(),"-2"));
resultMap.put("currentPage", page);
resultMap.put("maxPage", maxPage);
actionUrl="jobManageByStudent.jsp";
}
return resultMap;
}
}
package action;
import java.util.List;
import java.util.Map;
import operation.WorkOperation;
import util.Constant;
import exception.MyException;
import factory.OperationFactory;
/***
* 动作:查看作业列表(根据作业名称)
* @author 猫哥
* @date 2017.2.15
* @modify 2017.2.19 to remove context
*/
public class ViewWorkAction extends ViewAction{
protected int courseId;//所属course的id
public ViewWorkAction(String entityType,String operationType,int page,int courseId){
super(entityType,operationType,page);
this.courseId=courseId;
}
@Override
public Map<String,Object> execute() throws MyException {
//利用父类起码可以把actionUrl设置了
Map<String,Object> resultMap=super.execute();
//获取数据库操作对象
WorkOperation oper=(WorkOperation)OperationFactory.createOperation(entityType);
//处理分页
total=oper.selectCountByCourseId(courseId);
size=Constant.PageSize.get(entityType);
maxPage=total/size+((total%size)>0?1:0);
if(page>maxPage)//最后一页时点击下一页
page=maxPage;
if(page<1)//第一页时点击上一页
page=1;
offset=(page-1)*size;
List result=oper.selectPageByCourseId(offset, size,courseId);
resultMap.put(entityType.toLowerCase()+"s",result);
resultMap.put("currentPage", page);
resultMap.put("maxPage", maxPage);
resultMap.put("byCourseId", courseId);
return resultMap;
}
}
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦