1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package com.sun.star.report.pentaho.layoutprocessor;
24 
25 import com.sun.star.report.OfficeToken;
26 import com.sun.star.report.pentaho.OfficeNamespaces;
27 import com.sun.star.report.pentaho.model.FormattedTextElement;
28 import java.math.BigDecimal;
29 
30 import java.sql.Time;
31 
32 import java.text.SimpleDateFormat;
33 
34 import java.util.Date;
35 
36 import org.jfree.layouting.util.AttributeMap;
37 import org.jfree.report.DataFlags;
38 import org.jfree.report.DataSourceException;
39 import org.jfree.report.data.DefaultDataFlags;
40 import org.jfree.report.expressions.FormulaExpression;
41 import org.jfree.report.flow.FlowController;
42 import org.jfree.report.flow.layoutprocessor.LayoutControllerUtil;
43 
44 import org.pentaho.reporting.libraries.formula.util.HSSFDateUtil;
45 
46 /**
47  * Creation-Date: 06.06.2007, 17:03:30
48  *
49  * @author Thomas Morgner
50  */
51 public class FormatValueUtility
52 {
53 
54     private static final String BOOLEAN_VALUE = "boolean-value";
55     private static final String STRING_VALUE = "string-value";
56     public static final String VALUE_TYPE = "value-type";
57     public static final String VALUE = "value";
58     private static SimpleDateFormat dateFormat;
59     private static SimpleDateFormat timeFormat;
60 
FormatValueUtility()61     private FormatValueUtility()
62     {
63     }
64 
applyValueForVariable(final Object value, final AttributeMap variableSection)65     public static String applyValueForVariable(final Object value, final AttributeMap variableSection)
66     {
67         String ret = null;
68         if (value instanceof Time)
69         {
70             variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, VALUE_TYPE, "time");
71             ret = formatTime((Time) value);
72             variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, "time-value", ret);
73         }
74         else if (value instanceof java.sql.Date)
75         {
76             variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, VALUE_TYPE, "date");
77             ret = formatDate((Date) value);
78             variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, "date-value", ret);
79         }
80         else if (value instanceof Date)
81         {
82             variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, VALUE_TYPE, "float");
83             ret = HSSFDateUtil.getExcelDate((Date) value, false, 2).toString();
84             variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, VALUE, ret);
85         }
86         else if (value instanceof Number)
87         {
88             variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, VALUE_TYPE, "float");
89             variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, VALUE, String.valueOf(value));
90         }
91         else if (value instanceof Boolean)
92         {
93             variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, VALUE_TYPE, "boolean");
94             if (Boolean.TRUE.equals(value))
95             {
96                 variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, BOOLEAN_VALUE, OfficeToken.TRUE);
97             }
98             else
99             {
100                 variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, BOOLEAN_VALUE, OfficeToken.FALSE);
101             }
102         }
103         else if (value != null)
104         {
105             variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, VALUE_TYPE, "string");
106             variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, STRING_VALUE, String.valueOf(value));
107         }
108         else
109         {
110             variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, VALUE_TYPE, "string");
111             variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, STRING_VALUE, "");
112         }
113         return ret;
114     }
115 
applyValueForCell(final Object value, final AttributeMap variableSection, final String valueType)116     public static void applyValueForCell(final Object value, final AttributeMap variableSection, final String valueType)
117     {
118         if (value instanceof Time)
119         {
120             variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, "time-value", formatTime((Time) value));
121         }
122         else if (value instanceof java.sql.Date)
123         {
124             if ("float".equals(valueType))//@see http://qa.openoffice.org/issues/show_bug.cgi?id=108954
125             {
126                 variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, VALUE, HSSFDateUtil.getExcelDate((Date) value, false, 2).toString());
127             }
128             else
129             {
130                 variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, "date-value", formatDate((Date) value));
131             }
132         }
133         else if (value instanceof Date)
134         {
135             variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, VALUE_TYPE, "float");
136             variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, VALUE, HSSFDateUtil.getExcelDate((Date) value, false, 2).toString());
137         }
138         else if (value instanceof BigDecimal)
139         {
140             if ("date".equals(valueType))
141             {
142                 variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, "date-value", formatDate(HSSFDateUtil.getJavaDate((BigDecimal) value, false, 0)));
143             }
144             else
145             {
146                 variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, VALUE, String.valueOf(value));
147             }
148         }
149         else if (value instanceof Number)
150         {
151             variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, VALUE, String.valueOf(value));
152         }
153         else if (value instanceof Boolean)
154         {
155             if ("float".equals(valueType))
156             {
157                 float fvalue = Boolean.TRUE.equals(value) ? 1 : 0;
158                 variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, VALUE, String.valueOf(fvalue));
159             }
160             else
161             {
162                 if (Boolean.TRUE.equals(value))
163                 {
164                     variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, BOOLEAN_VALUE, OfficeToken.TRUE);
165                 }
166                 else
167                 {
168                     variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, BOOLEAN_VALUE, OfficeToken.FALSE);
169                 }
170             }
171         }
172         else if (value != null)
173         {
174             try
175             {
176                 final Float number = Float.valueOf(String.valueOf(value));
177                 applyValueForCell(number, variableSection, valueType);
178                 return;
179             }
180             catch (NumberFormatException e)
181             {
182             }
183             if (!"string".equals(valueType))
184             {
185                 variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, VALUE_TYPE, "string");
186                 //variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, VALUE, String.valueOf(value));
187             }
188             variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, STRING_VALUE, String.valueOf(value));
189         }
190         else
191         {
192             variableSection.setAttribute(OfficeNamespaces.OFFICE_NS, STRING_VALUE, "");
193         }
194     }
195 
formatDate(final Date date)196     private static synchronized String formatDate(final Date date)
197     {
198         if (dateFormat == null)
199         {
200             dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'S'Z'");
201         }
202         return dateFormat.format(date);
203     }
204 
formatTime(final Date date)205     private static synchronized String formatTime(final Date date)
206     {
207         if (timeFormat == null)
208         {
209             timeFormat = new SimpleDateFormat("'PT'HH'H'mm'M'ss'S'");
210         }
211         return timeFormat.format(date);
212     }
213 
computeDataFlag(final FormattedTextElement element, final FlowController flowController)214     public static DataFlags computeDataFlag(final FormattedTextElement element,
215             final FlowController flowController)
216             throws DataSourceException
217     {
218         // here it is relatively easy. We have to evaluate the expression, convert
219         // the result into a string, and print that string.
220         final FormulaExpression formulaExpression = element.getValueExpression();
221         final Object result = LayoutControllerUtil.evaluateExpression(flowController, element, formulaExpression);
222         if (result == null)
223         {
224             // ignore it. Ignoring it is much better than printing 'null'.
225             // LOGGER.debug("Formula '" + formulaExpression.getFormula() + "' evaluated to null.");
226             return null;
227         }
228         else if (result instanceof DataFlags)
229         {
230             return (DataFlags) result;
231         }
232         else
233         {
234             return new DefaultDataFlags(null, result, true);
235         }
236     }
237 }
238