package com.timesheet.validator;
import java.util.Map;
import com.timesheet.action.TimeSheetAction;
import com.timesheet.util.TimeSheetClient;
import com.timesheet.util.TimeSheetHolder;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.ValidatorSupport;
public class TimeSheetSubmission extends ValidatorSupport{
@Override
public void validate(Object arg0) throws ValidationException {
// TODO Auto-generated method stub
Map session = ActionContext.getContext().getSession();
TimeSheetAction act = (TimeSheetAction)ActionContext.getContext().getActionInvocation().getAction();
TimeSheetClient client = act.getClient();
if(session.get("holder") != null){
TimeSheetHolder holder = (TimeSheetHolder)session.get("holder");
if(!(excessTime(holder) && lessTime(holder))){
client.cleanUp();
addActionError(arg0);
return;
}else{
client.cleanUp();
return;
}
}
}
public boolean lessTime(TimeSheetHolder holder){
boolean done = true;
TimeSheetHolder.Day day = holder.dailyHours();
switch(day){
case Mon: setMessageKey("timeSheet.mon.less.hours");
done = false;
break;
case Tue: done = false;
setMessageKey("timeSheet.tue.less.hours");
break;
case Wed: done = false;
setMessageKey("timeSheet.wed.less.hours");
break;
case Thu: done = false;
setMessageKey("timeSheet.thu.less.hours");
break;
case Fri: done = false;
setMessageKey("timeSheet.fri.less.hours");
break;
default: break;
}
return done;
}
public boolean excessTime(TimeSheetHolder holder){
boolean done = true;
TimeSheetHolder.Day day = holder.moreTime();
switch(day){
case Mon: setMessageKey("timeSheet.mon.excess.hours");
done = false;
break;
case Tue: done = false;
setMessageKey("timeSheet.tue.excess.hours");
break;
case Wed: done = false;
setMessageKey("timeSheet.wed.excess.hours");
break;
case Thu: done = false;
setMessageKey("timeSheet.thu.excess.hours");
break;
case Fri: done = false;
setMessageKey("timeSheet.fri.excess.hours");
break;
default: break;
}
return done;
}
}