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.MemberValuePair;
024import japa.parser.ast.expr.NameExpr;
025import japa.parser.ast.expr.NormalAnnotationExpr;
026import japa.parser.ast.expr.QualifiedNameExpr;
027import japa.parser.ast.expr.StringLiteralExpr;
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.apache.ojb.broker.metadata.ClassDescriptor;
031import org.apache.ojb.broker.metadata.DescriptorRepository;
032import org.kuali.rice.devtools.jpa.eclipselink.conv.ojb.OjbUtil;
033import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.AnnotationResolver;
034import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.Level;
035import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.NodeData;
036
037import java.util.Collection;
038import java.util.Collections;
039
040public class TableResolver implements AnnotationResolver {
041
042    private static final Log LOG = LogFactory.getLog(TableResolver.class);
043
044    public static final String PACKAGE = "javax.persistence";
045    public static final String SIMPLE_NAME = "Table";
046
047    private final Collection<DescriptorRepository> descriptorRepositories;
048    private final boolean upperCaseTableName;
049
050    public TableResolver(Collection<DescriptorRepository> descriptorRepositories, boolean upperCaseTableName) {
051        this.descriptorRepositories = descriptorRepositories;
052        this.upperCaseTableName = upperCaseTableName;
053    }
054
055    @Override
056    public String getFullyQualifiedName() {
057        return PACKAGE + "." + SIMPLE_NAME;
058    }
059
060    @Override
061    public Level getLevel() {
062        return Level.CLASS;
063    }
064
065    @Override
066    public NodeData resolve(Node node, String mappedClass) {
067        if (!(node instanceof ClassOrInterfaceDeclaration)) {
068            throw new IllegalArgumentException("this annotation belongs only on ClassOrInterfaceDeclaration");
069        }
070
071        final TypeDeclaration dclr = (TypeDeclaration) node;
072        if (!(dclr.getParentNode() instanceof CompilationUnit)) {
073            //handling nested classes
074            return null;
075        }
076        final String name = dclr.getName();
077        final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString();
078        final String enclosingClass = pckg + "." + name;
079
080        final ClassDescriptor cd = OjbUtil.findClassDescriptor(enclosingClass, descriptorRepositories);
081        if (cd != null) {
082            final String tableName = getMappedTable(enclosingClass);
083            if (tableName == null) {
084                LOG.error(ResolverUtil.logMsgForClass(enclosingClass, mappedClass) + " table could not be found");
085                return null;
086            }
087
088            return new NodeData(new NormalAnnotationExpr(new NameExpr(SIMPLE_NAME), Collections.singletonList(new MemberValuePair("name", new StringLiteralExpr(upperCaseTableName ? tableName.toUpperCase() : tableName)))),
089                    new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false));
090        }
091        return null;
092    }
093
094    private String getMappedTable(String clazz) {
095        final ClassDescriptor cd = OjbUtil.findClassDescriptor(clazz, descriptorRepositories);
096        if (cd != null) {
097            return cd.getFullTableName();
098        }
099        return null;
100    }
101}