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.ImportDeclaration;
019import japa.parser.ast.expr.MemberValuePair;
020import japa.parser.ast.expr.NameExpr;
021import japa.parser.ast.expr.NormalAnnotationExpr;
022import japa.parser.ast.expr.QualifiedNameExpr;
023import japa.parser.ast.expr.StringLiteralExpr;
024
025import java.util.Collection;
026import java.util.Collections;
027
028import org.apache.commons.lang.StringUtils;
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.apache.ojb.broker.metadata.DescriptorRepository;
032import org.apache.ojb.broker.metadata.FieldDescriptor;
033import org.kuali.rice.devtools.jpa.eclipselink.conv.ojb.OjbUtil;
034import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.NodeData;
035
036public class GeneratedValueResolver extends AbstractMappedFieldResolver {
037    private static final Log LOG = LogFactory.getLog(GeneratedValueResolver.class);
038
039    public static final String PACKAGE = "javax.persistence";
040    public static final String SIMPLE_NAME = "GeneratedValue";
041
042    private final boolean upperCaseTableName;
043
044    public GeneratedValueResolver(Collection<DescriptorRepository> descriptorRepositories, boolean upperCaseTableName) {
045        super(descriptorRepositories);
046        this.upperCaseTableName = upperCaseTableName;
047    }
048
049    @Override
050    public String getFullyQualifiedName() {
051        return PACKAGE + "." + SIMPLE_NAME;
052    }
053
054    @Override
055    protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) {
056        final FieldDescriptor fd = OjbUtil.findFieldDescriptor(mappedClass, fieldName, descriptorRepositories);
057
058        if (fd != null) {
059            final boolean autoInc = fd.isAutoIncrement();
060            final String seqName = fd.getSequenceName();
061            if (autoInc && StringUtils.isBlank(seqName)) {
062                LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " field has autoincrement set to true but sequenceName is blank.");
063            }
064
065            if (!autoInc && StringUtils.isNotBlank(seqName)) {
066                LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " field has autoincrement set to false but sequenceName is " + seqName + ".");
067            }
068            if (autoInc || StringUtils.isNotBlank(seqName)) {
069                return new NodeData(new NormalAnnotationExpr(new NameExpr(SIMPLE_NAME), Collections.singletonList(new MemberValuePair("generator", new StringLiteralExpr(upperCaseTableName ? seqName.toUpperCase() : seqName)))),
070                        new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false));
071            }
072        }
073        return null;
074    }
075}