Refactor Prov DB handling
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / reports / FeedReport.java
index 6529788..3c63c28 100644 (file)
@@ -37,10 +37,9 @@ import java.util.Arrays;
 import java.util.Calendar;\r
 import java.util.Date;\r
 import java.util.GregorianCalendar;\r
-\r
 import org.json.JSONException;\r
 import org.json.JSONObject;\r
-import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
+import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;\r
 \r
 /**\r
  * Generate a feeds report.  The report is a .CSV file.\r
@@ -49,15 +48,6 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB;
  * @version $Id: FeedReport.java,v 1.2 2013/11/06 16:23:55 eby Exp $\r
  */\r
 public class FeedReport extends ReportBase {\r
-    private static final String SELECT_SQL =\r
-            // Note to use the time in the publish_id, use date(from_unixtime(substring(publish_id, 1, 10)))\r
-            // To just use month, substring(from_unixtime(event_time div 1000), 1, 7)\r
-            "select date(from_unixtime(event_time div 1000)) as date, type, feedid, delivery_subid, count(*) as count" +\r
-                    " from LOG_RECORDS" +\r
-                    " where type = 'pub' or type = 'del'" +\r
-                    " group by date, type, feedid, delivery_subid";\r
-    private static final String SELECT_SQL_OLD =\r
-            "select PUBLISH_ID, TYPE, FEEDID, DELIVERY_SUBID from LOG_RECORDS where EVENT_TIME >= ? and EVENT_TIME <= ?";\r
 \r
     @Override\r
     public void run() {\r
@@ -65,23 +55,22 @@ public class FeedReport extends ReportBase {
         JSONObject jo = new JSONObject();\r
         long start = System.currentTimeMillis();\r
         StringBuilder sb = new StringBuilder();\r
-        try {\r
-            DB db = new DB();\r
-            @SuppressWarnings("resource")\r
-            Connection conn = db.getConnection();\r
-           try( PreparedStatement ps = conn.prepareStatement(SELECT_SQL)) {\r
-               try (ResultSet rs = ps.executeQuery()) {\r
-                   while (rs.next()) {\r
-                       String date = rs.getString("date");\r
-                       String type = rs.getString("type");\r
-                       int feedid = rs.getInt("feedid");\r
-                       int subid = type.equals("del") ? rs.getInt("delivery_subid") : 0;\r
-                       int count = rs.getInt("count");\r
-                       sb.append(date + "," + type + "," + feedid + "," + subid + "," + count + "\n");\r
-                   }\r
-               }\r
-           }\r
-        db.release(conn);\r
+        try (Connection conn = ProvDbUtils.getInstance().getConnection();\r
+            PreparedStatement ps = conn.prepareStatement(\r
+                // Note to use the time in the publish_id, use date(from_unixtime(substring(publish_id, 1, 10)))\r
+                // To just use month, substring(from_unixtime(event_time div 1000), 1, 7)\r
+                "select date(from_unixtime(event_time div 1000)) as date, type, feedid, delivery_subid, count(*) "\r
+                    + "as count from LOG_RECORDS where type = 'pub' or type = 'del' group by date, type, feedid, delivery_subid")) {\r
+            try (ResultSet rs = ps.executeQuery()) {\r
+                while (rs.next()) {\r
+                    String date = rs.getString("date");\r
+                    String type = rs.getString("type");\r
+                    int feedid = rs.getInt("feedid");\r
+                    int subid = type.equals("del") ? rs.getInt("delivery_subid") : 0;\r
+                    int count = rs.getInt("count");\r
+                    sb.append(date + "," + type + "," + feedid + "," + subid + "," + count + "\n");\r
+                }\r
+            }\r
         } catch (SQLException e) {\r
             logger.error(e.toString());\r
         }\r
@@ -99,53 +88,50 @@ public class FeedReport extends ReportBase {
         JSONObject jo = new JSONObject();\r
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");\r
         long start = System.currentTimeMillis();\r
-        try {\r
-            DB db = new DB();\r
-            @SuppressWarnings("resource")\r
-            Connection conn = db.getConnection();\r
-            try(PreparedStatement ps = conn.prepareStatement(SELECT_SQL_OLD)) {\r
-                ps.setLong(1, from);\r
-                ps.setLong(2, to);\r
-                ps.setFetchSize(100000);\r
-                try(ResultSet rs = ps.executeQuery()) {\r
-                    while (rs.next()) {\r
-                        String id = rs.getString("PUBLISH_ID");\r
-                        String date = sdf.format(new Date(getPstart(id)));\r
-                        JSONObject datemap = jo.optJSONObject(date);\r
-                        if (datemap == null) {\r
-                            datemap = new JSONObject();\r
-                            jo.put(date, datemap);\r
-                        }\r
-                        int feed = rs.getInt("FEEDID");\r
-                        JSONObject feedmap = datemap.optJSONObject("" + feed);\r
-                        if (feedmap == null) {\r
-                            feedmap = new JSONObject();\r
-                            feedmap.put("pubcount", 0);\r
-                            datemap.put("" + feed, feedmap);\r
+        try (Connection conn = ProvDbUtils.getInstance().getConnection();\r
+            PreparedStatement ps = conn.prepareStatement(\r
+                "select PUBLISH_ID, TYPE, FEEDID, DELIVERY_SUBID from LOG_RECORDS "\r
+                    + "where EVENT_TIME >= ? and EVENT_TIME <= ?")) {\r
+            ps.setLong(1, from);\r
+            ps.setLong(2, to);\r
+            ps.setFetchSize(100000);\r
+            try(ResultSet rs = ps.executeQuery()) {\r
+                while (rs.next()) {\r
+                    String id = rs.getString("PUBLISH_ID");\r
+                    String date = sdf.format(new Date(getPstart(id)));\r
+                    JSONObject datemap = jo.optJSONObject(date);\r
+                    if (datemap == null) {\r
+                        datemap = new JSONObject();\r
+                        jo.put(date, datemap);\r
+                    }\r
+                    int feed = rs.getInt("FEEDID");\r
+                    JSONObject feedmap = datemap.optJSONObject("" + feed);\r
+                    if (feedmap == null) {\r
+                        feedmap = new JSONObject();\r
+                        feedmap.put("pubcount", 0);\r
+                        datemap.put("" + feed, feedmap);\r
+                    }\r
+                    String type = rs.getString("TYPE");\r
+                    if (type.equals("pub")) {\r
+                        try {\r
+                            int n = feedmap.getInt("pubcount");\r
+                            feedmap.put("pubcount", n + 1);\r
+                        } catch (JSONException e) {\r
+                            feedmap.put("pubcount", 1);\r
+                            logger.error(e.toString());\r
                         }\r
-                        String type = rs.getString("TYPE");\r
-                        if (type.equals("pub")) {\r
-                            try {\r
-                                int n = feedmap.getInt("pubcount");\r
-                                feedmap.put("pubcount", n + 1);\r
-                            } catch (JSONException e) {\r
-                                feedmap.put("pubcount", 1);\r
-                                logger.error(e.toString());\r
-                            }\r
-                        } else if (type.equals("del")) {\r
-                            String subid = "" + rs.getInt("DELIVERY_SUBID");\r
-                            try {\r
-                                int n = feedmap.getInt(subid);\r
-                                feedmap.put(subid, n + 1);\r
-                            } catch (JSONException e) {\r
-                                feedmap.put(subid, 1);\r
-                                logger.error(e.toString());\r
-                            }\r
+                    } else if (type.equals("del")) {\r
+                        String subid = "" + rs.getInt("DELIVERY_SUBID");\r
+                        try {\r
+                            int n = feedmap.getInt(subid);\r
+                            feedmap.put(subid, n + 1);\r
+                        } catch (JSONException e) {\r
+                            feedmap.put(subid, 1);\r
+                            logger.error(e.toString());\r
                         }\r
                     }\r
                 }\r
             }\r
-             db.release(conn);\r
         } catch (SQLException e) {\r
             logger.error(e.toString());\r
         }\r
@@ -225,11 +211,11 @@ public class FeedReport extends ReportBase {
             String[] feeds = JSONObject.getNames(j2);\r
             Arrays.sort(feeds);\r
             s.append("<tr><td rowspan=\"" + rc1 + "\">")\r
-                    .append(date)\r
-                    .append("</td>");\r
+                .append(date)\r
+                .append("</td>");\r
             s.append("<td rowspan=\"" + rc1 + "\">")\r
-                    .append(feeds.length)\r
-                    .append("</td>");\r
+                .append(feeds.length)\r
+                .append("</td>");\r
             String px1 = "";\r
             for (String feed : feeds) {\r
                 JSONObject j3 = j2.getJSONObject(feed);\r
@@ -239,15 +225,15 @@ public class FeedReport extends ReportBase {
                 String[] subs = JSONObject.getNames(j3);\r
                 Arrays.sort(subs);\r
                 s.append(px1)\r
-                        .append("<td rowspan=\"" + rc2 + "\">")\r
-                        .append(feed)\r
-                        .append("</td>");\r
+                    .append("<td rowspan=\"" + rc2 + "\">")\r
+                    .append(feed)\r
+                    .append("</td>");\r
                 s.append("<td rowspan=\"" + rc2 + "\">")\r
-                        .append(pubcount)\r
-                        .append("</td>");\r
+                    .append(pubcount)\r
+                    .append("</td>");\r
                 s.append("<td rowspan=\"" + rc2 + "\">")\r
-                        .append(subcnt)\r
-                        .append("</td>");\r
+                    .append(subcnt)\r
+                    .append("</td>");\r
                 String px2 = "";\r
                 for (String sub : subs) {\r
                     if (!sub.equals("pubcount")) {\r