Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added generator for microprofile openapi schema #96

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.8.4] - 2024-11-10
### Added

- package-helper-microprofile, to add model wrapper with openapi annotation.

### Fixed

## [1.8.4] - 2024-11-10

- use of LocalDateTime.now() in data facade when 'type-mapper-time-ng' is enabled

## [1.8.3] - 2024-11-09
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ private DaogenCatalogConstants() {}

public static final String GEN_PROP_PACKAGE_MODEL = "package-model";
public static final String GEN_PROP_PACKAGE_HELPER = "package-helper";
public static final String GEN_PROP_PACKAGE_HELPER_MICROPROFILE = "package-helper-microprofile";
public static final String GEN_PROP_PACKAGE_FACADE_DEF = "package-facade-def";
public static final String GEN_PROP_PACKAGE_FACADE_DATA_IMPL = "package-facade-data-impl";
public static final String GEN_PROP_PACKAGE_RSE = "package-rse";
Expand Down Expand Up @@ -111,6 +112,8 @@ private DaogenCatalogConstants() {}
public static final String PREFIX_HELPER = "Helper";

public static final String PREFIX_WRAPPER = "Wrapper";

public static final String PREFIX_MPSCHEMA = "MPSchema";

public static final String PREFIX_RESTLOAD = "Load";

Expand Down Expand Up @@ -149,6 +152,10 @@ public static String rseName( DaogenCatalogEntity entity ) {
public static String wrapperName( DaogenCatalogEntity entity ) {
return PREFIX_WRAPPER+entity.toClassName();
}

public static String mpSchemaName( DaogenCatalogEntity entity ) {
return PREFIX_MPSCHEMA+entity.toClassName();
}

public static String finderlName( DaogenCatalogEntity entity ) {
return entity.toClassName()+SUFFIX_FINDER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ public class DaogenCatalogField extends BasicIdConfigType {
public static final String ATT_COMMENTS = "comments";
public static final String ATT_UDT = "udt";
public static final String ATT_STRUCT_TYPE = "structType";

public static final String ATT_STRUCT_SQL_TYPE = "structSqlType=";
public static final String ATT_VIRTUAL = "virtual";
public static final String ATT_EXAMPLE_DATA = "exampleData";

/**
*
Expand Down Expand Up @@ -48,6 +49,8 @@ public class DaogenCatalogField extends BasicIdConfigType {

private String virtual;

private String exampleData;

public boolean isUserType() {
return BooleanUtils.isTrue( this.getUdt() );
}
Expand Down Expand Up @@ -140,6 +143,15 @@ public void setVirtual(String virtual) {
this.virtual = virtual;
}

public String getExampleData() {
return exampleData;
}

public void setExampleData(String exampleData) {
this.exampleData = exampleData;
}


public String describe() {
List<String> list = new ArrayList<>();
list.add( StringUtils.concat( ":" , ATT_SQL_TYPE, this.getSqlType() ) );
Expand All @@ -151,6 +163,7 @@ public String describe() {
list.add( StringUtils.concat( ":" , ATT_UDT, this.getUdt() ) );
list.add( StringUtils.concat( ":" , ATT_STRUCT_TYPE, this.getStructType() ) );
list.add( StringUtils.concat( ":" , ATT_VIRTUAL, this.getVirtual() ) );
list.add( StringUtils.concat( ":" , ATT_EXAMPLE_DATA, this.getExampleData() ) );
return StringUtils.concat( ",", list );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ private static void generageFile( JavaGenerator gen ) throws IOException {
}

private static void handleCurrentGenerator( DaogenBasicGenerator generator, DaogenCatalogConfig daogenConfig, DaogenCatalogEntity entity, FactoryType dataType ) throws ConfigException, IOException, ClassNotFoundException, NoSuchMethodException {
log.info( "current generator : {}, entity : {}", generator.getKey(), entity.getId() );
if ( generator.isGenerate( daogenConfig, entity ) ) {
Collection<FactoryType> decorators = daogenConfig.getDecoratorCatalog().getDataList( dataType.getId() );
if ( decorators != null ) {
Expand Down Expand Up @@ -71,6 +72,7 @@ private static void generate( DaogenCatalogConfig daogenConfig, DaogenGeneratorC
for ( FactoryType dataType : generatorCatalog.getFactoryGenerators( daogenConfig ) ) {
if ( daogenConfig.getGeneralProps().containsKey( dataType.getInfo() ) ) {
DaogenBasicGenerator generator = (DaogenBasicGenerator)(ClassHelper.newInstance( dataType.getType()));
log.debug( "current generator : {}", generator.getKey() );
generator.init( daogenConfig, null );
generageFile( generator );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public abstract class DaogenBasicGenerator extends SimpleJavaGenerator implement

protected static final String CLOSE_AND_THROWS = " ) throws ";

protected static final String AT_OVERRIDE = "@Override";
public static final String AT_OVERRIDE = "@Override";

protected static final String REAL_CLASS_COMMENT = TAB+"// [HELPER/IMPL MODEL] this class is a stub and can be modified as you see fit (it will not been overwritten)";

Expand Down Expand Up @@ -71,7 +71,7 @@ public abstract class DaogenBasicGenerator extends SimpleJavaGenerator implement

protected static final String RETURN_RES_LIT = "return res;";

protected static final String PUBLIC_SPACE_LIT = "public ";
public static final String PUBLIC_SPACE_LIT = "public ";

protected static final String PUBLIC_STATIC_SPACE_LIT = "public static ";

Expand Down Expand Up @@ -162,7 +162,7 @@ protected boolean isJdkVersionEquals( Integer jdkVerson ) {
return this.getJdkTargetVersion().intValue() == jdkVerson.intValue();
}

protected boolean isJdkVersionAtLeast( Integer jdkVerson ) {
public boolean isJdkVersionAtLeast( Integer jdkVerson ) {
return this.getJdkTargetVersion().intValue() >= jdkVerson.intValue();
}

Expand Down Expand Up @@ -270,7 +270,7 @@ public String getEntityHelperName() {
public String getEntityWrapperName() {
return DaogenCatalogConstants.wrapperName( this.getCurrentEntity() );
}

public String getEntityFacadeDefName() {
return DaogenCatalogConstants.facadeDefName( this.getCurrentEntity() );
}
Expand Down Expand Up @@ -377,7 +377,7 @@ public String getClassBaseWrapper() {
return classBaseWrapper;
}

protected void setClassBaseWrapper(String classBaseWrapper) {
public void setClassBaseWrapper(String classBaseWrapper) {
this.classBaseWrapper = classBaseWrapper;
}

Expand Down Expand Up @@ -472,7 +472,7 @@ protected void beforeClass() {
}
}

protected void generateSerial( boolean condition ) throws IOException {
public void generateSerial( boolean condition ) throws IOException {
if ( condition ) {
this.addSerialVerUID();
this.writeSerialHelpers();
Expand All @@ -494,4 +494,13 @@ protected void writeSerialHelpers() {
this.getWriter().println();
}

@Override
public void setExtendsClass(String extendsClass) {
super.setExtendsClass(extendsClass);
}

@Override
public void setImplementsInterface(String implementsInterface) {
super.setImplementsInterface(implementsInterface);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.fugerit.java.daogen.base.gen;

import org.fugerit.java.core.cfg.ConfigException;
import org.fugerit.java.core.db.dao.DAOException;
import org.fugerit.java.core.lang.helpers.StringUtils;
import org.fugerit.java.daogen.base.config.*;
import org.fugerit.java.daogen.base.gen.util.WrapperUtils;

import java.io.IOException;

public class MicroProfileSchemaWrapperGenerator extends DaogenBasicGenerator {

public static final String KEY = "MicroProfileSchemaWrapperGenerator";

@Override
public String getKey() {
return KEY;
}

public void init( DaogenCatalogConfig daogenConfig, DaogenCatalogEntity entity ) throws ConfigException {
super.init( daogenConfig.getGeneralProp( DaogenCatalogConstants.GEN_PROP_SRC_MAIN_JAVA ),
fullObjectName( daogenConfig.getGeneralProp( DaogenCatalogConstants.GEN_PROP_PACKAGE_HELPER_MICROPROFILE ), DaogenCatalogConstants.mpSchemaName( entity ) ),
STYLE_CLASS, daogenConfig, entity );
this.getImportList().add( "org.eclipse.microprofile.openapi.annotations.media.Schema" );
WrapperUtils.init( daogenConfig, entity, this );
new DaogenBasicDecorator() {
@Override
public void addBeforeClass() throws DAOException {
super.addBeforeClass();
if ( StringUtils.isNotEmpty( entity.getComments() ) ) {
this.accessGenerator().println( String.format( "@Schema( description = \"%s\")" , entity.getComments() ) );
}
}
}.init( this );
}

@Override
public String getEntityWrapperName() {
return DaogenCatalogConstants.mpSchemaName( this.getCurrentEntity() );
}

@Override
public void generateDaogenBody() throws IOException {
WrapperUtils.generateBody( this, f -> {
if (StringUtils.isNotEmpty( f.getComments() ) ) {
String base = "@Schema( description = \"%s\" %s)";
String extra = "";
if ( StringUtils.isNotEmpty( f.getExampleData() ) ) {
extra = String.format( ", example =\"%s\"", f.getExampleData() );
}
return String.format( base , f.getComments(), extra );
} else {
return null;
}
} );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
import java.io.IOException;

import org.fugerit.java.core.cfg.ConfigException;
import org.fugerit.java.core.javagen.GeneratorNameHelper;
import org.fugerit.java.core.lang.helpers.BooleanUtils;
import org.fugerit.java.core.lang.helpers.StringUtils;
import org.fugerit.java.daogen.base.config.DaogenCatalogConfig;
import org.fugerit.java.daogen.base.config.DaogenCatalogConstants;
import org.fugerit.java.daogen.base.config.DaogenCatalogEntity;
import org.fugerit.java.daogen.base.config.DaogenCatalogField;
import org.fugerit.java.daogen.base.config.DaogenCatalogRelation;
import org.fugerit.java.daogen.base.config.DaogenClassConfigHelper;
import org.fugerit.java.daogen.base.gen.util.WrapperUtils;

public class WrapperGenerator extends DaogenBasicGenerator {

Expand All @@ -26,119 +21,12 @@ public void init( DaogenCatalogConfig daogenConfig, DaogenCatalogEntity entity )
super.init( daogenConfig.getGeneralProp( DaogenCatalogConstants.GEN_PROP_SRC_MAIN_JAVA ),
fullObjectName( daogenConfig.getGeneralProp( DaogenCatalogConstants.GEN_PROP_PACKAGE_HELPER ), DaogenCatalogConstants.wrapperName( entity ) ),
STYLE_CLASS, daogenConfig, entity );
String daoWrapperNgMode = daogenConfig.getGeneralProp( DaogenCatalogConstants.GEN_PROP_DAO_WRAPPER_NG_MODE, DaogenCatalogConstants.GEN_PROP_DAO_WRAPPER_NG_MODE_DISABLED );
logger.info( "{} -> {}", DaogenCatalogConstants.GEN_PROP_DAO_WRAPPER_NG_MODE, daoWrapperNgMode );
if ( DaogenCatalogConstants.GEN_PROP_DAO_WRAPPER_NG_MODE_DISABLED.equalsIgnoreCase( daoWrapperNgMode ) ) {
this.setClassBaseWrapper( DaogenClassConfigHelper.addImport( daogenConfig , DaogenClassConfigHelper.DAO_WRAPPER_BASE, this.getImportList() ) );
this.setExtendsClass( this.getClassBaseWrapper()+"<"+this.getEntityModelName()+">" );
} else if ( DaogenCatalogConstants.GEN_PROP_DAO_WRAPPER_NG_MODE_ENABLED.equalsIgnoreCase( daoWrapperNgMode ) ) {
String wrapperNgClass = DaogenClassConfigHelper.findClassConfigProp( daogenConfig, DaogenClassConfigHelper.DAO_WRAPPER_NG_BASE, DaogenClassConfigHelper.DAO_BASE_CLASS );
if ( StringUtils.isNotEmpty( wrapperNgClass ) ) {
this.setClassBaseWrapper( DaogenClassConfigHelper.addImport( daogenConfig , DaogenClassConfigHelper.DAO_WRAPPER_NG_BASE, this.getImportList() ) );
this.setExtendsClass( this.getClassBaseWrapper()+"<"+this.getEntityModelName()+">" );
}
} else {
throw new ConfigException( "Invalid "+DaogenCatalogConstants.GEN_PROP_DAO_WRAPPER_NG_MODE+" parameter : "+daoWrapperNgMode );
}
this.getImportList().add( this.getDaogenConfig().getGeneralProp( DaogenCatalogConstants.GEN_PROP_PACKAGE_MODEL )+"."+this.getEntityModelName() );
this.setImplementsInterface( this.getEntityModelName() );
for ( DaogenCatalogRelation relation : this.getCurrentEntity().getRelations() ) {
DaogenCatalogEntity entityTo = this.getDaogenConfig().getListMap( relation.getTo() );
this.getImportList().add( this.getDaogenConfig().getGeneralProp( DaogenCatalogConstants.GEN_PROP_PACKAGE_MODEL )+"."+DaogenCatalogConstants.modelName( entityTo ) );
}
}

private void generateRelations() {
if ( !this.getCurrentEntity().getRelations().isEmpty() ) {
this.getWriter().println( TAB+"/*" );
this.getWriter().println( TAB+" * fields generated for relations " );
this.getWriter().println( TAB+" */" );
this.getWriter().println();
for ( DaogenCatalogRelation relation : this.getCurrentEntity().getRelations() ) {
DaogenCatalogEntity entityTo = this.getDaogenConfig().getListMap( relation.getTo() );
String baseType = DaogenCatalogConstants.modelName( entityTo );
String className = GeneratorNameHelper.toClassName( relation.getName() );
if ( DaogenCatalogRelation.MODE_MANY.equalsIgnoreCase( relation.getMode() ) ) {
baseType = "java.util.List<"+baseType+">";
}
// metodo set
this.getWriter().println( TAB+AT_OVERRIDE );
this.getWriter().println( TAB+"public void set"+className+"( "+baseType+" value ) {" );
this.getWriter().println( TAB_2+"this.unwrapModel().set"+className+"( value );" );
this.getWriter().println( TAB+"}" );
this.getWriter().println();
// metodo get
this.getWriter().println( TAB+AT_OVERRIDE );
this.getWriter().println( TAB+PUBLIC_SPACE_LIT+baseType+" get"+className+"() {" );
this.getWriter().println( TAB_2+"return this.unwrapModel().get"+className+"();" );
this.getWriter().println( TAB+"}" );
this.getWriter().println();
}
}
WrapperUtils.init( daogenConfig, entity, this );
}

@Override
public void generateDaogenBody() throws IOException {
String daoWrapperNgMode = this.getDaogenConfig().getGeneralProp( DaogenCatalogConstants.GEN_PROP_DAO_WRAPPER_NG_MODE, DaogenCatalogConstants.GEN_PROP_DAO_WRAPPER_NG_MODE_DISABLED );
this.generateSerial( DaogenCatalogConstants.GEN_PROP_DAO_WRAPPER_NG_MODE_DISABLED.equalsIgnoreCase( daoWrapperNgMode ) );
this.getWriter().println( TAB+PUBLIC_SPACE_LIT+this.getEntityWrapperName()+"( "+this.getEntityModelName()+" wrapped ) {" );
this.getWriter().println( TAB_2+"super( wrapped );" );
this.getWriter().println( TAB+"}" );
this.getWriter().println();
this.getWriter().println( TAB+PUBLIC_SPACE_LIT+this.getEntityModelName()+" unwrap( "+this.getEntityWrapperName()+" wrapper ) {" );
this.getWriter().println( TAB_2+""+this.getEntityModelName()+" res = wrapper;" );
if ( this.isJdkVersionAtLeast( DaogenCatalogConstants.GEN_PROP_JDK_TARGET_VERSION_17 ) ) {
this.getWriter().println( TAB_2+"while ( res instanceof "+this.getEntityWrapperName()+" wrappedinstance ) { " );
this.getWriter().println( TAB_3+"res = wrappedinstance.unwrapModel();" );
} else {
this.getWriter().println( TAB_2+"while ( res instanceof "+this.getEntityWrapperName()+" ) { " );
this.getWriter().println( TAB_3+"res = (("+this.getEntityWrapperName()+")res).unwrapModel();" );
}
this.getWriter().println( TAB_2+"}" );
this.getWriter().println( TAB_2+"return res;" );
this.getWriter().println( TAB+"}" );
this.getWriter().println();

boolean relationLast = "true".equalsIgnoreCase( this.getDaogenConfig().getGeneralProp( DaogenCatalogConstants.GEN_PROP_RELATIONS_LAST ) );
if ( !relationLast ) {
this.generateRelations();
}

this.getWriter().println( TAB+"/*" );
this.getWriter().println( TAB+" * fields generated for entity attributes " );
this.getWriter().println( TAB+" */" );
for ( DaogenCatalogField field : this.getCurrentEntity().getAllFields() ) {
// property
String javaSuffix = GeneratorNameHelper.toClassName( field.getId() );
String realJavaType = this.getDaogenConfig().getTypeMapper().mapForModel( field );
// metodo set
this.getWriter().println( TAB+AT_OVERRIDE );
this.getWriter().println( TAB+"public void set"+javaSuffix+"( "+realJavaType+" value ) {" );
this.getWriter().println( TAB_2+"this.unwrapModel().set"+javaSuffix+"( value );" );
this.getWriter().println( TAB+"}" );
this.getWriter().println();
// metodo get
this.getWriter().println( TAB+AT_OVERRIDE );
this.getWriter().println( TAB+PUBLIC_SPACE_LIT+realJavaType+" get"+javaSuffix+"() {" );
this.getWriter().println( TAB_2+"return this.unwrapModel().get"+javaSuffix+"();" );
this.getWriter().println( TAB+"}" );
this.getWriter().println();
}

if ( relationLast ) {
this.generateRelations();
}

if ( BooleanUtils.isTrue( this.getDaogenConfig().getGeneralProp( DaogenCatalogConstants.GEN_PROP_CHECK_EMPTY_INTERFACE ) ) ) {
this.getWriter().println( TAB+AT_OVERRIDE );
this.getWriter().println( TAB+"public boolean isEmpty() {" );
this.getWriter().println( TAB_2+"return this.unwrapModel().isEmpty();" );
this.getWriter().println( TAB+"}" );
this.getWriter().println();
}

WrapperUtils.generateBody( this, f -> null );
}



}
Loading
Loading