Skip to content

Commit

Permalink
Move get and set, add getPrimaryKeyType to base interface
Browse files Browse the repository at this point in the history
  • Loading branch information
minborg committed Mar 3, 2016
1 parent 7d80e62 commit ab61809
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 119 deletions.
Expand Up @@ -30,35 +30,35 @@
import java.util.Optional;
import java.util.function.Function;
import static com.speedment.util.NullUtil.requireNonNulls;
import com.speedment.util.tuple.Tuple1;

/**
*
* @author pemi
* @param <T> type of model to translate into
* @author pemi
* @param <T> type of model to translate into
*/
public abstract class EntityAndManagerTranslator<T extends ClassOrInterface<T>>
extends DefaultJavaClassTranslator<Table, T> {
public abstract class EntityAndManagerTranslator<T extends ClassOrInterface<T>>
extends DefaultJavaClassTranslator<Table, T> {

public final class ClassType {

public final static String
GENERATED_PACKAGE = "generated",
GENERATED_PREFIX = "Generated";


public final static String GENERATED_PACKAGE = "generated",
GENERATED_PREFIX = "Generated";

private final Type type;
private final Type implType;
private final Type generatedType;
private final Type generatedImplType;
private final Type optionalType;

private ClassType(String typeSuffix, String implSuffix) {
requireNonNulls(typeSuffix, implSuffix);
this.type = Type.of(fullyQualifiedTypeName() + typeSuffix);
this.implType = Type.of(fullyQualifiedTypeName() + typeSuffix + implSuffix);
this.generatedType = Type.of(fullyQualifiedTypeName(GENERATED_PACKAGE, GENERATED_PREFIX) + typeSuffix);

this.type = Type.of(fullyQualifiedTypeName() + typeSuffix);
this.implType = Type.of(fullyQualifiedTypeName() + typeSuffix + implSuffix);
this.generatedType = Type.of(fullyQualifiedTypeName(GENERATED_PACKAGE, GENERATED_PREFIX) + typeSuffix);
this.generatedImplType = Type.of(fullyQualifiedTypeName(GENERATED_PACKAGE, GENERATED_PREFIX) + typeSuffix + implSuffix);
this.optionalType = Type.of(Optional.class).add(Generic.of().add(type));
this.optionalType = Type.of(Optional.class).add(Generic.of().add(type));
}

public Type getType() {
Expand All @@ -68,11 +68,11 @@ public Type getType() {
public Type getImplType() {
return implType;
}

public Type getGeneratedType() {
return generatedType;
}

public Type getGeneratedImplType() {
return generatedImplType;
}
Expand All @@ -84,7 +84,7 @@ public String getName() {
public String getImplName() {
return Formatting.shortName(implType.getName());
}

public String getGeneratedName() {
return Formatting.shortName(generatedType.getName());
}
Expand All @@ -98,21 +98,19 @@ public Type getOptionalType() {
}
}

protected final ClassType
entity = new ClassType("", "Impl"),
manager = new ClassType("Manager", "Impl");
protected final ClassType entity = new ClassType("", "Impl"),
manager = new ClassType("Manager", "Impl");

protected final Generic
genericOfPk = Generic.of().add(typeOfPK()),
genericOfEntity = Generic.of().add(entity.getType()),
genericOfManager = Generic.of().add(manager.getType());
protected final Generic genericOfPk = Generic.of().add(typeOfPK()),
genericOfEntity = Generic.of().add(entity.getType()),
genericOfManager = Generic.of().add(manager.getType());

protected EntityAndManagerTranslator(
Speedment speedment,
Generator gen,
Table table,
Speedment speedment,
Generator gen,
Table table,
Function<String, T> modelConstructor) {

super(speedment, gen, table, modelConstructor);
}

Expand All @@ -124,49 +122,72 @@ protected Type typeOfPK() {
}

final Class<?> first = primaryKeyColumns()
.findFirst().get()
.findColumn().get()
.findTypeMapper().getJavaType();
.findFirst().get()
.findColumn().get()
.findTypeMapper().getJavaType();

if (pks == 1) {
return Type.of(first);
} else {
if (primaryKeyColumns().allMatch(c -> c.findColumn().get()
} else if (primaryKeyColumns().allMatch(c -> c.findColumn().get()
.findTypeMapper().getJavaType().equals(first))) {

return DefaultType.list(Type.of(first));
} else {
return DefaultType.list(DefaultType.WILDCARD);
}

return DefaultType.list(Type.of(first));
} else {
return DefaultType.list(DefaultType.WILDCARD);
}
}

protected Type pkTupleType() {
final long pks = primaryKeyColumns().count();
final Package package_ = Tuple1.class.getPackage();
final String tupleClassName = package_.getName() + ".Tuple" + pks;
final java.lang.Class<?> tupleClass;
try {
tupleClass = java.lang.Class.forName(tupleClassName);
} catch (ClassNotFoundException cnf) {
throw new SpeedmentException("Speedment does not support " + pks + " primary keys.", cnf);
}
final Type result = Type.of(tupleClass);
primaryKeyColumns().forEachOrdered(pk -> {
result.add(
Generic.of().add(
Type.of(java.lang.Class.class)
.add(Generic.of().add(
Type.of(pk.findColumn().get().findTypeMapper().getJavaType()))
)
)
);
});
return result;

}

public final ClassType entity() {
return entity;
}

public final Generic genericOfEntity() {
return genericOfEntity;
}

protected final Table tableOrThrow() {
return table().orElseThrow(() -> new SpeedmentException(
getClass().getSimpleName() + " must have a " +
Table.class.getSimpleName() + " document."
getClass().getSimpleName() + " must have a "
+ Table.class.getSimpleName() + " document."
));
}

protected final Schema schemaOrThrow() {
return schema().orElseThrow(() -> new SpeedmentException(
getClass().getSimpleName() + " must have a " +
Schema.class.getSimpleName() + " document."
getClass().getSimpleName() + " must have a "
+ Schema.class.getSimpleName() + " document."
));
}

protected final Dbms dbmsOrThrow() {
return dbms().orElseThrow(() -> new SpeedmentException(
getClass().getSimpleName() + " must have a " +
Dbms.class.getSimpleName() + " document."
getClass().getSimpleName() + " must have a "
+ Dbms.class.getSimpleName() + " document."
));
}
}
}
Expand Up @@ -31,7 +31,6 @@
import com.speedment.internal.codegen.lang.models.Method;
import com.speedment.internal.codegen.lang.models.Type;
import static com.speedment.internal.codegen.lang.models.constants.DefaultAnnotationUsage.OVERRIDE;
import com.speedment.internal.codegen.lang.models.constants.DefaultType;
import static com.speedment.internal.codegen.lang.models.constants.DefaultType.OBJECT;
import static com.speedment.internal.codegen.lang.models.constants.DefaultType.VOID;
import com.speedment.internal.codegen.lang.models.values.ReferenceValue;
Expand All @@ -52,7 +51,6 @@
import static com.speedment.internal.codegen.util.Formatting.indent;
import static com.speedment.internal.codegen.util.Formatting.nl;
import com.speedment.internal.util.sql.ResultSetUtil;
import com.speedment.util.tuple.Tuple;
import com.speedment.util.tuple.Tuple1;
import com.speedment.util.tuple.Tuples;
import static java.util.stream.Collectors.joining;
Expand Down Expand Up @@ -119,6 +117,8 @@ protected Class makeCodeGenModel(File file) {
.call($ -> file.add(Import.of(entity.getImplType())))
.call($ -> file.add(Import.of(Type.of(Speedment.class))))
)
.add(generateGet(file))
.add(generateSet(file))
.add(generateGetPrimaryKeyClassesField(file))
.add(generateGetPrimaryKeyClasses(file));
}
Expand Down Expand Up @@ -230,31 +230,6 @@ public Type getImplType() {
return manager.getImplType();
}

private Type pkTupleType() {
final long pks = primaryKeyColumns().count();
final Package package_ = Tuple1.class.getPackage();
final String tupleClassName = package_.getName() + ".Tuple" + pks;
final java.lang.Class<?> tupleClass;
try {
tupleClass = java.lang.Class.forName(tupleClassName);
} catch (ClassNotFoundException cnf) {
throw new SpeedmentException("Speedment does not support " + pks + " primary keys.", cnf);
}
final Type result = Type.of(tupleClass);
primaryKeyColumns().forEachOrdered(pk -> {
result.add(
Generic.of().add(
Type.of(java.lang.Class.class)
.add(Generic.of().add(
Type.of(pk.findColumn().get().findTypeMapper().getJavaType()))
)
)
);
});
return result;

}

protected Field generateGetPrimaryKeyClassesField(File file) {
file.add(Import.of(Type.of(Tuples.class)));
final Field field = Field.of(PRIMARY_KEY_CLASSES, pkTupleType())
Expand Down Expand Up @@ -287,7 +262,11 @@ protected Method generateGet(File file) {
.add(Field.of("entity", entity.getType()))
.add(Field.of("column", Type.of(Column.class)))
.add("switch (column.getName()) " + block(
columns().map(c -> "case \"" + c.getName() + "\" : return entity." + GETTER_METHOD_PREFIX + typeName(c) + "();").collect(Collectors.joining(nl()))
columns().map(c
-> "case \"" + c.getName()
+ "\" : return entity." + getterCode(c)
+ ";"
).collect(Collectors.joining(nl()))
+ nl() + "default : throw new IllegalArgumentException(\"Unknown column '\" + column.getName() + \"'.\");"
));
}
Expand All @@ -301,11 +280,23 @@ protected Method generateSet(File file) {
.add("switch (column.getName()) " + block(
columns()
.peek(c -> file.add(Import.of(Type.of(c.findTypeMapper().getJavaType()))))
.map(c -> "case \"" + c.getName() + "\" : entity." + SETTER_METHOD_PREFIX + typeName(c) + "((" + c.findTypeMapper().getJavaType().getSimpleName() + ") value); break;").collect(Collectors.joining(nl()))
.map(c
-> "case \"" + c.getName()
+ "\" : entity." + SETTER_METHOD_PREFIX + typeName(c)
+ "((" + c.findTypeMapper().getJavaType().getSimpleName()
+ ") value); break;").collect(Collectors.joining(nl()))
+ nl() + "default : throw new IllegalArgumentException(\"Unknown column '\" + column.getName() + \"'.\");"
));
}

private String getterCode(Column c) {
if (c.isNullable()) {
return GETTER_METHOD_PREFIX + typeName(c) + "().orElse(null)";
} else {
return GETTER_METHOD_PREFIX + typeName(c) + "()";
}
}

protected Method generatePrimaryKeyFor(File file) {
final Method method = Method.of("primaryKeyFor", typeOfPK()).public_().add(OVERRIDE)
.add(Field.of("entity", entity.getType()));
Expand Down
Expand Up @@ -70,8 +70,7 @@ protected Interface makeCodeGenModel(File file) {
.add("return " + manager.getName() + ".class;"))
.add(Method.of("getEntityClass", Type.of(Class.class).add(genericOfEntity)).default_().add(OVERRIDE)
.add("return " + entity.getName() + ".class;"))
.add(generateGet(file))
.add(generateSet(file));
.add(generateGetPrimaryKeyClasses(file));
}

protected Method generatePrimaryKeyFor(File file) {
Expand All @@ -91,45 +90,10 @@ protected Method generatePrimaryKeyFor(File file) {
return method;
}

protected Method generateGet(File file) {
file.add(Import.of(Type.of(IllegalArgumentException.class)));
return Method.of("get", OBJECT).default_().add(OVERRIDE)
.add(Field.of("entity", entity.getType()))
.add(Field.of("column", Type.of(Column.class)))
.add("switch (column.getName()) " + block(
columns().map(c
-> "case \"" + c.getName()
+ "\" : return entity." + getterCode(c)
+ ";"
).collect(Collectors.joining(nl()))
+ nl() + "default : throw new IllegalArgumentException(\"Unknown column '\" + column.getName() + \"'.\");"
));
}

protected Method generateSet(File file) {
file.add(Import.of(Type.of(IllegalArgumentException.class)));
return Method.of("set", VOID).default_().add(OVERRIDE)
.add(Field.of("entity", entity.getType()))
.add(Field.of("column", Type.of(Column.class)))
.add(Field.of("value", Type.of(Object.class)))
.add("switch (column.getName()) " + block(
columns()
.peek(c -> file.add(Import.of(Type.of(c.findTypeMapper().getJavaType()))))
.map(c
-> "case \"" + c.getName()
+ "\" : entity." + SETTER_METHOD_PREFIX + typeName(c)
+ "((" + c.findTypeMapper().getJavaType().getSimpleName()
+ ") value); break;").collect(Collectors.joining(nl()))
+ nl() + "default : throw new IllegalArgumentException(\"Unknown column '\" + column.getName() + \"'.\");"
));
}

private String getterCode(Column c) {
if (c.isNullable()) {
return GETTER_METHOD_PREFIX + typeName(c) + "().orElse(null)";
} else {
return GETTER_METHOD_PREFIX + typeName(c) + "()";
}
protected Method generateGetPrimaryKeyClasses(File file) {
return Method.of("getPrimaryKeyClasses", pkTupleType()).add(OVERRIDE);
}

@Override
Expand Down

0 comments on commit ab61809

Please sign in to comment.