001/*
002 * Copyright c 2018 Rusi Popov, MDA Tools.net All rights reserved.
003 *
004 * This program and the accompanying materials are made available under the terms of the
005 * Eclipse Public License v2.0 which accompanies this distribution, and is available at
006 * http://www.eclipse.org/legal/epl-v20.html
007 */
008package net.mdatools.modelant.core.operation.element;
009
010import java.util.ArrayList;
011import java.util.Collection;
012import java.util.Iterator;
013import java.util.List;
014
015import javax.jmi.model.MofClass;
016import javax.jmi.model.Reference;
017import javax.jmi.reflect.RefObject;
018
019import net.mdatools.modelant.core.api.Function;
020
021/**
022 * Retrieve all associations of the model element, either defined in it or inherited
023 * @author Rusi Popov (popovr@mdatools.net)
024 */
025public class RetrieveAssociations implements Function<RefObject, Collection<String>> {
026
027
028  /**
029   * Retrieve the names of all associations in the METAMODEL that
030   * the model element <b>element</b> has, i.e. these are the names of the
031   * MOF References of all associations in MOF the class of the <b> element</b>
032   * is described with.
033   * NOTE: These are NOT model associations!
034   * @param element is non-null object from the first model
035   * @return a non-null list of names of attributes
036   */
037  public Collection<String> execute(RefObject element) {
038    List<String> result;
039    List<MofClass> metaClasses;
040    MofClass metaClass;
041
042    Iterator featuresIterator;
043    Iterator<MofClass> superclassIterator;
044    Object contained;
045
046    result = new ArrayList<String>();
047    metaClass = (MofClass) element.refMetaObject();
048
049    // check all inherited attributes
050    metaClasses = new ArrayList<MofClass>();
051    metaClasses.add( metaClass );
052    metaClasses.addAll( metaClass.allSupertypes() );
053
054    superclassIterator = metaClasses.iterator();
055    while ( superclassIterator.hasNext() ) {
056      metaClass = superclassIterator.next();
057
058      featuresIterator = metaClass.getContents().iterator();
059      while ( featuresIterator.hasNext() ) {
060        contained = featuresIterator.next();
061
062        if ( contained instanceof Reference ) {
063          result.add( ((Reference) contained).getName() );
064        }
065      }
066    }
067    return result;
068  }
069}