Search in sources :

Example 1 with JavadocPart

use of com.abubusoft.kripton.processor.sqlite.AbstractSelectCodeGenerator.JavadocPart in project kripton by xcesco.

the class JavadocUtility method generateJavaDocForSelect.

public static void generateJavaDocForSelect(MethodSpec.Builder methodBuilder, List<String> sqlParams, final SQLiteModelMethod method, ModelAnnotation annotation, Set<JQLProjection> fieldList, SelectType selectResultType, JavadocPart... javadocParts) {
    final SQLiteDaoDefinition daoDefinition = method.getParent();
    final SQLiteEntity entity = daoDefinition.getEntity();
    TypeName beanTypeName = TypeName.get(daoDefinition.getEntity().getElement().asType());
    String sql = JQLChecker.getInstance().replace(method, method.jql, new JQLReplacerListenerImpl(method) {

        @Override
        public String onColumnName(String columnName) {
            SQLProperty tempProperty = daoDefinition.getEntity().get(columnName);
            AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
            return tempProperty.columnName;
        }
    });
    Set<JQLProjection> projectedColumns = JQLChecker.getInstance().extractProjections(method, method.jql.value, entity);
    methodBuilder.addJavadoc("<h2>Select SQL:</h2>\n\n", annotation.getSimpleName());
    methodBuilder.addJavadoc("<pre>$L</pre>", sql);
    methodBuilder.addJavadoc("\n\n");
    // there will be alway some projected column
    {
        methodBuilder.addJavadoc("<h2>Projected columns:</h2>\n");
        methodBuilder.addJavadoc("<dl>\n");
        for (JQLProjection column : projectedColumns) {
            // KRIPTON_DEBUG field info only it exists
            if (column.column != null) {
                methodBuilder.addJavadoc("\t<dt>$L</dt>", column.property.columnName);
                // SQLProperty attribute = fieldList.value1.get(i);
                methodBuilder.addJavadoc("<dd>is associated to bean's property <strong>$L</strong></dd>", column.column);
            } else {
                methodBuilder.addJavadoc("\t<dt>$L</dt>", column.expression);
                methodBuilder.addJavadoc("<dd>no bean's property is associated</dd>");
            }
            methodBuilder.addJavadoc("\n");
        }
        methodBuilder.addJavadoc("</dl>");
        methodBuilder.addJavadoc("\n\n");
    }
    // dynamic parts
    if (method.hasDynamicOrderByConditions() || method.hasDynamicWhereConditions() || method.hasDynamicPageSizeConditions()) {
        methodBuilder.addJavadoc("<h2>Method's parameters and associated dynamic parts:</h2>\n");
        methodBuilder.addJavadoc("<dl>\n");
        if (method.hasDynamicWhereConditions()) {
            methodBuilder.addJavadoc("<dt>$L</dt><dd>is part of where conditions resolved at runtime. In above SQL it is displayed as #{$L}</dd>\n", method.dynamicWhereParameterName, JQLDynamicStatementType.DYNAMIC_WHERE);
        }
        if (method.hasDynamicOrderByConditions()) {
            methodBuilder.addJavadoc("<dt>$L</dt>is part of order statement resolved at runtime. In above SQL it is displayed as #{$L}</dd>\n", method.dynamicOrderByParameterName, JQLDynamicStatementType.DYNAMIC_ORDER_BY);
        }
        if (method.hasDynamicPageSizeConditions()) {
            methodBuilder.addJavadoc("<dt>$L</dt>is part of limit statement resolved at runtime. In above SQL it is displayed as #{$L}</dd>\n", method.dynamicPageSizeName, JQLDynamicStatementType.DYNAMIC_PAGE_SIZE);
        }
        methodBuilder.addJavadoc("</dl>");
        methodBuilder.addJavadoc("\n\n");
    }
    // query parameters
    if (sqlParams.size() > 0) {
        methodBuilder.addJavadoc("<h2>Query's parameters:</h2>\n");
        methodBuilder.addJavadoc("<dl>\n");
        for (String param : sqlParams) {
            methodBuilder.addJavadoc("\t<dt>$L</dt><dd>is binded to method's parameter <strong>$L</strong></dd>\n", "${" + param + "}", method.findParameterNameByAlias(param));
        }
        methodBuilder.addJavadoc("</dl>");
        methodBuilder.addJavadoc("\n\n");
    }
    // method params
    ParameterSpec parameterSpec;
    for (Pair<String, TypeName> item : method.getParameters()) {
        parameterSpec = ParameterSpec.builder(item.value1, item.value0).build();
        methodBuilder.addJavadoc("@param $L\n", parameterSpec.name);
        if (beanTypeName.equals(item.value1)) {
            methodBuilder.addJavadoc("\tis used as $L\n", "${" + method.findParameterAliasByName(item.value0) + "}");
        } else if (TypeUtility.isTypeEquals(item.value1, ParameterizedTypeName.get(TypeUtility.className(OnReadBeanListener.class), beanTypeName))) {
            methodBuilder.addJavadoc("\tis the $T listener\n", beanTypeName);
        } else if (TypeUtility.isTypeEquals(item.value1, TypeUtility.className(OnReadCursorListener.class))) {
            methodBuilder.addJavadoc("\tis the cursor listener\n", beanTypeName);
        } else if (item.value0.equals(method.dynamicWhereParameterName)) {
            methodBuilder.addJavadoc("\tis used as <strong>dynamic WHERE statement</strong> and it is formatted by ({@link $T#format})\n", StringUtils.class);
        } else if (item.value0.equals(method.dynamicOrderByParameterName)) {
            methodBuilder.addJavadoc("\tis used as <strong>dynamic ORDER BY statement</strong> and it is formatted by ({@link $T#format})\n", StringUtils.class);
        } else if (item.value0.equals(method.dynamicPageSizeName)) {
            methodBuilder.addJavadoc("\tis used as <strong>dynamic LIMIT statement</strong> and it is formatted by ({@link $T#format})\n", StringUtils.class);
        } else {
            methodBuilder.addJavadoc("\tis binded to <code>$L</code>\n", "${" + method.findParameterAliasByName(item.value0) + "}");
        }
    }
    for (JavadocPart item : javadocParts) {
        if (item.javadocPartType != JavadocPartType.ADD_PARAMETER)
            continue;
        methodBuilder.addJavadoc("@param $L\n", item.name);
        methodBuilder.addJavadoc("\t$L\n", item.description);
    }
    for (JavadocPart item : javadocParts) {
        if (item.javadocPartType != JavadocPartType.RETURN)
            continue;
        methodBuilder.addJavadoc("@return $L\n", item.description);
        // override return
        return;
    }
    // return type
    switch(selectResultType) {
        case BEAN:
            methodBuilder.addJavadoc("@return selected bean or <code>null</code>.\n");
            break;
        case CURSOR:
            methodBuilder.addJavadoc("@return cursor. Closing the cursor is delegated to the calling code.\n");
            break;
        case LIST_BEAN:
            methodBuilder.addJavadoc("@return collection of bean or empty collection.\n");
            break;
        case LIST_SCALAR:
            methodBuilder.addJavadoc("@return collection of single value extracted by query.\n");
            break;
        case SCALAR:
            methodBuilder.addJavadoc("@return single value extracted by query.\n");
            break;
        case PAGED_RESULT:
            methodBuilder.addJavadoc("@return paginated result.\n");
            break;
        default:
            // case LISTENER_CURSOR:
            break;
    }
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) OnReadBeanListener(com.abubusoft.kripton.android.sqlite.OnReadBeanListener) ParameterSpec(com.squareup.javapoet.ParameterSpec) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) OnReadCursorListener(com.abubusoft.kripton.android.sqlite.OnReadCursorListener) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl) JQLProjection(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection) StringUtils(com.abubusoft.kripton.common.StringUtils) JavadocPart(com.abubusoft.kripton.processor.sqlite.AbstractSelectCodeGenerator.JavadocPart) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)

Aggregations

OnReadBeanListener (com.abubusoft.kripton.android.sqlite.OnReadBeanListener)1 OnReadCursorListener (com.abubusoft.kripton.android.sqlite.OnReadCursorListener)1 StringUtils (com.abubusoft.kripton.common.StringUtils)1 JavadocPart (com.abubusoft.kripton.processor.sqlite.AbstractSelectCodeGenerator.JavadocPart)1 JQLProjection (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection)1 JQLReplacerListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl)1 SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)1 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)1 SQLiteEntity (com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)1 ParameterSpec (com.squareup.javapoet.ParameterSpec)1 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)1 TypeName (com.squareup.javapoet.TypeName)1