Ebony Maw
Ebony Maw

Reputation: 524

Displaying the contents of a HashMap which contains Date objects in a dataTable in JSF/PrimeFaces

I created the following method, which returns a HashMap<Date, List<Date>> where the key is a Date object and the value is a List of Date Objects. The method accepts a List of timeStamps and groups them by day. It then returns those grouped timestamps in the aforementioned HashMap construct.

 public class GroupDatesByDay {

    HashMap<Date, List<Date>> groupedUserLogins = new HashMap<Date, List<Date>>();
    Calendar cal = Calendar.getInstance();

    public HashMap<Date, List<Date>> parseTimeStamps(List<Date> timeStamps) {

    DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS", Locale.US);
    List<Date> timeStamps = new ArrayList<Date>();

        for (Date ts : timeStamps) {

            cal.setTime(ts);
            cal.set(cal.HOUR_OF_DAY, 0);
            cal.set(cal.MINUTE, 0);
            cal.set(cal.SECOND, 0);
            cal.set(cal.MILLISECOND, 0);

            if (!groupedUserLogins.containsKey(cal.getTime())) {

                groupedUserLogins.put(cal.getTime(), new ArrayList<Date>());
            }
            groupedUserLogins.get(cal.getTime()).add(ts);
        }

        keySet = groupedUserLogins.keySet();
        keyList.addAll(keySet);
        return groupedUserLogins;
    }
}

The data within the HashMap should look something like this:

Key                          List<Date> 

2018-07-11 
                  2018-07-11 08:14:08.540000 
                  2018-07-11 10:46:23.575000 

2018-07-12  
                  2018-07-12 12:51:48.928000 
                  2018-07-12 13:09:00.701000 
                  2018-07-12 16:04:45.890000 

2018-07-13 
                  2018-07-13 14:14:17.461000 

In my XHTML, I would like to display this data within a dataTable, and RowExpansion to see the individual timestamps, per day. I have written the following XHTML. As you can see, userLogins is what my Java method has returned. I am iterating over it in this dataTable, but I don't know how to display the values inside it in the manner described above.

<p:dataTable var="userLogin" value="#{userEdit.userLogins}"class="DataTable">
        <p:column headerText=" Recent Logins">
          <h:outputLabel value="#{userLogin}">
          </h:outputLabel>
        </p:column>
</p:dataTable>

Upvotes: 1

Views: 369

Answers (1)

Ebony Maw
Ebony Maw

Reputation: 524

Here is how I finally solved it. As suggested by Melloware, I had to involve a List.

My implementation changed slightly:

private Hashtable<Date, List<Date>> groupedUserLogins = new Hashtable<Date, List<Date>>();
private Calendar cal = Calendar.getInstance();
private Set<Date> keySet = new TreeSet<Date>();
private List<Date> keyList = new ArrayList<Date>();

...

public Hashtable<Date, List<Date>> parseUserLogins(List<UserLogin> userLogins) {

        DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS", Locale.US);
        List<Date> timeStamps = new ArrayList<Date>();

        for (UserLogin u : userLogins) {
            timeStamps.add(u.getTimeStamp());
        }

        for (Date ts : timeStamps) {

            cal.setTime(ts);
            cal.set(cal.HOUR_OF_DAY, 0);
            cal.set(cal.MINUTE, 0);
            cal.set(cal.SECOND, 0);
            cal.set(cal.MILLISECOND, 0);
            dateFormat.format(ts);

            if (!groupedUserLogins.containsKey(cal.getTime())) {

                groupedUserLogins.put(cal.getTime(), new ArrayList<Date>());
            }
            groupedUserLogins.get(cal.getTime()).add(ts);
        }

        keySet = groupedUserLogins.keySet();
        keyList.addAll(keySet);
        return groupedUserLogins;
    }

My XHTML:

<p:dataTable var="loginDayDate" value="#{userEdit.keyList}"
    sortOrder="descending" sortBy="#{loginDayDate}" class="DataTable">

    <p:column width="15">
        <p:rowToggler />
    </p:column>

    <p:column headerText=" Recent Logins" width="110"
        sortBy="#{loginDayDate}">

        <h:outputLabel value="#{loginDayDate}">
            <f:convertDateTime pattern="#{Constants.DATE_FORMAT}" />

        </h:outputLabel>
    </p:column>
    <p:column headerText=" # of Logins" style="text-align : left">
        #{fn:length(userEdit.groupedUserLogins[loginDayDate])}
    </p:column>
    <p:rowExpansion>
        <p:dataTable var="specificDate"
            value="#{userEdit.groupedUserLogins[loginDayDate]}"
            class="DataTable">
            <p:column headerText="Time" style="text-align: left">
                <h:outputLabel value="#{specificDate}">
                    <f:convertDateTime pattern="#{Constants.DATETIME_FORMAT}" />
                </h:outputLabel>
            </p:column>
        </p:dataTable>
    </p:rowExpansion>
</p:dataTable>

How it looks:

enter image description here

Upvotes: 1

Related Questions