/* * Created on Jun 2, 2005 * */ package net.sourceforge.fenixedu.domain.assiduousness; import java.io.Serializable; import java.util.EnumSet; import java.util.HashMap; import java.util.ResourceBundle; import net.sourceforge.fenixedu.dataTransferObject.assiduousness.WorkScheduleDaySheet; import net.sourceforge.fenixedu.util.WeekDay; import org.joda.time.DateTime; import com.linkare.commons.metainfo.Linkare; /** * @author velouria@velouria.org & Prof. J. Cachopo * */ public class WorkWeek implements Serializable { public static final WorkWeek WORK_WEEK = new WorkWeek(EnumSet.range(WeekDay.MONDAY, WeekDay.SUNDAY)); // set containing the week days private final EnumSet days; public WorkWeek(EnumSet newDays) { EnumSet myDays = EnumSet.noneOf(WeekDay.class); for (WeekDay day : newDays) { myDays.add(day); } days = myDays; } public WorkWeek(WeekDay... newDays) { EnumSet myDays = EnumSet.noneOf(WeekDay.class); for (WeekDay day : newDays) { myDays.add(day); } days = myDays; } public EnumSet getDays() { return days; } public boolean worksAt(WeekDay day) { return getDays().contains(day); } // counts how many days per week the employee works public int workDaysPerWeek() { int workDays = 0; for (WeekDay day : getDays()) { if (getDays().contains(day)) { workDays++; } } return workDays; } @Linkare(author = "Paulo Zenida", comments = "Created method to be used from the outside to avoid violating the practice Naming - Suspicious equals method name") @Override public boolean equals(Object obj) { if (!(obj instanceof WorkWeek)) { return false; } return equalsTo((WorkWeek) obj); } @Linkare(author = "Paulo Zenida", comments = "Prevented violation Naming - suspicious equals method name") private boolean equalsTo(WorkWeek workWeek) { return workWeek.getDays().containsAll(getDays()) && getDays().containsAll(workWeek.getDays()); } public boolean contains(WorkWeek otherWorkWeek) { for (WeekDay otherWorkWeekDay : otherWorkWeek.getDays()) { if (!getDays().contains(otherWorkWeekDay)) return false; } return true; } public boolean contains(DateTime date) { WeekDay dayOfWeek = WeekDay.fromJodaTimeToWeekDay(date); if (getDays().contains(dayOfWeek)) { return true; } return false; } public void validateWorkScheduleDays(HashMap workScheduleDays, ResourceBundle bundle) { for (WeekDay weekDay : getDays()) { if (workScheduleDays.get(weekDay.toString()) == null) { WorkScheduleDaySheet workScheduleDaySheet = new WorkScheduleDaySheet(); workScheduleDaySheet.setSchedule(""); workScheduleDaySheet.setWeekDay(bundle.getString(weekDay.toString() + "_ACRONYM")); workScheduleDays.put(weekDay.toString(), workScheduleDaySheet); } } } }