KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > ad_background > PeriodicAlert


1 /*
2  ******************************************************************************
3  * The contents of this file are subject to the Compiere License Version 1.1
4  * ("License"); You may not use this file except in compliance with the License
5  * You may obtain a copy of the License at http://www.compiere.org/license.html
6  * Software distributed under the License is distributed on an "AS IS" basis,
7  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
8  * the specific language governing rights and limitations under the License.
9  * The Original Code is Compiere ERP & CRM Business Solution
10  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
11  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
12  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
13  * Contributor(s): Openbravo SL
14  * Contributions are Copyright (C) 2001-2006 Openbravo S.L.
15  ******************************************************************************
16 */

17 package org.openbravo.erpCommon.ad_background;
18
19 import org.openbravo.erpCommon.utility.SequenceIdData;
20 import org.openbravo.erpCommon.businessUtility.EMail;
21 import org.openbravo.erpCommon.utility.Utility;
22
23
24 public class PeriodicAlert implements BackgroundProcess {
25   public String JavaDoc batchSize="50";
26   static int counter = 0; // This variable might be used in order to define how often an alert is going to be executed.
27

28
29   private void processAlert(PeriodicAlertData alertRule, PeriodicBackground periodicBG) throws Exception JavaDoc{
30     periodicBG.addLog("processing rule "+alertRule.name);
31     PeriodicAlertData [] alert = null;
32     try {
33       alert = PeriodicAlertData.selectAlert(periodicBG.conn, alertRule.sql);
34     } catch (Exception JavaDoc ex) {
35       periodicBG.addLog("Error processing: "+ex.toString());
36       return;
37     }
38     //insert
39
if (alert!=null && alert.length!=0) {
40       int insertions = 0;
41       String JavaDoc msg = "";
42       for (int i=0; i<alert.length; i++){
43         if (PeriodicAlertData.existsReference(periodicBG.conn, alertRule.adAlertruleId, alert[i].referencekeyId).equals("0")) {
44           String JavaDoc adAlertId = SequenceIdData.getSequence(periodicBG.conn, "AD_Alert", alert[i].adClientId);
45           periodicBG.addLog("inserting alert "+adAlertId+" org:"+alert[i].adOrgId+" client:"+alert[i].adClientId+" reference key:"+alert[i].referencekeyId+" created"+alert[i].created);
46           PeriodicAlertData.InsertAlert(periodicBG.conn, adAlertId, alert[i].adClientId, alert[i].adOrgId,
47                                                          alert[i].created, alert[i].createdby,
48                                                          alertRule.adAlertruleId, alert[i].recordId, alert[i].referencekeyId,
49                                                          alert[i].description, alert[i].adUserId, alert[i].adRoleId);
50           
51          // periodicBG.addLog("alert inserted record:"+alert[i].record);
52
insertions ++;
53          msg += "\n\nAlert: "+alert[i].description+"\nRecord: "+alert[i].recordId;
54         }
55       }
56       periodicBG.addLog("inserted alerts "+insertions);
57       if (insertions>0) {
58        //sendmail
59
PeriodicAlertData[] mail =PeriodicAlertData.prepareMails(periodicBG.conn, alertRule.adAlertruleId);
60         if (mail!=null) {
61           for (int i=0; i<mail.length; i++) {
62             String JavaDoc head = Utility.messageBD(periodicBG.conn, "AlertMailHead", mail[i].adLanguage)+"\n";
63             EMail email = new EMail(null, mail[i].smtphost, mail[i].mailfrom, mail[i].mailto, "[OB Alert] "+alertRule.name, head+msg);
64             email.setEMailUser(mail[i].requestuser, mail[i].requestuserpw);
65             if("OK".equals(email.send())) periodicBG.addLog("mail sent ok");
66             else periodicBG.addLog("error sending mail");
67           }
68         }
69       }
70     }
71     
72     //update
73
try {
74       Integer JavaDoc count = PeriodicAlertData.updateAlert(periodicBG.conn, alertRule.adAlertruleId, alertRule.sql);
75       periodicBG.addLog("updated alerts: "+count);
76     } catch (Exception JavaDoc ex) {
77       periodicBG.addLog("Error updating: "+ex.toString());
78     }
79     
80   }
81
82   public void processPL(PeriodicBackground periodicBG, boolean directProcess) throws Exception JavaDoc {
83     String JavaDoc adNoteId = "";
84     counter ++;
85     periodicBG.addLog("Starting Alert Backgrouond Process. Loop "+counter);
86     
87     PeriodicAlertData [] alertRule = PeriodicAlertData.selectSQL(periodicBG.conn);
88     //periodicBG.addLog("alertRule.length: "+alertRule.length);
89
if (alertRule!=null && alertRule.length!=0) {
90       for (int i=0; i<alertRule.length; i++) {
91       // periodicBG.addLog("sql: "+alertRule[i].sql);
92
processAlert(alertRule[i], periodicBG);
93         
94         periodicBG.doPause();
95       }
96     }
97    /* if (periodicBG.vars==null || periodicBG.adClientId.equals("")) {
98       try {
99         PeriodicAcctServerData[] dataOrg = PeriodicAcctServerData.selectUserOrg(periodicBG.conn, periodicBG.adProcessId);
100         if (dataOrg==null || dataOrg.length==0) {
101           periodicBG.addLog("La carga de organizaciĆ³n y usuario a fallado.");
102           periodicBG.setProcessing(false);
103           return;
104         }
105         periodicBG.vars = new VariablesSecureApp(dataOrg[0].adUserId, periodicBG.adClientId,dataOrg[0].adOrgId);
106       } catch (ServletException ex) {
107         ex.printStackTrace();
108         return;
109       }
110     }
111
112     try {
113         PeriodicAcctServerData [] data = PeriodicAcctServerData.selectAcctTable(periodicBG.conn);
114         ArrayList<Object> vTableIds = new ArrayList<Object>();
115         for (int i=0;i<data.length;i++){
116          vTableIds.add(data[i].adTableId);
117         }
118         TableIds = new String [vTableIds.size()];
119         vTableIds.toArray(TableIds);
120     } catch (ServletException ex) {
121       ex.printStackTrace();
122       return;
123     }
124     adNoteId = periodicBG.saveLog(adNoteId, periodicBG.adClientId);
125     String[] tables=null;
126     String strTable = "";
127     if (directProcess) {
128       strTable = PeriodicAcctServerData.selectTable(periodicBG.conn, periodicBG.adPinstanceId);
129     }
130     if (!strTable.equals("")) {
131       tables = new String[1];
132       tables[0] = new String(strTable);
133     } else tables = TableIds;
134     for (int i=0;i<tables.length;i++){
135       periodicBG.doPause();
136       AcctServer acct = AcctServer.get(tables[i], periodicBG.adClientId);
137       acct.setBatchSize(batchSize);
138       int total = 0;
139       while (acct.checkDocuments()) {
140         periodicBG.doPause();
141         if (total==0) periodicBG.addLog("Contabilizando - " + acct.tableName, false);
142         else periodicBG.addLog("Contabilizados " + total + " - " + acct.tableName, false);
143         try {
144           acct.run(periodicBG.vars);
145           periodicBG.doPause();
146         } catch(IOException ex) {
147           ex.printStackTrace();
148           return;
149         } catch(InterruptedException e) {
150           return;
151         }
152         if (!periodicBG.canContinue(directProcess, periodicBG.adClientId)) {
153           periodicBG.addLog("Table=" + acct.tableName + " - " +acct.getInfo(periodicBG.vars));
154           adNoteId = periodicBG.saveLog(adNoteId, periodicBG.adClientId);
155           return;
156         }
157         total += Integer.valueOf(batchSize).intValue();
158       }
159       periodicBG.addLog("Table=" + acct.tableName + " - " + acct.getInfo(periodicBG.vars));
160       adNoteId = periodicBG.saveLog(adNoteId, periodicBG.adClientId);
161     }*/

162   }
163 }
164
Popular Tags