Repeating subreports

Request for help on DynamicReports

Moderator: ricardo

Repeating subreports

Postby altay » Mon Mar 20, 2017 6:56 am

Code: Select all
package com.aselsan.rehis.gpy.service.scenario.content.imp.presenter.opkar.barragejammingscenariolist;

import static net.sf.dynamicreports.report.builder.DynamicReports.report;
import static net.sf.dynamicreports.report.builder.DynamicReports.type;

import java.util.Comparator;
import java.util.List;
import java.util.UUID;
import java.util.function.Function;

import com.aselsan.rehis.gpy.entity.ew.comm.opkar.BarrageJammingScenarioBase;
import com.aselsan.rehis.gpy.entity.ew.comm.opkar.BarrageJammingTargetBase;
import com.aselsan.rehis.gpy.entity.ew.comm.opkar.OpkarSbBase;
import com.aselsan.rehis.gpy.entity.ew.comm.opkar.rules.OpkarBarrageJammingRuleBuilder;
import com.aselsan.rehis.gpy.entity.ew.comm.opkar.rules.OpkarBarrageJammingScenarioRuleBuilder;
import com.aselsan.rehis.gpy.entity.ew.comm.opkar.rules.OpkarSbRuleBuilder;
import com.aselsan.rehis.gpy.entity.ew.scenario.comm.ScenarioOpkarBarrageJammingScenario;

import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.builder.DynamicReports;
import net.sf.dynamicreports.report.builder.component.SubreportBuilder;
import net.sf.dynamicreports.report.builder.group.CustomGroupBuilder;
import net.sf.dynamicreports.report.constant.GroupHeaderLayout;
import net.sf.dynamicreports.report.datasource.DRDataSource;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;

public class DynamicReportDataOpkarBarrageJammingScenarioList
{
   private static OpkarBarrageJammingScenarioRuleBuilder RULE_BUILDER =
         new OpkarBarrageJammingScenarioRuleBuilder();
   private static OpkarSbRuleBuilder RULE_BUILDER_SB = new OpkarSbRuleBuilder();
   private static OpkarBarrageJammingRuleBuilder RULE_BUILDER_TB =
         new OpkarBarrageJammingRuleBuilder();

   private static Function<BarrageJammingScenarioBase, UUID> mKeyBinder =
         (p) -> p.getOid();
   private static Comparator<UUID> mComparator = (k1, k2) -> k1.compareTo(k2);
   private static SubreportBuilder subreportSB;
   private static SubreportBuilder subreportTB;

   public DynamicReportDataOpkarBarrageJammingScenarioList()
   {
      // build(mainDataList);

   }

   public JasperReportBuilder build(
         List<ScenarioOpkarBarrageJammingScenario> mainDataList)

   {

      CustomGroupBuilder itemGroup =
            DynamicReports.grp.group("scenName", String.class).setTitle("")
                  .setTitleWidth(30).setHeaderLayout(GroupHeaderLayout.TITLE_AND_VALUE);

      JasperReportBuilder sd = null;
      sd = report()
            .setTemplate(
                  DynamicReportTemplateOpkarBarrageJammingScenarioList.reportTemplate)
            .columns(
                  DynamicReports.col.column(
                        RULE_BUILDER.getScenarioNameRule().getTitle(),
                        "scenName",
                        type.stringType()),
                  DynamicReports.col.column(
                        RULE_BUILDER.getTotalOpkarCountRule().getTitle(),
                        "opkarCount",
                        type.integerType()),
                  DynamicReports.col.column(
                        RULE_BUILDER.getTotalTargetCountRule().getTitle(),
                        "targetCount",
                        type.integerType()),
                  DynamicReports.col.column(
                        RULE_BUILDER.getOutputPowerRule().getTitle(),
                        "outputPower",
                        type.stringType()),
                  DynamicReports.col.column(
                        RULE_BUILDER.getDurationRule().getTitle(),
                        "duration",
                        type.integerType()));

      for (int i = 0; i < mainDataList.size(); i++)
      {
         ScenarioOpkarBarrageJammingScenario main = mainDataList.get(i);

         SubreportBuilder subreport =
               DynamicReports.cmp.subreport(subTB()).setDataSource(createDataTB(main));

         SubreportBuilder subreportOpkar =
               DynamicReports.cmp.subreport(subSB()).setDataSource(createDataSB(main));

         sd.detailFooter(
               DynamicReports.cmp.horizontalList(
                     DynamicReports.cmp.horizontalGap(150),
                     subreport,
                     DynamicReports.cmp.horizontalGap(150)),
               DynamicReports.cmp.line())
               .detailFooter(
                     DynamicReports.cmp.horizontalList(
                           DynamicReports.cmp.horizontalGap(150),
                           subreportOpkar,
                           DynamicReports.cmp.horizontalGap(150)),
                     DynamicReports.cmp.line());

      }
      sd.setDataSource(mainReport(mainDataList)).groupBy(itemGroup);
      return sd;
   }

   private JRDataSource mainReport(List<ScenarioOpkarBarrageJammingScenario> mainDataList)
   {

      DRDataSource dataSource = new DRDataSource(
            "scenName",
            "opkarCount",
            "targetCount",
            "outputPower",
            "duration");

      for (int i = 0; i < mainDataList.size(); i++)
      {
         ScenarioOpkarBarrageJammingScenario target = mainDataList.get(i);
         dataSource.add(
               target.getName(),
               target.getOpkarFieldUnits().size(),
               target.getBarrageJammingTargets().size(),
               target.getOutputPowerType().getValue(),
               target.getDuration()

         );

      }
      return dataSource;

   }

   private JRDataSource createDataSource(int x)
   {
      return new JREmptyDataSource(0);
   }

   public JasperReportBuilder subTB()// son güncel
                                     // metotlarin
                                     // bunlar
   {
      JasperReportBuilder subreport = report();

      subreport
            .setTemplate(
                  DynamicReportTemplateOpkarBarrageJammingScenarioList.reportTemplate)

            // .title(
            // DynamicReports.cmp.text(mai.get(0).getName()).setStyle(
            // DynamicReportTemplateOpkarBarrageJammingScenarioList.rootStyle))
            .columns(
                  DynamicReports.col.column(
                        RULE_BUILDER_TB.getFrequencyMinRule().getTitle(),
                        "minFreq",
                        type.doubleType()),
                  DynamicReports.col.column(
                        RULE_BUILDER_TB.getFrequencyMaxRule().getTitle(),
                        "maxFreq",
                        type.doubleType()),
                  DynamicReports.col.column(
                        RULE_BUILDER_TB.getOutputPowerRule().getTitle(),
                        "outPower",
                        type.stringType()));
      return subreport;

   }

   public JasperReportBuilder subSB()
   {
      JasperReportBuilder subreport = report();

      subreport
            .setTemplate(
                  DynamicReportTemplateOpkarBarrageJammingScenarioList.reportTemplate)
            .columns(
                  DynamicReports.col.column(
                        RULE_BUILDER_SB.getNameRule().getTitle(),
                        "id",
                        DynamicReports.type.stringType()),
                  DynamicReports.col.column(
                        RULE_BUILDER_SB.getOpkarIdRule().getTitle(),
                        "name",
                        DynamicReports.type.stringType()));

      return subreport;
   }

   public JRDataSource createDataSB(ScenarioOpkarBarrageJammingScenario main)// son
                                                                             // güncel
                                                                             // metotlarin
                                                                             // bunlar
   {
      DRDataSource dataSource = new DRDataSource("id", "name");

      // for (ScenarioOpkarBarrageJammingScenario op : main)
      // {
      for (OpkarSbBase sb : main.getOpkarFieldUnits())
      {
         dataSource.add(sb.getOpkarID(), sb.getName());
      }
      // }
      return dataSource;
   }

   public JRDataSource createDataTB(ScenarioOpkarBarrageJammingScenario main)// son
                                                                             // güncel
                                                                             // metotlarin
                                                                             // bunlar
   {

      DRDataSource dataSource = new DRDataSource("minFreq", "maxFreq", "outPower");
      // for (ScenarioOpkarBarrageJammingScenario sc : main)
      // {
      for (BarrageJammingTargetBase tb : main.getBarrageJammingTargets())
      {
         dataSource.add(
               tb.getStartFrequency(),
               tb.getEndFrequency(),
               tb.getOutputPowerType().displayName());
      }
      // }

      return dataSource;

   }

   //
}

Attachments
Capture.PNG
Capture.PNG (57.73 KiB) Viewed 16 times
Last edited by altay on Mon Mar 20, 2017 6:03 pm, edited 2 times in total.
altay
 
Posts: 2
Joined: Mon Mar 20, 2017 6:51 am

Re: Repeating subreports

Postby andreas.hackl » Mon Mar 20, 2017 2:17 pm

Hello!

First of all: please put source code inside Code-Tags, so we can actually read it.

I am not exactly sure what your problem is, so I'm guessing...

You are putting your subreports in the detailHeader band. Here is the javadoc for this method:
"Adds components to the detail header band. The band is printed for each record row in the data source and it's placed above the detail band."

If you want the supreport to be displayed only once, you can put it in the summary band.

Code: Select all
sd.summary(subreport);


Best regards,
Andy
andreas.hackl
 
Posts: 38
Joined: Tue Jan 28, 2014 4:47 pm

Re: Repeating subreports

Postby altay » Mon Mar 20, 2017 5:57 pm

I edited the post pls check again . thanks
altay
 
Posts: 2
Joined: Mon Mar 20, 2017 6:51 am

Re: Repeating subreports

Postby andreas.hackl » Tue Mar 21, 2017 6:50 am

I think the problem is that in your build function you loop through your dataset and build the subreports inside the loop.
Code: Select all
for (int i = 0; i < mainDataList.size(); i++)


You don't need this. Just define your subreports and put them into the band you want (detailFooter in this case).
JasperReports will know, how often the subreports has to be generated.
andreas.hackl
 
Posts: 38
Joined: Tue Jan 28, 2014 4:47 pm


Return to Help

Who is online

Users browsing this forum: Google [Bot] and 3 guests