22
33import static dev .sassine .api .structure .export .builder .function .ImportBeanFunction .importJavaTime ;
44import static dev .sassine .api .structure .export .builder .function .StoreClassFuncation .store ;
5+ import static dev .sassine .api .structure .type .TypeConverter .TYPE_LOCAL_DATE ;
6+ import static dev .sassine .api .structure .type .TypeConverter .TYPE_LOCAL_DATE_TIME ;
7+ import static dev .sassine .api .structure .type .TypeConverter .TYPE_LOCAL_TIME ;
58import static java .lang .String .format ;
69import static java .lang .reflect .Modifier .PRIVATE ;
710import static java .lang .reflect .Modifier .PUBLIC ;
11+ import static java .util .Objects .nonNull ;
812import static java .util .Optional .ofNullable ;
13+ import static org .apache .commons .lang3 .StringUtils .isNotBlank ;
914import static org .apache .logging .log4j .LogManager .getLogger ;
1015
16+ import java .util .Map ;
17+
1118import javax .persistence .Column ;
1219import javax .persistence .Entity ;
1320import javax .persistence .GeneratedValue ;
2027import org .burningwave .core .classes .TypeDeclarationSourceGenerator ;
2128import org .burningwave .core .classes .UnitSourceGenerator ;
2229import org .burningwave .core .classes .VariableSourceGenerator ;
30+ import org .hibernate .annotations .ColumnDefault ;
2331
2432import dev .sassine .api .structure .export .builder .factory .Factory ;
2533import dev .sassine .api .structure .model .java .EntityModel ;
@@ -40,6 +48,11 @@ public class EntityFactory implements Factory {
4048 private static final String PARAM_NULLABLE = "nullable" ;
4149 private static final String PARAM_NAME = "name" ;
4250
51+ Map <String , String > defaultValueLocal = Map .of (
52+ TYPE_LOCAL_DATE , "LocalDate.now()" ,
53+ TYPE_LOCAL_TIME , "LocalTime.now()" ,
54+ TYPE_LOCAL_DATE_TIME , "LocalDateTime.now()" );
55+
4356 @ Override
4457 public void execute (EntityModel entityModel , boolean isAutoGenerated , String nameClass , String packageName ) {
4558 log .debug ("ClassName: ({}) - PackageName: ({}) " , nameClass , packageName );
@@ -51,6 +64,7 @@ public void execute(EntityModel entityModel, boolean isAutoGenerated, String nam
5164 entityModel .getFields ().forEach (fieldModel -> {
5265 VariableSourceGenerator field = this .buildField (fieldModel );
5366 importJavaTime (gen , fieldModel );
67+ this .setDefaultValue (field ,fieldModel );
5468 this .generateEntityPK (isAutoGenerated , fieldModel , field , entityModel );
5569 log .debug ("Generated EntityPK" );
5670 clEntity .addField (field );
@@ -61,6 +75,15 @@ public void execute(EntityModel entityModel, boolean isAutoGenerated, String nam
6175 log .debug ("ClassEntity ({}) stored" , nameClass );
6276 }
6377
78+ private void setDefaultValue (VariableSourceGenerator field , FieldModel fieldModel ) {
79+ if (isNotBlank (fieldModel .getDefaultValue ())) {
80+ var value = defaultValueLocal .get (fieldModel .getType ());
81+ if (nonNull (value )) field .setValue (defaultValueLocal .get (fieldModel .getType ()));
82+ field .addAnnotation (AnnotationSourceGenerator .create (ColumnDefault .class ).addParameter ("value" ,
83+ VariableSourceGenerator .create (format (FORMAT_STRING_FIELDS , fieldModel .getDefaultValue ()))));
84+ }
85+ }
86+
6487 private ClassSourceGenerator buildClassSource (EntityModel entityModel , String nameClass ) {
6588 return ClassSourceGenerator
6689 .create (TypeDeclarationSourceGenerator .create (format (FORMAT_ENTITY_CLASSNAME , nameClass )))
0 commit comments