fefad844a2a0283c07e8d9781368a8e1e3f637d4
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * eCOMP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.openecomp.portalsdk.analytics.scheduler;
21
22 import java.math.BigDecimal;
23 import java.sql.Connection;
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26 import java.sql.Statement;
27 import java.text.SimpleDateFormat;
28 import java.util.Calendar;
29 import java.util.Date;
30 import java.util.UUID;
31
32 import org.openecomp.portalsdk.analytics.error.RaptorException;
33 import org.openecomp.portalsdk.analytics.error.ReportSQLException;
34 import org.openecomp.portalsdk.analytics.scheduler.SchedulerUtil.Executor;
35 import org.openecomp.portalsdk.analytics.system.Globals;
36 import org.openecomp.portalsdk.analytics.util.Log;
37
38 public class SendNotifications {
39         
40         SchedulerUtil schedulerUtil;
41         public SendEmail sendEmail;
42         
43         public SendNotifications() throws Exception {
44                 schedulerUtil = new SchedulerUtil();
45                 sendEmail = new SendEmail();
46                 sendEmail.setSchedulerUtil(schedulerUtil);
47                 init();
48         }
49
50                 
51         public  void deInit() throws SQLException {
52                 schedulerUtil.closeConnection();
53         }
54         
55         public  void init() throws SQLException, ReportSQLException {
56                 schedulerUtil.init();
57         }
58         
59         public void send_notification(String p_mail_server, String p_sender, String p_system_name, String p_system_url, int connectionTimeout) throws RaptorException, Exception {
60
61                 System.out.println(p_mail_server + " " + p_sender + " " + p_system_name + " " + p_system_url);
62                 int p_time_interval = Globals.getSchedulerInterval();
63                 int v_num_recs = 0;
64                 String v_gen_key;
65                 BigDecimal v_id = null;
66                 String v_url;
67                 String v_r_action = "report.download.pdf";
68                 String v_email_msg;
69                 String v_formfields;
70                 // String error_m;
71                 // int transfer_timeout_limit = 1800;
72                 // boolean v_attach_email_yn = true;
73                 int v_schedule_id;
74                 Date v_touch_date;
75                 // Exception for_rec;
76
77                 Connection conn = schedulerUtil.getConnection();
78                 Statement stat = conn.createStatement();
79                 
80                 String CNotificationsql = 
81                                 /*
82                                 "SELECT x.rep_id, x.schedule_id, x.conditional_yn, x.condition_large_sql, x.notify_type, x.max_row, x.initial_formfields, x.processed_formfields, r.title, x.user_id "
83                                 + "FROM ("
84                                         + "SELECT rs.rep_id, rs.schedule_id, rs.sched_user_id user_id, rs.conditional_yn, rs.condition_large_sql, "
85                                         + "rs.notify_type, rs.max_row, rs.initial_formfields, rs.processed_formfields "
86                                         + "FROM cr_report_schedule rs "
87                                         + "WHERE rs.enabled_yn='Y' "
88                                         + "AND rs.start_"
89                                         + "date <= sysdate "
90                                         + "AND  (rs.end_date >= sysdate or rs.end_date is null ) "
91                                         + "AND rs.run_date IS NOT NULL "
92                                         + ") x, cr_report r "
93                                 + "WHERE x.rep_id = r.rep_id ";
94                                  */
95                 
96                 Globals.getAvailableSchedules().replace("[currentDate]", Globals.getCurrentDateString());
97                                 
98                 
99                 ResultSet rs = stat.executeQuery(CNotificationsql);
100
101                 while (rs.next()) {
102                         
103                         v_schedule_id = rs.getInt("schedule_id");
104                         int offset = get_report_sched_offset(rs.getInt("rep_id"), v_schedule_id);
105                         
106                         if(offset >= p_time_interval) continue;
107
108                         
109                         v_touch_date = (Date) schedulerUtil.getSingleResult("select touch_date from cr_report_email_sent_log where schedule_id = " + v_schedule_id + " and log_id = (select max(log_id) from cr_report_email_sent_log where schedule_id = " + v_schedule_id + ")", "touch_date");
110                         if (v_touch_date != null) {
111                                 if (Math.abs(System.currentTimeMillis() - v_touch_date.getTime()) /1000 < (p_time_interval - 1)) {
112                                         return;
113                                 }
114                         }
115
116                         if ("Y".equals(rs.getString("conditional_yn"))) {
117
118                                 v_num_recs = (Integer) schedulerUtil.getSingleResult("select count(*) count from (" + rs.getString("condition_large_sql") + " )", "count");
119                         }
120
121                         if (v_num_recs > 0 || "N".equals(rs.getString("conditional_yn"))) {
122
123                                 v_gen_key = ("Z" + UUID.randomUUID()).toString().substring(0,24); // 25 character string
124                                 Object sequenceId = schedulerUtil.getSingleResult(Globals.getSequenceNextVal().replace("[sequenceName]", "seq_email_sent_log_id"), "id");
125                                 
126                                 if(sequenceId instanceof Long)
127                                         v_id = new BigDecimal((Long)sequenceId);
128                                 else if(sequenceId instanceof BigDecimal)
129                                         v_id = (BigDecimal)sequenceId;
130                                 
131                                 schedulerUtil.insertOrUpdate("insert into cr_report_email_sent_log (log_id, gen_key, schedule_id, rep_id, user_id, touch_date) values (" + v_id + ",'" + v_gen_key + "'," + rs.getInt("schedule_id") + "," + rs.getInt("rep_id") + "," + rs.getInt("user_id") + ", " + Globals.getCurrentDateString() + " )");
132
133                                 int notify_type = rs.getInt("notify_type");
134                                 if (notify_type == 4)
135                                         v_r_action = "report.download";
136                                 else if (notify_type == 2)
137                                         v_r_action = "report.download.pdf";
138                                 else if (notify_type == 3)
139                                         v_r_action = "report.csv.download";
140                                 else if (notify_type == 5)
141                                         v_r_action = "report.download.excel2007";
142                                 else if (notify_type == 6)
143                                         v_r_action = "download.all";
144
145                                 if (rs.getObject("processed_formfields") != null)
146                                         v_formfields = modify_formfields(v_schedule_id, rs.getString("processed_formFields"));
147                                 else
148                                         v_formfields = strip_formfields(v_schedule_id, rs.getString("initial_formfields"));
149
150                                 v_url = p_system_url + "&r_action=" + v_r_action + "&log_id=" + v_id + "&user_id=" + rs.getString("user_id") + "&pdfAttachmentKey=" + v_gen_key + "&download_limit=" + rs.getInt("max_row") + v_formfields;
151
152                                 boolean v_attach_email_yn = shouldSendAttachmentInEmail(v_schedule_id);
153
154                                 v_email_msg = "<html><body><p><b><u><i>" + p_system_name + " System Notification</i></u></b></p>" + "<p>Report <b>" + rs.getString("title") + "</b> is available for viewing.</p><p>You can view the report if it is attached. </br>"
155                                                 + "If it is not attached, or you have problem to open it, you can log into Business Direct and run the report.</p>" + "</body></html>";
156
157                                 if (rs.getInt("notify_type") != 6) {
158
159                                         sendEmail.sendEmail(p_mail_server, p_sender, p_system_name + " System Notification: Report " + rs.getString("title") + " generated", v_email_msg, v_url, rs.getInt("notify_type"), v_schedule_id, p_time_interval, v_attach_email_yn,connectionTimeout);
160
161                                 } else {
162
163                                         // may not necessary
164                                         schedulerUtil.insertOrUpdate("insert into cr_schedule_activity_log (SCHEDULE_ID, url, notes, run_time) values  (" + v_schedule_id + ",'" + v_url + "'," + "'Success: http request began.', " + Globals.getCurrentDateString() + " )");
165                                         schedulerUtil.insertOrUpdate("insert into cr_schedule_activity_log (SCHEDULE_ID, url, notes, run_time) values  (" + v_schedule_id + ",'" + v_url + "'," + "'Success: http response recieved. Code  resp.status_code '' desc '' resp.reason_phrase', " + Globals.getCurrentDateString() + " )");
166
167                                 }
168                                 
169                                 schedulerUtil.insertOrUpdate("update cr_report_schedule set run_date = " + Globals.getCurrentDateString() +" where schedule_id=" + v_schedule_id);
170                                 
171                                 schedulerUtil.insertOrUpdate("update cr_report_email_sent_log set sent_date= " + Globals.getCurrentDateString() +" , access_flag='N' where log_id=" + v_id);
172                                 schedulerUtil.insertOrUpdate("insert into cr_schedule_activity_log (SCHEDULE_ID, url, notes, run_time) values (" + v_schedule_id + ",'" + v_url + "','Success: Email Sent', " + Globals.getCurrentDateString() + " )");
173
174                         }
175
176                 }
177
178                    if(rs!=null) 
179                                 rs.close();
180                    if(stat!=null)
181                         stat.close();
182                 //conn.close();
183
184         }
185
186         private boolean shouldSendAttachmentInEmail(int v_schedule_id) throws SQLException, ReportSQLException {
187
188                 String l_boolean = (String) schedulerUtil.getSingleResult("SELECT ATTACHMENT_YN from cr_report_schedule where schedule_id  = " + v_schedule_id, "ATTACHMENT_YN");
189                 if ("Y".equals(l_boolean))
190                         return true;
191                 return false;
192         }
193
194         private String strip_formfields(int v_schedule_id, String p_formfields) throws SQLException, ReportSQLException {
195
196                 String v_formfields_insert = "";
197                 String v_formfields_generate = "";
198                 String v_name = "";
199                 String v_value = "";
200                 
201                 
202                 String[] column_values = schedulerUtil.cr_dissecturl(p_formfields, "&");
203                 
204                 for(String column_value : column_values){
205                         if(column_value == null || column_value.isEmpty())
206                                 continue;
207                         
208                         v_name = column_value.substring(0, column_value.indexOf('='));
209                         v_formfields_insert += column_value + "&";
210                         v_value = column_value.substring(column_value.indexOf('=') + 1);
211                         if (column_value.indexOf("_auto") > 0) {
212                                 v_formfields_generate = v_formfields_generate + v_name.substring(0, v_name.indexOf("_auto")) + "=" + v_value + "&";
213                         } else {
214                                 v_formfields_generate = v_formfields_generate + column_value + "&";
215                         }
216                 }
217                 
218                 schedulerUtil.insertOrUpdate("update CR_REPORT_SCHEDULE set processed_formfields ='" + v_formfields_insert + "' where schedule_id = " + v_schedule_id);
219
220                 return v_formfields_generate.substring(0, v_formfields_generate.length());
221
222         }
223
224         private String modify_formfields(int v_schedule_id, String p_formfields) throws SQLException, ReportSQLException {
225
226                 class Result {
227
228                         String v_formfields_insert = "";
229                         String v_formfields_generate = "";
230                         String v_name = "";
231                         String v_value = "";
232                         Date v_date;
233                         String v_hour = "";
234                         String v_hour_value = "";
235                 }
236
237                 final Result result = new Result();
238
239                 final String v_recurrence = (String) schedulerUtil.getSingleResult("select recurrence from cr_report_schedule where schedule_id = " + v_schedule_id, "recurrence");
240
241                 String[] column_values = schedulerUtil.cr_dissecturl(p_formfields, "&");
242                 
243                 for(String column_value : column_values){
244                         if (column_value == null)
245                                 column_value = "";
246
247                         if ("MONTHLY".equals(v_recurrence)) {
248                                 if (column_value.indexOf("_auto") > 0) {
249                                         result.v_name = column_value.substring(0, column_value.indexOf('='));
250                                         result.v_date = schedulerUtil.to_date(column_value.substring(column_value.indexOf('=') + 1), "mm/dd/yyyy");
251                                         result.v_value = schedulerUtil.add_months(result.v_date, 1).toString();
252
253                                         if (result.v_name.length() > 0) {
254                                                 result.v_formfields_insert = result.v_formfields_insert + result.v_name + "=" + result.v_value + "&";
255                                                 result.v_formfields_generate = result.v_formfields_generate + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "=" + result.v_value + "&";
256                                         }
257                                 } else {
258                                         result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
259                                         result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
260                                 }
261
262                         } else if ("DAILY".equals(v_recurrence)) {
263                                 if (column_value.indexOf("_auto") > 0) {
264                                         result.v_name = column_value.substring(0, column_value.indexOf('='));
265                                         result.v_date = schedulerUtil.to_date(column_value.substring(column_value.indexOf('=') + 1), "mm/dd/yyyy");
266                                         result.v_value = schedulerUtil.add_months(result.v_date, 1).toString();
267
268                                         if (result.v_name.length() > 0) {
269                                                 result.v_formfields_insert = result.v_formfields_insert + result.v_name + "=" + result.v_value + "&";
270                                                 result.v_formfields_generate = result.v_formfields_generate + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "=" + result.v_value + "&";
271                                         }
272                                 } else {
273                                         result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
274                                         result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
275                                 }
276
277                         } else if ("DAILY_MO_FR".equals(v_recurrence)) {
278                                 if (column_value.indexOf("_auto") > 0) {
279                                         result.v_name = column_value.substring(0, column_value.indexOf('='));
280                                         result.v_date = schedulerUtil.to_date(column_value.substring(column_value.indexOf('=') + 1), "mm/dd/yyyy");
281                                         SimpleDateFormat sdf = new SimpleDateFormat("EEE");
282                                         sdf.format(result.v_date);
283                                         if ("FRI".equals(result.v_date.toString())) {
284                                                 result.v_date = schedulerUtil.add_days(result.v_date, 3);
285                                         } else if ("SAT".equals(result.v_date.toString())) {
286                                                 result.v_date = schedulerUtil.add_days(result.v_date, 2);
287                                         } else {
288                                                 result.v_date = schedulerUtil.add_days(result.v_date, 1);
289                                         }
290                                         result.v_value = result.v_date.toString();
291                                         if (result.v_name.length() > 0) {
292                                                 result.v_formfields_insert = result.v_formfields_insert + result.v_name + "=" + result.v_value + "&";
293                                                 result.v_formfields_generate = result.v_formfields_generate + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "=" + result.v_value + "&";
294                                         }
295
296                                 } else {
297                                         result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
298                                         result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
299                                 }
300
301                         } else if ("HOURLY".equals(v_recurrence)) {
302                                 
303                                 result.v_name = column_value.indexOf('=')>0?column_value.substring(0, column_value.indexOf('=')) : "";
304                                 if (column_value.indexOf("_auto") > 0) {
305                                         
306                                         
307                                         String[] column_values2 = schedulerUtil.cr_dissecturl(p_formfields, "&");
308                                         
309                                         for(String column_value2 : column_values2){
310                                                 
311                                                 String key = column_value2.substring(0, column_value2.indexOf("="));
312                                                 if(key.equals(result.v_name.substring(0, result.v_name.indexOf("_auto")))  || key.equals(result.v_name.substring(0, result.v_name.indexOf("_Hr")))){
313                                                         result.v_hour = column_value2;
314                                                 }
315                                         }
316                                         
317 //                                      schedulerUtil.getAndExecute("select c.column_value from table(CR_DISSECTURL(p_formfields)) c where substr(c.column_value, 1, instr(c.column_value, '=')-1) = substr(" + result.v_name + ",1,instr(" + result.v_name + ",'_auto')-1)||'_Hr'", new Executor() {
318 //
319 //                                              @Override
320 //                                              public void execute(ResultSet rs) throws SQLException {
321 //
322 //                                                      result.v_hour = rs.getString("column_value");
323 //                                              }
324 //
325 //                                      });
326
327                                         if (result.v_hour.length() > 0) {
328                                                 result.v_hour_value = result.v_hour.substring(result.v_hour.indexOf('=') + 1);
329                                         }
330
331                                         result.v_date = schedulerUtil.to_date(column_value.substring(column_value.indexOf('=') + 1) + " " + result.v_hour_value, "mm/dd/yyyy HH24:MI:SS");
332
333                                         result.v_value = schedulerUtil.to_date_str(schedulerUtil.add_hours(result.v_date, 1), "mm/dd/yyyy HH24");
334
335                                         if (result.v_name.length() > 0) {
336                                                 if (result.v_hour.length() > 0) {
337
338                                                         result.v_formfields_insert = result.v_formfields_insert + result.v_name + "=" + result.v_value.substring(0, 10) + "&" + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "_Hr=" + result.v_value.substring(11, 13);
339                                                         result.v_formfields_generate = result.v_formfields_generate + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "=" + result.v_value.substring(0, 10) + "&" + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "_Hr="
340                                                                         + result.v_value.substring(11, 13) + "&";
341
342                                                 } else {
343                                                         result.v_formfields_insert = result.v_formfields_insert + result.v_name + "=" + result.v_value + "&";
344                                                         result.v_formfields_generate = result.v_formfields_generate + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "=" + result.v_value + "&";
345                                                 }
346                                         }
347                                 }
348                                 if (column_value.indexOf("_Hr") <= 0) {
349                                         result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
350                                         result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
351                                 }
352                         } else if ("WEEKLY".equals(v_recurrence)) {
353
354                                 if (column_value.indexOf("_auto") > 0) {
355                                         result.v_name = column_value.substring(0, column_value.indexOf('='));
356                                         result.v_date = schedulerUtil.to_date(column_value.substring(column_value.indexOf('=') + 1), "mm/dd/yyyy");
357                                         result.v_value = schedulerUtil.add_days(result.v_date, 7).toString();
358
359                                         if (result.v_name.length() > 0) {
360                                                 result.v_formfields_insert = result.v_formfields_insert + result.v_name + "=" + result.v_value + "&";
361                                                 result.v_formfields_generate = result.v_formfields_generate + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "=" + result.v_value + "&";
362                                         }
363
364                                 } else {
365                                         result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
366                                         result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
367                                 }
368
369                         } else {
370                                 if (column_value.indexOf("_auto") > 0) {
371                                         result.v_name = column_value.substring(0, column_value.indexOf('='));
372                                         result.v_date = schedulerUtil.to_date(column_value.substring(column_value.indexOf('=') + 1), "mm/dd/yyyy");
373                                         result.v_value = schedulerUtil.add_days(result.v_date, 7).toString();
374                                         if (result.v_name.length() > 0) {
375                                                 result.v_formfields_insert = result.v_formfields_insert + result.v_name + "=" + result.v_value + "&";
376                                                 result.v_formfields_generate = result.v_formfields_generate + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "=" + result.v_value + "&";
377                                         }
378
379                                 } else {
380                                         result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
381                                         result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
382                                 }
383                         }
384                 }
385
386                 schedulerUtil.insertOrUpdate("update CR_REPORT_SCHEDULE set processed_formfields ='" + result.v_formfields_insert + "' where schedule_id =" + v_schedule_id);
387                 return "&" + result.v_formfields_generate.substring(0, result.v_formfields_generate.length());
388         }
389
390         private int get_report_sched_offset(int p_rep_id, int p_schedule_id) throws SQLException, ReportSQLException {
391
392                 class CrReportSchedule {
393
394                         Date run_date;
395                         String recurrence;
396                 }
397                 Date v_last_date = null;
398                 Date v_sysdate = new Date();
399
400                 final CrReportSchedule v_report_schedule_rec = new CrReportSchedule();
401
402                 schedulerUtil.getAndExecute("SELECT * FROM cr_report_schedule WHERE rep_id = " + p_rep_id + " and schedule_id = " + p_schedule_id, new Executor() {
403
404                         @Override
405                         public void execute(ResultSet rs) throws SQLException {
406
407                                 v_report_schedule_rec.recurrence = rs.getString("recurrence");
408                                 java.sql.Timestamp runDate = rs.getTimestamp("run_date");
409                                 v_report_schedule_rec.run_date = new Date(runDate.getTime()) ;
410                         }
411
412                 });
413
414                 if (v_report_schedule_rec.run_date == null || v_report_schedule_rec.run_date.compareTo(v_sysdate) > 0) {
415                         return Integer.MAX_VALUE;
416                 }
417
418                 Date v_next_date = v_report_schedule_rec.run_date;
419
420                 while (v_next_date.compareTo(v_sysdate) < 0) {
421                         
422                         if ("HOURLY".equals(v_report_schedule_rec.recurrence)) {
423                                 v_next_date = SchedulerUtil.add_hours(v_next_date, 1);
424                         } else if ("DAILY".equals(v_report_schedule_rec.recurrence)) {
425                                 v_next_date = SchedulerUtil.add_days(v_next_date, 1);
426                         } else if ("DAILY_MO_FR".equals(v_report_schedule_rec.recurrence)) {
427                                 SimpleDateFormat sdf = new SimpleDateFormat("EEE");
428                                 sdf.format(v_next_date);
429                                 if ("FRI".equals(v_next_date.toString())) {
430                                         v_next_date = SchedulerUtil.add_days(v_next_date, 3);
431                                 } else if ("SAT".equals(v_next_date.toString())) {
432                                         v_next_date = SchedulerUtil.add_days(v_next_date, 2);
433                                 } else {
434                                         v_next_date = SchedulerUtil.add_days(v_next_date, 1);
435                                 }
436                         } else if ("WEEKLY".equals(v_report_schedule_rec.recurrence)) {
437                                 v_next_date = SchedulerUtil.add_days(v_next_date, 7);
438                         } else if ("MONTHLY".equals(v_report_schedule_rec.recurrence)) {
439                                 v_next_date = SchedulerUtil.add_months(v_next_date, 1);
440                         } else {
441                                 break;
442                         }
443                         v_last_date = v_next_date;
444                 }
445
446                 if (SchedulerUtil.trunc_hour(v_last_date).compareTo(SchedulerUtil.trunc_hour(v_sysdate)) == 0) {
447                         return (int)(Math.abs (v_sysdate.getTime() - v_last_date.getTime()) / 1000);
448                 } else {
449                         // More than an hour
450                         return 3601;
451                 }
452         }
453
454         public SchedulerUtil getSchedulerUtil() {
455                 return schedulerUtil;
456         }
457
458         public void setSchedulerUtil(SchedulerUtil schedulerUtil) {
459                 this.schedulerUtil = schedulerUtil;
460         }
461
462 }