001: /**
002: * EventsManager.java Created on 08.03.2003, 12:35:19 Alex Package:
003: * net.sf.memoranda
004: *
005: * @author Alex V. Alishevskikh, alex@openmechanics.net Copyright (c) 2003
006: * Memoranda Team. http://memoranda.sf.net
007: */package net.sf.memoranda;
008:
009: import java.util.Calendar;
010: import java.util.Collection;
011: import java.util.HashMap;
012: import java.util.Vector;
013: import java.util.Map;
014:
015: import java.util.Collections; //import java.util.ArrayList;
016:
017: import net.sf.memoranda.date.CalendarDate;
018: import net.sf.memoranda.util.CurrentStorage;
019: import net.sf.memoranda.util.Util;
020: import nu.xom.Attribute; //import nu.xom.Comment;
021: import nu.xom.Document;
022: import nu.xom.Element;
023: import nu.xom.Elements;
024: import nu.xom.ParentNode;
025:
026: /**
027: *
028: */
029: /*$Id: EventsManager.java,v 1.11 2004/10/06 16:00:11 ivanrise Exp $*/
030: public class EventsManager {
031: /* public static final String NS_JNEVENTS =
032: "http://www.openmechanics.org/2003/jnotes-events-file";
033: */
034: public static final int NO_REPEAT = 0;
035: public static final int REPEAT_DAILY = 1;
036: public static final int REPEAT_WEEKLY = 2;
037: public static final int REPEAT_MONTHLY = 3;
038: public static final int REPEAT_YEARLY = 4;
039:
040: public static Document _doc = null;
041: static Element _root = null;
042:
043: static {
044: CurrentStorage.get().openEventsManager();
045: if (_doc == null) {
046: _root = new Element("eventslist");
047: /* _root.addNamespaceDeclaration("jnevents", NS_JNEVENTS);
048: _root.appendChild(
049: new Comment("This is JNotes 2 data file. Do not modify.")); */
050: _doc = new Document(_root);
051: } else
052: _root = _doc.getRootElement();
053:
054: }
055:
056: public static void createSticker(String text) {
057: Element el = new Element("sticker");
058: el.addAttribute(new Attribute("id", Util.generateId()));
059: el.appendChild(text);
060: _root.appendChild(el);
061: }
062:
063: public static Map getStickers() {
064: Map m = new HashMap();
065: Elements els = _root.getChildElements("sticker");
066: for (int i = 0; i < els.size(); i++) {
067: Element se = els.get(i);
068: m.put(se.getAttribute("id").getValue(), se.getValue());
069: }
070: return m;
071: }
072:
073: public static void removeSticker(String stickerId) {
074: Elements els = _root.getChildElements("sticker");
075: for (int i = 0; i < els.size(); i++) {
076: Element se = els.get(i);
077: if (se.getAttribute("id").getValue().equals(stickerId)) {
078: _root.removeChild(se);
079: break;
080: }
081: }
082: }
083:
084: public static boolean isNREventsForDate(CalendarDate date) {
085: Day d = getDay(date);
086: if (d == null)
087: return false;
088: if (d.getElement().getChildElements("event").size() > 0)
089: return true;
090: return false;
091: }
092:
093: public static Collection getEventsForDate(CalendarDate date) {
094: Vector v = new Vector();
095: Day d = getDay(date);
096: if (d != null) {
097: Elements els = d.getElement().getChildElements("event");
098: for (int i = 0; i < els.size(); i++)
099: v.add(new EventImpl(els.get(i)));
100: }
101: Collection r = getRepeatableEventsForDate(date);
102: if (r.size() > 0)
103: v.addAll(r);
104: //EventsVectorSorter.sort(v);
105: Collections.sort(v);
106: return v;
107: }
108:
109: public static Event createEvent(CalendarDate date, int hh, int mm,
110: String text) {
111: Element el = new Element("event");
112: el.addAttribute(new Attribute("id", Util.generateId()));
113: el.addAttribute(new Attribute("hour", String.valueOf(hh)));
114: el.addAttribute(new Attribute("min", String.valueOf(mm)));
115: el.appendChild(text);
116: Day d = getDay(date);
117: if (d == null)
118: d = createDay(date);
119: d.getElement().appendChild(el);
120: return new EventImpl(el);
121: }
122:
123: public static Event createRepeatableEvent(int type,
124: CalendarDate startDate, CalendarDate endDate, int period,
125: int hh, int mm, String text, boolean workDays) {
126: Element el = new Element("event");
127: Element rep = _root.getFirstChildElement("repeatable");
128: if (rep == null) {
129: rep = new Element("repeatable");
130: _root.appendChild(rep);
131: }
132: el.addAttribute(new Attribute("repeat-type", String
133: .valueOf(type)));
134: el.addAttribute(new Attribute("id", Util.generateId()));
135: el.addAttribute(new Attribute("hour", String.valueOf(hh)));
136: el.addAttribute(new Attribute("min", String.valueOf(mm)));
137: el
138: .addAttribute(new Attribute("startDate", startDate
139: .toString()));
140: if (endDate != null)
141: el
142: .addAttribute(new Attribute("endDate", endDate
143: .toString()));
144: el
145: .addAttribute(new Attribute("period", String
146: .valueOf(period)));
147: // new attribute for wrkin days - ivanrise
148: el.addAttribute(new Attribute("workingDays", String
149: .valueOf(workDays)));
150: el.appendChild(text);
151: rep.appendChild(el);
152: return new EventImpl(el);
153: }
154:
155: public static Collection getRepeatableEvents() {
156: Vector v = new Vector();
157: Element rep = _root.getFirstChildElement("repeatable");
158: if (rep == null)
159: return v;
160: Elements els = rep.getChildElements("event");
161: for (int i = 0; i < els.size(); i++)
162: v.add(new EventImpl(els.get(i)));
163: return v;
164: }
165:
166: public static Collection getRepeatableEventsForDate(
167: CalendarDate date) {
168: Vector reps = (Vector) getRepeatableEvents();
169: Vector v = new Vector();
170: for (int i = 0; i < reps.size(); i++) {
171: Event ev = (Event) reps.get(i);
172:
173: // --- ivanrise
174: // ignore this event if it's a 'only working days' event and today is weekend.
175: if (ev.getWorkingDays()
176: && (date.getCalendar().get(Calendar.DAY_OF_WEEK) == 1 || date
177: .getCalendar().get(Calendar.DAY_OF_WEEK) == 7))
178: continue;
179: // ---
180: /*
181: * /if ( ((date.after(ev.getStartDate())) &&
182: * (date.before(ev.getEndDate()))) ||
183: * (date.equals(ev.getStartDate()))
184: */
185: //System.out.println(date.inPeriod(ev.getStartDate(),
186: // ev.getEndDate()));
187: if (date.inPeriod(ev.getStartDate(), ev.getEndDate())) {
188: if (ev.getRepeat() == REPEAT_DAILY) {
189: int n = date.getCalendar()
190: .get(Calendar.DAY_OF_YEAR);
191: int ns = ev.getStartDate().getCalendar().get(
192: Calendar.DAY_OF_YEAR);
193: //System.out.println((n - ns) % ev.getPeriod());
194: if ((n - ns) % ev.getPeriod() == 0)
195: v.add(ev);
196: } else if (ev.getRepeat() == REPEAT_WEEKLY) {
197: if (date.getCalendar().get(Calendar.DAY_OF_WEEK) == ev
198: .getPeriod())
199: v.add(ev);
200: } else if (ev.getRepeat() == REPEAT_MONTHLY) {
201: if (date.getCalendar().get(Calendar.DAY_OF_MONTH) == ev
202: .getPeriod())
203: v.add(ev);
204: } else if (ev.getRepeat() == REPEAT_YEARLY) {
205: int period = ev.getPeriod();
206: //System.out.println(date.getCalendar().get(Calendar.DAY_OF_YEAR));
207: if ((date.getYear() % 4) == 0
208: && date.getCalendar().get(
209: Calendar.DAY_OF_YEAR) > 60)
210: period++;
211:
212: if (date.getCalendar().get(Calendar.DAY_OF_YEAR) == period)
213: v.add(ev);
214: }
215: }
216: }
217: return v;
218: }
219:
220: public static Collection getActiveEvents() {
221: return getEventsForDate(CalendarDate.today());
222: }
223:
224: public static Event getEvent(CalendarDate date, int hh, int mm) {
225: Day d = getDay(date);
226: if (d == null)
227: return null;
228: Elements els = d.getElement().getChildElements("event");
229: for (int i = 0; i < els.size(); i++) {
230: Element el = els.get(i);
231: if ((new Integer(el.getAttribute("hour").getValue())
232: .intValue() == hh)
233: && (new Integer(el.getAttribute("min").getValue())
234: .intValue() == mm))
235: return new EventImpl(el);
236: }
237: return null;
238: }
239:
240: public static void removeEvent(CalendarDate date, int hh, int mm) {
241: Day d = getDay(date);
242: if (d == null)
243: d.getElement().removeChild(
244: getEvent(date, hh, mm).getContent());
245: }
246:
247: public static void removeEvent(Event ev) {
248: ParentNode parent = ev.getContent().getParent();
249: parent.removeChild(ev.getContent());
250: }
251:
252: private static Day createDay(CalendarDate date) {
253: Year y = getYear(date.getYear());
254: if (y == null)
255: y = createYear(date.getYear());
256: Month m = y.getMonth(date.getMonth());
257: if (m == null)
258: m = y.createMonth(date.getMonth());
259: Day d = m.getDay(date.getDay());
260: if (d == null)
261: d = m.createDay(date.getDay());
262: return d;
263: }
264:
265: private static Year createYear(int y) {
266: Element el = new Element("year");
267: el
268: .addAttribute(new Attribute("year", new Integer(y)
269: .toString()));
270: _root.appendChild(el);
271: return new Year(el);
272: }
273:
274: private static Year getYear(int y) {
275: Elements yrs = _root.getChildElements("year");
276: String yy = new Integer(y).toString();
277: for (int i = 0; i < yrs.size(); i++)
278: if (yrs.get(i).getAttribute("year").getValue().equals(yy))
279: return new Year(yrs.get(i));
280: //return createYear(y);
281: return null;
282: }
283:
284: private static Day getDay(CalendarDate date) {
285: Year y = getYear(date.getYear());
286: if (y == null)
287: return null;
288: Month m = y.getMonth(date.getMonth());
289: if (m == null)
290: return null;
291: return m.getDay(date.getDay());
292: }
293:
294: static class Year {
295: Element yearElement = null;
296:
297: public Year(Element el) {
298: yearElement = el;
299: }
300:
301: public int getValue() {
302: return new Integer(yearElement.getAttribute("year")
303: .getValue()).intValue();
304: }
305:
306: public Month getMonth(int m) {
307: Elements ms = yearElement.getChildElements("month");
308: String mm = new Integer(m).toString();
309: for (int i = 0; i < ms.size(); i++)
310: if (ms.get(i).getAttribute("month").getValue().equals(
311: mm))
312: return new Month(ms.get(i));
313: //return createMonth(m);
314: return null;
315: }
316:
317: private Month createMonth(int m) {
318: Element el = new Element("month");
319: el.addAttribute(new Attribute("month", new Integer(m)
320: .toString()));
321: yearElement.appendChild(el);
322: return new Month(el);
323: }
324:
325: public Vector getMonths() {
326: Vector v = new Vector();
327: Elements ms = yearElement.getChildElements("month");
328: for (int i = 0; i < ms.size(); i++)
329: v.add(new Month(ms.get(i)));
330: return v;
331: }
332:
333: public Element getElement() {
334: return yearElement;
335: }
336:
337: }
338:
339: static class Month {
340: Element mElement = null;
341:
342: public Month(Element el) {
343: mElement = el;
344: }
345:
346: public int getValue() {
347: return new Integer(mElement.getAttribute("month")
348: .getValue()).intValue();
349: }
350:
351: public Day getDay(int d) {
352: if (mElement == null)
353: return null;
354: Elements ds = mElement.getChildElements("day");
355: String dd = new Integer(d).toString();
356: for (int i = 0; i < ds.size(); i++)
357: if (ds.get(i).getAttribute("day").getValue().equals(dd))
358: return new Day(ds.get(i));
359: //return createDay(d);
360: return null;
361: }
362:
363: private Day createDay(int d) {
364: Element el = new Element("day");
365: el.addAttribute(new Attribute("day", new Integer(d)
366: .toString()));
367: el.addAttribute(new Attribute("date", new CalendarDate(d,
368: getValue(), new Integer(((Element) mElement
369: .getParent()).getAttribute("year")
370: .getValue()).intValue()).toString()));
371:
372: mElement.appendChild(el);
373: return new Day(el);
374: }
375:
376: public Vector getDays() {
377: if (mElement == null)
378: return null;
379: Vector v = new Vector();
380: Elements ds = mElement.getChildElements("day");
381: for (int i = 0; i < ds.size(); i++)
382: v.add(new Day(ds.get(i)));
383: return v;
384: }
385:
386: public Element getElement() {
387: return mElement;
388: }
389:
390: }
391:
392: static class Day {
393: Element dEl = null;
394:
395: public Day(Element el) {
396: dEl = el;
397: }
398:
399: public int getValue() {
400: return new Integer(dEl.getAttribute("day").getValue())
401: .intValue();
402: }
403:
404: /*
405: * public Note getNote() { return new NoteImpl(dEl);
406: */
407:
408: public Element getElement() {
409: return dEl;
410: }
411: }
412: /*
413: static class EventsVectorSorter {
414:
415: private static Vector keys = null;
416:
417: private static int toMinutes(Object obj) {
418: Event ev = (Event) obj;
419: return ev.getHour() * 60 + ev.getMinute();
420: }
421:
422: private static void doSort(int L, int R) { // Hoar's QuickSort
423: int i = L;
424: int j = R;
425: int x = toMinutes(keys.get((L + R) / 2));
426: Object w = null;
427: do {
428: while (toMinutes(keys.get(i)) < x) {
429: i++;
430: }
431: while (x < toMinutes(keys.get(j))) {
432: j--;
433: }
434: if (i <= j) {
435: w = keys.get(i);
436: keys.set(i, keys.get(j));
437: keys.set(j, w);
438: i++;
439: j--;
440: }
441: }
442: while (i <= j);
443: if (L < j) {
444: doSort(L, j);
445: }
446: if (i < R) {
447: doSort(i, R);
448: }
449: }
450:
451: public static void sort(Vector theKeys) {
452: if (theKeys == null)
453: return;
454: if (theKeys.size() <= 0)
455: return;
456: keys = theKeys;
457: doSort(0, keys.size() - 1);
458: }
459:
460: }
461: */
462: }
|