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

br.com.objectos.way.orm.compiler.OrmProperty Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2014-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.way.orm.compiler;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

import br.com.objectos.way.code.AnnotationInfo;
import br.com.objectos.way.code.SimpleTypeInfo;
import br.com.objectos.way.pojo.plugin.Property;
import br.com.objectos.way.schema.info.TableInfoAnnotationInfo;
import br.com.objectos.way.schema.meta.ColumnAnnotation;
import br.com.objectos.way.schema.meta.ColumnClass;
import br.com.objectos.way.schema.meta.ForeignKeyAnnotation;
import br.com.objectos.way.testable.Testable;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.TypeName;

/**
 * @author [email protected] (Marcio Endo)
 */
abstract class OrmProperty implements Comparable, Testable {

  abstract Property property();
  abstract ReturnType returnType();
  abstract TableInfoAnnotationInfo tableInfo();
  abstract List columnAnnotationClassList();
  abstract int columnSeq();

  OrmProperty() {
  }

  public static Optional of(Property property) {
    Optional columnAnnotation = property.annotationInfoAnnotatedWith(ColumnAnnotation.class);

    if (columnAnnotation.isPresent()) {
      return columnAnnotation.map(ann -> ColumnOrmProperty.of(property, ann));
    }

    Optional foreignKey = property.annotationInfoAnnotatedWith(ForeignKeyAnnotation.class);

    if (foreignKey.isPresent()) {
      return foreignKey.map(ann -> ForeignKeyOrmProperty.of(property, ann));
    }

    return Optional.empty();
  }

  public abstract void acceptColumnsConstructor(ColumnsConstructor constructor);

  public abstract void acceptForeignKeyColumnsConstructor(ForeignKeyColumnsConstructor constructor);

  public void acceptIsOrmInsertableHelper(IsOrmInsertableHelper helper) {
    helper.addColumnClassNameStream(columnClassNameStream());

    if (isGenerated()) {
      helper.addGeneratedKeyListenerName(property().name());
    }
  }

  public abstract void acceptOrmPropertyHelper(OrmPropertyHelper helper);

  public final void acceptSetterMethodBody(CodeBlock.Builder body, Map parameterMap) {
    SetterParameter parameter = parameterMap.get(this);
    if (parameter != null) {
      acceptSetterMethodBody(body, parameter);
    } else {
      body.add(",\n    $L", name());
    }
  }

  public abstract  T adapt(OrmPropertyAdapter adapter);

  public Stream columnAnnotationClassStream() {
    return columnAnnotationClassList().stream();
  }

  @Override
  public int compareTo(OrmProperty o) {
    return Integer.compare(columnSeq(), o.columnSeq());
  }

  public boolean isGenerated() {
    return false;
  }

  public abstract boolean matches(AnnotationInfo annotationInfo);

  public boolean matchesAny(Set pkNameSet) {
    return false;
  }

  public String name() {
    return property().name();
  }

  public ParameterSpec parameterSpec() {
    String name = property().name();
    return parameterSpec(name);
  }

  public ParameterSpec parameterSpec(String name) {
    SimpleTypeInfo returnTypeInfo = property().returnTypeInfo();
    TypeName typeName = returnTypeInfo.typeName();
    return ParameterSpec.builder(typeName, name).build();
  }

  public abstract String rowConstructorParameterName(AtomicInteger i);

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

  public void where(CodeBlock.Builder expression) {
  }

  abstract void acceptSetterMethodBody(CodeBlock.Builder body, SetterParameter parameter);

  Stream columnClassNameStream() {
    return columnAnnotationClassList().stream()
        .map(SimpleTypeInfo::typeInfo)
        .filter(Optional::isPresent)
        .map(Optional::get)
        .map(typeInfo -> typeInfo.annotationInfo(ColumnClass.class).get())
        .map(ann -> ann.simpleTypeInfoValue("value").get())
        .map(SimpleTypeInfo::className);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy