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