1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24 package net.sf.jasperreports.parts.subreport;
25
26 import net.sf.jasperreports.engine.JRExpressionCollector;
27 import net.sf.jasperreports.engine.JRSubreportParameter;
28 import net.sf.jasperreports.engine.JRSubreportReturnValue;
29 import net.sf.jasperreports.engine.base.JRBaseObjectFactory;
30 import net.sf.jasperreports.engine.design.JRVerifier;
31 import net.sf.jasperreports.engine.part.PartComponent;
32 import net.sf.jasperreports.engine.part.PartComponentCompiler;
33
34 /**
35  * Compile-time handler of {@link SubreportPartComponent list component} instances.
36  * 
37  * @author Lucian Chirita (lucianc@users.sourceforge.net)
38  */

39 public class SubreportPartComponentCompiler implements PartComponentCompiler
40 {
41
42     @Override
43     public void collectExpressions(PartComponent component, JRExpressionCollector collector)
44     {
45         SubreportPartComponent subreport = (SubreportPartComponent) component;
46         
47         collector.addExpression(subreport.getParametersMapExpression());
48         
49         JRSubreportParameter[] parameters = subreport.getParameters();
50         if (parameters != null && parameters.length > 0)
51         {
52             for(int j = 0; j < parameters.length; j++)
53             {
54                 collector.addExpression(parameters[j].getExpression());
55             }
56         }
57
58         collector.addExpression(subreport.getExpression());
59     }
60
61     @Override
62     public PartComponent toCompiledComponent(PartComponent component,
63             JRBaseObjectFactory baseFactory)
64     {
65         SubreportPartComponent subreportComponent = (SubreportPartComponent) component;
66         StandardSubreportPartComponent compiledComponent = new StandardSubreportPartComponent(
67                 subreportComponent, baseFactory);
68         return compiledComponent;
69     }
70
71     @Override
72     public void verify(PartComponent component, JRVerifier verifier)
73     {
74         // largely copied from JRVerifier.verifySubreport
75         SubreportPartComponent subreportComponent = (SubreportPartComponent) component;
76         JRSubreportParameter[] parameters = subreportComponent.getParameters();
77         if (parameters != null && parameters.length > 0)
78         {
79             for(int index = 0; index < parameters.length; index++)
80             {
81                 JRSubreportParameter parameter = parameters[index];
82
83                 if (parameter.getName() == null || parameter.getName().trim().length() == 0)
84                 {
85                     verifier.addBrokenRule("Subreport part parameter name missing.", parameter);
86                 }
87             }
88         }
89
90         JRSubreportReturnValue[] returnValues = subreportComponent.getReturnValues();
91         if (returnValues != null && returnValues.length > 0)
92         {
93             for (int i = 0; i < returnValues.length; i++)
94             {
95                 JRSubreportReturnValue returnValue = returnValues[i];
96
97                 if (returnValue.getFromVariable() == null || returnValue.getFromVariable().trim().length() == 0)
98                 {
99                     verifier.addBrokenRule("Subreport part return value variable name missing.", returnValue);
100                 }
101
102                 if (returnValue.getToVariable() == null || returnValue.getToVariable().trim().length() == 0)
103                 {
104                     verifier.addBrokenRule("Subreport part return value to variable name missing.", returnValue);
105                 }
106
107                 if (!verifier.getReportDesign().getVariablesMap().containsKey(returnValue.getToVariable()))
108                 {
109                     verifier.addBrokenRule("Subreport part return value to variable not found.", returnValue);
110                 }
111             }
112         }
113     }
114
115 }
116