Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate a Multimap with JSP

Tags:

java

jsp

jstl

guava

I'm trying to write a backup dashboard showing the status of multiple servers backup. The idea is to show a table with JSP that has the last few days dates in the columns and server names in rows. In this poor man's table I wrote Yes/No values.

+------------+------------+------------+------------+
+ Host Name  | 2011-06-10 | 2011-06-09 | 2011-06-08 |
+------------+------------+------------+------------+
| web01      |     Y      |      Y     |     N      |
+------------+------------+------------+------------+
| web02      |     Y      |      Y     |     Y      |
+------------+------------+------------+------------+

Each server, does its own backup and saves the status into Amazon SimpleDb and I wrote a Java method to retrieve this information of the last few days with the following signature:

/**
 * List MySQL backups of the last howManyDays days. It starts from today 
 * included at index 0 and goes back in the past until we have a list of 
 * howManyDays days, even if some day doesn't have any data. Return a list of 
 * dates, each of which contains a list of backup jobs executed by servers in 
 * that day.
 * 
 * @param howManyDays
 *         how many days of backup to show
 * @return a Map where each key is the date in ISO format (2011-06-10) and each
 *         element is a backupJob which is represented by a Map where the key is 
 *         the server name (ex. web01, web01) and the value is "Y" if all was 
 *         fine, otherwise it contains the error message.
 */
public Multimap<String, Map<String, String>> listMysqlBackups(int howManyDays);

Multimap is the Google Guava Multimap because I've multiple backups per day. Example output:

{2011-06-10=[{web06=Y}, {web05=Y}], 2011-06-08=[{web05=Y}, {web06=Y}], 
 2011-06-09=[{web05=Y}, {web06=Y}], 2011-06-07=[{web05=Y}, {web06=Y}]} 

I don't know how to consume this information in JSP. I tried with foreach:

<c:forEach items="${backups}" var="backup" varStatus="backupId">
    ${backup.key}
</c:forEach>

And the answer was:

javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Don't know 
how to iterate over supplied "items" in <forEach>

Now I'm thinking if I'm shooting myself in the foot with a too complex return value and whether I should instead return a simple ArrayList of HashMap where each HashMap contains all the needed info (date, hostname, message). If you guys think is a better approach I don't have any problems to rewrite the Java method extracting the data, but each cell will now require to loop across all the ArrayList to get the element (which could be ok because 6 servers by 7 days is only 42 elements).

How would you approach this problem?

like image 939
stivlo Avatar asked Jun 10 '11 04:06

stivlo


2 Answers

The JSTL forEach tag does not support Multimaps. It can only iterate over standard collections / maps / arrays.

When I need to iterate over a Multimap in a JSP, I use its asMap() view. This lets me use forEach, since it knows how to iterate over the Map interface.

It would look like the following:

public Multimap<String, Map<String, String>> listMysqlBackups(int howManyDays) {
    // ...
}

public Map<String, Collection<Map<String, String>>> getListMysqlBackupsAsMap() {
    return listMysqlBackups(this.numberOfDays).asMap();
}


<c:forEach var="backup" items="${bean.listMysqlBackupsAsMap}">
    <c:set var="dateISO" value="${backup.key}/>
    <c:set var="backupJobs" value="${backup.value}/> <!-- a Collection<Map<String,String>> -->
    <c:forEach var="backupJob" items="${backupJobs}">
        <!-- do something with each backup job (Map<String, String>) for the current date -->
    </c:forEach>
</c:forEach>

If you can use JSP EL 2.1, you do not need the additional getter. You can simply call asMap() inside the JSP to obtain the Map view.


All this being said, I'm not sure your use of a Multimap really does what you want here. A Multimap<String, Map<String, String>> maps each key to a collection of Map<String,String>. In your case, it means that you have:

2011-06-09
    --> Collection
        --> Map<String, String>
        --> Map<String, String>
        --> Map<String, String>
2011-06-10
    --> Collection
        --> Map<String, String>
        --> Map<String, String>
        --> Map<String, String>

I'm not sure that's what you want here. I think you want a Map<String, Map<String, String>>.

Another solution would be to use Guava's Table.

like image 111
Etienne Neveu Avatar answered Nov 08 '22 19:11

Etienne Neveu


Just to summarize what I've done, and without claiming to be the best solution I answer to my question. I'm interested to know if using google Table collection could make things simpler or not.

Changed the return type of listMysqlBackups to a simple HashMap.

/**
 * List the MySQL backups of the last howManyDays days. It starts from today and 
 * goes back in the past until we have a list of howManyDays days, even if some 
 * day doesn't have any data. Return a Map with each index as the ISO date 
 * underscore the server name. Key example: 2011-06-11_web01
 * 
 * @param howManyDays
 *         how many days of backup to show
 * @return a Map where each key is the date in ISO format and each element is
 *         a backupJob which is represented by a Map where the key is the server
 *         name (ex. web01, web01) and the value is "Y" if all was fine, 
 *         otherwise it contains the error message.
 */
public Map<String, String> listMysqlBackups(int howManyDays)  

Added new methods to return the day list and the server list.

public static List<String> listDatesFromToday(int howManyDays) {
    List<String> dates = new ArrayList<String>();
    String currentDay = DateHelper.getCurrentDateAsIso();
    while (howManyDays > dates.size()) {
        dates.add(currentDay);
        currentDay = DateHelper.previousDay(currentDay);
    }
    return dates;
}

public static List<String> listHosts() {
    return ImmutableList.of("web05", "web06");
}

Show the table with a nested loop. I can seek the keys directly without searching them in the Map, because of the way I built the key.

<table class="dataTable">
    <tr>
    <th></th>
    <c:forEach items="${days}" var="day">
    <th>${day}${host}</th>
    </c:forEach>
    </tr>
<c:forEach items="${hosts}" var="host">
    <tr>
    <th>${host}</th>
    <c:forEach items="${days}" var="day">
    <c:set var="key" value="${day}_${host}"/>
    <td> ${backups[key]}  </td>
    </c:forEach>
    </tr>
</c:forEach>
</table>

I think this solution is simple, and I'm happy with it, but if you guys think that Google collection Table makes simpler, shorter and cleaner code I'd be happy to hear.

like image 45
stivlo Avatar answered Nov 08 '22 18:11

stivlo