/*
 * [TestItemSearch.java]
 *
 * Summary: Sample use of Amazon API to search for a book.
 *
 * Copyright: (c) 2012-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2012-03-02 initial version
 */
package com.mindprod.example;

import com.mindprod.aws.jax.AWSECommerceService;
import com.mindprod.aws.jax.AWSECommerceServicePortType;
import com.mindprod.aws.jax.AwsHandlerResolver;
import com.mindprod.aws.jax.Item;
import com.mindprod.aws.jax.ItemSearch;
import com.mindprod.aws.jax.ItemSearchRequest;
import com.mindprod.aws.jax.Items;
import com.mindprod.aws.jax.OperationRequest;

import javax.xml.namespace.QName;
import javax.xml.ws.Holder;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import static java.lang.System.*;

/**
 * Sample use of Amazon API to search for a book.
 * <p/>
 * All Amazon documentation on the AWS interface is out of date.
 * This code works, discovered by experiment and by reading hints posted on many forums.
 * You must download modify and compile AwsHandlerResolver.java.
 * I modified my copy to use CMP Base64.encode rather than the larger Apache package.
 * Prior to running, you must set up environment parameters:
 * SET awsaccesskeyid=xxxxx
 * SET awssecretaccesskey=xxxxx
 * SET awsassociatetag=canadianmindbprod
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2012-03-02 initial version
 * @since 2012-03-02
 */
public class TestItemSearch
    {
    /**
     * true if debugging, and want extra output
     */
    private static final boolean DEBUGGING = false;

    /**
     * true if probe amazon.ca , false amazon.com. Surprisingly this is only referenced twice in the program.
     */
    private static final boolean IS_CANADIAN = true;

    /**
     * fetch public amazon.com-assigned associateTag from environment set awsassociatetag=xxxxx
     */
    private static final String ASSOCIATE_TAG = IS_CANADIAN ? System.getenv( "awsassociatetagca" ) : System.getenv(
            "awsassociatetag" );

    /**
     * fetch confidential amazon.com-assigned awsAccessKeyId from environment set awsaccesskeyid=xxxxx
     */
    private static final String AWS_ACCESS_KEY_ID = System.getenv( "awsaccesskeyid" );

    /**
     * fetch confidential amazon.com-assigned awsSecretAccessKey assigned by Amazon set awssecretaccesskey=xxxxx
     */
    private static final String AWS_SECRET_ACCESS_KEY = System.getenv( "awssecretaccesskey" );

    /**
     * Nampspace for amazon.com in the USA.  Would have to be modified for other countries.
     */
    private static final String NAME_SPACE_URI = "http://webservices.amazon.com/AWSECommerceService/2013-08-01";

    /**
     * name of the SOAP service
     */
    private static final String QNAME = "AWSECommerceService";

    /**
     * location of JAX schmo, locally cached. for amazon.com in the USA.  Would have to be modified for other countries.
     * Original came from http://ecs.amazonaws.com/AWSECommerceService/AWSECommerceService.wsd
     */
    private static final String WSDL_LOCATION = "file:///E:/com/mindprod/aws/jax/AWSECommerceService.wsdl";

    /**
     * standard boilerplate to connect to amazon.com AWS server with SOAP
     *
     * @return port to use to send requests.
     * @throws java.net.MalformedURLException if some URLs were mis-specified in the following code.
     */
    private static AWSECommerceServicePortType getPort() throws MalformedURLException
        {
        // Set the service:
        AWSECommerceService service = new AWSECommerceService( new URL( WSDL_LOCATION ), new QName( NAME_SPACE_URI,
                QNAME ) );
        // AwsHandlerResolver does the timestamp, signing and Base64 encoding
        service.setHandlerResolver( new AwsHandlerResolver( AWS_SECRET_ACCESS_KEY ) );
        // Set the service port:
        return IS_CANADIAN ? service.getAWSECommerceServicePortCA() : service.getAWSECommerceServicePort();
        }

    /**
     * Test item search, to demonstrate the AMAZON AWS API.
     *
     * @param args not used
     *
     * @throws MalformedURLException if urls returned are malformed.
     */
    public static void main( String[] args ) throws MalformedURLException
        {
        if ( DEBUGGING )
            {
            out.println( "ASSOCIATE_TAG = " + ASSOCIATE_TAG );
            out.println( "AWS_ACCESS_KEY_ID = " + AWS_ACCESS_KEY_ID );
            out.println( "AWS_SECRET_ACCESS_KEY = " + AWS_SECRET_ACCESS_KEY );
            }
        // Set the service port:
        final AWSECommerceServicePortType port = getPort();
        // new ItemSearch
        final ItemSearch itemSearch = new ItemSearch();
        // new ItemSearchRequest which is part of the ItemSearch
        final ItemSearchRequest searchRequest = new ItemSearchRequest();
        final List<ItemSearchRequest> itemSearchRequests = itemSearch.getRequest();
        itemSearchRequests.add( searchRequest );
        // Set up the values of the ItemSearchRequest
        searchRequest.setSearchIndex( "Books" );
        searchRequest.setAuthor( "Richard Dawkins" );
        // specify info to include in response
        final List<String> responseGroup = searchRequest.getResponseGroup();
        responseGroup.add( "Images" );
        responseGroup.add( "ItemAttributes" );
        responseGroup.add( "Small" );
        // set up Holder for the response tree
        final Holder<OperationRequest> operationrequest = new Holder<>();
        final Holder<java.util.List<Items>> items = new Holder<>();
        final String marketplaceDomain = "";  // not yet supported
        final String xmlEscaping = "Single";
        final String validate = "";
        // Probe Amazon Server, WARNING! Amazon changes the parms to this method frequently.
        // Inserts awsAccessKeyID and associateTag
        // note order xmlEscaping, validate different from itemLookup
        port.itemSearch( marketplaceDomain, AWS_ACCESS_KEY_ID, ASSOCIATE_TAG, xmlEscaping, validate, searchRequest,
                itemSearchRequests, operationrequest, items );
        // analyse results
        final List<Items> result = items.value;
        final BigInteger totalPages = result.get( 0 ).getTotalResults();
        out.println( totalPages );
        final int size = result.get( 0 ).getItem().size();
        for ( int i = 0; i < size; i++ )
            {
            final Item item = result.get( 0 ).getItem().get( i );
            out.print( item.getASIN() );
            out.print( ", " );
            out.println( item.getDetailPageURL() );
            out.print( ", " );
            out.println( item.getLargeImage() == null ? "" : item.getLargeImage().getURL() );
            }
        }
    }