/*----------------------------------------------------------------------------
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.sci.enum.SciClassEnumeration;
import com.togethersoft.openapi.sci.enum.SciInheritanceEnumeration;

import com.togethersoft.openapi.rwi.RwiModelAccess;
import com.togethersoft.openapi.rwi.RwiElement;
import com.togethersoft.openapi.rwi.RwiDiagram;

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

import com.togethersoft.modules.patterns.JAVA.CLASS.HP_E_Speak.Service_Provider.CompositeSciTemplate;
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;

public class ServiceClassTemplate extends CompositeSciTemplate {

    private static final String DEFAULT_ESLIB_IMPORT       = "net.espeak.jesi.*";
    private static final String DEFAULT_ESEXCEPTION_IMPORT = "net.espeak.infra.cci.exception.*";
    private static final String DEFAULT_CLIENT_CLASS_NAME  = "ESClient";
    
    public ServiceClassTemplate(PropertyMap properties) {
        super(properties);
    
        addTemplate(new ServiceMainMethodTemplate(properties));
        addTemplate(new ServiceConstrTemplate(properties));
    
        //addTemplate(new ClientStartMethodTemplate(properties));
        addTemplate(new ServiceRegisterOpTemplate(properties));
    
        addTemplate(new ServiceConnGetterTemplate(properties));
        addTemplate(new ServiceExceptionTemplate(properties));
    }

    public boolean prepare(SciObject target) {
        /*
         * Value for import statement.
         */
    
        setPropertyValue(ClientProperty.ESLIB_IMPORT, DEFAULT_ESLIB_IMPORT);
        //setPropertyValue(ClientProperty.CUSTOM_CONSTRAINT, null);
        return super.prepare(target);
    }

        public SciObject generate(SciObject target) {

                SciPackage parent = (SciPackage) target;

                /*
                 * Generate class.
                 */
                SciClass clazz = getSciFactory().newClass();
                clazz.setName(getStringPropertyValue(ClientProperty.SERVICE_CLASS_NAME));
                clazz = (SciClass) parent.paste(clazz, null, true);

                /*
                 * Add implementation attribute.
                 *
                 */
                try {
                        SciAttribute attribute = (SciAttribute) getSciGenericFactory().newMember(
                                                "private " + getStringPropertyValue(ClientProperty.SERVICE_IMPLEMENTATION) + " " +
                                                ServiceAttribute.IMPLEMENTATION + " = new "  + getStringPropertyValue(ClientProperty.SERVICE_IMPLEMENTATION) + "();\n", clazz);

                        clazz.paste(attribute, null, true);
                        }
                catch(SciGenericFactoryException sciGFException) {}

                /*
                 * Add import statements.
                 */
                SciJavaImport javaImport = getSciFactory().newJavaImport();
                javaImport.setImportText(getStringPropertyValue(ClientProperty.ESLIB_IMPORT));
                javaImport = (SciJavaImport) clazz.getContainingFile().getJavaImportList().paste(javaImport, null, true);

                SciJavaImport exImport = getSciFactory().newJavaImport();
                exImport.setImportText(DEFAULT_ESEXCEPTION_IMPORT);
                clazz.getContainingFile().getJavaImportList().paste(exImport, javaImport, false);

                super.generate(clazz);

                getSciModel().formatFile(clazz.getContainingFile());
                if (Boolean.TRUE.equals(getPropertyValue(ClientProperty.ADD_SERVICE_SHORTCUT))) {
                        SciClass   sciESService = getSciModel().findClass(getLanguage(), getStringPropertyValue(ClientProperty.SERVICE_INTERFACE_NAME));

                        if (sciESService != null) {

                                RwiElement rwiESService = RwiModelAccess.getModel().findElement(sciESService.getUniqueName());
                                RwiElement rwiClient    = RwiModelAccess.getModel().findElement(clazz.getUniqueName());
                                if ((rwiESService == null) || (rwiClient == null)) {
                                        return clazz;
                                        }

                                RwiDiagram rwiDiagram   = RwiModelAccess.getModel().findDiagramFor(rwiClient);
                                if (rwiDiagram != null) {
                                        rwiDiagram.addReference(rwiESService);
                                        }
                                }
                        }

                clazz.getTagList().setTagValue("stereotype", ClientConstant.STEREOTYPE);
                return clazz;
                }

        public boolean canGenerate() {

                if (getPropertyValue(ClientProperty.SERVICE_CLASS_NAME) == null) {
                        setErrorMessage("Specify valid ES Service Provider Class name.");
                        return false;
                        }

                return super.canGenerate();
                }
}


