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.ImportDeclaration; 020import japa.parser.ast.Node; 021import japa.parser.ast.body.ClassOrInterfaceDeclaration; 022import japa.parser.ast.body.FieldDeclaration; 023import japa.parser.ast.body.TypeDeclaration; 024import japa.parser.ast.expr.NameExpr; 025import japa.parser.ast.expr.QualifiedNameExpr; 026import japa.parser.ast.expr.SingleMemberAnnotationExpr; 027import org.apache.logging.log4j.Logger; 028import org.apache.logging.log4j.LogManager; 029import org.apache.ojb.broker.metadata.ClassDescriptor; 030import org.apache.ojb.broker.metadata.CollectionDescriptor; 031import org.apache.ojb.broker.metadata.DescriptorRepository; 032import org.kuali.rice.devtools.jpa.eclipselink.conv.ojb.OjbUtil; 033import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.ParserUtil; 034import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.AnnotationResolver; 035import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.Level; 036import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.NodeData; 037 038import java.util.ArrayList; 039import java.util.Collection; 040import java.util.Collections; 041 042public class CustomizerResolver implements AnnotationResolver { 043 private static final Logger LOG = LogManager.getLogger(CustomizerResolver.class); 044 public static final String PACKAGE = "org.eclipse.persistence.annotations"; 045 public static final String SIMPLE_NAME = "Customizer"; 046 047 private final Collection<DescriptorRepository> descriptorRepositories; 048 049 public CustomizerResolver(Collection<DescriptorRepository> descriptorRepositories) { 050 this.descriptorRepositories = descriptorRepositories; 051 } 052 053 @Override 054 public String getFullyQualifiedName() { 055 return PACKAGE + "." + SIMPLE_NAME; 056 } 057 058 @Override 059 public Level getLevel() { 060 return Level.CLASS; 061 } 062 063 @Override 064 public NodeData resolve(Node node, String mappedClass) { 065 if (!(node instanceof ClassOrInterfaceDeclaration)) { 066 throw new IllegalArgumentException("this annotation belongs only on ClassOrInterfaceDeclaration"); 067 } 068 069 final TypeDeclaration dclr = (TypeDeclaration) node; 070 if (!(dclr.getParentNode() instanceof CompilationUnit)) { 071 //handling nested classes 072 return null; 073 } 074 final String name = dclr.getName(); 075 final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString(); 076 final String enclosingClass = pckg + "." + name; 077 final Collection<String> customizedFieldsOnNode = getFieldsOnNode(dclr, getCustomizedFields(mappedClass)); 078 if (customizedFieldsOnNode == null || customizedFieldsOnNode.isEmpty()) { 079 LOG.info(ResolverUtil.logMsgForClass(enclosingClass, mappedClass) + " has no customized fields"); 080 return null; 081 } 082 return new NodeData(new SingleMemberAnnotationExpr(new NameExpr(SIMPLE_NAME), new NameExpr("CreateCustomizerFor" + customizedFieldsOnNode.toString())), 083 new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); 084 } 085 086 private Collection<String> getCustomizedFields(String clazz) { 087 final ClassDescriptor cd = OjbUtil.findClassDescriptor(clazz, descriptorRepositories); 088 if (cd != null) { 089 Collection<String> customizedFields = new ArrayList<String>(); 090 for (CollectionDescriptor cld : (Collection<CollectionDescriptor>) cd.getCollectionDescriptors()) { 091 if (cld.getQueryCustomizer() != null) { 092 customizedFields.add(cld.getAttributeName()); 093 } 094 } 095 return customizedFields; 096 } 097 return Collections.emptySet(); 098 } 099 100 private Collection<String> getFieldsOnNode(TypeDeclaration node, Collection<String> fields) { 101 final Collection<String> fieldsOnNode = new ArrayList<String>(); 102 103 final Collection<FieldDeclaration> fds = ParserUtil.getFieldMembers(node.getMembers()); 104 if (fields != null) { 105 for (FieldDeclaration f : fds) { 106 final String name = ParserUtil.getFieldName(f); 107 if (fields.contains(name)) { 108 fieldsOnNode.add(name); 109 } 110 } 111 } 112 113 return fieldsOnNode; 114 } 115}