/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pdfdb.data.db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
import pdfdb.structure.Word;
/** Provides limited access to the word editting part of the data layer. Also
* includes minimal validation layer to protect the integrity and efficiency
* of the database.
* @author ug22cmg */
public class WordProvider
{
private static final Object LIST_LOCK = new Object();
private static final Set<String> ignored = new HashSet<String>();
/** Adds words to the restrictexd list. */
static
{
synchronized (LIST_LOCK)
{
ignored.add("a");
ignored.add("the");
ignored.add("an");
}
}
/** Gets the normalized form of the word.
* @param word The input string.
* @return The normalized string. */
private static String getNormalizedForm(String word)
{
String w = word.toLowerCase().trim();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < w.length(); i++)
{
char c = w.charAt(i);
if (Character.isLetterOrDigit(c) || c == '-' || c == '\'')
{
sb.append(c);
}
}
return sb.toString();
}
/** Inserts a word into the database that does not already exist.
* @param conn The connection to use.
* @param word The word to add.
* @throws java.sql.SQLException If an error occured. */
private static void createWord(Connection conn, String word) throws
SQLException
{
String sql = "INSERT INTO Words(Tag) VALUES(?)";
PreparedStatement s = null;
try
{
s = conn.prepareStatement(sql);
s.setString(1, getNormalizedForm(word));
s.executeUpdate();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
DatabaseConnection.close(s);
}
}
/** Converts a string array of word's into an array of word
* instances. Word creation is handled.
* @param input The input words.
* @return The word array. */
public static Word[] getWords(String[] input)
{
Connection conn = null;
try
{
conn = DatabaseConnection.getNewConnection();
Word[] words = new Word[input.length];
for (int i = 0; i < words.length; i++)
{
Word w = WordProvider.convertToWord(conn, input[i]);
words[i] = w;
}
return words;
}
finally
{
DatabaseConnection.close(conn);
}
}
/** Gets whether the word exists in the database.
* @param word The word to check.
* @return True if the word exists in the database. */
public static boolean exists(String word)
{
Connection conn = null;
try
{
conn = DatabaseConnection.getNewConnection();
return getExistingWord(conn, word) != null;
}
finally
{
DatabaseConnection.close(conn);
}
}
/** Converts a word in string representation to a word.
* @param conn The connection to use.
* @param wordStr The word in string representation.
* @return The word instance or null if not used. */
public static Word convertToWord(Connection conn, String wordStr)
{
if (isWordUsed(wordStr))
{
Word word = getExistingWord(conn, wordStr);
if (word == null)
{
try
{
createWord(conn, wordStr);
}
catch (SQLException se)
{
word = null;
}
word = getExistingWord(conn, wordStr);
}
return word;
}
else
{
return null;
}
}
/** Gets a word that already exists in the database.
* @param conn The connection to use.
* @param word The word in string representation.
* @return The word instance or null. */
private static Word getExistingWord(Connection conn, String word)
{
String sql = "SELECT WordId, Tag FROM Words WHERE Tag = ?";
PreparedStatement statement = null;
ResultSet rs = null;
try
{
statement = conn.prepareStatement(sql);
statement.setString(1, getNormalizedForm(word));
rs = statement.executeQuery();
if (rs.next())
{
int wordId = rs.getInt("WordId");
String tag = rs.getString("Tag");
return new Word(wordId, tag);
}
else
{
return null;
}
}
catch (Exception se)
{
return null;
}
finally
{
DatabaseConnection.close(statement, rs);
}
}
/** Gets whether the word should be added to the database.
* @param word The word to be added.
* @return True if the word can be added to the database. */
public static boolean isWordUsed(String word)
{
if (word == null || word.trim().equals(""))
{
return false;
}
else
{
synchronized (LIST_LOCK)
{
return !ignored.contains(word.toLowerCase());
}
}
}
}