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.Connection;
42 import java.sql.ResultSet;
43 import java.sql.SQLException;
44 import java.sql.Statement;
45 import java.text.SimpleDateFormat;
46 import java.util.Date;
47 import java.util.UUID;
49 import org.onap.portalsdk.analytics.error.RaptorException;
50 import org.onap.portalsdk.analytics.error.ReportSQLException;
51 import org.onap.portalsdk.analytics.scheduler.SchedulerUtil.Executor;
52 import org.onap.portalsdk.analytics.system.Globals;
54 public class SendNotifications {
56 SchedulerUtil schedulerUtil;
57 public SendEmail sendEmail;
59 public SendNotifications() throws Exception {
60 schedulerUtil = new SchedulerUtil();
61 sendEmail = new SendEmail();
62 sendEmail.setSchedulerUtil(schedulerUtil);
67 public void deInit() throws SQLException {
68 schedulerUtil.closeConnection();
71 public void init() throws SQLException, ReportSQLException {
75 public void send_notification(String p_mail_server, String p_sender, String p_system_name, String p_system_url, int connectionTimeout) throws RaptorException, Exception {
77 System.out.println(p_mail_server + " " + p_sender + " " + p_system_name + " " + p_system_url);
78 int p_time_interval = Globals.getSchedulerInterval();
81 BigDecimal v_id = null;
83 String v_r_action = "report.download.pdf";
87 // int transfer_timeout_limit = 1800;
88 // boolean v_attach_email_yn = true;
93 Connection conn = schedulerUtil.getConnection();
94 Statement stat = conn.createStatement();
96 String CNotificationsql =
98 "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 "
100 + "SELECT rs.rep_id, rs.schedule_id, rs.sched_user_id user_id, rs.conditional_yn, rs.condition_large_sql, "
101 + "rs.notify_type, rs.max_row, rs.initial_formfields, rs.processed_formfields "
102 + "FROM cr_report_schedule rs "
103 + "WHERE rs.enabled_yn='Y' "
106 + "AND (rs.end_date >= sysdate or rs.end_date is null ) "
107 + "AND rs.run_date IS NOT NULL "
108 + ") x, cr_report r "
109 + "WHERE x.rep_id = r.rep_id ";
112 Globals.getAvailableSchedules().replace("[currentDate]", Globals.getCurrentDateString());
115 ResultSet rs = stat.executeQuery(CNotificationsql);
119 v_schedule_id = rs.getInt("schedule_id");
120 int offset = get_report_sched_offset(rs.getInt("rep_id"), v_schedule_id);
122 if(offset >= p_time_interval) continue;
125 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");
126 if (v_touch_date != null) {
127 if (Math.abs(System.currentTimeMillis() - v_touch_date.getTime()) /1000 < (p_time_interval - 1)) {
132 if ("Y".equals(rs.getString("conditional_yn"))) {
134 v_num_recs = (Integer) schedulerUtil.getSingleResult("select count(*) count from (" + rs.getString("condition_large_sql") + " )", "count");
137 if (v_num_recs > 0 || "N".equals(rs.getString("conditional_yn"))) {
139 v_gen_key = ("Z" + UUID.randomUUID()).toString().substring(0,24); // 25 character string
140 Object sequenceId = schedulerUtil.getSingleResult(Globals.getSequenceNextVal().replace("[sequenceName]", "seq_email_sent_log_id"), "id");
142 if(sequenceId instanceof Long)
143 v_id = new BigDecimal((Long)sequenceId);
144 else if(sequenceId instanceof BigDecimal)
145 v_id = (BigDecimal)sequenceId;
147 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() + " )");
149 int notify_type = rs.getInt("notify_type");
150 if (notify_type == 4)
151 v_r_action = "report.download";
152 else if (notify_type == 2)
153 v_r_action = "report.download.pdf";
154 else if (notify_type == 3)
155 v_r_action = "report.csv.download";
156 else if (notify_type == 5)
157 v_r_action = "report.download.excel2007";
158 else if (notify_type == 6)
159 v_r_action = "download.all";
161 if (rs.getObject("processed_formfields") != null)
162 v_formfields = modify_formfields(v_schedule_id, rs.getString("processed_formFields"));
164 v_formfields = strip_formfields(v_schedule_id, rs.getString("initial_formfields"));
166 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;
168 boolean v_attach_email_yn = shouldSendAttachmentInEmail(v_schedule_id);
170 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>"
171 + "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>";
173 if (rs.getInt("notify_type") != 6) {
175 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);
180 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() + " )");
181 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() + " )");
185 schedulerUtil.insertOrUpdate("update cr_report_schedule set run_date = " + Globals.getCurrentDateString() +" where schedule_id=" + v_schedule_id);
187 schedulerUtil.insertOrUpdate("update cr_report_email_sent_log set sent_date= " + Globals.getCurrentDateString() +" , access_flag='N' where log_id=" + v_id);
188 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() + " )");
202 private boolean shouldSendAttachmentInEmail(int v_schedule_id) throws SQLException, ReportSQLException {
204 String l_boolean = (String) schedulerUtil.getSingleResult("SELECT ATTACHMENT_YN from cr_report_schedule where schedule_id = " + v_schedule_id, "ATTACHMENT_YN");
205 if ("Y".equals(l_boolean))
210 private String strip_formfields(int v_schedule_id, String p_formfields) throws SQLException, ReportSQLException {
212 String v_formfields_insert = "";
213 String v_formfields_generate = "";
218 String[] column_values = schedulerUtil.cr_dissecturl(p_formfields, "&");
220 for(String column_value : column_values){
221 if(column_value == null || column_value.isEmpty())
224 v_name = column_value.substring(0, column_value.indexOf('='));
225 v_formfields_insert += column_value + "&";
226 v_value = column_value.substring(column_value.indexOf('=') + 1);
227 if (column_value.indexOf("_auto") > 0) {
228 v_formfields_generate = v_formfields_generate + v_name.substring(0, v_name.indexOf("_auto")) + "=" + v_value + "&";
230 v_formfields_generate = v_formfields_generate + column_value + "&";
234 schedulerUtil.insertOrUpdate("update CR_REPORT_SCHEDULE set processed_formfields ='" + v_formfields_insert + "' where schedule_id = " + v_schedule_id);
236 return v_formfields_generate.substring(0, v_formfields_generate.length());
240 private String modify_formfields(int v_schedule_id, String p_formfields) throws SQLException, ReportSQLException {
244 String v_formfields_insert = "";
245 String v_formfields_generate = "";
250 String v_hour_value = "";
253 final Result result = new Result();
255 final String v_recurrence = (String) schedulerUtil.getSingleResult("select recurrence from cr_report_schedule where schedule_id = " + v_schedule_id, "recurrence");
257 String[] column_values = schedulerUtil.cr_dissecturl(p_formfields, "&");
259 for(String column_value : column_values){
260 if (column_value == null)
263 if ("MONTHLY".equals(v_recurrence)) {
264 if (column_value.indexOf("_auto") > 0) {
265 result.v_name = column_value.substring(0, column_value.indexOf('='));
266 result.v_date = schedulerUtil.to_date(column_value.substring(column_value.indexOf('=') + 1), "mm/dd/yyyy");
267 result.v_value = schedulerUtil.add_months(result.v_date, 1).toString();
269 if (result.v_name.length() > 0) {
270 result.v_formfields_insert = result.v_formfields_insert + result.v_name + "=" + result.v_value + "&";
271 result.v_formfields_generate = result.v_formfields_generate + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "=" + result.v_value + "&";
274 result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
275 result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
278 } else if ("DAILY".equals(v_recurrence)) {
279 if (column_value.indexOf("_auto") > 0) {
280 result.v_name = column_value.substring(0, column_value.indexOf('='));
281 result.v_date = schedulerUtil.to_date(column_value.substring(column_value.indexOf('=') + 1), "mm/dd/yyyy");
282 result.v_value = schedulerUtil.add_months(result.v_date, 1).toString();
284 if (result.v_name.length() > 0) {
285 result.v_formfields_insert = result.v_formfields_insert + result.v_name + "=" + result.v_value + "&";
286 result.v_formfields_generate = result.v_formfields_generate + 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_MO_FR".equals(v_recurrence)) {
294 if (column_value.indexOf("_auto") > 0) {
295 result.v_name = column_value.substring(0, column_value.indexOf('='));
296 result.v_date = schedulerUtil.to_date(column_value.substring(column_value.indexOf('=') + 1), "mm/dd/yyyy");
297 SimpleDateFormat sdf = new SimpleDateFormat("EEE");
298 sdf.format(result.v_date);
299 if ("FRI".equals(result.v_date.toString())) {
300 result.v_date = schedulerUtil.add_days(result.v_date, 3);
301 } else if ("SAT".equals(result.v_date.toString())) {
302 result.v_date = schedulerUtil.add_days(result.v_date, 2);
304 result.v_date = schedulerUtil.add_days(result.v_date, 1);
306 result.v_value = result.v_date.toString();
307 if (result.v_name.length() > 0) {
308 result.v_formfields_insert = result.v_formfields_insert + result.v_name + "=" + result.v_value + "&";
309 result.v_formfields_generate = result.v_formfields_generate + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "=" + result.v_value + "&";
313 result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
314 result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
317 } else if ("HOURLY".equals(v_recurrence)) {
319 result.v_name = column_value.indexOf('=')>0?column_value.substring(0, column_value.indexOf('=')) : "";
320 if (column_value.indexOf("_auto") > 0) {
323 String[] column_values2 = schedulerUtil.cr_dissecturl(p_formfields, "&");
325 for(String column_value2 : column_values2){
327 String key = column_value2.substring(0, column_value2.indexOf("="));
328 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")))){
329 result.v_hour = column_value2;
333 // 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() {
336 // public void execute(ResultSet rs) throws SQLException {
338 // result.v_hour = rs.getString("column_value");
343 if (result.v_hour.length() > 0) {
344 result.v_hour_value = result.v_hour.substring(result.v_hour.indexOf('=') + 1);
347 result.v_date = schedulerUtil.to_date(column_value.substring(column_value.indexOf('=') + 1) + " " + result.v_hour_value, "mm/dd/yyyy HH24:MI:SS");
349 result.v_value = schedulerUtil.to_date_str(schedulerUtil.add_hours(result.v_date, 1), "mm/dd/yyyy HH24");
351 if (result.v_name.length() > 0) {
352 if (result.v_hour.length() > 0) {
354 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);
355 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="
356 + result.v_value.substring(11, 13) + "&";
359 result.v_formfields_insert = result.v_formfields_insert + result.v_name + "=" + result.v_value + "&";
360 result.v_formfields_generate = result.v_formfields_generate + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "=" + result.v_value + "&";
364 if (column_value.indexOf("_Hr") <= 0) {
365 result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
366 result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
368 } else if ("WEEKLY".equals(v_recurrence)) {
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();
375 if (result.v_name.length() > 0) {
376 result.v_formfields_insert = result.v_formfields_insert + result.v_name + "=" + result.v_value + "&";
377 result.v_formfields_generate = result.v_formfields_generate + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "=" + result.v_value + "&";
381 result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
382 result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
386 if (column_value.indexOf("_auto") > 0) {
387 result.v_name = column_value.substring(0, column_value.indexOf('='));
388 result.v_date = schedulerUtil.to_date(column_value.substring(column_value.indexOf('=') + 1), "mm/dd/yyyy");
389 result.v_value = schedulerUtil.add_days(result.v_date, 7).toString();
390 if (result.v_name.length() > 0) {
391 result.v_formfields_insert = result.v_formfields_insert + result.v_name + "=" + result.v_value + "&";
392 result.v_formfields_generate = result.v_formfields_generate + result.v_name.substring(0, result.v_name.indexOf("_auto")) + "=" + result.v_value + "&";
396 result.v_formfields_insert = result.v_formfields_insert + column_value + "&";
397 result.v_formfields_generate = result.v_formfields_generate + column_value + "&";
402 schedulerUtil.insertOrUpdate("update CR_REPORT_SCHEDULE set processed_formfields ='" + result.v_formfields_insert + "' where schedule_id =" + v_schedule_id);
403 return "&" + result.v_formfields_generate.substring(0, result.v_formfields_generate.length());
406 private int get_report_sched_offset(int p_rep_id, int p_schedule_id) throws SQLException, ReportSQLException {
408 class CrReportSchedule {
413 Date v_last_date = null;
414 Date v_sysdate = new Date();
416 final CrReportSchedule v_report_schedule_rec = new CrReportSchedule();
418 schedulerUtil.getAndExecute("SELECT * FROM cr_report_schedule WHERE rep_id = " + p_rep_id + " and schedule_id = " + p_schedule_id, new Executor() {
421 public void execute(ResultSet rs) throws SQLException {
423 v_report_schedule_rec.recurrence = rs.getString("recurrence");
424 java.sql.Timestamp runDate = rs.getTimestamp("run_date");
425 v_report_schedule_rec.run_date = new Date(runDate.getTime()) ;
430 if (v_report_schedule_rec.run_date == null || v_report_schedule_rec.run_date.compareTo(v_sysdate) > 0) {
431 return Integer.MAX_VALUE;
434 Date v_next_date = v_report_schedule_rec.run_date;
436 while (v_next_date.compareTo(v_sysdate) < 0) {
438 if ("HOURLY".equals(v_report_schedule_rec.recurrence)) {
439 v_next_date = SchedulerUtil.add_hours(v_next_date, 1);
440 } else if ("DAILY".equals(v_report_schedule_rec.recurrence)) {
441 v_next_date = SchedulerUtil.add_days(v_next_date, 1);
442 } else if ("DAILY_MO_FR".equals(v_report_schedule_rec.recurrence)) {
443 SimpleDateFormat sdf = new SimpleDateFormat("EEE");
444 sdf.format(v_next_date);
445 if ("FRI".equals(v_next_date.toString())) {
446 v_next_date = SchedulerUtil.add_days(v_next_date, 3);
447 } else if ("SAT".equals(v_next_date.toString())) {
448 v_next_date = SchedulerUtil.add_days(v_next_date, 2);
450 v_next_date = SchedulerUtil.add_days(v_next_date, 1);
452 } else if ("WEEKLY".equals(v_report_schedule_rec.recurrence)) {
453 v_next_date = SchedulerUtil.add_days(v_next_date, 7);
454 } else if ("MONTHLY".equals(v_report_schedule_rec.recurrence)) {
455 v_next_date = SchedulerUtil.add_months(v_next_date, 1);
459 v_last_date = v_next_date;
462 if (SchedulerUtil.trunc_hour(v_last_date).compareTo(SchedulerUtil.trunc_hour(v_sysdate)) == 0) {
463 return (int)(Math.abs (v_sysdate.getTime() - v_last_date.getTime()) / 1000);
470 public SchedulerUtil getSchedulerUtil() {
471 return schedulerUtil;
474 public void setSchedulerUtil(SchedulerUtil schedulerUtil) {
475 this.schedulerUtil = schedulerUtil;