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

br.com.objectos.way.sql.compiler.PojoLoaderContext 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.transform;

import java.util.List;

import javax.annotation.processing.Processor;

import br.com.objectos.way.code.ClassInfo;
import br.com.objectos.way.code.ImportInfo;
import br.com.objectos.way.code.ImportInfoSet;
import br.com.objectos.way.code.MethodInfo;
import br.com.objectos.way.code.MethodInfoHasModifierInfo;
import br.com.objectos.way.code.MethodInfoHasParameterInfoListSize;
import br.com.objectos.way.code.MethodInfoHasReturnTypeInfo;
import br.com.objectos.way.code.ModifierInfo;
import br.com.objectos.way.code.SimpleTypePrimitives;
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.InstanceLoader;
import br.com.objectos.way.sql.Result;
import br.com.objectos.way.sql.ResultFunction;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;

/**
 * @author [email protected] (Marcio Endo)
 */
class PojoLoaderContext implements Supplier {

  private final Class processorClass;
  private final ClassInfo classInfo;

  public PojoLoaderContext(Class processorClass, ClassInfo classInfo) {
    this.processorClass = processorClass;
    this.classInfo = classInfo;
  }

  @Override
  public MustacheObject get() {
    ImportInfoSet importInfoSet = ImportInfoSet.setOf();
    importInfoSet.add(Result.class);
    importInfoSet.add(ResultFunction.class);

    List methods = classInfo.getMethodInfoIterable()
        .filter(MethodInfoHasModifierInfo.get(ModifierInfo.ABSTRACT))
        .filter(MethodInfoHasReturnTypeInfo.not(SimpleTypePrimitives.VOID))
        .filter(MethodInfoHasParameterInfoListSize.get(0))
        .transform(new MethodInfoToLoaderMethodInfo())
        .toImmutableList();

    List loaders = WayIterables.from(methods)
        .filter(new LoaderMethodInfoNotSqlType())
        .toImmutableList();

    if (!loaders.isEmpty()) {
      importInfoSet.add(InstanceLoader.class);
    }

    for (LoaderMethodInfo loader : loaders) {
      loader.toImportInfo(importInfoSet);
    }

    String loaderParameters = Joiner.on(", ").join(transform(loaders, new LoaderMethodInfoToDeclaration()));
    String loaderParameterNames = Joiner.on(", ").join(transform(loaders, new LoaderMethodInfoToParameter()));

    List imports = importInfoSet.toImportInfoList(classInfo);
    return Mustaches.toMustacheHelper()
        .add("imports", imports)
        .add("emptyImports", imports.isEmpty())
        .add("processorClass", processorClass.getName())
        .add("access", classInfo.getAccessInfo())
        .add("superClass", classInfo)
        .add("methods", methods)
        .add("loaders", loaders)
        .add("emptyLoaders", loaders.isEmpty())
        .add("loaderParameters", loaderParameters)
        .add("loaderParameterNames", loaderParameterNames)
        .toMustache();
  }

  private class LoaderMethodInfoNotSqlType implements Predicate {
    @Override
    public boolean apply(LoaderMethodInfo input) {
      return !input.sqlType();
    }
  }

  private class LoaderMethodInfoToDeclaration implements Function {
    @Override
    public String apply(LoaderMethodInfo input) {
      return input.loaderDeclaration();
    }
  }

  private class LoaderMethodInfoToParameter implements Function {
    @Override
    public String apply(LoaderMethodInfo input) {
      return input.loaderVarName();
    }
  }

  private class MethodInfoToLoaderMethodInfo implements Function {

    private int index = 1;

    @Override
    public LoaderMethodInfo apply(MethodInfo input) {
      return LoaderMethodInfo.wrap(input, index++);
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy