All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DataSetTypeConverter.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/dataaccess/dataset/DataSetTypeConverter.cpp
22 
23  \brief An converter for DataSetType.
24 */
25 
26 // TerraLib
27 #include "../../common/Translator.h"
28 #include "../../dataaccess/dataset/PrimaryKey.h"
29 #include "../datasource/DataSourceCapabilities.h"
30 #include "../Exception.h"
32 #include "DataSetType.h"
33 #include "DataSetTypeConverter.h"
34 
35 // STL
36 #include <cassert>
37 
39  : m_inDataSetType(dynamic_cast<DataSetType*>(type->clone()))
40 {
41  assert(type);
42 
43  // By default no property is automatically converted
44  for(std::size_t i = 0; i != type->size(); ++i)
45  m_convertedProperties.push_back(0);
46 
47  // Creates the empty converted DataSetType
48  m_outDataSetType = new DataSetType(type->getName(), type->getId());
50 }
51 
53  : m_inDataSetType(dynamic_cast<DataSetType*>(type->clone()))
54 {
55  assert(type);
56 
57  // By default no property is automatically converted
58  for(std::size_t i = 0; i != type->size(); ++i)
59  m_convertedProperties.push_back(0);
60 
61  // Creates the empty converted DataSetType
62  m_outDataSetType = new DataSetType(type->getName(), type->getId());
63 
64  // Gets the DataTypeCapabilities
65  const DataTypeCapabilities& dtCapabilities = capabilities.getDataTypeCapabilities();
66 
67  // Try automatic build the conversion
68  const std::vector<te::dt::Property*> properties = type->getProperties();
69  for(std::size_t i = 0; i < properties.size(); ++i)
70  {
71  te::dt::Property* p = properties[i];
72  assert(p);
73 
74  if(dtCapabilities.supports(p->getType()))
75  {
76  if(p->getType() == te::dt::STRING_TYPE)
77  {
78  CharEncodingConverter cec(ce);
79  add(i, p->clone(), cec);
80  }
81  else
82  add(i, p->clone());
83  }
84  else
85  {
86  // Try create the property from data source capabilities hint
87  int hintDataType = dtCapabilities.getHint(p->getType());
88 
89  if(hintDataType != te::dt::UNKNOWN_TYPE)
90  {
91  //add(propertyName, hintDataType, i); TODO: Utility method that returns a te::dt::Property* given a name and a data type.
92  }
93  }
94  }
95 
96  te::da::PrimaryKey* pk = type->getPrimaryKey();
97  if(pk)
98  {
100 
101  std::vector<te::dt::Property*> pkProps = pk->getProperties();
102  std::vector<te::dt::Property*> outPkProps;
103  for(std::size_t i = 0; i < pkProps.size(); ++i)
104  outPkProps.push_back(m_outDataSetType->getProperty(pkProps[i]->getName()));
105 
106  outPk->setProperties(outPkProps);
107  }
108 }
109 
111 {
112  delete m_outDataSetType;
113  delete m_inDataSetType;
114 }
115 
117 {
118  return m_inDataSetType;
119 }
120 
121 void te::da::DataSetTypeConverter::getNonConvertedProperties(std::vector<std::string>& propertyNames) const
122 {
123  for(std::size_t i = 0; i < m_inDataSetType->size(); ++i)
124  {
125  if(!isConverted(i))
126  propertyNames.push_back(m_inDataSetType->getProperty(i)->getName());
127  }
128 }
129 
130 void te::da::DataSetTypeConverter::getNonConvertedProperties(std::vector<std::size_t>& propertyPos) const
131 {
132  for(std::size_t i = 0; i < m_inDataSetType->size(); ++i)
133  {
134  if(!isConverted(i))
135  propertyPos.push_back(i);
136  }
137 }
138 
139 void te::da::DataSetTypeConverter::getConvertedProperties(const std::string& propertyName, std::vector<std::size_t>& convertedPropertyPos)
140 {
141  std::size_t pos = m_outDataSetType->getPropertyPosition(propertyName);
142  assert(pos != std::string::npos);
143 
144  return getConvertedProperties(pos, convertedPropertyPos);
145 }
146 
147 void te::da::DataSetTypeConverter::getConvertedProperties(std::size_t propertyPos, std::vector<std::size_t>& convertedPropertyPos)
148 {
149  assert(propertyPos >= 0 && propertyPos < m_propertyIndexes.size());
150 
151  std::vector<std::size_t> indexes = m_propertyIndexes[propertyPos];
152 
153  for(std::size_t i = 0; i < indexes.size(); ++i)
154  {
155  assert(indexes[i] >= 0 && indexes[i] < m_inDataSetType->size());
156  convertedPropertyPos.push_back(indexes[i]);
157  }
158 }
159 
160 std::string te::da::DataSetTypeConverter::getConverterName(std::size_t propertyPos)
161 {
162  return m_functionsNames[propertyPos];
163 }
164 
165 void te::da::DataSetTypeConverter::remove(const std::string& propertyName)
166 {
167  std::size_t pos = m_outDataSetType->getPropertyPosition(propertyName);
168  remove(pos);
169 }
170 
171 void te::da::DataSetTypeConverter::remove(std::size_t propertyPos)
172 {
173  assert(propertyPos >= 0 && propertyPos < m_outDataSetType->size());
174 
175  // Property that will be removed
176  te::dt::Property* p = m_outDataSetType->getProperty(propertyPos);
177 
178  // Remove from Converted DataSetType...
179  m_outDataSetType->remove(p);
180 
181  // Adjusting internal indexes...
182  const std::vector<std::size_t> indexes = m_propertyIndexes[propertyPos];
183 
184  for(std::size_t i = 0; i < indexes.size(); ++i)
185  m_convertedProperties[indexes[i]]--;
186 
187  m_propertyIndexes.erase(m_propertyIndexes.begin() + propertyPos);
188 
189  m_converters.erase(m_converters.begin() + propertyPos);
190 }
191 
192 void te::da::DataSetTypeConverter::add(const std::string& propertyName, te::dt::Property* p, const std::string& attributeConverterName)
193 {
194  std::size_t pos = m_inDataSetType->getPropertyPosition(propertyName);
195  add(pos, p);
196 }
197 
198 void te::da::DataSetTypeConverter::add(std::size_t propertyPos, te::dt::Property* p, const std::string& attributeConverterName)
199 {
200  std::vector<std::size_t> indexes;
201  indexes.push_back(propertyPos);
202  add(indexes, p, attributeConverterName);
203 }
204 
206 {
207  std::vector<std::size_t> indexes;
208  indexes.push_back(propertyPos);
209  add(indexes, p, conv);
210 }
211 
212 void te::da::DataSetTypeConverter::add(const std::vector<std::string>& propertyNames, te::dt::Property* p, const std::string& attributeConverterName)
213 {
214  std::vector<std::size_t> indexes;
215 
216  for(std::size_t i = 0; i < propertyNames.size(); ++i)
217  indexes.push_back(m_inDataSetType->getPropertyPosition(propertyNames[i]));
218 
219  add(indexes, p, attributeConverterName);
220 }
221 
222 void te::da::DataSetTypeConverter::add(const std::vector<std::size_t>& propertyPos, te::dt::Property* p, const std::string& attributeConverterName)
223 {
224  assert(!propertyPos.empty());
225  assert(p);
226 
228 
229  // Adding given property on out data set type
230  m_outDataSetType->add(p);
231 
232  // Storing the converted properties indexes
233  m_propertyIndexes.push_back(propertyPos);
234 
235  // Indexing the AttributeConverter functions
236  m_converters.push_back(conv);
237 
238  // Counting reference to converted properties
239  for(std::size_t i = 0; i < propertyPos.size(); ++i)
240  m_convertedProperties[propertyPos[i]]++;
241 
242  m_functionsNames.push_back(attributeConverterName);
243 }
244 
245 void te::da::DataSetTypeConverter::add(const std::vector<std::size_t>& propertyPos, te::dt::Property* p, AttributeConverter conv)
246 {
247  assert(!propertyPos.empty());
248  assert(p);
249 
250  // Adding given property on out data set type
251  m_outDataSetType->add(p);
252 
253  // Storing the converted properties indexes
254  m_propertyIndexes.push_back(propertyPos);
255 
256  // Indexing the AttributeConverter functions
257  m_converters.push_back(conv);
258 
259  // Counting reference to converted properties
260  for(std::size_t i = 0; i < propertyPos.size(); ++i)
261  m_convertedProperties[propertyPos[i]]++;
262 
263  m_functionsNames.push_back("UnknownAttributeConverter");
264 }
265 
266 
268 {
269  assert(type);
270 
271  // Gets the DataTypeCapabilities
272  const DataTypeCapabilities& dtCapabilities = capabilities.getDataTypeCapabilities();
273 
274  const std::vector<te::dt::Property*> properties = type->getProperties();
275  for(std::size_t i = 0; i < properties.size(); ++i)
276  {
277  te::dt::Property* p = properties[i];
278  assert(p);
279 
280  if(!dtCapabilities.supports(p->getType()))
281  return true;
282  }
283 
284  return false;
285 }
286 
288 {
289  return m_outDataSetType;
290 }
291 
292 const std::vector<std::vector<std::size_t> >& te::da::DataSetTypeConverter::getConvertedPropertyIndexes() const
293 {
294  return m_propertyIndexes;
295 }
296 
297 const std::vector<te::da::AttributeConverter>& te::da::DataSetTypeConverter::getConverters() const
298 {
299  return m_converters;
300 }
301 
303 {
304  assert(i < m_convertedProperties.size());
305  return m_convertedProperties[i] > 0;
306 }
DataSetType * m_outDataSetType
The converted DataSetType.
void setTitle(const std::string &title)
It sets a human descriptive title for the DataSetType.
Definition: DataSetType.h:137
Property * getProperty(std::size_t i) const
It returns the i-th property.
const std::vector< std::vector< std::size_t > > & getConvertedPropertyIndexes() const
CharEncoding
Supported charsets (character encoding).
An converter for DataSetType.
A class that models the description of a dataset.
Definition: DataSetType.h:72
bool isConverted(std::size_t i) const
virtual Property * clone() const =0
It returns a clone of the object.
void getNonConvertedProperties(std::vector< std::string > &propertyNames) const
This method returns the name of the properties that have not yet been converted.
PrimaryKey * getPrimaryKey() const
It returns the primary key associated to the dataset type.
Definition: DataSetType.h:214
const DataTypeCapabilities & getDataTypeCapabilities() const
A singleton to keep all the registered Attribute Converter.
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
std::string getConverterName(std::size_t propertyPos)
This method tells which Attribute Converter was used in the porperty position.
It models a property definition.
Definition: Property.h:59
DataSetType * getConvertee()
This method returns the pointer to the DataSetType that is handled by the converter.
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that take part of the primary key.
Definition: PrimaryKey.h:109
const std::vector< AttributeConverter > & getConverters() const
A class that represents the supported data types of a specific data source.
const std::vector< Property * > & getProperties() const
It returns the list of properties describing the CompositeProperty.
static AttributeConverterManager & getInstance()
It returns a reference to the singleton instance.
te::da::DataSourceCapabilities capabilities
void getConvertedProperties(const std::string &propertyName, std::vector< std::size_t > &convertedPropertyPos)
This method tells which properties of the input data set type that have been converted based on the g...
boost::function3< te::dt::AbstractData *, DataSet *, const std::vector< std::size_t > &, int > AttributeConverter
The type of attribute converter functions.
void setProperties(const std::vector< te::dt::Property * > &properties)
It sets the properties that form the primary key.
Definition: PrimaryKey.h:116
DataSetTypeConverter(DataSetType *type)
Constructor.
std::size_t size() const
It returns the number of properties of the CompositeProperty.
std::vector< std::size_t > m_convertedProperties
Internal vector to count the references to converted properties.
int getType() const
It returns the property data type.
Definition: Property.h:161
static bool needConverter(DataSetType *type, const DataSourceCapabilities &capabilities)
Static method that verifies if the given data set type need an converter based on given data source c...
bool supports(const int &type) const
A class that models the description of a dataset.
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
void remove(const std::string &propertyName)
This method removes a property of DataSetTypeConverter.
void add(const std::string &propertyName, te::dt::Property *p, const std::string &attributeConverterName="GenericAttributeConverter")
It adds a conversions to the given property of the input data set type.
virtual const std::string & getName() const
It returns the constraint name.
Definition: Constraint.h:119
AttributeConverter getConverter(const std::string &name)
unsigned int getId() const
It returns the property identifier.
Definition: Property.h:109
const std::string & getName() const
It returns the property name.
Definition: Property.h:127