1 19 20 package org.openharmonise.rm.tasks; 21 22 import java.io.*; 23 import java.net.*; 24 import java.sql.*; 25 import java.util.*; 26 import java.util.logging.*; 27 28 import javax.xml.parsers.*; 29 import javax.xml.transform.*; 30 import javax.xml.transform.dom.DOMSource ; 31 import javax.xml.transform.stream.StreamResult ; 32 33 import org.openharmonise.commons.cache.CacheException; 34 import org.openharmonise.commons.dsi.*; 35 import org.openharmonise.commons.dsi.dml.*; 36 import org.openharmonise.commons.net.Email; 37 import org.openharmonise.commons.xml.XMLPrettyPrint; 38 import org.openharmonise.commons.xml.namespace.NamespaceClashException; 39 import org.openharmonise.rm.*; 40 import org.openharmonise.rm.config.ConfigSettings; 41 import org.openharmonise.rm.dsi.ColumnRefCache; 42 import org.openharmonise.rm.factory.*; 43 import org.openharmonise.rm.metadata.*; 44 import org.openharmonise.rm.publishing.*; 45 import org.openharmonise.rm.resources.AbstractProfiledObject; 46 import org.openharmonise.rm.resources.lifecycle.*; 47 import org.openharmonise.rm.resources.publishing.*; 48 import org.openharmonise.rm.resources.users.User; 49 import org.openharmonise.rm.resources.xml.XSLResource; 50 import org.openharmonise.rm.search.Search; 51 import org.openharmonise.rm.sessions.Session; 52 import org.w3c.dom.*; 53 54 55 56 65 public class Task extends AbstractProfiledObject implements Editable, 66 Publishable { 67 69 public static final String TASK_SEQUENCE = "seq_task"; 70 71 73 public static final String TBL_TASK = "task"; 74 75 public static final String CLMN_TASK_SECONDS = "seconds"; 76 public static final String CLMN_TASK_MINUTES = "minutes"; 77 public static final String CLMN_TASK_HOURS = "hours"; 78 public static final String CLMN_TASK_DAYOFWEEK = "dayofweek"; 79 public static final String CLMN_TASK_DAYOFMONTH = "dayofmonth"; 80 public static final String CLMN_TASK_SCRIPT = "page_id"; 81 public static final String CLMN_TASK_TARGET = "target"; 82 public static final String CLMN_TASK_TARGETTYPE = "targettype"; 83 public static final String CLMN_TASK_NEXTRUNTIME = "nextruntime"; 84 public static final String CLMN_TASK_LASTRUNTIME = "lastruntime"; 85 86 public static final String TAG_TASK = "Task" ; 88 public static final String ATTRIB_FORMID = "formId"; 89 public static final String TAG_SECONDS = "Seconds"; 90 public static final String TAG_MINUTES = "Minutes"; 91 public static final String TAG_HOURS = "Hours"; 92 public static final String TAG_DAYOFWEEK = "DayOfWeek"; 93 public static final String TAG_DAYOFMONTH = "DayOfMonth"; 94 public static final String TAG_TARGET = "Target"; 95 96 97 100 public static final String EMAIL = "E"; 101 102 105 public static final String URI = "U"; 106 107 110 public static final String FILE = "F"; 111 112 115 public static final String TASK = "T"; 116 117 120 public static final String NONE = "N"; 121 122 125 public static final String FROM_ADDRESS = "task@harmonise.com"; 126 127 130 private boolean m_bNew = true; 131 132 135 private int m_nSeconds = -1; 136 137 140 private int m_nMinutes = -1; 141 142 145 private int m_nHours = -1; 146 147 150 private int m_nDayOfWeek = -1; 151 152 155 private int m_nDayOfMonth = -1; 156 157 160 private WebPage m_wpScript = null; 161 162 165 private String m_sTarget = ""; 166 167 170 private String m_sTargetType = ""; 171 private java.util.Date m_dNextRuntime = null; 172 173 private static XMLPrettyPrint m_xmlPrinter = new XMLPrettyPrint(); 174 175 178 private static final Logger m_logger = Logger.getLogger(Task.class.getName()); 179 180 183 public Task() { 184 super(); 185 186 } 187 188 195 public Task(AbstractDataStoreInterface con) { 196 super(con); 197 } 198 199 206 public Task(AbstractDataStoreInterface con, int nTaskId) { 207 super(con,nTaskId); 208 } 209 210 218 public static List getPendingTasks(AbstractDataStoreInterface dbinterf) throws DataAccessException { 219 Vector Tasks = new Vector(16); 220 Task task = new Task(dbinterf); 221 222 ResultSet rs = null; 223 224 try { 225 SelectStatement select = new SelectStatement(); 226 227 select.addSelectColumn(task.getInstanceColumnRef(Task.CLMN_ID, false)); 228 229 java.util.Date dtNow = new java.util.Date (); 230 231 select.addWhereCondition(task.getInstanceColumnRef( 232 Task.CLMN_TASK_NEXTRUNTIME, false), "<", 233 dtNow); 234 235 236 rs = dbinterf.execute(select); 237 238 while (rs.next()) { 239 Tasks.add(new Task(dbinterf, rs.getInt(1))); 240 } 241 } catch (Exception e) { 242 throw new DataAccessException(e); 243 } finally { 244 if (rs != null) { 245 try { 246 rs.close(); 247 } catch (SQLException e) { 248 throw new DataAccessException(e); 249 } 250 } 251 } 252 253 return Tasks; 254 } 255 256 261 public void execute() throws TaskExecutionException { 262 Session sess = null; 263 User usrNext = null; 264 State state = null; 265 Element elState = null; 266 Element elSess = null; 267 268 if(m_logger.isLoggable(Level.FINE)) { 269 m_logger.logp(Level.FINE, this.getClass().getName(), "execute", "Executing task " + getId()); 270 } 271 272 try { 273 List users = searchForUsers(); 274 275 if ((users != null) && (users.size() > 0)) { 277 HarmoniseOutput output = new HarmoniseOutput(DocumentBuilderFactory.newInstance() 278 .newDocumentBuilder() 279 .newDocument(), 280 m_dsi); 281 elState = output.createElement(State.TAG_STATE); 282 output.appendChild(elState); 283 elSess = output.createElement(Session.TAG_SESSION); 284 elState.appendChild(elSess); 285 286 for (Iterator iter = users.iterator(); iter.hasNext();) { 287 usrNext = (User) iter.next(); 288 sess = new Session(m_dsi, usrNext, m_wpScript.getTimeout()); 290 291 elState.removeChild(elSess); 292 elSess = sess.publish((Element)null, output, null); 293 elState.appendChild(elSess); 294 state = new State((org.w3c.dom.Document ) output, m_dsi); 295 296 execute(state); 297 } 298 299 } else { 300 state = new State(DocumentBuilderFactory.newInstance() 302 .newDocumentBuilder() 303 .newDocument(), 304 m_dsi); 305 elState = state.createElement(State.TAG_STATE); 306 state.appendChild(elState); 307 308 execute(state); 309 } 310 } catch (Exception e) { 311 throw new TaskExecutionException(e); 312 } 313 } 314 315 321 public void execute(State state) throws TaskExecutionException { 322 WebPageEngine pageEngine = null; 323 String sSessionId = state.getSessionId(); 324 325 try { 326 if ((sSessionId == null) || (sSessionId.length() == 0)) { 327 pageEngine = new WebPageEngine(this.m_dsi); 329 } else { 330 pageEngine = WebPageEngineCache.getInstance(m_dsi) 332 .getWebPageEngine(sSessionId); 333 } 334 335 XSLResource xsl = m_wpScript.getXSL(); 336 337 org.w3c.dom.Document xmlDoc = pageEngine.createXML(m_wpScript, state); 339 340 if (m_sTargetType.equalsIgnoreCase(FILE)) { 342 File f = new File(m_sTarget); 343 344 FileWriter fwriter = new FileWriter(f); 345 346 PrintWriter out = new PrintWriter(fwriter); 347 348 if (xsl == null) { 349 printNode(out, xmlDoc.getDocumentElement()); 350 } else { 351 352 processXML(xmlDoc, xsl.getTemplates(), 353 out); 354 } 355 356 out.close(); 357 } else if (m_sTargetType.equalsIgnoreCase(EMAIL)) { 358 StringWriter swriter = new StringWriter(); 359 PrintWriter out = new PrintWriter(swriter); 360 361 if (xsl == null) { 362 printNode(out, xmlDoc.getDocumentElement()); 363 } else { 364 processXML(xmlDoc, xsl.getTemplates(), 365 out); 366 } 367 368 out.close(); 369 370 String sTarget = ""; 371 372 if ((m_sTarget == null) || (m_sTarget.trim().length() == 0)) { 373 Profile pro = pageEngine.getSession().getUser().getProfile(); 374 GeneralPropertyInstance prop = (GeneralPropertyInstance) pro.getPropertyInstance("EMAIL"); 375 376 if (prop != null) { 377 sTarget = (String ) prop.getValue(); 378 } 379 } else { 380 sTarget = m_sTarget; 381 } 382 383 String sMessage = swriter.toString(); 384 385 Email email = new Email(ConfigSettings.getProperty("EMAIL_HOST"),sTarget, FROM_ADDRESS, "Task results", 386 sMessage); 387 388 email.send(); 389 } else if (m_sTargetType.equalsIgnoreCase(URI)) { 390 StringWriter swriter = new StringWriter(); 391 PrintWriter out = new PrintWriter(swriter); 392 393 if (xsl == null) { 394 printNode(out, xmlDoc.getDocumentElement()); 395 } else { 396 processXML(xmlDoc, xsl.getTemplates(), 397 out); 398 } 399 400 out.close(); 401 402 String sContent = swriter.toString(); 403 404 HttpURLConnection httpCon = null; 405 406 try { 407 URL url = new URL(m_sTarget); 408 409 httpCon = (HttpURLConnection) url.openConnection(); 410 411 httpCon.setRequestMethod("POST"); 412 413 httpCon.setAllowUserInteraction(false); 414 httpCon.setDoOutput(true); 415 httpCon.setDoInput(true); 416 417 String query = "Page/@id=1"; 418 httpCon.setRequestProperty("Content-Type", 419 "application/x-www-form-urlencoded"); 420 httpCon.setRequestProperty("Content-Length", 421 Integer.toString(sContent.length())); 422 423 424 OutputStream POSTStream = httpCon.getOutputStream(); 426 PrintWriter POSTWriter = new PrintWriter(POSTStream); 427 POSTWriter.println(sContent); 428 POSTWriter.flush(); 429 POSTWriter.close(); 430 431 InputStreamReader in = new InputStreamReader( 432 httpCon.getInputStream()); 433 BufferedReader reader = new BufferedReader(in); 434 String inputLine; 435 436 File f = new File("c:\\harmonise\\test2.txt"); 438 439 FileWriter fwriter = new FileWriter(f); 440 441 while ((inputLine = reader.readLine()) != null) { 442 fwriter.write(inputLine); 443 } 444 445 fwriter.close(); 446 in.close(); 447 448 int responseCode = httpCon.getResponseCode(); 450 451 if (responseCode != HttpURLConnection.HTTP_OK) { 452 throw new ConnectException("Http error: " + 453 httpCon.getResponseMessage()); 454 } 455 456 } finally { 457 if (httpCon != null) { 458 httpCon.disconnect(); 459 } 460 } 461 } else if (m_sTargetType.equalsIgnoreCase(TASK)) { 462 Task taskTarget = TaskCache.getInstance().getTask(Integer.parseInt(m_sTarget)); 463 464 pipeResults(xmlDoc, taskTarget); 465 } 466 467 UpdateStatement update = new UpdateStatement(); 469 470 update.addColumnValue(this.getInstanceColumnRef( 471 CLMN_TASK_LASTRUNTIME, false), 472 new java.util.Date ()); 473 474 update.addWhereCondition(this.getInstanceColumnRef(CLMN_ID, false), 475 "=", m_nId); 476 477 m_dsi.execute(update); 478 } catch (Exception e) { 479 throw new TaskExecutionException(e); 480 } 481 } 482 483 488 public void updateNextRuntime() throws EditException { 489 try { 491 UpdateStatement update = new UpdateStatement(); 492 493 update.addColumnValue(this.getInstanceColumnRef( 494 CLMN_TASK_NEXTRUNTIME, false), 495 this.calcNextRunTime()); 496 497 update.addWhereCondition(this.getInstanceColumnRef(CLMN_ID, false), 498 "=", m_nId); 499 500 501 m_dsi.execute(update); 502 } catch (DataStoreException e) { 503 throw new EditException(e); 504 } catch (PopulateException e) { 505 throw new EditException(e); 506 } 507 } 508 509 517 public org.w3c.dom.Element publish(Template template, 518 HarmoniseOutput xmlDoc, 519 State state) 520 throws PublishException { 521 Element el = null; 522 try { 523 el = publish(template.getTemplateRootElement(), xmlDoc, state); 524 } catch (DataAccessException e) { 525 throw new PublishException(e); 526 } 527 528 return el; 529 } 530 531 539 public org.w3c.dom.Element publish(Element topEl, HarmoniseOutput xmlDoc, 540 State state) 541 throws PublishException { 542 543 Element docEl = xmlDoc.createElement(topEl.getTagName()); 544 545 if (topEl.getTagName().equals(TAG_TASK)) { 546 if (!m_bNew) { 547 docEl.setAttribute(ATTRIB_ID, Integer.toString(m_nId)); 548 } 549 550 String form_id = (String ) topEl.getAttribute(ATTRIB_FORMID); 551 552 if (form_id != null) { 553 docEl.setAttribute(ATTRIB_FORMID, form_id); 554 } 555 } 556 557 NodeList nodes = topEl.getChildNodes(); 558 Element formEl; 559 Element el; 560 Text txt; 561 String sTagName; 562 Profile pro = null; 563 564 for (int i = 0; i < nodes.getLength(); i++) { 565 if (nodes.item(i).getNodeType() != Node.ELEMENT_NODE) { 566 continue; 567 } 568 569 formEl = (Element) nodes.item(i); 570 sTagName = formEl.getTagName(); 571 572 if (sTagName.equals(TAG_TARGET)) { 573 el = xmlDoc.createElement(TAG_TARGET); 574 txt = xmlDoc.createTextNode(getTarget()); 575 el.appendChild(txt); 576 el.setAttribute(ATTRIB_TYPE, getTargetType()); 577 docEl.appendChild(el); 578 xmlDoc.copyChildren(el, formEl); 579 } else if (sTagName.equals(WebPage.TAG_PAGE)) { 580 el = xmlDoc.createElement(WebPage.TAG_PAGE); 581 582 el.setAttribute(ATTRIB_ID, String.valueOf(m_wpScript.getId())); 583 docEl.appendChild(el); 584 xmlDoc.copyChildren(el, formEl); 585 } else if (sTagName.equals(TAG_SECONDS)) { 586 el = xmlDoc.createElement(TAG_SECONDS); 587 txt = xmlDoc.createTextNode(String.valueOf(getSeconds())); 588 el.appendChild(txt); 589 docEl.appendChild(el); 590 xmlDoc.copyChildren(el, formEl); 591 } else if (sTagName.equals(TAG_MINUTES)) { 592 el = xmlDoc.createElement(TAG_MINUTES); 593 txt = xmlDoc.createTextNode(String.valueOf(getMinutes())); 594 el.appendChild(txt); 595 docEl.appendChild(el); 596 xmlDoc.copyChildren(el, formEl); 597 } else if (sTagName.equals(TAG_HOURS)) { 598 el = xmlDoc.createElement(TAG_HOURS); 599 txt = xmlDoc.createTextNode(String.valueOf(getHours())); 600 el.appendChild(txt); 601 docEl.appendChild(el); 602 xmlDoc.copyChildren(el, formEl); 603 } else if (sTagName.equals(TAG_DAYOFWEEK)) { 604 el = xmlDoc.createElement(TAG_DAYOFWEEK); 605 txt = xmlDoc.createTextNode(String.valueOf(getDayOfWeek())); 606 el.appendChild(txt); 607 docEl.appendChild(el); 608 xmlDoc.copyChildren(el, formEl); 609 } else if (sTagName.equals(TAG_DAYOFMONTH)) { 610 el = xmlDoc.createElement(TAG_DAYOFMONTH); 611 txt = xmlDoc.createTextNode(String.valueOf(getDayOfMonth())); 612 el.appendChild(txt); 613 docEl.appendChild(el); 614 xmlDoc.copyChildren(el, formEl); 615 } else { 616 el = (Element) xmlDoc.copyNode(formEl); 617 docEl.appendChild(el); 618 } 619 } 620 621 622 return docEl; 623 } 624 625 626 632 public void populate(Element xmlElement, State state) 633 throws PopulateException { 634 if (xmlElement.getTagName().equals(TAG_TASK) == false) { 635 throw new PopulateException("Expecting " + TAG_TASK + 636 " got " + 637 xmlElement.getTagName()); 638 } 639 640 if (!xmlElement.getAttribute(ATTRIB_ID).equals("")) { 641 m_bNew = false; 642 } 643 644 NodeList nodes = xmlElement.getChildNodes(); 646 Element formEl; 647 Text txt; 648 String sTagName; 649 650 for (int i = 0; i < nodes.getLength(); i++) { 651 formEl = (Element) nodes.item(i); 652 sTagName = formEl.getTagName(); 653 654 if (sTagName.equals(TAG_TARGET)) { 655 m_sTargetType = formEl.getAttribute(ATTRIB_TYPE); 656 657 if (!m_sTargetType.equalsIgnoreCase(EMAIL) && 658 !m_sTargetType.equalsIgnoreCase(URI) && 659 !m_sTargetType.equalsIgnoreCase(FILE)) { 660 throw new PopulateException( 661 "Invalid target type given"); 662 } 663 664 txt = (Text) formEl.getFirstChild(); 665 m_sTarget = txt.getNodeValue(); 666 } else if (sTagName.equals(TAG_SECONDS)) { 667 txt = (Text) formEl.getFirstChild(); 668 m_nSeconds = Integer.parseInt(txt.getNodeValue()); 669 } else if (sTagName.equals(TAG_MINUTES)) { 670 txt = (Text) formEl.getFirstChild(); 671 m_nMinutes = Integer.parseInt(txt.getNodeValue()); 672 } else if (sTagName.equals(TAG_HOURS)) { 673 txt = (Text) formEl.getFirstChild(); 674 m_nHours = Integer.parseInt(txt.getNodeValue()); 675 } else if (sTagName.equals(TAG_DAYOFWEEK)) { 676 txt = (Text) formEl.getFirstChild(); 677 m_nDayOfWeek = Integer.parseInt(txt.getNodeValue()); 678 } else if (sTagName.equals(TAG_DAYOFMONTH)) { 679 txt = (Text) formEl.getFirstChild(); 680 m_nDayOfMonth = Integer.parseInt(txt.getNodeValue()); 681 } else if (sTagName.equals(WebPage.TAG_PAGE)) { 682 String attr_id = formEl.getAttribute(ATTRIB_ID); 683 m_wpScript = new WebPage(m_dsi, Integer.parseInt(attr_id)); 684 } 685 } 686 } 687 688 695 public ColumnRef getInstanceColumnRef(String columnRef, boolean bIsHist) 696 throws DataStoreException { 697 ColumnRef colRef = null; 698 699 if (columnRef.equals(CLMN_TASK_SECONDS)) { 700 colRef = new ColumnRef(this.m_sTable, CLMN_TASK_SECONDS, 701 ColumnRef.NUMBER); 702 } else if (columnRef.equals(CLMN_TASK_MINUTES)) { 703 colRef = new ColumnRef(this.m_sTable, CLMN_TASK_MINUTES, 704 ColumnRef.NUMBER); 705 } else if (columnRef.equals(CLMN_TASK_HOURS)) { 706 colRef = new ColumnRef(this.m_sTable, CLMN_TASK_HOURS, 707 ColumnRef.NUMBER); 708 } else if (columnRef.equals(CLMN_TASK_DAYOFWEEK)) { 709 colRef = new ColumnRef(this.m_sTable, CLMN_TASK_DAYOFWEEK, 710 ColumnRef.NUMBER); 711 } else if (columnRef.equals(CLMN_TASK_DAYOFMONTH)) { 712 colRef = new ColumnRef(this.m_sTable, CLMN_TASK_DAYOFMONTH, 713 ColumnRef.NUMBER); 714 } else if (columnRef.equals(CLMN_TASK_SCRIPT)) { 715 colRef = new ColumnRef(this.m_sTable, CLMN_TASK_SCRIPT, 716 ColumnRef.NUMBER); 717 } else if (columnRef.equals(CLMN_TASK_TARGET)) { 718 colRef = new ColumnRef(this.m_sTable, CLMN_TASK_TARGET, 719 ColumnRef.TEXT); 720 } else if (columnRef.equals(CLMN_TASK_TARGETTYPE)) { 721 colRef = new ColumnRef(this.m_sTable, CLMN_TASK_TARGETTYPE, 722 ColumnRef.TEXT); 723 } else if (columnRef.equals(CLMN_TASK_NEXTRUNTIME)) { 724 colRef = new ColumnRef(this.m_sTable, CLMN_TASK_NEXTRUNTIME, 725 ColumnRef.DATE); 726 } else if (columnRef.equals(CLMN_TASK_LASTRUNTIME)) { 727 colRef = new ColumnRef(this.m_sTable, CLMN_TASK_LASTRUNTIME, 728 ColumnRef.DATE); 729 } else { 730 colRef = super.getInstanceColumnRef(columnRef, bIsHist); 731 } 732 733 return colRef; 734 } 735 736 744 public JoinConditions getInstanceJoinConditions(String sObjectTag, 745 boolean bIsOuter) 746 throws DataStoreException { 747 return null; 748 } 749 750 755 private int getSeconds() { 756 return m_nSeconds; 757 } 758 759 763 private int getMinutes() { 764 return m_nMinutes; 765 } 766 767 771 private int getHours() { 772 return m_nHours; 773 } 774 775 779 private int getDayOfWeek() { 780 return m_nDayOfWeek; 781 } 782 783 787 private int getDayOfMonth() { 788 return m_nSeconds; 789 } 790 791 795 private String getTarget() { 796 return m_sTarget; 797 } 798 799 803 private String getTargetType() { 804 return m_sTargetType; 805 } 806 807 812 private static void printNode(PrintWriter out, Node node) { 813 try { 814 out.print(m_xmlPrinter.printNode(node)); 815 } catch (NamespaceClashException e) { 816 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 817 } 818 } 819 820 825 public java.util.Date calcNextRunTime() throws PopulateException { 826 java.util.GregorianCalendar dtNow = new java.util.GregorianCalendar (); 827 828 if ((this.m_dNextRuntime == null) || 829 m_dNextRuntime.before(dtNow.getTime())) { 830 831 if(isPopulated() == false) { 832 833 populateFromDatabase(); 834 835 } 836 837 java.util.GregorianCalendar dtNext = new java.util.GregorianCalendar (); 838 839 841 if ((m_nDayOfWeek < 0) && (m_nDayOfMonth < 0)) { 843 dtNext.set(Calendar.DAY_OF_MONTH, dtNow.get(Calendar.DAY_OF_MONTH)); 844 845 if (isSetTimePassedToday()) { 846 dtNext.add(Calendar.DAY_OF_MONTH, 1); 847 } 848 } else { 849 if (m_nDayOfWeek >= 0) { 850 dtNext.set(Calendar.DAY_OF_WEEK, m_nDayOfWeek); 851 852 if ((dtNext.get(Calendar.DAY_OF_MONTH) < dtNow.get(Calendar.DAY_OF_MONTH)) || 854 (dtNext.get(Calendar.DAY_OF_MONTH) == dtNow.get(Calendar.DAY_OF_MONTH) && 855 isSetTimePassedToday())) { 856 dtNext.add(Calendar.DAY_OF_MONTH, 7); 857 } 858 } 859 860 if (m_nDayOfMonth >= 0) { 861 if ((m_nDayOfWeek < 0) || 862 ((m_nDayOfWeek >= 0) && 863 (m_nDayOfMonth < dtNext.get(Calendar.DAY_OF_MONTH)) && 864 m_nDayOfMonth > dtNow.get(Calendar.DAY_OF_MONTH))) { 865 dtNext.set(Calendar.DAY_OF_MONTH, m_nDayOfMonth); 866 867 if ((dtNext.get(Calendar.DAY_OF_MONTH) == dtNow.get(Calendar.DAY_OF_MONTH)) && 868 isSetTimePassedToday()) { 869 dtNext.add(Calendar.MONTH, 1); 870 } 871 } 872 } 873 874 if (dtNow.get(Calendar.DAY_OF_MONTH) > dtNext.get(Calendar.DAY_OF_MONTH)) { 875 dtNext.add(Calendar.MONTH, 1); 876 } 877 } 878 879 if (m_nHours >= 0) { 880 dtNext.set(Calendar.HOUR_OF_DAY, m_nHours); 881 } else { 882 if (dtNow.get(Calendar.DAY_OF_MONTH) == dtNext.get(Calendar.DAY_OF_MONTH)) { 883 dtNext.set(Calendar.HOUR_OF_DAY, dtNow.get(Calendar.HOUR_OF_DAY)); 884 885 if ((m_nMinutes >= 0) && 886 (m_nMinutes < dtNow.get(Calendar.MINUTE))) { 887 dtNext.add(Calendar.HOUR_OF_DAY, 1); 888 } else if ((m_nSeconds >= 0) && 889 (m_nSeconds < dtNow.get(Calendar.SECOND))) { 890 dtNext.add(Calendar.HOUR_OF_DAY, 1); 891 } 892 } else { 893 dtNext.set(Calendar.HOUR_OF_DAY, 0); 894 } 895 } 896 897 if (m_nMinutes >= 0) { 898 dtNext.set(Calendar.MINUTE, m_nMinutes); 899 } else { 900 if ((dtNow.get(Calendar.DAY_OF_MONTH) == dtNext.get(Calendar.DAY_OF_MONTH)) && 901 (dtNow.get(Calendar.HOUR_OF_DAY) == dtNext.get(Calendar.HOUR_OF_DAY)) && 902 (dtNow.get(Calendar.MINUTE) == dtNext.get(Calendar.MINUTE))) { 903 if ((m_nSeconds >= 0) && 904 (m_nSeconds < dtNow.get(Calendar.SECOND))) { 905 dtNext.add(Calendar.MINUTE, 1); 906 } 907 } else { 908 dtNext.set(Calendar.MINUTE, 0); 909 } 910 } 911 912 if (m_nSeconds >= 0) { 913 dtNext.set(Calendar.SECOND, m_nSeconds); 914 } else { 915 if ((dtNow.get(Calendar.DAY_OF_MONTH) == dtNext.get(Calendar.DAY_OF_MONTH)) && 916 (dtNow.get(Calendar.HOUR_OF_DAY) == dtNext.get(Calendar.HOUR_OF_DAY)) && 917 (dtNow.get(Calendar.MINUTE) == dtNext.get(Calendar.MINUTE))) { 918 dtNext.add(Calendar.SECOND, 1); 919 } else { 920 dtNext.set(Calendar.SECOND, 0); 921 } 922 } 923 924 m_dNextRuntime = dtNext.getTime(); 925 } 926 927 return m_dNextRuntime; 928 } 929 930 public boolean isPending() throws DataAccessException { 931 if(m_dNextRuntime == null && isPopulated() == false) { 932 try { 933 populateFromDatabase(); 934 } catch (PopulateException e) { 935 throw new DataAccessException(e); 936 } 937 } 938 java.util.GregorianCalendar dtNow = new java.util.GregorianCalendar (); 939 940 if(m_logger.isLoggable(Level.FINE) && m_dNextRuntime.before(dtNow.getTime())) { 941 m_logger.logp(Level.FINE, this.getClass().getName(),"isPending", "Task [" + this.getId() + "] is pending."); 942 } 943 944 return m_dNextRuntime.before(dtNow.getTime()); 945 } 946 947 952 private boolean isSetTimePassedToday() throws PopulateException { 953 if(isPopulated() == false) { 954 populateFromDatabase(); 955 } 956 957 java.util.GregorianCalendar dtNow = new java.util.GregorianCalendar (); 958 int nTotSecs = 0; 959 int nTimeInSecs = (dtNow.get(Calendar.HOUR_OF_DAY) * 3600) + 960 (dtNow.get(Calendar.MINUTE) * 60) + 961 dtNow.get(Calendar.SECOND); 962 int nSecsInDay = 24 * 3600; 963 boolean bReturn = false; 964 965 if (m_nHours >= 0) { 966 nTotSecs = m_nHours * 3600; 967 968 if (m_nMinutes >= 0) { 969 nTotSecs = nTotSecs + (m_nMinutes * 60); 970 } else { 971 nTotSecs = nTotSecs + 3600; 972 } 973 974 if (m_nSeconds >= 0) { 975 nTotSecs = nTotSecs + m_nSeconds; 976 } else { 977 nTotSecs = nTotSecs + 60; 978 } 979 980 if (nTimeInSecs > nTotSecs) { 981 bReturn = true; 982 } 983 } else { 984 if (m_nMinutes >= 0) { 985 int nAmount2Sub = (60 - m_nMinutes) * 60; 986 987 if (m_nSeconds >= 0) { 988 nAmount2Sub = nAmount2Sub - (60 - m_nSeconds); 989 } else { 990 nAmount2Sub = nAmount2Sub - 60; 991 } 992 993 if (nTimeInSecs > (nSecsInDay - nAmount2Sub)) { 994 bReturn = true; 995 } 996 } else { 997 if (m_nSeconds >= 0) { 998 if (nTimeInSecs > (nSecsInDay - (60 - m_nSeconds))) { 999 bReturn = true; 1000 } 1001 } 1002 } 1003 } 1004 1005 return bReturn; 1006 } 1007 1008 1013 protected void pipeResults(org.w3c.dom.Document xmlDoc, Task taskTarget) throws TaskExecutionException 1014 { 1015 NodeList nodesLinks = xmlDoc.getElementsByTagName(TAG_LINK); 1016 Element elLink = null; 1017 State state = null; 1018 1019 for (int i = 0; i < nodesLinks.getLength(); i++) { 1020 elLink = (Element) nodesLinks.item(i); 1021 1022 try { 1023 state = buildState(elLink); 1024 } catch (StateException e) { 1025 throw new TaskExecutionException(e); 1026 } catch (ParserConfigurationException e) { 1027 throw new TaskExecutionException(e); 1028 } catch (FactoryConfigurationError e) { 1029 throw new TaskExecutionException(e); 1030 } 1031 taskTarget.execute(state); 1032 } 1033 } 1034 1035 1041 protected State buildState(Element elLink) throws StateException, ParserConfigurationException, FactoryConfigurationError { 1042 State state = new State(DocumentBuilderFactory.newInstance() 1043 .newDocumentBuilder() 1044 .newDocument(), 1045 m_dsi); 1046 state.appendChild(state.createElement(State.TAG_STATE)); 1047 1048 Element elObject = (Element) elLink.getParentNode(); 1050 Element elStateObject = state.createElement(elObject.getTagName()); 1051 NamedNodeMap attrs = elObject.getAttributes(); 1052 1053 for (int i = 0; i < attrs.getLength(); i++) { 1054 elStateObject.setAttribute(((Attr) attrs.item(i)).getName(), 1055 ((Attr) attrs.item(i)).getValue()); 1056 } 1057 1058 state.getDocumentElement().appendChild(elStateObject); 1059 1060 1061 addToState(elLink, state); 1063 1064 return state; 1065 } 1066 1067 1073 protected void addToState(Element elOutput, State state) { 1074 NodeList nodes = elOutput.getChildNodes(); 1075 Element elNext = null; 1076 1077 for (int i = 0; i < nodes.getLength(); i++) { 1078 if (nodes.item(i).getNodeType() != Node.ELEMENT_NODE) { 1079 continue; 1080 } 1081 1082 elNext = (Element) nodes.item(0); 1083 1084 if (elNext.getTagName().equals(State.TAG_STATE)) { 1085 state.copyChildren(state.getDocumentElement(), elNext); 1086 } 1087 } 1088 1089 if (elOutput.getTagName().equals(WebPage.TAG_HARMONISE_OBJECT) == false) { 1090 Element elParent = (Element) elOutput.getParentNode(); 1091 1092 addToState(elParent, state); 1093 } 1094 } 1095 1096 1101 protected List searchForUsers() throws DataAccessException { 1102 List users = null; 1103 Profile pro = getProfile(); 1104 1105 if (pro != null) { 1106 Search search = new Search(m_dsi); 1107 1108 search.addConditionProfile(pro); 1109 try { 1110 search.setSearchType(new User(m_dsi)); 1111 users = search.executeSearch(); 1112 } catch (Exception e) { 1113 throw new DataAccessException(e); 1114 } 1115 } 1116 1117 return users; 1118 } 1119 1120 protected void processXML(org.w3c.dom.Document xmlDoc, Templates xslStyle, 1121 PrintWriter outPut) throws java.lang.Exception , 1122 org.xml.sax.SAXException { 1123 Transformer trans = xslStyle.newTransformer(); 1124 1125 DOMSource ds = new DOMSource (xmlDoc.getDocumentElement()); 1126 1127 StreamResult res = new StreamResult (outPut); 1128 1129 trans.transform(ds, res); 1130 } 1131 1132 1135 protected void saveNonCoreData() throws EditException { 1136 } 1138 1139 1142 public String getDBTableName() { 1143 return TBL_TASK; 1144 } 1145 1146 1149 public String getTagName() { 1150 return TAG_TASK; 1151 } 1152 1153 1156 protected void addColumnsToPopulateQuery( 1157 SelectStatement select, 1158 boolean bIsHist) 1159 throws DataStoreException { 1160 1161 try { 1162 ColumnRefCache cache = ColumnRefCache.getInstance(); 1163 1164 select.addSelectColumn(cache.getColumnRef(this,CLMN_TASK_SECONDS, bIsHist)); 1165 select.addSelectColumn(cache.getColumnRef(this,CLMN_TASK_MINUTES, bIsHist)); 1166 select.addSelectColumn(cache.getColumnRef(this,CLMN_TASK_HOURS, bIsHist)); 1167 select.addSelectColumn(cache.getColumnRef(this,CLMN_TASK_DAYOFMONTH, bIsHist)); 1168 select.addSelectColumn(cache.getColumnRef(this,CLMN_TASK_DAYOFWEEK, bIsHist)); 1169 select.addSelectColumn(cache.getColumnRef(this,CLMN_TASK_TARGET, bIsHist)); 1170 select.addSelectColumn(cache.getColumnRef(this,CLMN_TASK_TARGETTYPE, bIsHist)); 1171 select.addSelectColumn(cache.getColumnRef(this,CLMN_TASK_SCRIPT, bIsHist)); 1172 select.addSelectColumn(cache.getColumnRef(this,CLMN_TASK_NEXTRUNTIME, bIsHist)); 1173 1174 } catch (CacheException e) { 1175 throw new DataStoreException(e); 1176 } 1177 1178 super.addColumnsToPopulateQuery(select, bIsHist); 1179 } 1180 1181 1184 protected void addDataToSave(InsertStatement insert) 1185 throws DataStoreException { 1186 1187 try { 1188 ColumnRefCache cache = ColumnRefCache.getInstance(); 1189 boolean bIsHist = isHistorical(); 1190 insert.addColumnValue(cache.getColumnRef(this,CLMN_TASK_SECONDS, bIsHist), 1191 m_nSeconds); 1192 insert.addColumnValue(cache.getColumnRef(this,CLMN_TASK_MINUTES, bIsHist), 1193 m_nMinutes); 1194 insert.addColumnValue(cache.getColumnRef(this,CLMN_TASK_HOURS, bIsHist), 1195 m_nHours); 1196 insert.addColumnValue(cache.getColumnRef(this,CLMN_TASK_DAYOFWEEK, bIsHist), m_nDayOfWeek); 1197 insert.addColumnValue(cache.getColumnRef(this,CLMN_TASK_DAYOFMONTH, bIsHist), m_nDayOfMonth); 1198 insert.addColumnValue(cache.getColumnRef(this,CLMN_TASK_TARGET, bIsHist), 1199 m_sTarget); 1200 insert.addColumnValue(cache.getColumnRef(this,CLMN_TASK_TARGETTYPE, bIsHist), m_sTargetType); 1201 1202 insert.addColumnValue(cache.getColumnRef(this,CLMN_TASK_NEXTRUNTIME, bIsHist), 1203 this.calcNextRunTime()); 1204 insert.addColumnValue(cache.getColumnRef(this,CLMN_TASK_SCRIPT, bIsHist), 1205 m_wpScript.getId()); 1206 1207 } catch (CacheException e) { 1208 throw new DataStoreException(e); 1209 } catch (PopulateException e) { 1210 throw new DataStoreException(e); 1211 } 1212 1213 super.addDataToSave(insert); 1214 } 1215 1216 1219 protected void populateFromResultSetRow( 1220 ResultSet rs, 1221 SelectStatement select) 1222 throws PopulateException { 1223 if (isPopulated() == false) { 1224 1225 String sTemp = null; 1226 int nTemp = -1; 1227 java.util.Date dTemp = null; 1228 ColumnRef colref = null; 1229 1230 try { 1231 ColumnRefCache cache = ColumnRefCache.getInstance(); 1232 1233 boolean bIsHist = isHistorical(); 1234 1235 ColumnRef dispNameCol = cache.getColumnRef(this,CLMN_DISPLAY_NAME,bIsHist); 1236 1237 if (select.containsSelectColumn(dispNameCol) == true) { 1238 1239 sTemp = rs.getString(select.getResultSetIndex(dispNameCol)); 1240 1241 if ((sTemp != null) && (sTemp.length() > 0)) { 1242 if ((m_sDisplayName == null) || (m_sDisplayName.length() == 0)) { 1243 m_sDisplayName = sTemp; 1244 } else if (m_sDisplayName.equals(sTemp) == false) { 1245 m_bIsChanged = true; 1246 } 1247 } 1248 } 1249 1250 colref = cache.getColumnRef(this,CLMN_TASK_SECONDS, bIsHist); 1251 1252 if(select.containsSelectColumn(colref)) { 1253 1254 nTemp = rs.getInt(select.getResultSetIndex(colref)); 1255 1256 if (m_nSeconds < 0) { 1257 m_nSeconds = nTemp; 1258 } else if (m_nSeconds != nTemp) { 1259 setIsChanged(true); 1260 } 1261 } 1262 1263 colref = cache.getColumnRef(this,CLMN_TASK_MINUTES, bIsHist); 1264 1265 if(select.containsSelectColumn(colref)) { 1266 1267 nTemp = rs.getInt(select.getResultSetIndex(colref)); 1268 1269 if (m_nMinutes < 0) { 1270 m_nMinutes = nTemp; 1271 } else if (m_nMinutes != nTemp) { 1272 setIsChanged(true); 1273 } 1274 } 1275 1276 colref = cache.getColumnRef(this,CLMN_TASK_HOURS, bIsHist); 1277 1278 if(select.containsSelectColumn(colref)) { 1279 1280 nTemp = rs.getInt(select.getResultSetIndex(colref)); 1281 1282 if (m_nHours < 0) { 1283 m_nHours = nTemp; 1284 } else if (m_nHours != nTemp) { 1285 setIsChanged(true); 1286 } 1287 } 1288 1289 colref = cache.getColumnRef(this,CLMN_TASK_DAYOFMONTH, bIsHist); 1290 1291 if(select.containsSelectColumn(colref)) { 1292 1293 nTemp = rs.getInt(select.getResultSetIndex(colref)); 1294 1295 if (m_nDayOfMonth < 0) { 1296 m_nDayOfMonth = nTemp; 1297 } else if (m_nDayOfMonth != nTemp) { 1298 setIsChanged(true); 1299 } 1300 } 1301 1302 colref = cache.getColumnRef(this,CLMN_TASK_DAYOFWEEK, bIsHist); 1303 1304 if(select.containsSelectColumn(colref)) { 1305 1306 nTemp = rs.getInt(select.getResultSetIndex(colref)); 1307 1308 if (m_nDayOfWeek < 0) { 1309 m_nDayOfWeek = nTemp; 1310 } else if (m_nDayOfWeek != nTemp) { 1311 setIsChanged(true); 1312 } 1313 } 1314 1315 colref = cache.getColumnRef(this,CLMN_TASK_TARGET, bIsHist); 1316 1317 if(select.containsSelectColumn(colref)) { 1318 1319 sTemp = rs.getString(select.getResultSetIndex(colref)); 1320 1321 if (m_sTarget == null) { 1322 m_sTarget = sTemp; 1323 } else if (m_sTarget.equals(sTemp) == false) { 1324 setIsChanged(true); 1325 } 1326 } 1327 1328 colref = cache.getColumnRef(this,CLMN_TASK_TARGETTYPE, bIsHist); 1329 1330 if(select.containsSelectColumn(colref)) { 1331 1332 sTemp = rs.getString(select.getResultSetIndex(colref)); 1333 1334 if (m_sTargetType == null) { 1335 m_sTargetType = sTemp; 1336 } else if (m_sTargetType.equals(sTemp) == false) { 1337 setIsChanged(true); 1338 } 1339 } 1340 1341 colref = cache.getColumnRef(this,CLMN_TASK_SCRIPT, bIsHist); 1342 1343 if(select.containsSelectColumn(colref)) { 1344 1345 nTemp = rs.getInt(select.getResultSetIndex(colref)); 1346 1347 if (m_wpScript == null) { 1348 m_wpScript = (WebPage) HarmoniseObjectFactory.instantiateHarmoniseObject(m_dsi, WebPage.class.getName(), nTemp); 1349 } else if (m_wpScript.getId() != nTemp) { 1350 setIsChanged(true); 1351 } 1352 } 1353 1354 colref = cache.getColumnRef(this,CLMN_TASK_NEXTRUNTIME, bIsHist); 1355 1356 if(select.containsSelectColumn(colref)) { 1357 1358 dTemp = rs.getTimestamp(select.getResultSetIndex(colref)); 1359 1360 if (dTemp != null) { 1361 m_dNextRuntime = dTemp; 1362 } 1363 } 1364 1365 } catch (CacheException e) { 1366 throw new PopulateException(e); 1367 } catch (SQLException e) { 1368 throw new PopulateException(e); 1369 } catch (HarmoniseFactoryException e) { 1370 throw new PopulateException(e); 1371 } 1372 1373 super.populateFromResultSetRow(rs, select); 1374 } 1375 } 1376 1377} | Popular Tags |