    'Copyright (c) 2004, Plumtree Software

    'All rights reserved.

    'Redistribution and use in source and binary forms, with or without modification,
    'are permitted provided that the following conditions are met:

    '1.  Neither the name of Plumtree Software nor the names of its contributors may
    'be used to endorse or promote products derived from this software without
    'specific prior written permission;

    '2.  Licensee acknowledges that no license or other permission is granted herein
    'with respect to any third party software and that Licensee may not use the code
    'in any way that would infringe any third party right.

    'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
    'CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
    'INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF TITLE,
    'NONINFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
    'PURPOSE ARE DISCLAIMED. REGARDLESS OF THE BASIS OF RECOVERY
    'CLAIMED, WHETHER UNDER ANY CONTRACT, WARRANTY, TORT (INCLUDING
    'NEGLIGENCE AND STRICT LIABILITY), BREACH OF STATUTORY DUTY,
    'PRINCIPLES OF CONTRIBUTION OR ANY OTHER THEORY OF LIABILITY, IN NO
    'EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
    'DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    'DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
    'GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    'INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    'WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
    'OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    'EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE; AND IN NO EVENT
    'WILL THE COPYRIGHT OWNER'S OR CONTRIBUTORS' EXCEED $10,000.

    Imports System

    Imports Plumtree.Remote.Util
    Imports Plumtree.Remote.Search

    Imports System.IO

    Namespace Plumtree.Remote.SWS.Google
        Public Class Search
            Implements IRemoteSearch

           'Runs a search and returns search results.
           'simple basic search function
           'ISearchUser and ISearchContext are ignored
           Public Function BasicSearch(ByVal searchQuery As ISearchQuery, ByVal searchUser As ISearchUser, ByVal searchContext As ISearchContext) As ISearchResult Implements IRemoteSearch.BasicSearch

               'get the SearchInfo, which includes the key
               'SearchInfo was populated from values set in the Web Service SCI Pages.
               'If not using SCI pages, hard-code the value below.
                Dim searchInfo As NamedValueMap = searchQuery.GetSearchInfo

                'if the key has not been set, throw the appropriate exception
                If searchInfo Is Nothing Then
                    Throw New SearchException(TypeSearchError.TYPE_ADMIN_PREFS_NOT_SET, "Admin Prefs not set")
                End If

                Dim key As String
                key = searchInfo.Get("KEY")
                If key Is Nothing Then
                    Throw New SearchException(TypeSearchError.TYPE_ADMIN_PREFS_NOT_SET, "Admin Prefs not set")
                End If

               'get the ISearchResult from the searchQuery- this will set NumberSkipped
                Dim result As ISearchResult = searchQuery.GetSearchResult

                Dim start As Integer = searchQuery.NumberToSkip
                Dim query As String = searchQuery.SearchString
                Try

                    'see the GoogleAPI docs
                    Dim searchService As New com.google.api.GoogleSearchService

                    Dim googleResult As com.google.api.GoogleSearchResult
                    googleResult = searchService.doGoogleSearch(key, query, start, 10, False, "", False, "", "", "")
                    Dim totalHits = googleResult.estimatedTotalResultsCount
                    Dim startIndex As Integer = googleResult.startIndex
                    Dim endIndex As Integer = googleResult.endIndex
                    Dim count As Integer = (endIndex - startIndex) + 1

                    'set total hits
                    result.TotalNumberOfHits = totalHits

                    'turn their array of search records into our search records
                    Dim googleElements() As com.google.api.ResultElement
                    ReDim googleElements(count)
                    googleElements = googleResult.resultElements
                    Dim len As Integer = googleElements.GetUpperBound(0)
                    Dim records(len) As ISearchRecord
                    Dim i As Integer

                    Dim record As RemoteSearchRecord
                    Dim element As com.google.api.ResultElement

                    For i = 0 To len
                        record = New RemoteSearchRecord
                        element = googleElements(i)
                        record.Title = element.title
                        record.Description = element.summary
                        record.OpenDocumentURL = element.URL
                        'include four different colors for the search results
                        'subsitute your logic here, e.g. set the image in an sci page
                        Select Case i Mod 4
                            Case 0
                                record.ImageURL = "http://labs.google.com/images/green.gif"
                            Case 1
                                record.ImageURL = "http://labs.google.com/images/yellow.gif"
                            Case 2
                                record.ImageURL = "http://labs.google.com/images/red.gif"
                            Case Else
                                record.ImageURL = "http://labs.google.com/images/blue.gif"
                        End Select

                        records(i) = record
                    Next
                    result.SetSearchResultList(records)

               'if there is an exception, return the message to the user
                Catch ex As Exception
                    Throw New SearchException(TypeSearchError.TYPE_OTHER_ERROR, ex.Message)

                End Try

                Return result

            End Function

        End Class
    End Namespace