001package io.ebean.querybean.generator; 002 003import java.io.IOException; 004import java.io.Writer; 005 006public class KotlinLangAdapter implements LangAdapter { 007 008 @Override 009 public void beginClass(Writer writer, String shortName) throws IOException { 010 //class QCountry : TQRootBean<Country, QCountry> { 011 writer.append("class ").append("Q").append(shortName) 012 .append(" : TQRootBean<").append(shortName) 013 .append(", Q").append(shortName).append("> {").append(NEWLINE); 014 } 015 016 @Override 017 public void beginAssocClass(Writer writer, String shortName, String origShortName) throws IOException { 018 writer.append("class ").append("Q").append(shortName); 019 writer.append("<R>(name: String, root: R) : TQAssocBean<").append(origShortName).append(",R>(name, root) {").append(NEWLINE); 020 } 021 022 @Override 023 public void alias(Writer writer, String shortName) throws IOException { 024 025 writer.append(" companion object {").append(NEWLINE); 026 writer.append(" /**").append(NEWLINE); 027 writer.append(" * shared 'Alias' instance used to provide").append(NEWLINE); 028 writer.append(" * properties to select and fetch clauses").append(NEWLINE); 029 writer.append(" */").append(NEWLINE); 030 writer.append(" val _alias = Q").append(shortName).append("(true)").append(NEWLINE); 031 writer.append(" }").append(NEWLINE).append(NEWLINE); 032 } 033 034 @Override 035 public void assocBeanConstructor(Writer writer, String shortName) throws IOException { 036 // not required for kotlin as part of type definition 037 } 038 039 @Override 040 public void fetch(Writer writer, String origShortName) throws IOException { 041 042 writeAssocBeanFetch(writer, origShortName, "", "Eagerly fetch this association loading the specified properties."); 043 writeAssocBeanFetch(writer, origShortName, "Query", "Eagerly fetch this association using a 'query join' loading the specified properties."); 044 writeAssocBeanFetch(writer, origShortName, "Lazy", "Use lazy loading for this association loading the specified properties."); 045 } 046 047 private void writeAssocBeanFetch(Writer writer, String origShortName, String fetchType, String comment) throws IOException { 048 049// fun fetch(vararg properties: TQProperty<QContact>): R { 050// return fetchProperties(*properties) 051// } 052 053 writer.append(" /**").append(NEWLINE); 054 writer.append(" * ").append(comment).append(NEWLINE); 055 writer.append(" */").append(NEWLINE); 056 writer.append(" fun fetch").append(fetchType).append("(vararg properties: TQProperty<Q").append(origShortName).append(">) : R {").append(NEWLINE); 057 writer.append(" return fetch").append(fetchType).append("Properties(*properties)").append(NEWLINE); 058 writer.append(" }").append(NEWLINE); 059 writer.append(NEWLINE); 060 } 061 062 063 @Override 064 public void rootBeanConstructor(Writer writer, String shortName) throws IOException { 065 066 writer.append(NEWLINE); 067 writer.append(" /**").append(NEWLINE); 068 writer.append(" * Construct with a given EbeanServer.").append(NEWLINE); 069 writer.append(" */").append(NEWLINE); 070 writer.append(" constructor(server: EbeanServer) : super(").append(shortName).append("::class.java, server)"); 071 writer.append(NEWLINE); 072 writer.append(NEWLINE); 073 074 writer.append(" /**").append(NEWLINE); 075 writer.append(" * Construct using the default EbeanServer.").append(NEWLINE); 076 writer.append(" */").append(NEWLINE); 077 writer.append(" constructor() : super(").append(shortName).append("::class.java)"); 078 writer.append(NEWLINE); 079 080 writer.append(NEWLINE); 081 writer.append(" /**").append(NEWLINE); 082 writer.append(" * Construct for Alias.").append(NEWLINE); 083 writer.append(" */").append(NEWLINE); 084 writer.append(" private constructor(dummy: Boolean) : super(dummy)").append(NEWLINE); 085 } 086 087 @Override 088 public void fieldDefn(Writer writer, String propertyName, String typeDefn) throws IOException { 089 090 writer.append(" lateinit var "); 091 writer.append(propertyName).append(": "); 092 if (typeDefn.endsWith(",Integer>")) { 093 typeDefn = typeDefn.replace(",Integer>", ",Int>"); 094 } 095 writer.append(typeDefn); 096 } 097 098}