/*----------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft Corporation. Patents pending. All rights reserved.
----------------------------------------------------------------------------*/
package com.togethersoft.modules.patterns.JAVA.CLASS.HP_E_Speak.Service_Provider.code;

import com.togethersoft.openapi.sci.*;
import com.togethersoft.openapi.util.propertyMap.PropertyMap;

import com.togethersoft.modules.patterns.JAVA.CLASS.HP_E_Speak.Service_Provider.LeafSciTemplate;
import com.togethersoft.modules.patterns.JAVA.CLASS.HP_E_Speak.Service_Provider.ClientProperty;
import com.togethersoft.modules.patterns.JAVA.CLASS.HP_E_Speak.Service_Provider.ClientConstant;

class ServiceConstrTemplate extends LeafSciTemplate {


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

        public SciObject generate(SciObject target) {

                SciClass clazz = (SciClass) target;

                /*
                 * Generate constructor.
                 */
                SciOperation operation = getSciFactory().newOperation();
                operation.setName(clazz.getName());
                operation.setProperty(SciProperty.CONSTRUCTOR, true);

                /*
                 * Set up constructor paramters.
                 */
                SciParameterList paramters = operation.getParameterList();
                String paramtersText = "";

                if (ClientConstant.PARAMETRIZED_CONNECTION.equals(getPropertyValue(ClientProperty.CONNECTION_TYPE))) {
                        paramtersText += "String ";
                        paramtersText += ServiceAttribute.PARAMETER_CONNECTION;
                        }

                if (ClientConstant.NAME_DESCRIPTION.equals(getPropertyValue(ClientProperty.SERVICE_DESCRIPTION_TYPE)) &&
                    Boolean.TRUE.equals(getPropertyValue(ClientProperty.PARAMETRIZE_NAME_CONSTRAINT))) {
                        if (paramtersText.length() > 0) {
                                paramtersText += ", ";
                                }
                        paramtersText += "String ";
                        paramtersText += ServiceAttribute.PARAMETER_SERVICE_NAME;
                        }
                if (ClientConstant.XML_DESCRIPTION.equals(getPropertyValue(ClientProperty.SERVICE_DESCRIPTION_TYPE)) &&
                    Boolean.TRUE.equals(getPropertyValue(ClientProperty.PARAMETRIZE_XMLFILE_NAME))) {
                        if (paramtersText.length() > 0) {
                                paramtersText += ", ";
                                }
                        paramtersText += "String ";
                        paramtersText += ServiceAttribute.PARAMETER_XMLFILE;
                        }

                paramters.setText(paramtersText);

                /*
                 * Generate constructor code. (assignmets).
                 */
                String code = "";

                if (ClientConstant.PARAMETRIZED_CONNECTION.equals(getPropertyValue(ClientProperty.CONNECTION_TYPE))) {
                        code += "this." + ServiceAttribute.GLOBAL_CONNECTION_PROPERTIES + " = " + ServiceAttribute.PARAMETER_CONNECTION + ";\n";
                        }

                if (ClientConstant.NAME_DESCRIPTION.equals(getPropertyValue(ClientProperty.SERVICE_DESCRIPTION_TYPE)) &&
                    Boolean.TRUE.equals(getPropertyValue(ClientProperty.PARAMETRIZE_NAME_CONSTRAINT))) {
                        code += "this." + ServiceAttribute.GLOBAL_SERVICE_NAME + " = " + ServiceAttribute.PARAMETER_SERVICE_NAME + ";\n";
                        }
                if (ClientConstant.XML_DESCRIPTION.equals(getPropertyValue(ClientProperty.SERVICE_DESCRIPTION_TYPE)) &&
                    Boolean.TRUE.equals(getPropertyValue(ClientProperty.PARAMETRIZE_XMLFILE_NAME))) {
                        code += "this." + ServiceAttribute.GLOBAL_XMLFILE + " = " + ServiceAttribute.PARAMETER_XMLFILE + ";\n";
                        }

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


                /*
                 * Generate attributes.
                 */
                if (ClientConstant.PARAMETRIZED_CONNECTION.equals(getPropertyValue(ClientProperty.CONNECTION_TYPE))) {
                        SciAttribute attribute = getSciFactory().newAttribute();
                        attribute.setName(ServiceAttribute.GLOBAL_CONNECTION_PROPERTIES);
                        attribute.getType().setText("String");
                        clazz.paste(attribute, null, true);
                        }

                if (ClientConstant.NAME_DESCRIPTION.equals(getPropertyValue(ClientProperty.SERVICE_DESCRIPTION_TYPE)) &&
                    Boolean.TRUE.equals(getPropertyValue(ClientProperty.PARAMETRIZE_NAME_CONSTRAINT))) {
                        SciAttribute attribute = getSciFactory().newAttribute();
                        attribute.setName(ServiceAttribute.GLOBAL_SERVICE_NAME);
                        attribute.getType().setText("String");
                        clazz.paste(attribute, null, true);
                        }

                if (ClientConstant.XML_DESCRIPTION.equals(getPropertyValue(ClientProperty.SERVICE_DESCRIPTION_TYPE)) &&
                    Boolean.TRUE.equals(getPropertyValue(ClientProperty.PARAMETRIZE_XMLFILE_NAME))) {
                        SciAttribute attribute = getSciFactory().newAttribute();
                        attribute.setName(ServiceAttribute.GLOBAL_XMLFILE);
                        attribute.getType().setText("String");
                        clazz.paste(attribute, null, true);
                        }

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

        public boolean canGenerate() {
                return true;
                }

        }


