/*----------------------------------------------------------------------------
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.*;
import  com.togethersoft.openapi.ide.IdeLocalDiagnostic;
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 ClientStartMethodTemplate extends LeafSciTemplate {


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

        public SciObject generate(SciObject target) {

                SciClass clazz = (SciClass) target;

                /*
                 * Generate method.
                 */
                SciOperation operation = null;
                try {
                        operation = (SciOperation) getSciGenericFactory().newMember("public void start() throws Exception {\n}", clazz);
                        }
                catch(SciGenericFactoryException e) {
                        return clazz;
                        }

                //operation.setName("start");
                //operation.setProperty(SciProperty.PUBLIC, true);
                //operation.getThrowList().setText("Exception");

                /*
                 * Generate getter code.
                 */
                String code = getStringPropertyValue(ClientProperty.SERVICE_INTERFACE_NAME) + " service = getESService();\n";
                code += "if (service != null) {\n";
                code += "// put your code here\n";
                // invoke test method here, if exists in service.
                SciClass servizeClazz = SciModelAccess.getModel().findClass(SciLanguage.JAVA, getStringPropertyValue(ClientProperty.SERVICE_INTERFACE_NAME));
                if (servizeClazz != null) {
                        for(SciOperationEnumeration operations = servizeClazz.operations(); operations.hasMoreElements();) {
                                SciOperation op = operations.nextSciOperation();
                                IdeLocalDiagnostic.trace("operation name : " + op.getName());
                                IdeLocalDiagnostic.trace("operation parameters : " + op.getParameterList().getText());
                                if (op.getName().equals("test") && (op.getParameterList().getText().trim().length() == 0)) {
                                        code += "service.test();\n";
                                        break;
                                        }

                                }
                        }
                // finish with if.
                code += "}\n";

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

                operation = (SciOperation) clazz.paste(operation, null, true);
                return super.generate(clazz);
                }

        public boolean canGenerate() {
                return true;
                }

        }


