/*-------------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft Corporation. Patents pending. All rights reserved.
-------------------------------------------------------------------------------*/

package com.togethersoft.modules.patterns.UNIVERSAL.CLASS.Coad_Classes.MomentInterval;

import java.util.Vector;

import com.togethersoft.openapi.sci.SciClass;
import com.togethersoft.openapi.sci.SciAttribute;
import com.togethersoft.openapi.sci.SciElement;
import com.togethersoft.openapi.sci.SciProperty;

import com.togethersoft.openapi.sci.pattern.SciPattern;
import com.togethersoft.openapi.sci.pattern.SciPatternUtil;
import com.togethersoft.openapi.sci.pattern.SciPatternCategory;
import com.togethersoft.openapi.sci.pattern.SciPatternProperty;
import com.togethersoft.openapi.sci.pattern.InspectorPatternUI;

import com.togethersoft.openapi.ide.message.IdeMessageManagerAccess;
import com.togethersoft.openapi.ide.message.IdeMessageType;

import com.togethersoft.modules.patterns.UNIVERSAL.CLASS.ClassPatternBase;
import com.togethersoft.modules.patterns.UNIVERSAL.PatternBaseException;

import com.togethersoft.openapi.ide.util.UIComponentService;
import com.togethersoft.openapi.ide.inspector.IdeInspector;
import com.togethersoft.openapi.ide.inspector.IdeInspectorProperty;
import com.togethersoft.openapi.ide.inspector.IdeInspectorComponent;
import com.togethersoft.openapi.ide.inspector.IdeInspectorPropertySetComponent;
import com.togethersoft.openapi.ide.inspector.util.table.PropertyTableComponentUI;
import com.togethersoft.openapi.ide.inspector.util.table.PropertyLineSetComponentUI;
import com.togethersoft.openapi.ide.inspector.util.property.PropertyMapInspectorProperty;

/**
 * MomentInterval from ArchetypesInColor
 * @author TogetherSoft
 */
public class Pattern extends ClassPatternBase implements SciPattern {

    private static final String PATTERN_NAME = "MomentInterval from ArchetypesInColor";

    public Pattern() {
        setPatternDisplayName(PATTERN_NAME);
    }

    protected void finalize() throws Throwable {
        super.finalize();
        removeAllListenedInspectorProperties();
        myMIClass = null;
        myMIDetailClass = null;
        myIMakeMIClass = null;
    }

    public boolean prepare() {
        try {
            super.prepare();
            notImplementedForIDL();

            SciElement testElement = getElement();
            if (testElement instanceof SciClass) {
                setPropertyValue(MOMENT_INTERVAL_NAME, testElement.getName());
                myMIClass = (SciClass)testElement;
            }
            else {
                setPropertyValue(MOMENT_INTERVAL_NAME, MOMENT_INTERVAL);
                myMIClass = null;
            }

            myMIDetailClass = null;
            myIMakeMIClass = null;

            setPropertyValue(MI_DETAIL_NAME, MI_DETAIL);
            setPropertyValue(MAKE_MOMENT_INTERVAL_INTERFACE_NAME, MAKE_MOMENT_INTERVAL_INTERFACE);

            initializeInspector();
            assignActivityResponse();
            return true;
        }
        catch(PatternBaseException e) {
            return false;
        }
    }

    public boolean canApply() {
        try {
            String name = classNameShouldBeCorrect(MOMENT_INTERVAL_NAME);
            String name2 = classNameShouldBeCorrect(MI_DETAIL_NAME);
            String name3 = classNameShouldBeCorrect(MAKE_MOMENT_INTERVAL_INTERFACE_NAME);
            if (name.equals(name2) || name2.equals(name3) || name.equals(name3)) {
                throw new PatternBaseException("Names should be different.");
            }
            if (myMIClass != null && !myMIClass.canSetName(name)) {
                throw new PatternBaseException("Cannot rename class to " + name);
            }
            return true;
        }
        catch(PatternBaseException e) {
            return false;
        }
    }

    public void apply() {
        if (!canApply()) {
            return;
        }
        super.apply();

/* Get or create Name. */

        String name = getStringPropertyValue(MOMENT_INTERVAL_NAME);
        String name2 = getStringPropertyValue(MI_DETAIL_NAME);
        String name3 = getStringPropertyValue(MAKE_MOMENT_INTERVAL_INTERFACE_NAME);
        if (myMIClass != null) {
            myMIClass.setName(name);
            if (!myMIClass.getName().equals(name)) {
                IdeMessageManagerAccess.printMessage(IdeMessageType.ERROR, "Couldn't rename class.");
            }
        }
        else {
            myMIClass = getClassByName(name, false);
        }
        myMIDetailClass = getClassByName(name2, false);
        myIMakeMIClass = getClassByName(name3, true);

  /* MomentInterval */

        myMIClass.getTagList().setTagValue(STEREOTYPE, STYPE_MOMENT_INTERVAL);

        SciAttribute attribute = getFactory().newAttribute();
        ((SciAttribute)myMIClass.paste(attribute, null, false)).setName("number");
        ((SciAttribute)myMIClass.paste(attribute, null, false)).setName("dateOrDateTimeOrInterval");
        ((SciAttribute)myMIClass.paste(attribute, null, false)).setName("priority");
        ((SciAttribute)myMIClass.paste(attribute, null, false)).setName("total");
        ((SciAttribute)myMIClass.paste(attribute, null, false)).setName("status");

        pasteNewOperation(myMIClass, "makeMomentInterval", INT);
        pasteNewOperation(myMIClass, "addDetail");
        pasteNewOperation(myMIClass, "calcTotal");
        pasteNewOperation(myMIClass, "recalTotal");
        pasteNewOperation(myMIClass, "complete");
        pasteNewOperation(myMIClass, "cancel");
        pasteNewOperation(myMIClass, "mi_generateNext");
        pasteNewOperation(myMIClass, "mi_assessWRTPrior");
        pasteNewOperation(myMIClass, "mi_assessWRTNext");
        pasteNewOperation(myMIClass, "mi_comparePlanVsActual");

        pasteNewOperation(myMIClass, "listMIs", INT).setProperty(SciProperty.STATIC, true);
        pasteNewOperation(myMIClass, "assessAcrossMIs", INT).setProperty(SciProperty.STATIC, true);

 /* myMIDetailClass */

        myMIDetailClass.getTagList().setTagValue(STEREOTYPE, STYPE_MI_DETAIL);
        pasteNewOperation(myMIDetailClass, "calcTotal", INT);
        pasteNewAttribute(myMIDetailClass, "qty");

 /* IMakeMomentInterval */

        myIMakeMIClass.getTagList().setTagValue(STEREOTYPE, STYPE_PLUG_IN_POINT);
        pasteNewOperation(myIMakeMIClass, "makeMomentInterval");

 /* Create links */
        SciClass    destClass = findClassByName(name);
        String      attrName = PREFIX_LINK + name;
        SciElement  MILink1 = null;
        MILink1 = createLinkByPattern(DEFAULT_ASSOCIATION, myMIClass, destClass, attrName, false);
        if (MILink1 == null) {
            return;
        }
        MILink1.getTagList().setTagValue(CARDINALITY_CLIENT, CARD_ZERO_ONE);
        MILink1.getTagList().setTagValue(ROLE_CLIENT, ROLE_PLAN);
        MILink1.getTagList().setTagValue(CARDINALITY_SUPPLIER, CARD_ZERO_MANY);
        MILink1.getTagList().setTagValue(ROLE_SUPPLIER, ROLE_ACTUAL);

        SciClass destClass2 = findClassByName(name2);
        String attrName2 = PREFIX_LINK + name2;
        SciAttribute MILink2 = null;

        boolean defaultPattern = true;
        MILink2 = (SciAttribute) createLinkByPattern("UnifiedAssociationAsVector", myMIClass, destClass2, attrName2, defaultPattern);
        if (MILink2 == null) {
            return;
        }
        MILink2.getTagList().setTagValue("shapeType", "AggregationLink");
        MILink2.getTagList().setTagValue("associates", "<b>" + name2 + "</b>");
        MILink2.getTagList().setTagValue(CARDINALITY_SUPPLIER, CARD_ONE_MANY);
        MILink2.getTagList().setTagValue(CARDINALITY_CLIENT, CARD_ONE);

        SciClass    destClass3 = findClassByName(name3);
        String      attrName3 = PREFIX_LINK + name3;
        SciElement  MILink3 = null;
        MILink3 = createLinkByPattern(DEFAULT_ASSOCIATION, myMIClass, destClass3, attrName3, false);
        if (MILink3 == null) {
            return;
        }
        MILink3.getTagList().setTagValue(CARDINALITY_SUPPLIER, CARD_ONE);

        removeAllListenedInspectorProperties();
    }

    protected void initializeInspector() {
        String pageName1 = "Pattern properties";

        Vector inspectorPropertiesVector = new Vector();

        IdeInspectorPropertySetComponent propertyPage = new IdeInspectorPropertySetComponent(null);

        propertyPage.setName(pageName1);
        propertyPage.setService(UIComponentService.class, new PropertyTableComponentUI(propertyPage));
        getIdeInspector().addComponent(propertyPage, null);

        IdeInspectorProperty property = null;

        // Create MomentInterval property
        property = new PropertyMapInspectorProperty(getProperties(), MOMENT_INTERVAL_NAME, String.class);
        property.setName(MOMENT_INTERVAL_NAME);
        propertyPage.addProperty(property, null);
        inspectorPropertiesVector.addElement(MOMENT_INTERVAL_NAME);

        // Create MIDetail property
        property = new PropertyMapInspectorProperty(getProperties(), MI_DETAIL_NAME, String.class);
        property.setName(MI_DETAIL_NAME);
        propertyPage.addProperty(property, null);
        inspectorPropertiesVector.addElement(MI_DETAIL_NAME);

        // Create MakeMomentInterval interface property
        property = new PropertyMapInspectorProperty(getProperties(), MAKE_MOMENT_INTERVAL_INTERFACE_NAME, String.class);
        property.setName(MAKE_MOMENT_INTERVAL_INTERFACE_NAME);
        propertyPage.addProperty(property, null);
        inspectorPropertiesVector.addElement(MAKE_MOMENT_INTERVAL_INTERFACE_NAME);

        InspectorPatternUI patternUI = new InspectorPatternUI(getIdeInspector());
        setPropertyValue(SciPatternProperty.PATTERN_UI, patternUI);

        assignListenedInspectorProperties(inspectorPropertiesVector);
    }

    protected void assignActivityResponse() {
    }

    private SciClass myMIClass;
    private SciClass myMIDetailClass;
    private SciClass myIMakeMIClass;

    private static final String MOMENT_INTERVAL                     = "MomentInterval";
    private static final String MOMENT_INTERVAL_NAME                = MOMENT_INTERVAL + " Name";
    private static final String MI_DETAIL                           = "MIDetail";
    private static final String MI_DETAIL_NAME                      = "MI_Detail Name";
    private static final String MAKE_MOMENT_INTERVAL_INTERFACE      = "IMakeMomentInterval";
    private static final String MAKE_MOMENT_INTERVAL_INTERFACE_NAME = "MakeMomentInterval Interface Name";
}
