java - 在 EmployeePanel NurseRoster 中显示假期的建议方法

标签 java swing optaplanner

我已经为 NurseRoster Optaplanner 实现了一个假期组件。我遇到的问题是如何最好地在EmployeePanelGUI中显示假期。 (底部代码元素 TODO)注意到我使用了Map Pair。一切正常,但想在 GUI 中显示。有什么建议吗?

下面是我的代码元素:

//My Data input Method
@Entity(name = "HolidaysData")
public class HolidaysData extends AbstractPersistable{

    private int weight;
    @ManyToOne
    private Employee employee;
    private LocalDate startdate;
    private LocalDate enddate;

    public int getWeight() {
        return weight;
    }
    public void setWeight(int weight) {
        this.weight = weight;
    }
    public Employee getEmployee() {
        return employee;
    }
    public void setEmployee(Employee employee) {
        this.employee = employee;
    }
    public LocalDate getStartdate() {
        return startdate;
    }
    public void setStartdate(LocalDate startdate) {
        this.startdate = startdate;
    }
    public LocalDate getEnddate() {
        return enddate;
    }
    public void setEnddate(LocalDate enddate) {
        this.enddate = enddate;
    }
}
//The emlements I have added to the NurseRoster Method
@ProblemFactCollectionProperty

private List<HolidayRequest> holidayRequestList;
public List<HolidayRequest> getHolidayRequestList() {
    return holidayRequestList;
}

public void setHolidayRequestList(List<HolidayRequest> holidayRequestList) {
    this.holidayRequestList = holidayRequestList;
}
//The elements I have added to the Employee Method
@OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
@MapKey(name="id")
 private Map<Pair<ShiftDate,ShiftDate>,  HolidayRequest> holidayRequestMap;
    public void setHolidayRequestMap(
        Map<Pair<ShiftDate, ShiftDate>, HolidayRequest> holidayRequestMap) {
    this.holidayRequestMap = holidayRequestMap;
}
//The method used in my DataLogic to read from the database
private void readHolidayRequestList(NurseRoster nurseRoster) {
        List<HolidayRequest> holidayRequestList;

        List<HolidaysData> holidayElementList = (List<HolidaysData>) rosterService
                .listHolidaysData();
        holidayRequestList = new ArrayList<>(holidayElementList.size());
        for (HolidaysData element : holidayElementList) {

            long Id = element.getId();
            int weight = element.getWeight();
            String empname = element.getEmployee().getName();
            Employee employee = employeeMap.get(empname);
            LocalDate startdate = element.getStartdate();
            LocalDate enddate = element.getEnddate();
            ShiftDate firstDate = shiftDateMap.get(startdate);
            ShiftDate lastDate = shiftDateMap.get(enddate);
            HolidayRequest holidayRequest = new HolidayRequest();
            holidayRequest.setId(Id);
            holidayRequest.setEmployee(employee);
            holidayRequest.setStartshiftDate(firstDate);
            holidayRequest.setEndshiftDate(lastDate);
            holidayRequest.setWeight(weight);
            holidayRequestList.add(holidayRequest);

            employee.getHolidayRequestMap().put(Pair.of(firstDate, lastDate),
                    holidayRequest);
        }
        nurseRoster.setHolidayRequestList(holidayRequestList);
}
//DRL Rule 
// Holiday input
rule "holidayRequest"
    when
        $holidayRequest : HolidayRequest($employee : employee, $startshiftDate : startshiftDate, $endshiftDate : endshiftDate, $weight : weight)
        $assignment : ShiftAssignment(employee == $employee, shiftDate >= $startshiftDate, shiftDate <= $endshiftDate)
    then
        scoreHolder.addHardConstraintMatch(kcontext, - $weight);
end
//Element I want to ideas on within EmployeePanel
shiftDatePanelMap = new LinkedHashMap<>(shiftDateList.size());
        for (ShiftDate shiftDate : shiftDateList) {
            // System.out.println(shiftDate);
            JPanel shiftDatePanel = new JPanel(new GridLayout(1, 0));
            Color backgroundColor = weekendDefinition.isWeekend(shiftDate.getDayOfWeek())
                    ? TangoColorFactory.ALUMINIUM_2 : shiftDatePanel.getBackground();
            if (employee != null) {
                if (employee.getDayOffRequestMap().containsKey(shiftDate)) {
                    backgroundColor = TangoColorFactory.ALUMINIUM_5;
                }
                //TODO Figure this out to show leave / holidays
                /*
                 * else if (employee.getHolidayRequestMap().is) {
                 *
                 * backgroundColor = TangoColorFactory.ALUMINIUM_4; }
                 */
                else if (employee.getDayOnRequestMap().containsKey(shiftDate)) {
                    backgroundColor = TangoColorFactory.SCARLET_1;
                }
            }

我想在休假/假期期间请求的所有日期中显示背景颜色。

最佳答案

我最终删除了这对并执行了以下狗代码:

private void readHolidayRequestList(NurseRoster nurseRoster) {
        List<LocalDate> allDates = new ArrayList<>();
        List<HolidayRequest> holidayRequestList;
        long Id = 0L;
        List<HolidaysData> holidayElementList = (List<HolidaysData>) rosterService
                .listHolidaysData();
        holidayRequestList = new ArrayList<>(holidayElementList.size());
        for (HolidaysData element : holidayElementList) {

            int weight = element.getWeight();
            String empname = element.getEmployee().getName();
            Employee employee = employeeMap.get(empname);
            LocalDate startdate = element.getStartdate();
            LocalDate enddate = element.getEnddate();
            if (startdate.isAfter(enddate)) {
                throw new IllegalStateException(
                        "start date must be before or equal to end date");
            }

            while (!startdate.isAfter(enddate)) {
                allDates.add(startdate);
                startdate = startdate.plusDays(1);
                ShiftDate firstDate = shiftDateMap.get(startdate);
                HolidayRequest holidayRequest = new HolidayRequest();
                Id++;
                holidayRequest.setId(Id);

                holidayRequest.setEmployee(employee);
                holidayRequest.setStartshiftDate(firstDate);

                holidayRequest.setWeight(weight);
                holidayRequestList.add(holidayRequest);

                employee.getHolidayRequestMap().put(firstDate, holidayRequest);
            }
        }

        nurseRoster.setHolidayRequestList(holidayRequestList);
    }


// Holiday input
rule "holidayRequest"
    when
        $holidayRequest : HolidayRequest($employee : employee, $startshiftDate : startshiftDate, $weight : weight)
        $assignment : ShiftAssignment(employee == $employee, shiftDate == $startshiftDate)
    then
        scoreHolder.addHardConstraintMatch(kcontext, - $weight);
end




 for (ShiftDate shiftDate : shiftDateList) {

            JPanel shiftDatePanel = new JPanel(new GridLayout(1, 0));
            Color backgroundColor = weekendDefinition.isWeekend(shiftDate.getDayOfWeek())
                    ? TangoColorFactory.ALUMINIUM_2 : shiftDatePanel.getBackground();
            if (employee != null) {
                if (employee.getDayOffRequestMap().containsKey(shiftDate)) {
                    backgroundColor = TangoColorFactory.ALUMINIUM_4;
                } else if (employee.getDayOnRequestMap().containsKey(shiftDate)) {
                    backgroundColor = TangoColorFactory.SCARLET_1;
                }

                else if  (employee.getHolidayRequestMap().containsKey(shiftDate)) {
                    backgroundColor = TangoColorFactory.ALUMINIUM_4;
                }


            }

关于java - 在 EmployeePanel NurseRoster 中显示假期的建议方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57051109/

相关文章:

optaplanner - 每个客户具有多个位置的车辆路线

java - 折叠工具栏问题标题停留在屏幕顶部并且不折叠 Android

java - 如何更改 JTable 中按钮的字体

java - 我如何将网站与 Optaplanner(html、javascript、php)集成?

java - 想让图形位于面板下方

java - 为什么 setBackground 仅在第一次有效? (J面板)

java - Drools Planner 规则分析

java - 如何从 Log4j Logger/Appender 中排除单个类?

java - 扫描仪在使用 next() 或 nextFoo() 后跳过 nextLine()?

java - 返回数字的第 n 位数字