[Corpusreader-svn] SF.net SVN: corpusreader:[225] trunk/corpusreader/src/main
Status: Alpha
Brought to you by:
sylvainloiseau
|
From: <syl...@us...> - 2009-09-12 18:50:06
|
Revision: 225
http://corpusreader.svn.sourceforge.net/corpusreader/?rev=225&view=rev
Author: sylvainloiseau
Date: 2009-09-12 18:49:58 +0000 (Sat, 12 Sep 2009)
Log Message:
-----------
Adding option to UpdateDatabase filter
Modified Paths:
--------------
trunk/corpusreader/src/main/java/tei/cr/filters/UpdateDatabase.java
trunk/corpusreader/src/main/java/tei/cr/querydoc/FilterArguments.java
trunk/corpusreader/src/main/resources/schema/filters/UpdateDatabase.rnc
Modified: trunk/corpusreader/src/main/java/tei/cr/filters/UpdateDatabase.java
===================================================================
--- trunk/corpusreader/src/main/java/tei/cr/filters/UpdateDatabase.java 2009-09-12 17:58:04 UTC (rev 224)
+++ trunk/corpusreader/src/main/java/tei/cr/filters/UpdateDatabase.java 2009-09-12 18:49:58 UTC (rev 225)
@@ -45,8 +45,13 @@
private ExtractLocation[] locators;
private String[] fieldTypes;
+ private List<Set<String>> fieldSet;
+ /** Should the statement be skipped if it entails duplicate value for this field in the db */
private boolean[] fieldUnique;
- private List<Set<String>> fieldSet;
+ /** Should the statement be skipped when the value is empty */
+ private boolean[] fieldNonEmpty;
+ /** Should the value be trimed when fetched from TEILocator */
+ private boolean[] fieldTrim;
private Connection connexion;
private int numberOfRecordsInserted;
@@ -75,17 +80,23 @@
String fieldType = fA.getText(element, FilterArguments.UPDATE_DATABASE_FIELD_TYPE);
String locatorName = fA.getText(element, FilterArguments.UPDATE_DATABASE_FIELD_FILTERNAME);
String uniquestr = fA.getText(element, FilterArguments.UPDATE_DATABASE_FIELD_UNIQUE);
- boolean unique = false;
- if (uniquestr != null && !uniquestr.equals("")) {
- if (uniquestr.equals("true")) unique = true;
- }
+ if (uniquestr != null && uniquestr.equals("true"))
+ setUnique(i, true);
+ String non_empty_str = fA.getText(element, FilterArguments.UPDATE_DATABASE_FIELD_NON_EMPTY);
+ if (non_empty_str != null && non_empty_str.equals("true"))
+ setNonEmpty(i, true);
+
+ String trim_str = fA.getText(element, FilterArguments.UPDATE_DATABASE_FIELD_TRIM);
+ if (trim_str != null && trim_str.equals("true"))
+ setTrim(i, true);
+
ExtractLocation locator = (ExtractLocation) nH.get(locatorName);
if (locator == null) {
throw new WrongArgsException("No filter named \"" + locatorName + "\"found in the pipeline");
}
try {
- setField(i, fieldType, locator, unique);
+ setField(i, fieldType, locator);
} catch (FilterException e) {
throw new WrongArgsException("Unable to set a field: " + e.getMessage(), e);
}
@@ -105,6 +116,8 @@
for (int i = 0; i < nbrOfFields; i++) {
fieldSet.add(new HashSet<String>());
}
+ fieldNonEmpty = new boolean[nbrOfFields];
+ fieldTrim = new boolean[nbrOfFields];
}
/**
@@ -119,10 +132,6 @@
* for retreive the value for this field.
*/
public void setField(int indexOfField, String fieldType, ExtractLocation locator) throws FilterException {
- setField(indexOfField, fieldType, locator, false);
- }
-
- public void setField(int indexOfField, String fieldType, ExtractLocation locator, boolean unique) throws FilterException {
if (indexOfField >= locators.length) {
throw new FilterException("Field index out of bound.");
}
@@ -137,18 +146,31 @@
}
locators[indexOfField] = locator;
fieldTypes[indexOfField] = fieldType;
- fieldUnique[indexOfField] = unique;
}
-
+
+ public void setNonEmpty(int index, boolean non_empty) {
+ fieldNonEmpty[index] = non_empty;
+ }
+
+ public void setTrim(int index, boolean trim) {
+ fieldTrim[index] = trim;
+ }
+
+ public void setUnique(int index, boolean unique) {
+ fieldUnique[index] = unique;
+ }
+
////////////////////////////////////////////////////////
/**
* Each end document is a query to the database.
*/
public void endDocument() throws SAXException {
- boolean test = shouldUpdate();
+ String[] values = fetchValues();
+ boolean test = shouldUpdate(values);
+
if (test) {
- doStatement();
+ doStatement(values);
numberOfRecordsInserted++;
if ((numberOfRecordsInserted % 1000) == 0) {
newConnection();
@@ -191,7 +213,18 @@
////////////////////////
- private void doStatement() throws FilterException {
+ private String[] fetchValues () {
+ String[] values = new String[fieldTypes.length];
+ for (int i = 0; i < values.length; i++) {
+ values[i] = locators[i].getReference();
+ if (fieldTrim[i]) {
+ values[i] = values[i].trim();
+ }
+ }
+ return values;
+ }
+
+ private void doStatement(String[] values) throws FilterException {
PreparedStatement statement;
try {
statement = connexion.prepareStatement(parameterizedQuery);
@@ -200,7 +233,7 @@
}
try {
- setParameters(statement);
+ setParameters(statement, values);
} catch (SQLException e) {
throw new FilterException("Unable to set parameters. " + e.getMessage(), e);
}
@@ -243,9 +276,9 @@
* Construct the query, calling the registered TEILocator
* for retreiving values.
*/
- private void setParameters(PreparedStatement statement) throws SQLException {
+ private void setParameters(PreparedStatement statement, String[] values) throws SQLException {
for(int i = 0; i < fieldTypes.length; i++) {
- String value = locators[i].getReference();
+ String value = values[i];
if (fieldTypes[i].equals("int")) {
statement.setInt(i+1, Integer.parseInt(value));
} else if (fieldTypes[i].equals("String")) {
@@ -259,13 +292,22 @@
* into a new query.
* @return
*/
- private boolean shouldUpdate() {
- for (int i = 0; i < fieldUnique.length; i++) {
+ private boolean shouldUpdate(String[] values) {
+ //Doing in that order, values are recorded in fieldSet only if
+ // they have realy been inserted (and not only seen) before.
+ for (int i = 0; i < values.length; i++) {
+ if (fieldNonEmpty[i]) {
+ if (values[i].length() == 0) {
+ return false;
+ }
+ }
+ }
+ for (int i = 0; i < values.length; i++) {
if (fieldUnique[i]) {
- if (fieldSet.get(i).contains(locators[i].getReference())) {
+ if (fieldSet.get(i).contains(values[i])) {
return false;
} else {
- fieldSet.get(i).add(locators[i].getReference());
+ fieldSet.get(i).add(values[i]);
}
}
}
Modified: trunk/corpusreader/src/main/java/tei/cr/querydoc/FilterArguments.java
===================================================================
--- trunk/corpusreader/src/main/java/tei/cr/querydoc/FilterArguments.java 2009-09-12 17:58:04 UTC (rev 224)
+++ trunk/corpusreader/src/main/java/tei/cr/querydoc/FilterArguments.java 2009-09-12 18:49:58 UTC (rev 225)
@@ -530,7 +530,10 @@
public final static String UPDATE_DATABASE_FIELD_TYPE = "@fieldType";
public final static String UPDATE_DATABASE_FIELD_FILTERNAME = "@filterName";
public final static String UPDATE_DATABASE_FIELD_UNIQUE = "@unique";
+ public final static String UPDATE_DATABASE_FIELD_NON_EMPTY = "@non_empty";
+ public final static String UPDATE_DATABASE_FIELD_TRIM = "@trim";
public final static String UPDATE_DATABASE_QUERY = "preparedQuery/@query";
+
// BUILD_TABLE_FROM_TEI_LOCATOR
Modified: trunk/corpusreader/src/main/resources/schema/filters/UpdateDatabase.rnc
===================================================================
--- trunk/corpusreader/src/main/resources/schema/filters/UpdateDatabase.rnc 2009-09-12 17:58:04 UTC (rev 224)
+++ trunk/corpusreader/src/main/resources/schema/filters/UpdateDatabase.rnc 2009-09-12 18:49:58 UTC (rev 225)
@@ -14,6 +14,8 @@
attribute fieldType { string },
attribute filterName { xs:IDREF },
attribute unique { ("true" | "false") }?,
+ attribute non_empty { ("true" | "false") }?,
+ attribute trim { ("true" | "false") }?,
empty
}+
},
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|