Promoter.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 #include "Promoter.h"
20 
21 // TerraLib
22 #include "../../../common/STLUtils.h"
23 #include "../../../core/translator/Translator.h"
24 #include "../../../dataaccess/dataset/DataSet.h"
25 #include "../../../dataaccess/dataset/ObjectId.h"
26 #include "../../../dataaccess/dataset/ObjectIdSet.h"
27 #include "../../../datatype/SimpleData.h"
28 #include "../../../dataaccess/utils/Utils.h"
29 #include "../Exception.h"
30 #include "../Config.h"
31 
32 // Qt
33 #include <QObject>
34 
36 {
40 };
41 
42 COMPARISON CompareStr (const std::string& lhs, const std::string& rhs)
43 {
44  int aux = lhs.compare(rhs);
45 
46  COMPARISON comp = (aux > 0) ? GREATER : (aux < 0) ? LESSER : EQUAL;
47 
48  return comp;
49 }
50 
51 template <typename T>
52 COMPARISON CompareSimpleData(const T& lhs, const T& rhs)
53 {
54  COMPARISON res;
55 
56  if(lhs.getValue() > rhs.getValue())
57  res = GREATER;
58  else if(lhs.getValue() < rhs.getValue())
59  res = LESSER;
60  else
61  res = EQUAL;
62 
63  return res;
64 }
65 
67 {
68  if(lhs == nullptr)
69  return GREATER;
70  if(rhs == nullptr)
71  return LESSER;
72 
73  COMPARISON res = EQUAL;
74 
75  if(lhs->getTypeCode() == te::dt::CHAR_TYPE)
76  res = CompareSimpleData<te::dt::Char>(*((te::dt::Char*)lhs), *((te::dt::Char*)rhs));
77  else if(lhs->getTypeCode() == te::dt::UCHAR_TYPE)
78  res = CompareSimpleData<te::dt::UChar>(*((te::dt::UChar*)lhs), *((te::dt::UChar*)rhs));
79  else if(lhs->getTypeCode() == te::dt::INT16_TYPE)
80  res = CompareSimpleData<te::dt::Int16>(*((te::dt::Int16*)lhs), *((te::dt::Int16*)rhs));
81  else if(lhs->getTypeCode() == te::dt::UINT16_TYPE)
82  res = CompareSimpleData<te::dt::UInt16>(*((te::dt::UInt16*)lhs), *((te::dt::UInt16*)rhs));
83  else if(lhs->getTypeCode() == te::dt::INT32_TYPE)
84  res = CompareSimpleData<te::dt::Int32>(*((te::dt::Int32*)lhs), *((te::dt::Int32*)rhs));
85  else if(lhs->getTypeCode() == te::dt::UINT32_TYPE)
86  res = CompareSimpleData<te::dt::UInt32>(*((te::dt::UInt32*)lhs), *((te::dt::UInt32*)rhs));
87  else if(lhs->getTypeCode() == te::dt::INT64_TYPE)
88  res = CompareSimpleData<te::dt::Int64>(*((te::dt::Int64*)lhs), *((te::dt::Int64*)rhs));
89  else if(lhs->getTypeCode() == te::dt::UINT64_TYPE)
90  res = CompareSimpleData<te::dt::UInt64>(*((te::dt::UInt64*)lhs), *((te::dt::UInt64*)rhs));
91  else if(lhs->getTypeCode() == te::dt::BOOLEAN_TYPE)
92  res = CompareSimpleData<te::dt::Boolean>(*((te::dt::Boolean*)lhs), *((te::dt::Boolean*)rhs));
93  else if(lhs->getTypeCode() == te::dt::FLOAT_TYPE)
94  res = CompareSimpleData<te::dt::Float>(*((te::dt::Float*)lhs), *((te::dt::Float*)rhs));
95  else if(lhs->getTypeCode() == te::dt::DOUBLE_TYPE)
96  res = CompareSimpleData<te::dt::Double>(*((te::dt::Double*)lhs), *((te::dt::Double*)rhs));
97  else if(lhs->getTypeCode() == te::dt::STRING_TYPE)
98  res = CompareStr(((te::dt::String*)lhs)->getValue(), ((te::dt::String*) rhs)->getValue());
99 
100  return res;
101 }
102 
103 std::vector<std::string> GetColumnsNames(te::da::DataSet* dset, const std::vector<size_t>& colsPositions)
104 {
105  std::vector<std::string> res;
106  std::vector<size_t>::const_iterator it;
107 
108  for(it=colsPositions.begin(); it!=colsPositions.end(); ++it)
109  res.push_back(dset->getPropertyName(*it));
110 
111  return res;
112 }
113 
114 template <class T>
115 void ClearVector(std::vector<T>& vec)
116 {
117  std::vector<T>().swap(vec);
118 }
119 
120 size_t GetRowPosition(const size_t& pos, const std::vector<size_t>& posVec)
121 {
122  for(size_t i=pos; i<posVec.size(); i++)
123  if(posVec[i] == pos)
124  return i;
125 
126  for(size_t i=0; i<posVec.size(); i++)
127  if(posVec[i] == pos)
128  return i;
129 
130  throw te::qt::widgets::Exception(TE_TR("Position not found."));
131 }
132 
134 
136 {
138 }
139 
141 {
142  for(size_t i=0; i<m_logicalRows.size(); i++)
143  m_logicalRows[i] = i;
144 }
145 
147 {
149 
151 }
152 
153 void te::qt::widgets::Promoter::preProcessKeys(te::da::DataSet* dset, const std::vector<size_t>& pkeys)
154 {
155  if(!m_PkeysRows.empty())
156  return;
157 
158  size_t setSize = dset->size();
159 
161 
162  if(m_logicalRows.empty())
163  m_logicalRows.resize(setSize);
164 
165  dset->moveFirst();
166 
167  for(size_t i=0; i<setSize; i++)
168  {
169  std::string pkey;
170 
171  for(size_t aux=0; aux<pkeys.size(); aux++)
172  {
173  if(dset->isNull(pkeys[aux]) == false)
174  pkey += dset->getAsString(pkeys[aux]) + ";";
175  }
176 
177  m_PkeysRows[pkey] = i;
178 
179  m_logicalRows[i] = i;
180 
181  dset->moveNext();
182  }
183 }
184 
185 size_t te::qt::widgets::Promoter::getLogicalRow(const size_t& visualRow)
186 {
187  return m_logicalRows.empty() ? visualRow : m_logicalRows[visualRow];
188 }
189 
191 {
192  m_PkeysRows.clear();
193 }
194 
196 {
197  if(m_logicalRows.empty() || m_PkeysRows.empty() || oids == nullptr)
198  return;
199 
200  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::const_iterator it;
201 
202  resetPromotion();
203 
204  std::map<size_t, size_t> new_pos;
205 
206  for(it = oids->begin(); it != oids->end(); ++it)
207  {
208  size_t dsPos = map2Row(*it);
209  new_pos[dsPos] = m_logicalRows[dsPos];
210  }
211 
212  std::map<size_t, size_t>::iterator mit;
213 
214  size_t pos = 0;
215 
216  for(mit = new_pos.begin(); mit != new_pos.end(); ++mit)
217  {
218  m_logicalRows.erase(m_logicalRows.begin()+mit->first);
219  m_logicalRows.insert(m_logicalRows.begin()+pos, mit->second);
220  pos++;
221  }
222 }
223 
225 {
226  boost::ptr_vector<te::dt::AbstractData> data = oid->getValue();
227  boost::ptr_vector<te::dt::AbstractData>::iterator it_d;
228 
229  std::string pkey;
230 
231  for(it_d=data.begin(); it_d!=data.end(); ++it_d)
232  pkey += it_d->toString() + ";";
233 
234  std::map<std::string, size_t>::iterator it = m_PkeysRows.find(pkey);
235 
236  if(it == m_PkeysRows.end())
237  throw Exception(TE_TR("Fail to get position of Object id"));
238 
239  return GetRowPosition(it->second, m_logicalRows);
240 }
size_t map2Row(te::da::ObjectId *oid)
Given an object id returns its row.
Definition: Promoter.cpp:224
Base exception class for plugin module.
Defines an mechanism for logical ordering of rows.
const boost::ptr_vector< te::dt::AbstractData > & getValue() const
It gets the properties values used to uniquely identify a data set element.
COMPARISON
Definition: Promoter.cpp:35
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
virtual std::size_t size() const =0
It returns the collection size, if it is known.
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
COMPARISON CompareAbsData(te::dt::AbstractData *lhs, te::dt::AbstractData *rhs)
Definition: Promoter.cpp:66
std::vector< size_t > m_logicalRows
Map original row to logical postion.
Definition: Promoter.h:136
size_t GetRowPosition(const size_t &pos, const std::vector< size_t > &posVec)
Definition: Promoter.cpp:120
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
This class represents an unique id for a data set element.
COMPARISON CompareSimpleData(const T &lhs, const T &rhs)
Definition: Promoter.cpp:52
COMPARISON CompareStr(const std::string &lhs, const std::string &rhs)
Definition: Promoter.cpp:42
virtual std::string getAsString(std::size_t i, int precision=0) const
Method for retrieving a data value as a string plain representation.
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
void promote(const te::da::ObjectIdSet *oids)
Promotes the rows identified by oids primary keys.
Definition: Promoter.cpp:195
void ClearVector(std::vector< T > &vec)
Definition: Promoter.cpp:115
virtual int getTypeCode() const =0
It returns the data type code associated to the data value.
A dataset is the unit of information manipulated by the data access module of TerraLib.
void preProcessKeys(te::da::DataSet *dset, const std::vector< size_t > &pkeys)
Proccess primary keys and stores it.
Definition: Promoter.cpp:153
~Promoter()
Destructor.
Definition: Promoter.cpp:135
std::set< ObjectId *, te::common::LessCmp< ObjectId * > >::const_iterator end() const
Returns an iterator for the object ids in container.
void cleanPreproccessKeys()
Cleans the storage of the primary keys. Next time enabled, will be necessary to proccess all primary ...
Definition: Promoter.cpp:190
std::vector< std::string > GetColumnsNames(te::da::DataSet *dset, const std::vector< size_t > &colsPositions)
Definition: Promoter.cpp:103
size_t getLogicalRow(const size_t &visualRow)
Returns the logical position of the row visualRow.
Definition: Promoter.cpp:185
virtual bool isNull(std::size_t i) const =0
It checks if the attribute value is NULL.
A template for atomic data types (integers, floats, strings and others).
Definition: SimpleData.h:59
std::map< std::string, size_t > m_PkeysRows
Map object id to its position in DataSet.
Definition: Promoter.h:134
virtual std::string getPropertyName(std::size_t i) const =0
It returns the property name at position pos.
std::set< ObjectId *, te::common::LessCmp< ObjectId * > >::const_iterator begin() const
Returns an iterator for the object ids in container.
virtual bool moveFirst()=0
It moves the internal pointer to the first item in the collection.
void resetPromotion()
Returns the rows to its original positions.
Definition: Promoter.cpp:140