001: package dinamica.calendar;
002:
003: import dinamica.*;
004: import java.sql.Types;
005: import java.util.*;
006:
007: /**
008: * Custom Calendar Transaction for Dinamica's built-in
009: * calendar services. All calendar transactions must
010: * store a recordset as a request attribute with ID "calendar"
011: * that should contain 3 columns:<br>
012: * <ul>
013: * <li> day - integer
014: * <li> onclick - varchar, value of the onclick attribute
015: * <li> html - content of the calendar cell for this day
016: * <li> class - class name defining style for this cell (see calendar template)
017: * </ul>
018: * <br>
019: * This transaction expects 2 optional request parameters:
020: * <ul>
021: * <li> id - ID of the element (textbox) attached to the calendar
022: * <li> date - selected date if format dd-mm-yyyy
023: * </ul>
024: * <br>
025: * This calendar transaction in particular was designed to be used
026: * by popup JavaScript calendars attached to textboxes, but can also
027: * be used for clickable agendas, etc.
028: * <br>
029: * Creation date: 2006-06-04
030: * @author mcordova (dinamica@martincordova.com)
031: *
032: */
033:
034: public class DefaultCalendar extends GenericTransaction {
035:
036: public int service(Recordset inputs) throws Throwable {
037: super .service(inputs);
038:
039: //retrieve parameters
040: String id = getRequest().getParameter("id");
041: String d = getRequest().getParameter("date");
042: String d2 = getRequest().getParameter("date.lbound");
043: String d3 = getRequest().getParameter("date.ubound");
044:
045: if (d2 == null)
046: d2 = "";
047:
048: if (d3 == null)
049: d3 = "";
050:
051: //set date
052: Date calDate = ValidatorUtil.testDate(d, "dd-MM-yy");
053: if (calDate == null)
054: calDate = new Date();
055:
056: //lower bound date
057: Date minDate = ValidatorUtil.testDate(d2, "dd-MM-yy");
058:
059: //upper bound date
060: Date maxDate = ValidatorUtil.testDate(d3, "dd-MM-yy");
061:
062: // save attached textbox id - used for callbacks
063: getRequest().setAttribute("parent.ElementID", id);
064: getRequest().setAttribute("date.lbound", d2);
065: getRequest().setAttribute("date.ubound", d3);
066:
067: // calendar object
068: Calendar c = Calendar.getInstance();
069: c.setLenient(true);
070: c.setTime(calDate);
071:
072: //recordset with selected year and month
073: Recordset rsDate = new Recordset();
074: rsDate.append("year", Types.INTEGER);
075: rsDate.append("month", Types.INTEGER);
076: rsDate.addNew();
077: rsDate.setValue("year", new Integer(c.get(Calendar.YEAR)));
078: rsDate
079: .setValue("month", new Integer(
080: c.get(Calendar.MONTH) + 1));
081:
082: //recordset with years
083: Recordset rsYears = new Recordset();
084: rsYears.append("year", Types.INTEGER);
085: for (int i = c.get(Calendar.YEAR) - 15; i < c
086: .get(Calendar.YEAR) + 15; i++) {
087: rsYears.addNew();
088: rsYears.setValue("year", new Integer(i));
089: }
090:
091: //selected day (if date==null -> day = today)
092: int day = c.get(Calendar.DAY_OF_MONTH);
093:
094: //set first day of month
095: c.set(Calendar.DATE, 1);
096:
097: //get partial date mask - used as an href parameter
098: String partialDate = StringUtil.formatDate(calDate, "M-yyyy");
099:
100: // define recordset
101: Recordset rs = createCalendarRecordset();
102:
103: // fill empty slots until 1st day of month
104: for (int i = Calendar.SUNDAY; i < c.get(Calendar.DAY_OF_WEEK); i++) {
105: rs.addNew();
106: rs.setValue("onclick", "");
107: rs.setValue("html", null);
108: rs.setValue("class", "calEmptyCell");
109: }
110:
111: // feed recordset with selected month
112: for (int i = 1; i <= c.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
113: c.set(Calendar.DATE, i);
114: String date = i + "-" + partialDate;
115: rs.addNew();
116: rs.setValue("day", new Integer(i));
117: rs.setValue("onclick", this .getCellOnClickValue(id, c, day,
118: minDate, maxDate, date));
119: rs.setValue("html", this .getCellHTML(c, day));
120: rs.setValue("class", this .getCellStyle(c, day, minDate,
121: maxDate));
122: }
123:
124: // fill empty slots from last day of month until next saturday
125: if (c.get(Calendar.DAY_OF_WEEK) < Calendar.SATURDAY) {
126: for (int i = c.get(Calendar.DAY_OF_WEEK); i < Calendar.SATURDAY; i++) {
127: rs.addNew();
128: rs.setValue("onclick", "");
129: rs.setValue("html", null);
130: rs.setValue("class", "calEmptyCell");
131: }
132: }
133:
134: //publish recordset as a request attribute for HGrid output class
135: getRequest().setAttribute("calendar", rs);
136:
137: //publish recordset with current month and year (used by combos)
138: publish("calconfig", rsDate);
139: publish("years", rsYears);
140:
141: return 0;
142:
143: }
144:
145: /**
146: * Creates the recordset that contains the basic data
147: * to print a Calendar.
148: * @return Recordset with this structure: day (int), onclick (varchar), html (varchar), class (varchar)
149: * @throws Throwable
150: */
151: Recordset createCalendarRecordset() throws Throwable {
152: Recordset rs = new Recordset();
153: rs.append("day", Types.INTEGER);
154: rs.append("onclick", Types.VARCHAR);
155: rs.append("html", Types.VARCHAR);
156: rs.append("class", Types.VARCHAR);
157: return rs;
158: }
159:
160: /**
161: * Returns the HTML content for a given calendar cell,
162: * only for cells that contain a day- not called for empty cells.
163: * @param c Calendar set for the day to be printed
164: * @param currentDay Current day
165: * @return A String that contains the HTML to be printed into the cell
166: * @throws Throwable
167: */
168: protected String getCellHTML(Calendar c, int currentDay)
169: throws Throwable {
170: return String.valueOf(c.get(Calendar.DATE));
171: }
172:
173: /**
174: * Returns the value of the CLASS attribute for a given calendar cell,
175: * only for cells that contain a day- not called for empty cells.
176: * @param c Calendar set for the day to be printed
177: * @param currentDay Selected day
178: * @param minDate Minimal date that can be selected (may be null)
179: * @return A String that contains the name of the class attribute for the table cell,
180: * should match those styles defined in the Calendar main template.
181: * @throws Throwable
182: */
183: protected String getCellStyle(Calendar c, int currentDay,
184: Date minDate, Date maxDate) throws Throwable {
185:
186: //default
187: String styleName = "calDay";
188:
189: //is current day?
190: if (c.get(Calendar.DATE) == currentDay)
191: styleName = "calCurDay";
192:
193: //should be disabled?
194: if (minDate != null) {
195: if (c.getTime().compareTo(minDate) < 0)
196: styleName = "calDisabled";
197: }
198: if (maxDate != null) {
199: if (c.getTime().compareTo(maxDate) > 0)
200: styleName = "calDisabled";
201: }
202:
203: return styleName;
204:
205: }
206:
207: /**
208: * Returns the value of the ONCLICK attribute for a given calendar cell,
209: * only for cells that contain a day- not called for empty cells.
210: * @param elementID ID of the element (textbox) attached to the Calendar
211: * @param c Calendar set for the day to be printed
212: * @param currentDay Selected day
213: * @param minDate Minimal date that can be selected (may be null)
214: * @param date Calendar date preformated as dd-mm-yyyy
215: * @return A String that contains the value of the onclick attribute for the table cell (calls some javascript function)
216: * @throws Throwable
217: */
218: protected String getCellOnClickValue(String elementID, Calendar c,
219: int currentDay, Date minDate, Date maxDate, String date)
220: throws Throwable {
221:
222: //default
223: String onclick = "parent.calendarReturnValue('" + elementID
224: + "', '" + date + "')";
225:
226: //should be disabled?
227: if (minDate != null) {
228: if (c.getTime().compareTo(minDate) < 0)
229: onclick = "";
230: }
231:
232: if (maxDate != null) {
233: if (c.getTime().compareTo(maxDate) > 0)
234: onclick = "";
235: }
236:
237: return onclick;
238:
239: }
240:
241: }
|