001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.module.builders;
011:
012: import java.util.*;
013:
014: import org.mmbase.storage.search.*;
015: import org.mmbase.storage.search.implementation.*;
016: import org.mmbase.module.core.*;
017: import org.mmbase.util.*;
018: import org.mmbase.util.logging.*;
019:
020: /**
021: * @javadoc
022: * @application Tools
023: * @author Daniel Ockeloen
024: * @version $Id: MMEvents.java,v 1.23 2007/08/03 18:13:42 michiel Exp $
025: */
026: public class MMEvents extends MMObjectBuilder {
027: private static final Logger log = Logging
028: .getLoggerInstance(MMEvents.class);
029: MMEventsProbe probe;
030: DateStrings datestrings;
031: private int notifyWindow = 3600;
032: private boolean enableNotify = true;
033:
034: public MMEvents() {
035: }
036:
037: public boolean init() {
038: String tmp;
039: int nw;
040: super .init();
041: datestrings = new DateStrings(mmb.getLanguage());
042: tmp = getInitParameter("NotifyWindow");
043: if (tmp != null) {
044: try {
045: nw = Integer.parseInt(tmp);
046: notifyWindow = nw;
047: } catch (NumberFormatException xx) {
048: }
049: }
050: tmp = getInitParameter("EnableNotify");
051: if (tmp != null && (tmp.equals("false") || tmp.equals("no"))) {
052: enableNotify = false;
053: }
054: if (enableNotify)
055: probe = new MMEventsProbe(this );
056: return true;
057: }
058:
059: public String getGUIIndicator(MMObjectNode node) {
060: int tmp = node.getIntValue("start");
061: //String str=DateSupport.getMonthDay(tmp)+"/"+DateSupport.getMonth(tmp)+"/"+DateSupport.getYear(tmp);
062: String str = DateSupport.getTime(tmp) + "/"
063: + DateSupport.getMonthDay(tmp) + "/"
064: + DateSupport.getMonth(tmp) + "/"
065: + DateSupport.getYear(tmp);
066: return (str);
067: }
068:
069: public String getGUIIndicator(String field, MMObjectNode node) {
070: if (field.equals("start")) {
071: int str = node.getIntValue("start");
072: return (DateSupport.getTimeSec(str) + " op "
073: + DateSupport.getMonthDay(str) + "/"
074: + DateSupport.getMonth(str) + "/" + DateSupport
075: .getYear(str));
076: } else if (field.equals("stop")) {
077: int str = node.getIntValue("stop");
078: return (DateSupport.getTimeSec(str) + " op "
079: + DateSupport.getMonthDay(str) + "/"
080: + DateSupport.getMonth(str) + "/" + DateSupport
081: .getYear(str));
082: } else if (field.equals("playtime")) {
083: int str = node.getIntValue("playtime");
084: return (DateSupport.getTimeSecLen(str));
085: }
086: return (null);
087: }
088:
089: public Object getValue(MMObjectNode node, String field) {
090: if (field.indexOf("time_") != -1) {
091: int str = node.getIntValue(field.substring(5));
092: return DateSupport.getTime(str);
093: } else if (field.equals("time(start)")) {
094: //node.prefix = "mmevents.";
095: int str = node.getIntValue("start");
096: //node.prefix="";
097: return DateSupport.getTime(str);
098: } else if (field.equals("time(stop)")) {
099: //node.prefix="mmevents.";
100: int str = node.getIntValue("stop");
101: //node.prefix="";
102: return DateSupport.getTime(str);
103: } else if (field.indexOf("timesec_") != -1) {
104: int str = node.getIntValue(field.substring(8));
105: return DateSupport.getTimeSec(str);
106: } else if (field.indexOf("longmonth_") != -1) {
107: int str = node.getIntValue(field.substring(10));
108: return datestrings.getMonth(DateSupport.getMonthInt(str));
109: } else if (field.indexOf("month_") != -1) {
110: int str = node.getIntValue(field.substring(6));
111: return datestrings.getShortMonth(DateSupport
112: .getMonthInt(str));
113: } else if (field.indexOf("weekday_") != -1) {
114: int str = node.getIntValue(field.substring(8));
115: return datestrings.getDay(DateSupport.getWeekDayInt(str));
116: } else if (field.indexOf("shortday_") != -1) {
117: int str = node.getIntValue(field.substring(8));
118: return datestrings.getShortDay(DateSupport
119: .getWeekDayInt(str));
120: } else if (field.indexOf("day_") != -1) {
121: int str = node.getIntValue(field.substring(4));
122: return "" + DateSupport.getDayInt(str);
123: } else if (field.indexOf("year_") != -1) {
124: int str = node.getIntValue(field.substring(5));
125: return DateSupport.getYear(str);
126: }
127: return super .getValue(node, field);
128: }
129:
130: public void probeCall() {
131: // the queue is really a bad idea have to make up
132: // a better way.
133: Vector<MMObjectNode> also = new Vector<MMObjectNode>();
134: log.debug("MMEvent probe CALL");
135: int now = (int) (System.currentTimeMillis() / 1000);
136: log.debug("The currenttime in seconds NOW=" + now);
137: MMObjectNode snode = null, enode = null;
138:
139: try {
140: NodeSearchQuery query = new NodeSearchQuery(this );
141: StepField startField = query.getField(getField("start"));
142: query.addSortOrder(startField);
143: query.setConstraint(new BasicFieldValueBetweenConstraint(
144: startField, now, now + notifyWindow));
145: if (log.isDebugEnabled())
146: log.debug("Executing query " + query);
147: also.addAll(getNodes(query));
148: if (also.size() > 0) {
149: snode = also.lastElement();
150: }
151: } catch (SearchQueryException e) {
152: log.error(e);
153: }
154:
155: try {
156: NodeSearchQuery query = new NodeSearchQuery(this );
157: StepField stopField = query.getField(getField("stop"));
158: query.addSortOrder(stopField);
159: query.setConstraint(new BasicFieldValueBetweenConstraint(
160: stopField, now, now + notifyWindow));
161: if (log.isDebugEnabled())
162: log.debug("Executing query " + query);
163: also.addAll(getNodes(query));
164: if (also.size() > 0) {
165: enode = also.lastElement();
166: }
167: } catch (SearchQueryException e) {
168: log.error(e);
169: }
170: MMObjectNode wnode = null;
171: int sleeptime = -1;
172: if (snode != null && enode == null) {
173: sleeptime = snode.getIntValue("start");
174: wnode = snode;
175: }
176: if (snode == null && enode != null) {
177: sleeptime = enode.getIntValue("stop");
178: wnode = enode;
179: }
180: if (snode != null && enode != null) {
181: if (snode.getIntValue("start") < enode.getIntValue("stop")) {
182: sleeptime = snode.getIntValue("start");
183: wnode = snode;
184: } else {
185: sleeptime = enode.getIntValue("stop");
186: wnode = enode;
187: }
188: }
189:
190: if (sleeptime != -1) {
191: if (log.isDebugEnabled()) {
192: log.debug("SLEEPTIME=" + (sleeptime - now) + " wnode="
193: + wnode + " also=" + also);
194: }
195: try {
196: Thread.sleep((sleeptime - now) * 1000);
197: } catch (InterruptedException f) {
198: log.debug("interrupted while sleeping");
199: }
200: log.debug("Node local change "
201: + wnode.getIntValue("number"));
202: super .nodeLocalChanged(mmb.getMachineName(), ""
203: + wnode.getIntValue("number"), tableName, "c");
204: Enumeration<MMObjectNode> g = also.elements();
205: while (g.hasMoreElements()) {
206: wnode = g.nextElement();
207: if ((wnode.getIntValue("start") == sleeptime)
208: || (wnode.getIntValue("stop") == sleeptime)) {
209: log.debug("Node local change "
210: + wnode.getIntValue("number"));
211: super .nodeLocalChanged(mmb.getMachineName(), ""
212: + wnode.getIntValue("number"), tableName,
213: "c");
214: }
215: }
216: } else {
217: try {
218: Thread.sleep(300 * 1000);
219: } catch (InterruptedException f) {
220: log.debug("interrupted while sleeping");
221: }
222: }
223: }
224:
225: public int insert(String owner, MMObjectNode node) {
226: int val = node.getIntValue("start");
227: int newval = (int) (System.currentTimeMillis() / 1000);
228: if (val == -1) {
229: node.setValue("start", newval);
230:
231: }
232: val = node.getIntValue("stop");
233: if (val == -1) {
234: node.setValue("stop", newval);
235:
236: }
237: return (super .insert(owner, node));
238: }
239:
240: public boolean commit(MMObjectNode node) {
241: int val = node.getIntValue("start");
242: int newval = (int) (System.currentTimeMillis() / 1000);
243: if (val == -1) {
244: node.setValue("start", newval);
245:
246: }
247: val = node.getIntValue("stop");
248: if (val == -1) {
249: node.setValue("stop", newval);
250:
251: }
252: return (super.commit(node));
253: }
254: }
|