src/terralib/binding/v8/jsi/dataaccess/DataSource.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 Geometry.cpp
22 
23  \brief Utility functions to register the Geometry class into Google JavaScript V8 engine.
24  */
25 
26 // TerraLib
27 #include "../../../../common/Exception.h"
28 #include "../../../../core/translator/Translator.h"
29 #include "../../../../dataaccess/datasource/DataSource.h"
30 #include "../../common/Utils.h"
31 #include "DataAccess.h"
32 
33 // STL
34 #include <cassert>
35 
36 // Boost
37 #include <boost/cstdint.hpp>
38 
39 ::v8::Handle<::v8::Value> DataSource_GetType(const ::v8::Arguments& args)
40 {
41  ::v8::HandleScope hs;
42 
43  if(args.Holder().IsEmpty())
44  return ::v8::ThrowException(::v8::String::New(TR_V8JSI("In order to use the getType method you must use the object notation: \"t = obj.getType();\"")));
45 
46  te::da::DataSource* ds = te::v8::common::Unwrap<te::da::DataSource>(args.Holder());
47 
48  if(ds == 0)
49  return ::v8::ThrowException(::v8::String::New(TR_V8JSI("Invalid data source in getType method!")));
50 
51  const std::string& dst = ds->getType();
52 
53  ::v8::Local<::v8::String> jdst = ::v8::String::New(dst.c_str());
54 
55  return hs.Close(jdst);
56 }
57 
58 ::v8::Handle<::v8::Value> DataSource_GetConnectionInfo(const ::v8::Arguments& args)
59 {
60  ::v8::HandleScope hs;
61 
62  if(args.Holder().IsEmpty())
63  return ::v8::ThrowException(::v8::String::New(TR_V8JSI("In order to use the getConnectionInfo method you must use the object notation: \"conninfo = obj.getConnectionInfo();\"")));
64 
65  te::da::DataSource* ds = te::v8::common::Unwrap<te::da::DataSource>(args.Holder());
66 
67  if(ds == 0)
68  return ::v8::ThrowException(::v8::String::New(TR_V8JSI("Invalid data source in getConnectionInfo method!")));
69 
70  const std::map<std::string, std::string>& conninfo = ds->getConnectionInfo();
71 
72  ::v8::Local<::v8::Object> jsmap = te::v8::common::Convert2Js(conninfo);
73 
74  return hs.Close(jsmap);
75 }
76 
77 ::v8::Handle<::v8::Value> DataSource_SetConnectionInfo(const ::v8::Arguments& args)
78 {
79  ::v8::HandleScope hs;
80 
81  if(args.Holder().IsEmpty())
82  return ::v8::ThrowException(::v8::String::New(TR_V8JSI("In order to use the setConnectionInfo method you must use the object notation: \"obj.setConnectionInfo(conninfo);\"")));
83 
84  te::da::DataSource* ds = te::v8::common::Unwrap<te::da::DataSource>(args.Holder());
85 
86  if(ds == 0)
87  return ::v8::ThrowException(::v8::String::New(TR_V8JSI("Invalid data source in setConnectionInfo method!")));
88 
89  if((args.Length() != 1) || args[0].IsEmpty() || !args[0]->IsArray())
90  return ::v8::ThrowException(::v8::String::New(TR_V8JSI("In order to use setConnectionInfo method you must use object notation: \"obj.setConnectionInfo(connfinfo);\"")));
91 
92  ::v8::Local<::v8::Object> aconninfo = args[0]->ToObject();
93 
94  std::map<std::string, std::string> conninfo;
95 
96  te::v8::common::Convert2Cpp(aconninfo, conninfo);
97 
98  ds->setConnectionInfo(conninfo);
99 
100  return ::v8::Undefined();
101 }
102 
103 ::v8::Handle<::v8::Value> DataSource_GetConnectionStr(const ::v8::Arguments& args)
104 {
105  ::v8::HandleScope hs;
106 
107  if(args.Holder().IsEmpty())
108  return ::v8::ThrowException(::v8::String::New(TR_V8JSI("In order to use the getConnectionStr method you must use the object notation: \"connstr = obj.getConnectionStr();\"")));
109 
110  te::da::DataSource* ds = te::v8::common::Unwrap<te::da::DataSource>(args.Holder());
111 
112  if(ds == 0)
113  return ::v8::ThrowException(::v8::String::New(TR_V8JSI("Invalid data source in the getConnectionStr method!")));
114 
115  const std::string& connStr = ds->getConnectionStr();
116 
117  ::v8::Local<::v8::String> jconnStr = ::v8::String::New(connStr.c_str());
118 
119  return hs.Close(jconnStr);
120 }
121 
122 ::v8::Handle<::v8::Value> DataSource_SetConnectionStr(const ::v8::Arguments& args)
123 {
124  ::v8::HandleScope hs;
125 
126  if(args.Holder().IsEmpty())
127  return ::v8::ThrowException(::v8::String::New(TR_V8JSI("In order to use the setConnectionStr method you must use the object notation: \"obj.setConnectionStr(connstr);\"")));
128 
129  if(args.Length() != 1 || args[0].IsEmpty() || !args[0]->IsString())
130  return ::v8::ThrowException(::v8::String::New(TR_V8JSI("In order to use the setConnectionStr method, you must use the object notation: \"obj.setConnectionStr(\"connStr\");\"")));
131 
132  te::da::DataSource* ds = te::v8::common::Unwrap<te::da::DataSource>(args.Holder());
133 
134  if(ds == 0)
135  return ::v8::ThrowException(::v8::String::New(TR_V8JSI("Invalid data source in setConnectionStr method!")));
136 
137  ::v8::String::Utf8Value jconnStr(args[0]->ToString());
138 
139  ds->setConnectionStr(*jconnStr);
140 
141  return ::v8::Undefined();
142 }
143 
144 ::v8::Handle<::v8::Value> DataSource_GetCapabilities(const ::v8::Arguments& /*args*/)
145 {
146  ::v8::HandleScope hs;
147  return ::v8::Undefined();
148 }
149 
150 ::v8::Handle<::v8::Value> DataSource_GetDialect(const ::v8::Arguments& /*args*/)
151 {
152  ::v8::HandleScope hs;
153  return ::v8::Undefined();
154 }
155 
156 ::v8::Handle<::v8::Value> DataSource_Open(const ::v8::Arguments& args)
157 {
158  ::v8::HandleScope hs;
159 
160  if(args.Holder().IsEmpty())
161  return ::v8::ThrowException(::v8::String::New(TR_V8JSI("In order to use the open method, you must use the object notation: \"obj.open();\" or \"obj.open(conn_string);\" or \"obj.open(conninfo);\"")));
162 
163  te::da::DataSource* ds = te::v8::common::Unwrap<te::da::DataSource>(args.Holder());
164 
165  if(ds == 0)
166  return ::v8::ThrowException(::v8::String::New(TR_V8JSI("Invalid data source in open method!")));
167 
168  try
169  {
170  if(args.Length() == 0)
171  {
172  ds->open();
173  }
174  else
175  {
176  if((args.Length() == 1) && (!args[0].IsEmpty()) && (args[0]->IsString()))
177  {
178  v8::String::Utf8Value jconnInfo(args[0]->ToString());
179  ds->open(*jconnInfo);
180  }
181  else if((args.Length() == 1) && (!args[0].IsEmpty()) && (args[0]->IsArray()))
182  {
183  ::v8::Local<::v8::Object> aconninfo = args[0]->ToObject();
184  std::map<std::string, std::string> conninfo;
185  te::v8::common::Convert2Cpp(aconninfo, conninfo);
186  ds->open(conninfo);
187  }
188 
189  throw te::common::Exception(TR_V8JSI("Wrong parameters in open method!"));
190  }
191  }
192  catch(const std::exception& e)
193  {
194  return ::v8::ThrowException(::v8::String::New(e.what()));
195  }
196  catch(...)
197  {
198  return ::v8::ThrowException(::v8::String::New(TR_V8JSI("Unexpected exception in open method!")));
199  }
200 
201  return ::v8::Undefined();
202 }
203 
204 ::v8::Handle<::v8::Value> DataSource_Close(const ::v8::Arguments& args)
205 {
206  ::v8::HandleScope hs;
207 
208  if(args.Holder().IsEmpty())
209  return ::v8::ThrowException(::v8::String::New("In order to use the close method, you must use the object notation: \"obj.close();\""));
210 
211  te::da::DataSource* ds = te::v8::common::Unwrap<te::da::DataSource>(args.Holder());
212 
213  if(ds == 0)
214  return ::v8::ThrowException(::v8::String::New("Invalid data source in the close method!"));
215 
216  try
217  {
218  ds->close();
219  }
220  catch(const std::exception& e)
221  {
222  return ::v8::ThrowException(::v8::String::New(e.what()));
223  }
224  catch(...)
225  {
226  return ::v8::ThrowException(::v8::String::New(TR_V8JSI("Unexpected exception in close method!")));
227  }
228 
229  return ::v8::Undefined();
230 }
231 
232 ::v8::Handle<::v8::Value> DataSource_IsOpened(const ::v8::Arguments& args)
233 {
234  ::v8::HandleScope hs;
235 
236  if(args.Holder().IsEmpty())
237  return ::v8::ThrowException(::v8::String::New("In order to use the isOpened method, you must use the object notation: \"result = obj.isOpened();\""));
238 
239  te::da::DataSource* ds = te::v8::common::Unwrap<te::da::DataSource>(args.Holder());
240 
241  if(ds == 0)
242  return ::v8::ThrowException(::v8::String::New("Invalid data source in isOpened method!"));
243 
244  bool result = ds->isOpened();
245 
246  ::v8::Handle<::v8::Boolean> jresult = ::v8::Boolean::New(result);
247 
248  return hs.Close(jresult);
249 }
250 
251 ::v8::Handle<::v8::Value> DataSource_IsValid(const ::v8::Arguments& args)
252 {
253  ::v8::HandleScope hs;
254 
255  if(args.Holder().IsEmpty())
256  return ::v8::ThrowException(::v8::String::New("In order to use the isValid method, you must use the object notation: \"result = obj.isValid();\""));
257 
258  te::da::DataSource* ds = te::v8::common::Unwrap<te::da::DataSource>(args.Holder());
259 
260  if(ds == 0)
261  return ::v8::ThrowException(::v8::String::New("Invalid data source in isValid method!"));
262 
263  bool result = ds->isValid();
264 
265  ::v8::Handle<::v8::Boolean> jresult = ::v8::Boolean::New(result);
266 
267  return hs.Close(jresult);
268 }
269 
270 ::v8::Handle<::v8::Value> DataSource_GetCatalog(const ::v8::Arguments& args)
271 {
272  ::v8::HandleScope hs;
273 
274  if(args.Holder().IsEmpty())
275  return ::v8::ThrowException(::v8::String::New("In order to use the getCatalog method you must use the object notation: \"c = obj.getCatalog();\""));
276 
277  te::da::DataSource* ds = te::v8::common::Unwrap<te::da::DataSource>(args.Holder());
278 
279  if(ds == 0)
280  return ::v8::ThrowException(::v8::String::New("Invalid data source in the getCatalog method!"));
281 
282  std::auto_ptr<te::da::DataSourceCatalog> catalog(ds->getCatalog());
283 
284  ::v8::Local<::v8::Object> jcatalog = te::v8::common::Make(catalog.get(), te::v8::jsi::GetDataSourceCatalogTemplate, false);
285 
286  catalog.release();
287 
288  return hs.Close(jcatalog);
289 }
290 
291 ::v8::Handle<::v8::Value> DataSource_GetTransactor(const ::v8::Arguments& args)
292 {
293  ::v8::HandleScope hs;
294 
295  if(args.Holder().IsEmpty())
296  return ::v8::ThrowException(::v8::String::New("In order to use the getTransactor method you must use the object notation: \"c = obj.getTransactor();\""));
297 
298  te::da::DataSource* ds = te::v8::common::Unwrap<te::da::DataSource>(args.Holder());
299 
300  if(ds == 0)
301  return ::v8::ThrowException(::v8::String::New("Invalid data source in the getTransactor method!"));
302 
303  try
304  {
305  std::auto_ptr<te::da::DataSourceTransactor> t(ds->getTransactor());
306  ::v8::Local<::v8::Object> jt = te::v8::common::Make(t.get(), te::v8::jsi::GetDataSourceTransactorTemplate, true);
307  t.release();
308  return hs.Close(jt);
309  }
310  catch(const std::exception& e)
311  {
312  return ::v8::ThrowException(::v8::String::New(e.what()));
313  }
314  catch(...)
315  {
316  return ::v8::ThrowException(::v8::String::New(TR_V8JSI("Unexpected exception in getTransactor method!")));
317  }
318 }
319 
320 ::v8::Handle<::v8::Value> DataSource_Create(const ::v8::Arguments& /*args*/)
321 {
322  ::v8::HandleScope hs;
323  return ::v8::Undefined();
324 }
325 
326 ::v8::Handle<::v8::Value> DataSource_Drop(const ::v8::Arguments& args)
327 {
328  ::v8::HandleScope hs;
329 
330  if(args.Length() != 1 || args[0].IsEmpty() || !args[0]->IsObject() )
331  return ::v8::ThrowException(::v8::String::New(TR_V8JSI("Wrong parameters drop method!")));
332 
333  te::da::DataSource* ds = te::v8::common::Unwrap<te::da::DataSource>(args[0]->ToObject());
334 
335  try
336  {
338  te::v8::common::LooseOwnership<te::da::DataSource>(args[0]->ToObject());
339  }
340  catch(const std::exception& e)
341  {
342  return ::v8::ThrowException(::v8::String::New(e.what()));
343  }
344  catch(...)
345  {
346  return ::v8::ThrowException(::v8::String::New(TR_V8JSI("Unexpected exception in drop method!")));
347  }
348 
349  return ::v8::Undefined();
350 }
351 
352 void te::v8::jsi::RegisterDataSource(::v8::Local<::v8::Object>& global)
353 {
354  ::v8::HandleScope hs;
355 
356  ::v8::Handle<::v8::ObjectTemplate> objTpl = ::v8::ObjectTemplate::New();
357 
358  objTpl->Set(::v8::String::NewSymbol("create"), ::v8::FunctionTemplate::New(DataSource_Create));
359  objTpl->Set(::v8::String::NewSymbol("drop"), ::v8::FunctionTemplate::New(DataSource_Drop));
360 
361  global->Set(::v8::String::New("TeDataSource"), objTpl->NewInstance());
362 }
363 
364 static ::v8::Persistent<::v8::FunctionTemplate> sg_datasource_template;
365 
366 ::v8::Persistent<::v8::FunctionTemplate>& te::v8::jsi::GetDataSourceTemplate()
367 {
368  if(sg_datasource_template.IsEmpty())
369  {
370  ::v8::Local<::v8::FunctionTemplate> result = ::v8::FunctionTemplate::New();
371  ::v8::Handle<::v8::ObjectTemplate> prototype = result->PrototypeTemplate();
372 
373  prototype->Set(::v8::String::NewSymbol("getType"), ::v8::FunctionTemplate::New(DataSource_GetType));
374  prototype->Set(::v8::String::NewSymbol("getConnectionInfo"), ::v8::FunctionTemplate::New(DataSource_GetConnectionInfo));
375  prototype->Set(::v8::String::NewSymbol("setConnectionInfo"), ::v8::FunctionTemplate::New(DataSource_SetConnectionInfo));
376  prototype->Set(::v8::String::NewSymbol("getConnectionStr"), ::v8::FunctionTemplate::New(DataSource_GetConnectionStr));
377  prototype->Set(::v8::String::NewSymbol("setConnectionStr"), ::v8::FunctionTemplate::New(DataSource_SetConnectionStr));
378  prototype->Set(::v8::String::NewSymbol("getCapabilities"), ::v8::FunctionTemplate::New(DataSource_GetCapabilities));
379  prototype->Set(::v8::String::NewSymbol("getDialect"), ::v8::FunctionTemplate::New(DataSource_GetDialect));
380  prototype->Set(::v8::String::NewSymbol("open"), ::v8::FunctionTemplate::New(DataSource_Open));
381  prototype->Set(::v8::String::NewSymbol("close"), ::v8::FunctionTemplate::New(DataSource_Close));
382  prototype->Set(::v8::String::NewSymbol("isOpened"), ::v8::FunctionTemplate::New(DataSource_IsOpened));
383  prototype->Set(::v8::String::NewSymbol("isValid"), ::v8::FunctionTemplate::New(DataSource_IsValid));
384  prototype->Set(::v8::String::NewSymbol("getCatalog"), ::v8::FunctionTemplate::New(DataSource_GetCatalog));
385  prototype->Set(::v8::String::NewSymbol("getTransactor"), ::v8::FunctionTemplate::New(DataSource_GetTransactor));
386 
387  sg_datasource_template = ::v8::Persistent<::v8::FunctionTemplate>::New(result);
388  }
389 
390  return sg_datasource_template;
391 }
392 
::v8::Local<::v8::Object > Make(T *obj, TF tfunc, const bool isOwner)
It creates a new JavaScript object from a C++ object (obj).
virtual bool isValid() const =0
It checks if the data source is valid (available for using).
::v8::Handle<::v8::Value > DataSource_Open(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > DataSource_GetDialect(const ::v8::Arguments &)
virtual std::unique_ptr< DataSourceTransactor > getTransactor()=0
It returns the set of parameters used to set up the access channel to the underlying repository...
virtual void open()=0
It opens the data source and makes it ready for using.
::v8::Handle<::v8::Value > DataSource_Create(const ::v8::Arguments &)
::v8::Handle<::v8::Value > DataSource_Drop(const ::v8::Arguments &args)
JavaScript exporting routine for the TerraLib Data Access module.
virtual bool isOpened() const =0
It returns true if the data source is opened, otherwise it returns false.
::v8::Handle<::v8::Value > DataSource_GetConnectionStr(const ::v8::Arguments &args)
::v8::Persistent<::v8::FunctionTemplate > & GetDataSourceCatalogTemplate()
It returns a reference to the persistent template of a DataSourceCatalog object.
static te::dt::Date ds(2010, 01, 01)
static void drop(const std::string &dsType, const std::string &connInfo)
It removes a data source identified by its connection information and the driver type.
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
::v8::Persistent<::v8::FunctionTemplate > & GetDataSourceTemplate()
It returns a reference to the persistent template of a DataSource object.
void RegisterDataSource(::v8::Local<::v8::Object > &global)
It registers the DataSource class.
::v8::Handle<::v8::Value > DataSource_GetCatalog(const ::v8::Arguments &args)
virtual std::string getType() const =0
It returns the data source type name (in UPPER CASE). Ex: POSTGIS, SQLITE, WFS, WMS, or MYSQL.
::v8::Handle<::v8::Value > DataSource_GetConnectionInfo(const ::v8::Arguments &args)
inline::v8::Local<::v8::Object > Convert2Js(const std::map< std::string, std::string > &m)
It converts the input map to an object representing the associative conteiner.
static::v8::Persistent<::v8::FunctionTemplate > sg_datasource_template
void Convert2Cpp(const ::v8::Local<::v8::Object > &jsmap, std::map< std::string, std::string > &cppmap)
It converts the input map to an object representing the associative conteiner.
::v8::Handle<::v8::Value > DataSource_IsValid(const ::v8::Arguments &args)
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
const te::core::URI & getConnectionInfo() const
An Uniform Resource Identifier used to describe the datasource connection.
::v8::Handle<::v8::Value > DataSource_GetType(const ::v8::Arguments &args)
virtual void close()=0
It closes the data source and clears all the resources used by its internal communication channel...
::v8::Handle<::v8::Value > DataSource_SetConnectionInfo(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > DataSource_IsOpened(const ::v8::Arguments &args)
::v8::Persistent<::v8::FunctionTemplate > & GetDataSourceTransactorTemplate()
It returns a reference to the persistent template of a DataSourceTransactor object.
::v8::Handle<::v8::Value > DataSource_SetConnectionStr(const ::v8::Arguments &args)
#define TR_V8JSI(message)
It marks a string in order to get translated. This is a special mark used in the Vector Geometry modu...
::v8::Handle<::v8::Value > DataSource_GetTransactor(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > DataSource_Close(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > DataSource_GetCapabilities(const ::v8::Arguments &)
std::string ToString(const XMLCh *const value)
It converts the XML string to a standard C++ string.