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