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.report;
24 
25 import com.sun.star.beans.PropertyState;
26 import com.sun.star.beans.XPropertySet;
27 import com.sun.star.beans.XPropertyState;
28 import com.sun.star.container.XNameAccess;
29 import com.sun.star.container.XNamed;
30 // import com.sun.star.lang.IndexOutOfBoundsException;
31 import com.sun.star.style.ParagraphAdjust;
32 import com.sun.star.table.XCell;
33 import com.sun.star.table.XCellRange;
34 import com.sun.star.table.XTableColumns;
35 import com.sun.star.table.XTableRows;
36 import com.sun.star.text.XDependentTextField;
37 import com.sun.star.text.XTextCursor;
38 import com.sun.star.text.XTextRange;
39 import com.sun.star.text.XTextTable;
40 import com.sun.star.uno.AnyConverter;
41 import com.sun.star.uno.Exception;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.util.XNumberFormats;
44 import com.sun.star.wizards.db.*;
45 import com.sun.star.wizards.common.*;
46 import com.sun.star.wizards.text.TextDocument;
47 import com.sun.star.wizards.text.TextTableHandler;
48 import com.sun.star.wizards.text.TextFieldHandler;
49 
50 public class DBColumn
51 {
52 
53     public XCell xValCell;
54     public XTextRange xValTextCell;
55     public XTextCursor xValCellCursor;
56     public XNumberFormats xValCellNumberFormats;
57     public XCell xNameCell;
58     public XTextRange xNameTextCell;
59     public boolean bAlignLeft;
60     public Object DefaultValue;
61     public String CharFontName;
62     public int FormatKey;
63     public PropertyState PropertyState;
64     public int ValColumn = 1;
65     public int ValRow = 0;
66     public FieldColumn CurDBField;
67     private XTextTable xTextTable;
68     private XTableColumns xTableColumns;
69     private XCellRange xCellRange;
70     public XNamed xTableName;
71     private boolean bIsGroupColumn;
72     TextDocument oTextDocument;
73     RecordParser CurDBMetaData;
74     RecordTable CurRecordTable;
75     TextTableHandler oTextTableHandler;
76 
DBColumn(TextTableHandler _oTextTableHandler, RecordParser _CurDBMetaData, int i)77     public DBColumn(TextTableHandler _oTextTableHandler, RecordParser _CurDBMetaData, int i) throws Exception
78     {
79         CurRecordTable = new RecordTable(_oTextTableHandler);
80         initializeRecordTableMembers(CurRecordTable, _oTextTableHandler, _CurDBMetaData, i, false);
81     }
82 
DBColumn(RecordTable _CurRecordTable, TextTableHandler _oTextTableHandler, RecordParser _CurDBMetaData, int i, boolean _bforce)83     public DBColumn(RecordTable _CurRecordTable, TextTableHandler _oTextTableHandler, RecordParser _CurDBMetaData, int i, boolean _bforce) throws Exception
84     {
85         initializeRecordTableMembers(_CurRecordTable, _oTextTableHandler, _CurDBMetaData, i, _bforce);
86     }
87 
DBColumn(RecordTable _CurRecordTable, TextTableHandler _oTextTableHandler, RecordParser _CurDBMetaData, int i)88     public DBColumn(RecordTable _CurRecordTable, TextTableHandler _oTextTableHandler, RecordParser _CurDBMetaData, int i) throws Exception
89     {
90         initializeRecordTableMembers(_CurRecordTable, _oTextTableHandler, _CurDBMetaData, i, false);
91     }
92 
initializeRecordTableMembers(RecordTable _CurRecordTable, TextTableHandler _oTextTableHandler, RecordParser _CurDBMetaData, int i, boolean _bForce)93     private void initializeRecordTableMembers(RecordTable _CurRecordTable, TextTableHandler _oTextTableHandler, RecordParser _CurDBMetaData, int i, boolean _bForce) throws Exception
94     {
95         this.oTextTableHandler = _oTextTableHandler;
96         this.CurDBMetaData = _CurDBMetaData;
97         this.CurRecordTable = _CurRecordTable;
98         bIsGroupColumn = false;
99         if (CurDBMetaData.RecordFieldColumns != null)
100         {
101             CurDBField = CurDBMetaData.getFieldColumnByFieldName(CurDBMetaData.RecordFieldColumns[i].getFieldName());
102         }
103         else
104         {
105             CurDBField = CurDBMetaData.getFieldColumnByFieldName(CurDBMetaData.getRecordFieldName(i));
106         }
107         if (_bForce)
108         {
109             assignCells(i, true);
110         }
111         else
112         {
113             for (int n = 0; n < CurRecordTable.xTableColumns.getCount(); n++)
114             {
115                 assignCells(n, false);
116             }
117         }
118     }
119 
assignCells(int _nColumn, boolean _bforce)120     private boolean assignCells(int _nColumn, boolean _bforce)
121     {
122         try
123         {
124             XCell xCell = CurRecordTable.xCellRange.getCellByPosition(_nColumn, 0);
125             XTextRange xTextCell = UnoRuntime.queryInterface(XTextRange.class, xCell);
126             String CompString = "Column";
127             XTextCursor xLocCellCursor = TextDocument.createTextCursor(xCell);
128             if (isNameCell(xLocCellCursor, CurDBField.getFieldName(), CompString) || (_bforce))
129             {
130                 xNameCell = xCell;
131                 xNameTextCell = xTextCell;
132                 xValCell = CurRecordTable.xCellRange.getCellByPosition(_nColumn, 1);
133                 xValTextCell = UnoRuntime.queryInterface(XTextRange.class, xValCell);
134                 xValCellCursor = TextDocument.createTextCursor(xValCell);
135                 ValColumn = _nColumn;
136                 return true;
137             }
138         }
139         catch (Exception e)
140         {
141             e.printStackTrace(System.out);
142         }
143         return false;
144     }
145 
DBColumn(TextTableHandler _oTextTableHandler, RecordParser _CurDBMetaData, String _FieldName, int GroupIndex, String TableName, DBColumn OldDBColumn)146     public DBColumn(TextTableHandler _oTextTableHandler, RecordParser _CurDBMetaData, String _FieldName, int GroupIndex, String TableName, DBColumn OldDBColumn) throws Exception
147     {
148         this.oTextTableHandler = _oTextTableHandler;
149         this.CurDBMetaData = _CurDBMetaData;
150         CurDBField = CurDBMetaData.getFieldColumnByDisplayName(_FieldName);
151         bIsGroupColumn = true;
152         getTableColumns(TableName);
153         xNameCell = OldDBColumn.xNameCell;
154         xNameTextCell = OldDBColumn.xNameTextCell;
155         xValCell = OldDBColumn.xValCell;
156         xValTextCell = OldDBColumn.xValTextCell;
157         xValCellCursor = TextDocument.createTextCursor(xValCell);
158         ValColumn = OldDBColumn.ValColumn;
159         ValRow = OldDBColumn.ValRow;
160         initializeNumberFormat();
161     }
162 
DBColumn(TextTableHandler _oTextTableHandler, RecordParser _CurDBMetaData, String _FieldName, int GroupIndex, String TableName)163     public DBColumn(TextTableHandler _oTextTableHandler, RecordParser _CurDBMetaData, String _FieldName, int GroupIndex, String TableName) throws Exception
164     {
165         this.oTextTableHandler = _oTextTableHandler;
166         this.CurDBMetaData = _CurDBMetaData;
167         CurDBField = CurDBMetaData.getFieldColumnByFieldName(_FieldName);
168         bIsGroupColumn = true;
169         XTextRange xTextCell;
170         XCell xCell;
171         getTableColumns(TableName);
172 
173         XTableRows xRows = null;
174         try
175         {
176             xRows = xTextTable.getRows();
177         }
178         catch (java.lang.NullPointerException e)
179         {
180             e.printStackTrace();
181 // TODO: handle the nullpointer right
182 //                return;
183         }
184         for (int n = 0; n < xTableColumns.getCount(); n++)
185         {
186             for (int m = 0; m < xRows.getCount(); m++)
187             {
188                 xCell = xCellRange.getCellByPosition(n, m);
189                 xTextCell = UnoRuntime.queryInterface(XTextRange.class, xCell);
190                 String CompString = TableName.substring(4);
191                 XTextCursor xLocCellCursor = TextDocument.createTextCursor(xCell);
192                 if (isNameCell(xLocCellCursor, CurDBField.getFieldName(), CompString))
193                 {
194                     xNameCell = xCell;
195                     xNameTextCell = xTextCell;
196                 }
197                 else
198                 {   //In Grouping Sections only two cells are allowed ' if (CellString.equals(CurFieldString)){
199                     xValCell = xCell;
200                     xValTextCell = xTextCell;
201                     xValCellCursor = xLocCellCursor;
202                     ValColumn = n;
203                     ValRow = m;
204                     checkforLeftAlignment();
205                 }
206             }
207         }
208     }
209 
getTableColumns(String TableName)210     private void getTableColumns(String TableName)
211     {
212         try
213         {
214             XNameAccess xAllTextTables = oTextTableHandler.xTextTablesSupplier.getTextTables();
215             if (xAllTextTables.hasByName(TableName))
216             {
217                 Object oTextTable = xAllTextTables.getByName(TableName);
218                 xCellRange = UnoRuntime.queryInterface(XCellRange.class, oTextTable);
219                 xTextTable = UnoRuntime.queryInterface(XTextTable.class, oTextTable);
220                 xTableName = UnoRuntime.queryInterface(XNamed.class, oTextTable);
221                 xTableColumns = xTextTable.getColumns();
222             }
223         }
224         catch (Exception e)
225         {
226             e.printStackTrace();
227         }
228     }
229 
initializeNumberFormat()230     public void initializeNumberFormat()
231     {
232         if (CurDBField.isBoolean())
233         {
234             NumberFormatter oNumberFormatter = oTextTableHandler.getNumberFormatter();
235             int iLogicalFormatKey = oNumberFormatter.setBooleanReportDisplayNumberFormat();
236             oNumberFormatter.setNumberFormat(xValCell, iLogicalFormatKey, oNumberFormatter);
237         }
238         else
239         {
240             oTextTableHandler.getNumberFormatter().setNumberFormat(xValCell, CurDBField.getDBFormatKey(), CurDBMetaData.getNumberFormatter());
241         }
242         setCellFont();
243     }
244 
insertColumnData(TextFieldHandler oTextFieldHandler, boolean _bIsLandscape)245     public void insertColumnData(TextFieldHandler oTextFieldHandler, boolean _bIsLandscape)
246     {
247         insertUserFieldToTableCell(oTextFieldHandler);
248         replaceValueCellofTable(_bIsLandscape);
249     }
250 
insertUserFieldToTableCell(TextFieldHandler oTextFieldHandler)251     public void insertUserFieldToTableCell(TextFieldHandler oTextFieldHandler)
252     {
253         XTextCursor xTextCursor = TextDocument.createTextCursor(xNameCell);
254         xTextCursor.gotoStart(false);
255         xTextCursor.gotoEnd(true);
256         xTextCursor.setString(PropertyNames.EMPTY_STRING);
257         oTextFieldHandler.insertUserField(xTextCursor, CurDBField.getFieldName(), CurDBField.getFieldTitle());
258     }
259 
insertUserFieldToTableCell(TextFieldHandler oTextFieldHandler, XCell xCell)260     public void insertUserFieldToTableCell(TextFieldHandler oTextFieldHandler, XCell xCell)
261     {
262         XTextCursor xTextCursor = TextDocument.createTextCursor(xCell);
263         xTextCursor.gotoStart(false);
264         xTextCursor.gotoEnd(true);
265         xTextCursor.setString(PropertyNames.EMPTY_STRING);
266         oTextFieldHandler.insertUserField(xTextCursor, CurDBField.getFieldName(), CurDBField.getFieldTitle());
267     }
268 
formatValueCell()269     public void formatValueCell()
270     {
271         initializeNumberFormat();
272         if (checkforLeftAlignment())
273         {
274             bAlignLeft = true;
275         }
276     }
277 
checkforLeftAlignment()278     private boolean checkforLeftAlignment()
279     {
280         bAlignLeft = ((CurDBField.isNumberFormat()) && (ValColumn == xTableColumns.getCount() - 1));
281         return bAlignLeft;
282     }
283 
modifyCellContent(XCellRange xCellRange, Object CurGroupValue)284     public void modifyCellContent(XCellRange xCellRange, Object CurGroupValue)
285     {
286         try
287         {
288             xValCell = xCellRange.getCellByPosition(ValColumn, ValRow);
289             xValTextCell = UnoRuntime.queryInterface(XTextRange.class, xValCell);
290             modifyCellContent(CurGroupValue);
291             if (bAlignLeft)
292             {
293                 xValCellCursor = TextDocument.createTextCursor(xValCell);
294                 Helper.setUnoPropertyValue(xValCellCursor, "ParaAdjust", new Integer(com.sun.star.style.ParagraphAdjust.LEFT_value));
295             }
296         }
297         catch (Exception exception)
298         {
299             exception.printStackTrace(System.out);
300         }
301     }
302 
modifyCellContent(Object CurGroupValue)303     public void modifyCellContent(Object CurGroupValue)
304     {
305         double dblValue = 0;
306         try
307         {
308             if (xValCell != null)
309             {
310                 if (AnyConverter.isString(CurGroupValue))
311                 {
312                     String sValue = AnyConverter.toString(CurGroupValue);
313                     xValTextCell.setString(sValue);
314                 }
315                 else
316                 {
317                     if (AnyConverter.isBoolean(CurGroupValue))
318                     {
319                         dblValue = (double) AnyConverter.toInt(CurGroupValue);
320                     }
321                     if (AnyConverter.isByte(CurGroupValue))
322                     {
323                         dblValue = (double) AnyConverter.toByte(CurGroupValue);
324                     }
325                     else if (AnyConverter.isDouble(CurGroupValue))
326                     {
327                         dblValue = AnyConverter.toDouble(CurGroupValue);
328                     }
329                     else if (AnyConverter.isFloat(CurGroupValue))
330                     {
331                         dblValue = (double) AnyConverter.toFloat(CurGroupValue);
332                     }
333                     else if (AnyConverter.isInt(CurGroupValue))
334                     {
335                         dblValue = (double) AnyConverter.toInt(CurGroupValue);
336                     }
337                     else if (AnyConverter.isLong(CurGroupValue))
338                     {
339                         dblValue = (double) AnyConverter.toLong(CurGroupValue);
340                     }
341                     else if (AnyConverter.isShort(CurGroupValue))
342                     {
343                         dblValue = (double) AnyConverter.toShort(CurGroupValue);
344                     }
345                     xValCell.setValue(dblValue);
346                 }
347             }
348         }
349         catch (Exception exception)
350         {
351             System.err.println(exception);
352         }
353     }
354     // If the parameter CurGroupValue is null the placeholders are inserted
replaceValueCellofTable(boolean _bIsLandscape)355     public void replaceValueCellofTable(boolean _bIsLandscape)
356     {
357         try
358         {
359             Object CurGroupValue;
360             if (!bIsGroupColumn && !CurDBField.isNumberFormat())
361             {
362                 CurGroupValue = BlindtextCreator.adjustBlindTextlength(CurDBField.getFieldTitle(), CurDBField.getFieldWidth(), _bIsLandscape, bIsGroupColumn, CurDBMetaData.getRecordFieldNames());
363             }
364             else
365             {
366                 CurGroupValue = CurDBField.getDefaultValue();
367             }
368             modifyCellContent(CurGroupValue);
369             if (bAlignLeft)
370             {
371                 Helper.setUnoPropertyValue(xValCellCursor, "ParaAdjust", new Integer(ParagraphAdjust.LEFT_value));
372             }
373 
374             int nFieldType = CurDBField.getFieldType();
375             if ((nFieldType == com.sun.star.sdbc.DataType.BIT) ||
376                 (nFieldType == com.sun.star.sdbc.DataType.BOOLEAN))
377             {
378                 CharFontName = "StarSymbol";
379                 Helper.setUnoPropertyValue(xValCellCursor, "CharFontName", CharFontName);
380                 if (!bIsGroupColumn)
381                 {
382                     Helper.setUnoPropertyValue(xValCellCursor, "ParaAdjust", new Integer(ParagraphAdjust.CENTER_value));
383                 }
384             }
385             else
386             {
387                 if (PropertyState == com.sun.star.beans.PropertyState.DEFAULT_VALUE)
388                 {
389                     XPropertyState xPropState = UnoRuntime.queryInterface(XPropertyState.class, xValCellCursor);
390                     xPropState.setPropertyToDefault("CharFontName");
391                 }
392                 else
393                 {
394                     if (PropertyState == com.sun.star.beans.PropertyState.DIRECT_VALUE)
395                     {
396                         Helper.setUnoPropertyValue(xValCellCursor, "CharFontName", CharFontName);
397                     }
398                 }
399             }
400         }
401         catch (com.sun.star.beans.UnknownPropertyException exception)
402         {
403             exception.printStackTrace(System.out);
404         }
405     }
406 
setCellFont()407     public void setCellFont()
408     {
409         try
410         {
411             XPropertyState xPropertyState;
412             int nFieldType = CurDBField.getFieldType();
413             if ((nFieldType == com.sun.star.sdbc.DataType.BIT) ||
414                 (nFieldType == com.sun.star.sdbc.DataType.BOOLEAN))
415             {
416                 CharFontName = "StarSymbol";
417                 PropertyState = com.sun.star.beans.PropertyState.DIRECT_VALUE;
418                 xValCellCursor.gotoStart(false);
419                 xValCellCursor.gotoEnd(true);
420                 Helper.setUnoPropertyValue(xValCellCursor, "CharFontName", CharFontName);
421             }
422             else
423             {
424                 xPropertyState = UnoRuntime.queryInterface(XPropertyState.class, xValCellCursor);
425                 PropertyState = xPropertyState.getPropertyState("CharFontName");
426                 CharFontName = AnyConverter.toString(Helper.getUnoPropertyValue(xValCellCursor, "CharFontName"));
427             }
428         }
429         catch (Exception exception)
430         {
431             exception.printStackTrace(System.out);
432         }
433     }
434 
isNameCell(XTextCursor xCellCursor, String CurFieldName, String CompString)435     private boolean isNameCell(XTextCursor xCellCursor, String CurFieldName, String CompString)
436     {
437         try
438         {
439             xCellCursor.gotoStart(false);
440             XTextRange xTextRange = xCellCursor.getEnd();
441             Object oTextField = Helper.getUnoPropertyValue(xTextRange, "TextField");
442             if (AnyConverter.isVoid(oTextField))
443             {
444                 return false;
445             }
446             else
447             {
448                 XDependentTextField xDependent = UnoRuntime.queryInterface(XDependentTextField.class, oTextField);
449                 XPropertySet xMaster = xDependent.getTextFieldMaster();
450                 String UserFieldName = (String) xMaster.getPropertyValue(PropertyNames.PROPERTY_NAME);
451                 boolean bIsNameCell = false;
452                 if ((UserFieldName.startsWith(CompString)) || (UserFieldName.equals(CurFieldName)))
453                 {
454                     bIsNameCell = true;
455                 }
456                 else
457                 {
458                     // stupid hack, 'Title' is not a real good Table-Cell-Name
459                     // take a look at xmloff/source/text/txtvfldi.txt, there exists 2 '_renamed_' strings
460                     String sLocalCurFieldName = CurFieldName + "_renamed_";
461                     if (UserFieldName.startsWith(sLocalCurFieldName))
462                     {
463                         bIsNameCell = true;
464                     }
465                 }
466                 return bIsNameCell;
467             }
468         }
469         // Todo: Insert a  resource; Exception should be thrown to the calling routine
470         catch (Exception exception)
471         {
472 //      sMsgInvalidTextField = oResource.getResText(UIConsts.RID_REPORT + 73);
473 //      SystemDialog.showMessageBox(oTextTableHandler.xMSFDoc, "ErrorBox", VclWindowPeerAttribute.OK, sMsgInvalidTextField);
474             exception.printStackTrace(System.out);
475             return true;    //most probably this is really the Namecell!!!!
476         }
477     }
478 }
479