This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository echobase. See http://git.codelutin.com/echobase.git commit 26be897ee9aea6bb4739a377a267f51225b66575 Author: Tony CHEMIT <chemit@codelutin.com> Date: Wed Dec 17 08:21:43 2014 +0100 refs #6271 code review --- .../services/service/spatial/GisService.java | 180 +++++++++++++-------- 1 file changed, 116 insertions(+), 64 deletions(-) diff --git a/echobase-services/src/main/java/fr/ifremer/echobase/services/service/spatial/GisService.java b/echobase-services/src/main/java/fr/ifremer/echobase/services/service/spatial/GisService.java index 12e5495..a152794 100644 --- a/echobase-services/src/main/java/fr/ifremer/echobase/services/service/spatial/GisService.java +++ b/echobase-services/src/main/java/fr/ifremer/echobase/services/service/spatial/GisService.java @@ -23,6 +23,7 @@ package fr.ifremer.echobase.services.service.spatial; import com.google.common.base.Charsets; import com.google.common.base.Preconditions; +import com.google.common.io.Files; import fr.ifremer.echobase.EchoBaseTechnicalException; import fr.ifremer.echobase.config.EchoBaseConfiguration; import fr.ifremer.echobase.entities.WorkingDbConfiguration; @@ -36,6 +37,7 @@ import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.HierarchicalINIConfiguration; import org.apache.commons.configuration.SubnodeConfiguration; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -43,8 +45,6 @@ import org.apache.commons.logging.LogFactory; import javax.inject.Inject; import java.io.BufferedWriter; import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileWriter; import java.io.IOException; import java.net.URL; import java.sql.Connection; @@ -66,13 +66,17 @@ public class GisService extends EchoBaseServiceSupport { /** Logger. */ private static final Log log = LogFactory.getLog(GisService.class); - public static final String[] TEMPLATE_MARKUP = {"{{dbname}}", "{{host}}", "{{port}}", "{{userName}}", "{{password}}", "{{voyageName}}", "{{voyageId}}", "{{resourcesPath}}"}; + private static final String[] TEMPLATE_MARKUP = {"{{dbname}}", "{{host}}", "{{port}}", "{{userName}}", "{{password}}", "{{voyageName}}", "{{voyageId}}", "{{resourcesPath}}"}; private static final String START_JDBC_URL = "jdbc:postgresql://"; private static final String GIS_INTERNAL_PATH = "/gis/templates/"; - private static final String QGIS_RESOURCES = "/lizmap/resources/"; + private static final String QGIS_RESOURCES = "/gis/lizmap/resources/"; + + private static final String[] TO_REPLACE = new String[]{"-", "."}; + + private static final String[] REPLACEMENT_LIST = new String[]{"", ""}; @Inject private WorkingDbConfigurationService workingDbConfigurationService; @@ -146,17 +150,21 @@ public class GisService extends EchoBaseServiceSupport { String repositoryName = getRepositoryName(conf); - File repository = new File(lizmapTarget, repositoryName); - EchoBaseIOUtil.forceMkdir(repository); + if (log.isInfoEnabled()) { + log.info("Lizmap repository name: " + repositoryName); + } + + File repositoryDirectory = new File(lizmapTarget, repositoryName); + EchoBaseIOUtil.forceMkdir(repositoryDirectory); // Add repository in Lizmap config - updateLizmapConfigFile(conf, repositoryName, repository); + updateLizmapConfigFile(conf, repositoryName, repositoryDirectory); // Add authorization in Lizmap database updateLizmapAuthorizations(repositoryName); for (Voyage voyage : persistenceService.getAllVoyages()) { - generateMap(conf, voyage, repository); + generateMap(conf, voyage, repositoryDirectory); } } @@ -174,15 +182,24 @@ public class GisService extends EchoBaseServiceSupport { WorkingDbConfiguration workingDbConfiguration = workingDbConfigurationService.getWorkingDbConfigurationByUrl(conf.getUrl()); - File lizmapConfig = getConfiguration().getLizmapConfigFile(); + File lizmapConfigFile = getConfiguration().getLizmapConfigFile(); try { - HierarchicalINIConfiguration lizmapIni = new HierarchicalINIConfiguration(lizmapConfig); + HierarchicalINIConfiguration lizmapIni = new HierarchicalINIConfiguration(lizmapConfigFile); + SubnodeConfiguration section = lizmapIni.getSection("repository:" + repositoryName); section.setProperty("label", workingDbConfiguration.getDescription()); section.setProperty("path", repository.getAbsolutePath() + "/"); - lizmapIni.save(new FileWriter(lizmapConfig)); + + BufferedWriter writer = Files.newWriter(lizmapConfigFile, Charsets.UTF_8); + try { + lizmapIni.save(writer); + writer.close(); + } finally { + + IOUtils.closeQuietly(writer); + } } catch (ConfigurationException e) { throw new EchoBaseTechnicalException("Could not load Lizmap config", e); @@ -196,40 +213,66 @@ public class GisService extends EchoBaseServiceSupport { String lizmapJdbcUrl = getConfiguration().getLizmapJdbcUrl(); Connection connection = null; - Statement statement = null; try { connection = DriverManager.getConnection(lizmapJdbcUrl); - statement = connection.createStatement(); - statement.execute("INSERT OR REPLACE INTO jacl2_rights " + - "(id_aclsbj, id_aclgrp, id_aclres, canceled) " + - "VALUES " + - "('lizmap.repositories.view', '__anonymous', '" + repositoryName + "', 0);"); - statement.execute("INSERT OR REPLACE INTO jacl2_rights " + - "(id_aclsbj, id_aclgrp, id_aclres, canceled) " + - "VALUES " + - "('lizmap.repositories.view', 'admins', '" + repositoryName + "', 0);"); + + executeQuery(connection, "INSERT OR REPLACE INTO jacl2_rights " + + "(id_aclsbj, id_aclgrp, id_aclres, canceled) " + + "VALUES " + + "('lizmap.repositories.view', '__anonymous', '" + repositoryName + "', 0);"); + + executeQuery(connection, "INSERT OR REPLACE INTO jacl2_rights " + + "(id_aclsbj, id_aclgrp, id_aclres, canceled) " + + "VALUES " + + "('lizmap.repositories.view', 'admins', '" + repositoryName + "', 0);"); + + connection.commit(); + + connection.close(); } catch (SQLException e) { throw new EchoBaseTechnicalException("Could not create rights in lizmap", e); } finally { try { - if (statement != null) { - statement.close(); - } - if (connection != null) { + if (connection != null && !connection.isClosed()) { connection.close(); } } catch (SQLException e) { + //FIXME We should never throw an exception in finally block throw new EchoBaseTechnicalException("Could not create rights in lizmap", e); } } } + protected void executeQuery(Connection connection, String query) throws SQLException { + + if (log.isInfoEnabled()) { + log.info("Execute sql query to lizmap: " + query); + } + + Statement statement = connection.createStatement(); + + try { + statement.execute(query); + + statement.close(); + + } finally { + + if (statement != null) { + statement.close(); + } + + } + + } + protected String getRepositoryName(JdbcConfiguration conf) { String url = conf.getUrl(); + //TODO Use a regex int hostIndex = START_JDBC_URL.length() - 1; int portIndex = url.indexOf(':', hostIndex); int dbnameIndex = url.indexOf("/", portIndex); @@ -238,9 +281,9 @@ public class GisService extends EchoBaseServiceSupport { String host = url.substring(hostIndex + 1, portIndex); String port = url.substring(portIndex + 1, dbnameIndex); - String repository = host + port + dbname; + String repository = getConfiguration().getLizmapRepositoryName() + host + port + dbname; - repository = StringUtils.replaceEach(repository, new String[]{"-", "."}, new String[]{"", ""}); + repository = StringUtils.replaceEach(repository, TO_REPLACE, REPLACEMENT_LIST); return repository; } @@ -248,73 +291,81 @@ public class GisService extends EchoBaseServiceSupport { /** * Generate map files for this database and this voyage. * - * @param conf data base connexion configuration - * @param voyage voyage for this map - * @param repository Lizmap repository + * @param conf data base connexion configuration + * @param voyage voyage for this map + * @param repositoryDirectory Lizmap repository * @return Name of project map */ - protected String generateMap(JdbcConfiguration conf, Voyage voyage, File repository) { + protected String generateMap(JdbcConfiguration conf, Voyage voyage, File repositoryDirectory) { + Preconditions.checkNotNull(conf); Preconditions.checkNotNull(voyage); - String project = voyage.getName(); + String voyageName = voyage.getName(); String[] templateValues = getTemplateValues(conf, voyage); - generateMapFile(getConfiguration().getQgisTemplateFile(), repository, project, templateValues); + generateFileFromTemplate(getConfiguration().getQgisTemplateFile(), repositoryDirectory, voyageName, templateValues); - generateMapFile(getConfiguration().getLizmapTemplateFile(), repository, project, templateValues); + generateFileFromTemplate(getConfiguration().getLizmapTemplateFile(), repositoryDirectory, voyageName, templateValues); - return project; + return voyageName; } /** * Generate file from template and values * - * @param template file template to used - * @param repository lizmap repository - * @param project project name - * @param templateValues values to used in the template + * @param templateFile file template to used + * @param repositoryDirectory lizmap repository + * @param voyageName project name + * @param templateValues values to used in the template * @return file generated */ - public File generateMapFile(File template, File repository, String project, String[] templateValues) { + public File generateFileFromTemplate(File templateFile, File repositoryDirectory, String voyageName, String... templateValues) { - String name = template.getName(); - name = project + name.substring(name.indexOf(".")); + String fileName = voyageName + "." + Files.getFileExtension(templateFile.getAbsolutePath()); + File target = new File(repositoryDirectory, fileName); - File target = new File(repository, name); + if (!target.exists()) { - if (!target.isFile()) { + if (log.isInfoEnabled()) { + log.info("Generate file: " + target); + } - Scanner scanner = null; - BufferedWriter bufferedWriter = null; try { - scanner = new Scanner(template, Charsets.UTF_8.name()); - bufferedWriter = new BufferedWriter(new FileWriter(target)); + Scanner scanner = new Scanner(templateFile, Charsets.UTF_8.name()); - String line; + try { + + BufferedWriter bufferedWriter = Files.newWriter(target, Charsets.UTF_8); - while (scanner.hasNextLine()) { - line = scanner.nextLine(); - line = StringUtils.replaceEach(line, TEMPLATE_MARKUP, templateValues); - bufferedWriter.write(line); - bufferedWriter.newLine(); - } - } catch (FileNotFoundException e) { - throw new EchoBaseTechnicalException(e); - } catch (IOException e) { - throw new EchoBaseTechnicalException(e); - } finally { - scanner.close(); - if (bufferedWriter != null) { try { + + while (scanner.hasNextLine()) { + + String line = scanner.nextLine(); + line = StringUtils.replaceEach(line, TEMPLATE_MARKUP, templateValues); + bufferedWriter.write(line); + bufferedWriter.newLine(); + + } + bufferedWriter.close(); - } catch (IOException e) { - throw new EchoBaseTechnicalException(e); + + } finally { + IOUtils.closeQuietly(bufferedWriter); } + + scanner.close(); + + } finally { + IOUtils.closeQuietly(scanner); } + + } catch (IOException e) { + throw new EchoBaseTechnicalException(e); } } @@ -338,6 +389,7 @@ public class GisService extends EchoBaseServiceSupport { throw new EchoBaseTechnicalException("JDBC URL '" + url + "' is no reference to a Postrges database"); } + //TODO Use a regex int hostIndex = START_JDBC_URL.length() - 1; int portIndex = url.indexOf(':', hostIndex); int dbnameIndex = url.indexOf("/", portIndex); -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.