r142 - in trunk/echobase-services/src: main/java/fr/ifremer/echobase/services main/java/fr/ifremer/echobase/services/models main/resources test/java/fr/ifremer/echobase test/java/fr/ifremer/echobase/services test/resources
Author: tchemit Date: 2011-12-05 09:24:55 +0100 (Mon, 05 Dec 2011) New Revision: 142 Url: http://forge.codelutin.com/repositories/revision/echobase/142 Log: -Introduce models package -remove dummy db configuration file -begin of implementation of import/export of global db -use same code for import/export of a entity and load/save it Added: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DbImportExportService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/CsvModelUtil.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/EntityAssociationCsvModel.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/EntityCsvModel.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/ExportSqlCsvModel.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/DbImportExportServiceTest.java Removed: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/CsvModelUtil.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DbEditorCsvModel.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportSqlCsvModel.java trunk/echobase-services/src/main/resources/echobase-db.properties trunk/echobase-services/src/test/resources/echobase.properties Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/CsvImportResult.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DbEditorService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DecoratorService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceContextImpl.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceSupport.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EmbeddedApplicationService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportSqlService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportHistoricalService.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/TestHelper.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/AbstractEchoBaseServiceTest.java Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/CsvImportResult.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/CsvImportResult.java 2011-12-05 08:22:27 UTC (rev 141) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/CsvImportResult.java 2011-12-05 08:24:55 UTC (rev 142) @@ -39,21 +39,35 @@ private static final long serialVersionUID = 1L; /** type of entity to import csv datas. */ - protected EchoBaseEntityEnum entityType; + protected final EchoBaseEntityEnum entityType; /** Name of the csv file to import. */ - protected String importFileName; + protected final String importFileName; + /** Flag to authorize to create entities not found in db. */ + protected final boolean createIfNotFound; + /** Count of created entities. */ - protected int numberCreated; /** Count of updated entities. */ protected int numberUpdated; - /** Flag to authorize to create entities not found in db. */ - protected boolean createIfNotFound; + public static CsvImportResult newResult(EchoBaseEntityEnum entityType, + String importFileName, + boolean createIfNotFound) { + CsvImportResult result = new CsvImportResult(entityType, importFileName, + createIfNotFound); + return result; + } + protected CsvImportResult(EchoBaseEntityEnum entityType, + String importFileName, + boolean createIfNotFound) { + this.entityType = entityType; + this.importFileName = importFileName; + this.createIfNotFound = createIfNotFound; + } public EchoBaseEntityEnum getEntityType() { return entityType; @@ -84,15 +98,4 @@ numberUpdated++; } - public void setEntityType(EchoBaseEntityEnum entityType) { - this.entityType = entityType; - } - - public void setImportFileName(String importFileName) { - this.importFileName = importFileName; - } - - public void setCreateIfNotFound(boolean createIfNotFound) { - this.createIfNotFound = createIfNotFound; - } } Deleted: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/CsvModelUtil.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/CsvModelUtil.java 2011-12-05 08:22:27 UTC (rev 141) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/CsvModelUtil.java 2011-12-05 08:24:55 UTC (rev 142) @@ -1,134 +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 com.google.common.base.Function; -import com.google.common.collect.Maps; -import fr.ifremer.echobase.EchoBaseTechnicalException; -import org.apache.commons.lang3.StringUtils; -import org.nuiton.topia.persistence.TopiaEntity; -import org.nuiton.util.csv.Common; -import org.nuiton.util.csv.ValueFormatter; -import org.nuiton.util.csv.ValueParserFormatter; - -import java.text.ParseException; -import java.util.Collection; -import java.util.Locale; -import java.util.Map; - -/** - * Usefull class to build csv import-export models. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.2 - */ -public class CsvModelUtil extends Common { - - public static <E extends TopiaEntity> ForeignKeyValue<E> newForeignKeyValue(Class<E> type) { - return new ForeignKeyValue<E>(type); - } - - public static <E extends TopiaEntity> ForeignKeyValue<E> newForeignKeyValue(Class<E> type, Collection<E> entitites) { - return new ForeignKeyValue<E>(type, entitites); - } - - public static <E extends TopiaEntity> ForeignKeyDecoratedValue<E> newForeignKeyDecoratedValue(Class<E> entityType, DecoratorService decoratorService, Locale locale) { - return new ForeignKeyDecoratedValue<E>(entityType, decoratorService, locale); - } - - public static class ForeignKeyValue<E extends TopiaEntity> implements ValueParserFormatter<E> { - - protected final Class<E> entityType; - - protected final Map<String, E> universe; - - public ForeignKeyValue(Class<E> entityType, Collection<E> entitites) { - this.entityType = entityType; - universe = Maps.uniqueIndex(entitites, new Function<E, String>() { - @Override - public String apply(E input) { - return input.getTopiaId(); - } - }); - } - - public ForeignKeyValue(Class<E> entityType) { - this.entityType = entityType; - universe = Maps.newHashMap(); - } - - @Override - public E parse(String value) throws ParseException { - E result = null; - if (StringUtils.isNotBlank(value)) { - - // get entity from universe - result = universe.get(value); - - if (result == null) { - - // can not find entity this is a big problem for us... - throw new EchoBaseTechnicalException( - "Could not find entity with name " + value); - } - } - return result; - } - - @Override - public String format(E e) { - String value = ""; - if (e != null) { - value = e.getTopiaId(); - } - return value; - } - } - - public static class ForeignKeyDecoratedValue<E extends TopiaEntity> implements ValueFormatter<E> { - - protected final Class<E> entityType; - - protected final DecoratorService decoratorService; - - protected final Locale locale; - - public ForeignKeyDecoratedValue(Class<E> entityType, DecoratorService decoratorService, Locale locale) { - this.entityType = entityType; - this.decoratorService = decoratorService; - this.locale = locale; - - } - - @Override - public String format(E e) { - String value = ""; - if (e != null) { - - value = decoratorService.decorate(locale, e, null); - } - return value; - } - } -} Deleted: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DbEditorCsvModel.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DbEditorCsvModel.java 2011-12-05 08:22:27 UTC (rev 141) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DbEditorCsvModel.java 2011-12-05 08:24:55 UTC (rev 142) @@ -1,159 +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.entities.meta.TableMeta; -import org.nuiton.topia.persistence.TopiaEntity; -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.Date; -import java.util.List; - -/** - * Import/Export csv model. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.2 - */ -public class DbEditorCsvModel<E extends TopiaEntity> implements ImportExportModel<E> { - - protected final char separator; - - protected final TableMeta tableMeta; - - protected ModelBuilder<E> modelBuilder; - - public DbEditorCsvModel(char separator, TableMeta tableMeta) { - this.separator = separator; - this.tableMeta = tableMeta; - modelBuilder = new ModelBuilder<E>(); - } - - @Override - public char getSeparator() { - return separator; - } - - @Override - public Collection<ExportableColumn<E, Object>> getColumnsForExport() { - return (Collection) - modelBuilder.getColumnsForExport(); - } - - @Override - public Collection<ImportableColumn<E, Object>> getColumnsForImport() { - return (Collection) - modelBuilder.getColumnsForImport(); - } - - @Override - public void pushCsvHeaderNames(List<String> headerNames) { - } - - @Override - public E newEmptyInstance() { - return (E) tableMeta.newEntity(); - } - - public void addForeignKeyForExport(String propertyName, - Class<TopiaEntity> entityType) { - - modelBuilder.newColumnForExport( - propertyName, - propertyName, - CsvModelUtil.newForeignKeyValue(entityType) - ); - } - - public void addForeignKeyForImport(String propertyName, - Class<TopiaEntity> entityType, - Collection<TopiaEntity> universe) { - - modelBuilder.newMandatoryColumn( - propertyName, - propertyName, - CsvModelUtil.newForeignKeyValue(entityType, universe) - ); - } - - public <E extends TopiaEntity> void addDefaultColumn(String propertyName, - Class<?> type) { - - if (Date.class.equals(type)) { - modelBuilder.newColumnForImportExport( - propertyName, - propertyName, - CsvModelUtil.DAY_TIME_SECOND - ); - } else if (float.class.equals(type)) { - modelBuilder.newColumnForImportExport( - propertyName, - propertyName, - CsvModelUtil.PRIMITIVE_FLOAT - ); - } else if (Float.class.equals(type)) { - modelBuilder.newColumnForImportExport( - propertyName, - propertyName, - CsvModelUtil.FLOAT - ); - } else if (int.class.equals(type)) { - modelBuilder.newColumnForImportExport( - propertyName, - propertyName, - CsvModelUtil.PRIMITIVE_INTEGER - ); - } else if (Integer.class.equals(type)) { - modelBuilder.newColumnForImportExport( - propertyName, - propertyName, - CsvModelUtil.INTEGER - ); - } else if (boolean.class.equals(type)) { - modelBuilder.newColumnForImportExport( - propertyName, - propertyName, - CsvModelUtil.PRIMITIVE_BOOLEAN - ); - } else if (Boolean.class.equals(type)) { - modelBuilder.newColumnForImportExport( - propertyName, - propertyName, - CsvModelUtil.BOOLEAN - ); - } else { - - // string - modelBuilder.newColumnForImportExport( - propertyName, - propertyName - ); - } - } - -} Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DbEditorService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DbEditorService.java 2011-12-05 08:22:27 UTC (rev 141) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DbEditorService.java 2011-12-05 08:24:55 UTC (rev 142) @@ -32,7 +32,8 @@ import fr.ifremer.echobase.entities.meta.ColumnMeta; import fr.ifremer.echobase.entities.meta.DbMeta; import fr.ifremer.echobase.entities.meta.TableMeta; -import org.apache.commons.io.IOUtils; +import fr.ifremer.echobase.services.models.CsvModelUtil; +import fr.ifremer.echobase.services.models.EntityCsvModel; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -42,17 +43,13 @@ import org.nuiton.topia.persistence.TopiaEntity; import org.nuiton.util.beans.BeanMonitor; import org.nuiton.util.beans.PropertyDiff; -import org.nuiton.util.csv.Export; -import org.nuiton.util.csv.ExportableColumn; -import org.nuiton.util.csv.Import; -import org.nuiton.util.csv.ImportableColumn; -import org.nuiton.util.csv.ModelBuilder; +import org.nuiton.util.csv.*; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; +import java.io.IOException; import java.util.Collection; -import java.util.Iterator; import java.util.List; import java.util.Map; @@ -88,7 +85,7 @@ } } - public TableMeta getTableMetas(EchoBaseEntityEnum tableName) { + public TableMeta getTableMeta(EchoBaseEntityEnum tableName) { DbMeta dbMeta = getDbMeta(); TableMeta result = dbMeta.getTable(tableName); return result; @@ -111,9 +108,9 @@ List<E> result; EchoBaseEntityEnum entityEnum = tableMeta.getEntityEnum(); - Class<? extends TopiaEntity> contract = entityEnum.getContract(); + Class<E> contract = (Class<E>) entityEnum.getContract(); try { - TopiaDAO dao = getDAO(contract); + TopiaDAO<E> dao = getDAO(contract); // first query to count datas TopiaQuery countQuery = dao.createQuery("e"); @@ -163,18 +160,17 @@ Boolean ascendantOrder, boolean addDecorated) { - TableMeta tableMeta = getTableMetas(entityType); + TableMeta meta = getTableMeta(entityType); - List<TopiaEntity> entities = getEntities(tableMeta, - pager, - sidx, - ascendantOrder + List<TopiaEntity> entities = getEntities(meta, + pager, + sidx, + ascendantOrder ); Map<?, ?>[] rows = new Map[entities.size()]; - DbEditorCsvModel<TopiaEntity> model = - buildForLoad(tableMeta, addDecorated); + ImportExportModel<TopiaEntity> model = buildForLoad(meta, addDecorated); int i = 0; for (Object o : entities) { @@ -191,23 +187,21 @@ try { TopiaDAO<?> dao = getDAO(entityEnum.getContract()); TopiaEntity entity = dao.findByTopiaId(topiaId); - DbEditorCsvModel<TopiaEntity> model = buildForLoad(tableMeta, true); + ImportExportModel<TopiaEntity> model = buildForLoad(tableMeta, true); return loadRow(entity, model); } catch (TopiaException eee) { throw new EchoBaseTechnicalException("Could not obtain data", eee); } } - public void saveEntity(TableMeta tableMeta, - String id, + public void saveEntity(TableMeta meta, Map<String, String> properties, EchoBaseUser user) { // transform properties entity - DbEditorCsvModel<TopiaEntity> model = buildForSave(tableMeta); + ImportExportModel<TopiaEntity> model = buildForSave(meta); TopiaEntity entity = model.newEmptyInstance(); - entity.setTopiaId(id); for (ImportableColumn<TopiaEntity, Object> column : model.getColumnsForImport()) { String propertyName = column.getHeaderName(); @@ -221,12 +215,12 @@ try { column.setValue(entity, o); } catch (Exception e) { - throw new EchoBaseTechnicalException("Could not set property [" + propertyName + "] with value " + stringValue + " to entity " + entity); + throw new EchoBaseTechnicalException( + "Could not set property [" + propertyName + "] with value " + stringValue + " to entity " + entity); } } - saveEntity( - tableMeta, + saveEntity(meta, "Modification utilisateur", entity, user, @@ -239,31 +233,28 @@ String importFileName, File importFile, boolean createIfNotFound, - EchoBaseUser user) { - TableMeta tableMeta = getTableMetas(entityType); - DbEditorCsvModel<TopiaEntity> csvModel = buildForImport(tableMeta); + EchoBaseUser user) throws IOException { + + TableMeta meta = getTableMeta(entityType); + ImportExportModel<TopiaEntity> csvModel = buildForImport(meta); String messagePrefix = "Import du fichier " + importFileName + " le " + newDate(); - CsvImportResult result = new CsvImportResult(); - result.setImportFileName(importFileName); - result.setEntityType(entityType); - BufferedReader bf = null; + CsvImportResult result = CsvImportResult.newResult(entityType, + importFileName, + createIfNotFound); + BufferedReader bf = new BufferedReader(new FileReader(importFile)); try { - bf = new BufferedReader(new FileReader(importFile)); Import<TopiaEntity> importer = Import.newImport(csvModel, bf); - Iterator<TopiaEntity> iterator = importer.startImport(); - try { - while (iterator.hasNext()) { - TopiaEntity entity = iterator.next(); - boolean create = saveEntity(tableMeta, - messagePrefix, - entity, - user, - createIfNotFound + for (TopiaEntity entity : importer) { + boolean create = saveEntity(meta, + messagePrefix, + entity, + user, + createIfNotFound ); if (create) { @@ -276,7 +267,7 @@ } } finally { - importer.stopImport(); + importer.close(); } commitTransaction("Could not commit transaction"); @@ -286,19 +277,18 @@ log.error("Failed to read import file " + importFile.getName(), eee); throw new EchoBaseTechnicalException(eee); } finally { - IOUtils.closeQuietly(bf); + bf.close(); } - return result; } public String exportDatas(EchoBaseEntityEnum entityType) { - TableMeta tableMeta = getTableMetas(entityType); + TableMeta tableMeta = getTableMeta(entityType); List<TopiaEntity> datas = getEntities(tableMeta, null, null, null); - DbEditorCsvModel<TopiaEntity> model = buildForExport(tableMeta); + ImportExportModel<TopiaEntity> model = buildForExport(tableMeta); Export<TopiaEntity> export = Export.newExport(model, datas); @@ -314,7 +304,6 @@ TopiaEntity entity, EchoBaseUser user, boolean createIfNotFound) { - String[] columnNames = tableMeta.getColumnNamesAsArray(); BeanMonitor monitor = null; @@ -340,7 +329,8 @@ if (!createIfNotFound) { // this is an error - throw new EchoBaseTechnicalException("Could not found entity with id " + id); + throw new EchoBaseTechnicalException( + "Could not found entity with id " + id); } else { entityToSave = dao.create(TopiaEntity.TOPIA_ID, id); create = true; @@ -358,7 +348,7 @@ tableMeta.copy(entity, entityToSave); PropertyDiff[] propertyDiffs = monitor == null ? null : - monitor.getPropertyDiffs(); + monitor.getPropertyDiffs(); createEntityModificationLog( tableMeta, @@ -392,7 +382,7 @@ buffer = new StringBuilder(messagePrefix + "\n"); if (create) { - buffer.append("Une entité à été crée d'identifiant ").append(topiaId); + buffer.append("Une entité a été crée d'identifiant ").append(topiaId); } else { int length = propertyDiffs.length; @@ -423,9 +413,9 @@ } } buffer.append(String.format("\n Propriété '%s', ancienne valeur : %s, nouvelle valeur : %s", - propertyName, - sourceValue, - targetValue + propertyName, + sourceValue, + targetValue )); } } @@ -436,7 +426,7 @@ dao.create( EntityModificationLog.PROPERTY_ENTITY_TYPE, tableMeta.getEntityEnum().name(), EntityModificationLog.PROPERTY_ENTITY_ID, topiaId, - EntityModificationLog.PROPERTY_MODIFICATION_USER, user, + EntityModificationLog.PROPERTY_MODIFICATION_USER, user.getEmail(), EntityModificationLog.PROPERTY_MODIFICATION_DATE, newDate(), EntityModificationLog.PROPERTY_MODIFICATION_TEXT, buffer.toString() ); @@ -444,7 +434,7 @@ protected <E extends TopiaEntity> Map<String, Object> loadRow( E entity, - DbEditorCsvModel<E> loadModel) { + ImportExportModel<E> loadModel) { Map<String, Object> row = Maps.newLinkedHashMap(); @@ -459,7 +449,7 @@ } catch (Exception eee) { throw new EchoBaseTechnicalException( "Could not obtain property [" + propertyName + - "] value ", eee); + "] value ", eee); } String formatedValue = column.formatValue(value); row.put(propertyName, formatedValue); @@ -468,19 +458,15 @@ return row; } - protected <E extends TopiaEntity> DbEditorCsvModel<E> buildForImport(TableMeta tableMetas) { + public <E extends TopiaEntity> ImportExportModel<E> buildForImport(TableMeta meta) { - DbEditorCsvModel<E> model = new DbEditorCsvModel<E>(getConfiguration().getCsvSeparator(), - tableMetas); - - ModelBuilder<E> modelBuilder = model.modelBuilder; - - modelBuilder.newColumnForImportExport( - TopiaEntity.TOPIA_ID, + EntityCsvModel<E> model = EntityCsvModel.newModel( + getConfiguration().getCsvSeparator(), + meta, TopiaEntity.TOPIA_ID ); - for (ColumnMeta columnMeta : model.tableMeta) { + for (ColumnMeta columnMeta : meta) { String propertyName = columnMeta.getName(); Class<?> type = columnMeta.getType(); if (columnMeta.isFK()) { @@ -495,30 +481,22 @@ return model; } - protected <E extends TopiaEntity> DbEditorCsvModel<E> buildForSave(TableMeta tableMetas) { + public <E extends TopiaEntity> ImportExportModel<E> buildForExport(TableMeta meta) { - DbEditorCsvModel<E> model = new DbEditorCsvModel<E>(getConfiguration().getCsvSeparator(), - tableMetas); - - ModelBuilder<E> modelBuilder = model.modelBuilder; - - modelBuilder.newColumnForExport( - TopiaEntity.TOPIA_ID, + EntityCsvModel<E> model = EntityCsvModel.newModel( + getConfiguration().getCsvSeparator(), + meta, TopiaEntity.TOPIA_ID ); - for (ColumnMeta columnMeta : model.tableMeta) { + + for (ColumnMeta columnMeta : meta) { String propertyName = columnMeta.getName(); Class<?> type = columnMeta.getType(); if (columnMeta.isFK()) { Class<TopiaEntity> entityType = (Class<TopiaEntity>) type; - - Collection<TopiaEntity> universe = getForeignDatas(entityType); - - // translate foreign key to his id - model.addForeignKeyForImport(propertyName, entityType, universe); - + model.addForeignKeyForExport(propertyName, entityType); } else { model.addDefaultColumn(propertyName, type); } @@ -526,25 +504,25 @@ return model; } - protected <E extends TopiaEntity> DbEditorCsvModel<E> buildForExport(TableMeta tableMetas) { + protected <E extends TopiaEntity> ImportExportModel<E> buildForSave(TableMeta meta) { - DbEditorCsvModel<E> model = new DbEditorCsvModel<E>(getConfiguration().getCsvSeparator(), - tableMetas); - - ModelBuilder<E> modelBuilder = model.modelBuilder; - - modelBuilder.newColumnForImportExport( - TopiaEntity.TOPIA_ID, + EntityCsvModel<E> model = EntityCsvModel.newModel( + getConfiguration().getCsvSeparator(), + meta, TopiaEntity.TOPIA_ID ); - for (ColumnMeta columnMeta : model.tableMeta) { + for (ColumnMeta columnMeta : meta) { String propertyName = columnMeta.getName(); Class<?> type = columnMeta.getType(); if (columnMeta.isFK()) { Class<TopiaEntity> entityType = (Class<TopiaEntity>) type; - model.addForeignKeyForExport(propertyName, entityType); + Collection<TopiaEntity> universe = getForeignDatas(entityType); + + // translate foreign key to his id + model.addForeignKeyForImport(propertyName, entityType, universe); + } else { model.addDefaultColumn(propertyName, type); } @@ -552,20 +530,16 @@ return model; } - protected <E extends TopiaEntity> DbEditorCsvModel<E> buildForLoad(TableMeta tableMetas, - boolean addDecorated) { + protected <E extends TopiaEntity> ImportExportModel<E> buildForLoad(TableMeta meta, + boolean addDecorated) { - DbEditorCsvModel<E> model = new DbEditorCsvModel<E>(getConfiguration().getCsvSeparator(), - tableMetas); - - ModelBuilder<E> modelBuilder = model.modelBuilder; - - modelBuilder.newColumnForImportExport( - "id", - TopiaEntity.TOPIA_ID + EntityCsvModel<E> model = EntityCsvModel.newModel( + getConfiguration().getCsvSeparator(), + meta, + "id" ); - for (ColumnMeta columnMeta : model.tableMeta) { + for (ColumnMeta columnMeta : meta) { String propertyName = columnMeta.getName(); Class<?> type = columnMeta.getType(); if (columnMeta.isFK()) { @@ -578,7 +552,7 @@ if (addDecorated) { // translate foreign key to his decorated value - modelBuilder.newColumnForExport( + model.addNewColumnForExport( propertyName + "_lib", propertyName, CsvModelUtil.newForeignKeyDecoratedValue( @@ -594,5 +568,4 @@ } return model; } - } Added: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DbImportExportService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DbImportExportService.java (rev 0) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DbImportExportService.java 2011-12-05 08:24:55 UTC (rev 142) @@ -0,0 +1,441 @@ +/* + * #%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 com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import fr.ifremer.echobase.EchoBaseTechnicalException; +import fr.ifremer.echobase.entities.EchoBaseDAOHelper; +import fr.ifremer.echobase.entities.EchoBaseEntityEnum; +import fr.ifremer.echobase.entities.EchoBaseUser; +import fr.ifremer.echobase.entities.EntitiesUtil; +import fr.ifremer.echobase.entities.meta.AssociationMeta; +import fr.ifremer.echobase.entities.meta.TableMeta; +import fr.ifremer.echobase.services.models.EntityAssociationCsvModel; +import org.apache.commons.io.FileUtils; +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.TopiaQuery; +import org.nuiton.topia.persistence.TopiaDAO; +import org.nuiton.topia.persistence.TopiaEntity; +import org.nuiton.util.FileUtil; +import org.nuiton.util.csv.Export; +import org.nuiton.util.csv.Import; +import org.nuiton.util.csv.ImportExportModel; + +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +/** + * Service to import / export a complete db. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.2 + */ +public class DbImportExportService extends EchoBaseServiceSupport { + + /** Logger. */ + private static final Log log = + LogFactory.getLog(DbImportExportService.class); + + protected DbEditorService dbEditorService; + + /** + * Import a complete db from the given zip file. + * + * @param file zip file where all csv files are located + * @param user user which perform the import + * @throws IOException if any io exception while import + */ + public void importDb(File file, EchoBaseUser user) throws IOException { + + ZipFile zipFile = new ZipFile(file); + try { + FileEntry[] entries = getEntries(); + + List<String> missingEntries = Lists.newArrayList(); + + Map<FileEntry, ZipEntry> entriestoConsume = Maps.newLinkedHashMap(); + + // check that all mandatories + for (FileEntry entry : entries) { + String filename = entry.getFilename(); + ZipEntry zipEntry = zipFile.getEntry(filename); + + if (zipEntry == null) { + missingEntries.add(filename); + } else { + entriestoConsume.put(entry, zipEntry); + } + } + + if (!missingEntries.isEmpty()) { + + // there is some missing file entries + throw new EchoBaseTechnicalException( + "There is some missing mandatory files " + + missingEntries + " in import " + file); + } + + // consume all found entries + for (Map.Entry<FileEntry, ZipEntry> entry : + entriestoConsume.entrySet()) { + + FileEntry entryDef = entry.getKey(); + ZipEntry value = entry.getValue(); + CsvImportResult csvResult = CsvImportResult.newResult( + entryDef.getSource(), + value.getName(), + false + ); + Reader reader = + new InputStreamReader(zipFile.getInputStream(value)); + try { + importFile(entryDef, reader, csvResult); + } finally { + reader.close(); + } + } + + // add a log entry of import db + if (log.isInfoEnabled()) { + log.info("Import done with user " + user.getEmail()); + } + commitTransaction("Could not commit db import " + file); + } finally { + zipFile.close(); + } + } + + /** + * Export the complete db into csv files stored in a zip archive file. + * + * @param fileName file name of the zip to create + * @return the zip archive file + */ + public File exportDb(String fileName) { + File tempDirectory = new File(FileUtils.getTempDirectory(), + "echobase-exportDb_" + System.nanoTime()); + tempDirectory.deleteOnExit(); + + File zipFile = new File(tempDirectory, fileName + ".zip"); + + FileEntry[] entries = getEntries(); + for (FileEntry entry : entries) { + + EchoBaseEntityEnum source = entry.getSource(); + + File entryFile = new File(tempDirectory, entry.getFilename()); + + List<TopiaEntity> entities = entry.getDatas(getTransaction()); + if (log.isInfoEnabled()) { + log.info("Export " + entities + " entities of type " + + source + " to " + entryFile); + } + + exportFile(entry, entryFile, entities); + } + + compressZipFile(zipFile, tempDirectory); + + return zipFile; + } + + protected void importFile(FileEntry entry, + Reader reader, + CsvImportResult csvResult) throws IOException { + + EchoBaseEntityEnum source = entry.getSource(); + + DbEditorService service = getDbEditorService(); + + TableMeta tableMeta = getTableMeta(source); + + if (entry.isAssociation()) { + + // load a association input + + AssociationMeta associationMeta = + tableMeta.getAssociations(entry.getPropertyName()); + + ImportExportModel<TopiaEntity> model = + EntityAssociationCsvModel.newModel( + getConfiguration().getCsvSeparator(), + associationMeta + ); + + importEntityAssociation(associationMeta, model, csvResult, reader); + + } else { + + // normal entity table import + + ImportExportModel<TopiaEntity> model = service.buildForImport(tableMeta); + + importEntities(tableMeta, model, csvResult, reader); + } + } + + protected void exportFile(FileEntry entry, + File entryFile, + List<TopiaEntity> entities) { + ImportExportModel<TopiaEntity> model; + + TableMeta meta = getTableMeta(entry.getSource()); + + if (entry.isAssociation()) { + AssociationMeta associationMeta = + meta.getAssociations(entry.getPropertyName()); + + model = EntityAssociationCsvModel.newModel( + getConfiguration().getCsvSeparator(), + associationMeta + ); + + } else { + + model = getDbEditorService().buildForExport(meta); + } + + Export<TopiaEntity> export = Export.newExport(model, entities); + + try { + String entryFileContent = export.startExportAsString(); + FileUtil.writeString(entryFile, entryFileContent, "utf-8"); + } catch (Exception eee) { + throw new EchoBaseTechnicalException("Can not export datas", eee); + } + } + + + protected TableMeta getTableMeta(EchoBaseEntityEnum tableName) { + return getDbEditorService().getTableMeta(tableName); + } + + public FileEntry[] getEntries() { + + List<FileEntry> result = Lists.newArrayList(); + + addEntries(result, EntitiesUtil.getReferenceTypesForCopy()); + addEntries(result, EntitiesUtil.getDataTypesforCopy()); + return result.toArray(new FileEntry[result.size()]); + } + + protected void addEntries(List<FileEntry> result, EchoBaseEntityEnum[] types) { + for (EchoBaseEntityEnum type : types) { + + TableMeta tableMeta = getTableMeta(type); + + result.add(FileEntry.newEntry(tableMeta)); + + for (AssociationMeta associationMeta : tableMeta.getAssociations()) { + result.add(FileEntry.newEntry(associationMeta)); + } + + } + } + + protected DbEditorService getDbEditorService() { + if (dbEditorService == null) { + dbEditorService = newService(DbEditorService.class); + } + return dbEditorService; + } + + public void importEntities(TableMeta meta, + ImportExportModel<TopiaEntity> csvModel, + CsvImportResult result, + Reader reader) { + + Import<TopiaEntity> importer = Import.newImport(csvModel, reader); + + + try { + TopiaDAO<TopiaEntity> dao = getDAO(meta.getEntityEnum()); + + for (TopiaEntity entity : importer) { + + + TopiaEntity entityToSave = dao.create(TopiaEntity.TOPIA_ID, + entity.getTopiaId()); + + meta.copy(entity, entityToSave); + + result.incrementsNumberUpdated(); + } + } catch (Exception eee) { + throw new EchoBaseTechnicalException("Could not import entities of type " + meta.getEntityEnum(), eee); + } finally { + + importer.close(); + } + } + + protected <E extends TopiaEntity> void importEntityAssociation(AssociationMeta meta, + ImportExportModel<E> csvModel, + CsvImportResult result, + Reader reader) { + + try { + + EchoBaseEntityEnum sourceType = meta.getSource(); + EchoBaseEntityEnum targetType = meta.getTarget(); + + Import<E> importer = Import.newImport(csvModel, reader); + TopiaDAO<TopiaEntity> sourceDAO = getDAO(sourceType); + TopiaDAO<TopiaEntity> targetDAO = getDAO(targetType); + + try { + for (E row : importer) { + + TopiaEntity entityToSave = + sourceDAO.findByTopiaId(row.getTopiaId()); + + // check entity exits ? + if (entityToSave == null) { + throw new EchoBaseTechnicalException( + "Could not find entity with id " + row.getTopiaId()); + } + Collection<TopiaEntity> assoc = meta.getChilds(row); + Collection<TopiaEntity> associationToSave = Lists.newArrayList(); + for (TopiaEntity topiaEntity : assoc) { + TopiaEntity byTopiaId = targetDAO.findByTopiaId(topiaEntity.getTopiaId()); + if (byTopiaId == null) { + throw new EchoBaseTechnicalException( + "Could not find entity with id " + topiaEntity.getTopiaId()); + } + associationToSave.add(byTopiaId); + result.incrementsNumberUpdated(); + } + meta.setChilds(entityToSave, associationToSave); + } + } finally { + + importer.close(); + } + + commitTransaction("Could not commit transaction"); + } catch (TopiaException eee) { + throw new EchoBaseTechnicalException(eee); + } + } + + public static class FileEntry { + + public static FileEntry newEntry(TableMeta tableMeta) { + return newEntry(tableMeta.getEntityEnum(), null, null); + } + + public static FileEntry newEntry(AssociationMeta tableMeta) { + return newEntry(tableMeta.getSource(), tableMeta.getTarget(), tableMeta.getName()); + } + + public static FileEntry newEntry(EchoBaseEntityEnum source, + EchoBaseEntityEnum target, + String propertyName) { + return new FileEntry(source, target, propertyName); + } + + final EchoBaseEntityEnum source; + + final EchoBaseEntityEnum target; + + final String propertyName; + + protected CsvImportResult importResult; + + FileEntry(EchoBaseEntityEnum source, + EchoBaseEntityEnum target, + String propertyName) { + + this.source = source; + this.target = target; + this.propertyName = propertyName; + } + + public EchoBaseEntityEnum getSource() { + return source; + } + + public EchoBaseEntityEnum getTarget() { + return target; + } + + public String getPropertyName() { + return propertyName; + } + + public boolean isAssociation() { + return target != null && propertyName != null; + } + + public String getFilename() { + String result; + if (isAssociation()) { + result = source.name() + "_" + propertyName + "_" + target.name(); + } else { + result = source.name(); + } + return result + ".csv"; + } + + public CsvImportResult getImportResult() { + return importResult; + } + + public void setImportResult(CsvImportResult importResult) { + this.importResult = importResult; + } + + public List<TopiaEntity> getDatas(TopiaContext transaction) { + + try { + TopiaDAO<TopiaEntity> dao = EchoBaseDAOHelper.<TopiaEntity, TopiaDAO<TopiaEntity>>getDAO(transaction, (Class<TopiaEntity>) source.getContract()); + + List<TopiaEntity> result; + if (isAssociation()) { + + // filter only entities with some data in the association + TopiaQuery query = dao.createQuery("e"); + query.addWhere("size(e." + propertyName + ") > 0"); + result = dao.findAllByQuery(query); + } else { + result = dao.findAll(); + } + return result; + } catch (TopiaException eee) { + throw new EchoBaseTechnicalException("Could not obtain datas for " + this, eee); + } + } + } +} Property changes on: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DbImportExportService.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/DecoratorService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DecoratorService.java 2011-12-05 08:22:27 UTC (rev 141) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DecoratorService.java 2011-12-05 08:24:55 UTC (rev 142) @@ -64,7 +64,7 @@ Preconditions.checkNotNull( decorator, "Could not find decorator for type " + type + - " and context " + context + " and context " + context ); return decorator; } @@ -107,7 +107,7 @@ Preconditions.checkNotNull( decorator, "Could not find decorator for type " + object.getClass() + - " and context " + context + " and context " + context ); DecoratorUtil.sort((JXPathDecorator<O>) decorator, list, 0); } Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceContextImpl.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceContextImpl.java 2011-12-05 08:22:27 UTC (rev 141) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceContextImpl.java 2011-12-05 08:24:55 UTC (rev 142) @@ -52,10 +52,10 @@ EchoBaseServiceContext serviceContext, TopiaContext transaction) { return newContext(serviceContext.getLocale(), - transaction, - serviceContext.getConfiguration(), - serviceContext.getDbMeta(), - serviceContext.getServiceFactory() + transaction, + serviceContext.getConfiguration(), + serviceContext.getDbMeta(), + serviceContext.getServiceFactory() ); } @@ -66,10 +66,10 @@ DbMeta dbMeta, EchoBaseServiceFactory serviceFactory) { return new EchoBaseServiceContextImpl(locale, - transaction, - configuration, - dbMeta, - serviceFactory); + transaction, + configuration, + dbMeta, + serviceFactory); } protected EchoBaseServiceContextImpl(Locale locale, Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceSupport.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceSupport.java 2011-12-05 08:22:27 UTC (rev 141) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceSupport.java 2011-12-05 08:24:55 UTC (rev 142) @@ -35,7 +35,11 @@ import org.nuiton.topia.TopiaException; import org.nuiton.topia.persistence.TopiaDAO; import org.nuiton.topia.persistence.TopiaEntity; +import org.nuiton.util.FileUtil; +import org.nuiton.util.ZipUtil; +import java.io.File; +import java.io.IOException; import java.util.Date; import java.util.List; import java.util.Locale; @@ -73,7 +77,7 @@ } catch (TopiaException eee) { throw new EchoBaseTechnicalException( "Could not obtain data of id [" + id + "] for type " + - entityType, eee); + entityType, eee); } } @@ -125,4 +129,15 @@ return result; } + protected void compressZipFile(File zipFile, File directory) { + try { + ZipUtil.compress(zipFile, directory); + } catch (IOException eee) { + throw new EchoBaseTechnicalException( + "Can not create zip file " + zipFile, eee); + } finally { + FileUtil.deleteRecursively(directory); + } + } + } 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-05 08:22:27 UTC (rev 141) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EmbeddedApplicationService.java 2011-12-05 08:24:55 UTC (rev 142) @@ -39,13 +39,8 @@ import org.nuiton.topia.replication.TopiaReplicationService; import org.nuiton.topia.replication.model.ReplicationModel; import org.nuiton.util.FileUtil; -import org.nuiton.util.ZipUtil; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.*; import java.net.URISyntaxException; import java.util.List; @@ -88,7 +83,7 @@ File warLocation = getConfiguration().getWarLocation(); File tempDirectory = new File(FileUtils.getTempDirectory(), - "echobase-embedded_" + System.nanoTime()); + "echobase-embedded_" + System.nanoTime()); tempDirectory.deleteOnExit(); File zipDirectory = new File(tempDirectory, fileName); @@ -97,7 +92,7 @@ if (log.isInfoEnabled()) { log.info("Creates zip file [" + zipFile + "] from directory " + - zipDirectory); + zipDirectory); } try { // create / @@ -118,19 +113,11 @@ } catch (Exception eee) { throw new EchoBaseTechnicalException( "Could not create zip structure at location " + - zipDirectory, eee); + zipDirectory, eee); } + compressZipFile(zipFile, zipDirectory); - try { - ZipUtil.compress(zipFile, zipDirectory); - } catch (IOException eee) { - throw new EchoBaseTechnicalException( - "Can not create zip file " + zipFile, eee); - } finally { - FileUtil.deleteRecursively(zipDirectory); - } - return zipFile; } @@ -193,7 +180,7 @@ // create new service context (with new transaction) EchoBaseServiceContext newServiceContext = EchoBaseServiceContextImpl.newContext(serviceContext, - topiaContext); + topiaContext); // get user service from h2 db UserService userService = Deleted: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportSqlCsvModel.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportSqlCsvModel.java 2011-12-05 08:22:27 UTC (rev 141) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportSqlCsvModel.java 2011-12-05 08:24:55 UTC (rev 142) @@ -1,85 +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 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(); - } -} Modified: 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-05 08:22:27 UTC (rev 141) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportSqlService.java 2011-12-05 08:24:55 UTC (rev 142) @@ -26,6 +26,7 @@ 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; @@ -72,7 +73,7 @@ entityToSave.setDescription(exportQuery.getDescription()); entityToSave.setSqlQuery(exportQuery.getSqlQuery()); entityToSave.setLastModifiedDate(newDate()); - entityToSave.setLastModifiedUser(user); + entityToSave.setLastModifiedUser(user.getEmail()); dao.update(entityToSave); getTransaction().commitTransaction(); @@ -103,7 +104,8 @@ sqlQuery.getResult(getTransaction()); return result.toArray(new Map[result.size()]); } catch (TopiaException eee) { - throw new EchoBaseTechnicalException("Could not execute sql query", eee); + throw new EchoBaseTechnicalException( + "Could not execute sql query", eee); } } @@ -121,7 +123,8 @@ String[] result = sqlQuery.getColumnNames(getTransaction()); return result; } catch (TopiaException eee) { - throw new EchoBaseTechnicalException("Could not execute query " + limitSql, eee); + throw new EchoBaseTechnicalException( + "Could not execute query " + limitSql, eee); } } @@ -132,7 +135,8 @@ try { rows = sqlQuery.getResult(getTransaction()); } catch (TopiaException eee) { - throw new EchoBaseTechnicalException("Could not execute sql query", eee); + throw new EchoBaseTechnicalException( + "Could not execute sql query", eee); } char csvSeparator = getConfiguration().getCsvSeparator(); ExportSqlCsvModel csvModel = sqlQuery.generateCsvModel(csvSeparator); @@ -217,7 +221,7 @@ // get row number (getRow() begins at 1) int rowNumber = set.getRow() - 1; if (rowNumber < pager.getStartIndex() || - rowNumber >= pager.getEndIndex()) { + rowNumber >= pager.getEndIndex()) { // out of pager bound, by returning null // result will not be take in account Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataService.java 2011-12-05 08:22:27 UTC (rev 141) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportDataService.java 2011-12-05 08:24:55 UTC (rev 142) @@ -49,7 +49,7 @@ try { Mission result = getDAO(Mission.class).create(mission); commitTransaction("Could not create mission " + - mission.getMissionName()); + mission.getMissionName()); return result; } catch (TopiaException eee) { throw new EchoBaseTechnicalException(eee); @@ -72,7 +72,7 @@ default: throw new IllegalStateException( "Can not know what to do with importDataMode " + - importDataMode); + importDataMode); } @@ -90,7 +90,7 @@ if (log.isInfoEnabled()) { log.info("Starts data import with mission " + - mission.getMissionName()); + mission.getMissionName()); } InputFile acousticFile = configuration.getAcousticFile(); @@ -118,7 +118,7 @@ if (log.isInfoEnabled()) { log.info("Starts data import with mission " + - mission.getMissionName()); + mission.getMissionName()); } InputFile acousticFile = configuration.getAcousticFile(); @@ -208,9 +208,9 @@ InputFile importFile) { if (log.isInfoEnabled()) { log.info("Starts " + importFile.getLabel() + " with file " + - importFile.getFile()); + importFile.getFile()); } - CsvImportResult result = new CsvImportResult(); + CsvImportResult result = CsvImportResult.newResult(null, importFile.getFileName(), false); prepareImport(); return result; } @@ -219,9 +219,9 @@ InputFile importFile) { if (log.isInfoEnabled()) { log.info("Starts " + importFile.getLabel() + " with file " + - importFile.getFile()); + importFile.getFile()); } - CsvImportResult result = new CsvImportResult(); + CsvImportResult result = CsvImportResult.newResult(null, importFile.getFileName(), false); prepareImport(); return result; } @@ -230,9 +230,9 @@ InputFile importFile) { if (log.isInfoEnabled()) { log.info("Starts " + importFile.getLabel() + " with file " + - importFile.getFile()); + importFile.getFile()); } - CsvImportResult result = new CsvImportResult(); + CsvImportResult result = CsvImportResult.newResult(null, importFile.getFileName(), false); prepareImport(); return result; } @@ -241,9 +241,9 @@ InputFile importFile) { if (log.isInfoEnabled()) { log.info("Starts " + importFile.getLabel() + " with file " + - importFile.getFile()); + importFile.getFile()); } - CsvImportResult result = new CsvImportResult(); + CsvImportResult result = CsvImportResult.newResult(null, importFile.getFileName(), false); prepareImport(); return result; } @@ -252,9 +252,9 @@ InputFile importFile) { if (log.isInfoEnabled()) { log.info("Starts " + importFile.getLabel() + " with file " + - importFile.getFile()); + importFile.getFile()); } - CsvImportResult result = new CsvImportResult(); + CsvImportResult result = CsvImportResult.newResult(null, importFile.getFileName(), false); prepareImport(); return result; } Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportHistoricalService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportHistoricalService.java 2011-12-05 08:22:27 UTC (rev 141) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ImportHistoricalService.java 2011-12-05 08:24:55 UTC (rev 142) @@ -50,7 +50,7 @@ if (log.isInfoEnabled()) { log.info("Starts historical import with file " + - input.getFileName()); + input.getFileName()); } try { @@ -65,7 +65,7 @@ protected CsvImportResult doImport(InputFile input) { - CsvImportResult result = new CsvImportResult(); + CsvImportResult result = CsvImportResult.newResult(null, input.getFileName(), false); prepareImport(); return result; } Copied: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/CsvModelUtil.java (from rev 139, trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/CsvModelUtil.java) =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/CsvModelUtil.java (rev 0) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/CsvModelUtil.java 2011-12-05 08:24:55 UTC (rev 142) @@ -0,0 +1,185 @@ +/* + * #%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 com.google.common.base.Function; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import fr.ifremer.echobase.EchoBaseTechnicalException; +import fr.ifremer.echobase.services.DecoratorService; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.nuiton.topia.persistence.TopiaEntity; +import org.nuiton.util.ObjectUtil; +import org.nuiton.util.StringUtil; +import org.nuiton.util.csv.Common; +import org.nuiton.util.csv.ValueFormatter; +import org.nuiton.util.csv.ValueParserFormatter; + +import java.text.ParseException; +import java.util.Collection; +import java.util.Locale; +import java.util.Map; + +/** + * Usefull class to build csv import-export models. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.2 + */ +public class CsvModelUtil extends Common { + + public static <E extends TopiaEntity> ForeignKeyValue<E> newForeignKeyValue(Class<E> type) { + return new ForeignKeyValue<E>(type); + } + + public static <E extends TopiaEntity> ForeignKeyValue<E> newForeignKeyValue(Class<E> type, Collection<E> entitites) { + return new ForeignKeyValue<E>(type, entitites); + } + + public static <E extends TopiaEntity> ForeignKeyDecoratedValue<E> newForeignKeyDecoratedValue(Class<E> entityType, + DecoratorService decoratorService, Locale locale) { + return new ForeignKeyDecoratedValue<E>(entityType, decoratorService, locale); + } + + public static <E extends TopiaEntity> AssociationValueParserFormatter<E> newAssociationValueParserFormatter(Class<E> type) { + return new AssociationValueParserFormatter<E>(type); + } + + public static class AssociationValueParserFormatter<E extends TopiaEntity> implements ValueParserFormatter<Collection<E>> { + + protected final Class<E> entityType; + + public AssociationValueParserFormatter(Class<E> entityType) { + this.entityType = entityType; + } + + @Override + public Collection<E> parse(String value) throws ParseException { + Collection<E> result = Lists.newArrayList(); + if (StringUtils.isNotBlank(value)) { + + String[] ids = value.split("|"); + for (String id : ids) { + E association = ObjectUtil.newInstance(entityType); + association.setTopiaId(id); + result.add(association); + } + } + return result; + } + + @Override + public String format(Collection<E> e) { + if (CollectionUtils.isEmpty(e)) { + return ""; + } + Collection<String> ids = Lists.newArrayList(); + for (E e1 : e) { + ids.add(e1.getTopiaId()); + } + String value = StringUtil.join(ids, "|", true); + return value; + } + } + + public static class ForeignKeyValue<E extends TopiaEntity> implements ValueParserFormatter<E> { + + protected final Class<E> entityType; + + protected final Map<String, E> universe; + + public ForeignKeyValue(Class<E> entityType, Collection<E> entitites) { + this.entityType = entityType; + universe = Maps.uniqueIndex(entitites, new Function<E, String>() { + @Override + public String apply(E input) { + return input.getTopiaId(); + } + }); + } + + public ForeignKeyValue(Class<E> entityType) { + this.entityType = entityType; + universe = Maps.newHashMap(); + } + + @Override + public E parse(String value) throws ParseException { + E result = null; + if (StringUtils.isNotBlank(value)) { + + // get entity from universe + result = universe.get(value); + + if (result == null) { + + // can not find entity this is a big problem for us... + throw new EchoBaseTechnicalException( + "Could not find entity with name " + value); + } + } + return result; + } + + @Override + public String format(E e) { + String value = ""; + if (e != null) { + value = e.getTopiaId(); + } + return value; + } + } + + public static class ForeignKeyDecoratedValue<E extends TopiaEntity> implements ValueFormatter<E> { + + protected final Class<E> entityType; + + protected final DecoratorService decoratorService; + + protected final Locale locale; + + public ForeignKeyDecoratedValue(Class<E> entityType, DecoratorService decoratorService, Locale locale) { + this.entityType = entityType; + this.decoratorService = decoratorService; + this.locale = locale; + + } + + @Override + public String format(E e) { + String value = ""; + if (e != null) { + + value = decoratorService.decorate(locale, e, null); + } + return value; + } + } + + protected CsvModelUtil() { + // avoid instanciation on helper class + } +} Property changes on: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/CsvModelUtil.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/EntityAssociationCsvModel.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/EntityAssociationCsvModel.java (rev 0) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/EntityAssociationCsvModel.java 2011-12-05 08:24:55 UTC (rev 142) @@ -0,0 +1,102 @@ +/* + * #%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.entities.meta.AssociationMeta; +import org.nuiton.topia.persistence.TopiaEntity; +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; + +/** + * A model to import / export associations of entities into csv files. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.2 + */ +public class EntityAssociationCsvModel<E extends TopiaEntity> implements ImportExportModel<E> { + + protected final char separator; + + protected final AssociationMeta meta; + + protected ModelBuilder<E> modelBuilder; + + public static <E extends TopiaEntity> EntityAssociationCsvModel<E> newModel(char separator, + AssociationMeta meta) { + return new EntityAssociationCsvModel<E>(separator, meta); + } + + @Override + public char getSeparator() { + return separator; + } + + @Override + public Collection<ExportableColumn<E, Object>> getColumnsForExport() { + return (Collection) + modelBuilder.getColumnsForExport(); + } + + @Override + public Collection<ImportableColumn<E, Object>> getColumnsForImport() { + return (Collection) + modelBuilder.getColumnsForImport(); + } + + @Override + public void pushCsvHeaderNames(List<String> headerNames) { + } + + @Override + public E newEmptyInstance() { + return (E) meta.newEntity(); + } + + protected EntityAssociationCsvModel(char separator, + AssociationMeta meta) { + this.separator = separator; + this.meta = meta; + modelBuilder = new ModelBuilder<E>(); + + // topiaId <-> topiaId + modelBuilder.newColumnForImportExport( + TopiaEntity.TOPIA_ID, + TopiaEntity.TOPIA_ID + ); + + // add association -> target + modelBuilder.newColumnForImportExport( + meta.getName(), + meta.getName(), + CsvModelUtil.newAssociationValueParserFormatter(meta.getTarget().getImplementation()) + ); + } + + +} \ No newline at end of file Property changes on: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/EntityAssociationCsvModel.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/EntityCsvModel.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/EntityCsvModel.java (rev 0) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/EntityCsvModel.java 2011-12-05 08:24:55 UTC (rev 142) @@ -0,0 +1,184 @@ +/* + * #%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.entities.meta.TableMeta; +import org.nuiton.topia.persistence.TopiaEntity; +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.Date; +import java.util.List; + +/** + * A model to import / export entities into csv files. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.2 + */ +public class EntityCsvModel<E extends TopiaEntity> implements ImportExportModel<E> { + + protected final char separator; + + protected final TableMeta tableMeta; + + protected ModelBuilder<E> modelBuilder; + + public static <E extends TopiaEntity> EntityCsvModel<E> newModel( + char separator, + TableMeta tableMeta, + String idHeader + ) { + return new EntityCsvModel<E>(separator, tableMeta, idHeader); + } + + @Override + public char getSeparator() { + return separator; + } + + @Override + public Collection<ExportableColumn<E, Object>> getColumnsForExport() { + return (Collection) + modelBuilder.getColumnsForExport(); + } + + @Override + public Collection<ImportableColumn<E, Object>> getColumnsForImport() { + return (Collection) + modelBuilder.getColumnsForImport(); + } + + @Override + public void pushCsvHeaderNames(List<String> headerNames) { + } + + @Override + public E newEmptyInstance() { + return (E) tableMeta.newEntity(); + } + + public void addForeignKeyForExport(String propertyName, + Class<TopiaEntity> entityType) { + + modelBuilder.newColumnForExport( + propertyName, + propertyName, + CsvModelUtil.newForeignKeyValue(entityType) + ); + } + + public void addForeignKeyForImport(String propertyName, + Class<TopiaEntity> entityType, + Collection<TopiaEntity> universe) { + + modelBuilder.newMandatoryColumn( + propertyName, + propertyName, + CsvModelUtil.newForeignKeyValue(entityType, universe) + ); + } + + public void addNewColumnForExport(String headerName, + String propertyName, + CsvModelUtil.ForeignKeyDecoratedValue<TopiaEntity> topiaEntityForeignKeyDecoratedValue) { + modelBuilder.newColumnForExport( + headerName, + propertyName, + topiaEntityForeignKeyDecoratedValue + ); + } + + public void addDefaultColumn(String propertyName, + Class<?> type) { + + if (Date.class.equals(type)) { + modelBuilder.newColumnForImportExport( + propertyName, + propertyName, + CsvModelUtil.DAY_TIME_SECOND + ); + } else if (float.class.equals(type)) { + modelBuilder.newColumnForImportExport( + propertyName, + propertyName, + CsvModelUtil.PRIMITIVE_FLOAT + ); + } else if (Float.class.equals(type)) { + modelBuilder.newColumnForImportExport( + propertyName, + propertyName, + CsvModelUtil.FLOAT + ); + } else if (int.class.equals(type)) { + modelBuilder.newColumnForImportExport( + propertyName, + propertyName, + CsvModelUtil.PRIMITIVE_INTEGER + ); + } else if (Integer.class.equals(type)) { + modelBuilder.newColumnForImportExport( + propertyName, + propertyName, + CsvModelUtil.INTEGER + ); + } else if (boolean.class.equals(type)) { + modelBuilder.newColumnForImportExport( + propertyName, + propertyName, + CsvModelUtil.PRIMITIVE_BOOLEAN + ); + } else if (Boolean.class.equals(type)) { + modelBuilder.newColumnForImportExport( + propertyName, + propertyName, + CsvModelUtil.BOOLEAN + ); + } else { + + // string + modelBuilder.newColumnForImportExport( + propertyName, + propertyName + ); + } + } + + protected EntityCsvModel(char separator, + TableMeta tableMeta, + String idHeader) { + this.separator = separator; + this.tableMeta = tableMeta; + modelBuilder = new ModelBuilder<E>(); + modelBuilder.newColumnForImportExport( + idHeader, + TopiaEntity.TOPIA_ID + ); + } + + +} \ No newline at end of file Property changes on: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/EntityCsvModel.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/models/ExportSqlCsvModel.java (from rev 139, trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportSqlCsvModel.java) =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/ExportSqlCsvModel.java (rev 0) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/ExportSqlCsvModel.java 2011-12-05 08:24:55 UTC (rev 142) @@ -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.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(); + } +} Property changes on: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/ExportSqlCsvModel.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Deleted: trunk/echobase-services/src/main/resources/echobase-db.properties =================================================================== --- trunk/echobase-services/src/main/resources/echobase-db.properties 2011-12-05 08:22:27 UTC (rev 141) +++ trunk/echobase-services/src/main/resources/echobase-db.properties 2011-12-05 08:24:55 UTC (rev 142) @@ -1,41 +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% -### -### -# DB configuration -hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect -hibernate.connection.driver_class=org.postgresql.Driver -hibernate.connection.provider_class=org.nuiton.topia.framework.TopiaConnectionProvider -hibernate.show_sql=false -hibernate.format_sql=false -hibernate.use_sql_comments=false -hibernate.hbm2ddl.auto=none -hibernate.connection.username=${configuration.login} -hibernate.connection.password=${configuration.password} -hibernate.connection.url=${configuration.url} - -# Migration configuration -topia.service.migration=org.nuiton.topia.migration.TopiaMigrationEngine -topia.service.migration.callback=fr.ifremer.echobase.services.migration.EchoBaseMigrationCallback -topia.service.migration.showSql=true - Modified: trunk/echobase-services/src/test/java/fr/ifremer/echobase/TestHelper.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/TestHelper.java 2011-12-05 08:22:27 UTC (rev 141) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/TestHelper.java 2011-12-05 08:24:55 UTC (rev 142) @@ -23,10 +23,16 @@ */ package fr.ifremer.echobase; +import com.google.common.base.Supplier; +import fr.ifremer.echobase.entities.EchoBaseEntityEnum; import org.apache.commons.io.FileUtils; import org.junit.Ignore; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.persistence.TopiaEntity; import java.io.File; +import java.util.ArrayList; +import java.util.List; /** * Utility methods for tests @@ -39,6 +45,8 @@ protected static File basedir; + protected static final long timestamp = System.currentTimeMillis(); + public static File getBasedir() { //FIXME : No we do not want to go in tmp directory :( We want to stay // inside of the project. So a mvn clean will remove all tests data. @@ -53,4 +61,38 @@ return basedir; } + public static File getTestBasedir(Class<?> testClass) { + File result = new File(getBasedir(), testClass.getName() + "_" + timestamp); + return result; + } + + public static TopiaContext getRootContext(File dbDir) { + EchoBaseTopiaRootContextSupplierFactory factory = + new EchoBaseTopiaRootContextSupplierFactory(); + + Supplier<TopiaContext> topiaContextSupplier = + factory.newEmbeddedDatabase(dbDir); + return topiaContextSupplier.get(); + } + + /** + * Used to get all contract of a package + * + * @param entitiesPackage package contening desired entities + * @return contracts contained in entitiesPackage + */ + public static EchoBaseEntityEnum[] getContractsOf(Package entitiesPackage) { + EchoBaseEntityEnum[] echoBaseEntityEnums = EchoBaseEntityEnum.values(); + List<EchoBaseEntityEnum> refClasses = new ArrayList<EchoBaseEntityEnum>(); + for (EchoBaseEntityEnum echoBaseEntityEnum : echoBaseEntityEnums) { + + // Get all entities in package fr.ifremer.echobase.entities.references + Class<? extends TopiaEntity> contract = echoBaseEntityEnum.getContract(); + if (entitiesPackage.equals(contract.getPackage())) { + refClasses.add(echoBaseEntityEnum); + } + } + return refClasses.toArray(new EchoBaseEntityEnum[refClasses.size()]); + } + } Modified: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/AbstractEchoBaseServiceTest.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/AbstractEchoBaseServiceTest.java 2011-12-05 08:22:27 UTC (rev 141) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/AbstractEchoBaseServiceTest.java 2011-12-05 08:24:55 UTC (rev 142) @@ -50,8 +50,11 @@ public abstract class AbstractEchoBaseServiceTest extends Assert { protected TopiaContext transaction; + protected EchoBaseConfiguration config; + protected EchoBaseServiceFactory serviceFactory; + protected DbMeta dbMeta; @After @@ -92,7 +95,7 @@ if (dbMeta == null) { List<EchoBaseEntityEnum> entityEnums = Lists.newArrayList(EchoBaseEntityEnum.values()); - dbMeta = new DbMeta(entityEnums.toArray(new EchoBaseEntityEnum[entityEnums.size()])); + dbMeta = DbMeta.newDbMeta(entityEnums); } return dbMeta; } Added: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/DbImportExportServiceTest.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/DbImportExportServiceTest.java (rev 0) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/DbImportExportServiceTest.java 2011-12-05 08:24:55 UTC (rev 142) @@ -0,0 +1,161 @@ +/* + * #%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 com.google.common.collect.Sets; +import fr.ifremer.echobase.TestHelper; +import fr.ifremer.echobase.entities.EchoBaseEntityEnum; +import fr.ifremer.echobase.entities.EntitiesUtil; +import junit.framework.Assert; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TestName; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.persistence.TopiaEntityEnum; +import org.nuiton.topia.replication.TopiaReplicationModelBuilder; +import org.nuiton.topia.replication.model.ReplicationModel; +import org.nuiton.topia.replication.model.ReplicationNode; + +import java.io.File; +import java.util.Collection; +import java.util.List; +import java.util.Set; + +/** + * Test the {@link DbImportExportService}. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.2 + */ +public class DbImportExportServiceTest { + + /** Logger. */ + private static final Log log = + LogFactory.getLog(DbImportExportServiceTest.class); + + @Rule + public TestName testName = new TestName(); + + protected File testdir; + + protected TopiaContext rootContext; + + @Before + public void setUp() throws Exception { + testdir = new File(TestHelper.getTestBasedir(getClass()), + testName.getMethodName()); + if (log.isInfoEnabled()) { + log.info("Test directory = " + testdir); + } +// rootContext = TestHelper.getRootContext(testdir); + } + + @Test + public void testGetReferentielCopyModel() throws TopiaException { + + EchoBaseEntityEnum[] types = EntitiesUtil.getReferenceTypesForCopy(); + Set<TopiaEntityEnum> dones = Sets.newHashSet(); + + ReplicationModel model = new ReplicationModel(types, false); + new TopiaReplicationModelBuilder().initModel(model, false); + List<ReplicationNode> order = model.getOrder(); + + StringBuilder buffer = new StringBuilder(); + for (ReplicationNode node : order) { + TopiaEntityEnum type = node.getContract(); + Assert.assertTrue(type + " was already treated.", dones.add(type)); + Collection<ReplicationNode> dependencies = node.getDependencies().values(); + for (ReplicationNode dependency : node.getDependencies().values()) { + TopiaEntityEnum dependencyType = dependency.getContract(); + Assert.assertTrue(dependencyType + + " was detected as a dependency of node " + + type + " but was not still registred", + dones.contains(dependencyType)); + } + Collection<ReplicationNode> assocations = node.getAssociations().values(); + for (ReplicationNode assocation : assocations) { + TopiaEntityEnum assocationType = assocation.getContract(); + Assert.assertTrue(assocationType + + " was detected as a association of node " + + type + " but was not still registred", + dones.contains(assocationType)); + } + buffer.append('\n'); + buffer.append(type); + buffer.append(" - "); + buffer.append(dependencies); + buffer.append(" - "); + buffer.append(assocations); + } + if (log.isInfoEnabled()) { + log.info(buffer.toString()); + } + } + + @Test + public void testGetDataCopyModel() throws TopiaException { + + EchoBaseEntityEnum[] types = EntitiesUtil.getDataTypesforCopy(); + Set<TopiaEntityEnum> dones = Sets.newHashSet(); + + ReplicationModel model = new ReplicationModel(types, false); + new TopiaReplicationModelBuilder().initModel(model, false); + List<ReplicationNode> order = model.getOrder(); + + StringBuilder buffer = new StringBuilder(); + for (ReplicationNode node : order) { + TopiaEntityEnum type = node.getContract(); + Assert.assertTrue(type + " was already treated.", dones.add(type)); + Collection<ReplicationNode> dependencies = node.getDependencies().values(); + for (ReplicationNode dependency : node.getDependencies().values()) { + TopiaEntityEnum dependencyType = dependency.getContract(); + Assert.assertTrue(dependencyType + + " was detected as a dependency of node " + + type + " but was not still registred", + dones.contains(dependencyType)); + } + Collection<ReplicationNode> assocations = node.getAssociations().values(); + for (ReplicationNode assocation : assocations) { + TopiaEntityEnum assocationType = assocation.getContract(); + Assert.assertTrue(assocationType + + " was detected as a association of node " + + type + " but was not still registred", + dones.contains(assocationType)); + } + buffer.append('\n'); + buffer.append(type); + buffer.append(" - "); + buffer.append(dependencies); + buffer.append(" - "); + buffer.append(assocations); + } + if (log.isInfoEnabled()) { + log.info(buffer.toString()); + } + } +} Property changes on: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/DbImportExportServiceTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Deleted: trunk/echobase-services/src/test/resources/echobase.properties =================================================================== --- trunk/echobase-services/src/test/resources/echobase.properties 2011-12-05 08:22:27 UTC (rev 141) +++ trunk/echobase-services/src/test/resources/echobase.properties 2011-12-05 08:24:55 UTC (rev 142) @@ -1,24 +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% -### -data.directory=${java.io.tmpdir} \ No newline at end of file
participants (1)
-
tchemit@users.forge.codelutin.com