Author: tchemit Date: 2011-12-26 20:07:07 +0100 (Mon, 26 Dec 2011) New Revision: 216 Url: http://forge.codelutin.com/repositories/revision/echobase/216 Log: - rename ExportSql to Exportquery - implements DbExportService - introduce a nive abstract long action configuration class Added: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/AbstractEchobaseActionConfiguration.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportDbConfiguration.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportQueryService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/ExportQueryCsvModel.java Removed: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportSqlService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/ExportSqlCsvModel.java Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DbExportService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EmbeddedApplicationService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataConfiguration.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDbConfiguration.java Added: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/AbstractEchobaseActionConfiguration.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/AbstractEchobaseActionConfiguration.java (rev 0) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/AbstractEchobaseActionConfiguration.java 2011-12-26 19:07:07 UTC (rev 216) @@ -0,0 +1,97 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 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% + */ +package fr.ifremer.echobase.services; + +import org.nuiton.util.StringUtil; + +import java.io.IOException; +import java.io.Serializable; +import java.util.Date; + +/** + * Abstract long action configuration. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.2 + */ +public abstract class AbstractEchobaseActionConfiguration implements Serializable { + + private static final long serialVersionUID = 1L; + + private int nbSteps; + + private float stepIncrement; + + private float progression; + + private Date startTime; + private Date endTime; + + public final int getNbSteps() { + return nbSteps; + } + + public final float getProgression() { + return progression; + } + + public final void setProgression(float progression) { + this.progression = progression; + } + + public final void incrementsProgression() { + setProgression(progression + stepIncrement); + } + + public final void setNbSteps(int nbSteps) { + + this.nbSteps = nbSteps; + stepIncrement = 100f / nbSteps; + } + + public final Date getStartTime() { + return startTime; + } + + public final void setStartTime(Date startTime) { + this.startTime = startTime; + } + + public final Date getEndTime() { + return endTime; + } + + public final void setEndTime(Date endTime) { + this.endTime = endTime; + } + + public String getActionTime() { + Date sTime = getStartTime(); + Date eTime = getEndTime(); + return StringUtil.convertTime(eTime.getTime() - sTime.getTime()); + } + + public void destroy() throws IOException { + } +} Property changes on: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/AbstractEchobaseActionConfiguration.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DbExportService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DbExportService.java 2011-12-26 10:13:15 UTC (rev 215) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DbExportService.java 2011-12-26 19:07:07 UTC (rev 216) @@ -24,16 +24,20 @@ package fr.ifremer.echobase.services; import fr.ifremer.echobase.EchoBaseIOUtil; +import fr.ifremer.echobase.entities.EchoBaseEntityEnum; import fr.ifremer.echobase.entities.EntitiesUtil; import fr.ifremer.echobase.entities.meta.AssociationMeta; import fr.ifremer.echobase.entities.meta.MetaFilenameAware; import fr.ifremer.echobase.entities.meta.TableMeta; -import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.nuiton.util.FileUtil; import org.nuiton.util.TimeLog; import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; /** * Service to import / export a complete db. @@ -52,24 +56,43 @@ /** * Export the complete db into csv files stored in a zip archive file. * - * @param fileName file name of the zip to create + * @param model file name of the zip to create * @return the zip archive file + * @throws IOException if could not create or write files. */ - public File exportDb(String fileName) { - File tempDirectory = new File(FileUtils.getTempDirectory(), - "echobase-exportDb_" + System.nanoTime()); - tempDirectory.deleteOnExit(); + public File exportDb(ExportDbConfiguration model) throws IOException { + String fileName = model.getFileName(); + + File tempDirectory = model.getWorkingDirectory(); + File zipFile = new File(tempDirectory, fileName + ".zip"); + if (log.isInfoEnabled()) { + log.info("Will export db to " + zipFile); + } + model.setExportFile(zipFile); + ExportService exportService = getService(ExportService.class); File dir = new File(tempDirectory, "echobase"); + FileUtil.createDirectoryIfNecessary(dir); + MetaFilenameAware[] entries = EntitiesUtil.getEntries(getDbMeta()); + + model.setNbSteps(entries.length); + + List<EchoBaseEntityEnum> referenceTypes = + Arrays.asList(EntitiesUtil.getReferenceTypes()); for (MetaFilenameAware entry : entries) { + model.incrementsProgression(); + +// if (!referenceTypes.contains(entry.getSource())) { +// //TODO remove this when tests are ok. +// continue; +// } File entryFile = new File(dir, entry.getFilename()); - if (entry instanceof AssociationMeta) { AssociationMeta associationMeta = (AssociationMeta) entry; exportService.exportDatas(associationMeta, entryFile); Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EmbeddedApplicationService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EmbeddedApplicationService.java 2011-12-26 10:13:15 UTC (rev 215) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EmbeddedApplicationService.java 2011-12-26 19:07:07 UTC (rev 216) @@ -203,11 +203,11 @@ UserService.DEFAULT_ADMIN_EMAIL); // get export query service - ExportSqlService exportSqlService = - getService(ExportSqlService.class); + ExportQueryService exportQueryService = + getService(ExportQueryService.class); // get all export queries from application - List<ExportQuery> queries = exportSqlService.getEntities(ExportQuery.class); + List<ExportQuery> queries = exportQueryService.getEntities(ExportQuery.class); // remove link to application user for (ExportQuery query : queries) { @@ -218,15 +218,15 @@ getTransaction().replicateEntities(topiaContext, queries); // create export sql service from h2 db - exportSqlService = - newServiceContext.newService(ExportSqlService.class); + exportQueryService = + newServiceContext.newService(ExportQueryService.class); // get all queries from h2 db - queries = exportSqlService.getEntities(ExportQuery.class); + queries = exportQueryService.getEntities(ExportQuery.class); // attach them to default created admin user in the db for (ExportQuery query : queries) { - exportSqlService.createOrUpdate(query, admin); + exportQueryService.createOrUpdate(query, admin); } // replicate referentiel to new h2 db Added: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportDbConfiguration.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportDbConfiguration.java (rev 0) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportDbConfiguration.java 2011-12-26 19:07:07 UTC (rev 216) @@ -0,0 +1,78 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 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% + */ +package fr.ifremer.echobase.services; + +import org.apache.commons.io.FileUtils; + +import java.io.File; +import java.io.IOException; + +/** + * Configuration of a complete db export. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.2 + */ +public class ExportDbConfiguration extends AbstractEchobaseActionConfiguration { + + private static final long serialVersionUID = 1L; + + private String fileName; + + private File workingDirectory; + + private File exportFile; + + public String getFileName() { + return fileName; + } + + public void setFileName(String fileName) { + this.fileName = fileName; + } + + public File getWorkingDirectory() { + return workingDirectory; + } + + public void setWorkingDirectory(File workingDirectory) { + this.workingDirectory = workingDirectory; + } + + public File getExportFile() { + return exportFile; + } + + public void setExportFile(File exportFile) { + this.exportFile = exportFile; + } + + @Override + public void destroy() throws IOException { + if (workingDirectory != null) { + FileUtils.deleteDirectory(workingDirectory); + } + } + +} \ No newline at end of file Property changes on: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportDbConfiguration.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportQueryService.java (from rev 211, trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportSqlService.java) =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportQueryService.java (rev 0) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportQueryService.java 2011-12-26 19:07:07 UTC (rev 216) @@ -0,0 +1,253 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 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% + */ +package fr.ifremer.echobase.services; + +import fr.ifremer.echobase.EchoBaseTechnicalException; +import fr.ifremer.echobase.entities.EchoBaseUser; +import fr.ifremer.echobase.entities.ExportQuery; +import fr.ifremer.echobase.entities.ExportQueryImpl; +import fr.ifremer.echobase.services.models.ExportQueryCsvModel; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.framework.TopiaContextImplementor; +import org.nuiton.topia.framework.TopiaSQLQuery; +import org.nuiton.topia.persistence.TopiaDAO; +import org.nuiton.util.csv.Export; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; +import java.util.Map; + +/** + * Service to deal with sql export. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.1 + */ +public class ExportQueryService extends EchoBaseServiceSupport { + + /** Logger. */ + private static final Log log = LogFactory.getLog(ExportQueryService.class); + + public ExportQuery getExportQuery(String topiaId) { + ExportQuery exportQuery = getEntityById(ExportQuery.class, topiaId); + ExportQuery entityToSave = new ExportQueryImpl(); + entityToSave.setTopiaId(exportQuery.getTopiaId()); + entityToSave.setName(exportQuery.getName()); + entityToSave.setDescription(exportQuery.getDescription()); + entityToSave.setSqlQuery(exportQuery.getSqlQuery()); + entityToSave.setLastModifiedDate(exportQuery.getLastModifiedDate()); + entityToSave.setLastModifiedUser(exportQuery.getLastModifiedUser()); + return entityToSave; + } + + public ExportQuery createOrUpdate(ExportQuery exportQuery, + EchoBaseUser user) { + try { + TopiaDAO<ExportQuery> dao = getDAO(ExportQuery.class); + ExportQuery entityToSave; + + // No id, creating new one entity + String id = exportQuery.getTopiaId(); + if (StringUtils.isEmpty(id)) { + entityToSave = dao.create(); + } else { + entityToSave = dao.findByTopiaId(id); + } + + entityToSave.setName(exportQuery.getName()); + entityToSave.setDescription(exportQuery.getDescription()); + entityToSave.setSqlQuery(exportQuery.getSqlQuery()); + entityToSave.setLastModifiedDate(newDate()); + entityToSave.setLastModifiedUser(user.getEmail()); + + dao.update(entityToSave); + getTransaction().commitTransaction(); + return entityToSave; + } catch (TopiaException eee) { + throw new EchoBaseTechnicalException(eee); + } + } + + public void delete(String topiaId) { + try { + TopiaDAO<ExportQuery> dao = getDAO(ExportQuery.class); + ExportQuery entityToDelete = dao.findByTopiaId(topiaId); + dao.delete(entityToDelete); + + getTransaction().commitTransaction(); + } catch (TopiaException eee) { + throw new EchoBaseTechnicalException(eee); + } + } + + public Map<String, Object>[] executeSql(String sql, Pager pager) { + + // get a query to count all rows for the request + GenericSQLQuery sqlQuery = new GenericSQLQuery(sql, pager); + try { + List<Map<String, Object>> result = + sqlQuery.getResult(getTransaction()); + return result.toArray(new Map[result.size()]); + } catch (TopiaException eee) { + throw new EchoBaseTechnicalException( + "Could not execute sql query", eee); + } + } + + public String[] getColumnNames(String sql) { + + String limitSql = sql.trim(); + if (limitSql.endsWith(";")) { + limitSql = limitSql.substring(0, limitSql.length() - 1); + limitSql += " LIMIT 1"; + } + try { + // do a limit query to one result and obtain the column names + // from the meta data of the result set + GenericSQLQuery sqlQuery = new GenericSQLQuery(limitSql, null); + String[] result = sqlQuery.getColumnNames(getTransaction()); + return result; + } catch (TopiaException eee) { + throw new EchoBaseTechnicalException( + "Could not execute query " + limitSql + " for reason " + eee.getCause().getMessage(), eee); + } catch (Exception eee) { + throw new EchoBaseTechnicalException( + "Could not execute query " + limitSql + " for reason " + eee.getMessage(), eee); + } + } + + public String createCsvFileContent(String sql) { + + GenericSQLQuery sqlQuery = new GenericSQLQuery(sql, null); + List<Map<String, Object>> rows; + try { + rows = sqlQuery.getResult(getTransaction()); + } catch (TopiaException eee) { + throw new EchoBaseTechnicalException( + "Could not execute sql query", eee); + } + char csvSeparator = getConfiguration().getCsvSeparator(); + ExportQueryCsvModel csvModel = sqlQuery.generateCsvModel(csvSeparator); + Export<Map<String, Object>> exporter = Export.newExport(csvModel, rows); + try { + String content = exporter.startExportAsString(); + return content; + } catch (Exception eee) { + throw new EchoBaseTechnicalException("Could not export sql", eee); + } + } + + private static class GenericSQLQuery extends TopiaSQLQuery<Map<String, Object>> { + + protected String[] columnNames; + + private final String sql; + + private final Pager pager; + + public GenericSQLQuery(String sql, Pager pager) { + this.sql = sql; + this.pager = pager; + } + + public String[] getColumnNames() { + return columnNames; + } + + public ExportQueryCsvModel generateCsvModel(char charSeparator) { + ExportQueryCsvModel model = new ExportQueryCsvModel( + charSeparator, + columnNames + ); + return model; + } + + public List<Map<String, Object>> getResult(TopiaContext tx) throws TopiaException { + List<Map<String, Object>> rows = + findMultipleResult((TopiaContextImplementor) tx); + return rows; + } + + public String[] getColumnNames(TopiaContext tx) throws TopiaException { + findSingleResult((TopiaContextImplementor) tx); + return columnNames; + } + + @Override + protected PreparedStatement prepareQuery(Connection connection) throws SQLException { + return connection.prepareStatement( + sql, + ResultSet.TYPE_SCROLL_INSENSITIVE, + ResultSet.CONCUR_READ_ONLY + ); + } + + @Override + protected void afterExecuteQuery(ResultSet set) throws SQLException { + super.afterExecuteQuery(set); + + // obtain columnNames + columnNames = getColumnNames(set); + + if (pager != null) { + + // must count rows + int nbRows = getNbRows(set); + if (log.isInfoEnabled()) { + log.info("For request " + sql + ", nb rows = " + nbRows); + } + pager.setRecords(nbRows); + pager.computeIndexesAndPageCount(); + } + } + + @Override + protected Map<String, Object> prepareResult(ResultSet set) throws SQLException { + + if (pager != null) { + + // get row number (getRow() begins at 1) + int rowNumber = set.getRow() - 1; + if (rowNumber < pager.getStartIndex() || + rowNumber >= pager.getEndIndex()) { + + // out of pager bound, by returning null + // result will not be take in account + return null; + } + } + + Map<String, Object> result = getRowAsMap(columnNames, set); + return result; + } + + } +} Property changes on: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportQueryService.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Deleted: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportSqlService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportSqlService.java 2011-12-26 10:13:15 UTC (rev 215) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportSqlService.java 2011-12-26 19:07:07 UTC (rev 216) @@ -1,237 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 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% - */ -package fr.ifremer.echobase.services; - -import fr.ifremer.echobase.EchoBaseTechnicalException; -import fr.ifremer.echobase.entities.EchoBaseUser; -import fr.ifremer.echobase.entities.ExportQuery; -import fr.ifremer.echobase.services.models.ExportSqlCsvModel; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.topia.TopiaContext; -import org.nuiton.topia.TopiaException; -import org.nuiton.topia.framework.TopiaContextImplementor; -import org.nuiton.topia.framework.TopiaSQLQuery; -import org.nuiton.topia.persistence.TopiaDAO; -import org.nuiton.util.csv.Export; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.List; -import java.util.Map; - -/** - * Service to deal with sql export. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.1 - */ -public class ExportSqlService extends EchoBaseServiceSupport { - - /** Logger. */ - private static final Log log = LogFactory.getLog(ExportSqlService.class); - - public ExportQuery createOrUpdate(ExportQuery exportQuery, - EchoBaseUser user) { - try { - TopiaDAO<ExportQuery> dao = getDAO(ExportQuery.class); - ExportQuery entityToSave; - - // No id, creating new one entity - String id = exportQuery.getTopiaId(); - if (StringUtils.isEmpty(id)) { - entityToSave = dao.create(); - } else { - entityToSave = dao.findByTopiaId(id); - } - - entityToSave.setName(exportQuery.getName()); - entityToSave.setDescription(exportQuery.getDescription()); - entityToSave.setSqlQuery(exportQuery.getSqlQuery()); - entityToSave.setLastModifiedDate(newDate()); - entityToSave.setLastModifiedUser(user.getEmail()); - - dao.update(entityToSave); - getTransaction().commitTransaction(); - return entityToSave; - } catch (TopiaException eee) { - throw new EchoBaseTechnicalException(eee); - } - } - - public void delete(String topiaId) { - try { - TopiaDAO<ExportQuery> dao = getDAO(ExportQuery.class); - ExportQuery entityToDelete = dao.findByTopiaId(topiaId); - dao.delete(entityToDelete); - - getTransaction().commitTransaction(); - } catch (TopiaException eee) { - throw new EchoBaseTechnicalException(eee); - } - } - - public Map<String, Object>[] executeSql(String sql, Pager pager) { - - // get a query to count all rows for the request - GenericSQLQuery sqlQuery = new GenericSQLQuery(sql, pager); - try { - List<Map<String, Object>> result = - sqlQuery.getResult(getTransaction()); - return result.toArray(new Map[result.size()]); - } catch (TopiaException eee) { - throw new EchoBaseTechnicalException( - "Could not execute sql query", eee); - } - } - - public String[] getColumnNames(String sql) { - - String limitSql = sql.trim(); - if (limitSql.endsWith(";")) { - limitSql = limitSql.substring(0, limitSql.length() - 1); - limitSql += " LIMIT 1"; - } - try { - // do a limit query to one result and obtain the column names - // from the meta data of the result set - GenericSQLQuery sqlQuery = new GenericSQLQuery(limitSql, null); - String[] result = sqlQuery.getColumnNames(getTransaction()); - return result; - } catch (TopiaException eee) { - throw new EchoBaseTechnicalException( - "Could not execute query " + limitSql, eee); - } - } - - public String createCsvFileContent(String sql) { - - GenericSQLQuery sqlQuery = new GenericSQLQuery(sql, null); - List<Map<String, Object>> rows; - try { - rows = sqlQuery.getResult(getTransaction()); - } catch (TopiaException eee) { - throw new EchoBaseTechnicalException( - "Could not execute sql query", eee); - } - char csvSeparator = getConfiguration().getCsvSeparator(); - ExportSqlCsvModel csvModel = sqlQuery.generateCsvModel(csvSeparator); - Export<Map<String, Object>> exporter = Export.newExport(csvModel, rows); - try { - String content = exporter.startExportAsString(); - return content; - } catch (Exception eee) { - throw new EchoBaseTechnicalException("Could not export sql", eee); - } - } - - private static class GenericSQLQuery extends TopiaSQLQuery<Map<String, Object>> { - - protected String[] columnNames; - - private final String sql; - - private final Pager pager; - - public GenericSQLQuery(String sql, Pager pager) { - this.sql = sql; - this.pager = pager; - } - - public String[] getColumnNames() { - return columnNames; - } - - public ExportSqlCsvModel generateCsvModel(char charSeparator) { - ExportSqlCsvModel model = new ExportSqlCsvModel( - charSeparator, - columnNames - ); - return model; - } - - public List<Map<String, Object>> getResult(TopiaContext tx) throws TopiaException { - List<Map<String, Object>> rows = - findMultipleResult((TopiaContextImplementor) tx); - return rows; - } - - public String[] getColumnNames(TopiaContext tx) throws TopiaException { - findSingleResult((TopiaContextImplementor) tx); - return columnNames; - } - - @Override - protected PreparedStatement prepareQuery(Connection connection) throws SQLException { - return connection.prepareStatement( - sql, - ResultSet.TYPE_SCROLL_INSENSITIVE, - ResultSet.CONCUR_READ_ONLY - ); - } - - @Override - protected void afterExecuteQuery(ResultSet set) throws SQLException { - super.afterExecuteQuery(set); - - // obtain columnNames - columnNames = getColumnNames(set); - - if (pager != null) { - - // must count rows - int nbRows = getNbRows(set); - if (log.isInfoEnabled()) { - log.info("For request " + sql + ", nb rows = " + nbRows); - } - pager.setRecords(nbRows); - pager.computeIndexesAndPageCount(); - } - } - - @Override - protected Map<String, Object> prepareResult(ResultSet set) throws SQLException { - - if (pager != null) { - - // get row number (getRow() begins at 1) - int rowNumber = set.getRow() - 1; - if (rowNumber < pager.getStartIndex() || - rowNumber >= pager.getEndIndex()) { - - // out of pager bound, by returning null - // result will not be take in account - return null; - } - } - - Map<String, Object> result = getRowAsMap(columnNames, set); - return result; - } - - } -} Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataConfiguration.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataConfiguration.java 2011-12-26 10:13:15 UTC (rev 215) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataConfiguration.java 2011-12-26 19:07:07 UTC (rev 216) @@ -29,7 +29,6 @@ import java.io.File; import java.io.IOException; -import java.io.Serializable; import java.util.Locale; import static org.nuiton.i18n.I18n.l_; @@ -40,7 +39,7 @@ * @author tchemit <chemit@codelutin.com> * @since 0.2 */ -public class ImportDataConfiguration implements Serializable { +public class ImportDataConfiguration extends AbstractEchobaseActionConfiguration { private static final long serialVersionUID = 1L; @@ -61,12 +60,6 @@ protected InputFile typeEchoSpeciesFile; - protected int nbSteps; - - private float stepIncrement; - - protected float progression; - public ImportDataConfiguration(Locale locale) { acousticFile = InputFile.newFile( l_(locale, "echobase.common.acousticImport")); @@ -112,14 +105,6 @@ return typeEchoSpeciesFile; } - public int getNbSteps() { - return nbSteps; - } - - public float getProgression() { - return progression; - } - public void setImportDataMode(ImportDataMode importDataMode) { this.importDataMode = importDataMode; } @@ -132,26 +117,21 @@ this.mission = mission; } - public void setProgression(float progression) { - this.progression = progression; - } - - public void incrementsProgression() { - setProgression(progression + stepIncrement); - } - public void computeSteps() { - nbSteps = 0; + int nbSteps = 0; switch (importDataMode) { case ALL: nbSteps = 5; + break; case ACOUSTIC: nbSteps = 1; + break; default: } - stepIncrement = 100f / nbSteps; + setNbSteps(nbSteps); } + @Override public void destroy() throws IOException { if (workingDirectory != null) { FileUtils.deleteDirectory(workingDirectory); Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDbConfiguration.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDbConfiguration.java 2011-12-26 10:13:15 UTC (rev 215) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDbConfiguration.java 2011-12-26 19:07:07 UTC (rev 216) @@ -28,7 +28,6 @@ import java.io.File; import java.io.IOException; -import java.io.Serializable; import java.util.Locale; import static org.nuiton.i18n.I18n.l_; @@ -39,7 +38,7 @@ * @author tchemit <chemit@codelutin.com> * @since 0.2 */ -public class ImportDbConfiguration implements Serializable { +public class ImportDbConfiguration extends AbstractEchobaseActionConfiguration { private static final long serialVersionUID = 1L; @@ -47,12 +46,6 @@ protected InputFile input; - protected int nbSteps; - - private float stepIncrement; - - protected float progression; - public ImportDbConfiguration(Locale locale) { input = InputFile.newFile( l_(locale, "echobase.common.importDbFile")); @@ -66,32 +59,11 @@ return input; } - public int getNbSteps() { - return nbSteps; - } - - public float getProgression() { - return progression; - } - public void setWorkingDirectory(File workingDirectory) { this.workingDirectory = workingDirectory; } - public void setProgression(float progression) { - this.progression = progression; - } - - public void incrementsProgression() { - setProgression(progression + stepIncrement); - } - - public void setNbSteps(int nbSteps) { - - this.nbSteps = nbSteps; - stepIncrement = 100f / nbSteps; - } - + @Override public void destroy() throws IOException { if (workingDirectory != null) { FileUtils.deleteDirectory(workingDirectory); Copied: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/ExportQueryCsvModel.java (from rev 211, trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/ExportSqlCsvModel.java) =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/ExportQueryCsvModel.java (rev 0) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/ExportQueryCsvModel.java 2011-12-26 19:07:07 UTC (rev 216) @@ -0,0 +1,86 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 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% + */ +package fr.ifremer.echobase.services.models; + +import fr.ifremer.echobase.services.ExportQueryService; +import org.nuiton.util.csv.ExportableColumn; +import org.nuiton.util.csv.ImportExportModel; +import org.nuiton.util.csv.ImportableColumn; +import org.nuiton.util.csv.ModelBuilder; + +import java.util.Collection; +import java.util.List; +import java.util.Map; + +/** + * Csv model to export sql in {@link ExportQueryService}. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.2 + */ +public class ExportQueryCsvModel implements ImportExportModel<Map<String, Object>> { + + + protected final char separator; + + protected final ModelBuilder<?> modelBuilder; + + public ExportQueryCsvModel(char separator, String[] columnHeaders) { + this.separator = separator; + modelBuilder = new ModelBuilder<Object>(); + for (String columnHeader : columnHeaders) { + modelBuilder.newColumnForExport( + columnHeader, + columnHeader, + CsvModelUtil.TO_STRING_FORMATTER + ); + } + } + + @Override + public char getSeparator() { + return separator; + } + + @Override + public Collection<ExportableColumn<Map<String, Object>, Object>> getColumnsForExport() { + return (Collection) modelBuilder.getColumnsForExport(); + } + + @Override + public Collection<ImportableColumn<Map<String, Object>, Object>> getColumnsForImport() { + // never do import from this model + throw new UnsupportedOperationException(); + } + + @Override + public void pushCsvHeaderNames(List<String> headerNames) { + } + + @Override + public Map<String, Object> newEmptyInstance() { + // never do import from this model + throw new UnsupportedOperationException(); + } +} Property changes on: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/ExportQueryCsvModel.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Deleted: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/ExportSqlCsvModel.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/ExportSqlCsvModel.java 2011-12-26 10:13:15 UTC (rev 215) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/ExportSqlCsvModel.java 2011-12-26 19:07:07 UTC (rev 216) @@ -1,86 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 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% - */ -package fr.ifremer.echobase.services.models; - -import fr.ifremer.echobase.services.ExportSqlService; -import org.nuiton.util.csv.ExportableColumn; -import org.nuiton.util.csv.ImportExportModel; -import org.nuiton.util.csv.ImportableColumn; -import org.nuiton.util.csv.ModelBuilder; - -import java.util.Collection; -import java.util.List; -import java.util.Map; - -/** - * Csv model to export sql in {@link ExportSqlService}. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.2 - */ -public class ExportSqlCsvModel implements ImportExportModel<Map<String, Object>> { - - - protected final char separator; - - protected final ModelBuilder<?> modelBuilder; - - public ExportSqlCsvModel(char separator, String[] columnHeaders) { - this.separator = separator; - modelBuilder = new ModelBuilder<Object>(); - for (String columnHeader : columnHeaders) { - modelBuilder.newColumnForExport( - columnHeader, - columnHeader, - CsvModelUtil.TO_STRING_FORMATTER - ); - } - } - - @Override - public char getSeparator() { - return separator; - } - - @Override - public Collection<ExportableColumn<Map<String, Object>, Object>> getColumnsForExport() { - return (Collection) modelBuilder.getColumnsForExport(); - } - - @Override - public Collection<ImportableColumn<Map<String, Object>, Object>> getColumnsForImport() { - // never do import from this model - throw new UnsupportedOperationException(); - } - - @Override - public void pushCsvHeaderNames(List<String> headerNames) { - } - - @Override - public Map<String, Object> newEmptyInstance() { - // never do import from this model - throw new UnsupportedOperationException(); - } -}