Refactor Prov DB handling
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / reports / SubscriberReport.java
index 51beac9..aac6dab 100644 (file)
@@ -33,8 +33,7 @@ import java.sql.SQLException;
 import java.util.HashMap;\r
 import java.util.Map;\r
 import java.util.TreeSet;\r
-\r
-import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
+import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;\r
 \r
 /**\r
  * Generate a subscribers report.  The report is a .CSV file.  It contains information per-day and per-subscriber,\r
@@ -45,15 +44,15 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB;
  */\r
 public class SubscriberReport extends ReportBase {\r
     private static final String SELECT_SQL =\r
-            "select date(from_unixtime(EVENT_TIME div 1000)) as DATE, DELIVERY_SUBID, RESULT, COUNT(RESULT) as COUNT" +\r
-                    " from LOG_RECORDS" +\r
-                    " where TYPE = 'del' and EVENT_TIME >= ? and EVENT_TIME <= ?" +\r
-                    " group by DATE, DELIVERY_SUBID, RESULT";\r
+        "select date(from_unixtime(EVENT_TIME div 1000)) as DATE, DELIVERY_SUBID, RESULT, COUNT(RESULT) as COUNT" +\r
+            " from LOG_RECORDS" +\r
+            " where TYPE = 'del' and EVENT_TIME >= ? and EVENT_TIME <= ?" +\r
+            " group by DATE, DELIVERY_SUBID, RESULT";\r
     private static final String SELECT_SQL2 =\r
-            "select date(from_unixtime(EVENT_TIME div 1000)) as DATE, DELIVERY_SUBID, COUNT(CONTENT_LENGTH_2) as COUNT" +\r
-                    " from LOG_RECORDS" +\r
-                    " where TYPE = 'dlx' and CONTENT_LENGTH_2 = -1 and EVENT_TIME >= ? and EVENT_TIME <= ?" +\r
-                    " group by DATE, DELIVERY_SUBID";\r
+        "select date(from_unixtime(EVENT_TIME div 1000)) as DATE, DELIVERY_SUBID, COUNT(CONTENT_LENGTH_2) as COUNT" +\r
+            " from LOG_RECORDS" +\r
+            " where TYPE = 'dlx' and CONTENT_LENGTH_2 = -1 and EVENT_TIME >= ? and EVENT_TIME <= ?" +\r
+            " group by DATE, DELIVERY_SUBID";\r
 \r
     private class Counters {\r
         private String date;\r
@@ -89,20 +88,17 @@ public class SubscriberReport extends ReportBase {
         @Override\r
         public String toString() {\r
             return date + "," + sub + "," +\r
-                    c100 + "," + c200 + "," + c300 + "," + c400 + "," + c500 + "," +\r
-                    cm1 + "," + cdlx;\r
+                c100 + "," + c200 + "," + c300 + "," + c400 + "," + c500 + "," +\r
+                cm1 + "," + cdlx;\r
         }\r
     }\r
 \r
     @Override\r
     public void run() {\r
-        Map<String, Counters> map = new HashMap<String, Counters>();\r
+        Map<String, Counters> map = new HashMap<>();\r
         long start = System.currentTimeMillis();\r
 \r
-        try {\r
-            DB db = new DB();\r
-            @SuppressWarnings("resource")\r
-            Connection conn = db.getConnection();\r
+        try (Connection conn = ProvDbUtils.getInstance().getConnection()) {\r
             try(PreparedStatement ps = conn.prepareStatement(SELECT_SQL)) {\r
                 ps.setLong(1, from);\r
                 ps.setLong(2, to);\r
@@ -123,38 +119,37 @@ public class SubscriberReport extends ReportBase {
                 }\r
             }\r
 \r
-           try( PreparedStatement ps2 = conn.prepareStatement(SELECT_SQL2)) {\r
-               ps2.setLong(1, from);\r
-               ps2.setLong(2, to);\r
-               try(ResultSet rs2 = ps2.executeQuery()) {\r
-                   while (rs2.next()) {\r
-                       String date = rs2.getString("DATE");\r
-                       int sub = rs2.getInt("DELIVERY_SUBID");\r
-                       int count = rs2.getInt("COUNT");\r
-                       String key = date + "," + sub;\r
-                       Counters c = map.get(key);\r
-                       if (c == null) {\r
-                           c = new Counters(date, sub);\r
-                           map.put(key, c);\r
-                       }\r
-                       c.addDlxCount(count);\r
-                   }\r
-                  }\r
-           }\r
-\r
-            db.release(conn);\r
+            try( PreparedStatement ps2 = conn.prepareStatement(SELECT_SQL2)) {\r
+                ps2.setLong(1, from);\r
+                ps2.setLong(2, to);\r
+                try (ResultSet rs2 = ps2.executeQuery()) {\r
+                    while (rs2.next()) {\r
+                        String date = rs2.getString("DATE");\r
+                        int sub = rs2.getInt("DELIVERY_SUBID");\r
+                        int count = rs2.getInt("COUNT");\r
+                        String key = date + "," + sub;\r
+                        Counters c = map.get(key);\r
+                        if (c == null) {\r
+                            c = new Counters(date, sub);\r
+                            map.put(key, c);\r
+                        }\r
+                        c.addDlxCount(count);\r
+                    }\r
+                }\r
+            }\r
         } catch (SQLException e) {\r
-            e.printStackTrace();\r
+            logger.error("SQLException: " + e.getMessage());\r
         }\r
         logger.debug("Query time: " + (System.currentTimeMillis() - start) + " ms");\r
-        try (PrintWriter os = new PrintWriter(outfile)){\r
+        try (PrintWriter os = new PrintWriter(outfile)) {\r
             os.println("date,subid,count100,count200,count300,count400,count500,countminus1,countdlx");\r
-            for (String key : new TreeSet<String>(map.keySet())) {\r
+            for (String key : new TreeSet<>(map.keySet())) {\r
                 Counters c = map.get(key);\r
                 os.println(c.toString());\r
             }\r
         } catch (FileNotFoundException e) {\r
             System.err.println("File cannot be written: " + outfile);\r
+            logger.error("FileNotFoundException: " + e.getMessage());\r
         }\r
     }\r
 }\r