Extract CsvReader - move DefaultCsvCallback and add module-info

This commit is contained in:
Rob Bygrave committed 2022-08-25 17:49:11 +12:00
1 parent 216c43eea6
commit 1aac01ead0
17 files changed
+19 -200

No files matched your search

-1
View File
@@ -5,7 +5,6 @@ import io.avaje.lang.Nullable;
import io.ebean.annotation.TxIsolation; import io.ebean.annotation.TxIsolation;
import io.ebean.cache.ServerCacheManager; import io.ebean.cache.ServerCacheManager;
import io.ebean.plugin.Property; import io.ebean.plugin.Property;
import io.ebean.text.csv.CsvReader;
import io.ebean.text.json.JsonContext; import io.ebean.text.json.JsonContext;
import javax.persistence.OptimisticLockException; import javax.persistence.OptimisticLockException;
@@ -9,7 +9,6 @@ import io.ebean.config.DatabaseConfig;
import io.ebean.meta.MetaInfoManager; import io.ebean.meta.MetaInfoManager;
import io.ebean.plugin.Property; import io.ebean.plugin.Property;
import io.ebean.plugin.SpiServer; import io.ebean.plugin.SpiServer;
import io.ebean.text.csv.CsvReader;
import io.ebean.text.json.JsonContext; import io.ebean.text.json.JsonContext;
import javax.persistence.OptimisticLockException; import javax.persistence.OptimisticLockException;
@@ -1,171 +0,0 @@
package io.ebean.text.csv;
import io.ebean.text.StringParser;
import java.io.Reader;
import java.util.Locale;
/**
* Reads CSV data turning it into object graphs that you can be saved (inserted)
* or processed yourself.
* <p>
* This first example doesn't use a {@link CsvCallback} and this means it will
* automatically create a transaction, save the customers and commit the
* transaction when successful.
* </p>
*
* <pre>{@code
* try {
* File f = new File("src/test/resources/test1.csv");
*
* FileReader reader = new FileReader(f, encoding);
*
* CsvReader<Customer> csvReader = DB.createCsvReader(Customer.class);
*
* csvReader.setPersistBatchSize(20);
*
* csvReader.addProperty("status");
* // ignore the next property
* csvReader.addIgnore();
* csvReader.addProperty("name");
* csvReader.addDateTime("anniversary", "dd-MMM-yyyy");
* csvReader.addProperty("billingAddress.line1");
* csvReader.addProperty("billingAddress.city");
*
* csvReader.process(reader);
*
* } catch (Exception e) {
* throw new RuntimeException(e);
* }
* }</pre>
*
* @param <T> the entity bean type
*/
public interface CsvReader<T> {
/**
* Explicitly set the default Locale.
*/
void setDefaultLocale(Locale defaultLocale);
/**
* Set the default format to use for Time types.
*/
void setDefaultTimeFormat(String defaultTimeFormat);
/**
* Set the default format to use for Date types.
*/
void setDefaultDateFormat(String defaultDateFormat);
/**
* Set the default format to use for Timestamp types.
*/
void setDefaultTimestampFormat(String defaultTimestampFormat);
/**
* Set the batch size for using JDBC statement batching.
* <p>
* By default this is set to 20 and setting this to 1 will disable the use of
* JDBC statement batching.
* </p>
*/
void setPersistBatchSize(int persistBatchSize);
/**
* Set to true if there is a header row that should be ignored.
* <p>
* If addPropertiesFromHeader is true then all the properties are added using
* the default time,date and timestamp formats.
* <p>
* If you have a mix of dateTime formats you can not use this method and must
* add the properties yourself.
* </p>
*/
void setHasHeader(boolean hasHeader, boolean addPropertiesFromHeader);
/**
* Same as setHasHeader(true,true);
* <p>
* This will use a header to define all the properties to load using the
* default formats for time, date and datetime types.
* </p>
*/
void setAddPropertiesFromHeader();
/**
* Same as setHasHeader(true, false);
* <p>
* This indicates that there is a header but that it should be ignored.
* </p>
*/
void setIgnoreHeader();
/**
* Set the frequency with which a INFO message will be logged showing the
* progress of the processing. You might set this to 1000 or 10000 etc.
* <p>
* If this is not set then no INFO messages will be logged.
* </p>
*/
void setLogInfoFrequency(int logInfoFrequency);
/**
* Ignore the next column of data.
*/
void addIgnore();
/**
* Define the property which will be loaded from the next column of data.
* <p>
* This takes into account the data type of the property and handles the
* String to object conversion automatically.
* </p>
*/
void addProperty(String propertyName);
/**
* Define the next property and use a custom StringParser to convert the
* string content into the appropriate type for the property.
*/
void addProperty(String propertyName, StringParser parser);
/**
* Add a property with a custom Date/Time/Timestamp format using the default
* Locale. This will convert the string into the appropriate java type for the
* given property (Date, Calendar, SQL Date, Time, Timestamp, JODA etc).
*/
void addDateTime(String propertyName, String dateTimeFormat);
/**
* Add a property with a custom Date/Time/Timestamp format. This will convert
* the string into the appropriate java type for the given property (Date,
* Calendar, SQL Date, Time, Timestamp, JODA etc).
*/
void addDateTime(String propertyName, String dateTimeFormat, Locale locale);
/**
* Automatically create a transaction if required to process all the CSV
* content from the reader.
* <p>
* This will check for a current transaction. If there is no current
* transaction then one is started and will commit (or rollback) at the end of
* processing. This will also set the persistBatchSize on the transaction.
* </p>
*/
void process(Reader reader) throws Exception;
/**
* Process the CSV content passing the bean to the CsvCallback after each row.
* <p>
* This provides you with the ability to modify and process the bean.
* </p>
* <p>
* When using a CsvCallback the reader WILL NOT create a transaction or save
* the bean(s) for you. If you want to insert the processed beans you must
* create your own transaction and save the bean(s) yourself.
* </p>
*/
void process(Reader reader, CsvCallback<T> callback) throws Exception;
}
@@ -1,4 +0,0 @@
/**
* CSV processing objects.
*/
package io.ebean.text.csv;
-1
View File
@@ -40,7 +40,6 @@ module io.ebean.api {
exports io.ebean.service; exports io.ebean.service;
exports io.ebean.text; exports io.ebean.text;
exports io.ebean.text.json; exports io.ebean.text.json;
exports io.ebean.text.csv;
exports io.ebean.util; exports io.ebean.util;
} }
@@ -21,7 +21,6 @@ import io.ebean.plugin.BeanType;
import io.ebean.plugin.Plugin; import io.ebean.plugin.Plugin;
import io.ebean.plugin.Property; import io.ebean.plugin.Property;
import io.ebean.plugin.SpiServer; import io.ebean.plugin.SpiServer;
import io.ebean.text.csv.CsvReader;
import io.ebean.text.json.JsonContext; import io.ebean.text.json.JsonContext;
import io.ebeaninternal.api.*; import io.ebeaninternal.api.*;
import io.ebeaninternal.api.SpiQuery.Type; import io.ebeaninternal.api.SpiQuery.Type;
@@ -1,4 +1,4 @@
package io.ebean.text.csv; package io.ebean.csv.reader;
import io.ebean.Database; import io.ebean.Database;
@@ -1,4 +1,4 @@
package io.ebeaninternal.server.text.csv; package io.ebean.csv.reader;
import io.ebean.Database; import io.ebean.Database;
import io.ebean.bean.EntityBean; import io.ebean.bean.EntityBean;
@@ -6,9 +6,6 @@ import io.ebean.plugin.BeanType;
import io.ebean.plugin.ExpressionPath; import io.ebean.plugin.ExpressionPath;
import io.ebean.text.StringParser; import io.ebean.text.StringParser;
import io.ebean.text.TextException; import io.ebean.text.TextException;
import io.ebean.text.csv.CsvCallback;
import io.ebean.text.csv.CsvReader;
import io.ebean.text.csv.DefaultCsvCallback;
import java.io.Reader; import java.io.Reader;
import java.sql.Types; import java.sql.Types;
@@ -24,7 +21,7 @@ import java.util.Locale;
/** /**
* Implementation of the CsvReader * Implementation of the CsvReader
*/ */
public class TCsvReader<T> {//implements CsvReader<T> { public class CsvReader<T> {
private static final TimeStringParser TIME_PARSER = new TimeStringParser(); private static final TimeStringParser TIME_PARSER = new TimeStringParser();
@@ -52,9 +49,9 @@ public class TCsvReader<T> {//implements CsvReader<T> {
private boolean addPropertiesFromHeader; private boolean addPropertiesFromHeader;
public TCsvReader(Database server, BeanType<T> descriptor) { public CsvReader(Database server, Class<T> type) {
this.server = server; this.server = server;
this.descriptor = descriptor; this.descriptor = server.pluginApi().beanType(type);
} }
////@Override ////@Override
@@ -229,7 +226,6 @@ public class TCsvReader<T> {//implements CsvReader<T> {
private void addPropertiesFromHeader(String[] line) { private void addPropertiesFromHeader(String[] line) {
for (String aLine : line) { for (String aLine : line) {
ExpressionPath elProp = descriptor.expressionPath(aLine); ExpressionPath elProp = descriptor.expressionPath(aLine);
//ElPropertyValue elProp = descriptor.elGetValue(aLine);
if (elProp == null) { if (elProp == null) {
throw new TextException("Property [" + aLine + "] not found"); throw new TextException("Property [" + aLine + "] not found");
} }
@@ -1,4 +1,4 @@
package io.ebeaninternal.server.text.csv; package io.ebean.csv.reader;
// Original name: au.com.bytecode.opencsv.CSVReader // Original name: au.com.bytecode.opencsv.CSVReader
// rbygrave: Made some Java Generics tweaks to remove warnings // rbygrave: Made some Java Generics tweaks to remove warnings
@@ -1,4 +1,4 @@
package io.ebean.text.csv; package io.ebean.csv.reader;
import io.ebean.Database; import io.ebean.Database;
import io.ebean.EbeanVersion; import io.ebean.EbeanVersion;
@@ -1,4 +1,4 @@
package io.ebeaninternal.server.text.csv; package io.ebean.csv.reader;
import io.ebean.text.StringParser; import io.ebean.text.StringParser;
@@ -0,0 +1,6 @@
module io.ebean.csv.reader {
requires io.ebean.api;
exports io.ebean.csv.reader;
}
@@ -1,8 +1,7 @@
package io.ebeaninternal.server.text.csv; package io.ebean.csv.reader;
import io.ebean.DB; import io.ebean.DB;
import io.ebean.Database; import io.ebean.Database;
import io.ebean.plugin.BeanType;
import io.ebean.util.IOUtils; import io.ebean.util.IOUtils;
import org.example.Country; import org.example.Country;
import org.example.Customer; import org.example.Customer;
@@ -12,7 +11,7 @@ import java.io.Reader;
import java.net.URL; import java.net.URL;
import java.util.Locale; import java.util.Locale;
class TestCsvReader { class CsvReaderTest {
@Test @Test
void test() throws Exception { void test() throws Exception {
@@ -29,8 +28,7 @@ class TestCsvReader {
try (Reader reader = IOUtils.newReader(resource.openStream())) { try (Reader reader = IOUtils.newReader(resource.openStream())) {
Database database = DB.getDefault(); Database database = DB.getDefault();
BeanType<Customer> type = database.pluginApi().beanType(Customer.class); CsvReader<Customer> csvReader = new CsvReader<>(database, Customer.class);
TCsvReader<Customer> csvReader = new TCsvReader<>(database, type);
//CsvReader<Customer> csvReader = DB.getDefault().createCsvReader(Customer.class); //CsvReader<Customer> csvReader = DB.getDefault().createCsvReader(Customer.class);
csvReader.setPersistBatchSize(2); csvReader.setPersistBatchSize(2);
@@ -1,4 +1,4 @@
package io.ebeaninternal.server.text.csv; package io.ebean.csv.reader;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -1,4 +1,4 @@
package io.ebeaninternal.server.text.csv; package io.ebean.csv.reader;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -15,7 +15,6 @@ import io.ebean.meta.MetaInfoManager;
import io.ebean.meta.MetricVisitor; import io.ebean.meta.MetricVisitor;
import io.ebean.plugin.Property; import io.ebean.plugin.Property;
import io.ebean.plugin.SpiServer; import io.ebean.plugin.SpiServer;
import io.ebean.text.csv.CsvReader;
import io.ebean.text.json.JsonContext; import io.ebean.text.json.JsonContext;
import io.ebeaninternal.api.*; import io.ebeaninternal.api.*;
import io.ebeaninternal.api.SpiQuery.Type; import io.ebeaninternal.api.SpiQuery.Type;
@@ -13,7 +13,6 @@ import io.ebean.meta.MetaInfoManager;
import io.ebean.plugin.BeanType; import io.ebean.plugin.BeanType;
import io.ebean.plugin.Property; import io.ebean.plugin.Property;
import io.ebean.plugin.SpiServer; import io.ebean.plugin.SpiServer;
import io.ebean.text.csv.CsvReader;
import io.ebean.text.json.JsonContext; import io.ebean.text.json.JsonContext;
import javax.annotation.Nullable; import javax.annotation.Nullable;