View Javadoc

1   /*
2    * DynamicJasper: A library for creating reports dynamically by specifying
3    * columns, groups, styles, etc. at runtime. It also saves a lot of development
4    * time in many cases! (http://sourceforge.net/projects/dynamicjasper)
5    *
6    * Copyright (C) 2008  FDV Solutions (http://www.fdvsolutions.com)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   *
11   * License as published by the Free Software Foundation; either
12   *
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   *
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19   *
20   * Lesser General Public License for more details.
21   *
22   * You should have received a copy of the GNU Lesser General Public
23   * License along with this library; if not, write to the Free Software
24   *
25   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
26   *
27   *
28   */
29  
30  package ar.com.fdvs.dj.domain.entities.columns;
31  
32  import java.util.Collection;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  import ar.com.fdvs.dj.domain.CustomExpression;
38  import ar.com.fdvs.dj.domain.DJCalculation;
39  import ar.com.fdvs.dj.domain.entities.Entity;
40  import ar.com.fdvs.dj.util.ExpressionUtils;
41  
42  /**
43   * Column created to handle Custom Expressions.</br>
44   * @see CustomExpression
45   */
46  public class ExpressionColumn extends SimpleColumn {
47  
48  	private static final long serialVersionUID = Entity.SERIAL_VERSION_UID;
49  	
50  	private static final Log log = LogFactory.getLog(ExpressionColumn.class);
51  
52  	private CustomExpression expression; //for showing
53  	private CustomExpression expressionForCalculation; //for calculation
54  
55  	private Collection columns;
56  	private Collection variables; //List of JRVariables
57  
58  	private String calculatedExpressionText = null;
59  
60  	protected String getCalculatedExpressionText() {
61  		return calculatedExpressionText;
62  	}
63  
64  	protected void setCalculatedExpressionText(String calculatedExpressionText) {
65  		this.calculatedExpressionText = calculatedExpressionText;
66  	}
67  
68  	public Collection getVariables() {
69  		return variables;
70  	}
71  
72  	public void setVariables(Collection variables) {
73  		this.variables = variables;
74  	}
75  
76  	public Collection getColumns() {
77  		return columns;
78  	}
79  
80  	public void setColumns(Collection columns) {
81  		this.columns = columns;
82  	}
83  
84  	public CustomExpression getExpression() {
85  		return expression;
86  	}
87  
88  	public void setExpression(CustomExpression expression) {
89  		this.expression = expression;
90  	}
91  
92  	public String getValueClassNameForExpression() {
93  		if (expression == null)
94  			return Object.class.getName();
95  
96  		return expression.getClassName();
97  	}
98  
99  	public String getTextForExpression() {
100 
101 		if (this.calculatedExpressionText != null)
102 			return this.calculatedExpressionText;
103 
104 		String stringExpression = ExpressionUtils.createCustomExpressionInvocationText(expression, getColumnProperty().getProperty());
105 		
106 		log.debug("Expression for CustomExpression = " + stringExpression);
107 
108 		this.calculatedExpressionText = stringExpression;
109 		return stringExpression;
110 	}
111 
112 	public String getTextForExpressionForCalculartion() {
113 		
114 		String stringExpression = ExpressionUtils.createCustomExpressionInvocationText(expressionForCalculation, getColumnProperty().getProperty()+"_calc");
115 		
116 		log.debug("Calculation Expression for CustomExpression = " + stringExpression);
117 		
118 		return stringExpression;
119 	}
120 
121 	public CustomExpression getExpressionForCalculation() {
122 		return expressionForCalculation;
123 	}
124 
125 	public void setExpressionForCalculation(
126 			CustomExpression expressionForCalculation) {
127 		this.expressionForCalculation = expressionForCalculation;
128 	}
129 	
130 	public String getVariableClassName(DJCalculation op) {
131 		if (op == DJCalculation.COUNT || op == DJCalculation.DISTINCT_COUNT)
132 			return Long.class.getName();
133 		
134 		if (expressionForCalculation != null)
135 			return expressionForCalculation.getClassName();
136 		
137 		if (expression != null)
138 			return expression.getClassName();
139 		
140 		return super.getVariableClassName(op);
141 			
142 	}	
143 	
144 	public String getInitialExpression(DJCalculation op) {
145 		if (op == DJCalculation.COUNT  || op == DJCalculation.DISTINCT_COUNT)
146 			return "new java.lang.Long(\"0\")";
147 		else if (op == DJCalculation.SUM) {
148 			
149 			return "new " + getVariableClassName(op) +"(\"0\")";
150 		}
151 		else return null;
152 	}
153 	
154 
155 }