Skip to content

Commit

Permalink
Generator: Fix #3 Map nullable collumn to optional int etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gikkman committed Jul 15, 2016
1 parent 7eab3d5 commit 0082bbd
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 87 deletions.
Expand Up @@ -35,6 +35,9 @@
import java.util.function.Supplier;

import static java.util.Objects.requireNonNull;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;

/**
* Constant implementations of the {@link Type} interface that can be used to
Expand Down Expand Up @@ -72,6 +75,9 @@ public enum DefaultType implements Type {
QUEUE(Queue.class),
STACK(Stack.class),
OPTIONAL(Optional.class),
OPTIONAL_INT(OptionalInt.class),
OPTIONAL_LONG(OptionalLong.class),
OPTIONAL_DOUBLE(OptionalDouble.class),
ENTRY(HashMap.Entry.class),
FUNCTION(Function.class),
PREDICATE(Predicate.class),
Expand Down
Expand Up @@ -36,7 +36,9 @@
import static com.speedment.common.codegen.internal.model.constant.DefaultType.*;
import static com.speedment.common.codegen.internal.util.Formatting.nl;
import static com.speedment.common.codegen.internal.util.Formatting.tab;
import com.speedment.runtime.util.OptionalUtil;
import static java.util.Objects.requireNonNull;
import java.util.Optional;
import static java.util.stream.Collectors.joining;

/**
Expand All @@ -59,13 +61,17 @@ protected Class makeCodeGenModel(File file) {
* Getters
*/
.forEveryColumn((clazz, col) -> {
final Type retType;
final Type retType = GeneratedEntityTranslator.getterReturnType(col);
final String getter;
if (col.isNullable()) {
retType = OPTIONAL.add(Generic.of().add(Type.of(col.findTypeMapper().getJavaType())));
getter = "Optional.ofNullable(" + getSupport().variableName(col) + ")";
final String varName = getSupport().variableName(col);
if (retType.getJavaImpl().get() == Optional.class) {
getter = "Optional.ofNullable(" + varName + ")";
} else {
file.add(Import.of(Type.of(OptionalUtil.class)));
getter = "OptionalUtil.ofNullable(" + varName + ")";
}
} else {
retType = Type.of(col.findTypeMapper().getJavaType());
getter = getSupport().variableName(col);
}
clazz
Expand Down Expand Up @@ -138,7 +144,7 @@ protected Method toStringMethod(File file) {
columns().forEachOrdered(c -> {
final String getter;
if (c.isNullable()) {
getter = "get" + getSupport().typeName(c) + "()" + ".orElse(null)";
getter = "OptionalUtil.unwrap(get" + getSupport().typeName(c) + "())";
} else {
getter = "get" + getSupport().typeName(c) + "()";
}
Expand Down
Expand Up @@ -44,11 +44,15 @@
import static com.speedment.common.codegen.internal.model.constant.DefaultAnnotationUsage.OVERRIDE;
import static com.speedment.common.codegen.internal.model.constant.DefaultJavadocTag.PARAM;
import static com.speedment.common.codegen.internal.model.constant.DefaultJavadocTag.RETURN;
import com.speedment.common.codegen.internal.model.constant.DefaultType;
import static com.speedment.common.codegen.internal.model.constant.DefaultType.OPTIONAL;
import static com.speedment.common.codegen.internal.model.constant.DefaultType.STRING;
import static com.speedment.common.codegen.internal.util.Formatting.shortName;
import com.speedment.runtime.config.Column;
import static com.speedment.runtime.internal.util.document.DocumentUtil.Name.DATABASE_NAME;
import static com.speedment.runtime.internal.util.document.DocumentUtil.relativeName;
import com.speedment.runtime.util.OptionalBoolean;
import com.speedment.runtime.util.OptionalUtil;

/**
*
Expand All @@ -58,15 +62,16 @@
public final class GeneratedEntityTranslator extends EntityAndManagerTranslator<Interface> {

public final static String IDENTIFIER_NAME = "Identifier";
private @Inject Injector injector;

private @Inject
Injector injector;

public GeneratedEntityTranslator(Table table) {
super(table, Interface::of);
}

@Override
protected Interface makeCodeGenModel(File file) {

final Enum identifier = Enum.of(IDENTIFIER_NAME)
.add(Field.of("columnName", STRING).private_().final_())
.add(Type.of(FieldIdentifier.class).add(Generic.of().add(getSupport().entityType())))
Expand All @@ -92,74 +97,77 @@ protected Interface makeCodeGenModel(File file) {
);

final Interface iface = newBuilder(file, getSupport().generatedEntityName())
/*** General ***/
.forEveryTable((intrf, col) ->
intrf.public_()
.add(identifier)
.add(Type.of(Entity.class).add(Generic.of().add(getSupport().entityType())))
/**
* * General **
*/
.forEveryTable((intrf, col)
-> intrf.public_()
.add(identifier)
.add(Type.of(Entity.class).add(Generic.of().add(getSupport().entityType())))
)

/*** Getters ***/
/**
* * Getters **
*/
.forEveryColumn((intrf, col) -> {
final Type retType = col.isNullable()
? OPTIONAL.add(
Generic.of().add(
Type.of(col.findTypeMapper().getJavaType())
)
)
: Type.of(col.findTypeMapper().getJavaType());

final Type retType = getterReturnType(col);

intrf.add(Method.of(GETTER_METHOD_PREFIX + getSupport().typeName(col), retType)
.set(Javadoc.of(
"Returns the " + getSupport().variableName(col) +
" of this " + getSupport().entityName() +
". The " + getSupport().variableName(col) +
" field corresponds to the database column " +
relativeName(col, Dbms.class, DATABASE_NAME) + "."
).add(RETURN.setText(
"the " + getSupport().variableName(col) +
" of this " + getSupport().entityName()
))
"Returns the " + getSupport().variableName(col)
+ " of this " + getSupport().entityName()
+ ". The " + getSupport().variableName(col)
+ " field corresponds to the database column "
+ relativeName(col, Dbms.class, DATABASE_NAME) + "."
).add(RETURN.setText(
"the " + getSupport().variableName(col)
+ " of this " + getSupport().entityName()
))
)
);
})

/*** Setters ***/
/**
* * Setters **
*/
.forEveryColumn((intrf, col) -> {
intrf.add(Method.of(SETTER_METHOD_PREFIX + getSupport().typeName(col), getSupport().entityType())
.add(Field.of(getSupport().variableName(col), Type.of(col.findTypeMapper().getJavaType())))
.set(Javadoc.of(
"Sets the " + getSupport().variableName(col) +
" of this " + getSupport().entityName() +
". The " + getSupport().variableName(col) +
" field corresponds to the database column " +
relativeName(col, Dbms.class, DATABASE_NAME) + "."
"Sets the " + getSupport().variableName(col)
+ " of this " + getSupport().entityName()
+ ". The " + getSupport().variableName(col)
+ " field corresponds to the database column "
+ relativeName(col, Dbms.class, DATABASE_NAME) + "."
)
.add(PARAM.setValue(getSupport().variableName(col)).setText("to set of this " + getSupport().entityName()))
.add(RETURN.setText("this " + getSupport().entityName() + " instance")))
.add(PARAM.setValue(getSupport().variableName(col)).setText("to set of this " + getSupport().entityName()))
.add(RETURN.setText("this " + getSupport().entityName() + " instance")))
);
})

/*** Fields ***/
/**
* * Fields **
*/
.forEveryColumn((intrf, col) -> {
final EntityTranslatorSupport.ReferenceFieldType ref =
EntityTranslatorSupport.getReferenceFieldType(

final EntityTranslatorSupport.ReferenceFieldType ref
= EntityTranslatorSupport.getReferenceFieldType(
file, getSupport().tableOrThrow(), col, getSupport().entityType(), getNamer()
);

final String typeMapper = col.getTypeMapper();
final Type entityType = getSupport().entityType();
final String typeMapper = col.getTypeMapper();
final Type entityType = getSupport().entityType();
final String shortEntityName = getSupport().entityName();
final Type typeMapperType = Type.of(typeMapper);
final Type typeMapperType = Type.of(typeMapper);
final String shortEntityVarName = getSupport().namer().javaVariableName(shortEntityName);

file.add(Import.of(entityType));
file.add(Import.of(typeMapperType));

final String getter = col.isNullable()
? ("o -> o.get" + getSupport().typeName(col) + "().orElse(null)")
: (shortEntityName + "::get" + getSupport().typeName(col));
final String getter;
if (col.isNullable()) {
getter = "o -> OptionalUtil.unwrap(o.get" + getSupport().typeName(col) + "())";
file.add(Import.of(Type.of(OptionalUtil.class)));
} else {
getter = shortEntityName + "::get" + getSupport().typeName(col);
}

final String finder = EntityTranslatorSupport.getForeignKey(getSupport().tableOrThrow(), col)
.map(fkc -> {
Expand All @@ -168,14 +176,12 @@ file, getSupport().tableOrThrow(), col, getSupport().entityType(), getNamer()

file.add(Import.of(fuSupport.entityType()));

return ", (" + shortEntityVarName + ", fkManager) -> fkManager.findAny(" +
fu.getForeignEmt().getSupport().entityName() + "." +
fuSupport.namer().javaStaticFieldName(fu.getForeignColumn().getJavaName()
) + ", " + shortEntityVarName + ".get" +
getNamer().javaTypeName(col.getJavaName()) + "()" +

(col.isNullable() ? ".orElse(null)" : "")

return ", (" + shortEntityVarName + ", fkManager) -> fkManager.findAny("
+ fu.getForeignEmt().getSupport().entityName() + "."
+ fuSupport.namer().javaStaticFieldName(fu.getForeignColumn().getJavaName()
) + ", " + shortEntityVarName + ".get"
+ getNamer().javaTypeName(col.getJavaName()) + "()"
+ (col.isNullable() ? ".orElse(null)" : "")
+ ").orElse(null)";
}).orElse("");

Expand All @@ -186,29 +192,28 @@ file, getSupport().tableOrThrow(), col, getSupport().entityType(), getNamer()

file.add(Import.of(ref.implType));
intrf.add(Field.of(getNamer().javaStaticFieldName(col.getJavaName()), ref.type)
.final_()
.set(new ReferenceValue(
"new " + shortName(ref.implType.getName())
+ "<>(Identifier."
+ constant
+ ", "
+ getter
+ setter
+ finder
+ ", new "
+ shortName(typeMapper)
+ "(), "
+ DocumentDbUtil.isUnique(col)
+ ")"
))
.set(Javadoc.of(
"This Field corresponds to the {@link " + shortEntityName + "} field that can be obtained using the "
+ "{@link " + shortEntityName + "#get" + getSupport().typeName(col) + "()} method."
)));
.final_()
.set(new ReferenceValue(
"new " + shortName(ref.implType.getName())
+ "<>(Identifier."
+ constant
+ ", "
+ getter
+ setter
+ finder
+ ", new "
+ shortName(typeMapper)
+ "(), "
+ DocumentDbUtil.isUnique(col)
+ ")"
))
.set(Javadoc.of(
"This Field corresponds to the {@link " + shortEntityName + "} field that can be obtained using the "
+ "{@link " + shortEntityName + "#get" + getSupport().typeName(col) + "()} method."
)));
})

.build();

return iface;
}

Expand All @@ -226,4 +231,29 @@ protected String getClassOrInterfaceName() {
public boolean isInGeneratedPackage() {
return true;
}
}

static Type getterReturnType(Column col) {
final Type retType;
final Class<?> javaType = col.findTypeMapper().getJavaType();

if (col.isNullable()) {
if (javaType == Integer.class) {
retType = DefaultType.OPTIONAL_INT;
} else if (javaType == Long.class) {
retType = DefaultType.OPTIONAL_LONG;
} else if (javaType == Double.class) {
retType = DefaultType.OPTIONAL_DOUBLE;
} else if (javaType == Boolean.class) {
retType = Type.of(OptionalBoolean.class);
} else {
retType = OPTIONAL.add(
Generic.of(Type.of(javaType))
);
}
} else {
retType = Type.of(javaType);
}

return retType;
}
}
Expand Up @@ -45,6 +45,7 @@
import static com.speedment.generator.internal.DefaultJavaClassTranslator.GETTER_METHOD_PREFIX;
import static com.speedment.generator.internal.DefaultJavaClassTranslator.SETTER_METHOD_PREFIX;
import static com.speedment.generator.internal.manager.GeneratedManagerImplTranslator.*;
import com.speedment.runtime.util.OptionalUtil;
import static java.util.stream.Collectors.joining;

/**
Expand All @@ -70,7 +71,7 @@ public static String[] generateGetBody(TranslatorSupport<Table> support, File fi
.filter(HasEnabled::isEnabled)
.map(c
-> "case " + support.namer().javaStaticFieldName(c.getJavaName())
+ " : return entity." + getterCode(support, c)
+ " : return " + getterCode(file, support, c)
+ ";"
).collect(Collectors.joining(nl()))
+ nl() + "default : throw new IllegalArgumentException(\"Unknown identifier '\" + identifier + \"'.\");"
Expand Down Expand Up @@ -186,11 +187,12 @@ private static String castToColumnTypeIfNotObject(Column c) {
}
}

private static String getterCode(TranslatorSupport<Table> support, Column c) {
private static String getterCode(File file, TranslatorSupport<Table> support, Column c) {
if (c.isNullable()) {
return GETTER_METHOD_PREFIX + support.typeName(c) + "().orElse(null)";
file.add(Import.of(Type.of(OptionalUtil.class)));
return "OptionalUtil.unwrap(entity." + GETTER_METHOD_PREFIX + support.typeName(c) + "())";
} else {
return GETTER_METHOD_PREFIX + support.typeName(c) + "()";
return "entity." + GETTER_METHOD_PREFIX + support.typeName(c) + "()";
}
}

Expand Down
Expand Up @@ -51,8 +51,10 @@ public void testLatestVersion() {
try {
final String latest = version.latestVersion().get(2, TimeUnit.SECONDS);
System.out.println("The latest released version of Speedment is " + latest + ".");
} catch (final ExecutionException | InterruptedException | TimeoutException ex) {
} catch (final ExecutionException | InterruptedException ex) {
throw new RuntimeException(ex);
} catch (final TimeoutException ex ) {
System.out.println("Connection timed out before a version could be established");
}
}

Expand Down

0 comments on commit 0082bbd

Please sign in to comment.