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 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbaccess.hxx"
26 
27 #ifndef DBAUI_TABLEFIELDDESC_HXX
28 #include "TableFieldDescription.hxx"
29 #endif
30 #ifndef _TOOLS_DEBUG_HXX
31 #include <tools/debug.hxx>
32 #endif
33 #ifndef _COM_SUN_STAR_SDBC_DATATYPE_HPP_
34 #include <com/sun/star/sdbc/DataType.hpp>
35 #endif
36 #include <comphelper/namedvaluecollection.hxx>
37 
38 #include <functional>
39 
40 using namespace ::com::sun::star::sdbc;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::beans;
43 using namespace comphelper;
44 using namespace dbaui;
45 
DBG_NAME(OTableFieldDesc)46 DBG_NAME(OTableFieldDesc)
47 //==============================================================================
48 OTableFieldDesc::OTableFieldDesc()
49     :m_pTabWindow(0)
50     ,m_eDataType(1000)
51     ,m_eFunctionType( FKT_NONE )
52     ,m_eFieldType(TAB_NORMAL_FIELD)
53     ,m_eOrderDir( ORDER_NONE )
54     ,m_nIndex(0)
55     ,m_nColWidth(0)
56     ,m_nColumnId((sal_uInt16)-1)
57     ,m_bGroupBy(sal_False)
58     ,m_bVisible(sal_False)
59 {
60 	DBG_CTOR(OTableFieldDesc,NULL);
61 }
62 //------------------------------------------------------------------------------
OTableFieldDesc(const OTableFieldDesc & rRS)63 OTableFieldDesc::OTableFieldDesc(const OTableFieldDesc& rRS)
64     : ::vos::OReference()
65 
66 {
67 	DBG_CTOR(OTableFieldDesc,NULL);
68     *this = rRS;
69 }
70 
71 //------------------------------------------------------------------------------
OTableFieldDesc(const::rtl::OUString & rT,const::rtl::OUString & rF)72 OTableFieldDesc::OTableFieldDesc(const ::rtl::OUString& rT, const ::rtl::OUString& rF )
73     :m_pTabWindow(0)
74     ,m_eFunctionType( FKT_NONE )
75     ,m_eOrderDir( ORDER_NONE )
76     ,m_nColumnId((sal_uInt16)-1)
77     ,m_bGroupBy(sal_False)
78     ,m_bVisible(sal_False)
79 {
80 	DBG_CTOR(OTableFieldDesc,NULL);
81 	SetField( rF );	SetTable( rT );
82 }
83 
84 //------------------------------------------------------------------------------
~OTableFieldDesc()85 OTableFieldDesc::~OTableFieldDesc()
86 {
87 	DBG_DTOR(OTableFieldDesc,NULL);
88 }
89 //------------------------------------------------------------------------------
operator =(const OTableFieldDesc & rRS)90 OTableFieldDesc& OTableFieldDesc::operator=( const OTableFieldDesc& rRS )
91 {
92     if (&rRS == this)
93 		return *this;
94 
95     m_aCriteria = rRS.GetCriteria();
96     m_aTableName = rRS.GetTable();
97     m_aAliasName = rRS.GetAlias();		// table range
98     m_aFieldName = rRS.GetField();		// column
99     m_aFieldAlias = rRS.GetFieldAlias();	// column alias
100     m_aFunctionName = rRS.GetFunction();	// Funktionsname
101     m_pTabWindow = rRS.GetTabWindow();
102     m_eDataType = rRS.GetDataType();
103     m_eFunctionType = rRS.GetFunctionType();
104     m_eFieldType = rRS.GetFieldType();
105     m_eOrderDir = rRS.GetOrderDir();
106     m_nIndex = rRS.GetFieldIndex();
107     m_nColWidth = rRS.GetColWidth();
108     m_nColumnId = rRS.m_nColumnId;
109     m_bGroupBy = rRS.IsGroupBy();
110     m_bVisible = rRS.IsVisible();
111 
112 	return *this;
113 }
114 //------------------------------------------------------------------------------
operator ==(const OTableFieldDesc & rDesc)115 sal_Bool OTableFieldDesc::operator==( const OTableFieldDesc& rDesc )
116 {
117 	DBG_CHKTHIS(OTableFieldDesc,NULL);
118 
119 	return (	m_eOrderDir != rDesc.GetOrderDir()		||
120 				m_eDataType != rDesc.GetDataType()		||
121 				m_aAliasName != rDesc.GetAlias()	 	||
122 				m_aFunctionName != rDesc.GetFunction()	||
123 				m_aFieldName != rDesc.GetField()		||
124 				m_aTableName != rDesc.GetTable()	 	||
125 				m_bGroupBy != rDesc.IsGroupBy()			||
126 				m_aCriteria != rDesc.GetCriteria()	||
127 				m_bVisible != rDesc.IsVisible() );
128 
129 }
130 
131 //------------------------------------------------------------------------------
SetCriteria(sal_uInt16 nIdx,const::rtl::OUString & rCrit)132 void OTableFieldDesc::SetCriteria( sal_uInt16 nIdx, const ::rtl::OUString& rCrit)
133 {
134 	DBG_CHKTHIS(OTableFieldDesc,NULL);
135 	if (nIdx < m_aCriteria.size())
136 		m_aCriteria[nIdx] = rCrit;
137 	else
138 	{
139 		for(sal_Int32 i=m_aCriteria.size();i<nIdx;++i)
140 			m_aCriteria.push_back( ::rtl::OUString());
141 		m_aCriteria.push_back(rCrit);
142 	}
143 }
144 
145 //------------------------------------------------------------------------------
GetCriteria(sal_uInt16 nIdx) const146 ::rtl::OUString OTableFieldDesc::GetCriteria( sal_uInt16 nIdx ) const
147 {
148 	DBG_CHKTHIS(OTableFieldDesc,NULL);
149 	::rtl::OUString aRetStr;
150 	if( nIdx < m_aCriteria.size())
151 		aRetStr = m_aCriteria[nIdx];
152 
153 	return aRetStr;
154 }
155 
156 // -----------------------------------------------------------------------------
157 namespace
158 {
159     struct SelectPropertyValueAsString : public ::std::unary_function< PropertyValue, ::rtl::OUString >
160     {
operator ()__anon886c7a5a0111::SelectPropertyValueAsString161         ::rtl::OUString operator()( const PropertyValue& i_rPropValue ) const
162         {
163             ::rtl::OUString sValue;
164             OSL_VERIFY( i_rPropValue.Value >>= sValue );
165             return sValue;
166         }
167     };
168 }
169 
170 // -----------------------------------------------------------------------------
Load(const::com::sun::star::beans::PropertyValue & i_rSettings,const bool i_bIncludingCriteria)171 void OTableFieldDesc::Load( const ::com::sun::star::beans::PropertyValue& i_rSettings, const bool i_bIncludingCriteria )
172 {
173 	DBG_CHKTHIS(OTableFieldDesc,NULL);
174 
175     ::comphelper::NamedValueCollection aFieldDesc( i_rSettings.Value );
176     m_aAliasName = aFieldDesc.getOrDefault( "AliasName", m_aAliasName );
177     m_aTableName = aFieldDesc.getOrDefault( "TableName", m_aTableName );
178     m_aFieldName = aFieldDesc.getOrDefault( "FieldName", m_aFieldName );
179     m_aFieldAlias = aFieldDesc.getOrDefault( "FieldAlias", m_aFieldAlias );
180     m_aFunctionName = aFieldDesc.getOrDefault( "FunctionName", m_aFunctionName );
181     m_eDataType = aFieldDesc.getOrDefault( "DataType", m_eDataType );
182     m_eFunctionType = aFieldDesc.getOrDefault( "FunctionType", m_eFunctionType );
183     m_nColWidth = aFieldDesc.getOrDefault( "ColWidth", m_nColWidth );
184     m_bGroupBy = aFieldDesc.getOrDefault( "GroupBy", m_bGroupBy );
185     m_bVisible = aFieldDesc.getOrDefault( "Visible", m_bVisible );
186 
187     m_eFieldType = static_cast< ETableFieldType >( aFieldDesc.getOrDefault( "FieldType", static_cast< sal_Int32 >( m_eFieldType ) ) );
188     m_eOrderDir = static_cast< EOrderDir >( aFieldDesc.getOrDefault( "OrderDir", static_cast< sal_Int32 >( m_eOrderDir ) ) );
189 
190     if ( i_bIncludingCriteria )
191     {
192         const Sequence< PropertyValue > aCriteria( aFieldDesc.getOrDefault( "Criteria", Sequence< PropertyValue >() ) );
193         m_aCriteria.resize( aCriteria.getLength() );
194         ::std::transform(
195             aCriteria.getConstArray(),
196             aCriteria.getConstArray() + aCriteria.getLength(),
197             m_aCriteria.begin(),
198             SelectPropertyValueAsString()
199         );
200     }
201 }
202 //------------------------------------------------------------------------------
Save(::comphelper::NamedValueCollection & o_rSettings,const bool i_bIncludingCriteria)203 void OTableFieldDesc::Save( ::comphelper::NamedValueCollection& o_rSettings, const bool i_bIncludingCriteria )
204 {
205 	DBG_CHKTHIS(OTableFieldDesc,NULL);
206 
207 	o_rSettings.put( "AliasName", m_aAliasName );
208 	o_rSettings.put( "TableName", m_aTableName );
209 	o_rSettings.put( "FieldName", m_aFieldName );
210 	o_rSettings.put( "FieldAlias", m_aFieldAlias );
211 	o_rSettings.put( "FunctionName", m_aFunctionName );
212 	o_rSettings.put( "DataType", m_eDataType );
213 	o_rSettings.put( "FunctionType", (sal_Int32)m_eFunctionType );
214 	o_rSettings.put( "FieldType", (sal_Int32)m_eFieldType );
215 	o_rSettings.put( "OrderDir", (sal_Int32)m_eOrderDir );
216 	o_rSettings.put( "ColWidth", m_nColWidth );
217 	o_rSettings.put( "GroupBy", m_bGroupBy );
218 	o_rSettings.put( "Visible", m_bVisible );
219 
220     if ( i_bIncludingCriteria )
221     {
222         if ( !m_aCriteria.empty() )
223         {
224             sal_Int32 c = 0;
225             Sequence< PropertyValue > aCriteria( m_aCriteria.size() );
226             for (   ::std::vector< ::rtl::OUString >::const_iterator crit = m_aCriteria.begin();
227                     crit != m_aCriteria.end();
228                     ++crit, ++c
229                 )
230             {
231                 aCriteria[c].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Criterion_" ) ) + ::rtl::OUString::valueOf( c );
232                 aCriteria[c].Value <<= *crit;
233             }
234 
235             o_rSettings.put( "Criteria", aCriteria );
236         }
237     }
238 }
239 // -----------------------------------------------------------------------------
240 
241 
242 
243