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

        public ClientVocFindTemplate(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))) {
                        code += "ESQuery vocabularyQuery = new ESQuery(\"Name == '" + getStringPropertyValue(ClientProperty.VOCABULARY_NAME) + "'\");\n";
                        }
                else {
                        return super.generate(target);
                        }

                code += "ESVocabularyFinder vocabularyFinder = new ESVocabularyFinder(getESConnection());\n";
                code += "ESVocabulary " + ClientAttribute.LOCAL_VOCABULARY + " = null;\n";
                code += "try {\n";
                code += ClientAttribute.LOCAL_VOCABULARY + " = vocabularyFinder.find(vocabularyQuery);\n";
                code += "}\ncatch(ESInvocationException invocationException) {\n";
                code += "handleException(\"during vocabulary lookup \", invocationException);\n";
                code += "return null;\n";
                code += "}\n";
                code += "\ncatch(LookupFailedException lookupException) {\n";
                code += "handleException(\"during vocabulary 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() {
                if (ClientConstant.CUSTOM_QUERY.equals(getPropertyValue(ClientProperty.SERVICE_QUERY_TYPE))) {
                        String vocName = getStringPropertyValue(ClientProperty.VOCABULARY_NAME);
                        if (vocName == null) {
                                setErrorMessage("Specify valid Vocabulary name.");
                                return false;
                                }
                        }
                return super.canGenerate();
                }
        }


