3 Replies Latest reply on Jun 2, 2010 2:20 PM by Elena Veretilo

    How to set up or cancel weekends using <rich:calendar> tag?

    Elena Veretilo Newbie

      Hi guys! It's my first post, so don't be angry for, maybe, some mistakes.

       

      I have a task - create a calendar, that will have the ability to set up additional weekends and also cancel the existing ones. For example -  this Satuday (05.06.2010) must be a working day and the 1st of July need to be a day off.

       

      I decided to use for this task <rich:calendar> tag with impl of CalendarDataModel and CalendarDataModelItem.

      I found out the example of it here:

      http://livedemo.exadel.com/richfaces-demo/richfaces/calendar.jsf?tab=organizer&cid=1205967

       

      I create the private variable isWeekend in the CalendarDataModelItem class and try to set true and false to it.

      But the problem is that - when I change to next or previous month I have the same dates using the same value of isWeekend variable.

      Suppose - I set up  02.06.2010 as weekend and then I have 02.07.2010 as weekend too.

       

      And also the weekends (all saturdays and sundays) works as they are ordinary working days.

       

      Maybe I need to do this task with some js or ajax script and dont have to use CalendarDataModel? If yse - how?

       

      Any ideas? Any help will be good for me!

       

      Thanks!

        • 1. Re: How to set up or cancel weekends using <rich:calendar> tag?
          Igor Grinfeld Newbie

          A rich:calendar component assumes that there are up to 2 weekend days and use this assumption during rendering. You can redefine javascript calendar class from calendar.js. It see to ways to do it. You can create your own component extending rich:calendar or to add javascript code to page you use the calendar at. Second way is simplier, but if you create your own component you'll have reusable solution. I've done it recently and you can see my discussions for help, issues and examples.

          See also http://javaisaholiday.blogspot.com/2010/05/extentending-component-with-richfaces_26.html

          In general you should create new component that extends both java and js classes of rich:calendar, extending UICalendar.java, CalendarRendererBase.java at server side and Calendar, CalendarView, CalendarContext at client side. In your case you may be don't need to override CalendarView and CalendarContext, only initialize() and render() methods of Calendar javascript class.

           

          Igor

          • 2. Re: How to set up or cancel weekends using <rich:calendar> tag?
            Nick Belaevski Master

            Hi Elena,

             

            CalendarDataModelItem has "styleClass" attribute, that can be used to override styling for any particular day in calendar.

            To clarify one the problem more, can you please:

             

            - post page/bean code

            - describe what should change for the particular day, if that is a shifted holiday: just look or behavior also

             

            ?

            • 3. Re: How to set up or cancel weekends using <rich:calendar> tag?
              Elena Veretilo Newbie

              Hi, Nick! Thanks for reply!

               

              The code of the page:

               

              <h:form id="form">
               <rich:messages/>
               <a4j:jsFunction name="ajaxSubmit" oncomplete="#{rich:component('panel')}.show()" reRender="editContent" />
               <rich:calendar 
               popup="false" enableManualInput="true"
               cellWidth="70px" cellHeight="70px" mode="ajax"
               dataModel="#{calendarDataModel}" onchanged="if (event.rich.date) {ajaxSubmit();}" 
               id="organizer" valueChangeListener="#{calendarDataModel.valueChanged}">
               <a4j:outputPanel layout="block" id="cell" onclick="#{rich:component('organizer')}.resetSelectedDate()" style="height: 100%;" styleClass="organizer-cell">
               <div>
               <h:outputText value="{day}" style="align:center"/>
               </div>
               <div>
               <h:outputText value="{weekend}"/>
               </div>
               </a4j:outputPanel>
               </rich:calendar>
               </h:form>
               
               <rich:modalPanel id="panel" resizeable="false">
               <f:facet name="header">Edit Day:</f:facet>
               <h:form>
               
               <h:outputText value="Are you sure you want to change this day?"/>
               <h:panelGrid columns="2" id="editContent">   
               <a4j:commandButton value="Store" action="#{calendarDataModel.storeDayDetails}" id="storebutton" oncomplete="#{rich:component('panel')}.hide()" reRender="organizer"/>
               <button type="button" id="cancelbutton" onclick="#{rich:component('panel')}.hide()">Cancel</button>
               </h:panelGrid>
               </h:form>
               </rich:modalPanel>
              
              
              

               

               

              The code of the dataModel:

               

              package no.sfront.calendar;
              
              import java.util.Calendar;
              import java.util.Date;
              import java.util.HashMap;
              import java.util.Map;
              import java.util.Set;
              
              import java.text.SimpleDateFormat;
              
              import javax.faces.event.ValueChangeEvent;
              
              import org.richfaces.model.CalendarDataModel;
              import org.richfaces.model.CalendarDataModelItem;
              
              import de.jollyday.*;
              
              import org.joda.time.LocalDate;
              
              public class CalendarDataModelImpl implements CalendarDataModel {
              
               private static Map<String, CalendarDataModelItem> calendarItemsMap;
               private CalendarDataModelItem[] items;
               private Date currentDate;
               private boolean currentDisabled;
               
               static 
               {
               calendarItemsMap = new HashMap<String, CalendarDataModelItem>();
               }
               
               /* (non-Javadoc)
               * @see org.richfaces.model.CalendarDataModel#getData(java.util.Date[])
               */
               public CalendarDataModelItem[] getData(Date[] dateArray) {
               if (dateArray == null) 
               return null;
               
               items = null;
               items = new CalendarDataModelItem[dateArray.length];
               
               SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE-MMMM-dd-yyyy"); 
               
               // if we have been here before - in this month of the year
               if (calendarItemsMap.containsKey(bartDateFormat.format(dateArray[0])))
               {
               for (int i = 0; i < dateArray.length; i++) 
               {
               items[i] = calendarItemsMap.get(bartDateFormat.format(dateArray[i]));
               }
               }
               else 
               {
               for (int i = 0; i < dateArray.length; i++) 
               {
               items[i] = createDataModelItem(dateArray[i]);
               calendarItemsMap.put(bartDateFormat.format(dateArray[i]), items[i]);
               }
               }
              /*        
               try
               {
               Manager manager = Manager.getInstance("us");
               Set<LocalDate> holidays = manager.getHolidays(2010, "ny");
               System.out.println("LocalDate = " + holidays.size());
               }
               catch (Exception e) 
               {
               System.out.println("Manager exception");
               e.printStackTrace();
               }
              */        
               return items;
               }
              
               
               /**
               * @param date
               * @return CalendarDataModelItem for date
               */
               protected CalendarDataModelItem createDataModelItem(Date date) {
               CalendarDataModelItemImpl item = new CalendarDataModelItemImpl();
              
               Calendar c = Calendar.getInstance();
               c.setTime(date);
               item.setDay(c.get(Calendar.DAY_OF_MONTH));
               
               boolean weekend = ((c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) || (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY));
               item.setWeekend(weekend);
               item.setEnabled(true);
               
               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 setter for items
               */
               public void setItems(CalendarDataModelItem[] items) {
               this.items = items;
               }
              
               
               /**
               * @param valueChangeEvent handling
               */
               public void valueChanged(ValueChangeEvent event) {
               System.out.println(event.getNewValue() + " selected");
               this.setCurrentDate((Date)event.getNewValue());
               }
               
              
               /**
               * Storing changes action
               */
               public void storeDayDetails() {
               Calendar calendar = Calendar.getInstance();
               calendar.setTime(getCurrentDate());
               
               CalendarDataModelItemImpl item = (CalendarDataModelItemImpl)items[calendar.get(Calendar.DAY_OF_MONTH)-1];
               item.changeWeekend();
               }
               
               /**
               * @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;
               }
               
              }
              

               

              And the code of dataModelItem:

               

              public class CalendarDataModelItemImpl implements CalendarDataModelItem {
              
               private Object data;
               private String styleClass;
               private Object toolTip;
               private int day;
               private boolean enabled = true;
               private boolean weekend;
               
               public boolean isWeekend() {
               return weekend;
               }
              
               public void setWeekend(boolean weekend) {
               this.weekend = weekend;
               }
               
               public void changeWeekend() {
               this.weekend = !this.weekend;
              
               if (this.weekend) 
               this.setStyleClass("calendar-weekend-color");
               else
               this.setStyleClass("calendar-working-day-color");
               }
              }
              

               

               

              As you can see I try to solve my task with the HashMap - I add the pair - (date and item) - to it and after that I check is there an item with that date. If no - I create a new one, if yes - I get it from the HashMap.

              This solution garantee that items will be different from month to month, because the date is different.

               

              Also I have a variable weekend - which I can changed if the day isnt day off.

               

              Well - I invented such solution, maybe you have a better one?

               

              About your second question - I think must change both  behavour and look.

               

              And one more question for you - why the method:

              public CalendarDataModelItem[] getData(Date[] dateArray)

              calls two times when I change the month in the calendar by clicking on the ">>" ?