PageRenderTime 2514ms CodeModel.GetById 202ms RepoModel.GetById 110ms app.codeStats 0ms

/activeobjects-plugin/src/main/java/com/atlassian/activeobjects/NamesLengthAndOracleReservedWordsEntitiesValidator.java

https://bitbucket.org/activeobjects/ao-plugin
Java | 90 lines | 78 code | 12 blank | 0 comment | 14 complexity | 622ff2ea2c8910f79fcbbf525ed4c82a MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.activeobjects;
  2. import com.atlassian.activeobjects.external.IgnoreReservedKeyword;
  3. import com.atlassian.plugin.PluginException;
  4. import com.google.common.base.Predicate;
  5. import com.google.common.collect.ImmutableSet;
  6. import net.java.ao.ActiveObjectsException;
  7. import net.java.ao.Common;
  8. import net.java.ao.Polymorphic;
  9. import net.java.ao.RawEntity;
  10. import net.java.ao.schema.FieldNameConverter;
  11. import net.java.ao.schema.Ignore;
  12. import net.java.ao.schema.NameConverters;
  13. import net.java.ao.schema.TableNameConverter;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import java.lang.reflect.Method;
  17. import java.util.Set;
  18. import static com.google.common.collect.Iterables.any;
  19. public final class NamesLengthAndOracleReservedWordsEntitiesValidator implements EntitiesValidator {
  20. static final Set<String> RESERVED_WORDS = ImmutableSet.of("BLOB", "CLOB", "NUMBER", "ROWID", "TIMESTAMP", "VARCHAR2");
  21. static final int MAX_NUMBER_OF_ENTITIES = 200;
  22. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  23. @Override
  24. public Set<Class<? extends RawEntity<?>>> check(Set<Class<? extends RawEntity<?>>> entityClasses, NameConverters nameConverters) {
  25. if (entityClasses.size() > MAX_NUMBER_OF_ENTITIES) {
  26. throw new PluginException("Plugins are allowed no more than " + MAX_NUMBER_OF_ENTITIES + " entities!");
  27. }
  28. for (Class<? extends RawEntity<?>> entityClass : entityClasses) {
  29. check(entityClass, nameConverters);
  30. }
  31. return entityClasses;
  32. }
  33. void check(Class<? extends RawEntity<?>> entityClass, NameConverters nameConverters) {
  34. checkTableName(entityClass, nameConverters.getTableNameConverter());
  35. final FieldNameConverter fieldNameConverter = nameConverters.getFieldNameConverter();
  36. for (Method method : entityClass.getMethods()) {
  37. checkColumnName(method, fieldNameConverter);
  38. checkPolymorphicColumnName(method, fieldNameConverter);
  39. }
  40. }
  41. void checkTableName(Class<? extends RawEntity<?>> entityClass, TableNameConverter tableNameConverter) {
  42. final String tableName = tableNameConverter.getName(entityClass);// will throw an exception if the entity name is too long
  43. if (isReservedWord(tableName)) {
  44. throw new ActiveObjectsException("Entity class' '" + entityClass.getName() + "' table name is " + tableName + " which is a reserved word!");
  45. }
  46. }
  47. void checkColumnName(Method method, FieldNameConverter fieldNameConverter) {
  48. if ((Common.isAccessor(method) || Common.isMutator(method)) && !method.isAnnotationPresent(Ignore.class)) {
  49. final String columnName = fieldNameConverter.getName(method);
  50. if (isReservedWord(columnName)) {
  51. if (method.isAnnotationPresent(IgnoreReservedKeyword.class)) {
  52. logger.warn("Method " + method + " is annotated with " + IgnoreReservedKeyword.class.getName() + ", it may cause issue on Oracle. " +
  53. "You should change this column name to a non-reserved keyword! "
  54. + "The list of reserved keywords is the following: " + RESERVED_WORDS);
  55. } else {
  56. throw new ActiveObjectsException("Method '" + method + "' column name is " + columnName + " which is a reserved word!");
  57. }
  58. }
  59. }
  60. }
  61. private boolean isReservedWord(final String name) {
  62. return any(RESERVED_WORDS, new Predicate<String>() {
  63. @Override
  64. public boolean apply(String reservedWord) {
  65. return reservedWord.equalsIgnoreCase(name);
  66. }
  67. });
  68. }
  69. void checkPolymorphicColumnName(Method method, FieldNameConverter fieldNameConverter) {
  70. final Class<?> attributeTypeFromMethod = Common.getAttributeTypeFromMethod(method);
  71. if (attributeTypeFromMethod != null && attributeTypeFromMethod.isAnnotationPresent(Polymorphic.class)) {
  72. final String polyTypeName = fieldNameConverter.getPolyTypeName(method);
  73. if (isReservedWord(polyTypeName)) {
  74. throw new ActiveObjectsException("Method '" + method + "' polymorphic column name is " + polyTypeName + " which is a reserved word!");
  75. }
  76. }
  77. }
  78. }