Skip to content

Commit 5408bdc

Browse files
committed
Feature: Adding flag isPostgres and useAutoGenerate Annotation
1 parent 3ef7cd8 commit 5408bdc

5 files changed

Lines changed: 46 additions & 36 deletions

File tree

src/main/java/dev/sassine/api/structure/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public class Main {
66

77
public static void main(final String[] args) throws FileNotFoundException {
8-
new Sqlschema2Java().compile();
8+
new Sqlschema2Java().compile("",false, false);
99
}
1010

1111
}

src/main/java/dev/sassine/api/structure/Sqlschema2Java.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,25 @@
1616
@Slf4j
1717
public class Sqlschema2Java {
1818

19-
public void compile() {
20-
String input = "C:\\poc\\poc.sql";
19+
public void compile(String inputFile,boolean isPostgress,boolean isAutoGenerated) {
20+
inputFile = "C:\\poc\\poc.sql";
2121
final Util util = new Util();
22-
log.debug("preparing to read SQL file( '{}')",input);
23-
try (InputStream in = util.getInputStream(input)) {
22+
log.debug("preparing to read SQL file('{}') ",inputFile);
23+
try (InputStream in = util.getInputStream(inputFile)) {
2424
log.debug("file read successfully");
25-
process(util.read(in));
25+
process(util.read(in),isPostgress,isAutoGenerated);
2626
} catch (final Exception e) {
2727
log.error(e.getMessage(), e);
2828
}
2929
}
3030

31-
private void process(final String sqlContent) {
31+
private void process(final String sqlContent, boolean isPostgress, boolean isAutoGenerated) {
3232
final SqlImport sqlImport = new SqlImport();
3333
final Database database = sqlImport.getDatabase(sqlContent);
34-
new TypeConverter().convertTypeFromSQLToEntityStore(database);
34+
new TypeConverter().convertTypeFromSQLToEntityStore(database,isPostgress);
3535
new DatabaseValidator().validate(database);
3636
List<EntityModel> entityModel = new DatabaseConverter().convert(database);
37-
new BuilderEntity().build(entityModel);
37+
new BuilderEntity().build(entityModel,isAutoGenerated);
3838
}
3939

4040
}

src/main/java/dev/sassine/api/structure/export/builder/BuilderEntity.java

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static java.lang.String.format;
44
import static java.lang.System.getProperty;
55
import static java.util.Optional.ofNullable;
6+
import static org.apache.commons.text.CaseUtils.toCamelCase;
67

78
import java.lang.reflect.Modifier;
89
import java.time.LocalDateTime;
@@ -15,7 +16,6 @@
1516
import javax.persistence.Id;
1617
import javax.persistence.Table;
1718

18-
import org.apache.commons.text.CaseUtils;
1919
import org.burningwave.core.classes.AnnotationSourceGenerator;
2020
import org.burningwave.core.classes.ClassSourceGenerator;
2121
import org.burningwave.core.classes.FunctionSourceGenerator;
@@ -34,9 +34,7 @@
3434
import lombok.NoArgsConstructor;
3535
import lombok.Setter;
3636
import lombok.ToString;
37-
import lombok.extern.slf4j.Slf4j;
3837

39-
@Slf4j
4038
public class BuilderEntity {
4139

4240
private static final String PACKAGE_DOMAIN_NAME = "domain";
@@ -62,32 +60,29 @@ public class BuilderEntity {
6260
private static final String PARAM_VALUE = "value";
6361
private static final String PARAM_NAME = "name";
6462

65-
public void build(List<EntityModel> entityModel) {
66-
entityModel.forEach(e -> build(e));
63+
public void build(List<EntityModel> entityModel,boolean isAutoGenerated) {
64+
entityModel.forEach(e -> build(e,isAutoGenerated));
6765
}
6866

69-
private void build(EntityModel entityModel) {
70-
String nameClass = CaseUtils.toCamelCase(entityModel.getName(), true, '_');
67+
private void build(EntityModel entityModel,boolean isAutoGenerated) {
68+
String nameClass = toCamelCase(entityModel.getName(), true, '_');
7169
String packageName = "dev.sassine.api.structure.delete";
72-
boolean isAutoGenerated = true;
73-
String type = null;
7470

75-
createEntity(entityModel, type, isAutoGenerated, nameClass, packageName);
71+
createEntity(entityModel, isAutoGenerated, nameClass, packageName);
7672
createDTO(entityModel, nameClass, packageName);
77-
createRepository(entityModel, type, nameClass, packageName);
78-
79-
log.info("Sucess ;)");
73+
createRepository(entityModel, nameClass, packageName);
74+
8075
}
8176

82-
private void createRepository(EntityModel entityModel, String nameClass, String packageName, String type) {
77+
private void createRepository(EntityModel entityModel, String nameClass, String packageName) {
8378

8479
UnitSourceGenerator gen = UnitSourceGenerator
8580
.create(format(FORMAT_PACKAGE_DOT_PACKAGE, packageName, PACKAGE_REPOSITORY_NAME));
8681
ClassSourceGenerator interfaceClass = ClassSourceGenerator
8782
.createInterface(TypeDeclarationSourceGenerator.create(ENTITY_REPOSITORY))
8883
.expands(TypeDeclarationSourceGenerator.create(CLASSNAME_JPAREPOSITORY)
8984
.addGeneric(GenericSourceGenerator.create(format(FORMAT_ENTITY_CLASSNAME, nameClass)))
90-
.addGeneric(GenericSourceGenerator.create(getIdClassType(type))))
85+
.addGeneric(GenericSourceGenerator.create(getIdClassType(entityModel))))
9186
.addModifier(Modifier.PUBLIC).addAnnotation(AnnotationSourceGenerator.create(Repository.class));
9287

9388
importEntityClass(nameClass, packageName, gen);
@@ -98,12 +93,13 @@ private void createRepository(EntityModel entityModel, String nameClass, String
9893
store(gen);
9994
}
10095

101-
private Class<?> getIdClassType(String type) {
102-
if (TypeConverter.TYPE_STRING.equalsIgnoreCase(type))
96+
private Class<?> getIdClassType(EntityModel entityModel) {
97+
String pkType = entityModel.getPkType();
98+
if (TypeConverter.TYPE_STRING.equalsIgnoreCase(pkType))
10399
return String.class;
104-
else if (TypeConverter.TYPE_UUID.equalsIgnoreCase(type))
100+
else if (TypeConverter.TYPE_UUID.equalsIgnoreCase(pkType))
105101
return UUID.class;
106-
else if (TypeConverter.TYPE_INTEGER.equalsIgnoreCase(type))
102+
else if (TypeConverter.TYPE_INTEGER.equalsIgnoreCase(pkType))
107103
return Integer.class;
108104
else
109105
return Long.class;
@@ -146,7 +142,7 @@ private void createDTO(EntityModel entityModel, String nameClass, String package
146142
store(gen);
147143
}
148144

149-
private void createEntity(EntityModel entityModel, String type, boolean isAutoGenerated, String nameClass,
145+
private void createEntity(EntityModel entityModel, boolean isAutoGenerated, String nameClass,
150146
String packageName) {
151147

152148
UnitSourceGenerator gen = UnitSourceGenerator
@@ -173,7 +169,7 @@ private void createEntity(EntityModel entityModel, String type, boolean isAutoGe
173169
VariableSourceGenerator.create(String.valueOf(fieldModel.getNullable()))));
174170

175171
importLocalDateTime(gen, fieldModel);
176-
generateEntityPK(isAutoGenerated, fieldModel, field, type);
172+
generateEntityPK(isAutoGenerated, fieldModel, field, entityModel);
177173

178174
clEntity.addField(field);
179175
}
@@ -183,11 +179,11 @@ private void createEntity(EntityModel entityModel, String type, boolean isAutoGe
183179
store(gen);
184180
}
185181

186-
private void generateEntityPK(boolean isAutoGenerated, FieldModel fieldModel, VariableSourceGenerator field, String type) {
182+
private void generateEntityPK(boolean isAutoGenerated, FieldModel fieldModel, VariableSourceGenerator field, EntityModel entityModel) {
187183
if (ofNullable(fieldModel.getIsPrimaryKey()).orElse(false)) {
188184
field.addAnnotation(AnnotationSourceGenerator.create(Id.class));
189185
if (isAutoGenerated) field.addAnnotation(AnnotationSourceGenerator.create(GeneratedValue.class));
190-
type = fieldModel.getType();
186+
entityModel.setPkType(fieldModel.getType());
191187
}
192188
}
193189

src/main/java/dev/sassine/api/structure/model/java/EntityModel.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,50 @@ public class EntityModel {
1010

1111
private String name;
1212
private String pkPolicy;
13+
private String pkType;
14+
1315
private List<FieldModel> fields = new ArrayList<FieldModel>();
1416

15-
1617
public FieldModel getFieldForName(final String name) {
1718
return fields.stream().filter(fl -> fl.getName().equalsIgnoreCase(name)).findFirst().orElse(null);
1819
}
1920

2021
public String getName() {
2122
return name;
2223
}
24+
2325
public void setName(final String name) {
2426
this.name = name;
2527
}
28+
2629
public List<FieldModel> getFields() {
2730
return fields;
2831
}
32+
2933
public void setFields(final List<FieldModel> fields) {
3034
this.fields = fields;
3135
}
36+
3237
public String getPkPolicy() {
3338
return pkPolicy;
3439
}
40+
3541
public void setPkPolicy(final String pkPolicy) {
3642
this.pkPolicy = pkPolicy;
3743
}
3844

45+
46+
public String getPkType() {
47+
return pkType;
48+
}
49+
50+
public void setPkType(String pkType) {
51+
this.pkType = pkType;
52+
}
53+
3954
public EntityModel(String name, String pkPolicy) {
4055
super();
4156
this.name = name;
4257
this.pkPolicy = pkPolicy;
4358
}
44-
4559
}

src/main/java/dev/sassine/api/structure/type/TypeConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ public class TypeConverter {
1515
public static final String TYPE_BOOLEAN = "Boolean";
1616
public static final String TYPE_LOCAL_DATE_TIME = "LocalDateTime";
1717

18-
public void convertTypeFromSQLToEntityStore(final Database database) {
18+
public void convertTypeFromSQLToEntityStore(final Database database, boolean isPostgress) {
1919
for (final TableModel table : database.getTables()) {
2020
for (final Column column : table.getColumnByNames().values()) {
2121
if (column.getType() == null) {
2222
column.setConvertedType("");
2323
} else {
24-
column.setConvertedType(convertTypeFromSQLToEntityStore(column.getType(), false));
24+
column.setConvertedType(convertTypeFromSQLToEntityStore(column.getType(), isPostgress));
2525
}
2626
}
2727
}

0 commit comments

Comments
 (0)