~jamesj/+junk/wsdlexplorer

« back to all changes in this revision

Viewing changes to wsdlexplorer/ServiceDialog.py

  • Committer: James Jesudason
  • Date: 2011-08-31 11:06:17 UTC
  • Revision ID: james.jesudason@canonical.com-20110831110617-e2jf7iichqv23t52
Added main files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
### BEGIN LICENSE
 
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.
 
7
 
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.
 
12
 
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/>.
 
15
### END LICENSE
 
16
 
 
17
import gtk
 
18
import base64
 
19
import sys
 
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
 
25
 
 
26
 
 
27
from wsdlexplorer.helpers import get_builder
 
28
 
 
29
import gettext
 
30
from gettext import gettext as _
 
31
gettext.textdomain('wsdlexplorer')
 
32
 
 
33
class ServiceDialog(gtk.Dialog):
 
34
    __gtype_name__ = "ServiceDialog"
 
35
 
 
36
    # The selected service details
 
37
    row = None
 
38
    client = None
 
39
    querygrid = None
 
40
    methodGrid = None
 
41
    objectGrid = None
 
42
 
 
43
    def __new__(cls):
 
44
        """Special static method that's automatically called by Python when 
 
45
        constructing a new instance of this class.
 
46
        
 
47
        Returns a fully instantiated ServiceDialog object.
 
48
        """
 
49
        builder = get_builder('ServiceDialog')
 
50
        new_object = builder.get_object('service_dialog')
 
51
        new_object.finish_initializing(builder)
 
52
        return new_object
 
53
 
 
54
    def finish_initializing(self, builder):
 
55
        """Called when we're finished initializing.
 
56
 
 
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
 
60
        instance.
 
61
        """
 
62
        # Get a reference to the builder and set up the signals.
 
63
        self.builder = builder
 
64
        self.builder.connect_signals(self)
 
65
 
 
66
        # Instantiate the CouchDb model object
 
67
        self.connection = Connection()
 
68
 
 
69
        self.resize(600,400)
 
70
 
 
71
    def ok(self, widget, data=None):
 
72
        """The user has elected to save the changes.
 
73
 
 
74
        Called before the dialog returns gtk.RESONSE_OK from run().
 
75
        """
 
76
        pass
 
77
 
 
78
    def cancel(self, widget, data=None):
 
79
        """The user has elected cancel changes.
 
80
 
 
81
        Called before the dialog returns gtk.RESPONSE_CANCEL for run()
 
82
        """
 
83
        pass
 
84
 
 
85
    def on_toolbuttonWSDL_clicked(self, widget, data=None):
 
86
        """Get the service description"""
 
87
        
 
88
        # Get the connection record from the database
 
89
        record = self.connection.get_record(self.row)
 
90
            
 
91
        # Create the web services client
 
92
        self._create_client(record["WSDL"], record["Type"], record["Username"], record["Password"], record["Token"])
 
93
 
 
94
        # Get the methods and object types for the web service
 
95
        methods = self.client.get_methods()
 
96
        objects = self.client.get_objects()
 
97
 
 
98
        # Remove the method grid, if it has been populated
 
99
        if self.methodGrid:
 
100
            self.builder.get_object("viewport3").remove(self.methodGrid)
 
101
 
 
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)
 
105
        
 
106
        # Create a grid for the objects
 
107
        if len(objects) > 0:
 
108
            if self.objectGrid:
 
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)
 
112
 
 
113
 
 
114
 
 
115
    def on_toolbuttonQuery_clicked(self, widget, data=None):
 
116
        """Run the SOQL query"""
 
117
        
 
118
        try:
 
119
            parent = "viewport2"
 
120
 
 
121
            # Get the connection record from the database
 
122
            record = self.connection.get_record(self.row)
 
123
 
 
124
            if record["Type"] != "Salesforce":
 
125
                return
 
126
 
 
127
            # Remove any existing grid
 
128
            if self.querygrid:
 
129
                self.builder.get_object(parent).remove(self.querygrid)
 
130
 
 
131
            # Create the web services client
 
132
            self._create_client(record["WSDL"], record["Type"], record["Username"], record["Password"], record["Token"])
 
133
            
 
134
            # Run the Salesforce query
 
135
            soql = self.query_text
 
136
            result = self.client.query(soql)
 
137
 
 
138
            # Build a list of dictionaries from the SOQL results
 
139
            if result.size > 0:
 
140
                self._build_results_grid(result)
 
141
            else:
 
142
                self.querygrid = gtk.Label("No records found.")
 
143
 
 
144
        except:
 
145
            # Display error message onscreen
 
146
            self.querygrid = gtk.Label(sys.exc_info()[1])
 
147
 
 
148
        finally:
 
149
            # Display the results or message onscreen
 
150
            self.querygrid.show()
 
151
            self.builder.get_object(parent).add(self.querygrid)
 
152
 
 
153
 
 
154
    def _build_results_grid(self, result):
 
155
        # Convert the result objects as dictionaries
 
156
        recList = self.client.results_as_dictionaries(result)
 
157
 
 
158
        # Display the query results in a grid
 
159
        hints = {"Id":StringColumn}
 
160
        self.querygrid = DictionaryGrid(dictionaries=recList, type_hints=hints)
 
161
 
 
162
 
 
163
    def _create_client(self, wsdl, type, username=None, password=None, token=None):
 
164
        if self.client != None:
 
165
            return
 
166
 
 
167
        if type == "Salesforce":
 
168
            self.client = SFClient(wsdl)
 
169
            self.client.login(username, base64.b64decode(password), token)
 
170
        else:
 
171
            self.client = Client(wsdl)
 
172
 
 
173
    def _grid_factory(self, objects, grid):
 
174
        grid = DictionaryGrid(objects)
 
175
        grid.editable = False
 
176
        grid.show()
 
177
        return grid
 
178
 
 
179
 
 
180
    @property
 
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())
 
184
 
 
185
 
 
186
 
 
187
if __name__ == "__main__":
 
188
    dialog = ServiceDialog()
 
189
    dialog.show()
 
190
    gtk.main()