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

br.com.objectos.schema.info.ThisPrimaryKeyInfo 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.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import br.com.objectos.code.MethodInfo;
import br.com.objectos.code.TypeInfo;
import br.com.objectos.core.util.ImmutableList;
import br.com.objectos.lazy.Lazy;
import br.com.objectos.schema.annotation.PrimaryKey;
import br.com.objectos.schema.meta.PrimaryKeyClassArray;
import br.com.objectos.schema.meta.PrimaryKeyName;

import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;

/**
 * @author [email protected] (Marcio Endo)
 */
abstract class ThisPrimaryKeyInfo extends PrimaryKeyInfo {

  private static final ThisPrimaryKeyInfo EMPTY = new EmptyPrimaryKey();

  private ThisPrimaryKeyInfo() {
  }

  public static ThisPrimaryKeyInfo empty() {
    return EMPTY;
  }

  public static Optional of(SchemaNameTypeInfo schemaName, TypeInfo typeInfo) {
    TableNameTypeInfo tableName = schemaName.tableName(typeInfo);

    List keyPartList = tableName.columnInfoMethodInfoStream()
        .filter(ColumnInfoMethodInfo::primaryKey)
        .map(ColumnInfoMethodInfo::toKeyPart)
        .collect(Collectors.toList());

    return keyPartList.size() > 0
        ? ofMethod(tableName, keyPartList)
        : ofType(schemaName, typeInfo);
  }

  private static Optional ofMethod(TableNameTypeInfo tableName, List keyPartList) {
    ThisPrimaryKeyInfo value = new MethodPrimaryKey(tableName.simpleName() + "_PK", keyPartList);
    return Optional.of(value);
  }

  private static Optional ofType(SchemaNameTypeInfo schemaName, TypeInfo typeInfo) {
    return typeInfo.declaredTypeInfoStream()
        .filter(it -> it.hasAnnotation(PrimaryKey.class))
        .findFirst()
        .map(it -> new TypeInfoPrimaryKey(schemaName, typeInfo, it));
  }

  public List annotationSpecList(ClassName tableClassName) {
    return ImmutableList.of(primaryKeyNameAnnotation(), primaryKeyClassArrayAnnotation(tableClassName));
  }

  @Override
  public String toString() {
    return name();
  }

  @Override
  List keyPartList() {
    return ImmutableList.of();
  }

  @Override
  String name() {
    return "";
  }

  private List identifierList() {
    return keyPartList().stream()
        .map(KeyPartColumnInfoMethodInfo::identifier)
        .collect(Collectors.toList());
  }

  private AnnotationSpec primaryKeyClassArrayAnnotation(ClassName tableClassName) {
    return AnnotationSpec.builder(PrimaryKeyClassArray.class)
        .addMember("value", primaryKeyClassArrayAnnotationValue(tableClassName))
        .build();
  }

  private AnnotationSpec primaryKeyNameAnnotation() {
    return AnnotationSpec.builder(PrimaryKeyName.class)
        .addMember("value", "$S", name())
        .build();
  }

  private CodeBlock primaryKeyClassArrayAnnotationValue(ClassName tableClassName) {
    CodeBlock.Builder columnAnnotationClassArray = CodeBlock.builder();
    columnAnnotationClassArray.add("{ ");
    List identifierList = identifierList();
    int size = identifierList.size();
    int index = 1;
    for (String identifier : identifierList) {
      ClassName annotationClassName = tableClassName.nestedClass(identifier);
      columnAnnotationClassArray.add("$T.class", annotationClassName);
      if (index++ != size) {
        columnAnnotationClassArray.add(", ");
      }
    }
    return columnAnnotationClassArray
        .add(" }")
        .build();
  }

  private static class EmptyPrimaryKey extends ThisPrimaryKeyInfo {

    @Override
    public List annotationSpecList(ClassName tableClassName) {
      return ImmutableList.of();
    }

  }

  private static class MethodPrimaryKey extends ThisPrimaryKeyInfo {

    private final String name;
    private final List keyPartList;

    public MethodPrimaryKey(String name, List keyPartList) {
      this.name = name;
      this.keyPartList = keyPartList;
    }

    @Override
    List keyPartList() {
      return keyPartList;
    }

    @Override
    String name() {
      return name;
    }

  }

  private static class TypeInfoPrimaryKey extends ThisPrimaryKeyInfo {

    private final SchemaNameTypeInfo schemaName;
    private final TypeInfo tableTypeInfo;
    private final TypeInfo typeInfo;

    private final Lazy> indexColumnInfoList = new LazyIndexColumnInfoList();

    public TypeInfoPrimaryKey(SchemaNameTypeInfo schemaName, TypeInfo tableTypeInfo, TypeInfo typeInfo) {
      this.schemaName = schemaName;
      this.tableTypeInfo = tableTypeInfo;
      this.typeInfo = typeInfo;
    }

    @Override
    List keyPartList() {
      return indexColumnInfoList.get();
    }

    @Override
    String name() {
      return typeInfo.simpleName();
    }

    private class LazyIndexColumnInfoList extends Lazy> {
      @Override
      protected List compute() {
        Set nameSet = typeInfo.methodInfoStream()
            .map(MethodInfo::name)
            .collect(Collectors.toSet());

        return schemaName.tableName(tableTypeInfo)
            .columnInfoMethodInfoStream()
            .filter(m -> m.hasMethodName(nameSet))
            .map(ColumnInfoMethodInfo::toKeyPart)
            .collect(Collectors.toList());
      }
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy