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