Skip to content

Commit

Permalink
Fixed Postgres compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Eichar committed Oct 29, 2013
1 parent f42b33d commit c6cc5d3
Show file tree
Hide file tree
Showing 17 changed files with 128 additions and 168 deletions.
29 changes: 15 additions & 14 deletions bin/start.ps1
@@ -1,3 +1,7 @@
param (
[string]$mode = "build"
)

$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
. "$scriptPath\config.ps1"

Expand All @@ -14,21 +18,18 @@ if (!(test-path Env:\JREBEL_HOME)) {
echo "Env:JREBEL_HOME is found. Configuring JRebel. JREBEL_OPTS = $JREBEL_OPTS"
}

$Env:MAVEN_OPTS=""

cmd /c "cd $scriptPath\..\common && mvn install $args"
cmd /c "cd $scriptPath\..\domain && mvn install $args"
cmd /c "cd $JEEVES_DIR && mvn install $args"
cmd /c "cd $scriptPath\..\core && mvn install $args"
cmd /c "cd $scriptPath\..\csw-server && mvn install $args"
cmd /c "cd $scriptPath\..\healthmonitor && mvn install $args"
cmd /c "cd $scriptPath\..\harvesters && mvn install $args"
cmd /c "cd $scriptPath\..\services && mvn install $args"
if ($mode -eq "build") {
$Env:MAVEN_OPTS=""

#if ( ]; then
# echo "[FAILURE] [deploy] Failed to execute 'jeeves' correctly"
# exit -1
#fi
cmd /c "cd $scriptPath\..\common && mvn install $args"
cmd /c "cd $scriptPath\..\domain && mvn install $args"
cmd /c "cd $JEEVES_DIR && mvn install $args"
cmd /c "cd $scriptPath\..\core && mvn install $args"
cmd /c "cd $scriptPath\..\csw-server && mvn install $args"
cmd /c "cd $scriptPath\..\healthmonitor && mvn install $args"
cmd /c "cd $scriptPath\..\harvesters && mvn install $args"
cmd /c "cd $scriptPath\..\services && mvn install $args"
}

$Env:MAVEN_OPTS="$JREBEL_OPTS $DEBUG $OVERRIDES $MEMORY -Dgeonetwork.dir=$DATA_DIR -Dfile.encoding=UTF8"

Expand Down
127 changes: 78 additions & 49 deletions core/src/main/java/org/fao/geonet/lib/DbLib.java
Expand Up @@ -23,14 +23,19 @@

package org.fao.geonet.lib;

import jeeves.server.context.ServiceContext;
import org.fao.geonet.utils.Log;
import org.fao.geonet.Constants;
import org.fao.geonet.constants.Geonet;
import org.springframework.context.ApplicationContext;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.ServletContext;
import javax.sql.DataSource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
Expand All @@ -47,31 +52,64 @@ public class DbLib {

private static final String SQL_EXTENSION = ".sql";

public void insertData(ServletContext servletContext, ServiceContext context, String appPath, String filePath,
String filePrefix) throws Exception {
if(Log.isDebugEnabled(Geonet.DB))
Log.debug(Geonet.DB, "Filling database tables");

List<String> data = loadSqlDataFile(servletContext, context.getApplicationContext(), appPath, filePath, filePrefix);
runSQL(context.getEntityManager(), data, true);
}

public void insertData(ServletContext servletContext, Statement statement, String appPath, String filePath,
String filePrefix) throws Exception {
if(Log.isDebugEnabled(Geonet.DB))
Log.debug(Geonet.DB, "Filling database tables");

List<String> data = loadSqlDataFile(servletContext, statement, appPath, filePath, filePrefix);
runSQL(statement, data);
runSQL(statement, data, true);
}

/**
* SQL File MUST be in UTF-8.
*
* @param statement
* @param sqlFile
* @throws Exception
*/
public void runSQL(ServletContext servletContext, Statement statement, File sqlFile) throws Exception {
runSQL(servletContext, statement, sqlFile, true);
}
private void runSQL(EntityManager entityManager, List<String> data, boolean failOnError) throws Exception {
StringBuffer sb = new StringBuffer();

for (String row : data) {
if (!row.toUpperCase().startsWith("REM") && !row.startsWith("--")
&& !row.trim().equals("")) {
sb.append(" ");
sb.append(row);

if (row.endsWith(";")) {
String sql = sb.toString();

sql = sql.substring(0, sql.length() - 1);

if(Log.isDebugEnabled(Geonet.DB))
Log.debug(Geonet.DB, "Executing " + sql);

try {
final String trimmedSQL = sql.trim();
final Query query = entityManager.createNativeQuery(trimmedSQL);
if (trimmedSQL.startsWith("SELECT")) {
query.setFirstResult(1);
query.getSingleResult();
} else {
query.executeUpdate();
}
} catch (Throwable e) {
Log.warning(Geonet.DB, "SQL failure for: " + sql + ", error is:" + e.getMessage());

if (failOnError)
throw new RuntimeException(e);
}
sb = new StringBuffer();
}
}
}
entityManager.flush();
entityManager.clear();

public void runSQL(ServletContext servletContext, Statement statement, File sqlFile, boolean failOnError) throws Exception {
List<String> data = Lib.text.load(servletContext, sqlFile.getCanonicalPath(), Constants.ENCODING);
runSQL(statement, data, failOnError);
}

private void runSQL(Statement statement, List<String> data, boolean failOnError) throws Exception {
StringBuffer sb = new StringBuffer();

Expand All @@ -88,7 +126,7 @@ private void runSQL(Statement statement, List<String> data, boolean failOnError)

if(Log.isDebugEnabled(Geonet.DB))
Log.debug(Geonet.DB, "Executing " + sql);

try {
if (sql.trim().startsWith("SELECT")) {
statement.executeQuery(sql).close();
Expand All @@ -99,39 +137,14 @@ private void runSQL(Statement statement, List<String> data, boolean failOnError)
Log.warning(Geonet.DB, "SQL failure for: " + sql + ", error is:" + e.getMessage());

if (failOnError)
throw e;
throw e;
}
sb = new StringBuffer();
}
}
}
statement.getConnection().commit();
}
private void runSQL(Statement statement, List<String> data) throws Exception {
runSQL(statement, data, true);
}

/**
* Execute query and commit
*
* @param statement
* @param query
* @return
*/
private boolean safeExecute(Statement statement, String query) {
try {
statement.execute(query);

// --- as far as I remember, PostgreSQL needs a commit even for DDL
statement.getConnection().commit();

return true;
} catch (SQLException e) {
if(Log.isDebugEnabled(Geonet.DB))
Log.debug(Geonet.DB, "Safe execute error: " + query + ", error is:" + e.getMessage());
return false;
}
}

/**
* Check if db specific SQL script exist, if not return default SQL script path.
Expand Down Expand Up @@ -181,14 +194,30 @@ private String testPath(String dbFilePath) {
return null;
}

private List<String> loadSqlDataFile(ServletContext servletContext, ApplicationContext appContext, String appPath, String filePath, String filePrefix)
throws IOException, SQLException {
final DataSource dataSource = appContext.getBean(DataSource.class);
Connection connection = null;
try {
connection = dataSource.getConnection();
// --- find out which dbms data file to load
String file = checkFilePath(servletContext, appPath, filePath, filePrefix, DatabaseType.lookup(connection).toString());

// --- load the sql data
return Lib.text.load(servletContext, appPath, file, Constants.ENCODING);
} finally {
if (connection != null) {
connection.close();
}
}
}
private List<String> loadSqlDataFile(ServletContext servletContext, Statement statement, String appPath, String filePath, String filePrefix)
throws IOException, SQLException {
// --- find out which dbms data file to load
String file = checkFilePath(servletContext, appPath, filePath, filePrefix, DatabaseType.lookup(statement.getConnection()).toString());

throws FileNotFoundException, IOException, SQLException {
// --- find out which dbms data file to load
String file = checkFilePath(servletContext, appPath, filePath, filePrefix, DatabaseType.lookup(statement.getConnection()).toString());

// --- load the sql data
return Lib.text.load(servletContext, appPath, file, Constants.ENCODING);
// --- load the sql data
return Lib.text.load(servletContext, appPath, file, Constants.ENCODING);
}

private String getObjectName(String createStatem) {
Expand Down

This file was deleted.

@@ -1,5 +1,7 @@
package org.fao.geonet.domain;

import org.hibernate.annotations.Type;

import javax.persistence.*;

/**
Expand Down Expand Up @@ -102,6 +104,7 @@ public CswCapabilitiesInfoField setFieldName(final String fieldName) {
*/
@Lob
@Column(name = "label")
@Type(type="org.hibernate.type.StringClobType") // this is a work around for postgres so postgres can correctly load clobs
public String getValue() {
return _value;
}
Expand Down
Expand Up @@ -2,6 +2,7 @@

import org.fao.geonet.utils.Log;
import org.fao.geonet.utils.Xml;
import org.hibernate.annotations.Type;
import org.jdom.Content;
import org.jdom.Element;
import org.jdom.JDOMException;
Expand Down Expand Up @@ -204,6 +205,7 @@ public HarvestHistory setDeleted(boolean deleted) {
* @return the harvester info.
*/
@Lob
@Type(type="org.hibernate.type.StringClobType") // this is a work around for postgres so postgres can correctly load clobs
public String getInfo() {
return _info;
}
Expand Down Expand Up @@ -257,6 +259,7 @@ public HarvestHistory setInfo(@Nullable Element info) {
* @return the parameters used for performing the harvesting.
*/
@Lob
@Type(type="org.hibernate.type.StringClobType") // this is a work around for postgres so postgres can correctly load clobs
public String getParams() {
return _params;
}
Expand Down
@@ -1,5 +1,7 @@
package org.fao.geonet.domain;

import org.hibernate.annotations.Type;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.persistence.*;
Expand Down Expand Up @@ -107,6 +109,7 @@ HarvesterSetting setName(@Nonnull String name) {
*/
@Lob
@Column(name = "value", nullable = true)
@Type(type="org.hibernate.type.StringClobType") // this is a work around for postgres so postgres can correctly load clobs
public
@Nullable
String getValue() {
Expand Down
2 changes: 2 additions & 0 deletions domain/src/main/java/org/fao/geonet/domain/Metadata.java
Expand Up @@ -3,6 +3,7 @@
import com.vividsolutions.jts.util.Assert;
import org.apache.lucene.document.Document;
import org.fao.geonet.utils.Xml;
import org.hibernate.annotations.Type;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.Format;
Expand Down Expand Up @@ -102,6 +103,7 @@ public Metadata setUuid(@Nonnull String uuid) {
@Column(nullable = false)
@Lob
@Basic(fetch = FetchType.LAZY)
@Type(type="org.hibernate.type.StringClobType") // this is a work around for postgres so postgres can correctly load clobs
public String getData() {
return _data;
}
Expand Down
@@ -1,5 +1,7 @@
package org.fao.geonet.domain;

import org.hibernate.annotations.Type;

import javax.persistence.*;

/**
Expand Down Expand Up @@ -126,6 +128,7 @@ public MetadataNotification setAction(MetadataNotificationAction action) {
*/
@Lob
@Column(name = "errormsg")
@Type(type="org.hibernate.type.StringClobType") // this is a work around for postgres so postgres can correctly load clobs
public String getErrorMessage() {
return _errorMessage;
}
Expand Down

0 comments on commit c6cc5d3

Please sign in to comment.