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.MemberValuePair;
023import japa.parser.ast.expr.NameExpr;
024import japa.parser.ast.expr.NormalAnnotationExpr;
025import japa.parser.ast.expr.QualifiedNameExpr;
026import org.apache.ojb.broker.metadata.DescriptorRepository;
027import org.apache.ojb.broker.metadata.FieldDescriptor;
028import org.kuali.rice.devtools.jpa.eclipselink.conv.ojb.OjbUtil;
029import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.NodeData;
030
031import java.util.Collection;
032import java.util.Collections;
033
034public class EnumeratedResolver extends AbstractMappedFieldResolver {
035
036    public static final String PACKAGE = "javax.persistence";
037    public static final String SIMPLE_NAME = "Enumerated";
038
039    public EnumeratedResolver(Collection<DescriptorRepository> descriptorRepositories) {
040        super(descriptorRepositories);
041    }
042
043    @Override
044    public String getFullyQualifiedName() {
045        return PACKAGE + "." + SIMPLE_NAME;
046    }
047
048    @Override
049    protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) {
050        final FieldDescriptor fd = OjbUtil.findFieldDescriptor(mappedClass, fieldName, descriptorRepositories);
051
052        if (fd != null) {
053            final Class<?> fc = ResolverUtil.getType(enclosingClass, fieldName);
054
055            if (isEnum(fc)) {
056                final Comment fixme = new BlockComment("\nFIXME:\n" +
057                        "Enums must be annotated with the @Enumerated annotation.\n " +
058                        "The @Enumerated annotation should set the EnumType.\n" +
059                        "If the EnumType is not set, then the Enumerated annotation is defaulted to EnumType.ORDINAL.\n" +
060                        "This conversion program cannot tell whether EnumType.ORDINAL is the appropriate EnumType.");
061                AnnotationExpr enumerated = new NormalAnnotationExpr(new NameExpr(SIMPLE_NAME), Collections.singletonList(new MemberValuePair("value", new NameExpr("EnumType."))));
062                enumerated.setComment(fixme);
063                return new NodeData(enumerated, new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false),
064                    Collections.singletonList(new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), "TemporalType"), false, false)));
065            }
066        }
067        return null;
068    }
069
070    private boolean isEnum(Class<?> fc) {
071        if (fc != null) {
072            return fc.isEnum();
073        }
074        return false;
075    }
076}