{"id":1199,"date":"2016-05-29T06:08:23","date_gmt":"2016-05-29T06:08:23","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1199"},"modified":"2016-05-30T13:44:17","modified_gmt":"2016-05-30T13:44:17","slug":"grouping-and-sub-grouping-summing","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/grouping-and-sub-grouping-summing\/","title":{"rendered":"Grouping and Sub Grouping Summing"},"content":{"rendered":"<p><strong>Rows as Displayed in Screen<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2016\/05\/GroupAddition_remove.png\" alt=\"\" height=\"473\" width=\"425\"\/><\/p>\n<p><strong>Rows from Database<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2016\/05\/GroupingRows.png\" alt=\"\" height=\"178\" width=\"518\"\/><\/p>\n<p><strong>Rows grouping Logic<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\tList&lt;RowsBean&gt; outputList = RowsToBeDisplayedList;\t\t\r\n\t\r\n\tHashMap&lt;String, ArrayList&gt;  superGroupPoints    = new HashMap&lt;String, ArrayList&gt;();\t\r\n\tHashMap&lt;String, EWMDecimal&gt; subGroupTotalPoints = new HashMap&lt;String, Integer&gt;();\t\r\n\t\r\n\tString groupName = null;\r\n\tString prevGroupName = null;\r\n\tString superGroupName = null;\r\n\tString prevSuperGroupName = null;\t\r\n\t\r\n\tList&lt;Map&gt; subGroupPointsList = new ArrayList();\t\r\n\t\r\n\t\r\n\t\/\/Compute grouping totals\r\n\tIterator itr = cicCarryPointsList.iterator();\r\n\t\r\n\t while(itr.hasNext()) \r\n\t {\r\n\t\tRowsBean rowBean = (RowsBean) itr.next();\t\t\r\n\t\tsuperGroupName   = rowBean.getSuperGroupName();\r\n\t\tgroupName \t\t = rowBean.getGroupName();\r\n\t\t\r\n  \t        \/\/Addition of points summed at Group Level\r\n\t\tif ((prevGroupName != null &amp;&amp; !prevGroupName.equals(groupName))\r\n\t\t\t\t|| (prevSuperGroupName != null &amp;&amp;    !prevSuperGroupName.equals(superGroupName)))\r\n\t\t{\r\n\t\t   subGroupPointsList.add(subGroupTotalPoints);\r\n\t\t   subGroupTotalPoints = new HashMap&lt;String, Integer&gt;();\r\n\t\t}\r\n\t\t\r\n\t\t\r\n\t\t\/\/Rows at GroupLevel with Sub Groups should be added only when Super Group Changes \r\n\t\tif((prevSuperGroupName != null &amp;&amp; !prevSuperGroupName.equals(superGroupName)))\r\n\t\t{\t\t\t   \r\n\t\t   superGroupPoints.put(prevSuperGroupName, (ArrayList) subGroupPointsList);\r\n\t\t   subGroupPointsList = new ArrayList();\r\n\t\t}\r\n\t\t\r\n\t\tInteger subGroupValPoints = rowBean.getPoints();\t\t    \r\n\r\n\t\t\/\/If InvGrp level Map exists \r\n\t\tif (subGroupTotalPoints.get(groupName) != null) {\r\n\t\t\tInteger currSummedPointsAtGrpLevel = subGroupTotalPoints.get(groupName);\r\n\t\t\tcurrSummedPointsAtGrpLevel = currSummedPointsAtGrpLevel.add(subGroupValPoints);\r\n\t\t\tsubGroupTotalPoints.put(groupName, currSummedPointsAtGrpLevel);\t\t\t\t\r\n\t\t} else {\r\n\t\t\tsubGroupTotalPoints.put(groupName, subGroupValPoints);\r\n\t\t}\r\n\t\t\r\n\t\t\/\/Incase of last element the loop exits without adding last summed element\r\n\t\t\/\/To prevent that we add it with out current and prev comparison\r\n\t\tif(!itr.hasNext())\r\n\t\t{\r\n\t\t\tsubGroupPointsList.add(subGroupTotalPoints);\t\t\t\t\r\n\t\t\tsuperGroupPoints.put(superGroupName, (ArrayList) subGroupPointsList);\r\n\t\t}\r\n\t\t\r\n\t\tprevSuperGroupName = superGroupName;\r\n\t\tprevGroupName = groupName;\r\n\t}\r\n<\/pre>\n<p><b>Retrieval of Rows for Displaying in Screen<\/b><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nString currentSuperGrouping = &quot;&quot;;\r\nString prevSuperGrouping    = &quot;&quot;;\r\nString currGrouping = &quot;&quot;;\r\nString prevGrouping = &quot;&quot;;\r\nString Value = &quot;&quot;;\r\n \r\nfor (RowsBean rowBean : outputList)\r\n{\r\n\tcurrentSuperGrouping = rowBean.getSuperGroupName();\r\n\tcurrGrouping = rowBean.getGroupName();\r\n\r\n    \/\/Level 1 - New Super Group Creation \r\n\t\/\/New Super Group should be created when ever prevSuperGrouping and  currentSuperGrouping are different\r\n    if (!currentSuperGrouping.equals(prevSuperGrouping))\r\n    {\r\n      .\r\n      .  \r\n      Super Group Row Creation HTML Code Goes Here\r\n      .\r\n      .\r\n      .\r\n    }\r\n     \r\n    \/\/Level 2 - Group addition under Super Group\r\n\t\/\/New Group should be created when ever SuperGroup or Group Changes\r\n    if(!currGrouping.equals(prevGrouping) || !currentSuperGrouping.equals(prevSuperGrouping))\r\n    {\r\n      \/\/Taking Group Level Maps List\r\n      ArrayList GroupLevelMapList = superGroup.get(rowBean.getGroupName());\r\n      Iterator  itr =  GroupLevelMapList.iterator();\r\n       \r\n\t  \/\/Taking the Summed up value at Group Level from List\r\n      while (itr.hasNext())\r\n      {\r\n         Map ii = (Map) itr.next();\r\n         Points = ii.get(rowBean.getGroupName());      \r\n      }  \r\n       \r\n      .\r\n      .  \r\n      Group Row Creation HTML Code Goes Here\r\n      .\r\n      .\r\n      .\r\n    }\r\n     \r\n    \/\/Level 3 - Sub Group Rows Addition\r\n\t\/\/Rows will be added \r\n    if(currentSuperGrouping.equals(rowBean.getGroupName) || currentSuperGrouping.equals(rowBean.getSubGroupName))\r\n    {\r\n      .\r\n      .  \r\n      Sub Group Row Creation HTML Code Goes Here\r\n      .\r\n      .\r\n      .\r\n    } \r\n     \r\n    prevSuperGrouping    = currentSuperGrouping;\r\n    prevGrouping = currGrouping;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Rows as Displayed in Screen Rows from Database Rows grouping Logic List&lt;RowsBean&gt; outputList = RowsToBeDisplayedList; HashMap&lt;String, ArrayList&gt; superGroupPoints = new HashMap&lt;String, ArrayList&gt;(); HashMap&lt;String, EWMDecimal&gt; subGroupTotalPoints = new HashMap&lt;String, Integer&gt;(); String groupName = null; String prevGroupName = null; String superGroupName = null; String prevSuperGroupName = null; List&lt;Map&gt; subGroupPointsList = new ArrayList(); \/\/Compute grouping totals Iterator itr&hellip; <a href=\"https:\/\/codethataint.com\/blog\/grouping-and-sub-grouping-summing\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[65],"tags":[],"class_list":["post-1199","post","type-post","status-publish","format-standard","hentry","category-codesnippet"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1199"}],"version-history":[{"count":12,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1199\/revisions"}],"predecessor-version":[{"id":1214,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1199\/revisions\/1214"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}