0

i have to get a datatable in JSF Primefaces Java filled with data that comes from the db and if there's not enough data, i have to fill it with empty or new objects/rows.

So to clear this above a bit, every row in the datatable represents a day in the present month. The data that's already in the db is data per date in that month.

So if i have only data for the second day of june. I need in my datatable an new empty object on the first day of the month, on the second day there has to be the data from the db.

So i've tried to do that but my result is the data from the db comes in the datatable stackt above the datatable. From there on it begins again from the first of the month.

So i want to set the database data on the specific day of the month.

Here is my code:

 LocalDate date = LocalDate.now().withDayOfMonth(1);
        dayDataList = dayDataTaskService.findAllDataForUser("Wim",Date.valueOf(date));
        LocalDate end = date.plusMonths(1);

        if (dayDataList == null || dayDataList.isEmpty()) {
            List<DayData> dayDataList = new ArrayList<>();
            while (date.isBefore(end)) {
                DayData dat = new DayData();
                dat.setRamDate(Date.valueOf(date));
                dayDataList.add(dat);

                date = date.plusDays(1);

            }


        } else {
            while (date.isBefore(end)) {
                int day = date.getDayOfMonth();
                if(dayDataList.contains(Date.valueOf(date)))
                {
                    for(DayData d : dayDataList)
                    {

                        if(d.getRamDate().equals(Date.valueOf(date)))
                        {
                            System.out.println(date + "-" + d.getRamDate());
                            dayDataList.set(5,d);

                            date.plusDays(1);
                        }
                    }

                }else{


                            dayDataList.add(new DayData(Date.valueOf(date)));
                            date = date.plusDays(1);


                }

and my result is:

Result from code above

So you can see that the first record is the first of the month and is data from the db. The second record is also the first of the month with no data. So i need to replace the rows in the datatable with these that are in the db at the specific date in the datatable

  • Try writing a unitest. This sounds like a pure 'service issue (https://stackoverflow.com/questions/30639785/jsf-controller-service-and-dao) where you need to populate a model correctly and not a JSF/PrimeFaces thing. Try doing that, printing it out the the console if needed. If that works, you have the model for PrimeFaces (JSF) as well). But if I seem to misunderstand, you have to explain better. – Kukeltje Jun 04 '18 at 20:16
  • @Kukeltje Thanks for the comment. In short its adding the in the db exsisting data to the list and when there is not enough data for every day, adding new objects to it to fill the list to the end of the month – Wim Van Geyt Jun 04 '18 at 20:39
  • That part I understood, but it is unclear what the **problem** is... – Kukeltje Jun 04 '18 at 20:42
  • That the data from the database appears at the top of the datatable. Say the first of June with data from the db. The row thereafter has to be the second of June with a default row with no data from the db. And now as you can see in my printscreen, The first of June appears two times in the datatable. – Wim Van Geyt Jun 04 '18 at 20:48
  • Ok that part is clear now, but the datatable just 'prints' what you feed it. Hence I stated that it is more of a 'service' thing and that if you print out the service result to stdout at the end of your processing, you'd have the same problem. So it is not a JSF or PrimeFaces thing (or does an h:datatable display it correctly?) And btw, your code is not complete ;-) – Kukeltje Jun 04 '18 at 21:04
  • If dayDataList does not contain a date, you do a dayDataList.add(). This appends allways at the end of the list. You can do a `dayDataList.add(currentIndex, new DayData())` – Holger Jun 05 '18 at 07:33

0 Answers0