2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 package org.openecomp.portalsdk.analytics.scheduler;
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;
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;
38 public class SendNotifications {
40 SchedulerUtil schedulerUtil;
41 public SendEmail sendEmail;
43 public SendNotifications() throws Exception {
44 schedulerUtil = new SchedulerUtil();
45 sendEmail = new SendEmail();
46 sendEmail.setSchedulerUtil(schedulerUtil);
51 public void deInit() throws SQLException {
52 schedulerUtil.closeConnection();
55 public void init() throws SQLException, ReportSQLException {
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 {
61 System.out.println(p_mail_server + " " + p_sender + " " + p_system_name + " " + p_system_url);
62 int p_time_interval = Globals.getSchedulerInterval();
65 BigDecimal v_id = null;
67 String v_r_action = "report.download.pdf";
71 // int transfer_timeout_limit = 1800;
72 // boolean v_attach_email_yn = true;
77 Connection conn = schedulerUtil.getConnection();
78 Statement stat = conn.createStatement();
80 String CNotificationsql =
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 "
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' "
90 + "AND (rs.end_date >= sysdate or rs.end_date is null ) "
91 + "AND rs.run_date IS NOT NULL "
93 + "WHERE x.rep_id = r.rep_id ";
96 Globals.getAvailableSchedules().replace("[currentDate]", Globals.getCurrentDateString());
99 ResultSet rs = stat.executeQuery(CNotificationsql);
103 v_schedule_id = rs.getInt("schedule_id");
104 int offset = get_report_sched_offset(rs.getInt("rep_id"), v_schedule_id);
106 if(offset >= p_time_interval) continue;
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)) {
116 if ("Y".equals(rs.getString("conditional_yn"))) {
118 v_num_recs = (Integer) schedulerUtil.getSingleResult("select count(*) count from (" + rs.getString("condition_large_sql") + " )", "count");
121 if (v_num_recs > 0 || "N".equals(rs.getString("conditional_yn"))) {
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");
126 if(sequenceId instanceof Long)
127 v_id = new BigDecimal((Long)sequenceId);
128 else if(sequenceId instanceof BigDecimal)
129 v_id = (BigDecimal)sequenceId;
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() + " )");
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";
145 if (rs.getObject("processed_formfields") != null)
146 v_formfields = modify_formfields(v_schedule_id, rs.getString("processed_formFields"));
148 v_formfields = strip_formfields(v_schedule_id, rs.getString("initial_formfields"));
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;
152 boolean v_attach_email_yn = shouldSendAttachmentInEmail(v_schedule_id);
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>";
157 if (rs.getInt("notify_type") != 6) {
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);
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() + " )");
169 schedulerUtil.insertOrUpdate("update cr_report_schedule set run_date = " + Globals.getCurrentDateString() +" where schedule_id=" + v_schedule_id);
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() + " )");
186 private boolean shouldSendAttachmentInEmail(int v_schedule_id) throws SQLException, ReportSQLException {
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))
194 private String strip_formfields(int v_schedule_id, String p_formfields) throws SQLException, ReportSQLException {
196 String v_formfields_insert = "";
197 String v_formfields_generate = "";
202 String[] column_values = schedulerUtil.cr_dissecturl(p_formfields, "&");
204 for(String column_value : column_values){
205 if(column_value == null || column_value.isEmpty())
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 + "&";
214 v_formfields_generate = v_formfields_generate + column_value + "&";
218 schedulerUtil.insertOrUpdate("update CR_REPORT_SCHEDULE set processed_formfields ='" + v_formfields_insert + "' where schedule_id = " + v_schedule_id);
220 return v_formfields_generate.substring(0, v_formfields_generate.length());
224 private String modify_formfields(int v_schedule_id, String p_formfields) throws SQLException, ReportSQLException {
228 String v_formfields_insert = "";
229 String v_formfields_generate = "";
234 String v_hour_value = "";
237 final Result result = new Result();
239 final String v_recurrence = (String) schedulerUtil.getSingleResult("select recurrence from cr_report_schedule where schedule_id = " + v_schedule_id, "recurrence");
241 String[] column_values = schedulerUtil.cr_dissecturl(p_formfields, "&");
243 for(String column_value : column_values){
244 if (column_value == null)
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();
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 + "&";
258 result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
259 result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
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();
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 + "&";
273 result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
274 result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
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);
288 result.v_date = schedulerUtil.add_days(result.v_date, 1);
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 + "&";
297 result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
298 result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
301 } else if ("HOURLY".equals(v_recurrence)) {
303 result.v_name = column_value.indexOf('=')>0?column_value.substring(0, column_value.indexOf('=')) : "";
304 if (column_value.indexOf("_auto") > 0) {
307 String[] column_values2 = schedulerUtil.cr_dissecturl(p_formfields, "&");
309 for(String column_value2 : column_values2){
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;
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() {
320 // public void execute(ResultSet rs) throws SQLException {
322 // result.v_hour = rs.getString("column_value");
327 if (result.v_hour.length() > 0) {
328 result.v_hour_value = result.v_hour.substring(result.v_hour.indexOf('=') + 1);
331 result.v_date = schedulerUtil.to_date(column_value.substring(column_value.indexOf('=') + 1) + " " + result.v_hour_value, "mm/dd/yyyy HH24:MI:SS");
333 result.v_value = schedulerUtil.to_date_str(schedulerUtil.add_hours(result.v_date, 1), "mm/dd/yyyy HH24");
335 if (result.v_name.length() > 0) {
336 if (result.v_hour.length() > 0) {
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) + "&";
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 + "&";
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 + "&";
352 } else if ("WEEKLY".equals(v_recurrence)) {
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();
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 + "&";
365 result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
366 result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
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 + "&";
380 result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
381 result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
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());
390 private int get_report_sched_offset(int p_rep_id, int p_schedule_id) throws SQLException, ReportSQLException {
392 class CrReportSchedule {
397 Date v_last_date = null;
398 Date v_sysdate = new Date();
400 final CrReportSchedule v_report_schedule_rec = new CrReportSchedule();
402 schedulerUtil.getAndExecute("SELECT * FROM cr_report_schedule WHERE rep_id = " + p_rep_id + " and schedule_id = " + p_schedule_id, new Executor() {
405 public void execute(ResultSet rs) throws SQLException {
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()) ;
414 if (v_report_schedule_rec.run_date == null || v_report_schedule_rec.run_date.compareTo(v_sysdate) > 0) {
415 return Integer.MAX_VALUE;
418 Date v_next_date = v_report_schedule_rec.run_date;
420 while (v_next_date.compareTo(v_sysdate) < 0) {
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);
434 v_next_date = SchedulerUtil.add_days(v_next_date, 1);
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);
443 v_last_date = v_next_date;
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);
454 public SchedulerUtil getSchedulerUtil() {
455 return schedulerUtil;
458 public void setSchedulerUtil(SchedulerUtil schedulerUtil) {
459 this.schedulerUtil = schedulerUtil;