binding/v8/jsi/dataaccess/DataSourceCatalog.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 DataSourceCatalog.cpp
22 
23  \brief Utility functions to register the DataSourceCatalog class into the Google JavaScript V8 engine.
24  */
25 
26 // TerraLib
27 #include "../../../../dataaccess/datasource/DataSourceCatalog.h"
28 #include "../../common/Utils.h"
29 #include "DataAccess.h"
30 
31 // STL
32 //#include <memory>
33 
34 // Boost
35 #include <boost/cstdint.hpp>
36 
37 ::v8::Handle<::v8::Value> DataSourceCatalog_GetId(const ::v8::Arguments& args)
38 {
39  ::v8::HandleScope hs;
40 
41  if(args.Holder().IsEmpty())
42  return ::v8::ThrowException(::v8::String::New("In order to use the getId method, you must use the object notation: \"id = obj.getId();\""));
43 
44  te::da::DataSourceCatalog* catalog = te::v8::common::Unwrap<te::da::DataSourceCatalog>(args.Holder());
45 
46  if(catalog == 0)
47  return ::v8::ThrowException(::v8::String::New("Invalid catalog in the getId method!"));
48 
49  unsigned int id = catalog->getId();
50 
51  ::v8::Local<::v8::Integer> jid = ::v8::Uint32::New(static_cast<boost::uint32_t>(id));
52 
53  return hs.Close(jid);
54 }
55 
56 ::v8::Handle<::v8::Value> DataSourceCatalog_SetId(const ::v8::Arguments& args)
57 {
58  ::v8::HandleScope hs;
59 
60  if(args.Holder().IsEmpty())
61  return ::v8::ThrowException(::v8::String::New("In order to use setId method, you must use the object notation: \"obj.setId();\""));
62 
63  te::da::DataSourceCatalog* catalog = te::v8::common::Unwrap<te::da::DataSourceCatalog>(args.Holder());
64 
65  if(catalog == 0)
66  return ::v8::ThrowException(::v8::String::New("Invalid catalog in the getId method!"));
67 
68  if((args.Length() == 1) && !args[0].IsEmpty() && args[0]->IsUint32())
69  {
70  unsigned int id = args[0]->ToUint32()->Value();
71  catalog->setId(id);
72  }
73 
74  // Completar com o setId(DatasetType* dt, unsigned int id) e setId(Sequence* s, unsigned int newId)
75 
76  return hs.Close(::v8::Undefined());
77 }
78 
79 ::v8::Handle<::v8::Value> DataSourceCatalog_GetDataSource(const ::v8::Arguments& args)
80 {
81  ::v8::HandleScope hs;
82 
83  if(args.Holder().IsEmpty())
84  return ::v8::ThrowException(::v8::String::New("In order to use the getDataSource method you must use the object notation: \"ds = obj.getDataSource();\""));
85 
86  te::da::DataSourceCatalog* catalog = te::v8::common::Unwrap<te::da::DataSourceCatalog>(args.Holder());
87 
88  if(catalog == 0)
89  return ::v8::ThrowException(::v8::String::New("Invalid data source catalog in the getDataSource method!"));
90 
91  te::da::DataSource* ds = catalog->getDataSource();
92 
93  ::v8::Local<::v8::Object> jds = te::v8::common::Make(ds, te::v8::jsi::GetDataSourceTemplate, false);
94 
95  return hs.Close(jds);
96 }
97 
98 ::v8::Handle<::v8::Value> DataSourceCatalog_SetDataSource(const ::v8::Arguments& args)
99 {
100  ::v8::HandleScope hs;
101 
102  if(args.Holder().IsEmpty())
103  return ::v8::ThrowException(::v8::String::New("In order to use the setDataSource method, you must use the object notation: \"obj.setDataSource(ds);\""));
104 
105  if((args.Length() != 1) || args[0].IsEmpty() || !args[0]->IsObject())
106  return ::v8::ThrowException(::v8::String::New("Missing parameter or wrong parameter type in the setDataSource method!"));
107 
108  te::da::DataSourceCatalog* catalog = te::v8::common::Unwrap<te::da::DataSourceCatalog>(args.Holder());
109 
110  if(catalog == 0)
111  return ::v8::ThrowException(::v8::String::New("Invalid data source catalog in the setDataSource method!"));
112 
113  te::da::DataSource* ds = te::v8::common::Unwrap<te::da::DataSource>(args[0]->ToObject());
114 
115  catalog->setDataSource(ds);
116 
117  return ::v8::Undefined();
118 }
119 
120 ::v8::Handle<::v8::Value> DataSourceCatalog_Clear(const ::v8::Arguments& args)
121 {
122  if(args.Holder().IsEmpty())
123  return ::v8::ThrowException(::v8::String::New("In order to use the clear method, you must use the object notation: \"obj.clear();\""));
124 
125  te::da::DataSourceCatalog* catalog = te::v8::common::Unwrap<te::da::DataSourceCatalog>(args.Holder());
126 
127  if(catalog == 0)
128  return ::v8::ThrowException(::v8::String::New("Invalid data source catalog in the clear method!"));
129 
130  catalog->clear();
131 
132  return ::v8::Undefined();
133 }
134 
135 ::v8::Handle<::v8::Value> DataSourceCatalog_Clone(const ::v8::Arguments& args)
136 {
137  ::v8::HandleScope hs;
138 
139  if(args.Holder().IsEmpty())
140  return ::v8::ThrowException(::v8::String::New("In order to use the clone method, you must use the object notation: \"obj.clone();\""));
141 
142  te::da::DataSourceCatalog* catalog = te::v8::common::Unwrap<te::da::DataSourceCatalog>(args.Holder());
143 
144  if(catalog == 0)
145  return ::v8::ThrowException(::v8::String::New("Invalid data source catalog in the clone method!"));
146 
147  te::da::DataSourceCatalog* cloneCatalog = catalog->clone();
148 
149  ::v8::Local<::v8::Object> jcloneCatalog = te::v8::common::Make(cloneCatalog, te::v8::jsi::GetDataSourceCatalogTemplate, true);
150 
151  return hs.Close(jcloneCatalog);
152 }
153 
154 ::v8::Handle<::v8::Value> DataSourceCatalog_GetNumberOfDatasets(const ::v8::Arguments& args)
155 {
156  ::v8::HandleScope hs;
157 
158  if(args.Holder().IsEmpty())
159  return ::v8::ThrowException(::v8::String::New("In order to use the getNumberOfDatasets method you must use the object notation: \"ndataSets = obj.getNumberOfDatasets();\""));
160 
161  te::da::DataSourceCatalog* catalog = te::v8::common::Unwrap<te::da::DataSourceCatalog>(args.Holder());
162 
163  if(catalog == 0)
164  return ::v8::ThrowException(::v8::String::New("Invalid data source catalog in the clone method!"));
165 
166  std::size_t ndataSets = catalog->getNumberOfDataSets();
167 
168  ::v8::Local<::v8::Integer> jndataSets = ::v8::Integer::New(static_cast<boost::int32_t>(ndataSets));
169 
170  return hs.Close(jndataSets);
171 }
172 
173 ::v8::Handle<::v8::Value> DataSourceCatalog_Add(const ::v8::Arguments& args)
174 {
175  return ::v8::Undefined();
176 }
177 
178 ::v8::Handle<::v8::Value> DataSourceCatalog_Remove(const ::v8::Arguments& args)
179 {
180  return ::v8::Undefined();
181 }
182 
183 ::v8::Handle<::v8::Value> DataSourceCatalog_Detach(const ::v8::Arguments& args)
184 {
185  return ::v8::Undefined();
186 }
187 
188 ::v8::Handle<::v8::Value> DataSourceCatalog_Rename(const ::v8::Arguments& args)
189 {
190  return ::v8::Undefined();
191 }
192 
193 ::v8::Handle<::v8::Value> DataSourceCatalog_GetDataSetType(const ::v8::Arguments& args)
194 {
195  return ::v8::Undefined();
196 }
197 
198 ::v8::Handle<::v8::Value> DataSourceCatalog_GetDataSetTypeById(const ::v8::Arguments& args)
199 {
200  return ::v8::Undefined();
201 }
202 
203 ::v8::Handle<::v8::Value> DataSourceCatalog_GetDataSetTypePos(const ::v8::Arguments& args)
204 {
205  return ::v8::Undefined();
206 }
207 
208 ::v8::Handle<::v8::Value> DataSourceCatalog_GetNumberOfSequences(const ::v8::Arguments& args)
209 {
210  ::v8::HandleScope hs;
211 
212  if(args.Holder().IsEmpty())
213  return ::v8::ThrowException(::v8::String::New("In order to use the getNumberOfSequences method you must use the object notation: \"nSequences = obj.getNumberOfSequences();\""));
214 
215  te::da::DataSourceCatalog* catalog = te::v8::common::Unwrap<te::da::DataSourceCatalog>(args.Holder());
216 
217  if(catalog == 0)
218  return ::v8::ThrowException(::v8::String::New("Invalid data source catalog in the clone method!"));
219 
220  std::size_t nSequences = catalog->getNumberOfSequences();
221 
222  ::v8::Local<::v8::Integer> jnSequences = ::v8::Integer::New(static_cast<boost::int32_t>(nSequences));
223 
224  return hs.Close(jnSequences);
225 }
226 
227 ::v8::Handle<::v8::Value> DataSourceCatalog_GetSequence(const ::v8::Arguments& args)
228 {
229  return ::v8::Undefined();
230 }
231 
232 ::v8::Handle<::v8::Value> DataSourceCatalog_GetSequenceById(const ::v8::Arguments& args)
233 {
234  return ::v8::Undefined();
235 }
236 
237 ::v8::Handle<::v8::Value> DataSourceCatalog_GetSequencePos(const ::v8::Arguments& args)
238 {
239  return ::v8::Undefined();
240 }
241 
242 ::v8::Handle<::v8::Value> DataSourceCatalog_AddRef(const ::v8::Arguments& args)
243 {
244  return ::v8::Undefined();
245 }
246 
247 ::v8::Handle<::v8::Value> DataSourceCatalog_RemoveRef(const ::v8::Arguments& args)
248 {
249  return ::v8::Undefined();
250 }
251 
252 ::v8::Handle<::v8::Value> DataSourceCatalog_GetRefFK(const ::v8::Arguments& args)
253 {
254  return ::v8::Undefined();
255 }
256 
257 ::v8::Handle<::v8::Value> DataSourceCatalog_DropDependentSequences(const ::v8::Arguments& args)
258 {
259  return ::v8::Undefined();
260 }
261 
262 ::v8::Handle<::v8::Value> DataSourceCatalog_Constructor(const ::v8::Arguments& args)
263 {
264  ::v8::HandleScope hs;
265 
266  if(!args.IsConstructCall())
267  return ::v8::ThrowException(::v8::String::New("In order to create a data source catalog, you need call its constructor like: var c = new TeDataSourceCatalog();"));
268 
269  if(args.Holder().IsEmpty())
270  return ::v8::ThrowException(::v8::String::New("The DataSourceCatalog constructor must use object notation!"));
271 
272  if(args.Length() != 0)
273  return ::v8::ThrowException(::v8::String::New("The DataSourceCatalog constructor has no parameters!"));
274 
275  std::auto_ptr<te::da::DataSourceCatalog> catalog(new te::da::DataSourceCatalog());
276 
277  ::v8::Local<::v8::Object> jcatalog = te::v8::common::Make(catalog.get(), te::v8::jsi::GetDataSourceCatalogTemplate, true);
278 
279  catalog.release();
280 
281  return hs.Close(jcatalog);
282 }
283 
284 void te::v8::jsi::RegisterDataSourceCatalog(::v8::Local<::v8::Object>& global)
285 {
286  ::v8::HandleScope hs;
287 
288  ::v8::Local<::v8::FunctionTemplate> jscatalog = ::v8::FunctionTemplate::New(DataSourceCatalog_Constructor);
289 
290  global->Set(::v8::String::New("TeDataSourceCatalog"), jscatalog->GetFunction());
291 }
292 
293 static ::v8::Persistent<::v8::FunctionTemplate> sds_datasourcecatalog_template;
294 
295 ::v8::Persistent<::v8::FunctionTemplate>& te::v8::jsi::GetDataSourceCatalogTemplate()
296 {
297  if(sds_datasourcecatalog_template.IsEmpty())
298  {
299  ::v8::Local<::v8::FunctionTemplate> result = ::v8::FunctionTemplate::New();
300 
301  ::v8::Handle<::v8::ObjectTemplate> prototype = result->PrototypeTemplate();
302 
303  prototype->Set(::v8::String::NewSymbol("getId"), ::v8::FunctionTemplate::New(DataSourceCatalog_GetId));
304  prototype->Set(::v8::String::NewSymbol("setId"), ::v8::FunctionTemplate::New(DataSourceCatalog_SetId));
305  prototype->Set(::v8::String::NewSymbol("getDataSource"), ::v8::FunctionTemplate::New(DataSourceCatalog_GetDataSource));
306  prototype->Set(::v8::String::NewSymbol("setDataSource"), ::v8::FunctionTemplate::New(DataSourceCatalog_SetDataSource));
307  prototype->Set(::v8::String::NewSymbol("clear"), ::v8::FunctionTemplate::New(DataSourceCatalog_Clear));
308  prototype->Set(::v8::String::NewSymbol("clone"), ::v8::FunctionTemplate::New(DataSourceCatalog_Clone));
309  prototype->Set(::v8::String::NewSymbol("getNumberOfDataSets"), ::v8::FunctionTemplate::New(DataSourceCatalog_GetNumberOfDatasets));
310  prototype->Set(::v8::String::NewSymbol("add"), ::v8::FunctionTemplate::New(DataSourceCatalog_Add));
311  prototype->Set(::v8::String::NewSymbol("remove"), ::v8::FunctionTemplate::New(DataSourceCatalog_Remove));
312  prototype->Set(::v8::String::NewSymbol("detach"), ::v8::FunctionTemplate::New(DataSourceCatalog_Detach));
313  prototype->Set(::v8::String::NewSymbol("rename"), ::v8::FunctionTemplate::New(DataSourceCatalog_Rename));
314  prototype->Set(::v8::String::NewSymbol("getDataSetType"), ::v8::FunctionTemplate::New(DataSourceCatalog_GetDataSetType));
315  prototype->Set(::v8::String::NewSymbol("getDataSetTypeById"), ::v8::FunctionTemplate::New(DataSourceCatalog_GetDataSetTypeById));
316  prototype->Set(::v8::String::NewSymbol("getDataSetTypePos"), ::v8::FunctionTemplate::New(DataSourceCatalog_GetDataSetTypePos));
317  prototype->Set(::v8::String::NewSymbol("getNumberOfSequences"), ::v8::FunctionTemplate::New(DataSourceCatalog_GetNumberOfSequences));
318  prototype->Set(::v8::String::NewSymbol("getSequence"), ::v8::FunctionTemplate::New(DataSourceCatalog_GetSequence));
319  prototype->Set(::v8::String::NewSymbol("getSequenceById"), ::v8::FunctionTemplate::New(DataSourceCatalog_GetSequenceById));
320  prototype->Set(::v8::String::NewSymbol("getSequencePos"), ::v8::FunctionTemplate::New(DataSourceCatalog_GetSequencePos));
321  prototype->Set(::v8::String::NewSymbol("addRef"), ::v8::FunctionTemplate::New(DataSourceCatalog_AddRef));
322  prototype->Set(::v8::String::NewSymbol("removeRef"), ::v8::FunctionTemplate::New(DataSourceCatalog_RemoveRef));
323  prototype->Set(::v8::String::NewSymbol("getRefFK"), ::v8::FunctionTemplate::New(DataSourceCatalog_GetRefFK));
324  prototype->Set(::v8::String::NewSymbol("dropDependentSequences"), ::v8::FunctionTemplate::New(DataSourceCatalog_DropDependentSequences));
325 
326  sds_datasourcecatalog_template = ::v8::Persistent<::v8::FunctionTemplate>::New(result);
327  }
328 
330 }
331 
void setDataSource(DataSource *ds)
It sets the DataSource associated to the catalog.
::v8::Handle<::v8::Value > DataSourceCatalog_SetId(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > DataSourceCatalog_GetDataSource(const ::v8::Arguments &args)
::v8::Local<::v8::Object > Make(T *obj, TF tfunc, const bool isOwner)
It creates a new JavaScript object from a C++ object (obj).
::v8::Handle<::v8::Value > DataSourceCatalog_GetId(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > DataSourceCatalog_GetSequenceById(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > DataSourceCatalog_Clone(const ::v8::Arguments &args)
It represents the system catalog of a DataSource.
::v8::Handle<::v8::Value > DataSourceCatalog_Clear(const ::v8::Arguments &args)
JavaScript exporting routine for the TerraLib Data Access module.
::v8::Persistent<::v8::FunctionTemplate > & GetDataSourceCatalogTemplate()
It returns a reference to the persistent template of a DataSourceCatalog object.
::v8::Handle<::v8::Value > DataSourceCatalog_GetNumberOfSequences(const ::v8::Arguments &args)
std::size_t getNumberOfDataSets() const
It returns the number of datasets in the catalog.
static te::dt::Date ds(2010, 01, 01)
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
void RegisterDataSourceCatalog(::v8::Local<::v8::Object > &global)
It registers the DataSourceCatalog class.
::v8::Handle<::v8::Value > DataSourceCatalog_GetSequence(const ::v8::Arguments &args)
DataSource * getDataSource() const
It returns the DataSource associated to the catalog.
unsigned int getId() const
It returns the catalog identifier.
::v8::Handle<::v8::Value > DataSourceCatalog_Rename(const ::v8::Arguments &args)
::v8::Persistent<::v8::FunctionTemplate > & GetDataSourceTemplate()
It returns a reference to the persistent template of a DataSource object.
void clear()
It clears the catalog, releasing all the resources used by it.
::v8::Handle<::v8::Value > DataSourceCatalog_Add(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > DataSourceCatalog_Constructor(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > DataSourceCatalog_GetRefFK(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > DataSourceCatalog_Remove(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > DataSourceCatalog_SetDataSource(const ::v8::Arguments &args)
std::size_t getNumberOfSequences() const
It returns the number of sequences in the catalog.
::v8::Handle<::v8::Value > DataSourceCatalog_AddRef(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > DataSourceCatalog_RemoveRef(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > DataSourceCatalog_GetDataSetTypeById(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > DataSourceCatalog_GetDataSetType(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > DataSourceCatalog_GetDataSetTypePos(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > DataSourceCatalog_Detach(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > DataSourceCatalog_GetSequencePos(const ::v8::Arguments &args)
void setId(unsigned int id)
It sets the catalog identifier.
::v8::Handle<::v8::Value > DataSourceCatalog_DropDependentSequences(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > DataSourceCatalog_GetNumberOfDatasets(const ::v8::Arguments &args)
static::v8::Persistent<::v8::FunctionTemplate > sds_datasourcecatalog_template