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