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

com.regnosys.rosetta.translate.SynonymToEnumMapGenerator Maven / Gradle / Ivy

There is a newer version: 11.25.1
Show newest version
package com.regnosys.rosetta.translate;

import com.google.common.collect.Iterables;
import com.regnosys.rosetta.generator.java.enums.EnumHelper;
import com.regnosys.rosetta.generator.java.types.JavaTypeTranslator;
import com.regnosys.rosetta.generator.object.ExpandedAttribute;
import com.regnosys.rosetta.generator.object.ExpandedSynonym;
import com.regnosys.rosetta.generator.object.ExpandedSynonymValue;
import com.regnosys.rosetta.rosetta.RosettaEnumeration;
import com.regnosys.rosetta.rosetta.RosettaExternalSynonymSource;
import com.regnosys.rosetta.rosetta.RosettaSynonymSource;
import com.regnosys.rosetta.translate.synonymmap.SynonymMapBuilder;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
import org.apache.log4j.Logger;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ListExtensions;

@SuppressWarnings("all")
public class SynonymToEnumMapGenerator {
  private static final Logger LOGGER = Logger.getLogger(SynonymToEnumMapGenerator.class);

  public static final String GENERATED_CLASS_NAME = "SynonymToEnumMapBuilderImpl";

  @Inject
  private JavaTypeTranslator typeTranslator;

  public GeneratedNameAndSource generate(final String packageName, final List params, final List rosettaEnumerations) {
    final Function1> _function = (GeneratorParams it) -> {
      return it.getSources();
    };
    final Set> synonymSources = IterableExtensions.>toSet(ListExtensions.>map(params, _function));
    final Function1, List> _function_1 = (Collection it) -> {
      return GeneratorUtil.synonymSourceNames(it);
    };
    final List synonymSourcesNames = IterableExtensions.toList(Iterables.concat(IterableExtensions., List>map(synonymSources, _function_1)));
    String _join = IterableExtensions.join(synonymSourcesNames, ", ");
    String _plus = ("Generating SynonymToEnumMap for synonym sources " + _join);
    SynonymToEnumMapGenerator.LOGGER.trace(_plus);
    final Function1, List> _function_2 = (Collection it) -> {
      return GeneratorUtil.externalSynonymSources(it);
    };
    final List externalSynonyms = IterableExtensions.toList(Iterables.concat(IterableExtensions., List>map(synonymSources, _function_2)));
    final Map> enumToSynonymToEnumValueMap = this.buildMap(synonymSourcesNames, externalSynonyms, rosettaEnumerations);
    CharSequence _generateClass = this.generateClass(packageName, enumToSynonymToEnumValueMap);
    final GenerationResult result = new GenerationResult(false, _generateClass);
    return new GeneratedNameAndSource(((packageName + ".") + SynonymToEnumMapGenerator.GENERATED_CLASS_NAME), result);
  }

  private Map> buildMap(final List synonymSourcesNames, final List externalSynonyms, final List rosettaEnumerations) {
    HashMap> _xblockexpression = null;
    {
      final SynonymMapBuilder synMapBuilder = new SynonymMapBuilder(synonymSourcesNames, externalSynonyms, this.typeTranslator);
      final HashMap> enumToSynonymToEnumValueMap = CollectionLiterals.>newHashMap();
      for (final RosettaEnumeration e : rosettaEnumerations) {
        {
          final HashMap synonymToEnumValueMap = CollectionLiterals.newHashMap();
          Collection _allEnumValue = synMapBuilder.getAllEnumValue(e);
          for (final ExpandedAttribute enumVal : _allEnumValue) {
            final Function1> _function = (ExpandedSynonym it) -> {
              return it.getValues();
            };
            final Function1 _function_1 = (ExpandedSynonymValue it) -> {
              return it.getName();
            };
            Iterable _map = IterableExtensions.map(Iterables.concat(ListExtensions.>map(synMapBuilder.getEnumSynonyms(enumVal), _function)), _function_1);
            for (final String enumSyn : _map) {
              synonymToEnumValueMap.put(enumSyn, enumVal);
            }
          }
          boolean _isEmpty = synonymToEnumValueMap.isEmpty();
          boolean _not = (!_isEmpty);
          if (_not) {
            enumToSynonymToEnumValueMap.put(e, synonymToEnumValueMap);
          }
        }
      }
      _xblockexpression = enumToSynonymToEnumValueMap;
    }
    return _xblockexpression;
  }

  private CharSequence generateClass(final String packageName, final Map> enumToSynonymToEnumValueMap) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package ");
    _builder.append(packageName);
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("import com.google.common.collect.ImmutableMap;");
    _builder.newLine();
    _builder.append("import com.regnosys.rosetta.translate.SynonymToEnumMapBuilder;");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import java.util.Map;");
    _builder.newLine();
    _builder.append("import java.util.Optional;");
    _builder.newLine();
    _builder.newLine();
    _builder.append("public class ");
    _builder.append(SynonymToEnumMapGenerator.GENERATED_CLASS_NAME);
    _builder.append(" implements SynonymToEnumMapBuilder {");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("private final Map, Map>> synonymToEnumMap = ImmutableMap., Map>>builder()");
    _builder.newLine();
    {
      Set>> _entrySet = enumToSynonymToEnumValueMap.entrySet();
      for(final Map.Entry> enumEntry : _entrySet) {
        {
          boolean _isEmpty = enumEntry.getValue().isEmpty();
          boolean _not = (!_isEmpty);
          if (_not) {
            _builder.append("\t\t");
            String _name = enumEntry.getKey().getModel().getName();
            String _plus = (_name + ".");
            String _name_1 = enumEntry.getKey().getName();
            final String qualifiedEnumName = (_plus + _name_1);
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append(".put(");
            _builder.append(qualifiedEnumName, "\t\t");
            _builder.append(".class, ImmutableMap.>builder()");
            _builder.newLineIfNotEmpty();
            {
              Set> _entrySet_1 = enumEntry.getValue().entrySet();
              for(final Map.Entry synonymToEnumValueEntry : _entrySet_1) {
                _builder.append("\t\t");
                _builder.append("\t");
                _builder.append(".put(\"");
                String _key = synonymToEnumValueEntry.getKey();
                _builder.append(_key, "\t\t\t");
                _builder.append("\", ");
                _builder.append(qualifiedEnumName, "\t\t\t");
                _builder.append(".");
                String _formatEnumName = EnumHelper.formatEnumName(synonymToEnumValueEntry.getValue().getName());
                _builder.append(_formatEnumName, "\t\t\t");
                _builder.append(")");
                _builder.newLineIfNotEmpty();
              }
            }
            _builder.append("\t\t");
            _builder.append("\t");
            _builder.append(".build())");
            _builder.newLine();
          }
        }
      }
    }
    _builder.append("\t\t");
    _builder.append(".build();");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("@Override");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public Map, Map>> getMap() {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("return synonymToEnumMap;");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    return _builder;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy