Skip to content

Commit 07062eb

Browse files
mmoquiSilverYoCha
authored andcommitted
Bug #9681: Now the filtering of the SQL query result is done directly in Java
and no more in JDBC. Add an operation item in the menu to reload the result (hence to execute again the query).
1 parent c5a20fc commit 07062eb

File tree

22 files changed

+356
-312
lines changed

22 files changed

+356
-312
lines changed

jdbcConnector/jdbcConnector-configuration/src/main/config/properties/org/silverpeas/jdbcConnector/multilang/jdbcConnector.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,5 @@ dataSourceField=Source de donn\u00E9es
8585
parametresConnectionIncorrects=mauvais param\u00E8tres de connexions
8686
erreurRequeteIncorrect=La requ\u00E8te SQL est incorrecte
8787
critereSupprimes=Les crit\u00E8res ont \u00E9t\u00E9 enlev\u00E9s
88-
sqlRequestExecutionFailure=L'ex\u00E9cution de la requ\u00E8te SQL a \u00E9chou\u00E9
88+
sqlRequestExecutionFailure=L'ex\u00E9cution de la requ\u00E8te SQL a \u00E9chou\u00E9
89+
reloadRequest=Rafra\u00EEchir

jdbcConnector/jdbcConnector-configuration/src/main/config/properties/org/silverpeas/jdbcConnector/multilang/jdbcConnector_de.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,5 @@ dataSourceField=Datenquelle
8686
parametresConnectionIncorrects=schlechte Verbindungseinstellungen
8787
erreurRequeteIncorrect=die SQL-Abfrage ist falsch
8888
critereSupprimes=Die Kriterien wurden entfernt
89-
sqlRequestExecutionFailure=The execution of the SQL request failed
89+
sqlRequestExecutionFailure=Die Ausführung der SQL-Anfrage ist fehlgeschlagen
90+
reloadRequest=Aufladen

jdbcConnector/jdbcConnector-configuration/src/main/config/properties/org/silverpeas/jdbcConnector/multilang/jdbcConnector_en.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,5 @@ contient = contains
8585
dataSourceField=Data Source
8686
erreurRequeteIncorrect=The SQL request is incorrect
8787
critereSupprimes=Criteria are removed
88-
sqlRequestExecutionFailure=The execution of the SQL request failed
88+
sqlRequestExecutionFailure=The execution of the SQL request failed
89+
reloadRequest=Reload

jdbcConnector/jdbcConnector-configuration/src/main/config/properties/org/silverpeas/jdbcConnector/multilang/jdbcConnector_fr.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,5 @@ dataSourceField=Source de donn\u00E9es
8585
parametresConnectionIncorrects=mauvais param\u00E8tres de connexions
8686
erreurRequeteIncorrect=La requ\u00E8te SQL est incorrecte
8787
critereSupprimes=Les crit\u00E8res ont \u00E9t\u00E9 enlev\u00E9s
88-
sqlRequestExecutionFailure=L'ex\u00E9cution de la requ\u00E8te SQL a \u00E9chou\u00E9
88+
sqlRequestExecutionFailure=L'ex\u00E9cution de la requ\u00E8te SQL a \u00E9chou\u00E9
89+
reloadRequest=Rafra\u00EEchir

jdbcConnector/jdbcConnector-library/src/main/java/org/silverpeas/components/jdbcconnector/service/JdbcRequester.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525
package org.silverpeas.components.jdbcconnector.service;
2626

2727
import org.silverpeas.components.jdbcconnector.model.DataSourceConnectionInfo;
28+
import org.silverpeas.core.util.StringUtil;
2829

2930
import java.sql.Connection;
3031
import java.sql.DatabaseMetaData;
3132
import java.sql.PreparedStatement;
3233
import java.sql.ResultSet;
3334
import java.sql.SQLException;
3435
import java.util.ArrayList;
36+
import java.util.Collections;
3537
import java.util.List;
3638

3739
/**
@@ -115,6 +117,16 @@ public boolean isDataSourceDefined() {
115117
return currentConnectionInfo.isDefined();
116118
}
117119

120+
/**
121+
* Is there is any default SQL request set with this requester? A request is set when there is a
122+
* {@link DataSourceConnectionInfo} instance defined for the component instance to which this
123+
* request is related and a SQL query has been defined for that connection information.
124+
* @return true if there is a SQL query defined for the current underlying connection information.
125+
*/
126+
public boolean isSQLRequestDefined() {
127+
return StringUtil.isNotDefined(currentConnectionInfo.getSqlRequest());
128+
}
129+
118130
/**
119131
* Gets all the tables in the data source (only public tables and views are fetched). If an error
120132
* occurs while requesting the data source, a {@link JdbcConnectorRuntimeException} is thrown.
@@ -158,12 +170,18 @@ public List<String> getColumnNames(final String tableName) {
158170
/**
159171
* Requests the data source by using the SQL query that was set with the current underlying
160172
* {@link DataSourceConnectionInfo} instance. A {@link JdbcConnectorRuntimeException} is thrown
161-
* if the data returned by the requesting fails to be read.
162-
* @return a list of rows, each of them represented by a {@link TableRow} instance.
173+
* if the data returned by the requesting fails to be read. If no request was set, then nothing
174+
* is requested and hence nothing is returned.
175+
* @return a list of rows, each of them represented by a {@link TableRow} instance. If no SQL
176+
* query is defined with the underlying {@link DataSourceConnectionInfo} instance, then an empty
177+
* list is returned.
163178
* @throws JdbcConnectorException if either no connection can be established or the requesting
164179
* failed.
165180
*/
166181
public List<TableRow> request() throws JdbcConnectorException {
182+
if (isSQLRequestDefined()) {
183+
return Collections.emptyList();
184+
}
167185
return request(currentConnectionInfo.getSqlRequest());
168186
}
169187

jdbcConnector/jdbcConnector-library/src/main/java/org/silverpeas/components/jdbcconnector/service/TableFieldValue.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@
2424

2525
package org.silverpeas.components.jdbcconnector.service;
2626

27-
import java.lang.reflect.InvocationTargetException;
28-
import java.lang.reflect.Method;
2927
import java.util.Objects;
3028

3129
/**
32-
* The value of a field in a table row.
30+
* The value of a field in a table row. Such instance is used instead of a given true field value
31+
* when that field value doesn't satisfy the {@link Comparable} interface.
3332
* @author mmoquillon
3433
*/
3534
public class TableFieldValue implements Comparable<TableFieldValue> {
@@ -40,6 +39,16 @@ public class TableFieldValue implements Comparable<TableFieldValue> {
4039
this.value = value;
4140
}
4241

42+
/**
43+
* Constructs a {@link TableFieldValue} instance from the specified {@link String} encoded field
44+
* value.
45+
* @param value the value from which a {@link TableFieldValue} has to be built.
46+
* @return a {@link TableFieldValue} instance wrapping the specified {@link String} value.
47+
*/
48+
public static TableFieldValue valueOf(final String value) {
49+
return new TableFieldValue(value);
50+
}
51+
4352
@Override
4453
public boolean equals(final Object o) {
4554
if (this == o) {
@@ -57,6 +66,15 @@ public int hashCode() {
5766
return Objects.hash(value);
5867
}
5968

69+
/**
70+
* Compares this {@link TableFieldValue} with the specified one. The comparing is actually done
71+
* on the wrapped values themselves. If the wrapped values satisfy the {@link Comparable}
72+
* interface then the {@link Comparable#compareTo(Object)} method is used, otherwise both of them
73+
* are converted in {@link String} objects and these {@link String} instances are then compared
74+
* between themselves.
75+
* @param o another {@link TableFieldValue} with which this one is compared.
76+
* @return the comparing distance between the two {@link TableFieldValue} instances.
77+
*/
6078
@Override
6179
public int compareTo(final TableFieldValue o) {
6280
if (o.value == null && value == null) {
@@ -65,20 +83,14 @@ public int compareTo(final TableFieldValue o) {
6583
return 1;
6684
} else if (value == null) {
6785
return -1;
68-
} else if (o.value.getClass().equals(value.getClass())) {
69-
try {
70-
Method m = o.getClass().getMethod("compareTo", o.getClass());
71-
return (Integer) m.invoke(value, o.value);
72-
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
73-
return toString().compareTo(o.toString());
74-
}
86+
} else if (value instanceof Comparable) {
87+
return ((Comparable) value).compareTo(o.value);
7588
}
76-
throw new IllegalArgumentException("The table field value isn't of the same type than this");
89+
return toString().compareTo(o.toString());
7790
}
7891

7992
@Override
8093
public String toString() {
8194
return value == null ? null : value.toString();
8295
}
8396
}
84-

jdbcConnector/jdbcConnector-library/src/main/java/org/silverpeas/components/jdbcconnector/service/TableRow.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
*/
4242
public class TableRow {
4343

44-
private final Map<String, Object> fields = new LinkedHashMap<>();
44+
private final Map<String, Comparable> fields = new LinkedHashMap<>();
4545

4646
/**
4747
* Constructs a {@link TableRow} from the specified {@link ResultSet}.
@@ -51,7 +51,16 @@ public class TableRow {
5151
try {
5252
ResultSetMetaData rsMetaData = rs.getMetaData();
5353
for (int i = 1; i <= rsMetaData.getColumnCount(); i++) {
54-
this.fields.put(rsMetaData.getColumnName(i), rs.getObject(i));
54+
Object value = rs.getObject(i);
55+
Comparable valueToStore;
56+
if (value == null) {
57+
valueToStore = null;
58+
} else if (value instanceof Comparable) {
59+
valueToStore = (Comparable) value;
60+
} else {
61+
valueToStore = new TableFieldValue(value);
62+
}
63+
this.fields.put(rsMetaData.getColumnName(i), valueToStore);
5564
}
5665
} catch (SQLException e) {
5766
throw new JdbcConnectorRuntimeException(e);
@@ -79,7 +88,7 @@ public Map<String, Object> getFields() {
7988
* @param field the name of the field.
8089
* @return the value of the asked field.
8190
*/
82-
public Object getFieldValue(final String field) {
91+
public Comparable getFieldValue(final String field) {
8392
return fields.get(field);
8493
}
8594
}

jdbcConnector/jdbcConnector-library/src/main/java/org/silverpeas/components/jdbcconnector/service/comparators/AbstractFieldComparatorBuilder.java

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@
2424

2525
package org.silverpeas.components.jdbcconnector.service.comparators;
2626

27-
import java.text.MessageFormat;
27+
import java.util.Objects;
2828

2929
/**
3030
* The equality comparator.
3131
* @author mmoquillon
3232
*/
33-
public class EqualityBuilder extends AbstractFieldComparatorBuilder {
34-
35-
private static final MessageFormat EQUALITY = new MessageFormat("{0} = {1}");
33+
public class Equality implements FieldValueComparator {
3634

3735
@Override
38-
protected MessageFormat getFormatter() {
39-
return EQUALITY;
36+
public <T extends Comparable<T>> boolean compare(final T left, final T right) {
37+
Objects.requireNonNull(left);
38+
Objects.requireNonNull(right);
39+
return left.compareTo(right) == 0;
4040
}
4141
}
4242

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@
2424

2525
package org.silverpeas.components.jdbcconnector.service.comparators;
2626

27-
import org.silverpeas.components.jdbcconnector.service.TableRow;
28-
2927
/**
30-
* A comparator of a field in a table row with a given value.
28+
* A comparator of comparable values. Such comparator is a comparing predicate between two
29+
* comparable objects.
3130
* @author mmoquillon
3231
*/
33-
public interface FieldComparatorBuilder {
32+
@FunctionalInterface
33+
public interface FieldValueComparator {
3434

3535
/**
36-
* Builds the comparator that compares the specified field of a table row with the specified
37-
* value.
38-
* @param fieldName the name of a field of a {@link TableRow} to compare.
39-
* @param value the value with which the field has to be compared.
40-
* @return the comparing statement according to the comparator this builder builds.
36+
* Compares the left value with the right one. The two specified values must be comparable and
37+
* instances of a same class.
38+
* @param left the left value. Must be non null.
39+
* @param right the right value. Must be non null.
40+
* @return true if the comparing predicate between the two specified values is satisfied.
4141
*/
42-
String compare(final String fieldName, final String value, final Class<?> type);
42+
<T extends Comparable<T>> boolean compare(final T left, final T right);
4343
}
4444

0 commit comments

Comments
 (0)