3 Replies Latest reply on Apr 15, 2009 2:39 PM by Christopher Logan

    Conversion error with CalendarDataModel from Seam

    Terrence Curran Newbie

      I am tring to use a CalendarDataModel to display an icon within each cell of a calendar. I have a custom class that implements CalendarDataModel but I am getting an error from jsf when I try to link it to a calendar.

      Here is my DataModel (almost the same as the demo site)

      public class ProblemCalendarDataModel implements CalendarDataModel {
      
       /* (non-Javadoc)
       * @see org.richfaces.component.CalendarDataModel#getData(java.util.Date[])
       */
      
       private CalendarDataModelItem[] items;
      
       private String currentIconHtml;
      
       private Date currentDate;
       private boolean currentDisabled;
      
      
       /* (non-Javadoc)
       * @see org.richfaces.model.CalendarDataModel#getData(java.util.Date[])
       */
       public CalendarDataModelItem[] getData(Date[] dateArray) {
       if (dateArray == null) {
       return null;
       }
       if (items==null) {
       items = new CalendarDataModelItem[dateArray.length];
       for (int i = 0; i < dateArray.length; i++) {
       items = createDataModelItem(dateArray);
       }
       }
       return items;
       }
      
       /**
       * @param date
       * @return CalendarDataModelItem for date
       */
       protected CalendarDataModelItem createDataModelItem(Date date) {
       CalendarDataModelItemImpl item = new CalendarDataModelItemImpl();
       Map data = new HashMap();
       data.put("iconHtml", "<img src=\"/img/icon-failed.gif\"/>");
      
       Calendar c = Calendar.getInstance();
       c.setTime(date);
       item.setDay(c.get(Calendar.DAY_OF_MONTH));
       item.setEnabled(true);
       item.setStyleClass("rel-hol");
       item.setData(data);
       return item;
       }
      
       /* (non-Javadoc)
       * @see org.richfaces.model.CalendarDataModel#getToolTip(java.util.Date)
       */
       public Object getToolTip(Date date) {
      
       // TODO Auto-generated method stub
       return null;
       }
      
       /**
       * @return items
       */
       public CalendarDataModelItem[] getItems() {
       return items;
       }
      
       /**
       * @param items - setter for items
       */
       public void setItems(CalendarDataModelItem[] items) {
       this.items = items;
       }
      
       /**
       * @param event - valueChangeEvent handling
       */
       public void valueChanged(ValueChangeEvent event) {
       System.out.println(event.getNewValue()+"selected");
       setCurrentDate((Date)event.getNewValue());
       Calendar calendar = Calendar.getInstance();
       calendar.setTime(getCurrentDate());
       setCurrentIconHtml((String)((HashMap)items[calendar.get(Calendar.DAY_OF_MONTH)-1].getData()).get("iconHtml"));
       }
      
       /**
       * Storing changes action
       */
       public void storeDayDetails() {
       Calendar calendar = Calendar.getInstance();
       calendar.setTime(getCurrentDate());
       ((HashMap)items[calendar.get(Calendar.DAY_OF_MONTH)-1].getData()).put("iconHtml", getCurrentIconHtml());
       }
      
       /**
       * @return currentIconHtml
       */
       public String getCurrentIconHtml() {
       return currentIconHtml;
       }
      
       /**
       * @param currentIconHtml
       */
       public void setCurrentIconHtml(String currentIconHtml) {
       this.currentIconHtml = currentIconHtml;
       }
      
       /**
       * @return currentDisabled
       */
       public boolean isCurrentDisabled() {
       return currentDisabled;
       }
      
       /**
       * @param currentDisabled
       */
       public void setCurrentDisabled(boolean currentDisabled) {
       this.currentDisabled = currentDisabled;
       }
      
       /**
       * @return currentDate
       */
       public Date getCurrentDate() {
       return currentDate;
       }
      
       /**
       * @param currentDate
       */
       public void setCurrentDate(Date currentDate) {
       this.currentDate = currentDate;
       }
      


      Then within my seam class I have:
       @Out
       private CalendarDataModel problemsCalData = new ProblemCalendarDataModel();
      


      And within my xhtml I have:
       <rich:calendar
       popup="false"
       showApplyButton="false"
       cellWidth="30px"
       cellHeight="30px"
       boundaryDatesMode="none"
       showWeeksBar="false"
       value="#{dashboard.problemsDate}"
       showFooter="false"
       mode="ajax"
       reRender="problemList"
       dataModel="#{problemsCalData}"
       valueChangeListener="#{problemsCalData.valueChanged}"
       >
       <a4j:support event="onchanged" reRender="problemList"/>
      
       <a4j:outputPanel layout="block" id="cell" style="height: 100%;">
       <h:panelGrid columns="1">
       <h:outputText value="{day}" style="align:center"/>
       <h:outputText value="{data.iconHtml}"/>
       </h:panelGrid>
       </a4j:outputPanel>
       </rich:calendar>
      


      This appears to me to be very similar to the example on the richfaces site, but I am getting this exception:

      java.lang.IllegalArgumentException: Cannot convert com.namemedia.sitesense.services.dashboard.model.ProblemCalendarDataModel@79134c of type class com.namemedia.sitesense.services.dashboard.model.ProblemCalendarDataModel to interface org.richfaces.model.CalendarDataModel
       at org.jboss.el.lang.ELSupport.coerceToType(ELSupport.java:358)
       at org.jboss.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:188)
       at com.sun.facelets.el.TagValueExpression.getValue(TagValueExpression.java:71)
       at org.richfaces.component.html.HtmlCalendar.getDataModel(HtmlCalendar.java:812)
       at org.richfaces.component.UICalendar.getPreload(UICalendar.java:608)
      




      Is this because Seam is wrapping my object in something that is no longer an implementation CalendarDataModel? Does anyone know what I am doing wrong?