/*
* 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.Hashtable;
import pdfdb.structure.Index;
import pdfdb.structure.IndexList;
import pdfdb.structure.Region;
import pdfdb.structure.RegionType;
/** Provides limited access to region editting part of the data layer.
* @author ug22cmg */
public class RegionProvider
{
/** Adds a region to the database.
* @param filePath The file to add the region to.
* @param regionType The region type.
* @throws java.sql.SQLException If an error occurs. */
public static void addRegion(String filePath,
RegionType regionType) throws SQLException
{
Connection conn = null;
try
{
conn = DatabaseConnection.getNewConnection();
addRegion(conn, filePath, regionType);
}
finally
{
DatabaseConnection.close(conn);
}
}
/** Adds a region to the database using the specified connection.
* @param conn The connection to use.
* @param filePath The file path to add the region to.
* @param regionType The region type.
* @throws java.sql.SQLException If an error occurs. */
private static void addRegion(Connection conn, String filePath,
RegionType regionType) throws SQLException
{
String sql = "INSERT INTO Regions(RegionType, FileId) VALUES(?, ?)";
PreparedStatement statement = null;
try
{
statement = conn.prepareStatement(sql);
statement.setInt(1, regionType.ordinal());
statement.setString(2, filePath);
statement.executeUpdate();
}
catch (Exception se)
{
System.err.println(se.getMessage());
}
finally
{
DatabaseConnection.close(statement);
}
}
/** Gets the regions with the specified file path.
* @param filePath The file path to get regions for.
* @return The regions.
* @throws java.sql.SQLException If an error occurs. */
public static Region[] getRegions(String filePath)
throws SQLException
{
Connection conn = null;
try
{
conn = DatabaseConnection.getNewConnection();
return getRegions(conn, filePath);
}
finally
{
DatabaseConnection.close(conn);
}
}
/** Gets the regions for the specified file.
* @param conn The connection to use.
* @param filePath The file path to get the regions for.
* @return The region array or null.
* @throws java.sql.SQLException If an error occurs. */
private static Region[] getRegions(Connection conn,
String filePath)
throws SQLException
{
String sql = "SELECT RegionId, RegionType FROM Regions WHERE FileId = ?";
ResultSet rs = null;
RegionType type = null;
Region[] rtn = null;
PreparedStatement statement = null;
int i = 0;
try
{
statement =
conn.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
statement.setString(1, filePath);
rs = statement.executeQuery();
rs.setFetchDirection(ResultSet.TYPE_SCROLL_INSENSITIVE);
rs.afterLast();
rs.previous();
rtn = new Region[rs.getRow()];
rs.beforeFirst();
while (rs.next())
{
Region region = null;
Index[] indexes = null;
int regionType = rs.getInt("RegionType");
for (RegionType rt : RegionType.values())
{
if (rt.ordinal() == regionType) type = rt;
}
region = new Region(rs.getInt("RegionId"), type);
indexes = IndexProvider.getIndexesForRegion(conn, region);
if (type == null) throw new IllegalArgumentException(
"Region type is null.");
for (Index index : indexes)
{
addIndex(region.getIndexes(), index);
}
rtn[i] = region;
i++;
}
return rtn;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
finally
{
DatabaseConnection.close(statement, rs);
}
}
/** Adds an index to an index table.
* @param table The index table.
* @param i The index to add. */
private static void addIndex(Hashtable<Integer, IndexList> table, Index i)
{
int wid = i.getWord().getWordId();
if (table.get(wid) == null) table.put(wid, new IndexList());
table.get(wid).add(i);
}
}