2 Replies Latest reply on Sep 6, 2011 2:33 AM by Simone Riccucci

    RichFaces 4: CalendarDataModeltem getData does not work

    Simone Riccucci Newbie

      I'm using rich:calendar to display a custom agenda using CalendarDataModel. I want to display a label for every day but the result is not displaying the actual data but just the string {data.label} as it is.

       

      What I have done:

       

       

      <a4j:outputPanel id="calendarioGroup"
                                  rendered="#{prenotazione.showCalendar}">
                                  <h:outputLabel value="Scegli il giorno" />
                                  <rich:calendar value="#{prenotazione.giorno}" mode="ajax"
                                      popup="false" dataModel="#{prenotazione.agendaModel}">
                                    <h:outputText value="{data.label}" />
                                  </rich:calendar>
                              </a4j:outputPanel>
      

       

      AgendaDataModel.java

       

      public class AgendaDataModel implements CalendarDataModel {
      
          private Dao<?,?> dao = null;
          private Risorsa risorsa;
          
          public AgendaDataModel(Dao<?,?> dao) {
              super();
              this.dao = dao;
          }
      
          /* (non-Javadoc)
           * @see org.richfaces.model.CalendarDataModel#getData(java.util.Date[])
           */
          @Override
          public CalendarDataModelItem[] getData(Date[] days) {
              int count=0;
              for(Date day: days) {
                  days[count++] = DateUtils.truncate(day, Calendar.DAY_OF_MONTH);
              }
                  
              Arrays.sort(days);
              String findDay = "select s.giorno, sum(case when maxDisponibilita - (select count(p) from s.prenotazioni p) > 0 then 1 else 0 end) as disponibili from Slot s where s.risorsa = ?1 and s.giorno in (?2) group by s.giorno order by s.giorno";
              CalendarDataModelItem[] items = new CalendarDataModelItem[days.length];
              
              dao.openSession();    
              try {
                  List<Object[]> selectedDays= (List<Object[]>) dao.findByQuery(findDay, this.risorsa, Arrays.asList(days));
                  int indexSelected=0;
                  int indexDays=0;
                  while(indexDays<days.length) {
                      Date day = days[indexDays];
                      Date giorno = null;
                      int disponibili = 0;
                      if(selectedDays.size()>indexSelected) {
                          giorno = (Date) selectedDays.get(indexSelected)[0];
                          disponibili = ((Long) selectedDays.get(indexSelected)[1]).intValue();
                      }
                      while(!day.equals(giorno) && indexDays<days.length) {
                          AgendaDataModelItem item = new AgendaDataModelItem();
                          Map<String,String> data = new HashMap<String,String>();
                          data.put("label", "Giorno non disponibile");
                          item.setData(data);
                          item.setEnabled(false);
                          item.setStyleClass("giornoNonDisponibile");
                          items[indexDays] = item;
                          indexDays++;
                          if(indexDays<days.length)
                              day = days[indexDays];
                      }
                      if(indexDays<days.length) {
                          AgendaDataModelItem item = new AgendaDataModelItem();
                          Map<String,String> data = new HashMap<String,String>();
                          if(disponibili > 0) {
                              if(disponibili == 1)
                                  data.put("label","Un orario disponibile");
                              else
                                  data.put("label",disponibili+" orari disponibili");
                              item.setEnabled(true);
                              item.setStyleClass("giornoDisponibile");
                          } else {
                              data.put("label","Nessun orario disponibile");
                              item.setEnabled(false);
                              item.setStyleClass("orarioNonDisponibile");
                          }
                          item.setData(data);
                          items[indexDays] = item;
                          indexDays++;
                          indexSelected++;
                      }
                  }
              } catch(Exception e) {
                  e.printStackTrace();
                  if(e instanceof RuntimeException)
                      throw (RuntimeException)e;
              } finally {
                  dao.closeSession();
              }
              return items;
          }
      
          /* (non-Javadoc)
           * @see org.richfaces.model.CalendarDataModel#getToolTip(java.util.Date)
           */
          @Override
          public Object getToolTip(Date arg0) {
              // TODO Auto-generated method stub
              return null;
          }
      
          public Risorsa getRisorsa() {
              return risorsa;
          }
      
          public void setRisorsa(Risorsa risorsa) {
              this.risorsa = risorsa;
          }
      
      }
      

       

      AgendaDataModelItem.java

       

      public class AgendaDataModelItem implements CalendarDataModelItem {
      
          private Object data = null;
          private int day = 0;
          private String styleClass = null;
          private Object toolTip = null;
          private boolean enabled = true;
          @Override
          public Object getData() {
              return data;
          }
      
          public void setData(Object data) {
              this.data = data;
          }
          @Override
          public int getDay() {
              return day;
          }
      
          public void setDay(int day) {
              this.day = day;
          }
          @Override
          public String getStyleClass() {
              return styleClass;
          }
      
          public void setStyleClass(String styleClass) {
              this.styleClass = styleClass;
          }
          @Override
          public Object getToolTip() {
              return toolTip;
          }
      
          public void setToolTip(Object toolTip) {
              this.toolTip = toolTip;
          }
          @Override
          public boolean isEnabled() {
              return enabled;
          }
      
          public void setEnabled(boolean enabled) {
              this.enabled = enabled;
          }
      
          @Override
          public boolean hasToolTip() {
              return this.toolTip != null;
          }
      
      
      }
      

       

      ManagedBean "prenotazione" just return an instance of AgendaDataModel.

       

      Thank you for any help.