class TransMetaConverter {

  public static Transformation convert( TransMeta transMeta ) {
    final org.pentaho.di.engine.model.Transformation transformation =
      new org.pentaho.di.engine.model.Transformation( createTransformationId( transMeta ) );
    try {
      TransMeta copyTransMeta = (TransMeta) transMeta.realClone( false );

      cleanupDisabledHops( copyTransMeta );

      // Turn off lazy conversion for AEL for now
      disableLazyConversion( copyTransMeta );

      resolveStepMetaResources( copyTransMeta );

      copyTransMeta.getSteps().forEach( createOperation( transformation ) );
      findHops( copyTransMeta, hop -> true ).forEach( createHop( transformation ) );

      transformation.setConfig( TRANS_META_CONF_KEY, copyTransMeta.getXML() );
      transformation.setConfig( TRANS_META_NAME_CONF_KEY,
        Optional.ofNullable( transMeta.getName() ).orElse( TRANS_DEFAULT_NAME ) );
      Map<String, Transformation> subTransformations = copyTransMeta.getResourceDependencies().stream()
        .flatMap( resourceReference -> resourceReference.getEntries().stream() )
        .filter( entry -> ResourceEntry.ResourceType.ACTIONFILE.equals( entry.getResourcetype() ) )
        .collect( toMap( ResourceEntry::getResource, entry -> {
          try {
            Repository repository = copyTransMeta.getRepository();
            if ( repository != null ) {
              Path path = Paths.get( entry.getResource() );
              RepositoryDirectoryInterface directory =
                repository.findDirectory( path.getParent().toString().replace( File.separator, "/" ) );
              return convert(
                repository.loadTransformation( path.getFileName().toString(), directory, null, true, null ) );
            }
            return convert( new TransMeta( entry.getResource(), copyTransMeta.getParentVariableSpace() ) );
          } catch ( KettleException e ) {
            throw new RuntimeException( e );
          }
        } ) );
      transformation.setConfig( SUB_TRANSFORMATIONS_KEY, (Serializable) subTransformations );
    } catch ( KettleException e ) {
      Throwables.propagate( e );
    }
    return transformation;
  }

}