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

com.regnosys.rosetta.translate.datamodel.json.schema.JavaFileWriter.xtend Maven / Gradle / Ivy

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

import com.regnosys.rosetta.translate.datamodel.Cardinality
import java.nio.file.Files
import java.nio.file.Path
import java.util.List
import org.apache.log4j.Logger
import org.eclipse.xtend.lib.annotations.Data
import com.rosetta.util.DottedPath

class JavaFileWriter {
	
	static val Logger LOGGER = Logger.getLogger(JavaFileWriter);
	
	def toJava(DottedPath mainPackage, DottedPath sharedPackage, PojoClass c) '''
		package «mainPackage»;
		
		import com.fasterxml.jackson.annotation.JsonProperty;
		«IF containsList(c.attributes)»
			import java.util.List;
		«ENDIF»
		«IF mainPackage !== sharedPackage»
			import «sharedPackage».*;
		«ENDIF»
		
		public class «c.name» {
			
			«FOR a : c.attributes SEPARATOR "\n"»
				@JsonProperty("«a.jsonProperty»")
				private «a.toAttributeClass.toFirstUpper» «a.name»;
			«ENDFOR»
			
			«FOR a : c.attributes SEPARATOR "\n"»
				public «a.toAttributeClass.toFirstUpper» get«a.name.toFirstUpper»() {
					return «a.name»;
				}
				
				public void set«a.name.toFirstUpper»(«a.toAttributeClass.toFirstUpper» «a.name») {
					this.«a.name» = «a.name»;
				}
			«ENDFOR»
		}
	'''

	def void writeFile(Path outputDir, DottedPath mainPackage, String fileName, String contents) {
		val path = outputDir.resolve(mainPackage.toPath)
		if (!Files.exists(path)) {
			Files.createDirectories(path);
		}
		val fullPath = path.resolve(fileName + ".java")
		LOGGER.debug("Writing class " + fullPath)
		Files.write(fullPath, contents.bytes);
	}
	
	private def String toAttributeClass(PojoAttribute a) {
		if (a.cardinality === Cardinality.MULTIPLE) {
			return '''List<«a.className»>'''
		} else {
			return a.className
		}
	}
	
	private def boolean containsList(List attributes) {
		return attributes.exists[it.cardinality === Cardinality.MULTIPLE]
	}
	
	@Data static class PojoClass {
		String name
		List attributes
	}
	
	@Data static class PojoAttribute {
		String jsonProperty
		String name
		String className
		Cardinality cardinality
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy