/*----------------------------------------------------------------------------
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
 */
public class ClientMainMethodTemplate extends LeafSciTemplate {


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

        public SciObject generate(SciObject target) {

                SciClass clazz = (SciClass) target;

                /*
                 * Generate method.
                 */
                SciOperation operation = getSciFactory().newOperation();
                operation.setName("main");
                operation.setProperty(SciProperty.PUBLIC, true);
                operation.setProperty(SciProperty.STATIC, true);
                operation.getParameterList().setText("String[] args");


                /*
                 * Generate getter code.
                 */
                String code = "try {\n";

                int baseIndex = 0;
                String parameters = "";
                if (ClientConstant.PARAMETRIZED_CONNECTION.equals(getPropertyValue(ClientProperty.CONNECTION_TYPE))) {
                        code += "String " + ClientAttribute.PARAMETER_CONNECTION + " = args[" + baseIndex + "];\n";
                        parameters += ClientAttribute.PARAMETER_CONNECTION;
                        baseIndex++;
                        }

                if (ClientConstant.NAME_QUERY.equals(getPropertyValue(ClientProperty.SERVICE_QUERY_TYPE)) &&
                    Boolean.TRUE.equals(getPropertyValue(ClientProperty.PARAMETRIZE_NAME_CONSTRAINT))) {
                        code += "String " + ClientAttribute.PARAMETER_SERVICE_NAME + " = args[" + baseIndex + "];\n";
                        if (parameters.length() > 0) {
                                parameters += ", ";
                                }
                        parameters += ClientAttribute.PARAMETER_SERVICE_NAME;
                        baseIndex++;
                        }
                if (ClientConstant.XML_QUERY.equals(getPropertyValue(ClientProperty.SERVICE_QUERY_TYPE)) &&
                    Boolean.TRUE.equals(getPropertyValue(ClientProperty.PARAMETRIZE_XMLFILE_NAME))) {
                        code += "String " + ClientAttribute.PARAMETER_XMLFILE + " = args[" + baseIndex + "];\n";
                        if (parameters.length() > 0) {
                                parameters += ", ";
                                }
                        parameters += ClientAttribute.PARAMETER_XMLFILE;
                        baseIndex++;
                        }

                String paramters =
                code += "(new " + clazz.getName() + "(" + parameters + ")).start();\n";

                code += "}\n catch(Exception e) {\n";
                code += "handleException(\"during executing \\\"main\\\" method\", e);\n";
                //code += "System.exit(-1);\n";
                code += "\n}\n";
                //code += "System.exit(0);\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;
                }

        }


