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.BlockComment;
019import japa.parser.ast.Comment;
020import japa.parser.ast.ImportDeclaration;
021import japa.parser.ast.expr.AnnotationExpr;
022import japa.parser.ast.expr.ArrayInitializerExpr;
023import japa.parser.ast.expr.Expression;
024import japa.parser.ast.expr.NameExpr;
025import japa.parser.ast.expr.QualifiedNameExpr;
026import japa.parser.ast.expr.SingleMemberAnnotationExpr;
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.apache.ojb.broker.metadata.DescriptorRepository;
030import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.NodeData;
031
032import java.util.Arrays;
033import java.util.Collection;
034import java.util.Collections;
035import java.util.List;
036
037public class JoinColumnsResolver extends AbstractJoinColumnResolver {
038    private static final Log LOG = LogFactory.getLog(JoinColumnsResolver.class);
039
040    public static final String PACKAGE = "javax.persistence";
041    public static final String SIMPLE_NAME = "JoinColumns";
042
043    public JoinColumnsResolver(Collection<DescriptorRepository> descriptorRepositories) {
044        super(descriptorRepositories);
045    }
046    @Override
047    public String getFullyQualifiedName() {
048        return PACKAGE + "." + SIMPLE_NAME;
049    }
050
051    @Override
052    protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) {
053        final List<Expression> joinColumns = getJoinColumns(enclosingClass, fieldName, mappedClass);
054        if (joinColumns != null && joinColumns.size() > 1) {
055            final Comment fixme = new BlockComment("\nFIXME: JPA_CONVERSION\n"
056                    + "For compound primary keys, make sure the join columns are in the correct order.\n");
057            AnnotationExpr
058                    annotation = new SingleMemberAnnotationExpr(new NameExpr(SIMPLE_NAME), new ArrayInitializerExpr(joinColumns));
059            annotation.setComment(fixme);
060            return new NodeData(annotation,
061                    new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false),
062                    Arrays.asList(
063                              new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PrimaryKeyJoinColumnResolver.PACKAGE),PrimaryKeyJoinColumnResolver.SIMPLE_NAME), false, false)
064                            , new ImportDeclaration(new QualifiedNameExpr(new NameExpr(JoinColumnResolver.PACKAGE),JoinColumnResolver.SIMPLE_NAME), false, false)
065                            ));
066        }
067
068        return null;
069    }
070}