/src/main/java/com/impetus/kundera/metadata/processor/AbstractEntityFieldProcessor.java

https://github.com/kkmishra/Kundera · Java · 147 lines · 91 code · 14 blank · 42 comment · 12 complexity · b09f072cdd971c9687899ac03097f775 MD5 · raw file

  1. /*******************************************************************************
  2. * * Copyright 2011 Impetus Infotech.
  3. * *
  4. * * Licensed under the Apache License, Version 2.0 (the "License");
  5. * * you may not use this file except in compliance with the License.
  6. * * You may obtain a copy of the License at
  7. * *
  8. * * http://www.apache.org/licenses/LICENSE-2.0
  9. * *
  10. * * Unless required by applicable law or agreed to in writing, software
  11. * * distributed under the License is distributed on an "AS IS" BASIS,
  12. * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * * See the License for the specific language governing permissions and
  14. * * limitations under the License.
  15. ******************************************************************************/
  16. package com.impetus.kundera.metadata.processor;
  17. import java.beans.BeanInfo;
  18. import java.beans.IntrospectionException;
  19. import java.beans.Introspector;
  20. import java.beans.PropertyDescriptor;
  21. import java.lang.reflect.Field;
  22. import java.util.Date;
  23. import javax.persistence.Basic;
  24. import javax.persistence.Column;
  25. import javax.persistence.Temporal;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. import com.impetus.kundera.metadata.EntityMetadata;
  29. import com.impetus.kundera.metadata.MetadataProcessor;
  30. /**
  31. * The Class AbstractEntityFieldProcessor.
  32. *
  33. * @author animesh.kumar
  34. */
  35. public abstract class AbstractEntityFieldProcessor implements MetadataProcessor
  36. {
  37. /** The Constant log. */
  38. private static final Log log = LogFactory.getLog(AbstractEntityFieldProcessor.class);
  39. /**
  40. * Gets the valid jpa column.
  41. *
  42. * @param entity
  43. * the entity
  44. * @param f
  45. * the f
  46. *
  47. * @return the valid jpa column
  48. */
  49. protected final String getValidJPAColumnName(Class<?> entity, Field f)
  50. {
  51. String name = null;
  52. if (f.isAnnotationPresent(Column.class))
  53. {
  54. Column c = f.getAnnotation(Column.class);
  55. if (!c.name().isEmpty())
  56. {
  57. name = c.name();
  58. }
  59. else
  60. {
  61. name = f.getName();
  62. }
  63. }
  64. else if (f.isAnnotationPresent(Basic.class))
  65. {
  66. name = f.getName();
  67. }
  68. if (f.isAnnotationPresent(Temporal.class))
  69. {
  70. if (!f.getType().equals(Date.class))
  71. {
  72. log.error("@Temporal must map to java.util.Date for @Entity(" + entity.getName() + "." + f.getName()
  73. + ")");
  74. return name;
  75. }
  76. if (null == name)
  77. {
  78. name = f.getName();
  79. }
  80. }
  81. return name;
  82. }
  83. /**
  84. * Populates @Id accesser methods like, getId and setId of clazz to
  85. * metadata.
  86. *
  87. * @param metadata
  88. * the metadata
  89. * @param clazz
  90. * the clazz
  91. * @param f
  92. * the f
  93. */
  94. protected final void populateIdAccessorMethods(EntityMetadata metadata, Class<?> clazz, Field f)
  95. {
  96. try
  97. {
  98. BeanInfo info = Introspector.getBeanInfo(clazz);
  99. for (PropertyDescriptor descriptor : info.getPropertyDescriptors())
  100. {
  101. if (descriptor.getName().equals(f.getName()))
  102. {
  103. metadata.setReadIdentifierMethod(descriptor.getReadMethod());
  104. metadata.setWriteIdentifierMethod(descriptor.getWriteMethod());
  105. return;
  106. }
  107. }
  108. }
  109. catch (IntrospectionException e)
  110. {
  111. throw new RuntimeException(e);
  112. }
  113. }
  114. protected final void populateIdColumn(EntityMetadata metadata, Class<?> clazz, Field f)
  115. {
  116. if (f.isAnnotationPresent(Column.class))
  117. {
  118. Column c = f.getAnnotation(Column.class);
  119. if (!c.name().isEmpty())
  120. {
  121. metadata.setIdColumn(metadata.new Column(c.name(), f));
  122. }
  123. else
  124. {
  125. metadata.setIdColumn(metadata.new Column(f.getName(), f));
  126. }
  127. }
  128. else
  129. {
  130. metadata.setIdColumn(metadata.new Column(f.getName(), f));
  131. }
  132. }
  133. }