/*----------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft Corporation. Patents pending. All rights reserved.
----------------------------------------------------------------------------*/
package com.togethersoft.modules.patterns.JAVA.CLASS.HP_E_Speak.Client.code;

import  com.togethersoft.openapi.sci.*;
import  com.togethersoft.openapi.util.propertyMap.PropertyMap;

import com.togethersoft.modules.patterns.JAVA.CLASS.HP_E_Speak.Client.LeafSciTemplate;
import com.togethersoft.modules.patterns.JAVA.CLASS.HP_E_Speak.Client.ClientProperty;
import com.togethersoft.modules.patterns.JAVA.CLASS.HP_E_Speak.Client.ClientConstant;
/**
 * @author TogetherSoft
 */
class ClientConnGetterTemplate extends LeafSciTemplate {


        public ClientConnGetterTemplate(PropertyMap properties) {
                super(properties);
                }

        public SciObject generate(SciObject target) {

                SciClass clazz = (SciClass) target;

                /*
                 * Generate getter.
                 */
                SciOperation operation = getSciFactory().newOperation();
                operation.setName("getESConnection");
                operation.setProperty(SciProperty.PROTECTED, true);
                operation.getReturnType().setText("ESConnection");


                /*
                 * Generate getter code.
                 */
                String code = "if (this." + ClientAttribute.GLOBAL_CONNECTION + " == null) {\n";
                code += "try {\n";

                if (ClientConstant.PARAMETRIZED_CONNECTION.equals(getPropertyValue(ClientProperty.CONNECTION_TYPE))) {
                        code += "this." + ClientAttribute.GLOBAL_CONNECTION + " = " + "new ESConnection(this." + ClientAttribute.GLOBAL_CONNECTION_PROPERTIES + ");\n";
                        }
                if (ClientConstant.DEFAULT_CONNECTION.equals(getPropertyValue(ClientProperty.CONNECTION_TYPE))) {
                        code += "this." + ClientAttribute.GLOBAL_CONNECTION + " = new ESConnection();\n";
                        }
                if (ClientConstant.HARDCODED_CONNECTION.equals(getPropertyValue(ClientProperty.CONNECTION_TYPE))) {
                        String parameters = "\"" + getStringPropertyValue(ClientProperty.HOSTNAME) + "\",";
                        parameters += getStringPropertyValue(ClientProperty.PORTNUMBER) + ",";
                        parameters += "\"" + getStringPropertyValue(ClientProperty.PROTOCOL) + "\"";

                        code += "this." + ClientAttribute.GLOBAL_CONNECTION + " = " + "new ESConnection(" + parameters + ");\n";
                        }

                code += "System.out.println(\"[HP-ES Client] Connected to HP-ES Core\");\n";
                code += "}\n";
                code += "catch(ESInvocationException invocationException) {\n";
                code += "\nhandleException(\"during establishing connection to ES Core\", invocationException);\n";
                code += "return null;\n}\n";
                code += "catch(ESLibException libException) {\n";
                code += "\nhandleException(\"during establishing connection to ES Core\", libException);\n";
                code += "return null;\n}\n";
                code += "}\n";
                code += "return this." + ClientAttribute.GLOBAL_CONNECTION + ";\n";

                try {
                        SciCodeBlock codeBlock = getSciGenericFactory().newCodeBlock(code);
                        operation.setBody(codeBlock);
                        }
                catch(SciGenericFactoryException sciGFException) {}

                operation = (SciOperation) clazz.paste(operation, null, true);

                /*
                 * Add ESConnetcion attribute.
                 */

                SciAttribute attribute = getSciFactory().newAttribute();
                attribute.setName(ClientAttribute.GLOBAL_CONNECTION);
                attribute.getType().setText("ESConnection");
                attribute = (SciAttribute) clazz.paste(attribute, null, true);

                return super.generate(clazz);
                }

        public boolean canGenerate() {
                if (ClientConstant.HARDCODED_CONNECTION.equals(getPropertyValue(ClientProperty.CONNECTION_TYPE))) {
                        String protocol = getStringPropertyValue(ClientProperty.PROTOCOL);
                        String hostname = getStringPropertyValue(ClientProperty.HOSTNAME);
                        String portnumber = getStringPropertyValue(ClientProperty.PORTNUMBER);

                        if (protocol == null) {
                                setErrorMessage("Specify valid protocol for ES Connection.");
                                return false;
                                }

                        if (hostname == null) {
                                setErrorMessage("Specify valid host name for ES Connection.");
                                return false;
                                }
                        try {
                                Integer.parseInt(portnumber);
                                }
                        catch(Exception nfException) {
                                setErrorMessage("Specify valid port number for ES Connection.");
                                return false;
                                }
                        }

                return super.canGenerate();
                }

        }


