.//====================================================================
.//
.// File:      $RCSfile: gen_uuid_convert_util.arc,v $
.// Version:   $Revision: 1.14 $
.// Modified:  $Date: 2013/01/10 23:34:58 $
.//
.// (c) Copyright 2005-2014 by Mentor Graphics Corp.  All rights reserved.
.//
.//====================================================================
.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
.invoke arc_env1 = GET_ENV_VAR( "IO_CORE_ARC_DIR" )
.assign io_core_archetypes = arc_env1.result
.if ( io_core_archetypes == "" )
  .print "\nERROR: Environment variable IO_CORE_ARC_DIR not set."
  .exit 100
.end if
.//
.include "${mc_archetypes}/arch_utils.inc"
.//
.assign class_name = "IDConversionUtil"
.//
.//This method returns only persistable children only
.//If all children all required in hierarchy, override this method by removing .if((child.ComponentRoot) or (child.WritePosition!=none))
.//This is because we are keeping two instances for some of the elements, to store them some where outside navigation hierarchy
.function get_children
    .param inst_ref eo
    .//
    .select many eos from instances of EO
    .assign attr_children = eos - eos
    .//
    .select one child related by eo->EO[R1.'is_parent_of']	
    .if(not_empty child)
        .if(child.WritePosition!="none")
        .assign attr_children = attr_children | child
        .end if
        .select one sibling related by child->EO[R2.'follows']
        .while(not_empty sibling)
           .if(sibling.WritePosition!="none")
            .assign attr_children = attr_children | sibling
           .end if 
            .select one sibling related by sibling->EO[R2.'follows']
        .end while
    .end if
.end function
.//
.function generate_chain
    .param inst_ref_set path
    .param string child_var
.//     
    .assign parent_picked = true
    .select any parent_element from instances of EO where ( "${selected.Id}" == "-1" )
    .assign child_element = parent_element
    .// 
    .for each element in path
        .if (first path)
            .assign parent_element = element
        .else
		    .select one parent_table related by parent_element->EI[R3]->T[R4]
		    .invoke parent_class_name = get_class_name(parent_element)
		    .invoke nav = get_nav_func_name(parent_table, element, "one")
${parent_class_name.body}.${nav.body}(\
		    .assign parent_element = element
        .end if
        .if (last path)
            .assign child_element = element
        .end if
    .end for
    .invoke child_class_name = get_class_name(child_element)
(${child_class_name.body})${child_var}\
    .for each element in path
        .if (not_first path)
)\
        .end if
    .end for   
.end function
.//
.//
.//
.function get_non_ref_id_col
  .param inst_ref eo
  .select one table related by eo->EI[R3]->T[R4]
  .invoke result = get_non_ref_id_col_from_table(table)
  .assign attr_col = result.col
.end function
.//
.//
.//
.function get_non_ref_id_col_from_table
  .param inst_ref table
  .select any attr_col from instances of C where (selected.tableName == "")
  .if(not_empty table)
    .select many col_set related by table->C[R5]
    .for each col in col_set
      .if((col.type == "unique_id") and ((col.isIdentifier) and ((not col.isReferential) or (col.isOptional))))
         .assign attr_col = col
         .break for
      .end if
    .end for
  .end if
.end function
.//
//========================================================================
//
// File: ${class_name}.java
//
// WARNING:      Do not edit this generated file
// Generated by: ${info.arch_file_name}
// Version:      $$Revision: 1.14 $$
//
// (c) Copyright 2005-2014 by Mentor Graphics Corp.  All rights reserved.
//
//========================================================================
// 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.
//======================================================================== 
package org.xtuml.bp.io.mdl;

import org.xtuml.bp.core.*;
import org.xtuml.bp.core.common.*;
import org.xtuml.bp.ui.canvas.*;
import java.util.*;
.// 
.select many eos from instances of EO
.assign id_eos = eos - eos
.for each eo in eos
  .// excluding diagrams and repeated model elements
  .invoke result = get_non_ref_id_col( eo )
  .if ((not_empty result.col) and ((eo.Card != "") and (eo.writePosition != "none")))
    .assign id_eos = id_eos | eo
  .end if
.end for

public class ${class_name} extends IDConvertor{

    protected final String createKey(NonRootModelElement modelElement){
.for each id_eo in id_eos
  .invoke cn = get_class_name(id_eo) 
  .if(first id_eos)
        if(modelElement instanceof ${cn.body}){
  .else
        }else if(modelElement instanceof ${cn.body}){
  .end if
  .invoke result = get_non_ref_id_col(id_eo)
  .invoke a = get_attribute_accessor(result.col)
  .assign col = result.col
            return "$Cr{id_eo.Name}.$Cr{col.Name}." + ((${cn.body})modelElement).${a.body}LongBased();
.end for
        }
        return null;        
    }

    public final UUID getId(NonRootModelElement modelElement){
.for each id_eo in id_eos
  .invoke cn = get_class_name(id_eo) 
  .if(first id_eos)
        if(modelElement instanceof ${cn.body}){
  .else
        }else if(modelElement instanceof ${cn.body}){
  .end if
  .invoke result = get_non_ref_id_col(id_eo)
  .invoke a = get_attribute_accessor(result.col)
            return ((${cn.body})modelElement).${a.body}();
.end for
        }
        return null;        
    }
    
    public final void setId(NonRootModelElement modelElement, UUID id){
.for each id_eo in id_eos
  .invoke cn = get_class_name(id_eo) 
  .if(first id_eos)
        if(modelElement instanceof ${cn.body}){
  .else
        }else if(modelElement instanceof ${cn.body}){
  .end if
  .invoke result = get_non_ref_id_col(id_eo)
  .assign col = result.col
  .if (not col.isOptional)
            ((${cn.body})modelElement).set$cr{col.Name}(id);
  .else
            // identifier is a referential.  We are setting it because
            // in this case there is no RTO.
            ((${cn.body})modelElement).set$cr{col.Name}(id);
  .end if
.end for
        }
        modelElement.setUniqueId(id);
    }
    
    private static ModelRoot getGraphicsModelRoot(NonRootModelElement element){
        return Ooaofgraphics.getInstance(element.getModelRoot().getId());
    }
        
}
.//
.emit to file "src/org/xtuml/bp/io/mdl/${class_name}.java"
.//
.//
