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.Collection;
031import java.util.Collections;
032import java.util.List;
033
034/**
035 * this only does mappings for OneToOne at the field level for compound fks.
036 */
037public class PrimaryKeyJoinColumnsResolver extends AbstractPrimaryKeyJoinColumnResolver {
038
039    public static final String PACKAGE = "javax.persistence";
040    public static final String SIMPLE_NAME = "PrimaryKeyJoinColumns";
041
042    public PrimaryKeyJoinColumnsResolver(Collection<DescriptorRepository> descriptorRepositories) {
043        super(descriptorRepositories);
044    }
045    @Override
046    public String getFullyQualifiedName() {
047        return PACKAGE + "." + SIMPLE_NAME;
048    }
049
050    @Override
051    protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) {
052        final List<Expression> joinColumns = getJoinColumns(enclosingClass, fieldName, mappedClass);
053        if (joinColumns != null && joinColumns.size() > 1) {
054            final Comment fixme = new BlockComment("\nFIXME: JPA_CONVERSION\n"
055                    + "For compound primary keys, make sure the join columns are in the correct order.\n");
056            AnnotationExpr annotation = new SingleMemberAnnotationExpr(new NameExpr(SIMPLE_NAME), new ArrayInitializerExpr(joinColumns));
057            annotation.setComment(fixme);
058            return new NodeData(annotation,
059                    new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false),
060                    Collections.singletonList(new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), "PrimaryKeyJoinColumn"), false, false)));
061        }
062
063        return null;
064    }
065}