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.List;
012
013import javax.jmi.reflect.RefPackage;
014import javax.jmi.reflect.RefStruct;
015
016import net.mdatools.modelant.core.api.Function;
017import net.mdatools.modelant.core.util.Navigator;
018
019/**
020 * Instantiate a model Structure.
021 * @author Rusi Popov (popovr@mdatools.net)
022 */
023public class CreateStruct implements Function<RefPackage, RefStruct> {
024
025  /**
026   * Path in the metamodel to the metapackage whose instances (in the model/extent) are to be
027   * processed. Format of metapackage attribute: { <package>::} <meta class>
028   */
029  private final String metapackage;
030
031  /**
032   * The Struct type name within the metapackage
033   */
034  private final String type;
035
036  /**
037   * Collected values of the structure's fields
038   */
039  private final List parameters = new ArrayList();
040
041  public CreateStruct(String metapackage, String type, List<String> parameters){
042    if ( metapackage == null ) {
043      throw new IllegalArgumentException( "Epected a non-null metapackage");
044    }
045    if ( type == null || type.trim().length() == 0 ) { // no extent provided
046      throw new IllegalArgumentException( "Eoected a non-empty struct type");
047    }
048    this.metapackage = metapackage;
049    this.type = type;
050    this.parameters.addAll( parameters );
051  }
052
053  /**
054   * The task's execution method. This method filters the model classes associated with metapackage and
055   * executes the nested &lt;template&gt; tasks for the filtered classes collection. This
056   * methods invokes the internal formatting tasks with the metapackage itself.
057   */
058  public RefStruct execute(RefPackage sourceExtent) throws IllegalArgumentException {
059    RefStruct result;
060    RefPackage refPackage;
061
062    if ( sourceExtent == null ) {
063      throw new IllegalArgumentException("Expected non-null extent");
064    }
065
066    refPackage = Navigator.getMetaPackage( sourceExtent, metapackage ); // non-null
067
068    result = refPackage.refCreateStruct( type, parameters );
069
070    return result;
071  }
072}