.//=======================================================================
.// Licensed under the Apache License, Version 2.0 (the "License"); you may not
.// use this file except in compliance with the License.  You may obtain a copy
.// of the License at
.//
.//      http://www.apache.org/licenses/LICENSE-2.0
.//
.// Unless required by applicable law or agreed to in writing, software
.// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
.// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the
.// License for the specific language governing permissions and limitations under
.// the License.
.//=======================================================================
.//
.//
.invoke arc_env = GET_ENV_VAR( "PTC_MC_ARC_DIR" )
.assign mc_archetypes = arc_env.result
.if ( mc_archetypes == "" )
  .print "\nERROR: Environment variable PTC_MC_ARC_DIR not set."
  .exit 100
.end if
.//
.include "${mc_archetypes}/arch_utils.inc"
.include "name_search_utils.inc"
.//
package org.xtuml.bp.core.util;
//========================================================================
//
// File: org.xtuml.bp.core/src/org/xtuml/bp/core/util/ActionLanguageDescriptionUtil.java
//
// WARNING:      Do not edit this generated file 
// Generated by: ${info.arch_file_name}
//
//========================================================================
// Licensed under the Apache License, Version 2.0 (the "License"); you may not 
// use this file except in compliance with the License.  You may obtain a copy 
// of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the 
// License for the specific language governing permissions and limitations under
// the License.
//======================================================================== 
//
import java.util.ArrayList;
import java.util.List;

import org.xtuml.bp.core.*;

/**
 *  This class holds a utility method to determine
 *  if a given class has a descrip attribute, 
 *  the necessary action language attribute, or
 *  a searchable name.
 */
public class ActionLanguageDescriptionUtil {
 	public static boolean hasDescriptionAttribute(Class<?> clazz) {
.select many objs from instances of O_OBJ
.for each obj in objs
  .select any attr related by obj->O_ATTR[R102] where (selected.Name == "Descrip")
  .if(not_empty attr)
    .select one dba related by attr->O_BATTR[R106]->O_DBATTR[R107]
    .if(empty dba)
      .invoke gn = get_class_name(obj)
      .assign className = gn.body
		if(clazz == ${className}.class) {
			return true;
		}
    .end if
  .end if
.end for
		return false;
	}

 	public static boolean hasActionLanguageAttribute(Class<?> clazz) {
.select many objs from instances of O_OBJ
.for each obj in objs
  .select any attr related by obj->O_ATTR[R102] where (selected.Name == "Action_Semantics")
  .if(not_empty attr)
    .invoke gn = get_class_name(obj)
    .assign className = gn.body
		if(clazz == ${className}.class) {
			return true;
		}
  .end if
.end for
		return false;
	}
	
    public static boolean hasSearchableName(Class<?> clazz) {
.select many objs from instances of O_OBJ
.for each obj in objs
  .// First, check to see if the object has a Name attribute.  We generally want
  .// to include these in the search, but there are a few we don't care about so we
  .// skip them.
  .select any attr related by obj->O_ATTR[R102] where (selected.Name == "Name")
  .if(not_empty attr)
    .invoke result = skip_class_with_name_attr(obj)
    .if (result.skip)
        .// skipping 
    .else
      .invoke gn = get_class_name(obj)
      .assign className = gn.body
        if(clazz == ${className}.class) {
            return true;
        }
    .end if
  .end if
  .// Next, check to see if the object has a get_name() operation.  We generally want
  .// to skip these in the search, but there are some we do care about so we
  .// include them.
  .select any otfr related by obj->O_TFR[R115] where (selected.Name == "get_name")
  .if(not_empty otfr)
    .invoke result = include_class_with_get_name_otfr(obj)
    .if (result.include)
      .invoke gn = get_class_name(obj)
      .assign className = gn.body
        if(clazz == ${className}.class) {
            return true;
        }
    .end if
  .end if
.end for
        return false;
    }
    
	public static String getDescriptionAttributeValue(Object element) {
.select many objs from instances of O_OBJ
.for each obj in objs
  .select any attr related by obj->O_ATTR[R102] where (selected.Name == "Descrip")
  .if(not_empty attr)
    .select one dba related by attr->O_BATTR[R106]->O_DBATTR[R107]
    .if(empty dba)
      .invoke gn = get_class_name(obj)
      .assign className = gn.body
		if(element instanceof ${className}) {
			return ((${className}) element).getDescrip();
		}
    .end if
  .end if
.end for	
		return "";
	}
	
	public static String getActionLanguageAttributeValue(Object element) {
.select many objs from instances of O_OBJ
.for each obj in objs
  .select any attr related by obj->O_ATTR[R102] where (selected.Name == "Action_Semantics")
  .if(not_empty attr)
    .invoke gn = get_class_name(obj)
    .assign className = gn.body
		if(element instanceof ${className}) {
			return ((${className}) element).getAction_semantics();
		}
  .end if
.end for
  		return "";
	}
	
    public static String getNameAttributeValue(Object element) {
.// See comments above in hasSearchableName() about the flow used here.
.select many objs from instances of O_OBJ
.for each obj in objs
  .select any attr related by obj->O_ATTR[R102] where (selected.Name == "Name")
  .if(not_empty attr)
    .invoke result = skip_class_with_name_attr(obj)
    .if (result.skip)
        .// skipping 
    .else
      .invoke gn = get_class_name(obj)
      .assign className = gn.body
        if(element instanceof ${className}) {
            return ((${className}) element).getName();
        }
    .end if
  .end if
  .select any otfr related by obj->O_TFR[R115] where (selected.Name == "get_name")
  .if(not_empty otfr)
    .invoke result = include_class_with_get_name_otfr(obj)
    .if (result.include)
      .invoke gn = get_class_name(obj)
      .assign className = gn.body
        if(element instanceof ${className}) {
            return ((${className}) element).Get_name();
        }
    .end if
  .end if
.end for
        return "";
    }

    public static String getKeyLetterValue(Object element) {
        if(element instanceof ModelClass_c) {
            return ((ModelClass_c) element).getKey_lett();
        }
        return "";
    }

    public static String[] getRolePhraseValues(Object element) {
        String[] rval = new String[]{"",""};
        if(element instanceof Association_c) {
            SimpleAssociation_c rsimp = SimpleAssociation_c.getOneR_SIMPOnR206((Association_c)element);
            if (rsimp != null) {
                ClassAsSimpleParticipant_c[] rparts = ClassAsSimpleParticipant_c.getManyR_PARTsOnR207(rsimp);
                if (rparts.length > 1) {
                    // We know we have two rparts
                    rval[0] = rparts[0].getTxt_phrs();
                    rval[1] = rparts[1].getTxt_phrs();
                } else {
                    // We know we have one rpart and one rform
                    rval[0] = rparts[0].getTxt_phrs();
                    ClassAsSimpleFormalizer_c rform = ClassAsSimpleFormalizer_c.getOneR_FORMOnR208(rsimp);
                    if (rform != null) {
                        rval[1] = rform.getTxt_phrs();
                    }
                }
                return rval;
            }

            LinkedAssociation_c rassoc = LinkedAssociation_c.getOneR_ASSOCOnR206((Association_c)element);
            if ( rassoc != null ) {
                ClassAsAssociatedOneSide_c raone = ClassAsAssociatedOneSide_c.getOneR_AONEOnR209(rassoc);
                if (raone != null) {
                    rval[0] = raone.getTxt_phrs();
                }
                ClassAsAssociatedOtherSide_c raoth = ClassAsAssociatedOtherSide_c.getOneR_AOTHOnR210(rassoc);
                if (raoth != null) {
                    rval[1] = raoth.getTxt_phrs();
                }
                return rval;
            }

            DerivedAssociation_c rcomp = DerivedAssociation_c.getOneR_COMPOnR206((Association_c)element);
            if (rcomp != null) {
                ClassAsDerivedOneSide_c rcone = ClassAsDerivedOneSide_c.getOneR_CONEOnR214(rcomp);
                if (rcone != null) {
                    rval[0] = rcone.getTxt_phrs();
                }
                ClassAsDerivedOtherSide_c rcoth = ClassAsDerivedOtherSide_c.getOneR_COTHOnR215(rcomp);
                if (rcoth != null) {
                    rval[1] = rcoth.getTxt_phrs();
                }
                return rval;
            }
        }
        return rval;
    }
    
	public static List<Class<?>> getClassesSupportingDescriptions() {
		List<Class<?>> classes = new ArrayList<Class<?>>();
.select many objs from instances of O_OBJ
.for each obj in objs
  .select any attr related by obj->O_ATTR[R102] where (selected.Name == "Descrip")
  .if(not_empty attr)
    .select one dba related by attr->O_BATTR[R106]->O_DBATTR[R107]
    .if(empty dba)
      .invoke gn = get_class_name(obj)
      .assign className = gn.body
		classes.add(${className}.class);
    .end if
  .end if
.end for
		return classes;
	}

	public static List<Class<?>> getClassesSupportingActionLanguage() {
		List<Class<?>> classes = new ArrayList<Class<?>>();
.select many objs from instances of O_OBJ
.for each obj in objs
  .select any attr related by obj->O_ATTR[R102] where (selected.Name == "Action_Semantics")
  .if(not_empty attr)
    .invoke gn = get_class_name(obj)
    .assign className = gn.body
		classes.add(${className}.class);
  .end if
.end for
		return classes;
	}
	
    public static List<Class<?>> getClassesSupportingName() {
        List<Class<?>> classes = new ArrayList<Class<?>>();
.// See comments above in hasSearchableName() about the flow used here.
.select many objs from instances of O_OBJ
.for each obj in objs
  .select any attr related by obj->O_ATTR[R102] where (selected.Name == "Name")
  .if(not_empty attr)
    .invoke result = skip_class_with_name_attr(obj)
    .if (result.skip)
        .// skipping 
    .else
      .invoke gn = get_class_name(obj)
      .assign className = gn.body
        classes.add(${className}.class);
    .end if
  .end if
  .select any otfr related by obj->O_TFR[R115] where (selected.Name == "get_name")
  .if(not_empty otfr)
    .invoke result = include_class_with_get_name_otfr(obj)
    .if (result.include)
      .invoke gn = get_class_name(obj)
      .assign className = gn.body
        classes.add(${className}.class);
    .end if
  .end if
.end for
        return classes;
    }
}
.//
.emit to file "src/org/xtuml/bp/core/util/ActionLanguageDescriptionUtil.java"
.//