Search in sources :

Example 6 with StringUtils.defaultString

use of org.jooq.tools.StringUtils.defaultString in project jOOQ by jOOQ.

the class JavaGenerator method generateInterfaceGetter0.

private final void generateInterfaceGetter0(TypedElementDefinition<?> column, @SuppressWarnings("unused") int index, JavaWriter out) {
    final String comment = StringUtils.defaultString(column.getComment());
    final String getter = getStrategy().getJavaGetterName(column, Mode.INTERFACE);
    final String type = out.ref(getJavaType(column.getType(), Mode.INTERFACE));
    final String name = column.getQualifiedOutputName();
    out.tab(1).javadoc("Getter for <code>%s</code>.%s", name, defaultIfBlank(" " + escapeEntities(comment), ""));
    if (column instanceof ColumnDefinition)
        printColumnJPAAnnotation(out, (ColumnDefinition) column);
    printValidationAnnotation(out, column);
    if (scala)
        out.tab(1).println("def %s : %s", getter, type);
    else
        out.tab(1).println("public %s %s();", type, getter);
}
Also used : StringUtils.defaultString(org.jooq.tools.StringUtils.defaultString)

Example 7 with StringUtils.defaultString

use of org.jooq.tools.StringUtils.defaultString in project jOOQ by jOOQ.

the class JavaGenerator method generateRoutine.

protected void generateRoutine(RoutineDefinition routine, JavaWriter out) {
    final SchemaDefinition schema = routine.getSchema();
    final String className = getStrategy().getJavaClassName(routine);
    final String returnType = (routine.getReturnValue() == null) ? Void.class.getName() : out.ref(getJavaType(routine.getReturnType()));
    final List<String> returnTypeRef = list((routine.getReturnValue() != null) ? getJavaTypeReference(database, routine.getReturnType()) : null);
    final List<String> returnConverter = out.ref(list((routine.getReturnValue() != null) ? routine.getReturnType().getConverter() : null));
    final List<String> returnBinding = out.ref(list((routine.getReturnValue() != null) ? routine.getReturnType().getBinding() : null));
    final List<String> interfaces = out.ref(getStrategy().getJavaClassImplements(routine, Mode.DEFAULT));
    final String schemaId = out.ref(getStrategy().getFullJavaIdentifier(schema), 2);
    final List<String> packageId = out.ref(getStrategy().getFullJavaIdentifiers(routine.getPackage()), 2);
    printPackage(out, routine);
    if (scala) {
        out.println("object %s {", className);
        for (ParameterDefinition parameter : routine.getAllParameters()) {
            final String paramType = out.ref(getJavaType(parameter.getType()));
            final String paramTypeRef = getJavaTypeReference(parameter.getDatabase(), parameter.getType());
            final String paramId = out.ref(getStrategy().getJavaIdentifier(parameter), 2);
            final String paramName = parameter.getName();
            final String paramComment = StringUtils.defaultString(parameter.getComment());
            final String isDefaulted = parameter.isDefaulted() ? "true" : "false";
            final String isUnnamed = parameter.isUnnamed() ? "true" : "false";
            final List<String> converters = out.ref(list(parameter.getType().getConverter(), parameter.getType().getBinding()));
            out.tab(1).javadoc("The parameter <code>%s</code>.%s", parameter.getQualifiedOutputName(), defaultIfBlank(" " + escapeEntities(paramComment), ""));
            out.tab(1).println("val %s : %s[%s] = %s.createParameter(\"%s\", %s, %s, %s[[before=, ][new %s]])", paramId, Parameter.class, paramType, AbstractRoutine.class, paramName, paramTypeRef, isDefaulted, isUnnamed, converters);
        }
        out.println("}");
        out.println();
    }
    generateRoutineClassJavadoc(routine, out);
    printClassAnnotations(out, schema);
    if (scala) {
        out.println("class %s extends %s[%s](\"%s\", %s[[before=, ][%s]][[before=, ][%s]]" + converterTemplate(returnConverter) + converterTemplate(returnBinding) + ")[[before= with ][separator= with ][%s]] {", className, AbstractRoutine.class, returnType, routine.getName(), schemaId, packageId, returnTypeRef, returnConverter, returnBinding, interfaces);
    } else {
        out.println("public class %s extends %s<%s>[[before= implements ][%s]] {", className, AbstractRoutine.class, returnType, interfaces);
        out.printSerial();
        for (ParameterDefinition parameter : routine.getAllParameters()) {
            final String paramType = out.ref(getJavaType(parameter.getType()));
            final String paramTypeRef = getJavaTypeReference(parameter.getDatabase(), parameter.getType());
            final String paramId = out.ref(getStrategy().getJavaIdentifier(parameter), 2);
            final String paramName = parameter.getName();
            final String paramComment = StringUtils.defaultString(parameter.getComment());
            final String isDefaulted = parameter.isDefaulted() ? "true" : "false";
            final String isUnnamed = parameter.isUnnamed() ? "true" : "false";
            final List<String> converter = out.ref(list(parameter.getType().getConverter()));
            final List<String> binding = out.ref(list(parameter.getType().getBinding()));
            out.tab(1).javadoc("The parameter <code>%s</code>.%s", parameter.getQualifiedOutputName(), defaultIfBlank(" " + paramComment, ""));
            out.tab(1).println("public static final %s<%s> %s = createParameter(\"%s\", %s, %s, %s" + converterTemplate(converter) + converterTemplate(binding) + ");", Parameter.class, paramType, paramId, paramName, paramTypeRef, isDefaulted, isUnnamed, converter, binding);
        }
    }
    if (scala) {
        out.tab(1).println("{");
    } else {
        out.tab(1).javadoc("Create a new routine call instance");
        out.tab(1).println("public %s() {", className);
        out.tab(2).println("super(\"%s\", %s[[before=, ][%s]][[before=, ][%s]]" + converterTemplate(returnConverter) + converterTemplate(returnBinding) + ");", routine.getName(), schemaId, packageId, returnTypeRef, returnConverter, returnBinding);
        if (routine.getAllParameters().size() > 0) {
            out.println();
        }
    }
    for (ParameterDefinition parameter : routine.getAllParameters()) {
        final String paramId = getStrategy().getJavaIdentifier(parameter);
        if (parameter.equals(routine.getReturnValue())) {
            if (scala)
                out.tab(2).println("setReturnParameter(%s.%s)", className, paramId);
            else
                out.tab(2).println("setReturnParameter(%s);", paramId);
        } else if (routine.getInParameters().contains(parameter)) {
            if (routine.getOutParameters().contains(parameter)) {
                if (scala)
                    out.tab(2).println("addInOutParameter(%s.%s)", className, paramId);
                else
                    out.tab(2).println("addInOutParameter(%s);", paramId);
            } else {
                if (scala)
                    out.tab(2).println("addInParameter(%s.%s)", className, paramId);
                else
                    out.tab(2).println("addInParameter(%s);", paramId);
            }
        } else {
            if (scala)
                out.tab(2).println("addOutParameter(%s.%s)", className, paramId);
            else
                out.tab(2).println("addOutParameter(%s);", paramId);
        }
    }
    if (routine.getOverload() != null) {
        if (scala)
            out.tab(2).println("setOverloaded(true)");
        else
            out.tab(2).println("setOverloaded(true);");
    }
    out.tab(1).println("}");
    for (ParameterDefinition parameter : routine.getInParameters()) {
        final String setterReturnType = fluentSetters() ? className : (scala ? "Unit" : "void");
        final String setter = getStrategy().getJavaSetterName(parameter, Mode.DEFAULT);
        final String numberValue = parameter.getType().isGenericNumberType() ? "Number" : "Value";
        final String numberField = parameter.getType().isGenericNumberType() ? "Number" : "Field";
        final String paramId = getStrategy().getJavaIdentifier(parameter);
        final String paramName = "value".equals(paramId) ? "value_" : "value";
        out.tab(1).javadoc("Set the <code>%s</code> parameter IN value to the routine", parameter.getOutputName());
        if (scala) {
            out.tab(1).println("def %s(%s : %s) : Unit = {", setter, paramName, refNumberType(out, parameter.getType()));
            out.tab(2).println("set%s(%s.%s, %s)", numberValue, className, paramId, paramName);
            out.tab(1).println("}");
        } else {
            out.tab(1).println("public void %s(%s %s) {", setter, varargsIfArray(refNumberType(out, parameter.getType())), paramName);
            out.tab(2).println("set%s(%s, %s);", numberValue, paramId, paramName);
            out.tab(1).println("}");
        }
        if (routine.isSQLUsable()) {
            out.tab(1).javadoc("Set the <code>%s</code> parameter to the function to be used with a {@link org.jooq.Select} statement", parameter.getOutputName());
            if (scala) {
                out.tab(1).println("def %s(field : %s[%s]) : %s = {", setter, Field.class, refExtendsNumberType(out, parameter.getType()), setterReturnType);
                out.tab(2).println("set%s(%s.%s, field)", numberField, className, paramId);
                if (fluentSetters())
                    out.tab(2).println("this");
                out.tab(1).println("}");
            } else {
                out.tab(1).println("public %s %s(%s<%s> field) {", setterReturnType, setter, Field.class, refExtendsNumberType(out, parameter.getType()));
                out.tab(2).println("set%s(%s, field);", numberField, paramId);
                if (fluentSetters())
                    out.tab(2).println("return this;");
                out.tab(1).println("}");
            }
        }
    }
    for (ParameterDefinition parameter : routine.getAllParameters()) {
        boolean isReturnValue = parameter.equals(routine.getReturnValue());
        boolean isOutParameter = routine.getOutParameters().contains(parameter);
        if (isOutParameter && !isReturnValue) {
            final String paramName = parameter.getOutputName();
            final String paramType = out.ref(getJavaType(parameter.getType()));
            final String paramGetter = getStrategy().getJavaGetterName(parameter, Mode.DEFAULT);
            final String paramId = getStrategy().getJavaIdentifier(parameter);
            out.tab(1).javadoc("Get the <code>%s</code> parameter OUT value from the routine", paramName);
            if (scala) {
                out.tab(1).println("def %s : %s = {", paramGetter, paramType);
                out.tab(2).println("get(%s.%s)", className, paramId);
                out.tab(1).println("}");
            } else {
                out.tab(1).println("public %s %s() {", paramType, paramGetter);
                out.tab(2).println("return get(%s);", paramId);
                out.tab(1).println("}");
            }
        }
    }
    generateRoutineClassFooter(routine, out);
    out.println("}");
}
Also used : StringUtils.defaultString(org.jooq.tools.StringUtils.defaultString)

Aggregations

StringUtils.defaultString (org.jooq.tools.StringUtils.defaultString)7 IOException (java.io.IOException)1 Pattern (java.util.regex.Pattern)1 Field (org.jooq.Field)1 Package (org.jooq.Package)1 TableField (org.jooq.TableField)1 UDTField (org.jooq.UDTField)1 SQLDialectNotSupportedException (org.jooq.exception.SQLDialectNotSupportedException)1 ReflectException (org.jooq.tools.reflect.ReflectException)1