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

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

import java.util.List;
import java.util.Set;

import br.com.objectos.way.code.ClassInfo;
import br.com.objectos.way.code.CodeCanvasArtifact;
import br.com.objectos.way.code.CodeCanvasWriter;
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.ParameterInfo;
import br.com.objectos.way.code.mustache.Writers;
import br.com.objectos.way.core.tmpl.mustache.IsMustacheSerializable;
import br.com.objectos.way.core.tmpl.mustache.MustacheObject;
import br.com.objectos.way.sql.Transaction;

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

/**
 * @author [email protected] (Marcio Endo)
 */
class QueryMethod implements IsMustacheSerializable {

  private final ClassInfo classInfo;
  private final MethodInfo methodInfo;

  public QueryMethod(ClassInfo classInfo, MethodInfo methodInfo) {
    this.classInfo = classInfo;
    this.methodInfo = methodInfo;
  }

  public static QueryMethod wrap(ClassInfo classInfo, MethodInfo methodInfo) {
    return new QueryMethod(classInfo, methodInfo);
  }

  public boolean isValid() {
    List parameterInfoList = methodInfo.getParameterInfoList();

    if (parameterInfoList.isEmpty()) {
      methodInfo.compilationError("@SqlQuery methods must accept a Transaction parameter.");
      return false;
    }

    ParameterInfo firstParameterInfo = parameterInfoList.get(0);
    if (!firstParameterInfo.isInfoOf(Transaction.class)) {
      methodInfo.compilationError("@SqlQuery methods first parameter must be a Transaction.");
      return false;
    }

    return true;
  }

  @Override
  public MustacheObject toMustache() {
    Optional maybeParameter = QueryParameter.parameterOf(methodInfo);
    return methodInfo.toMustacheHelper()
        .add("cacheKeyType", cacheKeyType(maybeParameter))
        .add("cacheKeyVar", cacheKeyVar(maybeParameter))
        .add("cacheLoaderClassName", cacheLoaderClassName())
        .add("cacheLoaderVarName", cacheLoaderVarName())
        .add("executeParameters", executeParameters())
        .add("sqlClassName", sqlClasName())
        .toMustache();
  }

  public CodeCanvasArtifact toCodeCanvasArtifact() {
    QuerySql context = QuerySql.wrap(classInfo, methodInfo);
    return CodeCanvasWriter.forTemplate("/way-sql-compiler/Query.mustache")
        .namedAfter(classInfo, methodInfo.getClassName() + "Sql")
        .toCodeCanvasArtifact(Writers.INSTANCE, context.toMustache());
  }

  public void addTo(ImportInfoSet importInfoSet) {
    Set importSet = methodInfo.toImportInfoSet();
    importInfoSet.addAll(importSet);
  }

  private String cacheKeyType(Optional maybeParameter) {
    String res = "Boolean";

    if (maybeParameter.isPresent()) {
      QueryParameter parameter = maybeParameter.get();
      res = parameter.getCacheKeyType();
    }

    return res;
  }

  private String cacheKeyVar(Optional maybeParameter) {
    String res = "Boolean.TRUE";

    if (maybeParameter.isPresent()) {
      QueryParameter parameter = maybeParameter.get();
      res = parameter.getCacheKeyVar();
    }

    return res;
  }

  private String cacheLoaderClassName() {
    return methodInfo.getClassName() + "Loader";
  }

  private String cacheLoaderVarName() {
    return methodInfo.name() + "Cache";
  }

  private String executeParameters() {
    List parameterInfoList = methodInfo.getParameterInfoList();
    List parameters = newArrayListWithCapacity(parameterInfoList.size() + 1);

    for (ParameterInfo parameterInfo : parameterInfoList) {
      String name = parameterInfo.getName();
      parameters.add(name);
    }

    parameters.add(1, classInfo.getSimpleName() + "Pojo.this");

    return Joiner.on(", ").join(parameters);
  }

  private String sqlClasName() {
    return classInfo.getSimpleName() + methodInfo.getClassName() + "Sql";
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy