View Javadoc

1   package com.explosion.datastream.exql.gui.table.editandrender;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.sql.Types;
24  import java.text.NumberFormat;
25  import java.text.ParseException;
26  import java.text.SimpleDateFormat;
27  import java.util.Date;
28  
29  import org.apache.log4j.LogManager;
30  import org.apache.log4j.Logger;
31  
32  import com.explosion.expfmodules.rdbmsconn.dbom.DBEntityColumn;
33  import com.explosion.expfmodules.rdbmsconn.dbom.utils.InvalidDataException;
34  import com.explosion.utilities.exception.ExceptionManagerFactory;
35  
36  
37  /***
38   * @author Steve.Cowx
39   */
40  public class EditorAndRenderFactory {
41  
42      private static Logger log = LogManager.getLogger(EditorAndRenderFactory.class);
43      
44  	/***
45       * Date time format for dates "dd/MM/yyyy HH:mm:ss"
46       */
47      public static final String DATE_TIME_FORMAT = "dd/MM/yyyy HH:mm:ss";
48      
49  	public static SimpleDateFormat format = new SimpleDateFormat(DATE_TIME_FORMAT);
50  	
51  	/***
52       * A general number format instance, it's a heavy weight, i only want one
53       */
54      private static NumberFormat generalNumberFormat = NumberFormat.getNumberInstance();
55      
56      /***
57       * A number format instance, it's a heavy weight, i only want one
58       */
59      private static NumberFormat numberFormat = NumberFormat.getNumberInstance();
60      
61      /***
62       * Converts the data item into a renderable String (if possible)
63       * If the value is null, it returns an empty string
64       * USeful for dates/times and datetimes so far.
65       * @param data
66       * @param text
67       * @param dbColumn
68       * @return
69       */
70      public static String renderValueForColumn(Object data, DBEntityColumn dbColumn) {
71          if (data == null)
72              return "";
73          
74          try {
75              if (dbColumn != null)
76              {
77                //log.debug("Rendering object " + data + " of type " + data.getClass().getName());
78                switch (dbColumn.getType())
79                {
80                    case (Types.DATE):
81                    case (Types.TIMESTAMP):
82                    case (Types.TIME):
83                        return format.format(data);
84                    case (java.sql.Types.BIGINT):
85      	          case (java.sql.Types.SMALLINT):
86      	          case (java.sql.Types.TINYINT):
87      	          case (java.sql.Types.INTEGER):
88      	          case (java.sql.Types.REAL):
89      	          case (java.sql.Types.DECIMAL):
90      	          case (java.sql.Types.DOUBLE):
91      	          case (java.sql.Types.FLOAT):
92      	          case (java.sql.Types.NUMERIC):
93      	          	//The number formats suck so I am returning it as is
94      	          	return data.toString();
95                }
96              }
97          } catch (Throwable e) {
98              ExceptionManagerFactory.getExceptionManager().manageException(e,"Exception caught while rendering data value " + data + " of type " + data.getClass().getName());
99              return null;
100         }
101         
102         return data.toString(); 
103     }
104     
105     /***
106      * Converts the data item into a renderable String (if possible)
107      * If the value is null, it returns an empty string
108      * USeful for dates/times and datetimes so far.
109      * @param data
110      * @param text
111      * @param dbColumn
112      * @return
113      * @throws ParseException
114      * @throws InvalidDataException
115      */
116     public static Object convertIntoCorrectTypeForColumn(Object data, DBEntityColumn dbColumn) throws InvalidDataException {
117         if (data == null || data.equals("NULL"))
118             return null;
119         
120 	    if (dbColumn != null)
121 	    {
122 	      switch (dbColumn.getType())
123 	      {
124 	          case (Types.DATE):
125 	              if (data instanceof String)
126 	                 return parseDate((String)data);
127 	              else
128 	                 throw new InvalidDataException("Cant convert " + data + " of type " + data.getClass().getName() + " into a date.");
129 	          
130 	          case (Types.TIMESTAMP):
131 	              if (data instanceof String)
132 	                  return parseDate((String)data);
133 	              else
134 	                  throw new InvalidDataException("Cant convert " + data + " of type " + data.getClass().getName() + " into a date.");
135 	              
136 	          case (Types.TIME):
137 	              if (data instanceof String)
138 	                  return parseDate((String)data);
139 	              else
140 	                   throw new InvalidDataException("Cant convert " + data + " of type " + data.getClass().getName() + " into a date.");
141 	          
142 	          case (java.sql.Types.BIGINT):
143 	          case (java.sql.Types.SMALLINT):
144 	          case (java.sql.Types.TINYINT):
145 	          case (java.sql.Types.INTEGER):
146                   if (data instanceof String)
147                       return new Integer((String)data);
148                   else
149                        throw new InvalidDataException("Cant convert " + data + " of type " + data.getClass().getName() + " into an integer.");
150 	          case (java.sql.Types.REAL):
151 	          case (java.sql.Types.DECIMAL):
152 	          case (java.sql.Types.DOUBLE):
153                   if (data instanceof String)
154                       return new Double((String)data);
155                   else
156                        throw new InvalidDataException("Cant convert " + data + " of type " + data.getClass().getName() + " into a double.");
157 	          case (java.sql.Types.FLOAT):
158                   if (data instanceof String)
159                       return new Float((String)data);
160                   else
161                        throw new InvalidDataException("Cant convert " + data + " of type " + data.getClass().getName() + " into a float.");
162 	          case (java.sql.Types.NUMERIC):
163                   if (data instanceof String)
164                       return new Integer((String)data);
165                   else
166                        throw new InvalidDataException("Cant convert " + data + " of type " + data.getClass().getName() + " into a number.");
167 	      }
168 	    }
169 	    
170         return data; 
171     }
172     
173     /***
174      * Parses a string and returns the value as a date if it can
175      * @param date
176      * @param format
177      * @return
178      */
179     private static Date parseDate(String data) throws InvalidDataException
180     {
181         try
182         {
183             if (data == null)
184                 return null;
185             
186             if (data.trim().length() < 1)
187                 return null;
188             
189             return format.parse((String)data);
190         }
191         catch (ParseException e)
192         {
193             throw new InvalidDataException("ParseException:  Try using the same format as this example '" + format.format(new Date())+"'.");
194         }
195     }
196     
197     /***
198      * Parses a string and returns the value as a date if it can
199      * @param date
200      * @param format
201      * @return
202      */
203     private static Number parseNumber(String data, NumberFormat format, Number example) throws InvalidDataException
204     {
205         try
206         {
207             if (data == null)
208                 return null;
209             
210             if (data.trim().length() < 1)
211                 return null;
212             
213             return format.parse((String)data);
214         }
215         catch (ParseException e)
216         {
217             throw new InvalidDataException("Invalid value for numeric column. Please provide a numeric value in the same format as this example  '" + format.format(example)+"'.");
218         }
219         
220     }
221 
222 }