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 "../../core/translator/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  m_encondingType(te::core::EncodingType::UTF8)
41 {
42  assert(type);
43 
44  // By default no property is automatically converted
45  for(std::size_t i = 0; i != type->size(); ++i)
46  m_convertedProperties.push_back(0);
47 
48  // Creates the empty converted DataSetType
49  m_outDataSetType = new DataSetType(type->getName(), type->getId());
51 }
52 
54  : m_inDataSetType(dynamic_cast<DataSetType*>(type->clone())),
55  m_encondingType(et)
56 {
57  assert(type);
58 
59  // By default no property is automatically converted
60  for(std::size_t i = 0; i != type->size(); ++i)
61  m_convertedProperties.push_back(0);
62 
63  // Creates the empty converted DataSetType
64  m_outDataSetType = new DataSetType(type->getName(), type->getId());
65 
66  // Gets the DataTypeCapabilities
67  const DataTypeCapabilities& dtCapabilities = capabilities.getDataTypeCapabilities();
68 
69  // Try automatic build the conversion
70  const std::vector<te::dt::Property*> properties = type->getProperties();
71  for(std::size_t i = 0; i < properties.size(); ++i)
72  {
73  te::dt::Property* p = properties[i];
74  assert(p);
75 
76  if(dtCapabilities.supports(p->getType()))
77  {
78  if(p->getType() == te::dt::STRING_TYPE)
79  {
80  CharEncodingConverter cec(et);
81  add(i, p->clone(), cec);
82  }
83  else
84  add(i, p->clone());
85  }
86  else
87  {
88  // Try create the property from data source capabilities hint
89  int hintDataType = dtCapabilities.getHint(p->getType());
90 
91  if(hintDataType != te::dt::UNKNOWN_TYPE)
92  {
93  //add(propertyName, hintDataType, i); TODO: Utility method that returns a te::dt::Property* given a name and a data type.
94  }
95  }
96  }
97 
98  te::da::PrimaryKey* pk = type->getPrimaryKey();
99  if(pk)
100  {
102 
103  std::vector<te::dt::Property*> pkProps = pk->getProperties();
104  std::vector<te::dt::Property*> outPkProps;
105  for(std::size_t i = 0; i < pkProps.size(); ++i)
106  outPkProps.push_back(m_outDataSetType->getProperty(pkProps[i]->getName()));
107 
108  outPk->setProperties(outPkProps);
109  }
110 }
111 
113 {
114  delete m_outDataSetType;
115  delete m_inDataSetType;
116 }
117 
119 {
120  return m_inDataSetType;
121 }
122 
123 void te::da::DataSetTypeConverter::getNonConvertedProperties(std::vector<std::string>& propertyNames) const
124 {
125  for(std::size_t i = 0; i < m_inDataSetType->size(); ++i)
126  {
127  if(!isConverted(i))
128  propertyNames.push_back(m_inDataSetType->getProperty(i)->getName());
129  }
130 }
131 
132 void te::da::DataSetTypeConverter::getNonConvertedProperties(std::vector<std::size_t>& propertyPos) const
133 {
134  for(std::size_t i = 0; i < m_inDataSetType->size(); ++i)
135  {
136  if(!isConverted(i))
137  propertyPos.push_back(i);
138  }
139 }
140 
141 void te::da::DataSetTypeConverter::getConvertedProperties(const std::string& propertyName, std::vector<std::size_t>& convertedPropertyPos)
142 {
143  std::size_t pos = m_outDataSetType->getPropertyPosition(propertyName);
144  assert(pos != std::string::npos);
145 
146  return getConvertedProperties(pos, convertedPropertyPos);
147 }
148 
149 void te::da::DataSetTypeConverter::getConvertedProperties(std::size_t propertyPos, std::vector<std::size_t>& convertedPropertyPos)
150 {
151  assert(propertyPos >= 0 && propertyPos < m_propertyIndexes.size());
152 
153  std::vector<std::size_t> indexes = m_propertyIndexes[propertyPos];
154 
155  for(std::size_t i = 0; i < indexes.size(); ++i)
156  {
157  assert(indexes[i] >= 0 && indexes[i] < m_inDataSetType->size());
158  convertedPropertyPos.push_back(indexes[i]);
159  }
160 }
161 
162 std::string te::da::DataSetTypeConverter::getConverterName(std::size_t propertyPos)
163 {
164  return m_functionsNames[propertyPos];
165 }
166 
167 void te::da::DataSetTypeConverter::remove(const std::string& propertyName)
168 {
169  std::size_t pos = m_outDataSetType->getPropertyPosition(propertyName);
170  remove(pos);
171 }
172 
173 void te::da::DataSetTypeConverter::remove(std::size_t propertyPos)
174 {
175  assert(propertyPos >= 0 && propertyPos < m_outDataSetType->size());
176 
177  // Property that will be removed
179 
180  // Remove from Converted DataSetType...
182 
183  // Adjusting internal indexes...
184  const std::vector<std::size_t> indexes = m_propertyIndexes[propertyPos];
185 
186  for(std::size_t i = 0; i < indexes.size(); ++i)
187  m_convertedProperties[indexes[i]]--;
188 
189  m_propertyIndexes.erase(m_propertyIndexes.begin() + propertyPos);
190 
191  m_converters.erase(m_converters.begin() + propertyPos);
192 }
193 
195  const std::string& propertyName, te::dt::Property* p,
196  const std::string& /*attributeConverterName*/)
197 {
198  std::size_t pos = m_inDataSetType->getPropertyPosition(propertyName);
199  add(pos, p);
200 }
201 
202 void te::da::DataSetTypeConverter::add(std::size_t propertyPos, te::dt::Property* p, const std::string& attributeConverterName)
203 {
204  std::vector<std::size_t> indexes;
205  indexes.push_back(propertyPos);
206  add(indexes, p, attributeConverterName);
207 }
208 
210 {
211  std::vector<std::size_t> indexes;
212  indexes.push_back(propertyPos);
213  add(indexes, p, conv);
214 }
215 
216 void te::da::DataSetTypeConverter::add(const std::vector<std::string>& propertyNames, te::dt::Property* p, const std::string& attributeConverterName)
217 {
218  std::vector<std::size_t> indexes;
219 
220  for(std::size_t i = 0; i < propertyNames.size(); ++i)
221  indexes.push_back(m_inDataSetType->getPropertyPosition(propertyNames[i]));
222 
223  add(indexes, p, attributeConverterName);
224 }
225 
226 void te::da::DataSetTypeConverter::add(const std::vector<std::size_t>& propertyPos, te::dt::Property* p, const std::string& attributeConverterName)
227 {
228  assert(!propertyPos.empty());
229  assert(p);
230 
231  te::da::AttributeConverter conv = te::da::AttributeConverterManager::getInstance().getConverter(attributeConverterName);
232 
233  // Adding given property on out data set type
234  m_outDataSetType->add(p);
235 
236  // Storing the converted properties indexes
237  m_propertyIndexes.push_back(propertyPos);
238 
239  // Indexing the AttributeConverter functions
240  m_converters.push_back(conv);
241 
242  // Counting reference to converted properties
243  for(std::size_t i = 0; i < propertyPos.size(); ++i)
244  m_convertedProperties[propertyPos[i]]++;
245 
246  m_functionsNames.push_back(attributeConverterName);
247 }
248 
249 void te::da::DataSetTypeConverter::add(const std::vector<std::size_t>& propertyPos, te::dt::Property* p, AttributeConverter conv)
250 {
251  assert(!propertyPos.empty());
252  assert(p);
253 
254  // Adding given property on out data set type
255  m_outDataSetType->add(p);
256 
257  // Storing the converted properties indexes
258  m_propertyIndexes.push_back(propertyPos);
259 
260  // Indexing the AttributeConverter functions
261  m_converters.push_back(conv);
262 
263  // Counting reference to converted properties
264  for(std::size_t i = 0; i < propertyPos.size(); ++i)
265  m_convertedProperties[propertyPos[i]]++;
266 
267  m_functionsNames.push_back("UnknownAttributeConverter");
268 }
269 
271 {
272  return m_encondingType;
273 }
274 
276 {
277  m_encondingType = et;
278 
279  const std::vector<te::dt::Property*> properties = m_outDataSetType->getProperties();
280 
281  for (std::size_t i = 0; i < properties.size(); ++i)
282  {
283  te::dt::Property* p = properties[i];
284 
285  if (p->getType() == te::dt::STRING_TYPE)
286  {
287  CharEncodingConverter cec(et);
288  m_converters[i] = cec;
289  }
290  }
291 }
292 
294 {
295  assert(type);
296 
297  // Gets the DataTypeCapabilities
298  const DataTypeCapabilities& dtCapabilities = capabilities.getDataTypeCapabilities();
299 
300  const std::vector<te::dt::Property*> properties = type->getProperties();
301  for(std::size_t i = 0; i < properties.size(); ++i)
302  {
303  te::dt::Property* p = properties[i];
304  assert(p);
305 
306  if(!dtCapabilities.supports(p->getType()))
307  return true;
308  }
309 
310  return false;
311 }
312 
314 {
315  return m_outDataSetType;
316 }
317 
318 const std::vector<std::vector<std::size_t> >& te::da::DataSetTypeConverter::getConvertedPropertyIndexes() const
319 {
320  return m_propertyIndexes;
321 }
322 
323 const std::vector<te::da::AttributeConverter>& te::da::DataSetTypeConverter::getConverters() const
324 {
325  return m_converters;
326 }
327 
329 {
330  assert(i < m_convertedProperties.size());
331  return m_convertedProperties[i] > 0;
332 }
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
te::core::EncodingType m_encondingType
The enconding type used.
void setEncodingType(te::core::EncodingType et)
It changes all encoding types from m_converters property that are CharEncodingConverter type...
An converter for DataSetType.
A class that models the description of a dataset.
Definition: DataSetType.h:72
bool isConverted(std::size_t i) const
std::vector< std::vector< std::size_t > > m_propertyIndexes
A vector that stores the converted property indexes.
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...
virtual Property * clone() const =0
It returns a clone of the object.
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
EncodingType
Supported character encodings.
Definition: CharEncoding.h:50
const std::vector< AttributeConverter > & getConverters() const
A class that represents the supported data types of a specific data source.
void remove(Constraint *c)
It removes the constraint.
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
URI C++ Library.
Definition: Attributes.h:37
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.
te::gm::Polygon * p
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.
std::vector< AttributeConverter > m_converters
A vector that stores the attribute converters functions.
int getType() const
It returns the property data type.
Definition: Property.h:161
void add(Constraint *c)
It adds a new constraint.
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.
te::core::EncodingType getEncodingType()
It gets the encoding type used to convert.
std::size_t getPropertyPosition(const std::string &name) const
It returns the property position based on its name.
DataSetType * m_inDataSetType
A pointer to DataSetType that will be converted.
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
unsigned int getId() const
It returns the property identifier.
Definition: Property.h:109
std::vector< std::string > m_functionsNames
A vector that stores the attribute converters functions names.
const std::string & getName() const
It returns the property name.
Definition: Property.h:127