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

br.com.objectos.way.sql.compiler.PojoInsert 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 static com.google.common.collect.Lists.newArrayList;

import java.util.List;

import br.com.objectos.way.code.AnnotationInfo;
import br.com.objectos.way.code.AnnotationInfoGetEnclosingTypeInfo;
import br.com.objectos.way.code.AnnotationInfoGetSimpleName;
import br.com.objectos.way.code.ClassInfo;
import br.com.objectos.way.code.ImportInfoSet;
import br.com.objectos.way.code.SimpleTypeInfo;
import br.com.objectos.way.code.SimpleTypeInfoGetTypeInfo;
import br.com.objectos.way.code.TypeInfo;
import br.com.objectos.way.core.util.WayIterables;
import br.com.objectos.way.sql.InsertValuesExe;
import br.com.objectos.way.sql.InsertValuesSql;
import br.com.objectos.way.sql.IsInsertable;
import br.com.objectos.way.sql.SqlException;
import br.com.objectos.way.sql.Transaction;
import br.com.objectos.way.sql.WaySql;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;

/**
 * @author [email protected] (Marcio Endo)
 */
abstract class PojoInsert {

  private final PojoTableHolder pojoTableHolder;

  PojoInsert(PojoTableHolder pojoTableHolder) {
    this.pojoTableHolder = pojoTableHolder;
  }

  public static PojoInsert wrap(ClassInfo classInfo) {
    final List columnNameList = newArrayList();
    List pojoMethodList = PojoMethod.listOf(classInfo);
    final PojoTableHolder pojoTableHolder = new PojoTableHolder();

    boolean insertable = WayIterables.from(pojoMethodList)
        .transform(PojoMethodGetColumnAnnotationInfo.get())
        .presentInstancesOf(AnnotationInfo.class)
        .toListAndStore(AnnotationInfo.class)

        .transform(AnnotationInfoGetSimpleName.get())
        .appendTo(columnNameList)

        .restore(AnnotationInfo.class)
        .transform(AnnotationInfoGetEnclosingTypeInfo.get())
        .presentInstancesOf(SimpleTypeInfo.class)
        .toSet()
        .transform(SimpleTypeInfoGetTypeInfo.get())
        .presentInstancesOf(TypeInfo.class)
        .asMap(new Function, Boolean>() {
          @Override
          public Boolean apply(List input) {
            Boolean res = Boolean.FALSE;

            if (input.size() == 1) {
              TypeInfo first = input.get(0);
              PojoTable table = new PojoTable(first);
              pojoTableHolder.set(table);
              res = table.getColumnNameList().equals(columnNameList);
            }

            return res;
          }
        }).booleanValue();

    return insertable
        ? new Insertable(pojoTableHolder, pojoMethodList)
        : new NotInsertable(pojoTableHolder);
  }

  public List getPojoMethodList() {
    return ImmutableList.of();
  }

  public PojoTable getTable() {
    return pojoTableHolder.get();
  }

  public abstract boolean isInsertable();

  public void addTo(ImportInfoSet importInfoSet) {
    // noop
  }

  private static class Insertable extends PojoInsert {

    private final List pojoMethodList;

    public Insertable(PojoTableHolder pojoTableHolder, List pojoMethodList) {
      super(pojoTableHolder);
      this.pojoMethodList = pojoMethodList;
    }

    @Override
    public List getPojoMethodList() {
      return pojoMethodList;
    }

    @Override
    public boolean isInsertable() {
      return true;
    }

    @Override
    public void addTo(ImportInfoSet importInfoSet) {
      importInfoSet.add(InsertValuesExe.class);
      importInfoSet.add(InsertValuesSql.class);
      importInfoSet.add(IsInsertable.class);
      importInfoSet.add(SqlException.class);
      importInfoSet.add(Transaction.class);
      importInfoSet.add(WaySql.class);

      getTable().addTo(importInfoSet);
    }

  }

  private static class NotInsertable extends PojoInsert {

    public NotInsertable(PojoTableHolder pojoTableHolder) {
      super(pojoTableHolder);
    }

    @Override
    public boolean isInsertable() {
      return false;
    }

  }

  private static class PojoTableHolder {

    private PojoTable table;

    public PojoTable get() {
      return table;
    }

    public void set(PojoTable table) {
      this.table = table;
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy