/*----------------------------------------------------------------------------
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.sci.enum.SciClassEnumeration;
import  com.togethersoft.openapi.sci.enum.SciInheritanceEnumeration;

import  com.togethersoft.openapi.util.propertyMap.PropertyMap;
import  com.togethersoft.openapi.util.propertyMap.Property;

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;

import  java.util.Enumeration;
/**
 * @author TogetherSoft
 */
class ClientSrvFindTemplate extends LeafSciTemplate {

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


        public SciObject generate(SciObject target) {
                SciCodeBlock codeBlock = (SciCodeBlock) target;

                String code = "\n";

                if (ClientConstant.CUSTOM_QUERY.equals(getPropertyValue(ClientProperty.SERVICE_QUERY_TYPE))) {
                        for(Enumeration c = getProperties().properties(ClientProperty.CUSTOM_CONSTRAINT); c.hasMoreElements();) {
                                String constraint = (String) ((Property) c.nextElement()).getValue();
                                code += ClientAttribute.LOCAL_ESQUERY + ".addConstraint(\"" + constraint + "\");\n";
                                }
                        }

                code += "ESServiceFinder serviceFinder = new ESServiceFinder(getESConnection(), " + getStringPropertyValue(ClientProperty.SERVICE_INTERFACE_NAME) +".class.getName());\n";
                code += "try {\n";
                code += "this." + ClientAttribute.GLOBAL_SERVICE + " = (" + getStringPropertyValue(ClientProperty.SERVICE_INTERFACE_NAME)+ ") serviceFinder.find(" + ClientAttribute.LOCAL_ESQUERY + ");\n";
                code += "System.out.println(\"[HP-ES Client] Service fetched from HP-ES Core.\");";
                code += "}\ncatch(ESInvocationException invocationException) {\n";
                code += "handleException(\"during service lookup \", invocationException);\n";
                code += "return null;\n";
                code += "}\n";
                code += "\ncatch(LookupFailedException lookupException) {\n";
                code += "handleException(\"during service lookup \", lookupException);\n";
                code += "return null;\n";
                code += "}\n\n";

                try {
                        SciCodeBlock findCodeBlock = getSciGenericFactory().newCodeBlock(code);

                        SciStatement lastStatement = null;
                        for(Enumeration statements = findCodeBlock.statements(); statements.hasMoreElements();) {
                                SciStatement statement = (SciStatement) statements.nextElement();
                                lastStatement = (SciStatement) codeBlock.paste(statement, lastStatement, false);
                                }
                        }
                catch(SciGenericFactoryException sciGFException) {}

                return super.generate(codeBlock);
                }

        public boolean canGenerate() {
                return super.canGenerate();
                }
        }


