/*----------------------------------------------------------------------------
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.SciObject;
import  com.togethersoft.openapi.sci.SciElement;
import  com.togethersoft.openapi.sci.SciClass;
import  com.togethersoft.openapi.sci.SciPackage;
import  com.togethersoft.openapi.sci.SciInheritanceList;
import  com.togethersoft.openapi.sci.SciInheritance;
import  com.togethersoft.openapi.sci.SciProperty;
import  com.togethersoft.openapi.sci.SciContainer;
import  com.togethersoft.openapi.sci.SciJavaImportList;
import  com.togethersoft.openapi.sci.SciJavaImport;
import  com.togethersoft.openapi.sci.SciFile;
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.Client.CompositeSciTemplate;
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 ClientClassTemplate 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 ClientClassTemplate(PropertyMap properties) {
                super(properties);

                addTemplate(new ClientMainMethodTemplate(properties));
                addTemplate(new ClientConstrTemplate(properties));
                addTemplate(new ClientStartMethodTemplate(properties));
                addTemplate(new ClientSrvGetterTemplate(properties));
                addTemplate(new ClientConnGetterTemplate(properties));
                addTemplate(new ClientExceptionTemplate(properties));
                }

        public boolean prepare(SciObject target) {

                /*
                 * Provide default client class name.
                 */

                SciPackage parentPackage = null;

                if (target instanceof SciPackage) {
                        parentPackage = (SciPackage) target;
                        }

                if (target instanceof SciClass) {
                        parentPackage = ((SciClass) target).getContainingPackage();
                        }

                for(int i = 1; i >= 0; i++) {
                        // look for unique name.
                        setPropertyValue(ClientProperty.CLIENT_CLASS_NAME, DEFAULT_CLIENT_CLASS_NAME + i);

                        String qualifiedName = DEFAULT_CLIENT_CLASS_NAME + i;
                        if ((parentPackage != null) &&
                            (parentPackage.getQualifiedName() != null) &&
                            (parentPackage.getQualifiedName().trim().length() > 0)) {
                                qualifiedName = parentPackage.getQualifiedName() + '.' + qualifiedName;
                                }

                        if (getSciModel().findClass(getLanguage(), qualifiedName) == null) {
                                break;
                                }
                       }
                /*
                 * Value for import statement.
                 */
                setPropertyValue(ClientProperty.ESLIB_IMPORT, DEFAULT_ESLIB_IMPORT);
                return super.prepare(target);
                }

        public SciObject generate(SciObject target) {

                SciPackage parent = (SciPackage) target;

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

                /*
                 * 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.CLIENT_CLASS_NAME) == null) {
                        setErrorMessage("Specify valid ES Client Class name.");
                        return false;
                        }

                return super.canGenerate();
                }
        }


