@Test public void determineAnyKeyColumnName() { final ImplicitAnyKeyColumnNameSource source = Mockito.mock(ImplicitAnyKeyColumnNameSource.class); mockContext(source); final AttributePath attributePath= Mockito.mock(AttributePath.class); Mockito.when(attributePath.getProperty()).thenReturn("myProperty"); Mockito.when(source.getAttributePath()).thenReturn(attributePath); final Identifier identifier = new ImplicitNamingStrategyNiceJpaImpl().determineAnyKeyColumnName(source); Assert.assertEquals("my_property", identifier.getText()); }
@Test public void determineJoinColumnName() { final ImplicitJoinColumnNameSource source = Mockito.mock(ImplicitJoinColumnNameSource.class); mockContext(source); Mockito.when(source.getNature()).thenReturn(ImplicitJoinColumnNameSource.Nature.ENTITY); final AttributePath attributePath= Mockito.mock(AttributePath.class); Mockito.when(attributePath.getProperty()).thenReturn("myProperty"); Mockito.when(source.getAttributePath()).thenReturn(attributePath); Mockito.when(source.getReferencedTableName()).thenReturn(DatabaseIdentifier.toIdentifier("MyTa_ble")); final Identifier identifier = new ImplicitNamingStrategyNiceJpaImpl().determineJoinColumnName(source); Assert.assertEquals("my_property", identifier.getText()); }
@Test public void determineJoinTableName() { final ImplicitJoinTableNameSource source = Mockito.mock(ImplicitJoinTableNameSource.class); mockContext(source); final AttributePath attributePath= Mockito.mock(AttributePath.class); Mockito.when(attributePath.getProperty()).thenReturn("myProperty"); Mockito.when(source.getAssociationOwningAttributePath()).thenReturn(attributePath); Mockito.when(source.getOwningPhysicalTableName()).thenReturn("Table1"); final Identifier identifier = new ImplicitNamingStrategyNiceJpaImpl().determineJoinTableName(source); Assert.assertEquals("Table1_my_property", identifier.getText()); }
@Override public Identifier determineBasicColumnName(ImplicitBasicColumnNameSource source) { String columnName = StringHelper.toLowerCase( StringHelper.insertUnderscoreBetweenWords(source.getAttributePath().getProperty()) ); ImplicitBasicColumnNameSource newSource = new ImplicitBasicColumnNameSource() { @Override public AttributePath getAttributePath() { return AttributePath.parse(columnName); } @Override public boolean isCollectionElement() { return source.isCollectionElement(); } @Override public MetadataBuildingContext getBuildingContext() { return source.getBuildingContext(); } }; return super.determineBasicColumnName(newSource); }
public static void process(AttributePath attributePath, StringBuilder sb) { if ( attributePath.getParent() != null ) { process( attributePath.getParent(), sb ); if ( !"".equals( attributePath.getParent().getProperty() ) ) { sb.append( "_" ); } } String property = attributePath.getProperty(); property = property.replace( "<", "" ); property = property.replace( ">", "" ); sb.append( property ); }
@Override protected String transformAttributePath(AttributePath attributePath) { String path = super.transformAttributePath(attributePath); LOGGER.trace("transformAttributePath {} -> {}", attributePath, path); return path; }
@Override protected String transformAttributePath(AttributePath attributePath) { final StringBuilder sb = new StringBuilder(); process( attributePath, sb ); return sb.toString(); }
@Override protected String transformAttributePath(AttributePath attributePath) { String attributeName= super.transformAttributePath(attributePath); return addUnderscores(attributeName); }
@Override public Identifier determineJoinColumnName(ImplicitJoinColumnNameSource source) { Identifier i = super.determineJoinColumnName(source); // RObject, creatorRef.target, oid -> m_object.creatorRef_targetOid // RObjectReference, owner, oid -> m_object_reference.owner_oid // RObjectReference, target, oid -> m_object_reference.targetOid AttributePath path = source.getAttributePath(); String property = path.getProperty(); String columnName = source.getReferencedColumnName().getText(); Identifier real; if (path.getDepth() == 1) { String name; if (property.endsWith("target") && "oid".equals(columnName)) { name = property + "Oid"; } else { name = StringUtils.join(Arrays.asList(property, columnName), "_"); } real = toIdentifier(name, source.getBuildingContext()); } else { // TODO fixme BRUTAL HACK -- we are not able to eliminate columns like 'ownerRefCampaign_targetOid' from the schema (even with @AttributeOverride/@AssociationOverride) if ("ownerRefCampaign.target".equals(path.getFullPath()) || "ownerRefDefinition.target".equals(path.getFullPath()) || "ownerRefTask.target".equals(path.getFullPath())) { path = AttributePath.parse("ownerRef.target"); } AttributePath parent = path.getParent(); String translatedParent = transformAttributePath(parent); columnName = property + StringUtils.capitalize(columnName); real = toIdentifier(StringUtils.join(Arrays.asList(translatedParent, columnName), "_"), source.getBuildingContext()); } LOGGER.trace("determineJoinColumnName {} {} -> {}, {}", source.getReferencedTableName(), source.getReferencedColumnName(), i, real); return real; }
/** * For JPA standards we typically need the unqualified name. However, a more usable * impl tends to use the whole path. This method provides an easy hook for subclasses * to accomplish that * * @param attributePath * The attribute path * @return The extracted name */ @Override protected String transformAttributePath(final AttributePath attributePath) { return StringUtils.lowerCase(StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(attributePath.getProperty()), '_')); }