/*
 * Copyright 2015, Yahoo Inc.
 * Copyrights licensed under the Apache 2.0 License.
 * See the accompanying LICENSE file for terms.
 */
package com.yahoo.squidb.processor.data;

import com.yahoo.aptutils.model.DeclaredTypeName;
import com.yahoo.aptutils.utils.AptUtils;
import com.yahoo.squidb.annotations.TableModelSpec;
import com.yahoo.squidb.processor.TypeConstants;
import com.yahoo.squidb.processor.plugins.PluginEnvironment;

import java.util.Set;

import javax.lang.model.element.TypeElement;

public class TableModelSpecWrapper extends ModelSpec<TableModelSpec> {

    public TableModelSpecWrapper(TypeElement modelSpecElement, PluginEnvironment pluginEnv, AptUtils utils) {
        super(modelSpecElement, TableModelSpec.class, pluginEnv, utils);
    }

    @Override
    public <RETURN, PARAMETER> RETURN accept(ModelSpecVisitor<RETURN, PARAMETER> visitor, PARAMETER data) {
        return visitor.visitTableModel(this, data);
    }

    /**
     * @return true if the table model is for a virtual table, false otherwise
     */
    public boolean isVirtualTable() {
        return !AptUtils.isEmpty(modelSpecAnnotation.virtualModule());
    }

    @Override
    protected String getGeneratedClassNameString() {
        return modelSpecAnnotation.className();
    }

    @Override
    protected DeclaredTypeName getDefaultModelSuperclass() {
        return TypeConstants.TABLE_MODEL;
    }

    @Override
    protected void addModelSpecificImports(Set<DeclaredTypeName> imports) {
        imports.add(TypeConstants.LONG_PROPERTY);
        imports.add(TypeConstants.TABLE_MODEL);
        imports.add(TypeConstants.TABLE_MODEL_NAME);
        imports.add(getTableType());
    }

    /**
     * @return the name of the table class (e.g. Table or VirtualTable)
     */
    public DeclaredTypeName getTableType() {
        return isVirtualTable() ? TypeConstants.VIRTUAL_TABLE : TypeConstants.TABLE;
    }
}

@@ -317,16 +317,63 @@
 elSpec;%0A
+import com.yahoo.squidb.processor.SqlKeywords;%0A
 import c
@@ -413,16 +413,16 @@
 stants;%0A
-
 import c
@@ -542,16 +542,47 @@
 Element;
+%0Aimport javax.tools.Diagnostic;
 %0A%0Apublic
@@ -753,24 +753,24 @@
 ls utils) %7B%0A
-
         supe
@@ -830,16 +830,996 @@
 utils);%0A
+        checkTableName();%0A    %7D%0A%0A    private void checkTableName() %7B%0A        String tableName = getSpecAnnotation().tableName();%0A        if (tableName.toLowerCase().startsWith(%22sqlite_%22)) %7B%0A            logError(%22Table names cannot start with 'sqlite_'; such names are reserved for internal use%22,%0A                    getModelSpecElement());%0A        %7D else if (SqlKeywords.isKeyword(tableName)) %7B%0A            if (SqlKeywords.isRestrictedKeyword(tableName)) %7B%0A                logError(%22Table name '%22 + tableName + %22' is a reserved SQLite keyword that cannot be %22%0A                        + %22used as a table name%22, getModelSpecElement());%0A            %7D else %7B%0A                utils.getMessager().printMessage(Diagnostic.Kind.WARNING, %22Table name '%22 + tableName + %22' is a SQLite %22%0A                        + %22keyword. It is allowed as a table name but it is recommended you choose a non-keyword %22%0A                        + %22name instead%22, getModelSpecElement());%0A            %7D%0A        %7D%0A
     %7D%0A%0A 

Validate table names against keywords or the 'sqlite_' prefix