I solved the problem.
When I request DAO to return a MAP < Date, List < MyObject > > already ordered by Date.
DAO Methods
@PersistenceContext
private EntityManager em;
public Map<Date, List<MyObject>> ultimos30MyObject() throws ParseException{
Locale BRAZIL = new Locale("pt","BR");
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy",BRAZIL);
Map<Date, List<MyObject>> map = new TreeMap<Date, List<MyObject>>(new OrdenarMyObjectDate());
List<MyObject> lista = getUltimos30MyObject();
List<String> dataunicas = getDatasUnicas(lista);
for(String s: dataunicas){
List<MyObject> lista2= new ArrayList<MyObject>();
for(MyObject l: lista){
if(s.equals(df.format(l.getData())))
lista2.add(l);
}
if(!lista2.isEmpty())
map.put(df.parse(s), lista2);
}
return map;
}
@SuppressWarnings("unchecked")
public List<MyObject> getUltimos30MyObject(){
final String jpql = "select c from MyObject c order by c.id desc";
final Query query = em.createQuery(jpql);
query.setFirstResult(0).setMaxResults(30);
return (List<MyObject>)query.getResultList();
}
private List<String> getDatasUnicas(List<MyObject> lista){
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
List<String> datau = new ArrayList<String>();
for(MyObject l: lista){
String s = df.format(l.getData());
if(datau.isEmpty()){
datau.add(s);
}
else{
boolean okay = true;
for(String aux : datau){
if(aux.equals(s))
okay=false;
}
if(okay)
datau.add(s);
}
}
return datau;
}
My MyBean
@Inject
MyObjectDao dao;
public Map<Date, List<MyObject>> ultimos30MyObject() throws ParseException{
return dao.ultimos30MyObject();
}
My comparator, the map's (Date) keys are sorted.
public class OrdenarMyObjectDate implements Comparator<Date> {
@Override
public int compare(Date o1, Date o2) {
return o2.compareTo(o1);
}
}
On the XHTML page
<c:if test="#{applicationScope.listMyObject == null}">
<c:set var="listMyObject" value="#{MyBean.ultimos30MyObject()}" scope="application"/>
</c:if>
<ui:repeat value="#{applicationScope.listMyObject.keySet().toArray()}" var="k">
<h:form>
<p:dataTable var="obj" value="#{applicationScope.listMyObject.get(k)}" styleClass="borderless tree-table-no-header" rowStyleClass="ui-datatable-odd">
<f:facet name="header">
<h:outputText value="#{k}">
<f:convertDateTime pattern="dd/MM/yyyy"/>
</h:outputText>
</f:facet>
<p:column>
<h:outputText value="#{obj.id}">
</p:column>
<p:column>
<h:outputText value="#{obj.nome}">
</p:column>
</p:dataTable>
</h:form>
</ui:repeat>
As the Map is in the Application Scope every time New Registration MyObject I update the list within the edit () and register () methods
ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
servletContext.setAttribute("listMyObject", dao.ultimos30MyObject());