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.Node;
020import japa.parser.ast.body.FieldDeclaration;
021import japa.parser.ast.expr.MarkerAnnotationExpr;
022import japa.parser.ast.expr.NameExpr;
023import japa.parser.ast.expr.QualifiedNameExpr;
024import org.apache.ojb.broker.metadata.DescriptorRepository;
025import org.kuali.rice.devtools.jpa.eclipselink.conv.ojb.OjbUtil;
026import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.ParserUtil;
027import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.AnnotationResolver;
028import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.Level;
029import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.NodeData;
030
031import java.util.Collection;
032
033public class TransientResolver implements AnnotationResolver {
034
035    public static final String PACKAGE = "javax.persistence";
036    public static final String SIMPLE_NAME = "Transient";
037
038    private final Collection<DescriptorRepository> descriptorRepositories;
039
040    public TransientResolver(Collection<DescriptorRepository> descriptorRepositories) {
041        this.descriptorRepositories = descriptorRepositories;
042    }
043
044    @Override
045    public String getFullyQualifiedName() {
046        return PACKAGE + "." + SIMPLE_NAME;
047    }
048
049    @Override
050    public Level getLevel() {
051        return Level.FIELD;
052    }
053
054    @Override
055    public NodeData resolve(Node node, String mappedClass) {
056        if (!(node instanceof FieldDeclaration)) {
057            throw new IllegalArgumentException("this annotation belongs only on FieldDeclaration");
058        }
059
060        final FieldDeclaration field = (FieldDeclaration) node;
061
062        if (ResolverUtil.canFieldBeAnnotated(field)) {
063            final boolean mappedColumn = OjbUtil.isMappedColumn(mappedClass, ParserUtil.getFieldName(field),
064                    descriptorRepositories);
065            if (!mappedColumn) {
066                return new NodeData(new MarkerAnnotationExpr(new NameExpr(SIMPLE_NAME)),
067                        new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false));
068            }
069        }
070        return null;
071    }
072}