2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portalsdk.analytics.scheduler;
40 import java.math.BigDecimal;
41 import java.sql.ResultSet;
42 import java.sql.SQLException;
43 import java.text.SimpleDateFormat;
44 import java.util.Date;
45 import java.util.UUID;
46 import org.onap.portalsdk.analytics.error.RaptorException;
47 import org.onap.portalsdk.analytics.error.ReportSQLException;
48 import org.onap.portalsdk.analytics.scheduler.SchedulerUtil.Executor;
49 import org.onap.portalsdk.analytics.system.Globals;
50 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
52 public class SendNotifications {
54 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SendNotifications.class);
55 SchedulerUtil schedulerUtil;
56 public SendEmail sendEmail;
58 public SendNotifications() throws Exception {
59 schedulerUtil = new SchedulerUtil();
60 sendEmail = new SendEmail();
61 sendEmail.setSchedulerUtil(schedulerUtil);
65 public void deInit() throws SQLException {
66 schedulerUtil.closeConnection();
69 public void init() throws SQLException, ReportSQLException {
73 public void send_notification(final String p_mail_server, final String p_sender, final String p_system_name,
74 final String p_system_url, final int connectionTimeout) throws RaptorException, Exception {
76 logger.info(p_mail_server + " " + p_sender + " " + p_system_name + " " + p_system_url);
77 final int p_time_interval = Globals.getSchedulerInterval();
79 BigDecimal v_id = null;
80 String v_r_action = "report.download.pdf";
82 final String CNotificationsql = Globals.getAvailableSchedules()
83 .replace("[currentDate]", Globals.getCurrentDateString());
85 try (final ResultSet rs = schedulerUtil.getConnection().createStatement().executeQuery(CNotificationsql);) {
87 final int v_schedule_id = rs.getInt("schedule_id");
88 final int offset = get_report_sched_offset(rs.getInt("rep_id"), v_schedule_id);
90 if (offset >= p_time_interval) {
94 final Date v_touch_date = (Date) schedulerUtil.getSingleResult(
95 "select touch_date from cr_report_email_sent_log where schedule_id = " + v_schedule_id
96 + " and log_id = (select max(log_id) from cr_report_email_sent_log where schedule_id = "
97 + v_schedule_id + ")",
99 if (v_touch_date != null) {
100 if (Math.abs(System.currentTimeMillis() - v_touch_date.getTime()) / 1000 < (p_time_interval - 1)) {
105 if ("Y".equals(rs.getString("conditional_yn"))) {
106 v_num_recs = (Integer) schedulerUtil
108 "select count(*) count from (" + rs.getString("condition_large_sql") + " )",
112 if (v_num_recs > 0 || "N".equals(rs.getString("conditional_yn"))) {
113 final String v_gen_key = ("Z" + UUID.randomUUID()).toString()
114 .substring(0, 24); // 25 character string
115 final Object sequenceId =
116 schedulerUtil.getSingleResult(
117 Globals.getSequenceNextVal().replace("[sequenceName]", "seq_email_sent_log_id"),
120 if (sequenceId instanceof Long) {
121 v_id = new BigDecimal((Long) sequenceId);
122 } else if (sequenceId instanceof BigDecimal) {
123 v_id = (BigDecimal) sequenceId;
126 schedulerUtil.insertOrUpdate(
127 "insert into cr_report_email_sent_log (log_id, gen_key, schedule_id, rep_id, user_id, touch_date) values ("
128 + v_id + ",'" + v_gen_key + "'," + rs.getInt("schedule_id") + ","
129 + rs.getInt("rep_id")
131 + rs.getInt("user_id") + ", " + Globals.getCurrentDateString() + " )");
133 final 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";
146 final String v_formfields;
147 if (rs.getObject("processed_formfields") != null) {
148 v_formfields = modify_formfields(v_schedule_id, rs.getString("processed_formFields"));
150 v_formfields = strip_formfields(v_schedule_id, rs.getString("initial_formfields"));
154 p_system_url + "&r_action=" + v_r_action + "&log_id=" + v_id + "&user_id=" + rs
155 .getString("user_id")
156 + "&pdfAttachmentKey=" + v_gen_key + "&download_limit=" + rs.getInt("max_row")
159 final boolean v_attach_email_yn = shouldSendAttachmentInEmail(v_schedule_id);
161 final String v_email_msg =
162 "<html><body><p><b><u><i>" + p_system_name + " System Notification</i></u></b></p>"
163 + "<p>Report <b>" + rs.getString("title")
164 + "</b> is available for viewing.</p><p>You can view the report if it is attached. </br>"
165 + "If it is not attached, or you have problem to open it, you can log into Business Direct and run the report.</p>"
168 if (rs.getInt("notify_type") != 6) {
169 sendEmail.sendEmail(p_mail_server, p_sender,
170 p_system_name + " System Notification: Report " + rs.getString("title") + " generated",
171 v_email_msg, v_url, rs.getInt("notify_type"), v_schedule_id, p_time_interval,
177 schedulerUtil.insertOrUpdate(
178 "insert into cr_schedule_activity_log (SCHEDULE_ID, url, notes, run_time) values ("
179 + v_schedule_id + ",'" + v_url + "'," + "'Success: http request began.', "
181 .getCurrentDateString()
183 schedulerUtil.insertOrUpdate(
184 "insert into cr_schedule_activity_log (SCHEDULE_ID, url, notes, run_time) values ("
185 + v_schedule_id + ",'" + v_url + "',"
186 + "'Success: http response recieved. Code resp.status_code '' desc '' resp.reason_phrase', "
187 + Globals.getCurrentDateString() + " )");
190 schedulerUtil.insertOrUpdate(
191 "update cr_report_schedule set run_date = " + Globals.getCurrentDateString()
192 + " where schedule_id="
195 schedulerUtil.insertOrUpdate(
196 "update cr_report_email_sent_log set sent_date= " + Globals.getCurrentDateString()
197 + " , access_flag='N' where log_id=" + v_id);
198 schedulerUtil.insertOrUpdate(
199 "insert into cr_schedule_activity_log (SCHEDULE_ID, url, notes, run_time) values ("
201 + ",'" + v_url + "','Success: Email Sent', " + Globals.getCurrentDateString()
205 } catch (final SQLException e) {
206 logger.warn("Failed to open connection", e);
210 private boolean shouldSendAttachmentInEmail(int v_schedule_id) throws SQLException, ReportSQLException {
212 String l_boolean = (String) schedulerUtil.getSingleResult(
213 "SELECT ATTACHMENT_YN from cr_report_schedule where schedule_id = " + v_schedule_id, "ATTACHMENT_YN");
214 if ("Y".equals(l_boolean))
219 private String strip_formfields(int v_schedule_id, String p_formfields) throws SQLException, ReportSQLException {
221 String v_formfields_insert = "";
222 String v_formfields_generate = "";
226 String[] column_values = schedulerUtil.cr_dissecturl(p_formfields, "&");
228 for (String column_value : column_values) {
229 if (column_value == null || column_value.isEmpty())
232 v_name = column_value.substring(0, column_value.indexOf('='));
233 v_formfields_insert += column_value + "&";
234 v_value = column_value.substring(column_value.indexOf('=') + 1);
235 if (column_value.indexOf("_auto") > 0) {
236 v_formfields_generate =
237 v_formfields_generate + v_name.substring(0, v_name.indexOf("_auto")) + "=" + v_value + "&";
239 v_formfields_generate = v_formfields_generate + column_value + "&";
243 schedulerUtil.insertOrUpdate("update CR_REPORT_SCHEDULE set processed_formfields ='" + v_formfields_insert
244 + "' where schedule_id = " + v_schedule_id);
246 return v_formfields_generate.substring(0, v_formfields_generate.length());
250 private String modify_formfields(int v_schedule_id, String p_formfields) throws SQLException, ReportSQLException {
254 String v_formfields_insert = "";
255 String v_formfields_generate = "";
260 String v_hour_value = "";
263 final Result result = new Result();
265 final String v_recurrence = (String) schedulerUtil.getSingleResult(
266 "select recurrence from cr_report_schedule where schedule_id = " + v_schedule_id, "recurrence");
268 String[] column_values = schedulerUtil.cr_dissecturl(p_formfields, "&");
270 for (String column_value : column_values) {
271 if (column_value == null)
274 if ("MONTHLY".equals(v_recurrence)) {
275 if (column_value.indexOf("_auto") > 0) {
276 result.v_name = column_value.substring(0, column_value.indexOf('='));
278 schedulerUtil.to_date(column_value.substring(column_value.indexOf('=') + 1), "mm/dd/yyyy");
279 result.v_value = schedulerUtil.add_months(result.v_date, 1).toString();
281 if (result.v_name.length() > 0) {
282 result.v_formfields_insert =
283 result.v_formfields_insert + result.v_name + "=" + result.v_value + "&";
284 result.v_formfields_generate = result.v_formfields_generate
285 + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "=" + result.v_value
289 result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
290 result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
293 } else if ("DAILY".equals(v_recurrence)) {
294 if (column_value.indexOf("_auto") > 0) {
295 result.v_name = column_value.substring(0, column_value.indexOf('='));
297 schedulerUtil.to_date(column_value.substring(column_value.indexOf('=') + 1), "mm/dd/yyyy");
298 result.v_value = schedulerUtil.add_months(result.v_date, 1).toString();
300 if (result.v_name.length() > 0) {
301 result.v_formfields_insert =
302 result.v_formfields_insert + result.v_name + "=" + result.v_value + "&";
303 result.v_formfields_generate = result.v_formfields_generate
304 + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "=" + result.v_value
308 result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
309 result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
312 } else if ("DAILY_MO_FR".equals(v_recurrence)) {
313 if (column_value.indexOf("_auto") > 0) {
314 result.v_name = column_value.substring(0, column_value.indexOf('='));
316 schedulerUtil.to_date(column_value.substring(column_value.indexOf('=') + 1), "mm/dd/yyyy");
317 SimpleDateFormat sdf = new SimpleDateFormat("EEE");
318 sdf.format(result.v_date);
319 if ("FRI".equals(result.v_date.toString())) {
320 result.v_date = schedulerUtil.add_days(result.v_date, 3);
321 } else if ("SAT".equals(result.v_date.toString())) {
322 result.v_date = schedulerUtil.add_days(result.v_date, 2);
324 result.v_date = schedulerUtil.add_days(result.v_date, 1);
326 result.v_value = result.v_date.toString();
327 if (result.v_name.length() > 0) {
328 result.v_formfields_insert =
329 result.v_formfields_insert + result.v_name + "=" + result.v_value + "&";
330 result.v_formfields_generate = result.v_formfields_generate
331 + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "=" + result.v_value
336 result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
337 result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
340 } else if ("HOURLY".equals(v_recurrence)) {
343 column_value.indexOf('=') > 0 ? column_value.substring(0, column_value.indexOf('=')) : "";
344 if (column_value.indexOf("_auto") > 0) {
346 String[] column_values2 = schedulerUtil.cr_dissecturl(p_formfields, "&");
348 for (String column_value2 : column_values2) {
350 String key = column_value2.substring(0, column_value2.indexOf("="));
351 if (key.equals(result.v_name.substring(0, result.v_name.indexOf("_auto")))
352 || key.equals(result.v_name.substring(0, result.v_name.indexOf("_Hr")))) {
353 result.v_hour = column_value2;
357 // schedulerUtil.getAndExecute("select c.column_value from table(CR_DISSECTURL(p_formfields)) c
358 // where substr(c.column_value, 1, instr(c.column_value, '=')-1) = substr(" + result.v_name +
359 // ",1,instr(" + result.v_name + ",'_auto')-1)||'_Hr'", new Executor() {
362 // public void execute(ResultSet rs) throws SQLException {
364 // result.v_hour = rs.getString("column_value");
369 if (result.v_hour.length() > 0) {
370 result.v_hour_value = result.v_hour.substring(result.v_hour.indexOf('=') + 1);
373 result.v_date = schedulerUtil.to_date(
374 column_value.substring(column_value.indexOf('=') + 1) + " " + result.v_hour_value,
375 "mm/dd/yyyy HH24:MI:SS");
378 schedulerUtil.to_date_str(schedulerUtil.add_hours(result.v_date, 1), "mm/dd/yyyy HH24");
380 if (result.v_name.length() > 0) {
381 if (result.v_hour.length() > 0) {
383 result.v_formfields_insert =
384 result.v_formfields_insert + result.v_name + "=" + result.v_value.substring(0, 10)
385 + "&" + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "_Hr="
386 + result.v_value.substring(11, 13);
387 result.v_formfields_generate = result.v_formfields_generate
388 + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "="
389 + result.v_value.substring(0, 10) + "&"
390 + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "_Hr="
391 + result.v_value.substring(11, 13) + "&";
394 result.v_formfields_insert =
395 result.v_formfields_insert + result.v_name + "=" + result.v_value + "&";
396 result.v_formfields_generate = result.v_formfields_generate
397 + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "=" + result.v_value
402 if (column_value.indexOf("_Hr") <= 0) {
403 result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
404 result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
406 } else if ("WEEKLY".equals(v_recurrence)) {
408 if (column_value.indexOf("_auto") > 0) {
409 result.v_name = column_value.substring(0, column_value.indexOf('='));
411 schedulerUtil.to_date(column_value.substring(column_value.indexOf('=') + 1), "mm/dd/yyyy");
412 result.v_value = schedulerUtil.add_days(result.v_date, 7).toString();
414 if (result.v_name.length() > 0) {
415 result.v_formfields_insert =
416 result.v_formfields_insert + result.v_name + "=" + result.v_value + "&";
417 result.v_formfields_generate = result.v_formfields_generate
418 + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "=" + result.v_value
423 result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
424 result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
428 if (column_value.indexOf("_auto") > 0) {
429 result.v_name = column_value.substring(0, column_value.indexOf('='));
431 schedulerUtil.to_date(column_value.substring(column_value.indexOf('=') + 1), "mm/dd/yyyy");
432 result.v_value = schedulerUtil.add_days(result.v_date, 7).toString();
433 if (result.v_name.length() > 0) {
434 result.v_formfields_insert =
435 result.v_formfields_insert + result.v_name + "=" + result.v_value + "&";
436 result.v_formfields_generate = result.v_formfields_generate
437 + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "=" + result.v_value
442 result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
443 result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
448 schedulerUtil.insertOrUpdate("update CR_REPORT_SCHEDULE set processed_formfields ='"
449 + result.v_formfields_insert + "' where schedule_id =" + v_schedule_id);
450 return "&" + result.v_formfields_generate.substring(0, result.v_formfields_generate.length());
453 private int get_report_sched_offset(int p_rep_id, int p_schedule_id) throws SQLException, ReportSQLException {
455 class CrReportSchedule {
460 Date v_last_date = null;
461 Date v_sysdate = new Date();
463 final CrReportSchedule v_report_schedule_rec = new CrReportSchedule();
465 schedulerUtil.getAndExecute(
466 "SELECT * FROM cr_report_schedule WHERE rep_id = " + p_rep_id + " and schedule_id = " + p_schedule_id,
470 public void execute(ResultSet rs) throws SQLException {
472 v_report_schedule_rec.recurrence = rs.getString("recurrence");
473 java.sql.Timestamp runDate = rs.getTimestamp("run_date");
474 v_report_schedule_rec.run_date = new Date(runDate.getTime());
479 if (v_report_schedule_rec.run_date == null || v_report_schedule_rec.run_date.compareTo(v_sysdate) > 0) {
480 return Integer.MAX_VALUE;
483 Date v_next_date = v_report_schedule_rec.run_date;
485 while (v_next_date.compareTo(v_sysdate) < 0) {
487 if ("HOURLY".equals(v_report_schedule_rec.recurrence)) {
488 v_next_date = SchedulerUtil.add_hours(v_next_date, 1);
489 } else if ("DAILY".equals(v_report_schedule_rec.recurrence)) {
490 v_next_date = SchedulerUtil.add_days(v_next_date, 1);
491 } else if ("DAILY_MO_FR".equals(v_report_schedule_rec.recurrence)) {
492 SimpleDateFormat sdf = new SimpleDateFormat("EEE");
493 sdf.format(v_next_date);
494 if ("FRI".equals(v_next_date.toString())) {
495 v_next_date = SchedulerUtil.add_days(v_next_date, 3);
496 } else if ("SAT".equals(v_next_date.toString())) {
497 v_next_date = SchedulerUtil.add_days(v_next_date, 2);
499 v_next_date = SchedulerUtil.add_days(v_next_date, 1);
501 } else if ("WEEKLY".equals(v_report_schedule_rec.recurrence)) {
502 v_next_date = SchedulerUtil.add_days(v_next_date, 7);
503 } else if ("MONTHLY".equals(v_report_schedule_rec.recurrence)) {
504 v_next_date = SchedulerUtil.add_months(v_next_date, 1);
508 v_last_date = v_next_date;
511 if (v_last_date != null
512 && SchedulerUtil.trunc_hour(v_last_date).compareTo(SchedulerUtil.trunc_hour(v_sysdate)) == 0) {
513 return (int) (Math.abs(v_sysdate.getTime() - v_last_date.getTime()) / 1000);
520 public SchedulerUtil getSchedulerUtil() {
521 return schedulerUtil;
524 public void setSchedulerUtil(SchedulerUtil schedulerUtil) {
525 this.schedulerUtil = schedulerUtil;