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

br.com.objectos.way.sql.compiler.PojoContext 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 java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import javax.annotation.processing.Processor;

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.mustache.Writers;
import br.com.objectos.way.core.tmpl.mustache.ToMustacheHelper;
import br.com.objectos.way.sql.RuntimeSqlException;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

/**
 * @author [email protected] (Marcio Endo)
 */
class PojoContext extends br.com.objectos.way.code.pojo.PojoContext {

  private final PojoInsert pojoInsert;
  private final List queryMethodList;

  public PojoContext(Class processorClass,
                     ClassInfo classInfo,
                     PojoInsert pojoInsert,
                     List queryMethodList) {
    super(processorClass, classInfo);
    this.pojoInsert = pojoInsert;
    this.queryMethodList = queryMethodList;
  }

  public CodeCanvasArtifact toCodeCanvasArtifact() {
    return CodeCanvasWriter.forTemplate(templateName())
        .namedAfter(classInfo, "Pojo")
        .toCodeCanvasArtifact(Writers.INSTANCE, get());
  }

  @Override
  protected ToMustacheHelper toMustacheHelper() {
    pojoInsert.addTo(importInfoSet);

    if (!queryMethodList.isEmpty()) {
      importInfoSet.add(Cache.class);
      importInfoSet.add(CacheBuilder.class);

      importInfoSet.add(RuntimeSqlException.class);

      importInfoSet.add(Callable.class);
      importInfoSet.add(ExecutionException.class);
      importInfoSet.add(TimeUnit.class);
    }

    for (QueryMethod queryMethod : queryMethodList) {
      queryMethod.addTo(importInfoSet);
    }

    return super.toMustacheHelper()
        .add("isInsertable", pojoInsert.isInsertable())
        .add("hasQueryMethods", hasQueryMethods())
        .add("queryMethods", queryMethodList)
        .add("table", pojoInsert.getTable())
        .add("pojoMethods", pojoInsert.getPojoMethodList());
  }

  private String templateName() {
    return "/way-sql-compiler/Pojo.mustache";
  }

  private boolean hasQueryMethods() {
    return queryMethodList.size() > 0;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy