package net.sourceforge.fenixedu.dataTransferObject.assiduousness; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Set; import net.sourceforge.fenixedu.domain.DomainReference; import net.sourceforge.fenixedu.domain.RootDomainObject; import net.sourceforge.fenixedu.domain.assiduousness.Assiduousness; import net.sourceforge.fenixedu.domain.assiduousness.AssiduousnessRecord; import net.sourceforge.fenixedu.domain.assiduousness.AssiduousnessRecordMonthIndex; import net.sourceforge.fenixedu.domain.assiduousness.AssiduousnessStatus; import net.sourceforge.fenixedu.domain.assiduousness.AssiduousnessStatusHistory; import net.sourceforge.fenixedu.domain.assiduousness.Justification; import net.sourceforge.fenixedu.domain.assiduousness.JustificationMotive; import net.sourceforge.fenixedu.domain.assiduousness.Leave; import net.sourceforge.fenixedu.domain.assiduousness.MissingClocking; import net.sourceforge.fenixedu.domain.assiduousness.WorkSchedule; import net.sourceforge.fenixedu.domain.assiduousness.WorkScheduleType; import net.sourceforge.fenixedu.domain.organizationalStructure.Unit; import org.apache.commons.lang.StringUtils; import org.joda.time.Interval; import org.joda.time.LocalDate; import org.joda.time.YearMonthDay; public class AssiduousnessExportChoices implements Serializable { public enum AssiduousnessExportChoicesDatesType { MONTHS, DATES; } private AssiduousnessExportChoicesDatesType assiduousnessExportChoicesDatesType; private Boolean canChooseDateType; private Boolean chooseYear; private YearMonth yearMonth; private LocalDate beginDate; private LocalDate endDate; private Integer costCenter; private String scheduleAcronym; private DomainReference workScheduleType; private String justificationMotiveAcronym; private DomainReference justificationMotive; private DomainReference assiduousnessStatus; private String assiduousnessStatusDescription; private String action; public AssiduousnessExportChoices(String action) { this.action = action; this.endDate = new LocalDate(); this.beginDate = new LocalDate(endDate.getYear(), endDate.getMonthOfYear(), 01); this.yearMonth = new YearMonth(endDate); setCanChooseDateType(false); setChooseYear(false); } public List getAssiduousnesses() { setYearMonth(); HashMap> justificationsMap = setJustificationMap(); List result = new ArrayList(); for (Assiduousness assiduousness : RootDomainObject.getInstance().getAssiduousnesss()) { if (assiduousness.isStatusActive(beginDate, endDate) && satisfiedCostCenter(assiduousness) && satisfiedScheduleAcronym(assiduousness) && satisfiedJustification(justificationsMap, assiduousness) && satisfiedStatus(assiduousness)) { result.add(assiduousness); } } return result; } public boolean satisfiedAll(Assiduousness assiduousness) { setYearMonth(); return (assiduousness.isStatusActive(beginDate, endDate) && satisfiedCostCenter(assiduousness) && satisfiedScheduleAcronym(assiduousness) && satisfiedStatus(assiduousness)); } private boolean satisfiedJustification(HashMap> justificationsMap, Assiduousness assiduousness) { if (!StringUtils.isEmpty(getJustificationMotiveAcronym())) { if (justificationsMap.values().isEmpty()) { return false; } return justificationsMap.get(assiduousness) != null; } return true; } private boolean satisfiedStatus(Assiduousness assiduousness) { if (!StringUtils.isEmpty(getAssiduousnessStatusDescription())) { List assiduousnessStatusList = assiduousness.getStatusBetween(getBeginDate(), getEndDate()); for (AssiduousnessStatusHistory assiduousnessStatusHistory : assiduousnessStatusList) { if ((getAssiduousnessStatus() != null && assiduousnessStatusHistory.getAssiduousnessStatus() == getAssiduousnessStatus()) || getAssiduousnessStatusDescription().equalsIgnoreCase( assiduousnessStatusHistory.getAssiduousnessStatus().getDescription())) { return true; } } return false; } return true; } private boolean satisfiedScheduleAcronym(Assiduousness assiduousness) { if (getScheduleAcronym() != null && getScheduleAcronym().length() != 0) { HashMap workSchedulesMap = assiduousness.getWorkSchedulesBetweenDates(beginDate, endDate); for (WorkSchedule workSchedule : workSchedulesMap.values()) { if (workSchedule != null && workSchedule.getWorkScheduleType().getAcronym().equalsIgnoreCase(getScheduleAcronym())) { return true; } } return false; } return true; } private boolean satisfiedCostCenter(Assiduousness assiduousness) { if (getCostCenter() != null) { Unit unit = assiduousness.getEmployee().getLastWorkingPlace(new YearMonthDay(beginDate), new YearMonthDay(endDate)); if (unit != null && unit.getCostCenterCode().equals(getCostCenter())) { return true; } return false; } return true; } public HashMap> setJustificationMap() { HashMap> justificationsMap = new HashMap>(); if (!StringUtils.isEmpty(getJustificationMotiveAcronym())) { Interval interval = new Interval(beginDate.toDateTimeAtStartOfDay(), Assiduousness.defaultEndWorkDay .toDateTime(endDate.toDateTimeAtStartOfDay())); Set assiduousnessRecordList = AssiduousnessRecordMonthIndex.getAssiduousnessRecordBetweenDates( interval.getStart(), interval.getEnd()); for (AssiduousnessRecord assiduousnessRecord : assiduousnessRecordList) { if (assiduousnessRecord.isLeave() && !assiduousnessRecord.isAnulated() && ((Leave) assiduousnessRecord).getJustificationMotive().getAcronym().equals( getJustificationMotiveAcronym())) { Interval leaveInterval = new Interval(assiduousnessRecord.getDate(), ((Leave) assiduousnessRecord) .getEndDate().plusSeconds(1)); if (leaveInterval.overlaps(interval)) { List justificationsList = justificationsMap.get(assiduousnessRecord.getAssiduousness()); if (justificationsList == null) { justificationsList = new ArrayList(); } justificationsList.add((Justification) assiduousnessRecord); justificationsMap.put(assiduousnessRecord.getAssiduousness(), justificationsList); } } else if (assiduousnessRecord.isMissingClocking() && !assiduousnessRecord.isAnulated() && ((MissingClocking) assiduousnessRecord).getJustificationMotive().getAcronym().equals( getJustificationMotiveAcronym()) && interval.contains(assiduousnessRecord.getDate())) { List justificationsList = justificationsMap.get(assiduousnessRecord.getAssiduousness()); if (justificationsList == null) { justificationsList = new ArrayList(); } justificationsList.add((Justification) assiduousnessRecord); justificationsMap.put(assiduousnessRecord.getAssiduousness(), justificationsList); } } } return justificationsMap; } public HashMap> getAllJustificationMap() { HashMap> justificationsMap = new HashMap>(); Interval interval = new Interval(beginDate.toDateTimeAtStartOfDay(), Assiduousness.defaultEndWorkDay.toDateTime(endDate .toDateTimeAtStartOfDay())); Set assiduousnessRecordList = AssiduousnessRecordMonthIndex.getAssiduousnessRecordBetweenDates( interval.getStart(), interval.getEnd()); for (AssiduousnessRecord assiduousnessRecord : assiduousnessRecordList) { if (assiduousnessRecord.isLeave() && !assiduousnessRecord.isAnulated() && (StringUtils.isEmpty(getJustificationMotiveAcronym()) || ((Leave) assiduousnessRecord) .getJustificationMotive().getAcronym().equals(getJustificationMotiveAcronym()))) { Interval leaveInterval = new Interval(assiduousnessRecord.getDate(), ((Leave) assiduousnessRecord).getEndDate() .plusSeconds(1)); if (leaveInterval.overlaps(interval)) { List justificationsList = justificationsMap.get(assiduousnessRecord.getAssiduousness()); if (justificationsList == null) { justificationsList = new ArrayList(); } justificationsList.add((Justification) assiduousnessRecord); justificationsMap.put(assiduousnessRecord.getAssiduousness(), justificationsList); } } else if (assiduousnessRecord.isMissingClocking() && !assiduousnessRecord.isAnulated() && (StringUtils.isEmpty(getJustificationMotiveAcronym()) || ((MissingClocking) assiduousnessRecord) .getJustificationMotive().getAcronym().equals(getJustificationMotiveAcronym())) && interval.contains(assiduousnessRecord.getDate())) { List justificationsList = justificationsMap.get(assiduousnessRecord.getAssiduousness()); if (justificationsList == null) { justificationsList = new ArrayList(); } justificationsList.add((Justification) assiduousnessRecord); justificationsMap.put(assiduousnessRecord.getAssiduousness(), justificationsList); } } return justificationsMap; } public LocalDate getBeginDate() { return beginDate; } public void setBeginDate(LocalDate beginDate) { this.beginDate = beginDate; } public Integer getCostCenter() { return costCenter; } public void setCostCenter(Integer costCenter) { this.costCenter = costCenter; } public LocalDate getEndDate() { return endDate; } public void setEndDate(LocalDate endDate) { this.endDate = endDate; } public String getScheduleAcronym() { return scheduleAcronym; } public void setScheduleAcronym(String scheduleAcronym) { this.scheduleAcronym = scheduleAcronym; } public WorkScheduleType getWorkScheduleType() { return workScheduleType == null ? null : workScheduleType.getObject(); } public void setWorkScheduleType(WorkScheduleType workScheduleType) { if (workScheduleType != null) { this.workScheduleType = new DomainReference(workScheduleType); } else { this.workScheduleType = null; } } public JustificationMotive getJustificationMotive() { return justificationMotive == null ? null : justificationMotive.getObject(); } public void setJustificationMotive(JustificationMotive justificationMotive) { if (justificationMotive != null) { this.justificationMotive = new DomainReference(justificationMotive); } else { this.justificationMotive = null; } } public String getJustificationMotiveAcronym() { return justificationMotiveAcronym; } public void setJustificationMotiveAcronym(String justificationMotiveAcronym) { this.justificationMotiveAcronym = justificationMotiveAcronym; } public AssiduousnessStatus getAssiduousnessStatus() { return assiduousnessStatus == null ? null : assiduousnessStatus.getObject(); } public void setAssiduousnessStatus(AssiduousnessStatus assiduousnessStatus) { if (assiduousnessStatus != null) { this.assiduousnessStatus = new DomainReference(assiduousnessStatus); } else { this.assiduousnessStatus = null; } } public String getAssiduousnessStatusDescription() { return assiduousnessStatusDescription; } public void setAssiduousnessStatusDescription(String statusDescription) { this.assiduousnessStatusDescription = statusDescription; } public YearMonth getYearMonth() { return yearMonth; } public void setYearMonth() { if (assiduousnessExportChoicesDatesType.equals(AssiduousnessExportChoicesDatesType.MONTHS)) { if (getChooseYear()) { this.beginDate = new LocalDate(yearMonth.getYear(), 1, 1); this.endDate = new LocalDate(yearMonth.getYear(), 12, 31); } else { this.beginDate = new LocalDate(yearMonth.getYear(), yearMonth.getMonth().ordinal() + 1, 01); int endDay = beginDate.dayOfMonth().getMaximumValue(); if (yearMonth.getYear() == new LocalDate().getYear() && yearMonth.getMonth().getNumberOfMonth() == new LocalDate().getMonthOfYear()) { endDay = new LocalDate().getDayOfMonth(); } this.endDate = new LocalDate(yearMonth.getYear(), yearMonth.getMonth().getNumberOfMonth(), endDay); } } else { yearMonth = new YearMonth(beginDate); } } public AssiduousnessExportChoicesDatesType getAssiduousnessExportChoicesDatesType() { return assiduousnessExportChoicesDatesType; } public void setAssiduousnessExportChoicesDatesType(AssiduousnessExportChoicesDatesType assiduousnessExportChoicesDatesType) { this.assiduousnessExportChoicesDatesType = assiduousnessExportChoicesDatesType; } public String getAction() { return action; } public void setAction(String action) { this.action = action; } public Boolean getCanChooseDateType() { return canChooseDateType; } public void setCanChooseDateType(Boolean canChooseDateType) { this.canChooseDateType = canChooseDateType; if (!canChooseDateType) { setAssiduousnessExportChoicesDatesType(AssiduousnessExportChoicesDatesType.MONTHS); } } public Boolean getChooseYear() { return chooseYear; } public void setChooseYear(Boolean chooseYear) { this.chooseYear = chooseYear; } public boolean validDates() { return !getBeginDate().isAfter(getEndDate()); } }