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.engine.base;
25
26 import java.io.IOException;
27 import java.io.ObjectInputStream;
28 import java.io.Serializable;
29
30 import net.sf.jasperreports.engine.JRConstants;
31 import net.sf.jasperreports.engine.JRExpression;
32 import net.sf.jasperreports.engine.JRGroup;
33 import net.sf.jasperreports.engine.JRRuntimeException;
34 import net.sf.jasperreports.engine.JRVariable;
35 import net.sf.jasperreports.engine.design.events.JRChangeEventsSupport;
36 import net.sf.jasperreports.engine.design.events.JRPropertyChangeSupport;
37 import net.sf.jasperreports.engine.type.CalculationEnum;
38 import net.sf.jasperreports.engine.type.IncrementTypeEnum;
39 import net.sf.jasperreports.engine.type.ResetTypeEnum;
40 import net.sf.jasperreports.engine.util.CloneStore;
41 import net.sf.jasperreports.engine.util.JRClassLoader;
42 import net.sf.jasperreports.engine.util.JRCloneUtils;
43 import net.sf.jasperreports.engine.util.StoreCloneable;
44
45
46 /**
47  * @author Teodor Danciu (teodord@users.sourceforge.net)
48  */

49 public class JRBaseVariable implements JRVariable, Serializable, StoreCloneable<JRBaseVariable>, JRChangeEventsSupport
50 {
51
52
53     /**
54      *
55      */

56     private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
57     
58     public static final String PROPERTY_DESCRIPTION = "description";
59
60     /**
61      *
62      */

63     protected String name;
64     protected String description;
65     protected String valueClassName = java.lang.String.class.getName();
66     protected String valueClassRealName;
67     protected String incrementerFactoryClassName;
68     protected String incrementerFactoryClassRealName;
69     protected ResetTypeEnum resetTypeValue = ResetTypeEnum.REPORT;
70     protected IncrementTypeEnum incrementTypeValue = IncrementTypeEnum.NONE;
71     protected CalculationEnum calculationValue = CalculationEnum.NOTHING;
72     protected boolean isSystemDefined;
73
74     protected transient Class<?> valueClass;
75     protected transient Class<?> incrementerFactoryClass;
76
77     /**
78      *
79      */

80     protected JRExpression expression;
81     protected JRExpression initialValueExpression;
82     protected JRGroup resetGroup;
83     protected JRGroup incrementGroup;
84
85
86     /**
87      *
88      */

89     protected JRBaseVariable()
90     {
91     }
92     
93     
94     /**
95      *
96      */

97     protected JRBaseVariable(JRVariable variable, JRBaseObjectFactory factory)
98     {
99         factory.put(variable, this);
100         
101         name = variable.getName();
102         description = variable.getDescription();
103         valueClassName = variable.getValueClassName();
104         incrementerFactoryClassName = variable.getIncrementerFactoryClassName();
105         resetTypeValue = variable.getResetTypeValue();
106         incrementTypeValue = variable.getIncrementTypeValue();
107         calculationValue = variable.getCalculationValue();
108         isSystemDefined = variable.isSystemDefined();
109         
110         expression = factory.getExpression(variable.getExpression());
111         initialValueExpression = factory.getExpression(variable.getInitialValueExpression());
112
113         resetGroup = factory.getGroup(variable.getResetGroup());
114         incrementGroup = factory.getGroup(variable.getIncrementGroup());
115     }
116         
117
118     @Override
119     public String getName()
120     {
121         return this.name;
122     }
123         
124     @Override
125     public String getDescription()
126     {
127         return this.description;
128     }
129         
130     @Override
131     public void setDescription(String description)
132     {
133         Object old = this.description;
134         this.description = description;
135         getEventSupport().firePropertyChange(PROPERTY_DESCRIPTION, old, this.description);
136     }
137     
138     @Override
139     public Class<?> getValueClass()
140     {
141         if (valueClass == null)
142         {
143             String className = getValueClassRealName();
144             if (className != null)
145             {
146                 try
147                 {
148                     valueClass = JRClassLoader.loadClassForName(className);
149                 }
150                 catch(ClassNotFoundException e)
151                 {
152                     throw new JRRuntimeException(e);
153                 }
154             }
155         }
156         
157         return valueClass;
158     }
159         
160     @Override
161     public String getValueClassName()
162     {
163         return valueClassName;
164     }
165
166     /**
167      *
168      */

169     private String getValueClassRealName()
170     {
171         if (valueClassRealName == null)
172         {
173             valueClassRealName = JRClassLoader.getClassRealName(valueClassName);
174         }
175         
176         return valueClassRealName;
177     }
178
179     @Override
180     public Class<?> getIncrementerFactoryClass()
181     {
182         if (incrementerFactoryClass == null)
183         {
184             String className = getIncrementerFactoryClassRealName();
185             if (className != null)
186             {
187                 try
188                 {
189                     incrementerFactoryClass = JRClassLoader.loadClassForName(className);
190                 }
191                 catch(ClassNotFoundException e)
192                 {
193                     throw new JRRuntimeException(e);
194                 }
195             }
196         }
197         
198         return incrementerFactoryClass;
199     }
200         
201     @Override
202     public String getIncrementerFactoryClassName()
203     {
204         return incrementerFactoryClassName;
205     }
206
207     /**
208      *
209      */

210     private String getIncrementerFactoryClassRealName()
211     {
212         if (incrementerFactoryClassRealName == null)
213         {
214             incrementerFactoryClassRealName = JRClassLoader.getClassRealName(incrementerFactoryClassName);
215         }
216         
217         return incrementerFactoryClassRealName;
218     }
219
220     @Override
221     public ResetTypeEnum getResetTypeValue()
222     {
223         return this.resetTypeValue;
224     }
225         
226     @Override
227     public IncrementTypeEnum getIncrementTypeValue()
228     {
229         return this.incrementTypeValue;
230     }
231         
232     @Override
233     public CalculationEnum getCalculationValue()
234     {
235         return this.calculationValue;
236     }
237
238     @Override
239     public boolean isSystemDefined()
240     {
241         return this.isSystemDefined;
242     }
243
244     @Override
245     public JRExpression getExpression()
246     {
247         return this.expression;
248     }
249         
250     @Override
251     public JRExpression getInitialValueExpression()
252     {
253         return this.initialValueExpression;
254     }
255         
256     @Override
257     public JRGroup getResetGroup()
258     {
259         return this.resetGroup;
260     }
261         
262     @Override
263     public JRGroup getIncrementGroup()
264     {
265         return this.incrementGroup;
266     }
267
268     @Override
269     public Object clone() 
270     {
271         JRBaseVariable clone = null;
272         
273         try
274         {
275             clone = (JRBaseVariable)super.clone();
276         }
277         catch (CloneNotSupportedException e)
278         {
279             throw new JRRuntimeException(e);
280         }
281
282         clone.expression = JRCloneUtils.nullSafeClone(expression);
283         clone.initialValueExpression = JRCloneUtils.nullSafeClone(initialValueExpression);
284         clone.eventSupport = null;
285         
286         return clone;
287     }
288
289     @Override
290     public JRBaseVariable clone(CloneStore cloneStore)
291     {
292         JRBaseVariable clone = (JRBaseVariable) clone();
293         //early store for circular dependencies
294         cloneStore.store(this, clone);
295         clone.resetGroup = cloneStore.clone(resetGroup);
296         clone.incrementGroup = cloneStore.clone(incrementGroup);
297         return clone;
298     }
299
300     
301     private transient JRPropertyChangeSupport eventSupport;
302     
303     @Override
304     public JRPropertyChangeSupport getEventSupport()
305     {
306         synchronized (this)
307         {
308             if (eventSupport == null)
309             {
310                 eventSupport = new JRPropertyChangeSupport(this);
311             }
312         }
313         
314         return eventSupport;
315     }
316
317     /*
318      * These fields are only for serialization backward compatibility.
319      */

320     private int PSEUDO_SERIAL_VERSION_UID = JRConstants.PSEUDO_SERIAL_VERSION_UID_3_7_2; //NOPMD
321     /**
322      * @deprecated
323      */

324     private byte resetType;
325     /**
326      * @deprecated
327      */

328     private byte incrementType;
329     /**
330      * @deprecated
331      */

332     private byte calculation;
333     
334     @SuppressWarnings("deprecation")
335     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
336     {
337         in.defaultReadObject();
338
339         if (PSEUDO_SERIAL_VERSION_UID < JRConstants.PSEUDO_SERIAL_VERSION_UID_3_7_2)
340         {
341             resetTypeValue = ResetTypeEnum.getByValue(resetType);
342             incrementTypeValue = IncrementTypeEnum.getByValue(incrementType);
343             calculationValue = CalculationEnum.getByValue(calculation);
344         }
345         
346     }
347
348 }
349