Skip to content

Commit

Permalink
Merge branch 'feature/new-model-set-entity-mode' into portofino6
Browse files Browse the repository at this point in the history
  • Loading branch information
alessiostalla committed Jan 4, 2024
2 parents 7222fd2 + a7bfece commit 3cd8375
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
<artifactId>portofino-postgresql</artifactId>
<version>${portofino.version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>

<!--
///////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
<artifactId>portofino-postgresql</artifactId>
<version>${portofino.version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>

<!--
///////////////////////////////////////////////////////////////////////
Expand Down
1 change: 0 additions & 1 deletion demo-tt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.5.1</version>
</dependency>
<dependency>
<groupId>com.manydesigns</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import com.manydesigns.portofino.database.model.annotations.JDBCConnection;

@JDBCConnection(driver = "org.postgresql.Driver")
domain tt;
domain tt;
2 changes: 1 addition & 1 deletion microservices/spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.0</version>
<version>2.7.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.manydesigns</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ public static FileObject getApplicationDirectory(ApplicationArguments applicatio
if (main.isFolder()) {
FileObject javaActionsDir =
main.resolveFile("java").resolveFile("portofino").resolveFile("actions");
if(javaActionsDir.isFolder()) {
logger.error("--dev does not work with Java actions since the compiled version is in the build directory");
System.exit(1);
if (javaActionsDir.isFolder()) {
logger.error("--dev does not work with Java actions since the compiled version is in the build directory.");
} else {
FileObject appDir = main.resolveFile("resources").resolveFile("portofino");
if (appDir.isFolder()) {
Expand All @@ -128,6 +127,8 @@ public static FileObject getApplicationDirectory(ApplicationArguments applicatio
return appDir;
}
}
} else {
logger.error("--dev requires the following path to refer to a directory: " + main.getName().getPath());
}
}
if(new File("portofino.properties").isFile()) {
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@
<artifactId>commons-vfs2</artifactId>
<version>${commons.vfs.version}</version>
</dependency>
<!-- Database drivers -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.5.1</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ public void init(Object context, Configuration configuration) {
}
}
}
//Make sure the ECore annotation has the correct details. New ArrayList because otherwise the list is
//modified in place, losing properties before they can be added back.
// Make sure the ECore annotation has the correct details. New ArrayList because otherwise the list is
// modified in place, losing properties before they can be added back.
setProperties(new ArrayList<>(properties));
if(javaAnnotation == null) {
Map<String, Object> valueMap = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,9 @@ protected synchronized Domain doLoadDomain(String name, Domain parent, FileObjec
} finally {
toLinkQueue.clear();
}
} else {
return null;
}
} else {
throw new IOException("Not a directory: " + parentDirectory.getName().getPath());
}
return null;
}

protected Domain loadDomainDirectory(Model model, Domain parent, FileObject domainDir) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public static boolean isInFk(Column column) {
//**************************************************************************
public static String normalizeName(String name) {
name = StringUtils.replaceChars(name, ".", "_");
String firstLetter = name.substring(0,1);
String firstLetter = name.substring(0, 1);
String others = name.substring(1);

StringBuilder result = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.manydesigns.portofino.database.model.annotations;

public @interface Database {
String entityMode() default "MAP"; // TODO explicitly refer to EntityMode.MAP
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import io.reactivex.disposables.Disposable;
import org.apache.commons.vfs2.AllFileSelector;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -60,6 +61,7 @@ public class DatabaseModule implements Module, ApplicationContextAware, Applicat
public static final String copyright =
"Copyright (C) 2005-2020 ManyDesigns srl";
public static final String GENERATED_CLASSES_DIRECTORY_NAME = "classes-generated";
public static final String SAVE_GENERATED_CLASSES_PROPERTY = "portofino.database.saveGeneratedClasses";

@Autowired
public ServletContext servletContext;
Expand Down Expand Up @@ -134,9 +136,26 @@ public Persistence getPersistence(
applicationContext.getAutowireCapableBeanFactory().autowireBean(persistence);
}

configureGeneratedClassesVisibility(persistence);
this.persistence = persistence;
return persistence;
}

private void configureGeneratedClassesVisibility(Persistence persistence) throws FileSystemException {
FileObject generatedClassesRoot = applicationDirectory.resolveFile(GENERATED_CLASSES_DIRECTORY_NAME);
generatedClassesRoot.createFolder();
AllFileSelector allFileSelector = new AllFileSelector();
String saveGeneratedClasses = configuration.getProperties().getString(
SAVE_GENERATED_CLASSES_PROPERTY, "pojo"
);
boolean alwaysSave = saveGeneratedClasses.equalsIgnoreCase("always") || saveGeneratedClasses.equalsIgnoreCase("true");
boolean neverSave = saveGeneratedClasses.equalsIgnoreCase("never") || saveGeneratedClasses.equalsIgnoreCase("false");
;
if (!alwaysSave && !neverSave && !saveGeneratedClasses.equalsIgnoreCase("pojo")) {
logger.warn(
"Invalid setting for " + SAVE_GENERATED_CLASSES_PROPERTY + ": " + saveGeneratedClasses + "; " +
"the application will default to saving only classes with POJO entity mode.");
}
//When the entity mode is POJO:
// - make generated classes visible to shared classes and actions;
// - write them in the application directory so the user's IDE and tools can know about them.
Expand All @@ -148,7 +167,7 @@ public Persistence getPersistence(
switch (e.type) {
case Persistence.DatabaseSetupEvent.ADDED:
persistenceCodeBase.add(e.accessor.getCodeBase());
if(e.accessor.getEntityMode() == EntityMode.POJO) {
if(alwaysSave || (!neverSave && e.accessor.getEntityMode() == EntityMode.POJO)) {
externalDatabaseDir.copyFrom(inMemoryDatabaseDir, allFileSelector);
}
break;
Expand All @@ -160,14 +179,12 @@ public Persistence getPersistence(
case Persistence.DatabaseSetupEvent.REPLACED:
persistenceCodeBase.replace(e.oldAccessor.getCodeBase(), e.accessor.getCodeBase());
externalDatabaseDir.deleteAll();
if(e.accessor.getEntityMode() == EntityMode.POJO) {
if(alwaysSave || (!neverSave && e.accessor.getEntityMode() == EntityMode.POJO)) {
externalDatabaseDir.copyFrom(inMemoryDatabaseDir, allFileSelector);
}
break;
}
});
this.persistence = persistence;
return persistence;
}

@PreDestroy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -245,9 +246,40 @@ private void annotateDatabase(Database db) {

protected Database setupDatabase(Domain domain) {
logger.info("Setting up database " + domain.getQualifiedName());
ConnectionProvider connectionProvider = setupConnectionProvider(domain);
if(connectionProvider != null) {
Database database = DatabaseLogic.findDatabaseByName(databases, domain.getName());
boolean alreadyExists = database != null;
if (alreadyExists) {
logger.debug("Database " + database.getName() + " already exists");
} else {
database = new Database(domain);
databases.add(database);
}
//Can't use getJavaAnnotation as they've not yet been resolved
EAnnotation ann = domain.getEAnnotation(
com.manydesigns.portofino.database.model.annotations.Database.class.getName()
);
if(ann != null) {
database.setEntityMode(ann.getDetails().get("entityMode"));
}
connectionProvider.setDatabase(database);
database.setConnectionProvider(connectionProvider);
if (!alreadyExists) { // TODO check – should we also refresh the schemas here?
Database db = database;
domain.getSubdomains().forEach(subd -> setupSchema(db, subd));
}
return database;
} else {
return null;
}
}

@Nullable
protected ConnectionProvider setupConnectionProvider(Domain domain) {
ConnectionProvider connectionProvider = null;
//Can't use getJavaAnnotation as they've not yet been resolved
EAnnotation ann = domain.getEAnnotation(JDBCConnection.class.getName());
ConnectionProvider connectionProvider = null;
if(ann != null) {
JdbcConnectionProvider cp = new JdbcConnectionProvider();
cp.setUrl(ann.getDetails().get("url"));
Expand All @@ -263,21 +295,7 @@ protected Database setupDatabase(Domain domain) {
connectionProvider = cp;
}
}
if(connectionProvider != null) {
Database database = new Database(domain);
Database existing = DatabaseLogic.findDatabaseByName(databases, database.getName());
if (existing != null) {
logger.debug("Database " + database.getName() + " already exists");
return existing;
}
databases.add(database);
connectionProvider.setDatabase(database);
database.setConnectionProvider(connectionProvider);
domain.getSubdomains().forEach(subd -> setupSchema(database, subd));
return database;
} else {
return null;
}
return connectionProvider;
}

protected Schema setupSchema(Database database, Domain domain) {
Expand Down Expand Up @@ -433,9 +451,7 @@ public void addDatabase(Database database) {
}

protected boolean initDatabase(Database database) {
new ResetVisitor().visit(database);
new InitVisitor(databases, configuration.getProperties()).visit(database);
new LinkVisitor(databases, configuration.getProperties()).visit(database);
initModelObject(database);
Boolean enabled = database.getJavaAnnotation(Enabled.class).map(Enabled::value).orElse(true);
if(enabled) {
initConnectionProvider(database);
Expand All @@ -446,6 +462,12 @@ protected boolean initDatabase(Database database) {
}
}

public void initModelObject(ModelObject modelObject) {
new ResetVisitor().visit(modelObject);
new InitVisitor(databases, configuration.getProperties()).visit(modelObject);
new LinkVisitor(databases, configuration.getProperties()).visit(modelObject);
}

protected void initConnectionProvider(Database database) {
logger.info("Initializing connection provider for database " + database.getDatabaseName());
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,10 @@ protected void syncTables(DatabaseSnapshot databaseSnapshot, Schema sourceSchema
if(sourceTable == null) {
logger.debug("Added new table: {}", tableName);
sourceTable = new Table();
sourceTable.setTableName(tableName);
}
Table targetTable = new Table(targetSchema);
copyAnnotations(sourceTable, targetTable);
targetSchema.getTables().add(targetTable);

targetTable.setTableName(tableName);
Expand All @@ -413,7 +415,6 @@ protected void syncTables(DatabaseSnapshot databaseSnapshot, Schema sourceSchema
targetTable.setEntityName(sourceTable.getEntityName());
targetTable.setJavaClass(sourceTable.getJavaClass());
targetTable.setShortName(sourceTable.getShortName());
copyAnnotations(sourceTable, targetTable);

syncColumns(liquibaseTable, sourceTable, targetTable);

Expand All @@ -438,20 +439,19 @@ protected void syncViews(DatabaseSnapshot databaseSnapshot, Schema sourceSchema,
//tryToDeterminePrimaryKey(liquibaseView);
}
View targetView = new View(targetSchema);
copyAnnotations(sourceTable, targetView);
if(sourceTable instanceof View) {
View sourceView = (View) sourceTable;
targetView.setInsertable(sourceView.isInsertable());
targetView.setUpdatable(sourceView.isUpdatable());
}
targetSchema.getTables().add(targetView);

targetView.setTableName(viewName);

logger.debug("Merging view attributes and annotations");
targetView.setEntityName(sourceTable.getEntityName());
targetView.setJavaClass(sourceTable.getJavaClass());
targetView.setShortName(sourceTable.getShortName());
copyAnnotations(sourceTable, targetView);

syncColumns(liquibaseView, sourceTable, targetView);

Expand Down Expand Up @@ -538,7 +538,13 @@ protected void copySelectionProviders(Table sourceTable, Table targetTable) {
for(liquibase.structure.core.Column liquibaseColumn : relation.getColumns()) {
logger.debug("Processing column: {}", liquibaseColumn.getName());

Column sourceColumn = DatabaseLogic.findColumnByNameIgnoreCase(sourceTable, liquibaseColumn.getName());
Column targetColumn = new Column(targetTable);
if(sourceColumn != null) {
copyAnnotations(sourceColumn, targetColumn);
targetColumn.setPropertyName(sourceColumn.getPropertyName());
targetColumn.setJavaType(sourceColumn.getJavaType());
}

targetColumn.setColumnName(liquibaseColumn.getName());

Expand Down Expand Up @@ -586,14 +592,7 @@ protected void copySelectionProviders(Table sourceTable, Table targetTable) {
targetColumn.setScale(liquibaseColumn.getType().getDecimalDigits());
//TODO liquibaseColumn.getLengthSemantics()

Column sourceColumn = DatabaseLogic.findColumnByNameIgnoreCase(sourceTable, liquibaseColumn.getName());
if(sourceColumn != null) {
targetColumn.setPropertyName(sourceColumn.getPropertyName());
targetColumn.setJavaType(sourceColumn.getJavaType());
copyAnnotations(sourceColumn, targetColumn);
}

logger.debug("Column creation successfull. Adding column to table.");
logger.debug("Column creation successful. Adding column to table.");
targetTable.getColumns().add(targetColumn);
}

Expand Down Expand Up @@ -628,12 +627,11 @@ public int compare(Column c1, Column c2) {

protected void copyAnnotations(Annotated source, Annotated target) {
for(Annotation ann : source.getAnnotations()) {
Annotation annCopy = new Annotation();
annCopy.setParent(target);
annCopy.setType(ann.getType());
// Note: we use ensureAnnotation because we add some annotations automatically and we want to overwrite,
// not duplicate those
Annotation annCopy = target.ensureAnnotation(ann.getType());
annCopy.setValues(ann.getValues());
annCopy.setProperties(ann.getProperties());
target.getAnnotations().add(annCopy);
}
}

Expand Down
Loading

0 comments on commit 3cd8375

Please sign in to comment.