Skip to content

Commit

Permalink
Database setup / Instead of declaring all SQL for each language, decl…
Browse files Browse the repository at this point in the history
…are the language and load SQL files for each languages if available.
  • Loading branch information
fxprunayre committed Jun 6, 2024
1 parent 9e419fe commit b159cda
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 94 deletions.
7 changes: 4 additions & 3 deletions core/src/main/java/org/fao/geonet/lib/DbLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private void runSQL(Statement statement, List<String> data, boolean failOnError)
*
* @param type @return
*/
private Path checkFilePath(ServletContext servletContext, Path appPath, Path filePath, String prefix, String type) {
private Path checkFilePath(ServletContext servletContext, Path appPath, Path filePath, String prefix, String type) throws IOException {
Path finalPath;
finalPath = testPath(filePath.resolve(prefix + type + SQL_EXTENSION));

Expand Down Expand Up @@ -214,9 +214,10 @@ private Path checkFilePath(ServletContext servletContext, Path appPath, Path fil
if (finalPath != null)
return finalPath;
else {
Log.debug(Geonet.DB, " No default SQL script found: " + (filePath + "/" + prefix + type + SQL_EXTENSION));
String msg = String.format("SQL script not found: %s", filePath + "/" + prefix + type + SQL_EXTENSION);
Log.debug(Geonet.DB, msg);
throw new IOException(msg);
}
return toPath("");
}

private Path toPath(String pathString) {
Expand Down
8 changes: 0 additions & 8 deletions docs/manual/docs/contributing/translating.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,6 @@ Then on the Java application, register the new language in `src/main/webResource
Add a new SQL file for the database initialization in `src/main/webapp/WEB-INF/classes/setup/sql/data/loc-cze-default.sql` and update translations. This file is used to register the new language for database entity translations (eg. groups, status).
In `src/main/webResources/WEB-INF/config-db/initial_data.xml`, register the new SQL file:
``` xml
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
<constructor-arg value="loc-cze-"/>
</bean>
```
# Translating a standard
Expand Down
34 changes: 28 additions & 6 deletions web/src/main/java/org/fao/geonet/Geonetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.fao.geonet.kernel.setting.Settings;
import org.fao.geonet.kernel.thumbnail.ThumbnailMaker;
import org.fao.geonet.languages.IsoLanguagesMapper;
import org.fao.geonet.lib.DatabaseType;
import org.fao.geonet.lib.DbLib;
import org.fao.geonet.lib.Lib;
import org.fao.geonet.repository.MetadataRepository;
Expand Down Expand Up @@ -82,10 +83,13 @@
import javax.servlet.ServletContext;
import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -420,14 +424,32 @@ private void importDatabaseData(final ServiceContext context) {

// import data from init files
List<Pair<String, String>> importData = context.getBean("initial-data", List.class);
Set<String> applicationLanguages = (Set<String>) ApplicationContextHolder.get().getBean("languages");

final DbLib dbLib = new DbLib();
for (Pair<String, String> pair : importData) {
final ServletContext servletContext = context.getServlet().getServletContext();
final Path appPath = context.getAppPath();
final Path filePath = IO.toPath(pair.one());
final String filePrefix = pair.two();
Log.warning(Geonet.DB, "Executing SQL from: " + filePath + " " + filePrefix);
dbLib.insertData(servletContext, context, appPath, filePath, filePrefix);
String folderPath = pair.one();
String sqlFilePath = pair.two();
Path appPath = context.getAppPath();
ServletContext servletContext = context.getServlet().getServletContext();

List<String> filePaths = new ArrayList<>();
if (sqlFilePath.contains("{lang}")) {
for (String lang : applicationLanguages) {
filePaths.add(sqlFilePath.replace("{lang}", lang));
}
} else {
filePaths.add(sqlFilePath);
}

for (String filePathStr : filePaths) {
Log.debug(Geonet.DB, "Executing SQL from: " + folderPath + " " + filePathStr);
try {
dbLib.insertData(servletContext, context, appPath, Path.of(folderPath), filePathStr);
} catch (IOException ioe) {
Log.warning(Geonet.DB, "File not found during initialization. For language add required files if needed. " + ioe.getMessage());
}
}
}
String siteUuid = UUID.randomUUID().toString();
context.getBean(SettingManager.class).setSiteUuid(siteUuid);
Expand Down
78 changes: 1 addition & 77 deletions web/src/main/webResources/WEB-INF/config-db/initial_data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,83 +41,7 @@
</bean>
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
<constructor-arg value="loc-eng-"/>
</bean>
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
<constructor-arg value="loc-fre-"/>
</bean>
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
<constructor-arg value="loc-dan-"/>
</bean>
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
<constructor-arg value="loc-ger-"/>
</bean>
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
<constructor-arg value="loc-spa-"/>
</bean>
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
<constructor-arg value="loc-cat-"/>
</bean>
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
<constructor-arg value="loc-rus-"/>
</bean>
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
<constructor-arg value="loc-por-"/>
</bean>
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
<constructor-arg value="loc-chi-"/>
</bean>
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
<constructor-arg value="loc-dut-"/>
</bean>
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
<constructor-arg value="loc-nor-"/>
</bean>
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
<constructor-arg value="loc-fin-"/>
</bean>
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
<constructor-arg value="loc-ara-"/>
</bean>
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
<constructor-arg value="loc-ita-"/>
</bean>
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
<constructor-arg value="loc-tur-"/>
</bean>
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
<constructor-arg value="loc-vie-"/>
</bean>
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
<constructor-arg value="loc-pol-"/>
</bean>
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
<constructor-arg value="loc-slo-"/>
</bean>
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
<constructor-arg value="loc-swe-"/>
</bean>
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
<constructor-arg value="loc-wel-"/>
<constructor-arg value="loc-{lang}-"/>
</bean>
<bean class="org.fao.geonet.domain.Pair" factory-method="read">
<constructor-arg value="WEB-INF/classes/setup/sql/data"/>
Expand Down

0 comments on commit b159cda

Please sign in to comment.