All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DataSetTypeConverter.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008-2013 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  add(i, p->clone());
77  }
78  else
79  {
80  // Try create the property from data source capabilities hint
81  int hintDataType = dtCapabilities.getHint(p->getType());
82 
83  if(hintDataType != te::dt::UNKNOWN_TYPE)
84  {
85  //add(propertyName, hintDataType, i); TODO: Utility method that returns a te::dt::Property* given a name and a data type.
86  }
87  }
88  }
89 
90  te::da::PrimaryKey* pk = type->getPrimaryKey();
91  if(pk)
92  {
94 
95  std::vector<te::dt::Property*> pkProps = pk->getProperties();
96  std::vector<te::dt::Property*> outPkProps;
97  for(std::size_t i = 0; i < pkProps.size(); ++i)
98  outPkProps.push_back(m_outDataSetType->getProperty(pkProps[i]->getName()));
99 
100  outPk->setProperties(outPkProps);
101  }
102 }
103 
105 {
106  delete m_outDataSetType;
107  delete m_inDataSetType;
108 }
109 
111 {
112  return m_inDataSetType;
113 }
114 
115 void te::da::DataSetTypeConverter::getNonConvertedProperties(std::vector<std::string>& propertyNames) const
116 {
117  for(std::size_t i = 0; i < m_inDataSetType->size(); ++i)
118  {
119  if(!isConverted(i))
120  propertyNames.push_back(m_inDataSetType->getProperty(i)->getName());
121  }
122 }
123 
124 void te::da::DataSetTypeConverter::getNonConvertedProperties(std::vector<std::size_t>& propertyPos) const
125 {
126  for(std::size_t i = 0; i < m_inDataSetType->size(); ++i)
127  {
128  if(!isConverted(i))
129  propertyPos.push_back(i);
130  }
131 }
132 
133 void te::da::DataSetTypeConverter::getConvertedProperties(const std::string& propertyName, std::vector<std::size_t>& convertedPropertyPos)
134 {
135  std::size_t pos = m_outDataSetType->getPropertyPosition(propertyName);
136  assert(pos != std::string::npos);
137 
138  return getConvertedProperties(pos, convertedPropertyPos);
139 }
140 
141 void te::da::DataSetTypeConverter::getConvertedProperties(std::size_t propertyPos, std::vector<std::size_t>& convertedPropertyPos)
142 {
143  assert(propertyPos >= 0 && propertyPos < m_propertyIndexes.size());
144 
145  std::vector<std::size_t> indexes = m_propertyIndexes[propertyPos];
146 
147  for(std::size_t i = 0; i < indexes.size(); ++i)
148  {
149  assert(indexes[i] >= 0 && indexes[i] < m_inDataSetType->size());
150  convertedPropertyPos.push_back(indexes[i]);
151  }
152 }
153 
154 std::string te::da::DataSetTypeConverter::getConverterName(std::size_t propertyPos)
155 {
156  return m_functionsNames[propertyPos];
157 }
158 
159 void te::da::DataSetTypeConverter::remove(const std::string& propertyName)
160 {
161  std::size_t pos = m_outDataSetType->getPropertyPosition(propertyName);
162  remove(pos);
163 }
164 
165 void te::da::DataSetTypeConverter::remove(std::size_t propertyPos)
166 {
167  assert(propertyPos >= 0 && propertyPos < m_outDataSetType->size());
168 
169  // Property that will be removed
170  te::dt::Property* p = m_outDataSetType->getProperty(propertyPos);
171 
172  // Remove from Converted DataSetType...
173  m_outDataSetType->remove(p);
174 
175  // Adjusting internal indexes...
176  const std::vector<std::size_t> indexes = m_propertyIndexes[propertyPos];
177 
178  for(std::size_t i = 0; i < indexes.size(); ++i)
179  m_convertedProperties[indexes[i]]--;
180 
181  m_propertyIndexes.erase(m_propertyIndexes.begin() + propertyPos);
182 
183  m_converters.erase(m_converters.begin() + propertyPos);
184 }
185 
186 void te::da::DataSetTypeConverter::add(const std::string& propertyName, te::dt::Property* p, const std::string& attributeConverterName)
187 {
188  std::size_t pos = m_inDataSetType->getPropertyPosition(propertyName);
189  add(pos, p);
190 }
191 
192 void te::da::DataSetTypeConverter::add(std::size_t propertyPos, te::dt::Property* p, const std::string& attributeConverterName)
193 {
194  std::vector<std::size_t> indexes;
195  indexes.push_back(propertyPos);
196  add(indexes, p, attributeConverterName);
197 }
198 
199 void te::da::DataSetTypeConverter::add(const std::vector<std::string>& propertyNames, te::dt::Property* p, const std::string& attributeConverterName)
200 {
201  std::vector<std::size_t> indexes;
202 
203  for(std::size_t i = 0; i < propertyNames.size(); ++i)
204  indexes.push_back(m_inDataSetType->getPropertyPosition(propertyNames[i]));
205 
206  add(indexes, p, attributeConverterName);
207 }
208 
209 void te::da::DataSetTypeConverter::add(const std::vector<std::size_t>& propertyPos, te::dt::Property* p, const std::string& attributeConverterName)
210 {
211  assert(!propertyPos.empty());
212  assert(p);
213 
215 
216  // Adding given property on out data set type
217  m_outDataSetType->add(p);
218 
219  // Storing the converted properties indexes
220  m_propertyIndexes.push_back(propertyPos);
221 
222  // Indexing the AttributeConverter functions
223  m_converters.push_back(conv);
224 
225  // Counting reference to converted properties
226  for(std::size_t i = 0; i < propertyPos.size(); ++i)
227  m_convertedProperties[propertyPos[i]]++;
228 
229  m_functionsNames.push_back(attributeConverterName);
230 }
231 
233 {
234  assert(type);
235 
236  // Gets the DataTypeCapabilities
237  const DataTypeCapabilities& dtCapabilities = capabilities.getDataTypeCapabilities();
238 
239  const std::vector<te::dt::Property*> properties = type->getProperties();
240  for(std::size_t i = 0; i < properties.size(); ++i)
241  {
242  te::dt::Property* p = properties[i];
243  assert(p);
244 
245  if(!dtCapabilities.supports(p->getType()))
246  return true;
247  }
248 
249  return false;
250 }
251 
253 {
254  return m_outDataSetType;
255 }
256 
257 const std::vector<std::vector<std::size_t> >& te::da::DataSetTypeConverter::getConvertedPropertyIndexes() const
258 {
259  return m_propertyIndexes;
260 }
261 
262 const std::vector<te::da::AttributeConverter>& te::da::DataSetTypeConverter::getConverters() const
263 {
264  return m_converters;
265 }
266 
268 {
269  assert(i < m_convertedProperties.size());
270  return m_convertedProperties[i] > 0;
271 }
A class that models the description of a dataset.
A singleton to keep all the registered Attribute Converter.
void remove(const std::string &propertyName)
This method removes a property of DataSetTypeConverter.
bool supports(const int &type) const
std::vector< std::size_t > m_convertedProperties
Internal vector to count the references to converted properties.
Property * getProperty(std::size_t i) const
It returns the i-th property.
std::size_t size() const
It returns the number of properties of the CompositeProperty.
AttributeConverter getConverter(const std::string &name)
bool isConverted(std::size_t i) const
boost::function3< te::dt::AbstractData *, DataSet *, const std::vector< std::size_t > &, int > AttributeConverter
The type of attribute converter functions.
DataSetType * m_outDataSetType
The converted DataSetType.
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
const std::string & getName() const
It returns the property name.
Definition: Property.h:126
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
const std::vector< AttributeConverter > & getConverters() const
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that take part of the primary key.
Definition: PrimaryKey.h:109
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.
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...
const std::vector< Property * > & getProperties() const
It returns the list of properties describing the CompositeProperty.
virtual const std::string & getName() const
It returns the constraint name.
Definition: Constraint.h:119
DataSetTypeConverter(DataSetType *type)
Constructor.
const DataTypeCapabilities & getDataTypeCapabilities() const
A class that models the description of a dataset.
Definition: DataSetType.h:72
An converter for DataSetType.
It models a property definition.
Definition: Property.h:59
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...
int getType() const
It returns the property data type.
Definition: Property.h:143
static AttributeConverterManager & getInstance()
It returns a reference to the singleton instance.
void setTitle(const std::string &title)
It sets a human descriptive title for the DataSetType.
Definition: DataSetType.h:137
PrimaryKey * getPrimaryKey() const
It returns the primary key associated to the dataset type.
Definition: DataSetType.h:214
unsigned int getId() const
It returns the property identifier.
Definition: Property.h:108
std::string getConverterName(std::size_t propertyPos)
This method tells which Attribute Converter was used in the porperty position.
A class that represents the supported data types of a specific data source.
void setProperties(const std::vector< te::dt::Property * > &properties)
It sets the properties that form the primary key.
Definition: PrimaryKey.h:116
virtual Property * clone() const =0
It returns a clone of the object.
const std::vector< std::vector< std::size_t > > & getConvertedPropertyIndexes() const
te::da::DataSourceCapabilities capabilities
void getNonConvertedProperties(std::vector< std::string > &propertyNames) const
This method returns the name of the properties that have not yet been converted.
DataSetType * getConvertee()
This method returns the pointer to the DataSetType that is handled by the converter.