<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<script runat="server">

    'Amazon Service classes(AWSEのプロキシクラス）
    Private amazonService As AWSECommerceService = New AWSECommerceService
    
    'ItemSearch(著者、キーワード、タイトル、出版社で検索用)
    Private itemSearch As ItemSearch = New ItemSearch
    Private itemSearchRequest As ItemSearchRequest = New ItemSearchRequest
    Private itemSearchResponse As ItemSearchResponse
    
    'ItemLookup(ISBNで検索用）
    Private itemLookup As ItemLookup = New ItemLookup
    Private itemLookupRequest As ItemLookupRequest = New ItemLookupRequest
    Private itemLookupResponse As ItemLookupResponse
    
    'Public properties for subscriptionID and associateTag
    Private _SubscriptionID As String = "082Y49Q2WZZTJHHBN782" ' <-- 要書き換え
    Private _AssociateTag As String = "accessknowh0e-22"       ' <-- 要書き換え
    
    Sub Page_Load(sender As Object, e As EventArgs)
      Ajax.Manager.Register(Me, "My.Page", Ajax.Debug.None)
    End Sub
    
    <Ajax.Method> _
    Public Function GetBooks(queryString As String) As DataTable
      Dim searchType As String = "keywords"
      Dim itemPage As String = "1"
      Dim sort As String = "salesrank"
    
      Dim dt As DataTable = CreateDataTable(1)
      Dim itemsResponse() As Items
    
      If searchType = "ISBN" Then
        With itemLookupRequest
          .ItemID = New String() {queryString}
          .ResponseGroup = New String() {"Small", "Images", "ItemAttributes", "OfferFull", "SalesRank", "Reviews"}
        End With
    
        With itemLookup
          .SubscriptionId = Me.SubscriptionID
          .AssociateTag = Me.AssociateTag
          .Request = New itemLookupRequest() {itemLookupRequest}
        End With
    
        itemLookupResponse = amazonService.ItemLookup(itemLookup)
        itemsResponse = itemLookupResponse.Items
      Else  ' search by author, keywords, title, publisher
        With itemSearchRequest
          .SearchIndex = "Books"
          .ResponseGroup = New String() {"Small","Medium", "Large",  "Images", "ItemAttributes", "OfferFull", "SalesRank", "Reviews"}
          .ItemPage = itemPage
    
          If searchType = "keywords" Then
            .Keywords = queryString
          ElseIf searchType = "title" Then
            .Title = queryString
          ElseIf searchType = "author" Then
            .Author = queryString
          ElseIf searchType = "publisher" Then
            .Publisher = queryString
          ElseIf searchType = "browseNode" Then
            .BrowseNode = queryString
          End If
          .Sort = sort
        End With
    
        With itemSearch
          .SubscriptionId = Me.SubscriptionID
          .AssociateTag = Me.AssociateTag
          .Request = New ItemSearchRequest() {itemSearchRequest}
        End With
    
        itemSearchResponse = amazonService.ItemSearch(itemSearch)
        itemsResponse = itemSearchResponse.Items
      End If
    
      If (itemsResponse Is Nothing) Then
        Return dt
      End If
    
      If (Not (itemsResponse(0).Request.Errors) Is Nothing) Then
        Return dt
      End If
    
      If (Not (itemsResponse) Is Nothing) Then
        Dim items As Items = itemsResponse(0)
        If (Not (items) Is Nothing) Then
          Dim results() As Item = items.Item
          If (Not (results) Is Nothing) Then
            Dim enumerator As System.Collections.IEnumerator = CType(results, System.Collections.IEnumerable).GetEnumerator
            Do While enumerator.MoveNext
              Dim item As Item = CType(enumerator.Current, Item)
              If (Not (item) Is Nothing) Then
                Dim dataRow As DataRow = dt.NewRow
                If (Not (item.ASIN) Is Nothing) Then
                  dataRow("ASIN") = item.ASIN
                End If
                If (Not (item.SalesRank) Is Nothing) Then
                  dataRow("SalesRank") = item.SalesRank
                End If
                If (Not (item.DetailPageURL) Is Nothing) Then
                  dataRow("AmazonURL") = item.DetailPageURL
                End If
                If (Not (item.SmallImage) Is Nothing) Then
                  dataRow("SmallImage") = item.SmallImage.URL
                End If
                If (Not (item.MediumImage) Is Nothing) Then
                  dataRow("MediumImage") = item.MediumImage.URL
                End If
                If (Not (item.LargeImage) Is Nothing) Then
                  dataRow("LargeImage") = item.LargeImage.URL
                End If
    
                If (Not (item.ItemAttributes.Binding) Is Nothing) Then
                  dataRow("Binding") = item.ItemAttributes.Binding
                End If
    
                If (Not (item.ItemAttributes.PublicationDate) Is Nothing) Then
                  dataRow("PublicationDate") = item.ItemAttributes.PublicationDate
                End If
                If (Not (item.ItemAttributes.Publisher) Is Nothing) Then
                  dataRow("Publisher") = item.ItemAttributes.Publisher
                End If
                If (Not (item.ItemAttributes.ListPrice) Is Nothing) Then
                  dataRow("ListPrice") = item.ItemAttributes.ListPrice.FormattedPrice
                End If
                If (Not (item.ItemAttributes.Title) Is Nothing) Then
                  dataRow("Title") = item.ItemAttributes.Title
                End If
    
                If (Not (item.ItemAttributes.Author) Is Nothing) Then
                  dataRow("Author") = item.ItemAttributes.Author(0)
                Else
                  dataRow("Author") = " "
                End If
    
                If (Not (item.ItemAttributes.ProductGroup) Is Nothing) Then
                  dataRow("ProductGroup") = item.ItemAttributes.ProductGroup
                End If
    
                If (Not (item.Offers) Is Nothing) Then
                  Dim offerArray As Offer() = item.Offers.Offer
                  If (Not (offerArray) Is Nothing) Then
                    If (Not (offerArray(0).OfferListing) Is Nothing) Then
                      If (Not (offerArray(0).OfferListing(0).Price) Is Nothing) Then
                        dataRow("AmazonPrice") = offerArray(0).OfferListing(0).Price.FormattedPrice
                      End If
                    End If
                  End If
                End If
    
                If (Not (item.CustomerReviews) Is Nothing) Then
                  dataRow("AverageRating") = item.CustomerReviews.AverageRating.ToString()
                  dataRow("TotalReviews") = item.CustomerReviews.TotalReviews.ToString()
                End If
    
                dt.Rows.Add(dataRow)
              End If
            Loop
          End If
        End If
      End If
    
      Return dt
    End Function
    
    Private Function CreateDataTable(seed As Integer) As DataTable
      Dim dt As New DataTable
      Dim dc As DataColumn
    
      With dt.Columns
        dc = New DataColumn("AutoID", GetType(Int32))
        dc.AutoIncrement = True
        dc.AutoIncrementSeed = seed
        dc.AutoIncrementStep = 1
        .Add(dc)
        .Add(New DataColumn("ASIN", GetType(System.String)))
        .Add(New DataColumn("SmallImage", GetType(System.String)))
        .Add(New DataColumn("MediumImage", GetType(System.String)))
        .Add(New DataColumn("LargeImage", GetType(System.String)))
    
        .Add(New DataColumn("AmazonURL", GetType(System.String)))
        .Add(New DataColumn("Title", GetType(System.String)))
        .Add(New DataColumn("Author", GetType(System.String)))
        .Add(New DataColumn("ListPrice", GetType(System.String)))
    
        .Add(New DataColumn("SalesRank", GetType(System.String)))
        .Add(New DataColumn("PublicationDate", GetType(System.String)))
        .Add(New DataColumn("Publisher", GetType(System.String)))
        .Add(New DataColumn("Binding", GetType(System.String)))
        .Add(New DataColumn("AverageRating", GetType(System.String)))
        .Add(New DataColumn("TotalReviews", GetType(System.String)))
    
        .Add(New DataColumn("ProductGroup", GetType(System.String)))
        .Add(New DataColumn("AmazonPrice", GetType(System.String)))
      End With
      Return dt
    End Function
    
    Public Property SubscriptionID() As String
      Get
        Return _SubscriptionID
      End Get
      Set(ByVal Value As String)
        _SubscriptionID = Value
      End Set
    End Property
    
    Public Property AssociateTag() As String
      Get
        Return _AssociateTag
      End Get
      Set(ByVal Value As String)
        _AssociateTag = Value
      End Set
    End Property

</script>
<html>
<head>
<script>
function getBooks() {
  var queryString = document.getElementById("txtQueryString").value;
  My.Page.GetBooks(queryString, getBooksCallBack);
  window.status = "Amazon検索中...";
}

function getBooksCallBack(result) {
  var books = result.value;
  var div = document.getElementById("divAmazonBooks");
  div.innerHTML = "";
  for (var i=0; i < books.Rows.length; ++i) {
    anchor = document.createElement("A");
    anchor.setAttribute("href",books.Rows[i].AmazonURL);
    anchor.setAttribute("target","_blank");

    image = document.createElement("IMG");
    image.setAttribute("src", books.Rows[i].MediumImage);
    image.setAttribute("alt", books.Rows[i].Title);
    image.setAttribute("border","0");
    image.setAttribute("width",100);
    image.setAttribute("height",130);

    anchor.appendChild(image);
    div.appendChild(anchor);
  }
  window.status = "Amazon検索完了!";
}
</script>
</head>
<body>
    <form runat="server">
        <h3>Ajax4 (<%= DateTime.Now %>)</h3>
        <input id="txtQueryString" type="text" size="20" value="asp.net" />
        <input onclick="getBooks();" type="button" value="Amazon検索" />
        <br />
        <div id="divAmazonBooks">
        </div>
    </form>
</body>
</html>
