/*----------------------------------------------------------------------------
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.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 ClientSrvQueryTemplate extends LeafSciTemplate {

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


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

                String code = "\n";
                if (ClientConstant.XML_QUERY.equals(getPropertyValue(ClientProperty.SERVICE_QUERY_TYPE))) {
                        String xmlFile = "new ESXMLFile(\"" + getStringPropertyValue(ClientProperty.XMLFILE_NAME) + "\")";
                        if (Boolean.TRUE.equals(getPropertyValue(ClientProperty.PARAMETRIZE_XMLFILE_NAME))) {
                                xmlFile = "new ESXMLFile(this." + ClientAttribute.GLOBAL_XMLFILE + ")";
                                }

                        code += "ESXMLQuery " + ClientAttribute.LOCAL_ESQUERY + " = null;";
                        code += "try {\n";
                        code += "new ESXMLQuery(getESConnection(), " + xmlFile + ");\n";
                        code += "}\n catch(ESLibException esLibException) {\n";
                        code += "handleException(\"during creation of ESXMLQuery\", esLibException);\n";
                        code += "return null;";
                        code += "}\n";
                        }
                else if (ClientConstant.NAME_QUERY.equals(getPropertyValue(ClientProperty.SERVICE_QUERY_TYPE))) {
                        String serviceName = getStringPropertyValue(ClientProperty.NAME_CONSTRAINT);
                        if (Boolean.TRUE.equals(getPropertyValue(ClientProperty.PARAMETRIZE_NAME_CONSTRAINT))) {
                                serviceName = "this." + ClientAttribute.GLOBAL_SERVICE_NAME;
                                }

                        code += "ESQuery " + ClientAttribute.LOCAL_ESQUERY + " = new ESQuery(" + "\"Name == '" + serviceName + "'\"" + ");\n";
                        }
                else {
                        code += "ESQuery " + ClientAttribute.LOCAL_ESQUERY + " = new ESQuery(" +  ClientAttribute.LOCAL_VOCABULARY + ");\n";
                        }
                code += "\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() {
                if (ClientConstant.NAME_QUERY.equals(getPropertyValue(ClientProperty.SERVICE_QUERY_TYPE)) &&
                    Boolean.FALSE.equals(getPropertyValue(ClientProperty.PARAMETRIZE_NAME_CONSTRAINT))
                    ) {
                        String serviceName = getStringPropertyValue(ClientProperty.NAME_CONSTRAINT);
                        if (serviceName == null) {
                                setErrorMessage("Specify valid Service name.");
                                return false;
                                }
                        }

                if (ClientConstant.XML_QUERY.equals(getPropertyValue(ClientProperty.SERVICE_QUERY_TYPE)) &&
                    Boolean.FALSE.equals(getPropertyValue(ClientProperty.PARAMETRIZE_XMLFILE_NAME))
                    ) {
                        String xmlName = getStringPropertyValue(ClientProperty.XMLFILE_NAME);
                        if (xmlName == null) {
                                setErrorMessage("Specify valid XML File name.");
                                return false;
                                }
                        }
                return super.canGenerate();
                }
        }


