All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.schema.info.TableCompiler Maven / Gradle / Ivy

There is a newer version: 0.3.0
Show newest version
/*
 * Copyright 2015 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.schema.info;

import java.util.Optional;

import br.com.objectos.code.AnnotationInfo;
import br.com.objectos.code.Artifact;
import br.com.objectos.code.SimpleTypeInfo;
import br.com.objectos.code.TypeInfo;
import br.com.objectos.schema.annotation.Migration;
import br.com.objectos.schema.annotation.Schema;

import com.squareup.javapoet.JavaFile;

/**
 * @author [email protected] (Marcio Endo)
 */
public class TableCompiler {

  private TableCompiler() {
  }

  public static Artifact generate(TypeInfo typeInfo) {
    if (!typeInfo.isInterface()) {
      typeInfo.compilationError("@Table must be an interface.");
      return Artifact.empty();
    }

    Optional maybeEnclosingType = typeInfo.enclosingTypeInfo();
    if (!maybeEnclosingType.isPresent()) {
      typeInfo.compilationError("@Table interfaces must be defined in a @Migration class.");
      return Artifact.empty();
    }

    TypeInfo migrationTypeInfo = maybeEnclosingType.get();
    Optional maybeMigrationAnnotationInfo = migrationTypeInfo.annotationInfo(Migration.class);
    if (!maybeMigrationAnnotationInfo.isPresent()) {
      typeInfo.compilationError("@Table interfaces must be defined in a @Migration class.");
      return Artifact.empty();
    }

    TypeInfo schemaTypeInfo = maybeMigrationAnnotationInfo.get()
        .simpleTypeInfoValue("schema")
        .flatMap(SimpleTypeInfo::typeInfo)
        .get();
    if (!schemaTypeInfo.hasAnnotation(Schema.class)) {
      typeInfo.compilationError("@Migration must be associated to a @Schema class.");
      return Artifact.empty();
    }

    JavaFile file = SchemaNameTypeInfo.of(schemaTypeInfo)
        .tableInfo(typeInfo)
        .toMigrationTableType()
        .generate();
    return Artifact.of(file);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy