/*
 * Licensed Materials - Property of IBM
 * © Copyright IBM Corporation 2015. All Rights Reserved.
 */

package com.ibm.mil.cafejava;

import android.support.annotation.NonNull;
import android.support.annotation.StringDef;

import com.worklight.wlclient.api.WLResourceRequest;
import com.worklight.wlclient.api.WLResponseListener;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * Implementation for invoking a procedure from a Java based adapter.
 *
 * @see JSProcedureInvoker
 *
 * @author John Petitto  (github @jpetitto)
 * @author Tanner Preiss (github @t-preiss)
 */
public final class JavaProcedureInvoker implements ProcedureInvoker {
    /** StringDef for basic HTTP method types: {@code GET, POST, PUT, DELETE}. */
    @Retention(RetentionPolicy.SOURCE)
    @StringDef({
            GET,
            POST,
            PUT,
            DELETE
    })
    public @interface HttpMethod {}
    /** Annotated with the {@code HttpMethod} StringDef */
    public static final String GET = WLResourceRequest.GET;
    /** Annotated with the {@code HttpMethod} StringDef */
    public static final String POST = WLResourceRequest.POST;
    /** Annotated with the {@code HttpMethod} StringDef */
    public static final String PUT = WLResourceRequest.PUT;
    /** Annotated with the {@code HttpMethod} StringDef */
    public static final String DELETE = WLResourceRequest.DELETE;

    private final String adapterName;
    private final String procedureName;
    private HashMap<String, String> pathParams;
    private HashMap<String, String> queryParams;
    private @HttpMethod String httpMethod;

    private JavaProcedureInvoker(String adapterName, String procedureName) {
        this.adapterName = adapterName;
        this.procedureName = procedureName;
    }

    @Override
    public void invoke(WLResponseListener wlResponseListener) {
        try {
            URI path = new URI("adapters/" + adapterName + "/" + procedureName);
            WLResourceRequest request = new WLResourceRequest(path, httpMethod);
            request.setQueryParameters(queryParams);
            request.send(pathParams, wlResponseListener);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    /** Configures and instantiates a {@code JavaProcedureInvoker}. */
    public static class Builder {
        private final String adapterName;
        private final String procedureName;
        private HashMap<String, String> pathParams = new HashMap<>();
        private HashMap<String, String> queryParams = new HashMap<>();
        private @HttpMethod String httpMethod = GET;

        public Builder(String adapterName, String procedureName) {
            this.adapterName = adapterName;
            this.procedureName = procedureName;
        }

        public Builder pathParam(@NonNull String name, @NonNull String value) {
            pathParams.put(name, value);
            return this;
        }

        public Builder pathParams(@NonNull Map<String, String> params) {
            pathParams.putAll(params);
            return this;
        }

        public Builder queryParam(@NonNull String name, @NonNull String value) {
            queryParams.put(name, value);
            return this;
        }

        public Builder queryParams(@NonNull Map<String, String> params) {
            queryParams.putAll(params);
            return this;
        }

        /** Expects an {@code HttpMethod} StringDef constant value. Default is {@code GET}. */
        public Builder httpMethod(@NonNull @HttpMethod String httpMethod) {
            this.httpMethod = httpMethod;
            return this;
        }

        public JavaProcedureInvoker build() {
            JavaProcedureInvoker invoker = new JavaProcedureInvoker(adapterName, procedureName);
            invoker.pathParams = pathParams;
            invoker.queryParams = queryParams;
            invoker.httpMethod = httpMethod;
            return invoker;
        }
    }

}

@@ -1583,36 +1583,27 @@
 nal String p
-rocedureName
+ath
 ;%0A    privat
@@ -1790,36 +1790,27 @@
 me, String p
-rocedureName
+ath
 ) %7B%0A        
@@ -1859,36 +1859,18 @@
 is.p
-rocedureName = procedureName
+ath = path
 ;%0A  
@@ -1982,20 +1982,19 @@
     URI 
-path
+uri
  = new U
@@ -2031,28 +2031,19 @@
  %22/%22 + p
-rocedureName
+ath
 );%0A     
@@ -2099,20 +2099,19 @@
 Request(
-path
+uri
 , httpMe
@@ -2496,28 +2496,19 @@
 String p
-rocedureName
+ath
 ;%0A      
@@ -2747,28 +2747,19 @@
 String p
-rocedureName
+ath
 ) %7B%0A    
@@ -2820,36 +2820,18 @@
 is.p
-rocedureName = procedureName
+ath = path
 ;%0A  
@@ -3834,20 +3834,11 @@
 e, p
-rocedureName
+ath
 );%0A 

Change name "procedureName" to "path" in JavaProcedureInvoker