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.ImportDeclaration; 019import japa.parser.ast.expr.BooleanLiteralExpr; 020import japa.parser.ast.expr.IntegerLiteralExpr; 021import japa.parser.ast.expr.MemberValuePair; 022import japa.parser.ast.expr.NameExpr; 023import japa.parser.ast.expr.NormalAnnotationExpr; 024import japa.parser.ast.expr.QualifiedNameExpr; 025import japa.parser.ast.expr.StringLiteralExpr; 026import org.apache.commons.lang.StringUtils; 027import org.apache.logging.log4j.Logger; 028import org.apache.logging.log4j.LogManager; 029import org.apache.ojb.broker.metadata.DescriptorRepository; 030import org.apache.ojb.broker.metadata.FieldDescriptor; 031import org.kuali.rice.devtools.jpa.eclipselink.conv.ojb.OjbUtil; 032import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.NodeData; 033 034import java.util.ArrayList; 035import java.util.Collection; 036import java.util.List; 037 038public class ColumnResolver extends AbstractMappedFieldResolver { 039 private static final Logger LOG = LogManager.getLogger(ColumnResolver.class); 040 041 public static final String PACKAGE = "javax.persistence"; 042 public static final String SIMPLE_NAME = "Column"; 043 044 private final boolean upperCaseTableName; 045 046 public ColumnResolver(Collection<DescriptorRepository> descriptorRepositories, boolean upperCaseTableName) { 047 super(descriptorRepositories); 048 this.upperCaseTableName = upperCaseTableName; 049 } 050 051 @Override 052 public String getFullyQualifiedName() { 053 return PACKAGE + "." + SIMPLE_NAME; 054 } 055 056 @Override 057 protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) { 058 final FieldDescriptor fd = OjbUtil.findFieldDescriptor(mappedClass, fieldName, descriptorRepositories); 059 060 if (fd != null) { 061 List<MemberValuePair> pairs = new ArrayList<MemberValuePair>(); 062 final String access = fd.getAccess(); 063 if ("readonly".equals(access)) { 064 pairs.add(new MemberValuePair("insertable", new BooleanLiteralExpr(false))); 065 pairs.add(new MemberValuePair("updatable", new BooleanLiteralExpr(false))); 066 } else if ("readwrite".equals(access)) { 067 LOG.debug(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " field access is readwrite keeping @Column attributes (insertable, updatable) at defaults"); 068 } else if ("anonymous".equals(access)) { 069 LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " field access is anonymous, the field should not exist in the java class as is the meaning anonymous access"); 070 } else if (access == null) { 071 LOG.debug(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " field access is null keeping @Column attributes (insertable, updatable) at defaults"); 072 } else { 073 LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " field access is " + access + ", unsupported conversion to @Column attributes"); 074 } 075 076 final String columnName = fd.getColumnName(); 077 if (StringUtils.isNotBlank(columnName)) { 078 pairs.add(new MemberValuePair("name", new StringLiteralExpr(upperCaseTableName ? columnName.toUpperCase() : columnName))); 079 } else { 080 LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " field column is blank"); 081 } 082 /* don't bother with column type attribute...this is mostly taken care of automatically by JPA 083 final String columnType = fd.getColumnType(); 084 if (StringUtils.isNotBlank(columnType)) { 085 LOG.error(enclosingClass + "." + fieldName + " for the mapped class " + mappedClass + " field column type is " + columnType + ", unsupported conversion to @Column attributes"); 086 } 087 */ 088 final boolean required = fd.isRequired(); 089 if (required) { 090 pairs.add(new MemberValuePair("nullable", new BooleanLiteralExpr(false))); 091 } else { 092 LOG.debug(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " field is nullable keeping @Column attribute (nullable) at default"); 093 } 094 095 final int length = fd.getLength(); 096 if (length > 0) { 097 pairs.add(new MemberValuePair("length", new IntegerLiteralExpr(String.valueOf(length)))); 098 } else { 099 LOG.debug(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " field length is not set keeping @Column attribute (length) at default"); 100 } 101 102 final int precision = fd.getPrecision(); 103 if (precision > 0) { 104 pairs.add(new MemberValuePair("precision", new IntegerLiteralExpr(String.valueOf(precision)))); 105 } else { 106 LOG.debug(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " field precision is not set keeping @Column attribute (precision) at default"); 107 } 108 109 final int scale = fd.getScale(); 110 if (scale > 0) { 111 pairs.add(new MemberValuePair("scale", new IntegerLiteralExpr(String.valueOf(scale)))); 112 } else { 113 LOG.debug(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " field scale is not set keeping @Column attribute (scale) at default"); 114 } 115 return new NodeData(new NormalAnnotationExpr(new NameExpr(SIMPLE_NAME), pairs), 116 new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); 117 } 118 return null; 119 } 120}