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