cleanup Action enum

This commit is contained in:
Gavin 2023-05-02 15:23:24 +03:00 committed by Gavin King
parent 2538c96cad
commit 18ddbe15d6
1 changed files with 49 additions and 33 deletions

View File

@ -27,7 +27,7 @@ public enum Action {
* <p> * <p>
* Valid in JPA; compatible with Hibernate's HBM2DDL action of the same name. * Valid in JPA; compatible with Hibernate's HBM2DDL action of the same name.
*/ */
NONE( "none" ), NONE,
/** /**
* Create the schema. * Create the schema.
* <p> * <p>
@ -36,7 +36,7 @@ public enum Action {
* *
* @see org.hibernate.tool.schema.spi.SchemaCreator * @see org.hibernate.tool.schema.spi.SchemaCreator
*/ */
CREATE_ONLY( "create", "create-only" ), CREATE_ONLY,
/** /**
* Drop the schema. * Drop the schema.
* <p> * <p>
@ -44,14 +44,14 @@ public enum Action {
* *
* @see org.hibernate.tool.schema.spi.SchemaDropper * @see org.hibernate.tool.schema.spi.SchemaDropper
*/ */
DROP( "drop" ), DROP,
/** /**
* Drop and then recreate the schema. * Drop and then recreate the schema.
* *
* @see org.hibernate.tool.schema.spi.SchemaDropper * @see org.hibernate.tool.schema.spi.SchemaDropper
* @see org.hibernate.tool.schema.spi.SchemaCreator * @see org.hibernate.tool.schema.spi.SchemaCreator
*/ */
CREATE( "drop-and-create", "create" ), CREATE,
/** /**
* Drop the schema and then recreate it on {@code SessionFactory} startup. * Drop the schema and then recreate it on {@code SessionFactory} startup.
* Additionally, drop the schema on {@code SessionFactory} shutdown. * Additionally, drop the schema on {@code SessionFactory} shutdown.
@ -70,7 +70,7 @@ public enum Action {
* @see org.hibernate.tool.schema.spi.SchemaDropper * @see org.hibernate.tool.schema.spi.SchemaDropper
* @see org.hibernate.tool.schema.spi.SchemaCreator * @see org.hibernate.tool.schema.spi.SchemaCreator
*/ */
CREATE_DROP( null, "create-drop" ), CREATE_DROP,
/** /**
* Validate the database schema. * Validate the database schema.
* <p> * <p>
@ -78,7 +78,7 @@ public enum Action {
* *
* @see org.hibernate.tool.schema.spi.SchemaValidator * @see org.hibernate.tool.schema.spi.SchemaValidator
*/ */
VALIDATE( null, "validate" ), VALIDATE,
/** /**
* Update (alter) the database schema. * Update (alter) the database schema.
* <p> * <p>
@ -86,7 +86,7 @@ public enum Action {
* *
* @see org.hibernate.tool.schema.spi.SchemaMigrator * @see org.hibernate.tool.schema.spi.SchemaMigrator
*/ */
UPDATE( null, "update" ), UPDATE,
/** /**
* Truncate the tables in the schema. * Truncate the tables in the schema.
* <p> * <p>
@ -96,31 +96,47 @@ public enum Action {
* *
* @since 6.2 * @since 6.2
*/ */
TRUNCATE( null, null); TRUNCATE;
private final String externalJpaName;
private final String externalHbm2ddlName;
Action(String externalJpaName) {
this( externalJpaName, externalJpaName );
}
Action(String externalJpaName, String externalHbm2ddlName) {
this.externalJpaName = externalJpaName;
this.externalHbm2ddlName = externalHbm2ddlName;
}
public String getExternalJpaName() { public String getExternalJpaName() {
return externalJpaName; switch (this) {
case NONE:
return "none";
case CREATE_ONLY:
return "create";
case DROP:
return "drop";
case CREATE_DROP:
return "drop-and-create";
default:
return null;
}
} }
public String getExternalHbm2ddlName() { public String getExternalHbm2ddlName() {
return externalHbm2ddlName; switch (this) {
case NONE:
return "none";
case CREATE_ONLY:
return "create-only";
case DROP:
return "drop";
case CREATE:
return "create";
case CREATE_DROP:
return "create-drop";
case VALIDATE:
return "validate";
case UPDATE:
return "update";
default:
return null;
}
} }
@Override @Override
public String toString() { public String toString() {
return getClass().getSimpleName() + "(externalJpaName=" + externalJpaName + ", externalHbm2ddlName=" + externalHbm2ddlName + ")"; return getClass().getSimpleName() + "(externalJpaName=" + getExternalJpaName() + ", externalHbm2ddlName=" + getExternalHbm2ddlName() + ")";
} }
/** /**
@ -144,29 +160,29 @@ public enum Action {
} }
final String name = value.toString().trim(); final String name = value.toString().trim();
if ( name.isEmpty() || NONE.externalJpaName.equals( name ) ) { if ( name.isEmpty() || NONE.getExternalJpaName().equals( name ) ) {
// default is NONE // default is NONE
return NONE; return NONE;
} }
// prefer JPA external names // prefer JPA external names
for ( Action action : values() ) { for ( Action action : values() ) {
if ( action.externalJpaName == null ) { if ( action.getExternalJpaName() == null ) {
continue; continue;
} }
if ( action.externalJpaName.equals( name ) ) { if ( action.getExternalJpaName().equals( name ) ) {
return action; return action;
} }
} }
// then check hbm2ddl names // then check hbm2ddl names
for ( Action action : values() ) { for ( Action action : values() ) {
if ( action.externalHbm2ddlName == null ) { if ( action.getExternalHbm2ddlName() == null ) {
continue; continue;
} }
if ( action.externalHbm2ddlName.equals( name ) ) { if ( action.getExternalHbm2ddlName().equals( name ) ) {
return action; return action;
} }
} }
@ -200,29 +216,29 @@ public enum Action {
} }
final String name = value.toString().trim(); final String name = value.toString().trim();
if ( name.isEmpty() || NONE.externalJpaName.equals( name ) ) { if ( name.isEmpty() || NONE.getExternalJpaName().equals( name ) ) {
// default is NONE // default is NONE
return NONE; return NONE;
} }
// prefer hbm2ddl names // prefer hbm2ddl names
for ( Action action : values() ) { for ( Action action : values() ) {
if ( action.externalHbm2ddlName == null ) { if ( action.getExternalHbm2ddlName() == null ) {
continue; continue;
} }
if ( action.externalHbm2ddlName.equals( name ) ) { if ( action.getExternalHbm2ddlName().equals( name ) ) {
return hbm2ddlSetting( action ); return hbm2ddlSetting( action );
} }
} }
// then check JPA external names // then check JPA external names
for ( Action action : values() ) { for ( Action action : values() ) {
if ( action.externalJpaName == null ) { if ( action.getExternalJpaName() == null ) {
continue; continue;
} }
if ( action.externalJpaName.equals( name ) ) { if ( action.getExternalJpaName().equals( name ) ) {
return hbm2ddlSetting( action ); return hbm2ddlSetting( action );
} }
} }