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.NameExpr; 020import japa.parser.ast.expr.QualifiedNameExpr; 021import japa.parser.ast.expr.SingleMemberAnnotationExpr; 022import org.apache.commons.logging.Log; 023import org.apache.commons.logging.LogFactory; 024import org.apache.ojb.broker.metadata.DescriptorRepository; 025import org.apache.ojb.broker.metadata.FieldDescriptor; 026import org.kuali.rice.devtools.jpa.eclipselink.conv.ojb.OjbUtil; 027import org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.NodeData; 028 029import java.sql.Time; 030import java.sql.Timestamp; 031import java.util.Calendar; 032import java.util.Collection; 033import java.util.Collections; 034 035public class TemporalResolver extends AbstractMappedFieldResolver { 036 private static final Log LOG = LogFactory.getLog(TemporalResolver.class); 037 038 public static final String PACKAGE = "javax.persistence"; 039 public static final String SIMPLE_NAME = "Temporal"; 040 public static final String DATE = "DATE"; 041 public static final String TIMESTAMP = "TIMESTAMP"; 042 public static final String TIME = "TIME"; 043 044 public TemporalResolver(Collection<DescriptorRepository> descriptorRepositories) { 045 super(descriptorRepositories); 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 Class<?> fc = ResolverUtil.getType(enclosingClass, fieldName); 059 final String columnType = fd.getColumnType(); 060 if (isJavaSqlDate(fc)) { 061 LOG.warn(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " is a java.sql.Date. " + getWarnMessageFragment(columnType)); 062 } else if (isJavaSqlTimestamp(fc)) { 063 LOG.warn(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " is a java.sql.Timestamp. " + getWarnMessageFragment(columnType)); 064 } else if (isJavaSqlTime(fc)) { 065 LOG.warn(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " is a java.sql.Time. " + getWarnMessageFragment(columnType)); 066 } else if (isJavaUtilDate(fc) || isJavaUtilCalendar(fc)) { 067 if (DATE.equals(columnType)) { 068 return new NodeData(new SingleMemberAnnotationExpr(new NameExpr(SIMPLE_NAME), new NameExpr("TemporalType.DATE")), 069 new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false), 070 Collections.singletonList(new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), "TemporalType"), false, false))); 071 } else if (TIMESTAMP.equals(columnType)) { 072 return new NodeData(new SingleMemberAnnotationExpr(new NameExpr(SIMPLE_NAME), new NameExpr("TemporalType.TIMESTAMP")), 073 new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false), 074 Collections.singletonList(new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), "TemporalType"), false, false))); 075 } else if (TIME.equals(columnType)) { 076 return new NodeData(new SingleMemberAnnotationExpr(new NameExpr(SIMPLE_NAME), new NameExpr("TemporalType.TIME")), 077 new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false), 078 Collections.singletonList(new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), "TemporalType"), false, false))); 079 } 080 081 LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " is a java.sql.Date or java.util.Calendar but the column type " + columnType + " is unknown. Unable to add @Temporal annotation"); 082 } 083 084 return null; 085 } 086 return null; 087 } 088 089 private boolean isJavaUtilDate(Class<?> fc) { 090 if (fc != null) { 091 return !java.sql.Date.class.isAssignableFrom(fc) && java.util.Date.class.isAssignableFrom(fc); 092 } 093 return false; 094 } 095 096 private boolean isJavaUtilCalendar(Class<?> fc) { 097 if (fc != null) { 098 return Calendar.class.isAssignableFrom(fc); 099 } 100 return false; 101 } 102 103 private boolean isJavaSqlDate(Class<?> fc) { 104 if (fc != null) { 105 return java.sql.Date.class.isAssignableFrom(fc); 106 } 107 return false; 108 } 109 110 private boolean isJavaSqlTimestamp(Class<?> fc) { 111 if (fc != null) { 112 return Timestamp.class.isAssignableFrom(fc); 113 } 114 return false; 115 } 116 117 private boolean isJavaSqlTime(Class<?> fc) { 118 if (fc != null) { 119 return Time.class.isAssignableFrom(fc); 120 } 121 return false; 122 } 123 124 private String getWarnMessageFragment(String columnType) { 125 if (DATE.equals(columnType)) { 126 return "Consider converting to java.util.Calendar or java.util.Date with a @Temporal(TemporalType.DATE) annotation"; 127 } else if (TIMESTAMP.equals(columnType)) { 128 return "Consider converting to java.util.Calendar or java.util.Date with a @Temporal(TemporalType.TIMESTAMP) annotation"; 129 } else if (TIME.equals(columnType)) { 130 return "Consider converting to java.util.Calendar or java.util.Date with a @Temporal(TemporalType.TIME) annotation"; 131 } else { 132 return "Consider converting to java.util.Calendar or java.util.Date with a @Temporal annotation"; 133 } 134 } 135}