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

br.com.objectos.way.sql.compiler.PojoMethod Maven / Gradle / Ivy

The 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.way.sql.compiler;

import java.util.List;

import br.com.objectos.way.code.AnnotationInfo;
import br.com.objectos.way.code.ClassInfo;
import br.com.objectos.way.code.TypeInfo;
import br.com.objectos.way.core.testing.Testable;
import br.com.objectos.way.core.testing.Testables;
import br.com.objectos.way.core.tmpl.mustache.IsMustacheSerializable;
import br.com.objectos.way.core.tmpl.mustache.MustacheObject;
import br.com.objectos.way.core.tmpl.mustache.Mustaches;
import br.com.objectos.way.core.util.WayIterables;
import br.com.objectos.way.sql.Column;

import com.google.common.base.Function;
import com.google.common.base.Optional;

/**
 * @author [email protected] (Patricia Nascimento)
 */
abstract class PojoMethod implements Testable, IsMustacheSerializable {

  final SqlColumn sqlColumn;

  PojoMethod(SqlColumn sqlColumn) {
    this.sqlColumn = sqlColumn;
  }

  public static List listOf(TypeInfo typeInfo) {
    List sqlColumnList = SqlColumn.columnListOf(typeInfo);
    return listOf(sqlColumnList);
  }

  public static List listOf(ClassInfo classInfo) {
    List sqlColumnList = SqlColumn.columnListOf(classInfo);
    return listOf(sqlColumnList);
  }

  public static PojoMethod wrap(SqlColumn sqlColumn) {
    Optional maybeForeignKey = sqlColumn.getForeignKey();
    if (maybeForeignKey.isPresent()) {
      SqlColumn foreignKey = maybeForeignKey.get();
      return new PojoMethodForeignKey(sqlColumn, foreignKey);
    }

    if (sqlColumn.isGeneratedValue()) {
      return new PojoMethodGeneratedValue(sqlColumn);
    }

    return new PojoMethodDefault(sqlColumn);
  }

  private static List listOf(List sqlColumnList) {
    return WayIterables.from(sqlColumnList)
        .transform(new Function() {
          @Override
          public PojoMethod apply(SqlColumn input) {
            return input.toPojoMethod();
          }
        })
        .toImmutableList();
  }

  public Optional getColumnAnnotation() {
    return sqlColumn.methodInfo().annotationInfoMap().getFirstAnnotatedWith(Column.class);
  }

  @Override
  public boolean isEqual(PojoMethod that) {
    return Testables.isEqualHelper()
        .equal(sqlColumn, that.sqlColumn)
        .result();
  }

  public boolean isGeneratedValue() {
    return sqlColumn.isGeneratedValue();
  }

  @Override
  public MustacheObject toMustache() {
    return Mustaches.toMustacheHelper()
        .add("binder", binder())
        .add("generatedValue", isGeneratedValue())
        .toMustache();
  }

  abstract String binder();

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy