001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.resolver; 017 018import japa.parser.ast.ImportDeclaration; 019import japa.parser.ast.expr.NameExpr; 020import japa.parser.ast.expr.QualifiedNameExpr; 021import japa.parser.ast.expr.SingleMemberAnnotationExpr; 022import japa.parser.ast.expr.StringLiteralExpr; 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogFactory; 025import org.apache.ojb.broker.metadata.CollectionDescriptor; 026import org.apache.ojb.broker.metadata.DescriptorRepository; 027import org.apache.ojb.broker.metadata.FieldHelper; 028import org.kuali.rice.devtools.jpa.eclipselink.conv.ojb.OjbUtil; 029import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.NodeData; 030 031import java.util.Collection; 032 033public class OrderByResolver extends AbstractMappedFieldResolver { 034 private static final Log LOG = LogFactory.getLog(OrderByResolver.class); 035 036 public static final String PACKAGE = "javax.persistence"; 037 public static final String SIMPLE_NAME = "OrderBy"; 038 039 public OrderByResolver(Collection<DescriptorRepository> descriptorRepositories) { 040 super(descriptorRepositories); 041 } 042 @Override 043 public String getFullyQualifiedName() { 044 return PACKAGE + "." + SIMPLE_NAME; 045 } 046 047 /** gets the annotation but also adds an import in the process if a Convert annotation is required. */ 048 @Override 049 protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) { 050 final CollectionDescriptor cld = OjbUtil.findCollectionDescriptor(mappedClass, fieldName, descriptorRepositories); 051 if (cld != null) { 052 Collection<FieldHelper> orderBy = cld.getOrderBy(); 053 if (orderBy != null && !orderBy.isEmpty()) { 054 String orderByStr = ""; 055 for (FieldHelper fh : orderBy) { 056 orderByStr += fh.name + (fh.isAscending ? "" : " DESC") + ", "; 057 } 058 orderByStr = orderByStr.replaceAll(", $", ""); 059 return new NodeData(new SingleMemberAnnotationExpr(new NameExpr(SIMPLE_NAME), new StringLiteralExpr(orderByStr)), 060 new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); 061 } 062 } 063 return null; 064 } 065}