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

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

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

import com.regnosys.rosetta.generator.java.types.JavaTypeTranslator
import com.regnosys.rosetta.generator.object.ExpandedAttribute
import com.regnosys.rosetta.rosetta.RosettaEnumeration
import com.regnosys.rosetta.rosetta.RosettaExternalSynonymSource
import com.regnosys.rosetta.translate.synonymmap.SynonymMapBuilder
import java.util.List
import java.util.Map
import javax.inject.Inject
import org.apache.log4j.Logger

import static extension com.regnosys.rosetta.generator.java.enums.EnumHelper.formatEnumName
import static extension com.regnosys.rosetta.translate.GeneratorUtil.*

class SynonymToEnumMapGenerator {
	
	static final Logger LOGGER = Logger.getLogger(SynonymToEnumMapGenerator);

	public static final String GENERATED_CLASS_NAME = "SynonymToEnumMapBuilderImpl"
	
	@Inject
	JavaTypeTranslator typeTranslator
	
	def GeneratedNameAndSource generate(String packageName, List params, List rosettaEnumerations) {
		// TODO can all synonym sources be used, or just the last?
		val synonymSources = params.map[sources].toSet
		val synonymSourcesNames = synonymSources.map[synonymSourceNames].flatten.toList
		
		LOGGER.info("Generating SynonymToEnumMap for synonym sources " + synonymSourcesNames.join(", "))
		
		val externalSynonyms = synonymSources.map[externalSynonymSources].flatten.toList
		val enumToSynonymToEnumValueMap = buildMap(synonymSourcesNames, externalSynonyms, rosettaEnumerations)
		val result = new GenerationResult(false, generateClass(packageName, enumToSynonymToEnumValueMap))
		return new GeneratedNameAndSource(packageName + "." + GENERATED_CLASS_NAME, result)
	}
	
	private def Map> buildMap(List synonymSourcesNames, List externalSynonyms, List rosettaEnumerations) {
		val synMapBuilder = new SynonymMapBuilder(synonymSourcesNames, externalSynonyms, typeTranslator)
		val enumToSynonymToEnumValueMap = newHashMap
		
		for (RosettaEnumeration e : rosettaEnumerations) {
			val synonymToEnumValueMap = newHashMap
			for (ExpandedAttribute enumVal : synMapBuilder.getAllEnumValue(e)) {
				for (String enumSyn : synMapBuilder.getEnumSynonyms(enumVal).map[values].flatten.map[name]) {
					synonymToEnumValueMap.put(enumSyn, enumVal)	
				}		
			}
			if (!synonymToEnumValueMap.empty) {
				enumToSynonymToEnumValueMap.put(e, synonymToEnumValueMap)
			}
		}
		enumToSynonymToEnumValueMap
	}
	
	private def generateClass(String packageName, Map> enumToSynonymToEnumValueMap) '''
		package «packageName»;
		
		import com.google.common.collect.ImmutableMap;
		import com.regnosys.rosetta.translate.SynonymToEnumMapBuilder;
		
		import java.util.Map;
		import java.util.Optional;
		
		public class «GENERATED_CLASS_NAME» implements SynonymToEnumMapBuilder {
			
			private final Map, Map>> synonymToEnumMap = ImmutableMap., Map>>builder()
				«FOR enumEntry : enumToSynonymToEnumValueMap.entrySet»
					«IF !enumEntry.value.empty»
						«val qualifiedEnumName = enumEntry.key.model.name + "." + enumEntry.key.name»
						.put(«qualifiedEnumName».class, ImmutableMap.>builder()
							«FOR synonymToEnumValueEntry : enumEntry.value.entrySet»
								.put("«synonymToEnumValueEntry.key»", «qualifiedEnumName».«synonymToEnumValueEntry.value.name.formatEnumName»)
							«ENDFOR»
							.build())
					«ENDIF»
				«ENDFOR»
				.build();
		
			@Override
			public Map, Map>> getMap() {
				return synonymToEnumMap;
			}
		}
	'''
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy