All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ulting.orcas.orcas-domainextension-extenions.8.6.4.source-code.DomainExtensionHelper Maven / Gradle / Ivy

Go to download

orcas custom extensions (contains only support classes and an empty-extension). If custom-extensions are provided, there will be a custom artifact.

The newest version!
package de.opitzconsulting.orcas.extensions;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.EcoreUtil2;

import de.opitzconsulting.orcasDsl.ColumnDomain;
import de.opitzconsulting.orcasDsl.Domain;
import de.opitzconsulting.orcasDsl.GenNameRule;
import de.opitzconsulting.orcasDsl.GenNameRulePart;
import de.opitzconsulting.orcasDsl.Model;
import de.opitzconsulting.orcasDsl.ModelElement;

public class DomainExtensionHelper extends OrcasBaseExtensionWithParameter
{
  public static int getIntNullValue()
  {
    return -1;
  }

  private static boolean isUnset( int pValue )
  {
    return pValue == 0 || pValue == getIntNullValue();
  }

  public static boolean isIntEqual( int pValue1, int pValue2 )
  {
    if( isUnset( pValue1 ) )
    {
      return isUnset( pValue2 );
    }

    return pValue1 == pValue2;
  }

  public static Domain getDomain( String pDomainName, Model pModel )
  {
    for( ModelElement lModelElement : new ArrayList( pModel.getModel_elements() ) )
    {
      if( lModelElement instanceof Domain )
      {
        if( ((Domain) lModelElement).getName().equals( pDomainName ) )
        {
          return (Domain) lModelElement;
        }
      }
    }

    throw new RuntimeException( "domain not found: " + pDomainName );
  }

  public static ColumnDomain getColumnDomain( String pColumnDomainName, Model pModel )
  {
    for( ModelElement lModelElement : new ArrayList( pModel.getModel_elements() ) )
    {
      if( lModelElement instanceof ColumnDomain )
      {
        if( ((ColumnDomain) lModelElement).getName().equals( pColumnDomainName ) )
        {
          return (ColumnDomain) lModelElement;
        }
      }
    }

    throw new RuntimeException( "column domain not found: " + pColumnDomainName );
  }

  private static class TokenBuilder
  {
    private String returnValue = "";
    private boolean removeNext = false;
    private String removeToken = "";

    void handleNextToken( String pToken, String pRegexpReplace, String pRegexpReplacement )
    {
      handleNextToken( pRegexpReplace == null ? pToken : pToken.replaceAll( pRegexpReplace, pRegexpReplacement == null ? "" : pRegexpReplacement ) );
    }

    void handleNextToken( String pToken )
    {
      if( pToken != null )
      {
        if( removeNext )
        {
          removeToken = removeToken + pToken;
          removeNext = false;
        }
        else
        {
          returnValue = returnValue + pToken;

          if( removeToken != null )
          {
            if( returnValue.indexOf( removeToken ) < 0 )
            {
              throw new RuntimeException( "token not found " + returnValue + " " + removeToken );
            }

            returnValue = returnValue.replace( removeToken, "" );
            removeToken = "";
          }
        }
      }
    }
  }

  private static String getGeneratedName( List pGenNameRuleList, String pColumnName, String pColumnDomainName, String pTableName, String pAlias )
  {
    TokenBuilder lTokenBuilder = new TokenBuilder();

    for( GenNameRule lGenNameRule : pGenNameRuleList )
    {
      lTokenBuilder.handleNextToken( lGenNameRule.getConstant_name() );

      if( lGenNameRule.getConstant_part() == GenNameRulePart.COLUMN_NAME )
      {
        if( pColumnName == null )
        {
          throw new RuntimeException( "p_column_name invalid" + pAlias + pTableName );
        }
        lTokenBuilder.handleNextToken( pColumnName, lGenNameRule.getRegexp(), lGenNameRule.getReplace() );
      }
      if( lGenNameRule.getConstant_part() == GenNameRulePart.COLUMN_DOMAIN_NAME )
      {
        if( pColumnDomainName == null )
        {
          throw new RuntimeException( "p_column_domain_name invalid" + pColumnName + pTableName );
        }
        lTokenBuilder.handleNextToken( pColumnDomainName, lGenNameRule.getRegexp(), lGenNameRule.getReplace() );
      }
      if( lGenNameRule.getConstant_part() == GenNameRulePart.TABLE_NAME )
      {
        if( pTableName == null )
        {
          throw new RuntimeException( "p_table_name invalid" + pColumnName + pAlias );
        }
        lTokenBuilder.handleNextToken( pTableName, lGenNameRule.getRegexp(), lGenNameRule.getReplace() );
      }
      if( lGenNameRule.getConstant_part() == GenNameRulePart.ALIAS_NAME )
      {
        lTokenBuilder.handleNextToken( pAlias, lGenNameRule.getRegexp(), lGenNameRule.getReplace() );
      }
      if( lGenNameRule.getConstant_part() == GenNameRulePart.REMOVE_NEXT )
      {
        lTokenBuilder.removeNext = true;
      }
    }

    return lTokenBuilder.returnValue;
  }

  public static String getGeneratedNameTable( List pGenNameRuleList, String pTableName, String pAlias )
  {
    return getGeneratedName( pGenNameRuleList, null, null, pTableName, pAlias );
  }

  public static String getGeneratedNameColumn( List pGenNameRuleList, String pColumnName, String pTableName, String pAlias )
  {
    return getGeneratedName( pGenNameRuleList, pColumnName, null, pTableName, pAlias );
  }

  public static String getGeneratedNameColDomain( List pGenNameRuleList, String pColumnDomainName )
  {
    return getGeneratedName( pGenNameRuleList, null, pColumnDomainName, null, null );
  }

  @Override
  public Model transformModel( Model pModel )
  {
    return pModel;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy