1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
3
# Copyright (C) 2010 James Jesudason <james.jesudason@canonical.com>
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU General Public License version 3, as published
6
# by the Free Software Foundation.
8
# This program is distributed in the hope that it will be useful, but
9
# WITHOUT ANY WARRANTY; without even the implied warranties of
10
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
# PURPOSE. See the GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License along
14
# with this program. If not, see <http://www.gnu.org/licenses/>.
20
from wsdlexplorer.ws.client import Client
21
from wsdlexplorer.ws.sfclient import SFClient
22
from wsdlexplorer.common.connection import Connection
23
from quickly.widgets.dictionary_grid import DictionaryGrid
24
from quickly.widgets.grid_column import StringColumn
27
from wsdlexplorer.helpers import get_builder
30
from gettext import gettext as _
31
gettext.textdomain('wsdlexplorer')
33
class ServiceDialog(gtk.Dialog):
34
__gtype_name__ = "ServiceDialog"
36
# The selected service details
44
"""Special static method that's automatically called by Python when
45
constructing a new instance of this class.
47
Returns a fully instantiated ServiceDialog object.
49
builder = get_builder('ServiceDialog')
50
new_object = builder.get_object('service_dialog')
51
new_object.finish_initializing(builder)
54
def finish_initializing(self, builder):
55
"""Called when we're finished initializing.
57
finish_initalizing should be called after parsing the ui definition
58
and creating a ServiceDialog object with it in order to
59
finish initializing the start of the new ServiceDialog
62
# Get a reference to the builder and set up the signals.
63
self.builder = builder
64
self.builder.connect_signals(self)
66
# Instantiate the CouchDb model object
67
self.connection = Connection()
71
def ok(self, widget, data=None):
72
"""The user has elected to save the changes.
74
Called before the dialog returns gtk.RESONSE_OK from run().
78
def cancel(self, widget, data=None):
79
"""The user has elected cancel changes.
81
Called before the dialog returns gtk.RESPONSE_CANCEL for run()
85
def on_toolbuttonWSDL_clicked(self, widget, data=None):
86
"""Get the service description"""
88
# Get the connection record from the database
89
record = self.connection.get_record(self.row)
91
# Create the web services client
92
self._create_client(record["WSDL"], record["Type"], record["Username"], record["Password"], record["Token"])
94
# Get the methods and object types for the web service
95
methods = self.client.get_methods()
96
objects = self.client.get_objects()
98
# Remove the method grid, if it has been populated
100
self.builder.get_object("viewport3").remove(self.methodGrid)
102
# Create a grid for the methods
103
self.methodGrid = self._grid_factory(methods, self.methodGrid)
104
self.builder.get_object("viewport3").add(self.methodGrid)
106
# Create a grid for the objects
109
self.builder.get_object("viewport4").remove(self.objectGrid)
110
self.objectGrid = self._grid_factory(objects, self.objectGrid)
111
self.builder.get_object("viewport4").add(self.objectGrid)
115
def on_toolbuttonQuery_clicked(self, widget, data=None):
116
"""Run the SOQL query"""
121
# Get the connection record from the database
122
record = self.connection.get_record(self.row)
124
if record["Type"] != "Salesforce":
127
# Remove any existing grid
129
self.builder.get_object(parent).remove(self.querygrid)
131
# Create the web services client
132
self._create_client(record["WSDL"], record["Type"], record["Username"], record["Password"], record["Token"])
134
# Run the Salesforce query
135
soql = self.query_text
136
result = self.client.query(soql)
138
# Build a list of dictionaries from the SOQL results
140
self._build_results_grid(result)
142
self.querygrid = gtk.Label("No records found.")
145
# Display error message onscreen
146
self.querygrid = gtk.Label(sys.exc_info()[1])
149
# Display the results or message onscreen
150
self.querygrid.show()
151
self.builder.get_object(parent).add(self.querygrid)
154
def _build_results_grid(self, result):
155
# Convert the result objects as dictionaries
156
recList = self.client.results_as_dictionaries(result)
158
# Display the query results in a grid
159
hints = {"Id":StringColumn}
160
self.querygrid = DictionaryGrid(dictionaries=recList, type_hints=hints)
163
def _create_client(self, wsdl, type, username=None, password=None, token=None):
164
if self.client != None:
167
if type == "Salesforce":
168
self.client = SFClient(wsdl)
169
self.client.login(username, base64.b64decode(password), token)
171
self.client = Client(wsdl)
173
def _grid_factory(self, objects, grid):
174
grid = DictionaryGrid(objects)
175
grid.editable = False
181
def query_text(self):
182
buffer = self.builder.get_object("textviewQuery").get_buffer()
183
return buffer.get_text(buffer.get_start_iter(), buffer.get_end_iter())
187
if __name__ == "__main__":
188
dialog = ServiceDialog()