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.CompilationUnit; 019import japa.parser.ast.ImportDeclaration; 020import japa.parser.ast.Node; 021import japa.parser.ast.body.ClassOrInterfaceDeclaration; 022import japa.parser.ast.body.TypeDeclaration; 023import japa.parser.ast.expr.MarkerAnnotationExpr; 024import japa.parser.ast.expr.NameExpr; 025import japa.parser.ast.expr.QualifiedNameExpr; 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.apache.ojb.broker.metadata.ClassDescriptor; 029import org.apache.ojb.broker.metadata.DescriptorRepository; 030import org.kuali.rice.devtools.jpa.eclipselink.conv.ojb.OjbUtil; 031import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.AnnotationResolver; 032import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.Level; 033import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.NodeData; 034 035import java.util.Collection; 036 037public class EntityResolver implements AnnotationResolver { 038 private static final Log LOG = LogFactory.getLog(EntityResolver.class); 039 040 public static final String PACKAGE = "javax.persistence"; 041 public static final String SIMPLE_NAME = "Entity"; 042 043 private final Collection<DescriptorRepository> descriptorRepositories; 044 045 public EntityResolver(Collection<DescriptorRepository> descriptorRepositories) { 046 this.descriptorRepositories = descriptorRepositories; 047 } 048 049 @Override 050 public String getFullyQualifiedName() { 051 return PACKAGE + "." + SIMPLE_NAME; 052 } 053 054 @Override 055 public Level getLevel() { 056 return Level.CLASS; 057 } 058 059 @Override 060 public NodeData resolve(Node node, String mappedClass) { 061 if (!(node instanceof ClassOrInterfaceDeclaration)) { 062 throw new IllegalArgumentException("this annotation belongs only on ClassOrInterfaceDeclaration"); 063 } 064 065 final TypeDeclaration dclr = (TypeDeclaration) node; 066 if (!(dclr.getParentNode() instanceof CompilationUnit)) { 067 //handling nested classes 068 return null; 069 } 070 071 final String name = dclr.getName(); 072 final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString(); 073 final String enclosingClass = pckg + "." + name; 074 075 final ClassDescriptor cd = OjbUtil.findClassDescriptor(enclosingClass, descriptorRepositories); 076 if (cd != null) { 077 return new NodeData(new MarkerAnnotationExpr(new NameExpr(SIMPLE_NAME)), 078 new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); 079 } 080 081 return null; 082 } 083}