All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Promoter.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011-2012 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 "../../../common/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 == 0)
69  return GREATER;
70  if(rhs == 0)
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(TR_QT_WIDGETS("Position not found."));
131 }
132 
134 {
135 }
136 
138 {
139  cleanPreproccessKeys();
140 }
141 
143 {
144  for(size_t i=0; i<m_logicalRows.size(); i++)
145  m_logicalRows[i] = i;
146 }
147 
149 {
150  cleanPreproccessKeys();
151 
152  ClearVector(m_logicalRows);
153 }
154 
155 void te::qt::widgets::Promoter::preProcessKeys(te::da::DataSet* dset, const std::vector<size_t>& pkeys)
156 {
157  size_t setSize = dset->size();
158 
159  cleanPreproccessKeys();
160 
161  if(m_logicalRows.empty())
162  m_logicalRows.resize(setSize);
163 
164  dset->moveFirst();
165 
166  for(size_t i=0; i<setSize; i++)
167  {
168  std::string pkey;
169 
170  for(size_t aux=0; aux<pkeys.size(); aux++)
171  pkey += dset->getAsString(pkeys[aux]) + ";";
172 
173  m_PkeysRows[pkey] = i;
174 
175  m_logicalRows[i] = i;
176 
177  dset->moveNext();
178  }
179 }
180 
181 size_t te::qt::widgets::Promoter::getLogicalRow(const size_t& visualRow)
182 {
183  return m_logicalRows.empty() ? visualRow : m_logicalRows[visualRow];
184 }
185 
187 {
188  m_PkeysRows.clear();
189 }
190 
192 {
193  if(m_logicalRows.empty() || m_PkeysRows.empty())
194  return;
195 
196  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::const_iterator it;
197 
198  resetPromotion();
199 
200  std::map<size_t, size_t> new_pos;
201 
202  for(it = oids->begin(); it != oids->end(); ++it)
203  {
204  size_t dsPos = map2Row(*it);
205  new_pos[dsPos] = m_logicalRows[dsPos];
206  }
207 
208  std::map<size_t, size_t>::iterator mit;
209 
210  size_t pos = 0;
211 
212  for(mit = new_pos.begin(); mit != new_pos.end(); ++mit)
213  {
214  m_logicalRows.erase(m_logicalRows.begin()+mit->first);
215  m_logicalRows.insert(m_logicalRows.begin()+pos, mit->second);
216  pos++;
217  }
218 }
219 
221 {
222  boost::ptr_vector<te::dt::AbstractData> data = oid->getValue();
223  boost::ptr_vector<te::dt::AbstractData>::iterator it_d;
224 
225  std::string pkey;
226 
227  for(it_d=data.begin(); it_d!=data.end(); ++it_d)
228  pkey += it_d->toString() + ";";
229 
230  std::map<std::string, size_t>::iterator it = m_PkeysRows.find(pkey);
231 
232  if(it == m_PkeysRows.end())
233  throw Exception(TR_QT_WIDGETS("Fail to get position of Object id"));
234 
235  return GetRowPosition(it->second, m_logicalRows);
236 }
virtual std::string getPropertyName(std::size_t i) const =0
It returns the property name at position pos.
size_t getLogicalRow(const size_t &visualRow)
Returns the logical position of the row visualRow.
Definition: Promoter.cpp:181
Defines an mechanism for logical ordering of rows.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:53
virtual std::string getAsString(std::size_t i, int precision=0) const
Method for retrieving a data value as a string plain representation.
Definition: DataSet.cpp:218
virtual int getTypeCode() const =0
It returns the data type code associated to the data value.
const boost::ptr_vector< te::dt::AbstractData > & getValue() const
It gets the properties values used to uniquely identify a data set element.
Definition: ObjectId.cpp:46
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
A template for atomic data types (integers, floats, strings and others).
Definition: SimpleData.h:59
size_t map2Row(te::da::ObjectId *oid)
Given an object id returns its row.
Definition: Promoter.cpp:220
void ClearVector(std::vector< T > &vec)
Definition: Promoter.cpp:115
std::vector< std::string > GetColumnsNames(te::da::DataSet *dset, const std::vector< size_t > &colsPositions)
Definition: Promoter.cpp:103
void cleanPreproccessKeys()
Cleans the storage of the primary keys. Next time enabled, will be necessary to proccess all primary ...
Definition: Promoter.cpp:186
void preProcessKeys(te::da::DataSet *dset, const std::vector< size_t > &pkeys)
Proccess primary keys and stores it.
Definition: Promoter.cpp:155
COMPARISON CompareSimpleData(const T &lhs, const T &rhs)
Definition: Promoter.cpp:52
void promote(const te::da::ObjectIdSet *oids)
Promotes the rows identified by oids primary keys.
Definition: Promoter.cpp:191
size_t GetRowPosition(const size_t &pos, const std::vector< size_t > &posVec)
Definition: Promoter.cpp:120
Promoter()
Constructor.
Definition: Promoter.cpp:133
std::set< ObjectId *, te::common::LessCmp< ObjectId * > >::const_iterator begin() const
Returns an iterator for the object ids in container.
std::set< ObjectId *, te::common::LessCmp< ObjectId * > >::const_iterator end() const
Returns an iterator for the object ids in container.
This class represents an unique id for a data set element.
Definition: ObjectId.h:47
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
COMPARISON CompareAbsData(te::dt::AbstractData *lhs, te::dt::AbstractData *rhs)
Definition: Promoter.cpp:66
~Promoter()
Destructor.
Definition: Promoter.cpp:137
#define TR_QT_WIDGETS(message)
It marks a string in order to get translated. This is a special mark used in the TerraLib Qt Widgets ...
Definition: Config.h:60
virtual std::size_t size() const =0
It returns the collection size, if it is known.
virtual bool moveFirst()=0
It moves the internal pointer to the first item in the collection.
COMPARISON
Definition: Promoter.cpp:35
void resetPromotion()
Returns the rows to its original positions.
Definition: Promoter.cpp:142
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:111
COMPARISON CompareStr(const std::string &lhs, const std::string &rhs)
Definition: Promoter.cpp:42