Topia-commits
Threads by month
- ----- 2026 -----
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2005 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2004 -----
- December
- November
- October
- September
- August
- July
- June
- May
September 2012
- 2 participants
- 16 discussions
r2660 - in branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv: . in
by tchemit@users.nuiton.org Sept. 11, 2012
by tchemit@users.nuiton.org Sept. 11, 2012
Sept. 11, 2012
Author: tchemit
Date: 2012-09-11 18:47:05 +0200 (Tue, 11 Sep 2012)
New Revision: 2660
Url: http://nuiton.org/repositories/revision/topia/2660
Log:
fixes #2310: Add progression model while importing data
Added:
branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/CsvProgressionModel.java
Modified:
branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in/CsvImportResult.java
branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in/TopiaCsvImports.java
Added: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/CsvProgressionModel.java
===================================================================
--- branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/CsvProgressionModel.java (rev 0)
+++ branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/CsvProgressionModel.java 2012-09-11 16:47:05 UTC (rev 2660)
@@ -0,0 +1,46 @@
+package org.nuiton.topia.persistence.csv;
+
+/*
+ * #%L
+ * ToPIA :: Persistence
+ * $Id$
+ * $HeadURL$
+ * %%
+ * Copyright (C) 2004 - 2012 CodeLutin
+ * %%
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser 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 Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU General Lesser Public
+ * License along with this program. If not, see
+ * <http://www.gnu.org/licenses/lgpl-3.0.html>.
+ * #L%
+ */
+
+import java.io.Serializable;
+
+/**
+ * Csv progression model contract.
+ *
+ * @author tchemit <chemit(a)codelutin.com>
+ * @since 2.6.13
+ */
+public interface CsvProgressionModel extends Serializable {
+
+ long getNbSteps();
+
+ float getProgression();
+
+ void setProgression(float progression);
+
+ void incrementsProgression();
+
+ void setNbSteps(long nbSteps);
+}
Property changes on: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/CsvProgressionModel.java
___________________________________________________________________
Added: svn:keywords
+ Author Date Id Revision HeadURL
Added: svn:eol-style
+ native
Modified: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in/CsvImportResult.java
===================================================================
--- branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in/CsvImportResult.java 2012-09-10 12:29:27 UTC (rev 2659)
+++ branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in/CsvImportResult.java 2012-09-11 16:47:05 UTC (rev 2660)
@@ -24,6 +24,7 @@
*/
import org.nuiton.topia.persistence.TopiaEntityEnum;
+import org.nuiton.topia.persistence.csv.CsvProgressionModel;
import java.io.Serializable;
@@ -53,20 +54,33 @@
/** Count of updated entities. */
protected int numberUpdated;
+ protected final CsvProgressionModel progressionModel;
+
public static <T extends TopiaEntityEnum> CsvImportResult<T> newResult(T entityType,
String importFileName,
boolean createIfNotFound) {
+ CsvImportResult<T> result = newResult(entityType, importFileName,
+ createIfNotFound, null);
+ return result;
+ }
+
+ public static <T extends TopiaEntityEnum> CsvImportResult<T> newResult(T entityType,
+ String importFileName,
+ boolean createIfNotFound,
+ CsvProgressionModel progressionModel) {
CsvImportResult<T> result = new CsvImportResult<T>(entityType, importFileName,
- createIfNotFound);
+ createIfNotFound, progressionModel);
return result;
}
protected CsvImportResult(T entityType,
String importFileName,
- boolean createIfNotFound) {
+ boolean createIfNotFound,
+ CsvProgressionModel progressionModel) {
this.entityType = entityType;
this.importFileName = importFileName;
this.createIfNotFound = createIfNotFound;
+ this.progressionModel = progressionModel;
}
public T getEntityType() {
@@ -98,4 +112,7 @@
numberUpdated++;
}
+ public CsvProgressionModel getProgressionModel() {
+ return progressionModel;
+ }
}
Modified: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in/TopiaCsvImports.java
===================================================================
--- branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in/TopiaCsvImports.java 2012-09-10 12:29:27 UTC (rev 2659)
+++ branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in/TopiaCsvImports.java 2012-09-11 16:47:05 UTC (rev 2660)
@@ -31,9 +31,11 @@
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.persistence.TopiaDAO;
import org.nuiton.topia.persistence.TopiaEntity;
import org.nuiton.topia.persistence.TopiaEntityEnum;
+import org.nuiton.topia.persistence.csv.CsvProgressionModel;
import org.nuiton.topia.persistence.metadata.AssociationMeta;
import org.nuiton.topia.persistence.metadata.MetaFilenameAware;
import org.nuiton.topia.persistence.metadata.TableMeta;
@@ -186,9 +188,9 @@
* @see ImportStrategy#importTable(TableMeta, Import, CsvImportResult)
*/
public static <T extends TopiaEntityEnum, E extends TopiaEntity> Iterable<E> importTableAndReturn(Reader reader,
- ImportStrategy<T> importStrategy,
- TableMeta<T> meta,
- CsvImportResult<T> csvResult) throws TopiaException, IOException {
+ ImportStrategy<T> importStrategy,
+ TableMeta<T> meta,
+ CsvImportResult<T> csvResult) throws TopiaException, IOException {
if (log.isInfoEnabled()) {
@@ -246,6 +248,8 @@
Import<E> importer,
CsvImportResult<T> csvResult) throws TopiaException {
+ CsvProgressionModel progressionModel = csvResult == null ? null :
+ csvResult.getProgressionModel();
for (E entity : importer) {
Map<String, Object> properties = meta.prepareCreate(
@@ -256,15 +260,54 @@
if (csvResult != null) {
csvResult.incrementsNumberUpdated();
+ if (progressionModel != null) {
+ progressionModel.incrementsProgression();
+ }
}
}
}
+ public static <T extends TopiaEntityEnum, E extends TopiaEntity> void importAllEntities(TopiaDAO<E> dao,
+ TableMeta<T> meta,
+ Import<E> importer,
+ CsvImportResult<T> csvResult,
+ int nbRowBuffer) throws TopiaException {
+
+ TopiaContextImplementor context = dao.getContext();
+
+ CsvProgressionModel progressionModel = csvResult == null ? null :
+ csvResult.getProgressionModel();
+
+ int compt = 0;
+ for (E entity : importer) {
+
+ Map<String, Object> properties = meta.prepareCreate(
+ entity, entity.getTopiaId());
+ E entityToSave = dao.create(properties);
+
+ meta.copy(entity, entityToSave);
+
+ if (csvResult != null) {
+ csvResult.incrementsNumberUpdated();
+ if (progressionModel != null) {
+ progressionModel.incrementsProgression();
+ }
+ }
+ compt++;
+ if (compt % nbRowBuffer == 0) {
+ // flush it
+ context.getHibernate().flush();
+ }
+ }
+ }
+
public static <T extends TopiaEntityEnum, E extends TopiaEntity> Iterable<E> importAllEntitiesAndReturnThem(TopiaDAO<E> dao,
TableMeta<T> meta,
Import<E> importer,
CsvImportResult<T> csvResult) throws TopiaException {
+ CsvProgressionModel progressionModel = csvResult == null ? null :
+ csvResult.getProgressionModel();
List<E> result = Lists.newArrayList();
for (E entity : importer) {
@@ -276,7 +319,11 @@
if (csvResult != null) {
csvResult.incrementsNumberUpdated();
+ if (progressionModel != null) {
+ progressionModel.incrementsProgression();
+ }
}
+
result.add(entityToSave);
}
return result;
@@ -288,6 +335,8 @@
Import<E> importer,
CsvImportResult<T> csvResult) throws TopiaException {
+ CsvProgressionModel progressionModel = csvResult == null ? null :
+ csvResult.getProgressionModel();
for (E entity : importer) {
String topiaId = entity.getTopiaId();
@@ -311,6 +360,9 @@
if (csvResult != null) {
csvResult.incrementsNumberCreated();
+ if (progressionModel != null) {
+ progressionModel.incrementsProgression();
+ }
}
} else {
// existing entity, nothing to create
@@ -330,6 +382,8 @@
CsvImportResult<T> csvResult,
int nbRowBuffer) throws TopiaException {
+ CsvProgressionModel progressionModel = csvResult == null ? null :
+ csvResult.getProgressionModel();
T source = meta.getSource();
T target = meta.getTarget();
@@ -361,6 +415,9 @@
}
if (csvResult != null) {
csvResult.incrementsNumberUpdated();
+ if (progressionModel != null) {
+ progressionModel.incrementsProgression();
+ }
}
}
if (builder.length() > 0) {
@@ -374,6 +431,8 @@
CsvImportResult<T> csvResult,
int nbRowBuffer) throws TopiaException {
+ CsvProgressionModel progressionModel = csvResult == null ? null :
+ csvResult.getProgressionModel();
T source = meta.getSource();
T target = meta.getTarget();
@@ -409,6 +468,9 @@
}
if (csvResult != null) {
csvResult.incrementsNumberUpdated();
+ if (progressionModel != null) {
+ progressionModel.incrementsProgression();
+ }
}
}
if (builder.length() > 0) {
@@ -423,6 +485,8 @@
CsvImportResult<T> csvResult,
int nbRowBuffer) throws TopiaException {
+ CsvProgressionModel progressionModel = csvResult == null ? null :
+ csvResult.getProgressionModel();
T source = meta.getSource();
T target = meta.getTarget();
@@ -460,6 +524,9 @@
}
if (csvResult != null) {
csvResult.incrementsNumberUpdated();
+ if (progressionModel != null) {
+ progressionModel.incrementsProgression();
+ }
}
}
if (builder.length() > 0) {
@@ -474,6 +541,9 @@
CsvImportResult<T> csvResult,
int nbRowBuffer) throws TopiaException {
+ CsvProgressionModel progressionModel = csvResult == null ? null :
+ csvResult.getProgressionModel();
+
T source = meta.getSource();
T target = meta.getTarget();
@@ -508,6 +578,9 @@
}
if (csvResult != null) {
csvResult.incrementsNumberUpdated();
+ if (progressionModel != null) {
+ progressionModel.incrementsProgression();
+ }
}
}
if (builder.length() > 0) {
1
0
r2659 - branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in
by tchemit@users.nuiton.org Sept. 10, 2012
by tchemit@users.nuiton.org Sept. 10, 2012
Sept. 10, 2012
Author: tchemit
Date: 2012-09-10 14:29:27 +0200 (Mon, 10 Sep 2012)
New Revision: 2659
Url: http://nuiton.org/repositories/revision/topia/2659
Log:
fixes #2309: Add a new method to import a talbe and return imported entities
Modified:
branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in/ImportStrategy.java
branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in/TopiaCsvImports.java
Modified: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in/ImportStrategy.java
===================================================================
--- branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in/ImportStrategy.java 2012-09-07 16:21:52 UTC (rev 2658)
+++ branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in/ImportStrategy.java 2012-09-10 12:29:27 UTC (rev 2659)
@@ -49,7 +49,7 @@
ImportModelFactory<T> getModelFactory();
/**
- * Import a table given a {@code importer}. with an optional csv result.
+ * Import a table given a {@code importer} with an optional csv result.
*
* @param meta type of table to import
* @param importer the csv importer
@@ -60,8 +60,22 @@
CsvImportResult<T> csvResult) throws TopiaException;
/**
- * Import a association given a {@code importer}. with an optional csv result.
+ * Import a table given a {@code importer} with an optional csv result,
+ * and return them.
*
+ * @param meta type of table to import
+ * @param importer the csv importer
+ * @param csvResult optional object where to put csv import result
+ * @return imported entities
+ * @throws TopiaException if any db problem
+ * @since 2.6.14
+ */
+ <E extends TopiaEntity> Iterable<E> importTableAndReturnThem(TableMeta<T> meta, Import<E> importer,
+ CsvImportResult<T> csvResult) throws TopiaException;
+
+ /**
+ * Import a association given a {@code importer} with an optional csv result.
+ *
* @param meta type of association to import
* @param importer the csv importer
* @param csvResult optional object where to put csv import result
Modified: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in/TopiaCsvImports.java
===================================================================
--- branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in/TopiaCsvImports.java 2012-09-07 16:21:52 UTC (rev 2658)
+++ branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in/TopiaCsvImports.java 2012-09-10 12:29:27 UTC (rev 2659)
@@ -164,14 +164,47 @@
ImportModel<E> model = importStrategy.getModelFactory().buildForImport(meta);
Import<E> importer = Import.newImport(model, reader);
try {
- importStrategy.importTable(meta, importer,
- csvResult);
+ importStrategy.importTable(meta, importer, csvResult);
} finally {
importer.close();
}
}
/**
+ * To import a table (given by his {@code meta}) from a reader and a strategy.
+ * <p/>
+ * Result of import can be stored in an optional csv result.
+ *
+ * @param reader where to read csv data
+ * @param importStrategy import strategy used to store csv data
+ * @param meta meta of the data
+ * @param csvResult optional csv result
+ * @param <T> type of entity enum
+ * @param <E> type of data
+ * @throws TopiaException if any db problem while storing datas
+ * @throws IOException if any pb while reading csv data
+ * @see ImportStrategy#importTable(TableMeta, Import, CsvImportResult)
+ */
+ public static <T extends TopiaEntityEnum, E extends TopiaEntity> Iterable<E> importTableAndReturn(Reader reader,
+ ImportStrategy<T> importStrategy,
+ TableMeta<T> meta,
+ CsvImportResult<T> csvResult) throws TopiaException, IOException {
+
+
+ if (log.isInfoEnabled()) {
+ log.info("Will import " + meta);
+ }
+
+ ImportModel<E> model = importStrategy.getModelFactory().buildForImport(meta);
+ Import<E> importer = Import.newImport(model, reader);
+ try {
+ return importStrategy.importTableAndReturnThem(meta, importer, csvResult);
+ } finally {
+ importer.close();
+ }
+ }
+
+ /**
* To import a association (given by his {@code meta}) from a reader and a strategy.
* <p/>
* Result of import can be stored in an optional csv result.
1
0
r2658 - branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in
by tchemit@users.nuiton.org Sept. 7, 2012
by tchemit@users.nuiton.org Sept. 7, 2012
Sept. 7, 2012
Author: tchemit
Date: 2012-09-07 18:21:52 +0200 (Fri, 07 Sep 2012)
New Revision: 2658
Url: http://nuiton.org/repositories/revision/topia/2658
Log:
fixes #2306: CsvFileImportResult does not give back correct entities number
Modified:
branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in/CsvFileImportResult.java
Modified: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in/CsvFileImportResult.java
===================================================================
--- branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in/CsvFileImportResult.java 2012-09-05 15:59:50 UTC (rev 2657)
+++ branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/in/CsvFileImportResult.java 2012-09-07 16:21:52 UTC (rev 2658)
@@ -98,15 +98,13 @@
protected int getInteger(Map<T, Integer> map, T entityType) {
Integer result = map.get(entityType);
- return result == null ? 0 : result;
+ return result;
}
protected void increments(Map<T, Integer> map, T entityType) {
Integer result = map.get(entityType);
- if (result == null) {
-
+ if (result == 0) {
entityTypes.add(entityType);
- result = 0;
}
map.put(entityType, ++result);
}
1
0
r2657 - branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out
by tchemit@users.nuiton.org Sept. 5, 2012
by tchemit@users.nuiton.org Sept. 5, 2012
Sept. 5, 2012
Author: tchemit
Date: 2012-09-05 17:59:50 +0200 (Wed, 05 Sep 2012)
New Revision: 2657
Url: http://nuiton.org/repositories/revision/topia/2657
Log:
fixes #2302: TopiaCsvExport should use as incoming source Iterable not List
Modified:
branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/PrepareDataForExport.java
branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/TopiaCsvExports.java
Modified: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/PrepareDataForExport.java
===================================================================
--- branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/PrepareDataForExport.java 2012-09-05 15:58:19 UTC (rev 2656)
+++ branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/PrepareDataForExport.java 2012-09-05 15:59:50 UTC (rev 2657)
@@ -28,8 +28,6 @@
import org.nuiton.topia.persistence.metadata.AssociationMeta;
import org.nuiton.topia.persistence.metadata.TableMeta;
-import java.util.List;
-
/**
* Prepare data to export.
*
@@ -38,7 +36,7 @@
*/
public interface PrepareDataForExport<T extends TopiaEntityEnum> {
- <E extends TopiaEntity> List<E> prepareData(TableMeta<T> tableMeta);
+ <E extends TopiaEntity> Iterable<E> prepareData(TableMeta<T> tableMeta);
- <E extends TopiaEntity> List<E> prepareData(AssociationMeta<T> associationMeta);
+ <E extends TopiaEntity> Iterable<E> prepareData(AssociationMeta<T> associationMeta);
}
Modified: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/TopiaCsvExports.java
===================================================================
--- branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/TopiaCsvExports.java 2012-09-05 15:58:19 UTC (rev 2656)
+++ branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/TopiaCsvExports.java 2012-09-05 15:59:50 UTC (rev 2657)
@@ -136,8 +136,7 @@
ExportModelFactory<T> modelFactory,
PrepareDataForExport<T> prepareDataForExport) {
-
- List<E> datas = prepareDataForExport.prepareData(tableMeta);
+ Iterable<E> datas = prepareDataForExport.prepareData(tableMeta);
ExportModel<E> model = modelFactory.buildForExport(tableMeta);
return Export.newExport(model, datas);
}
@@ -146,7 +145,7 @@
ExportModelFactory<T> modelFactory,
PrepareDataForExport<T> prepareDataForExport) {
- List<E> datas = prepareDataForExport.prepareData(associationMeta);
+ Iterable<E> datas = prepareDataForExport.prepareData(associationMeta);
ExportModel<E> model = modelFactory.buildForExport(associationMeta);
return Export.newExport(model, datas);
}
1
0
r2656 - branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/framework
by tchemit@users.nuiton.org Sept. 5, 2012
by tchemit@users.nuiton.org Sept. 5, 2012
Sept. 5, 2012
Author: tchemit
Date: 2012-09-05 17:58:19 +0200 (Wed, 05 Sep 2012)
New Revision: 2656
Url: http://nuiton.org/repositories/revision/topia/2656
Log:
fix javadoc
Modified:
branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java
Modified: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java
===================================================================
--- branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java 2012-09-05 15:58:04 UTC (rev 2655)
+++ branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java 2012-09-05 15:58:19 UTC (rev 2656)
@@ -1403,8 +1403,7 @@
/**
* Clear hibernate cache to free memory.
*
- * @see http://docs.jboss.org/hibernate/orm/3.5/reference/en-US/html/transactions.h…
- * @since 2.6.13
+ * see http://docs.jboss.org/hibernate/orm/3.5/reference/en-US/html/transactions.h…
*/
@Override
public void clearCache() throws TopiaException {
1
0
r2655 - in branches/topia-2.6.x/topia-persistence/src: main/java/org/nuiton/topia/persistence test/java/org/nuiton/topia/persistence
by tchemit@users.nuiton.org Sept. 5, 2012
by tchemit@users.nuiton.org Sept. 5, 2012
Sept. 5, 2012
Author: tchemit
Date: 2012-09-05 17:58:04 +0200 (Wed, 05 Sep 2012)
New Revision: 2655
Url: http://nuiton.org/repositories/revision/topia/2655
Log:
fixes #2301: Add methods findAllLazyByQuery on TopiaDAO to iterate on result by a lazy mecanism
Modified:
branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java
branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java
branches/topia-2.6.x/topia-persistence/src/test/java/org/nuiton/topia/persistence/TopiaDAOTest.java
Modified: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java
===================================================================
--- branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java 2012-08-30 14:44:13 UTC (rev 2654)
+++ branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java 2012-09-05 15:58:04 UTC (rev 2655)
@@ -60,7 +60,7 @@
* @author tchemit <chemit(a)codelutin.com>
* @version $Id$
*/
-public interface TopiaDAO<E extends TopiaEntity> extends TopiaDAODeprecated<E> {
+public interface TopiaDAO<E extends TopiaEntity> extends TopiaDAODeprecated<E>, Iterable<E> {
//------------------------------------------------------------------------//
//-- Create - update - delete methods ------------------------------------//
@@ -328,6 +328,74 @@
Object... params) throws TopiaException;
/**
+ * Gets all entities in lazy mode when executing the given select query
+ * for the dao entity type.
+ * <p/>
+ * <strong>Important note:</strong>
+ *
+ * @param hql hql query
+ * @param params query params
+ * @return entites of the query result
+ * @throws TopiaException if any pb while getting datas
+ * @since 2.6.14
+ */
+ Iterable<E> findAllLazyByQuery(String hql,
+ Object... params) throws TopiaException;
+
+ /**
+ * Gets all entities in lazy mode when executing the given select query
+ * for the given {@code type} which may not be a entity type (int, long, map,...).
+ * <p/>
+ * <strong>Important note:</strong>
+ *
+ * @param type type of data to return
+ * @param hql hql query
+ * @param params query params
+ * @return entites of the query result
+ * @throws TopiaException if any pb while getting datas
+ * @since 2.6.14
+ */
+ <R> Iterable<R> findAllLazyByQuery(Class<R> type,
+ String hql,
+ Object... params) throws TopiaException;
+
+ /**
+ * Gets all entities in lazy mode when executing the given select query
+ * for the dao entity type.
+ * <p/>
+ * <strong>Important note:</strong>
+ *
+ * @param batchSize batch size
+ * @param hql hql query
+ * @param params query params
+ * @return entites of the query result
+ * @throws TopiaException if any pb while getting datas
+ * @since 2.6.14
+ */
+ Iterable<E> findAllLazyByQuery(int batchSize,
+ String hql,
+ Object... params) throws TopiaException;
+
+ /**
+ * Gets all entities in lazy mode when executing the given select query
+ * for the given {@code type} which may not be a entity type (int, long, map,...).
+ * <p/>
+ * <strong>Important note:</strong>
+ *
+ * @param type type of data to return
+ * @param batchSize batch size
+ * @param hql hql query
+ * @param params query params
+ * @return entites of the query result
+ * @throws TopiaException if any pb while getting datas
+ * @since 2.6.14
+ */
+ <R> Iterable<R> findAllLazyByQuery(Class<R> type,
+ int batchSize,
+ String hql,
+ Object... params) throws TopiaException;
+
+ /**
* Gets a page of entities when executing the given select query for the dao
* entity type (will only return the window of {@code startIndex -
* endIndex} entities).
@@ -340,6 +408,25 @@
* @throws TopiaException if any pb while getting datas
* @since 2.6.12
*/
+ <R> List<R> findAllByQueryWithBound(Class<R> type,
+ String hql,
+ int startIndex,
+ int endIndex,
+ Object... params) throws TopiaException;
+
+ /**
+ * Gets a page of entities when executing the given select query for the dao
+ * entity type (will only return the window of {@code startIndex -
+ * endIndex} entities).
+ *
+ * @param hql hql query to execute
+ * @param startIndex first index of entity to return
+ * @param endIndex last index of entity to return
+ * @param params query params
+ * @return entites of the paginated query result
+ * @throws TopiaException if any pb while getting datas
+ * @since 2.6.12
+ */
List<E> findAllByQueryWithBound(String hql,
int startIndex,
int endIndex,
@@ -349,6 +436,7 @@
* Gets a page of entities of the given select {@code hql} query using the
* {@code pager} to obtain the window of entities to return.
*
+ * @param type type of data to return
* @param hql hql query to execute
* @param pager pager to obtan the correct window of data
* @param params params of the query
@@ -357,6 +445,23 @@
* @see TopiaFilterPagerUtil.FilterPagerBean
* @since 2.6.12
*/
+ <R> List<R> findAllByQueryAndPager(Class<R> type,
+ String hql,
+ TopiaFilterPagerUtil.FilterPagerBean pager,
+ Object... params) throws TopiaException;
+
+ /**
+ * Gets a page of entities of the given select {@code hql} query using the
+ * {@code pager} to obtain the window of entities to return.
+ *
+ * @param hql hql query to execute
+ * @param pager pager to obtan the correct window of data
+ * @param params params of the query
+ * @return entities of the paginated query result
+ * @throws TopiaException if any pb while getting datas
+ * @see TopiaFilterPagerUtil.FilterPagerBean
+ * @since 2.6.12
+ */
List<E> findAllByQueryAndPager(String hql,
TopiaFilterPagerUtil.FilterPagerBean pager,
Object... params) throws TopiaException;
@@ -520,6 +625,24 @@
Class<E> getEntityClass();
/**
+ * Obtains the batch size used to load data.
+ * <p/>
+ * Default value if 1000.
+ *
+ * @return the batch size.
+ * @since 2.6.14
+ */
+ int getBatchSize();
+
+ /**
+ * Set a new default batch size.
+ *
+ * @param batchSize new batch size to use when iterating.
+ * @since 2.6.14
+ */
+ void setBatchSize(int batchSize);
+
+ /**
* When TopiaContextImpl create the TopiaDAOHibernate, it must call this
* method just after.
*
Modified: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java
===================================================================
--- branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2012-08-30 14:44:13 UTC (rev 2654)
+++ branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2012-09-05 15:58:04 UTC (rev 2655)
@@ -38,6 +38,7 @@
package org.nuiton.topia.persistence;
import com.google.common.base.Preconditions;
+import com.google.common.collect.Iterators;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
@@ -46,6 +47,7 @@
import org.hibernate.Session;
import org.hibernate.metadata.ClassMetadata;
import org.nuiton.topia.TopiaException;
+import org.nuiton.topia.TopiaRuntimeException;
import org.nuiton.topia.event.TopiaEntityListener;
import org.nuiton.topia.event.TopiaEntityVetoable;
import org.nuiton.topia.framework.TopiaContextImplementor;
@@ -56,8 +58,10 @@
import java.lang.reflect.InvocationTargetException;
import java.security.Permission;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.NoSuchElementException;
/**
* Cette classe permet d'avoir un ensemble de méthode implantée de façon
@@ -79,10 +83,27 @@
/** to use log facility, just put in your code: log.info(\"...\"); */
private static Log log = LogFactory.getLog(TopiaDAOImpl.class);
+ /**
+ * Type of entity managed by this dao.
+ *
+ * @since ever
+ */
protected Class<E> entityClass;
+ /**
+ * Underlying context used by this dao to do actions on db.
+ *
+ * @since ever
+ */
protected TopiaContextImplementor context;
+ /**
+ * Default batch size used to iterate on data.
+ *
+ * @since 2.6.14
+ */
+ private int batchSize = 1000;
+
@Override
public TopiaEntityEnum getTopiaEntityEnum() {
throw new UnsupportedOperationException(
@@ -95,6 +116,28 @@
"This method must be overided in generated DAO");
}
+ @Override
+ public int getBatchSize() {
+ return batchSize;
+ }
+
+ @Override
+ public void setBatchSize(int batchSize) {
+ this.batchSize = batchSize;
+ }
+
+ @Override
+ public Iterator<E> iterator() {
+
+ Iterator<E> iterator = new FindAllIterator<E, E>(
+ this,
+ getEntityClass(),
+ batchSize,
+ "FROM " + getTopiaEntityEnum().getImplementationFQN());
+
+ return iterator;
+ }
+
/**
* Retourne l'id de l'entity
*
@@ -592,12 +635,14 @@
+ " n'a pas de cle primaire naturelle");
}
+ @Override
public boolean existsByQuery(String hql,
Object... params) throws TopiaException {
long count = countByQuery(hql, params);
return count > 0;
}
+ @Override
public long countByQuery(String hql,
Object... params) throws TopiaException {
@@ -607,11 +652,13 @@
return findByQuery(Long.class, hql, params);
}
+ @Override
public E findByQuery(String hql,
Object... params) throws TopiaException {
return findByQuery(getEntityClass(), hql, params);
}
+ @Override
public <R> R findByQuery(Class<R> type,
String hql,
Object... params) throws TopiaException {
@@ -625,12 +672,14 @@
return (R) unique;
}
+ @Override
public List<E> findAllByQuery(String hql,
Object... params) throws TopiaException {
return findAllByQuery(getEntityClass(), hql, params);
}
+ @Override
public <R> List<R> findAllByQuery(Class<R> type,
String hql,
Object... params) throws TopiaException {
@@ -642,20 +691,75 @@
return result;
}
- public List<E> findAllByQueryWithBound(String hql,
- int startIndex,
- int endIndex,
- Object... params) throws TopiaException {
+ @Override
+ public Iterable<E> findAllLazyByQuery(String hql,
+ Object... params) throws TopiaException {
+ return findAllLazyByQuery(batchSize, hql, params);
+ }
+ @Override
+ public <R> Iterable<R> findAllLazyByQuery(Class<R> type,
+ String hql,
+ Object... params) throws TopiaException {
+ return findAllLazyByQuery(type, batchSize, hql, params);
+ }
+
+ @Override
+ public Iterable<E> findAllLazyByQuery(int batchSize,
+ String hql,
+ Object... params) throws TopiaException {
+ return findAllLazyByQuery(getEntityClass(), batchSize, hql, params);
+ }
+
+ @Override
+ public <R> Iterable<R> findAllLazyByQuery(Class<R> type,
+ int batchSize,
+ String hql,
+ Object... params) throws TopiaException {
+
+ final Iterator<R> iterator = new FindAllIterator<E, R>(this,
+ type,
+ batchSize,
+ hql,
+ params);
+ return new Iterable<R>() {
+ @Override
+ public Iterator<R> iterator() {
+ return iterator;
+ }
+ };
+ }
+
+ @Override
+ public <R> List<R> findAllByQueryWithBound(Class<R> type,
+ String hql,
+ int startIndex,
+ int endIndex,
+ Object... params) throws TopiaException {
+ Preconditions.checkNotNull(type);
Preconditions.checkNotNull(hql);
- List<E> result = getContext().find(hql, startIndex, endIndex, params);
+ List<R> result = getContext().find(hql, startIndex, endIndex, params);
return result;
}
- public List<E> findAllByQueryAndPager(String hql,
- TopiaFilterPagerUtil.FilterPagerBean pager,
- Object... params) throws TopiaException {
+ @Override
+ public List<E> findAllByQueryWithBound(String hql,
+ int startIndex,
+ int endIndex,
+ Object... params) throws TopiaException {
+ return findAllByQueryWithBound(getEntityClass(),
+ hql,
+ startIndex,
+ endIndex,
+ params);
+ }
+
+ @Override
+ public <R> List<R> findAllByQueryAndPager(Class<R> type,
+ String hql,
+ TopiaFilterPagerUtil.FilterPagerBean pager,
+ Object... params) throws TopiaException {
Preconditions.checkNotNull(pager);
Preconditions.checkNotNull(hql);
@@ -665,13 +769,25 @@
hql += " DESC";
}
}
- List<E> result = findAllByQueryWithBound(hql,
+ List<R> result = findAllByQueryWithBound(type, hql,
pager.getRecordStartIndex(),
pager.getRecordEndIndex() - 1,
params);
return result;
}
+ @Override
+ public List<E> findAllByQueryAndPager(String hql,
+ TopiaFilterPagerUtil.FilterPagerBean pager,
+ Object... params) throws TopiaException {
+
+ return findAllByQueryAndPager(getEntityClass(),
+ hql,
+ pager,
+ params);
+ }
+
+ @Override
public void computeAndAddRecordsToPager(String hql,
TopiaFilterPagerUtil.FilterPagerBean pager,
Object... params) throws TopiaException {
@@ -709,4 +825,82 @@
return meta;
}
+ public static class FindAllIterator<E extends TopiaEntity, R> implements Iterator<R> {
+
+ protected Iterator<R> data;
+
+ protected final TopiaDAO<E> dao;
+
+ protected final Class<R> type;
+
+ protected final String hql;
+
+ protected final Object[] params;
+
+ protected TopiaFilterPagerUtil.FilterPagerBean pager;
+
+ public FindAllIterator(TopiaDAO<E> dao,
+ Class<R> type,
+ int batchSize,
+ String hql,
+ Object... params) {
+ this.dao = dao;
+ this.type = type;
+ this.hql = hql;
+ this.params = params;
+ try {
+ long count = dao.countByQuery("SELECT COUNT(*) " + hql, params);
+ pager = TopiaFilterPagerUtil.newFilterPagerBean();
+ pager.setRecords((int) count);
+ pager.setPageSize(batchSize);
+ TopiaFilterPagerUtil.computeRecordIndexesAndPagesNumber(pager);
+ } catch (TopiaException e) {
+ throw new TopiaRuntimeException(e);
+ }
+
+ // empty iterator (will be changed at first next call)
+ data = Iterators.emptyIterator();
+ }
+
+ @Override
+ public boolean hasNext() {
+ return data.hasNext() || // no more data
+ pager.getPageIndex() < pager.getPagesNumber();
+ }
+
+ @Override
+ public R next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException();
+ }
+
+ if (!data.hasNext()) {
+
+ // must load iterator
+
+ // increments page index
+ pager.setPageIndex(pager.getPageIndex() + 1);
+ TopiaFilterPagerUtil.computeRecordIndexesAndPagesNumber(pager);
+
+ // load new window of data
+ try {
+ data = dao.findAllByQueryAndPager(type,
+ hql,
+ pager,
+ params).iterator();
+ } catch (TopiaException e) {
+ throw new TopiaRuntimeException(e);
+ }
+ }
+
+ R next = data.next();
+ return next;
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException(
+ "This iterator does not support remove operation.");
+ }
+ }
} //TopiaDAOImpl
Modified: branches/topia-2.6.x/topia-persistence/src/test/java/org/nuiton/topia/persistence/TopiaDAOTest.java
===================================================================
--- branches/topia-2.6.x/topia-persistence/src/test/java/org/nuiton/topia/persistence/TopiaDAOTest.java 2012-08-30 14:44:13 UTC (rev 2654)
+++ branches/topia-2.6.x/topia-persistence/src/test/java/org/nuiton/topia/persistence/TopiaDAOTest.java 2012-09-05 15:58:04 UTC (rev 2655)
@@ -25,12 +25,15 @@
package org.nuiton.topia.persistence;
+import com.google.common.collect.Lists;
import org.junit.Assert;
+import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.matchers.JUnitMatchers;
import org.nuiton.topia.TopiaContext;
import org.nuiton.topia.TopiaDatabase;
+import org.nuiton.topia.TopiaException;
import org.nuiton.topia.TopiaTestDAOHelper;
import org.nuiton.topia.test.entities.Person;
import org.nuiton.topia.test.entities.PersonDAO;
@@ -51,6 +54,18 @@
@Rule
public final TopiaDatabase db = new TopiaDatabase();
+ protected TopiaContext context;
+
+ protected PersonDAO dao;
+
+ @Before
+ public void setup() throws TopiaException {
+
+ context = db.beginTransaction();
+
+ dao = TopiaTestDAOHelper.getPersonDAO(context);
+ }
+
/**
* Test de creer une entité et de verifier qu'elle est
* présente dans la persistence au sein de la transaction.
@@ -60,30 +75,128 @@
@Test
public void testCreateAndFindInTransaction() throws Exception {
- TopiaContext context = db.beginTransaction();
-
- PersonDAO personDAO = TopiaTestDAOHelper.getPersonDAO(context);
-
// appel 1 find all
- Person person = personDAO.create(Person.PROPERTY_NAME, "toto");
- List<Person> allPerson = personDAO.findAll();
+ Person person = createPerson("toto");
+ List<Person> allPerson = dao.findAll();
Assert.assertEquals(1, allPerson.size());
context.commitTransaction();
// recherce la personne créée dans la même transaction
- Person person2 = personDAO.create(Person.PROPERTY_NAME, "titi");
- allPerson = personDAO.findAll();
+ Person person2 = createPerson("titi");
+ allPerson = dao.findAll();
Assert.assertEquals(2, allPerson.size());
Assert.assertThat(allPerson, JUnitMatchers.hasItem(person2));
context.rollbackTransaction();
// meme test apres roolback
- Person person3 = personDAO.create(Person.PROPERTY_NAME, "tata");
- allPerson = personDAO.findAll();
+ Person person3 = createPerson("tata");
+ allPerson = dao.findAll();
Assert.assertEquals(2, allPerson.size());
Assert.assertThat(allPerson, JUnitMatchers.hasItem(person3));
context.commitTransaction();
}
+
+ @Test
+ public void findAllLazyByQuery() throws TopiaException {
+
+ Assert.assertEquals(dao.count(), 0);
+
+ createPersons(101);
+
+ Iterable<Person> allByLazy = dao.findAllLazyByQuery(
+ 100,
+ "FROM " + dao.getTopiaEntityEnum().getImplementationFQN());
+
+ List<Person> actual = Lists.newArrayList();
+
+ for (Person person : allByLazy) {
+ actual.add(person);
+ }
+ Assert.assertEquals(dao.count(), actual.size());
+
+ allByLazy = dao.findAllLazyByQuery(
+ 54,
+ "FROM " + dao.getTopiaEntityEnum().getImplementationFQN());
+
+ actual = Lists.newArrayList();
+
+ for (Person person : allByLazy) {
+ actual.add(person);
+ }
+ Assert.assertEquals(dao.count(), actual.size());
+
+ allByLazy = dao.findAllLazyByQuery(
+ 49,
+ "FROM " + dao.getTopiaEntityEnum().getImplementationFQN());
+
+ actual = Lists.newArrayList();
+
+ for (Person person : allByLazy) {
+ actual.add(person);
+ }
+ Assert.assertEquals(dao.count(), actual.size());
+
+ allByLazy = dao.findAllLazyByQuery(
+ 101,
+ "FROM " + dao.getTopiaEntityEnum().getImplementationFQN());
+
+ actual = Lists.newArrayList();
+
+ for (Person person : allByLazy) {
+ actual.add(person);
+ }
+ Assert.assertEquals(dao.count(), actual.size());
+
+ allByLazy = dao.findAllLazyByQuery(
+ 102,
+ "FROM " + dao.getTopiaEntityEnum().getImplementationFQN());
+
+ actual = Lists.newArrayList();
+
+ for (Person person : allByLazy) {
+ actual.add(person);
+ }
+ Assert.assertEquals(dao.count(), actual.size());
+ }
+
+ @Test
+ public void iterateOnTopiaDAO() throws TopiaException {
+
+ createPersons(1999);
+
+ List<Person> excepted = dao.findAll();
+
+ List<Person> actual = Lists.newArrayList();
+
+ for (Person person : dao) {
+ Assert.assertThat(excepted, JUnitMatchers.hasItem(person));
+ actual.add(person);
+ }
+ Assert.assertEquals(excepted.size(), actual.size());
+
+ dao.setBatchSize(54);
+
+ actual = Lists.newArrayList();
+
+ for (Person person : dao) {
+ Assert.assertThat(excepted, JUnitMatchers.hasItem(person));
+ actual.add(person);
+ }
+ Assert.assertEquals(excepted.size(), actual.size());
+
+ }
+
+ protected void createPersons(int number) throws TopiaException {
+ for (int i = 0; i < number; i++) {
+ createPerson("toto" + i);
+ }
+
+ context.commitTransaction();
+ }
+
+ protected Person createPerson(String name) throws TopiaException {
+ return dao.create(Person.PROPERTY_NAME, name);
+ }
}
1
0