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.CompilationUnit;
019import japa.parser.ast.Node;
020import japa.parser.ast.body.FieldDeclaration;
021import japa.parser.ast.body.TypeDeclaration;
022import org.apache.ojb.broker.metadata.DescriptorRepository;
023import org.kuali.rice.devtools.jpa.eclipselink.conv.ojb.OjbUtil;
024import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.ParserUtil;
025import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.AnnotationResolver;
026import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.Level;
027import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.NodeData;
028
029import java.util.Collection;
030
031public abstract class AbstractMappedFieldResolver implements AnnotationResolver {
032    protected final Collection<DescriptorRepository> descriptorRepositories;
033
034    public AbstractMappedFieldResolver(Collection<DescriptorRepository> descriptorRepositories) {
035        this.descriptorRepositories = descriptorRepositories;
036    }
037
038    @Override
039    public final Level getLevel() {
040        return Level.FIELD;
041    }
042
043    @Override
044    public final NodeData resolve(Node node, String mappedClass) {
045        if (!(node instanceof FieldDeclaration)) {
046            throw new IllegalArgumentException("this annotation belongs only on FieldDeclaration");
047        }
048
049        final FieldDeclaration field = (FieldDeclaration) node;
050
051        if (ResolverUtil.canFieldBeAnnotated(field)) {
052            final TypeDeclaration dclr = (TypeDeclaration) node.getParentNode();
053
054            final String name = dclr.getName();
055            final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString();
056            final String fullyQualifiedClass = pckg + "." + name;
057            final boolean mappedColumn = OjbUtil.isMappedColumn(mappedClass, ParserUtil.getFieldName(field),
058                    descriptorRepositories);
059            if (mappedColumn) {
060                return getAnnotationNodes(fullyQualifiedClass, ParserUtil.getFieldName(field), mappedClass);
061            }
062        }
063        return null;
064    }
065
066    /**
067     * Override this method to resolve the annotation data by executing annotation specific rules.
068     *
069     * @param enclosingClass the class containing the field
070     * @param fieldName the field name
071     * @param mappedClass the napped class name
072     * @return annotation node data or null if the annotation should not be created.
073     */
074    protected abstract NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass);
075}