This is an automated email from the git hooks/post-receive script. New commit to branch feature/6271 in repository echobase. See http://git.codelutin.com/echobase.git commit 209e5491f800d839c8e988aea09423a957011f0c Author: Tony CHEMIT <chemit@codelutin.com> Date: Mon Dec 15 15:46:33 2014 +0100 fixes #6269: Problème de chargement de l'écran spatial sur l'application embarquée --- .../EchoBaseUserTopiaApplicationContext.java | 126 +++++++ .../services/service/spatial/GisService.java | 10 + .../service/spatial/SpatialDataService.java | 141 ++++++++ .../services/service/spatial/SpatialService.java | 386 --------------------- .../echobase/ui/actions/spatial/ShowMap.java | 36 -- 5 files changed, 277 insertions(+), 422 deletions(-) diff --git a/echobase-domain/src/main/java/fr/ifremer/echobase/entities/EchoBaseUserTopiaApplicationContext.java b/echobase-domain/src/main/java/fr/ifremer/echobase/entities/EchoBaseUserTopiaApplicationContext.java new file mode 100644 index 0000000..2c9a4b2 --- /dev/null +++ b/echobase-domain/src/main/java/fr/ifremer/echobase/entities/EchoBaseUserTopiaApplicationContext.java @@ -0,0 +1,126 @@ +package fr.ifremer.echobase.entities; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.hibernate.cfg.Environment; + +import java.util.Map; +import java.util.Properties; + +public class EchoBaseUserTopiaApplicationContext extends AbstractEchoBaseUserTopiaApplicationContext { + + /** Logger. */ + private static final Log log = LogFactory.getLog(EchoBaseUserTopiaApplicationContext.class); + + /** + * Is database has spatial support? + * + * @since 2.8 + */ + protected boolean spatialSupport; + + /** + * Is database spatial structures are found? + * + * @since 2.8 + */ + protected boolean spatialStructureFound; + + public EchoBaseUserTopiaApplicationContext(Properties properties) { + super(properties); + initInternalSpatialStates(); + } + + public EchoBaseUserTopiaApplicationContext(Map<String, String> configuration) { + super(configuration); + initInternalSpatialStates(); + } + + @Override + public EchoBaseUserTopiaPersistenceContext newPersistenceContext() { + + EchoBaseUserTopiaPersistenceContext persistenceContext = super.newPersistenceContext(); + persistenceContext.setSpatialSupport(spatialSupport); + persistenceContext.setSpatialStructureFound(spatialStructureFound); + + return persistenceContext; + + } + + public boolean isSpatialStructureFound() { + return spatialStructureFound; + } + + public boolean isSpatialSupport() { + return spatialSupport; + } + + public void initInternalSpatialStates() { + + EchoBaseUserTopiaPersistenceContext persistenceContext = newPersistenceContext(); + + try { + + spatialSupport = computeSpatialSupport(persistenceContext); + + if (log.isInfoEnabled()) { + log.info("spatialSupport: " + spatialSupport); + } + + if (spatialSupport) { + + spatialStructureFound = computeSpatialStructureFound(persistenceContext); + if (log.isInfoEnabled()) { + log.info("spatialStructureFound: " + spatialStructureFound); + } + + } else { + + // no spatial support, so no spatial structure + spatialStructureFound = false; + + } + + } finally { + + persistenceContext.closeContext(); + + } + + } + + private boolean computeSpatialSupport(EchoBaseUserTopiaPersistenceContext persistenceContext) { + + String dialect = persistenceContext.getHibernateSupport().getHibernateConfiguration().getProperty(Environment.DIALECT); + boolean result = DriverType.POSTGRESQL.getDialectClass().getName().equals(dialect); + return result; + + + } + + private boolean computeSpatialStructureFound(EchoBaseUserTopiaPersistenceContext persistenceContext) { + + boolean result; + try { + + persistenceContext.getSqlSupport().executeSql("select count(*) from echobase_cell_spatial;"); + result = true; + + } catch (Exception e) { + + // table not found (or other, ...) + result = false; + + // rollback (otherwise transaction will stay dirty) + persistenceContext.rollback(); + + } + + return result; + + } + + public void setSpatialStructureFound(boolean spatialStructureFound) { + this.spatialStructureFound = spatialStructureFound; + } +} 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 new file mode 100644 index 0000000..60af56e --- /dev/null +++ b/echobase-services/src/main/java/fr/ifremer/echobase/services/service/spatial/GisService.java @@ -0,0 +1,10 @@ +package fr.ifremer.echobase.services.service.spatial; + +/** + * Created on 12/15/14. + * + * @author Tony Chemit - chemit@codelutin.com + * @since XXX + */ +public class GisService { +} diff --git a/echobase-services/src/main/java/fr/ifremer/echobase/services/service/spatial/SpatialDataService.java b/echobase-services/src/main/java/fr/ifremer/echobase/services/service/spatial/SpatialDataService.java new file mode 100644 index 0000000..6132501 --- /dev/null +++ b/echobase-services/src/main/java/fr/ifremer/echobase/services/service/spatial/SpatialDataService.java @@ -0,0 +1,141 @@ +package fr.ifremer.echobase.services.service.spatial; + +/* + * #%L + * EchoBase :: Services + * %% + * Copyright (C) 2011 - 2013 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ + +import fr.ifremer.echobase.EchoBaseTechnicalException; +import fr.ifremer.echobase.entities.EchoBaseUserPersistenceContext; +import fr.ifremer.echobase.io.EchoBaseIOUtil; +import fr.ifremer.echobase.services.EchoBaseServiceSupport; +import fr.ifremer.echobase.services.service.UserDbPersistenceService; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.util.TimeLog; + +import javax.inject.Inject; + +/** + * Spatial service. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.2 + */ +public class SpatialDataService extends EchoBaseServiceSupport { + + /** Logger. */ + private static final Log log = LogFactory.getLog(SpatialDataService.class); + + public static final TimeLog TILE_LOG = new TimeLog(SpatialDataService.class); + + public static final String POSTGIS_STRUCTURE_SQL = "/postgis-structure.sql"; + + public static final String POSTGIS_VIEW_SQL = "/postgis-view.sql"; + + @Inject + private UserDbPersistenceService persistenceService; + + @Inject + private EchoBaseUserPersistenceContext persistenceContext; + + public void addSpatialSupport() { + + // add spatial structure + executeSqlScript(POSTGIS_STRUCTURE_SQL); + + // add spatial views + executeSqlScript(POSTGIS_VIEW_SQL); + + // do commit + persistenceService.commit(); + + persistenceService.setSpatialStructureFound(); + + } + + /** + * To update the {@code echobase_cell_spatial} table from the filled + * table {@code echobase_cell_sptaial_temp}. + * + * @since 2.2 + */ + public void updatePostgisTable() { + + if (persistenceContext.isSpatialStructureFound()) { + + // try the update only for postgresql + try { + if (log.isInfoEnabled()) { + log.info("Will try to compute operation spatial data from temp table..."); + } + persistenceService.executeSQL("SELECT echobase_fill_operation_spatial_table();"); + if (log.isInfoEnabled()) { + log.info("Will try to compute cell spatial data from temp table..."); + } + persistenceService.executeSQL("SELECT echobase_fill_cell_spatial_table();"); + persistenceService.commit(); + } catch (Exception e) { + throw new EchoBaseTechnicalException("Could not compute spatial data", e); + } + } + } + + + /** + * To update all posgis materialized views.. + * + * @since 2.6 + */ + public void updatePostgisViews() { + + if (persistenceContext.isSpatialStructureFound()) { + + // try the update only for postgresql + try { + if (log.isInfoEnabled()) { + log.info("Will try to refresh all spatial views..."); + } + persistenceService.executeSQL("SELECT echobase_refresh_views();"); + persistenceService.commit(); + } catch (Exception e) { + throw new EchoBaseTechnicalException("Could not refresh spatial views", e); + } + } + } + + protected void executeSqlScript(String scriptPath) { + + // get sql script + String sql = EchoBaseIOUtil.loadScript(scriptPath); + + if (log.isInfoEnabled()) { + log.info("Will execute sql file " + scriptPath); + } + + try { + persistenceService.executeSQL(sql); + } catch (Exception e) { + throw new EchoBaseTechnicalException( + "Could not execute sql file " + scriptPath, e); + } + } + + +} diff --git a/echobase-services/src/main/java/fr/ifremer/echobase/services/service/spatial/SpatialService.java b/echobase-services/src/main/java/fr/ifremer/echobase/services/service/spatial/SpatialService.java deleted file mode 100644 index 4e58a46..0000000 --- a/echobase-services/src/main/java/fr/ifremer/echobase/services/service/spatial/SpatialService.java +++ /dev/null @@ -1,386 +0,0 @@ -package fr.ifremer.echobase.services.service.spatial; - -/* - * #%L - * EchoBase :: Services - * %% - * Copyright (C) 2011 - 2013 Ifremer, Codelutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ - -import com.google.common.base.Charsets; -import com.google.common.base.Preconditions; -import fr.ifremer.echobase.EchoBaseTechnicalException; -import fr.ifremer.echobase.entities.WorkingDbConfiguration; -import fr.ifremer.echobase.entities.data.Voyage; -import fr.ifremer.echobase.io.EchoBaseIOUtil; -import fr.ifremer.echobase.persistence.JdbcConfiguration; -import fr.ifremer.echobase.services.EchoBaseServiceSupport; -import fr.ifremer.echobase.services.service.UserDbPersistenceService; -import fr.ifremer.echobase.services.service.workingDb.WorkingDbConfigurationService; -import org.apache.commons.configuration.ConfigurationException; -import org.apache.commons.configuration.HierarchicalINIConfiguration; -import org.apache.commons.configuration.SubnodeConfiguration; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.util.TimeLog; - -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.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.Scanner; - -/** - * Spatial service. - * - * @author tchemit <chemit@codelutin.com> - * @since 2.2 - */ -public class SpatialService extends EchoBaseServiceSupport { - - /** Logger. */ - private static final Log log = LogFactory.getLog(SpatialService.class); - - public static final TimeLog TILE_LOG = new TimeLog(SpatialService.class); - - public static final String POSTGIS_STRUCTURE_SQL = "/postgis-structure.sql"; - - public static final String POSTGIS_VIEW_SQL = "/postgis-view.sql"; - - public static final String[] TEMPLATE_MARKUP = {"{{dbname}}", "{{host}}", "{{port}}", "{{userName}}", "{{password}}", "{{voyageName}}", "{{voyageId}}", "{{resourcesPath}}" }; - - public static final String START_JDBC_URL = "jdbc:postgresql://"; - - @Inject - private UserDbPersistenceService persistenceService; - - @Inject - private WorkingDbConfigurationService workingDbConfigurationService; - - - public void addSpatialSupport() { - - // add spatial structure - executeSqlScript(POSTGIS_STRUCTURE_SQL); - - // add spatial views - executeSqlScript(POSTGIS_VIEW_SQL); - - // do commit - persistenceService.commit(); - } - - protected void executeSqlScript(String scriptPath) { - - // get sql script - String sql = EchoBaseIOUtil.loadScript(scriptPath); - - if (log.isInfoEnabled()) { - log.info("Will execute sql file " + scriptPath); - } - - try { - persistenceService.executeSQL(sql); - } catch (Exception e) { - throw new EchoBaseTechnicalException( - "Could not execute sql file " + scriptPath, e); - } - } - - - public boolean isSpatialAware() { - boolean result = persistenceService.isSpatialAware(); - return result; - } - - public boolean isPostgresql() { - boolean result = persistenceService.isPostgresql(); - return result; - } - - /** - * To update the {@code echobase_cell_spatial} table from the filled - * table {@code echobase_cell_sptaial_temp}. - * - * @since 2.2 - */ - public void updatePostgisTable() { - - if (isSpatialAware()) { - - // try the update only for postgresql - try { - if (log.isInfoEnabled()) { - log.info("Will try to compute operation spatial data from temp table..."); - } - persistenceService.executeSQL("SELECT echobase_fill_operation_spatial_table();"); - if (log.isInfoEnabled()) { - log.info("Will try to compute cell spatial data from temp table..."); - } - persistenceService.executeSQL("SELECT echobase_fill_cell_spatial_table();"); - persistenceService.commit(); - } catch (Exception e) { - throw new EchoBaseTechnicalException("Could not compute spatial data", e); - } - } - } - - /** - * To update all posgis materialized views.. - * - * @since 2.6 - */ - public void updatePostgisViews() { - - if (isSpatialAware()) { - - // try the update only for postgresql - try { - if (log.isInfoEnabled()) { - log.info("Will try to refresh all spatial views..."); - } - persistenceService.executeSQL("SELECT echobase_refresh_views();"); - persistenceService.commit(); - } catch (Exception e) { - throw new EchoBaseTechnicalException("Could not refresh spatial views", e); - } - } - } - - - /** - * Generate maps files for this database. - * @param conf data base connexion configuration - * @return Name of repository maps - */ - public String generateMaps(JdbcConfiguration conf) { - - WorkingDbConfiguration workingDbConfiguration - = workingDbConfigurationService.getWorkingDbConfigurationByUrl(conf.getUrl()); - - File lizmapTarget = getConfiguration().getLizmapTarget(); - - File lizmapConfig = getConfiguration().getLizmapConfig(); - - String lizmapJdbcUrl = getConfiguration().getLizmapJdbcUrl(); - - if (!lizmapTarget.isDirectory()) { - throw new EchoBaseTechnicalException("Map target (" + lizmapTarget.getAbsolutePath() + ") is not directory"); - } - - String repositoryName = getRepositoryName(conf); - File repository = new File(lizmapTarget, repositoryName); - - if (!repository.isDirectory()) { - repository.mkdir(); - } - - // Add repository in Lizmap - - try { - HierarchicalINIConfiguration lizmapIni = new HierarchicalINIConfiguration(lizmapConfig); - SubnodeConfiguration section = lizmapIni.getSection("repository:" + repositoryName); - section.setProperty("label", workingDbConfiguration.getDescription()); - section.setProperty("path", repository.getAbsolutePath() + "/"); - lizmapIni.save(new FileWriter(lizmapConfig)); - - } catch (ConfigurationException e) { - throw new EchoBaseTechnicalException("Could not load Lizmap config", e); - } catch (IOException e) { - throw new EchoBaseTechnicalException("Could not save Lizmap config", e); - } - - // Add authorization - 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);"); - - } catch (SQLException e) { - throw new EchoBaseTechnicalException("Could not create rights in lizmap", e); - } finally { - try { - if (statement != null) { - statement.close(); - } - if (connection != null) { - connection.close(); - } - } catch (SQLException e) { - throw new EchoBaseTechnicalException("Could not create rights in lizmap", e); - } - } - - - for (Voyage voyage : persistenceService.getAllVoyages()) { - generateMap(conf, voyage, repository); - } - - return repositoryName; - } - - protected String getRepositoryName(JdbcConfiguration conf) { - - String url = conf.getUrl(); - - int hostIndex = START_JDBC_URL.length() - 1; - int portIndex = url.indexOf(':', hostIndex); - int dbnameIndex = url.indexOf("/", portIndex); - - String dbname = url.substring(dbnameIndex + 1); - String host = url.substring(hostIndex + 1, portIndex); - String port = url.substring(portIndex + 1, dbnameIndex); - - String repository = host + port + dbname; - - repository = StringUtils.replaceEach(repository, new String[]{"-", "."}, new String[]{"", ""}); - - return repository; - } - - /** - * 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 - * @return Name of project map - */ - protected String generateMap (JdbcConfiguration conf, Voyage voyage, File repository) { - Preconditions.checkNotNull(conf); - Preconditions.checkNotNull(voyage); - - String project = voyage.getName(); - - String[] templateValues = getTemplateValues(conf, voyage); - - generateMapFile(getConfiguration().getQgisTemplate(), repository, project, templateValues); - - generateMapFile(getConfiguration().getLizmapTemplate(), repository, project, templateValues); - - return project; - - } - - /** - * 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 - * @return file generated - */ - protected File generateMapFile(File template, File repository, String project, String[] templateValues) { - - String name = template.getName(); - name = project + name.substring(name.indexOf(".")); - - File target = new File(repository, name); - - if (!target.isFile()) { - - Scanner scanner = null; - BufferedWriter bufferedWriter = null; - try { - scanner = new Scanner(template, Charsets.UTF_8.name()); - - bufferedWriter = new BufferedWriter(new FileWriter(target)); - - String line; - - 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 { - bufferedWriter.close(); - } catch (IOException e) { - throw new EchoBaseTechnicalException(e); - } - } - } - } - - return target; - - } - - /** - * Extract data value to used in template - * @param conf data base connexion configuration - * @param voyage voyage for this map - * @return values table - */ - protected String[] getTemplateValues(JdbcConfiguration conf, Voyage voyage) { - - String[] templateValues = new String[TEMPLATE_MARKUP.length]; - - String url = conf.getUrl(); - if (!url.startsWith(START_JDBC_URL)) { - throw new EchoBaseTechnicalException("JDBC URL '" + url + "' is no reference to a Postrges database"); - } - - int hostIndex = START_JDBC_URL.length() - 1; - int portIndex = url.indexOf(':', hostIndex); - int dbnameIndex = url.indexOf("/", portIndex); - - // dbname - templateValues[0] = url.substring(dbnameIndex + 1); - // host - templateValues[1] = url.substring(hostIndex + 1, portIndex); - // port - templateValues[2] = url.substring(portIndex + 1, dbnameIndex); - // userName - templateValues[3] = conf.getLogin(); - // password - templateValues[4] = conf.getPassword(); - // voyage name - templateValues[5] = voyage.getName(); - // voyage id - templateValues[6] = voyage.getTopiaId(); - // ressourcesPath - templateValues[7] = getConfiguration().getQgisResources().getAbsolutePath(); - - return templateValues; - } - -} diff --git a/echobase-ui/src/main/java/fr/ifremer/echobase/ui/actions/spatial/ShowMap.java b/echobase-ui/src/main/java/fr/ifremer/echobase/ui/actions/spatial/ShowMap.java deleted file mode 100644 index 346ee7f..0000000 --- a/echobase-ui/src/main/java/fr/ifremer/echobase/ui/actions/spatial/ShowMap.java +++ /dev/null @@ -1,36 +0,0 @@ -package fr.ifremer.echobase.ui.actions.spatial; - -/* - * #%L - * EchoBase :: UI - * %% - * Copyright (C) 2011 - 2013 Ifremer, Codelutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ - -import fr.ifremer.echobase.ui.actions.EchoBaseActionSupport; - -/** - * To display only the spatial data map. - * <p/> - * This action will be reused to raster spatial data maps. - * - * @author tchemit <chemit@codelutin.com> - * @since 2.2 - */ -public class ShowMap extends EchoBaseActionSupport { - -} -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.