DataConnection

Code in this section is saved in a file called DataConnection.java.

Code

All the connections-to-purposes mappings have been reorganized into the DataConnection class. The AE determines its purpose based on the API type of the data connection and the value of the AE environment variables. This code will be used in the section ApplyDriver Version 3.

// DataConnection.java
// Encapsulates logic used once a data connection
// has been obtained
package org.netezza.education;

import org.netezza.ae.*;

public class DataConnection
{
    static public void useApi(NzaeApi api) {
        Nzae ae;
        switch (api.apiType)
        {
        case NzaeApi.FUNCTION:
            ae = api.aeFunction;
            break;
        case NzaeApi.SHAPER:
            try {
                String whichFunction =
                    api.aeShaper.getEnvironment().getValue(
                       "APPLY_FUNCTION");
                if (whichFunction == null) {
                    String msg =
                        "missing APPLY_FUNCTION " +
                        "environment variable";
                    System.err.println(msg);
                    api.aeShaper.userError(msg);
                } else if (!whichFunction.equals("CLONE")) {
                    String msg = "only clone has a shaper";
                    System.err.println(msg);
                    api.aeShaper.userError(msg);
                } else {
                    String printInfo =
                        api.aeShaper.getEnvironment().getValue(
                            "APPLY_INFO");
                    if (printInfo != null
                            && printInfo.equalsIgnoreCase(
                                "true")) {
                        ApplyUtil.printMetadata(api.aeShaper);
                        ApplyUtil.printRuntime(api.aeShaper);
                        ApplyUtil.printSharedLibraries(
                            api.aeShaper);
                        ApplyUtil.printEnvironment(
                            api.aeShaper);
                    }
                    CloneRows.runShaper(api.aeShaper);
                    return;
                }
            } finally {
                api.close();
            }
            return;
        default:
        {
            System.err.println("unexpected API");
            api.close();
            return;
        } // end of default
        } // end of case

        try {
            // determine which function is being invoked
            String whichFunction =
                ae.getEnvironment().getValue("APPLY_FUNCTION");
            if (whichFunction == null) {
                ae.userError(
                    "missing APPLY_FUNCTION " +
                    "environment variable");
                return;
            }
            String printInfo =
                ae.getEnvironment().getValue("APPLY_INFO");
            if (printInfo != null
                    && printInfo.equalsIgnoreCase("true")) {
                ApplyUtil.printMetadata(ae);
                ApplyUtil.printRuntime(ae);
                ApplyUtil.printSharedLibraries(ae);
                ApplyUtil.printEnvironment(ae);
            }

            if (whichFunction.equals("OPERATION")) {
                ApplyOperation.runAe(ae);
            } else if (whichFunction.equals("RESULT")) {
                ApplyResult.runAe(ae);
            } else if (whichFunction.equals("CLONE")) {
                CloneRows.runAe(ae);
            } else {
                ae.userError("unexpected value for " +
                    "environment variable APPLY_FUNCTION");
            }
        } finally {
            ae.close();
        }
    }
}