util.DnCalendar.java Source code

Java tutorial

Introduction

Here is the source code for util.DnCalendar.java

Source

/**
* Copyright (c) 2001-2012 "Redbasin Networks, INC" [http://redbasin.org]
*
* This file is part of Redbasin OpenDocShare community project.
*
* Redbasin OpenDocShare is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package util;

import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Date;
import java.util.ArrayList;

import java.text.SimpleDateFormat;
import java.text.ParseException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Smitha Gudur (smitha@redbasin.com)
 * @version $Revision: 1.1 $
 */
public class DnCalendar {

    /** Logger for this class and subclasses */
    protected static final Log logger = LogFactory.getLog("util.DnCalendar");

    private volatile String todayColor = "00ff00";
    private volatile String currentColor = "0000ff";

    private volatile SimpleDateFormat nativeFormatter;
    private volatile SimpleDateFormat displayFormatter;

    /**
     * Get an HTML formatted calendar widget based on the date and 
     * the url and a list of String dates which have blog activity.
     * The widget will have today's day highlighted if present in
     * the month specified in the cdate parameter, and the current
     * day in the cdate parameter will also be highlighted with a
     * different color.
     *
     * @param cdate The date is in the format YYYY-MM-DD.
     * @param url The url will be used as a prefix in each link.
     * @param dates A list of String dates in format YYYY-MM-DD.
     */
    public String getWidget(String cdate, String url, HashSet dates, String member) {

        if (cdate == null)
            return null;

        String year, monthNumStr, day;
        year = monthNumStr = day = null;
        if (cdate.length() >= 7) {
            year = MyUtils.getYear(cdate);
            monthNumStr = MyUtils.getMonthNum(cdate);
        }
        if (cdate.length() == 10) {
            day = MyUtils.getDay(cdate);
        }

        //logger.info("year = " + year + " month = " + monthNumStr + " day = " + day + " cdate= " + cdate);
        //logger.info("url = " + url);
        //logger.info("dates = " + dates.toString());

        Calendar cal = Calendar.getInstance();
        int yrNum = new Integer(year).intValue();
        int monthNum = new Integer(monthNumStr).intValue();
        //System.out.println("yrNum = " + yrNum);
        //System.out.println("monthNum = " + monthNum);
        cal.set(yrNum, monthNum - 1, 1);
        //System.out.println(cal.getTime().toString());

        StringBuffer sb = new StringBuffer();

        // table top
        sb.append("<TABLE BORDER=\"1\" CELLSPACING=\"1\">\n");

        // today's date
        sb.append("<TR>\n");
        sb.append("<TD WIDTH=\"100\">\n");

        sb.append("<TABLE BORDER=\"0\" CELLSPACING=\"1\" WIDTH=\"100\">\n");
        //sb.append("<TR ALIGN=\"CENTER\">\n");
        sb.append("<TR>\n");

        //previous month link
        int prevMonthNum = monthNum - 1;
        int prevYrNum = yrNum;
        if (monthNum == 1) {
            prevMonthNum = 12;
            prevYrNum = yrNum - 1;
        }
        String ndate = null;
        String prevMonthStr = MyUtils.getMonthString(prevMonthNum);
        if (prevMonthNum < 10) {
            ndate = prevYrNum + "-0" + prevMonthNum;
        } else {
            ndate = prevYrNum + "-" + prevMonthNum;
        }
        //logger.info("prev month = " + prevMonthStr + " ndate = " + ndate);
        sb.append("<TD ALIGN=\"CENTER\">\n");
        sb.append("<font color=\"");
        sb.append(GlobalConst.calLabelColor);
        sb.append("\" size=\"");
        sb.append(GlobalConst.calFontSize);
        sb.append("\"><B>\n");

        // url begin
        sb.append("<a href=\"");
        sb.append(GlobalConst.PblogMonthlyUrl);
        sb.append("?date=");
        sb.append(ndate);
        sb.append("&member=");
        sb.append(member);
        sb.append("\">");
        sb.append("<font color=\"");
        sb.append(GlobalConst.calUrlColor);
        sb.append("\">");
        sb.append(prevMonthStr).append(" ").append(year);
        sb.append("</font>");
        sb.append("</a>");
        // url end
        sb.append("</B></font>\n</TD>\n");
        sb.append("<TD>\n&nbsp;</TD>\n<TD>\n&nbsp;</TD>\n");

        // current month
        sb.append("<TD ALIGN=\"RIGHT\">\n");
        sb.append("<font color=\"");
        sb.append(GlobalConst.calLabelColor);
        sb.append("\" size=\"");
        sb.append(GlobalConst.calFontSize);
        sb.append("\"><B>\n");
        String monthStr = MyUtils.getMonthString(monthNum);
        //logger.info("current month = " + monthStr);
        sb.append(monthStr).append(" ").append(year);
        sb.append("</B></font>\n");
        sb.append("</TD>\n");

        // fill the gaps
        sb.append("<TD>&nbsp;</TD>\n<TD>&nbsp;</TD>\n");

        // next month 
        int nextMonthNum = monthNum + 1;
        int nextYrNum = yrNum;
        if (monthNum == 12) {
            nextMonthNum = 1;
            nextYrNum = yrNum + 1;
        }
        String nextMonthStr = MyUtils.getMonthString(nextMonthNum);
        if (nextMonthNum < 10) {
            ndate = nextYrNum + "-0" + nextMonthNum;
        } else {
            ndate = nextYrNum + "-" + nextMonthNum;
        }
        //logger.info("next month = " + nextMonthStr + " ndate = " + ndate);

        //sb.append("<font color=\"");
        //sb.append(GlobalConst.calLabelColor);
        //sb.append("\"><font size=\"");
        //sb.append(GlobalConst.calFontSize);
        //sb.append("\"></font><u><B>\n");

        // url begin
        sb.append("<TD ALIGN=\"CENTER\">\n");
        sb.append("<a href=\"");
        sb.append(GlobalConst.PblogMonthlyUrl);
        sb.append("?date=");
        sb.append(ndate);
        sb.append("&member=");
        sb.append(member);
        sb.append("\">");
        sb.append("<font color=\"");
        sb.append(GlobalConst.calUrlColor);
        sb.append("\"><font size=\"");
        sb.append(GlobalConst.calFontSize);
        sb.append("\"></font><B>\n");
        sb.append(nextMonthStr).append(" ").append(year);
        sb.append("</B></font>\n");
        sb.append("</a>");
        // url end

        //sb.append("</u></B></font>\n");
        sb.append("</TD>\n");

        sb.append("</TR>\n");
        sb.append("</TABLE>\n");

        sb.append("</TD>\n");
        sb.append("</TR>\n");

        // week days, month days
        sb.append("<TR>\n");
        sb.append("<TD WIDTH=\"100\">\n");

        StringBuffer colorBegin = new StringBuffer("<TD><font color=\"");
        colorBegin.append(GlobalConst.calLabelColor);
        colorBegin.append("\">");

        StringBuffer colorEnd = new StringBuffer("</font></TD>\n");

        sb.append("<TABLE BORDER=\"1\" CELLSPACING=\"1\">\n");
        sb.append("<TR>\n");

        sb.append(colorBegin.toString());
        sb.append("S");
        sb.append(colorEnd.toString());

        sb.append(colorBegin.toString());
        sb.append("M");
        sb.append(colorEnd.toString());

        sb.append(colorBegin.toString());
        sb.append("T");
        sb.append(colorEnd.toString());

        sb.append(colorBegin.toString());
        sb.append("W");
        sb.append(colorEnd.toString());

        sb.append(colorBegin.toString());
        sb.append("T");
        sb.append(colorEnd.toString());

        sb.append(colorBegin.toString());
        sb.append("F");
        sb.append(colorEnd.toString());

        sb.append(colorBegin.toString());
        sb.append("S");
        sb.append(colorEnd.toString());
        /*
               sb.append("<TD><font color=\"ffffff\">S</font></TD>\n");
               sb.append("<TD><font color=\"ffffff\">M</font></TD>\n");
               sb.append("<TD><font color=\"ffffff\">T</font></TD>\n");
               sb.append("<TD><font color=\"ffffff\">W</font></TD>\n");
               sb.append("<TD><font color=\"ffffff\">T</font></TD>\n");
               sb.append("<TD><font color=\"ffffff\">F</font></TD>\n");
               sb.append("<TD><font color=\"ffffff\">S</font></TD>\n");
        */

        sb.append("</TR>\n");
        int dayMax = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        int dayOfWeek = 0;
        for (int i = 1; i <= dayMax; i++) {
            cal.set(Calendar.DAY_OF_MONTH, i);
            ndate = yrNum + "-" + monthNumStr + "-" + i;

            // 01 to 09
            if (i < 10) {
                ndate = yrNum + "-" + monthNumStr + "-0" + i;
            }

            //logger.info("ndate = " + ndate);

            boolean islink = false;
            if (dates != null) {
                islink = dates.contains(ndate);
            }
            //logger.info("islink = " + ndate);
            dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

            if ((dayOfWeek == 1) || (i == 1)) {
                sb.append("<TR>\n");
                for (int j = 1; j < dayOfWeek; j++) {
                    sb.append("<TD>\n");
                    sb.append("&nbsp;");
                    sb.append("</TD>\n");
                }
            }
            sb.append("<TD>\n");
            if (islink) {
                //logger.info("islink exists " + url);
                sb.append("<a href=\"");
                sb.append(url);
                sb.append("&date=");
                sb.append(ndate);
                sb.append("\">");
                sb.append("<font color=\"");
                sb.append(GlobalConst.calUrlColor);
                sb.append("\">");
            } else {
                sb.append("<font color=\"");
                sb.append(GlobalConst.calLabelColor);
                sb.append("\">");
            }

            sb.append(i);
            sb.append("</font>");
            if (islink)
                sb.append("</a>");
            sb.append("</TD>\n");
            if (dayOfWeek == 7) {
                sb.append("</TR>\n");
            }
        }

        if (dayOfWeek != 7) {
            sb.append("</TR>\n");
        }
        sb.append("</TABLE>\n");
        sb.append("</TD>\n");
        sb.append("</TR>\n");

        /*
               sb.append("</TD>\n");
               sb.append("</TR>\n");
        */

        // table bottom
        sb.append("</TABLE>\n");
        return sb.toString();
    }

    /**
     * This property is set by spring. It is the highlight background
     * color of today's day.
     *
     * @param todayColor
     */
    public void setTodayColor(String todayColor) {
        this.todayColor = todayColor;
    }

    /**
     * Parses incoming date in this format: 2006-01-22 20:28:51.0
     *
     * @param date
     */
    public String getDisplayDate(String date) throws ParseException {
        return getDisplayDate(nativeFormatter.parse(date));
    }

    /**
     * Parses incoming date in this format: 2006-01-22 20:28:51.0
     *
     * @param date
     */
    public String getDisplayDate(Date date) throws ParseException {
        return displayFormatter.format(date);
    }

    /**
     * This property is set by spring. It is the highlight background
     * color of current date's day.
     *
     * @param currentColor
     */
    public void setCurrentColor(String currentColor) {
        this.currentColor = currentColor;
    }

    /**
     * Date formatter set by spring. Used for parsing db dates.
     *
     * @param nativeFormatter
     */
    public void setNativeFormatter(SimpleDateFormat nativeFormatter) {
        this.nativeFormatter = nativeFormatter;
    }

    /**
     * Date formatter set by spring. Used for displaying dates.
     *
     * @param displayFormatter
     */
    public void setDisplayFormatter(SimpleDateFormat displayFormatter) {
        this.displayFormatter = displayFormatter;
    }

    public static void main(String[] args) {
        DnCalendar dncalendar = new DnCalendar();
        //dncalendar.getWidget(args[0], "showpersonalblog?loginid=3432443", null);
        HashSet dates = new HashSet();
        //List dates = new ArrayList();
        dates.add("2005-04-06");
        dates.add("2005-04-10");
        dates.add("2005-04-15");
        dates.add("2005-04-25");
        //System.out.println(dncalendar.getWidget(args[0], "showpersonalblog?loginid=3432443", dates));
    }
}