1 10 package org.mmbase.module.builders; 11 12 import java.util.*; 13 14 import org.mmbase.storage.search.*; 15 import org.mmbase.storage.search.implementation.*; 16 import org.mmbase.module.core.*; 17 import org.mmbase.util.*; 18 import org.mmbase.util.logging.*; 19 20 26 public class MMEvents extends MMObjectBuilder { 27 private static final Logger log = Logging.getLoggerInstance(MMEvents.class); 28 MMEventsProbe probe; 29 DateStrings datestrings; 30 private int notifyWindow=3600; 31 private boolean enableNotify=true; 32 33 public MMEvents() { 34 } 35 36 public boolean init() { 37 String tmp; 38 int nw; 39 super.init(); 40 datestrings = new DateStrings(mmb.getLanguage()); 41 tmp=getInitParameter("NotifyWindow"); 42 if (tmp!=null) { 43 try { 44 nw=Integer.parseInt(tmp); 45 notifyWindow=nw; 46 } catch (NumberFormatException xx) {} 47 } 48 tmp=getInitParameter("EnableNotify"); 49 if (tmp!=null && (tmp.equals("false") || tmp.equals("no"))) { 50 enableNotify=false; 51 } 52 if (enableNotify) probe=new MMEventsProbe(this); 53 return true; 54 } 55 56 57 public String getGUIIndicator(MMObjectNode node) { 58 int tmp=node.getIntValue("start"); 59 String str=DateSupport.getTime(tmp)+"/"+DateSupport.getMonthDay(tmp)+"/"+DateSupport.getMonth(tmp)+"/"+DateSupport.getYear(tmp); 61 return(str); 62 } 63 64 public String getGUIIndicator(String field,MMObjectNode node) { 65 if (field.equals("start")) { 66 int str=node.getIntValue("start"); 67 return(DateSupport.getTimeSec(str)+" op "+DateSupport.getMonthDay(str)+"/"+DateSupport.getMonth(str)+"/"+DateSupport.getYear(str)); 68 } else if (field.equals("stop")) { 69 int str=node.getIntValue("stop"); 70 return(DateSupport.getTimeSec(str)+" op "+DateSupport.getMonthDay(str)+"/"+DateSupport.getMonth(str)+"/"+DateSupport.getYear(str)); 71 } else if (field.equals("playtime")) { 72 int str=node.getIntValue("playtime"); 73 return(DateSupport.getTimeSecLen(str)); 74 } 75 return(null); 76 } 77 78 public Object getValue(MMObjectNode node, String field) { 79 if (field.indexOf("time_")!=-1) { 80 int str = node.getIntValue(field.substring(5)); 81 return DateSupport.getTime(str); 82 } else if (field.equals("time(start)")) { 83 int str=node.getIntValue("start"); 85 return DateSupport.getTime(str); 87 } else if (field.equals("time(stop)")) { 88 int str = node.getIntValue("stop"); 90 return DateSupport.getTime(str); 92 } else if (field.indexOf("timesec_")!=-1) { 93 int str = node.getIntValue(field.substring(8)); 94 return DateSupport.getTimeSec(str); 95 } else if (field.indexOf("longmonth_")!=-1) { 96 int str = node.getIntValue(field.substring(10)); 97 return datestrings.getMonth(DateSupport.getMonthInt(str)); 98 } else if (field.indexOf("month_")!=-1) { 99 int str = node.getIntValue(field.substring(6)); 100 return datestrings.getShortMonth(DateSupport.getMonthInt(str)); 101 } else if (field.indexOf("weekday_")!=-1) { 102 int str = node.getIntValue(field.substring(8)); 103 return datestrings.getDay(DateSupport.getWeekDayInt(str)); 104 } else if (field.indexOf("shortday_")!=-1) { 105 int str = node.getIntValue(field.substring(8)); 106 return datestrings.getShortDay(DateSupport.getWeekDayInt(str)); 107 } else if (field.indexOf("day_")!=-1) { 108 int str = node.getIntValue(field.substring(4)); 109 return ""+DateSupport.getDayInt(str); 110 } else if (field.indexOf("year_")!=-1) { 111 int str = node.getIntValue(field.substring(5)); 112 return DateSupport.getYear(str); 113 } 114 return super.getValue(node,field); 115 } 116 117 public void probeCall() { 118 Vector also = new Vector(); 121 log.debug("MMEvent probe CALL"); 122 int now=(int)(System.currentTimeMillis()/1000); 123 log.debug("The currenttime in seconds NOW="+now); 124 MMObjectNode snode = null, enode = null; 125 126 127 try { 128 NodeSearchQuery query = new NodeSearchQuery(this); 129 StepField startField = query.getField(getField("start")); 130 query.addSortOrder(startField); 131 query.setConstraint(new BasicFieldValueBetweenConstraint(startField, new Integer (now), new Integer (now + notifyWindow))); 132 if (log.isDebugEnabled()) log.debug("Executing query " + query); 133 also.addAll(getNodes(query)); 134 if (also.size() > 0) { 135 snode = (MMObjectNode) also.lastElement(); 136 } 137 } catch (SearchQueryException e) { 138 log.error(e); 139 } 140 141 try { 142 NodeSearchQuery query = new NodeSearchQuery(this); 143 StepField stopField = query.getField(getField("stop")); 144 query.addSortOrder(stopField); 145 query.setConstraint(new BasicFieldValueBetweenConstraint(stopField, new Integer (now), new Integer (now + notifyWindow))); 146 if (log.isDebugEnabled()) log.debug("Executing query " + query); 147 also.addAll(getNodes(query)); 148 if (also.size() > 0 ) { 149 enode = (MMObjectNode) also.lastElement(); 150 } 151 } catch (SearchQueryException e) { 152 log.error(e); 153 } 154 MMObjectNode wnode=null; 155 int sleeptime=-1; 156 if (snode!=null && enode==null) { 157 sleeptime=snode.getIntValue("start"); 158 wnode=snode; 159 } 160 if (snode==null && enode!=null) { 161 sleeptime=enode.getIntValue("stop"); 162 wnode=enode; 163 } 164 if (snode!=null && enode!=null) { 165 if (snode.getIntValue("start")<enode.getIntValue("stop")) { 166 sleeptime=snode.getIntValue("start"); 167 wnode=snode; 168 } else { 169 sleeptime=enode.getIntValue("stop"); 170 wnode=enode; 171 } 172 } 173 174 if (sleeptime!=-1) { 175 if (log.isDebugEnabled()) { 176 log.debug("SLEEPTIME="+(sleeptime-now)+" wnode="+wnode+" also="+also); 177 } 178 try { 179 Thread.sleep((sleeptime-now)*1000); 180 } catch (InterruptedException f) { 181 log.error("interrupted while sleeping"); 182 } 183 log.debug("Node local change "+wnode.getIntValue("number")); 184 super.nodeLocalChanged(mmb.getMachineName(),""+wnode.getIntValue("number"),tableName,"c"); 185 Enumeration g=also.elements(); 186 while (g.hasMoreElements()) { 187 wnode=(MMObjectNode)g.nextElement(); 188 if ((wnode.getIntValue("start")==sleeptime) || (wnode.getIntValue("stop")==sleeptime)) { 189 log.debug("Node local change "+wnode.getIntValue("number")); 190 super.nodeLocalChanged(mmb.getMachineName(),""+wnode.getIntValue("number"),tableName,"c"); 191 } 192 } 193 } else { 194 try { 195 Thread.sleep(300*1000); 196 } catch (InterruptedException f) { 197 log.error("interrupted while sleeping"); 198 } 199 } 200 } 201 202 public int insert(String owner,MMObjectNode node) { 203 int val=node.getIntValue("start"); 204 int newval=(int)(System.currentTimeMillis()/1000); 205 if (val==-1) { 206 node.setValue("start",newval); 207 208 } 209 val=node.getIntValue("stop"); 210 if (val==-1) { 211 node.setValue("stop",newval); 212 213 } 214 return(super.insert(owner,node)); 215 } 216 217 public boolean commit(MMObjectNode node) { 218 int val=node.getIntValue("start"); 219 int newval=(int)(System.currentTimeMillis()/1000); 220 if (val==-1) { 221 node.setValue("start",newval); 222 223 } 224 val=node.getIntValue("stop"); 225 if (val==-1) { 226 node.setValue("stop",newval); 227 228 } 229 return(super.commit(node)); 230 } 231 } 232 | Popular Tags |