Sonar Blocker fixes in FeedReport.java 41/64441/1
authorAmaresh Kumar <kamaresh@in.ibm.com>
Tue, 4 Sep 2018 14:09:16 +0000 (19:39 +0530)
committerAmaresh Kumar <kamaresh@in.ibm.com>
Tue, 4 Sep 2018 14:09:24 +0000 (19:39 +0530)
https://sonar.onap.org/project/issues?fileUuids=AWHFaQitEU5FCDfKtnEQ&id=org.onap.dmaap.datarouter%3Aparent&resolved=false&severities=BLOCKER

Issue-ID: DMAAP-700
Change-Id: Ic6ba57c8a5d3bd28a010e0ab57fd8ae377a15f7c
Signed-off-by: Amaresh Kumar <kamaresh@in.ibm.com>i
datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/FeedReport.java

index d638aaa..4346a0e 100644 (file)
@@ -69,45 +69,43 @@ public class FeedReport extends ReportBase {
             DB db = new DB();\r
             @SuppressWarnings("resource")\r
             Connection conn = db.getConnection();\r
-            PreparedStatement ps = conn.prepareStatement(SELECT_SQL);\r
-//            ps.setLong(1, from);\r
-//            ps.setLong(2, to);\r
-            ResultSet rs = ps.executeQuery();\r
-            while (rs.next()) {\r
-                if (alg1) {\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
-                } else {\r
-                    String date = rs.getString("date");\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
-                    int count = rs.getInt("count");\r
-                    if (type.equals("pub")) {\r
-                        feedmap.put("pubcount", count);\r
-                    } else if (type.equals("del")) {\r
-                        String subid = "" + rs.getInt("DELIVERY_SUBID");\r
-                        feedmap.put(subid, count);\r
-                    }\r
-                }\r
-            }\r
-            rs.close();\r
-            ps.close();\r
-            db.release(conn);\r
+           try( PreparedStatement ps = conn.prepareStatement(SELECT_SQL)) {\r
+               try (ResultSet rs = ps.executeQuery()) {\r
+                   while (rs.next()) {\r
+                       if (alg1) {\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
+                       } else {\r
+                           String date = rs.getString("date");\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
+                           int count = rs.getInt("count");\r
+                           if (type.equals("pub")) {\r
+                               feedmap.put("pubcount", count);\r
+                           } else if (type.equals("del")) {\r
+                               String subid = "" + rs.getInt("DELIVERY_SUBID");\r
+                               feedmap.put(subid, count);\r
+                           }\r
+                       }\r
+                   }\r
+               }\r
+           }\r
+        db.release(conn);\r
         } catch (SQLException e) {\r
             e.printStackTrace();\r
         }\r
@@ -134,47 +132,47 @@ public class FeedReport extends ReportBase {
             DB db = new DB();\r
             @SuppressWarnings("resource")\r
             Connection conn = db.getConnection();\r
-            PreparedStatement ps = conn.prepareStatement(SELECT_SQL_OLD);\r
-            ps.setLong(1, from);\r
-            ps.setLong(2, to);\r
-            ps.setFetchSize(100000);\r
-            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
-                    }\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
+            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
+                        }\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
+                            }\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
+                            }\r
+                        }\r
                     }\r
                 }\r
             }\r
-            rs.close();\r
-            ps.close();\r
-            db.release(conn);\r
+             db.release(conn);\r
         } catch (SQLException e) {\r
             e.printStackTrace();\r
         }\r
@@ -343,59 +341,59 @@ public class FeedReport extends ReportBase {
         }\r
         try {\r
             JSONObject jo = new JSONObject();\r
-            LineNumberReader lr = new LineNumberReader(new FileReader(infile));\r
-            String line = lr.readLine();\r
-            while (line != null) {\r
-                String[] tt = line.split(",");\r
-                if (tt[0].startsWith("2")) {\r
-                    String date = tt[0];\r
-                    switch (rtype) {\r
-                        case 1:\r
-                            String[] xx = date.split("-");\r
-                            Calendar cal = new GregorianCalendar(new Integer(xx[0]), new Integer(xx[1]) - 1, new Integer(xx[2]));\r
-                            date = xx[0] + "-W" + cal.get(Calendar.WEEK_OF_YEAR);\r
-                            break;\r
-                        case 2:\r
-                            date = date.substring(0, 7);\r
-                            break;\r
-                        case 3:\r
-                            date = date.substring(0, 4);\r
-                            break;\r
-                    }\r
-                    JSONObject datemap = jo.optJSONObject(date);\r
-                    if (datemap == null) {\r
-                        datemap = new JSONObject();\r
-                        jo.put(date, datemap);\r
-                    }\r
-                    int feed = Integer.parseInt(tt[2]);\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 = tt[1];\r
-                    int count = Integer.parseInt(tt[4]);\r
-                    if (type.equals("pub")) {\r
-                        try {\r
-                            int n = feedmap.getInt("pubcount");\r
-                            feedmap.put("pubcount", n + count);\r
-                        } catch (JSONException e) {\r
-                            feedmap.put("pubcount", count);\r
+            try(LineNumberReader lr = new LineNumberReader(new FileReader(infile))) {\r
+                String line = lr.readLine();\r
+                while (line != null) {\r
+                    String[] tt = line.split(",");\r
+                    if (tt[0].startsWith("2")) {\r
+                        String date = tt[0];\r
+                        switch (rtype) {\r
+                            case 1:\r
+                                String[] xx = date.split("-");\r
+                                Calendar cal = new GregorianCalendar(new Integer(xx[0]), new Integer(xx[1]) - 1, new Integer(xx[2]));\r
+                                date = xx[0] + "-W" + cal.get(Calendar.WEEK_OF_YEAR);\r
+                                break;\r
+                            case 2:\r
+                                date = date.substring(0, 7);\r
+                                break;\r
+                            case 3:\r
+                                date = date.substring(0, 4);\r
+                                break;\r
+                        }\r
+                        JSONObject datemap = jo.optJSONObject(date);\r
+                        if (datemap == null) {\r
+                            datemap = new JSONObject();\r
+                            jo.put(date, datemap);\r
+                        }\r
+                        int feed = Integer.parseInt(tt[2]);\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
-                    } else if (type.equals("del")) {\r
-                        String subid = tt[3];\r
-                        try {\r
-                            int n = feedmap.getInt(subid);\r
-                            feedmap.put(subid, n + count);\r
-                        } catch (JSONException e) {\r
-                            feedmap.put(subid, count);\r
+                        String type = tt[1];\r
+                        int count = Integer.parseInt(tt[4]);\r
+                        if (type.equals("pub")) {\r
+                            try {\r
+                                int n = feedmap.getInt("pubcount");\r
+                                feedmap.put("pubcount", n + count);\r
+                            } catch (JSONException e) {\r
+                                feedmap.put("pubcount", count);\r
+                            }\r
+                        } else if (type.equals("del")) {\r
+                            String subid = tt[3];\r
+                            try {\r
+                                int n = feedmap.getInt(subid);\r
+                                feedmap.put(subid, n + count);\r
+                            } catch (JSONException e) {\r
+                                feedmap.put(subid, count);\r
+                            }\r
                         }\r
                     }\r
+                    line = lr.readLine();\r
                 }\r
-                line = lr.readLine();\r
             }\r
-            lr.close();\r
             String t = toHTML(jo);\r
             switch (rtype) {\r
                 case 1:\r