
br.com.objectos.way.sql.compiler.QueryOrderBy 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 br.com.objectos.way.code.AnnotationInfo;
import br.com.objectos.way.code.AnnotationInfoMap;
import br.com.objectos.way.code.AnnotationValueInfo;
import br.com.objectos.way.code.MethodInfo;
import br.com.objectos.way.code.SimpleTypeInfo;
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.sql.Column;
import br.com.objectos.way.sql.SortOrder;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
/**
* @author [email protected] (Marcio Endo)
*/
class QueryOrderBy implements IsMustacheSerializable {
private final List columnList;
private QueryOrderBy(List columnList) {
this.columnList = columnList;
}
public static QueryOrderBy wrap(MethodInfo methodInfo) {
AnnotationInfoMap annotationInfoMap = methodInfo.annotationInfoMap();
List columnList = annotationInfoMap.getAnnotatedWith(Column.class);
return new QueryOrderBy(columnList);
}
@Override
public MustacheObject toMustache() {
return Mustaches.toMustacheHelper()
.add("declaration", declaration())
.add("render", render())
.toMustache();
}
private String declaration() {
List declarationList = transform(columnList, new ToDeclaration());
return Joiner.on(", ").join(declarationList);
}
private boolean render() {
return columnList.size() > 0;
}
private class ToDeclaration implements Function {
@Override
public String apply(AnnotationInfo input) {
SimpleTypeInfo table = input.getEnclosingTypeInfo().get();
AnnotationInfo column = input.annotationInfoMap().get(Column.class).get();
Optional maybeColumnName = column.getValue("name");
AnnotationValueInfo columnName = maybeColumnName.get();
Optional maybeOrderBy = input.getValue("orderBy");
AnnotationValueInfo orderBy = maybeOrderBy.get();
SortOrder sortOrder = orderBy.getEnumValue(SortOrder.class);
return String.format("%s.%s().%s()",
table.getSimpleName(), columnName.getStringValue(), sortOrder.name().toLowerCase());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy