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.wizards.db;
24 
25 import com.sun.star.beans.XPropertySet;
26 import com.sun.star.container.XNameAccess;
27 // import com.sun.star.lang.IllegalArgumentException;
28 import com.sun.star.sdbc.DataType;
29 import com.sun.star.uno.AnyConverter;
30 import com.sun.star.uno.Exception;
31 import com.sun.star.uno.UnoRuntime;
32 import com.sun.star.wizards.common.*;
33 
34 public class FieldColumn
35 {
36     protected int ColIndex;
37 
38     private Object DefaultValue;
39     private String m_sFieldName;
40     private String m_sDisplayFieldName;
41     private String FieldTitle;
42     private String m_sCommandName;
43     private int m_nDBFormatKey;
44     private int m_nFieldType;
45     private XPropertySet m_xColPropertySet;
46 
47     // field meta data
48     private int FieldWidth;
49     private int StandardFormatKey;
50     private boolean bIsNumberFormat;
51 
52     private static boolean bFormatKeysInitialized = false;
53     private static int iDateFormatKey;
54     private static int iDateTimeFormatKey;
55     private static int iNumberFormatKey;
56     private static int iTextFormatKey;
57     private static int iTimeFormatKey;
58     private static int iLogicalFormatKey;
59 
60     private CommandMetaData m_aCommandMetaData;
61 
FieldColumn(CommandMetaData oCommandMetaData, String _DisplayFieldName)62     public FieldColumn(CommandMetaData oCommandMetaData, String _DisplayFieldName)
63     {
64         m_sDisplayFieldName = _DisplayFieldName;
65         m_sCommandName = oCommandMetaData.getCommandName();
66         m_sFieldName = getOnlyFieldName(m_sDisplayFieldName, m_sCommandName);
67 // TODO: could be wrong here!
68 //        FieldTitle = _DisplayFieldName; // oCommandMetaData.getFieldTitle(m_sFieldName);
69         FieldTitle = m_sFieldName;
70         DBMetaData.CommandObject oTable = oCommandMetaData.getTableByName(m_sCommandName);
71         initializeFormatKeys(oCommandMetaData, oTable.getColumns());
72     }
73 
FieldColumn(CommandMetaData oCommandMetaData, String _FieldName, String _CommandName, boolean _bInstantiateByDisplayName)74     public FieldColumn(CommandMetaData oCommandMetaData, String _FieldName, String _CommandName, boolean _bInstantiateByDisplayName)
75     {
76         m_sCommandName = _CommandName;
77         if (_bInstantiateByDisplayName)
78         {
79             m_sDisplayFieldName = _FieldName;
80             m_sFieldName = getOnlyFieldName(_FieldName, _CommandName);
81         }
82         else
83         {
84             m_sFieldName = _FieldName;
85             m_sDisplayFieldName = composeDisplayFieldName(_CommandName, m_sFieldName);
86         }
87         FieldTitle = m_sFieldName;
88         m_aCommandMetaData = oCommandMetaData;
89     }
90 
FieldColumn(CommandMetaData oCommandMetaData, XNameAccess _xColumns, String _FieldName)91     public FieldColumn(CommandMetaData oCommandMetaData, XNameAccess _xColumns, String _FieldName)
92     {
93         m_sFieldName = _FieldName;
94 //        FieldTitle = m_sFieldName;
95         m_sDisplayFieldName = m_sFieldName;
96         ColIndex = JavaTools.FieldInList(_xColumns.getElementNames(), m_sFieldName) + 1;
97         initializeFormatKeys(oCommandMetaData, _xColumns);
98     }
99 
getFieldType()100     public int getFieldType()
101     {
102         if (m_nFieldType == 0)
103         {
104             DBMetaData.CommandObject oTable = m_aCommandMetaData.getTableByName(m_sCommandName);
105             initializeFormatKeys(m_aCommandMetaData, oTable.getColumns());
106         }
107         return m_nFieldType;
108     }
109 
getFieldWidth()110     public int getFieldWidth()
111     {
112         getFieldType(); // will collect meta data 'bout the column, if not already done so
113         return FieldWidth;
114     }
115 
getDBFormatKey()116     public int getDBFormatKey()
117     {
118         getFieldType(); // will collect meta data 'bout the column, if not already done so
119         return m_nDBFormatKey;
120     }
121 
getStandardFormatKey()122     public int getStandardFormatKey()
123     {
124         getFieldType(); // will collect meta data 'bout the column, if not already done so
125         return StandardFormatKey;
126     }
isNumberFormat()127     public boolean isNumberFormat()
128     {
129         getFieldType(); // will collect meta data 'bout the column, if not already done so
130         return bIsNumberFormat;
131     }
132 
133     /**
134      * Remove the pre name, we want the name after the 'dot'
135      * @param _DisplayFieldName
136      * @param _CommandName
137      * @return
138      */
getOnlyFieldName(String _DisplayFieldName, String _CommandName)139     private String getOnlyFieldName(String _DisplayFieldName, String _CommandName)
140     {
141         return _DisplayFieldName.substring(_CommandName.length() + 1, _DisplayFieldName.length());
142     }
143 
composeDisplayFieldName(String _sCommandName, String _sFieldName)144     public static String composeDisplayFieldName(String _sCommandName, String _sFieldName)
145     {
146         return _sCommandName + "." + _sFieldName;
147     }
148 
initializeFormatKeys(CommandMetaData oCommandMetaData, XNameAccess _xColumns)149     private void initializeFormatKeys(CommandMetaData oCommandMetaData, XNameAccess _xColumns)
150     {
151         try
152         {
153             m_xColPropertySet = UnoRuntime.queryInterface(XPropertySet.class, _xColumns.getByName(m_sFieldName));
154             ColIndex = JavaTools.FieldInList(_xColumns.getElementNames(), m_sFieldName) + 1;
155             m_nFieldType = AnyConverter.toInt(m_xColPropertySet.getPropertyValue("Type"));
156             getTyperelatedFieldData();
157 
158              if (!bFormatKeysInitialized)
159              {
160                 final NumberFormatter aNumberFormatter = oCommandMetaData.getNumberFormatter();
161 
162                 iDateFormatKey = aNumberFormatter.getDateFormatKey();
163                 iDateTimeFormatKey = aNumberFormatter.getDateTimeFormatKey();
164                 iNumberFormatKey = aNumberFormatter.getNumberFormatKey();
165                 iTextFormatKey = aNumberFormatter.getTextFormatKey();
166                 iTimeFormatKey = aNumberFormatter.getTimeFormatKey();
167                 iLogicalFormatKey = aNumberFormatter.getLogicalFormatKey();
168                 bFormatKeysInitialized = true;
169              }
170         }
171         catch (Exception e)
172         {
173             e.printStackTrace(System.out);
174         }
175     }
176 
getXColumnPropertySet()177     public XPropertySet getXColumnPropertySet()
178     {
179         getFieldType(); // will collect meta data 'bout the column, if not already done so
180         return m_xColPropertySet;
181     }
182 
setCommandName(String _CommandName)183     public void setCommandName(String _CommandName)
184     {
185         m_sCommandName = _CommandName;
186     }
187 
getDisplayFieldName()188     public String getDisplayFieldName()
189     {
190         return m_sDisplayFieldName;
191     }
192 
getCommandName()193     public String getCommandName()
194     {
195         return m_sCommandName;
196     }
197 
getFieldName()198     public String getFieldName()
199     {
200         return m_sFieldName;
201     }
202 
getFieldTitle()203     public String getFieldTitle()
204     {
205         return FieldTitle;
206     }
207 
setFieldTitle(String _sTitle)208     public void setFieldTitle(String _sTitle)
209     {
210         FieldTitle = _sTitle;
211     }
212 
getCommandName(String _DisplayName)213     public static String getCommandName(String _DisplayName)
214     {
215         String locCommandName = null;
216         String[] sFieldMetaData = JavaTools.ArrayoutofString(_DisplayName, ".");
217         if (sFieldMetaData.length >= 2)
218         {
219             String locfieldname = sFieldMetaData[sFieldMetaData.length - 1];
220             locCommandName = _DisplayName.substring(0, _DisplayName.length() - locfieldname.length() - 1);
221         }
222         return locCommandName;
223     }
224 
isBoolean()225     public boolean isBoolean()
226     {
227         boolean bIsBoolean = false;
228         switch ( getFieldType() )
229         {
230             case DataType.BIT: // ==  -7;
231             case DataType.BOOLEAN:
232                 bIsBoolean = true;
233                 break;
234             default:
235                 bIsBoolean = false;
236         }
237         return bIsBoolean;
238     }
239 
getTyperelatedFieldData()240     private void getTyperelatedFieldData()
241     {
242         try
243         {
244             switch ( getFieldType() )
245             {
246                 case DataType.BIT: // ==  -7;
247                 case DataType.BOOLEAN:
248                     // Todo: Look if the defaultvalue has been set in the Datasource
249                     StandardFormatKey = iLogicalFormatKey;
250                     FieldWidth = 5;
251                     bIsNumberFormat = true;
252                     break;
253 
254                 case DataType.TINYINT: // ==  -6;
255                     StandardFormatKey = iNumberFormatKey;
256                     FieldWidth = 5;
257                     bIsNumberFormat = true;
258                     break;
259 
260                 case DataType.SMALLINT: // ==   5;
261                     StandardFormatKey = iNumberFormatKey;
262                     FieldWidth = 5;
263                     bIsNumberFormat = true;
264                     break;
265 
266                 case DataType.INTEGER: // ==   4;
267                     StandardFormatKey = iNumberFormatKey;
268                     FieldWidth = 10;
269                     bIsNumberFormat = true;
270                     break;
271 
272                 case DataType.BIGINT: // ==  -5;
273                     StandardFormatKey = iNumberFormatKey;
274                     FieldWidth = 15;
275                     bIsNumberFormat = true;
276                     break;
277 
278                 case DataType.CHAR: // ==   1;
279                     StandardFormatKey = iTextFormatKey;
280                     getTextFieldWidth(10);
281                     bIsNumberFormat = false;
282                     break;
283 
284                 case DataType.VARCHAR: // ==  12;
285                     StandardFormatKey = iTextFormatKey;
286                     getTextFieldWidth(30);
287                     bIsNumberFormat = false;
288                     break;
289 
290                 case DataType.LONGVARCHAR: // ==  -1;
291                     StandardFormatKey = iTextFormatKey;
292                     getTextFieldWidth(60);
293                     bIsNumberFormat = false;
294                     break;
295 
296                 case DataType.NUMERIC: // ==   2;
297                     StandardFormatKey = iNumberFormatKey;
298                     FieldWidth = 20;
299                     bIsNumberFormat = true;
300                     break;
301 
302                 case DataType.DECIMAL: // ==   3;  [mit Nachkommastellen]
303                     StandardFormatKey = iNumberFormatKey;
304                     FieldWidth = 10 + AnyConverter.toInt(m_xColPropertySet.getPropertyValue("Scale")) + 1;
305                     bIsNumberFormat = true;
306                     break;
307 
308                 case DataType.FLOAT: // ==   6;
309                     StandardFormatKey = iNumberFormatKey;
310                     FieldWidth = 10 + AnyConverter.toInt(m_xColPropertySet.getPropertyValue("Scale")) + 1;
311                     bIsNumberFormat = true;
312                     break;
313 
314                 case DataType.REAL: // ==   7;
315                     StandardFormatKey = iNumberFormatKey;
316                     FieldWidth = 10 + AnyConverter.toInt(m_xColPropertySet.getPropertyValue("Scale")) + 1;
317                     bIsNumberFormat = true;
318                     break;
319 
320                 case DataType.DOUBLE: // ==   8;
321                     StandardFormatKey = iNumberFormatKey;
322                     FieldWidth = 10 + AnyConverter.toInt(m_xColPropertySet.getPropertyValue("Scale")) + 1;
323                     bIsNumberFormat = true;
324                     break;
325 
326                 case DataType.DATE: // ==  91;
327                     StandardFormatKey = iDateFormatKey;
328                     FieldWidth = 10;
329                     bIsNumberFormat = true;
330                     break;
331 
332                 case DataType.TIME: // ==  92;
333                     StandardFormatKey = iTimeFormatKey;
334                     FieldWidth = 10;
335                     bIsNumberFormat = true;
336                     break;
337 
338                 case DataType.TIMESTAMP: // ==  93;
339                     StandardFormatKey = iDateTimeFormatKey;
340                     FieldWidth = 20;
341                     bIsNumberFormat = true;
342                     break;
343             }
344 
345             Object oKey = m_xColPropertySet.getPropertyValue("FormatKey");
346             if (AnyConverter.isVoid(oKey))
347             {
348                 m_nDBFormatKey = StandardFormatKey;
349             }
350             else
351             {
352                 m_nDBFormatKey = AnyConverter.toInt(oKey);
353             }
354         }
355         catch (Exception exception)
356         {
357             exception.printStackTrace(System.out);
358             m_nDBFormatKey = StandardFormatKey;
359         }
360     }
361 
getTextFieldWidth(int iWidth)362     private void getTextFieldWidth(int iWidth)
363     {
364         try
365         {
366             FieldWidth = AnyConverter.toInt(m_xColPropertySet.getPropertyValue("Precision"));
367             if (FieldWidth > 0)
368             {
369                 if (FieldWidth > (2 * iWidth))
370                 {
371                     FieldWidth = 2 * iWidth;
372                 }
373                 else if (FieldWidth == 0)
374                 {
375                     FieldWidth = iWidth;
376                 }
377             }
378         }
379         catch (Exception exception)
380         {
381             exception.printStackTrace(System.out);
382         }
383     }
384 
initDefaultValue()385     public void initDefaultValue()
386     {
387         switch (getFieldType())
388         {
389             case DataType.BIT: // ==  -7;
390             case DataType.BOOLEAN:
391                 DefaultValue = Integer.valueOf("1");
392                 break;
393 
394             case DataType.TINYINT: // ==  -6;
395                 DefaultValue = Integer.valueOf("98");
396                 break;
397 
398             case DataType.SMALLINT: // ==   5;
399                 DefaultValue = Integer.valueOf("987");
400                 break;
401 
402             case DataType.INTEGER: // ==   4;
403                 DefaultValue = Integer.valueOf("9876");
404                 break;
405 
406             case DataType.BIGINT: // ==  -5;
407                 DefaultValue = Integer.valueOf("98765");
408                 break;
409 
410             case DataType.CHAR: // ==   1;
411                 DefaultValue = String.valueOf('x');
412                 break;
413 
414             case DataType.VARCHAR: // ==  12;
415                 DefaultValue = BlindtextCreator.getBlindTextString(FieldTitle, FieldWidth, FieldWidth);
416                 break;
417 
418             case DataType.LONGVARCHAR: // ==  -1;
419                 DefaultValue = BlindtextCreator.getBlindTextString(FieldTitle, FieldWidth, FieldWidth);
420                 break;
421 
422             case DataType.NUMERIC: // ==   2;
423                 DefaultValue = Double.valueOf("9876.5");
424                 break;
425 
426             case DataType.DECIMAL: // ==   3;  [mit Nachkommastellen]
427                 DefaultValue = Double.valueOf("9876.5");
428                 break;
429 
430             case DataType.FLOAT: // ==   6;
431                 DefaultValue = Double.valueOf("9876.5");
432                 break;
433 
434             case DataType.REAL: // ==   7;
435                 DefaultValue = Double.valueOf("9876.5");
436                 break;
437 
438             case DataType.DOUBLE: // ==   8;
439                 DefaultValue = Double.valueOf("9876.54");
440                 break;
441 
442             case DataType.DATE: // ==  91;
443                 DefaultValue = Double.valueOf("42510");
444                 break;
445 
446             case DataType.TIME: // ==  92;
447                 DefaultValue = Double.valueOf("10");
448                 break;
449 
450             case DataType.TIMESTAMP: // ==  93;
451                 DefaultValue = Double.valueOf("5454110");
452                 break;
453 
454             default:
455                 break;
456         }
457     }
458 
getDefaultValue()459     public Object getDefaultValue()
460     {
461         if ( DefaultValue == null )
462             initDefaultValue();
463         return DefaultValue;
464     }
465 
466 }
467