Fix problem mapping distinct metadata values to items

git-svn-id: http://scm.dspace.org/svn/repo/trunk@2634 9c30dcfa-912a-0410-8fc2-9e0234be79fd
This commit is contained in:
Graham Triggs
2008-02-11 12:23:52 +00:00
parent 800e2142c9
commit debbfcc0c4
4 changed files with 112 additions and 86 deletions

View File

@@ -177,18 +177,6 @@ public interface BrowseCreateDAO
*/
public int insertDistinctRecord(String table, String value, String sortValue) throws BrowseException;
/**
* Create a mapping between an item id and a distinct metadata field such as an author,
* who can appear in multiple items. To get the id of the distinct record you should
* use either getDistinctID or insertDistinctRecord as defined above.
*
* @param table the mapping table
* @param itemID the item id
* @param distinctID the id of the distinct record
* @throws BrowseException
*/
public void createDistinctMapping(String table, int itemID, int distinctID) throws BrowseException;
/**
* Update a mapping between an item id and a distinct metadata field such as an author,
* who can appear in multiple items. To get the id of the distinct record you should
@@ -196,10 +184,10 @@ public interface BrowseCreateDAO
*
* @param table the mapping table
* @param itemID the item id
* @param distinctID the id of the distinct record
* @param distinctIDs the id of the distinct record
* @throws BrowseException
*/
public boolean updateDistinctMapping(String table, int itemID, int distinctID) throws BrowseException;
public boolean updateDistinctMappings(String table, int itemID, int[] distinctIDs) throws BrowseException;
/**
* Find out of a given table exists.

View File

@@ -241,50 +241,64 @@ public class BrowseCreateDAOOracle implements BrowseCreateDAO
}
}
/* (non-Javadoc)
* @see org.dspace.browse.BrowseCreateDAO#createDistinctMapping(java.lang.String, int, int)
*/
public void createDistinctMapping(String table, int itemID, int distinctID) throws BrowseException
{
try
{
TableRow tr = DatabaseManager.create(context, table);
tr.setColumn("item_id", itemID);
tr.setColumn("distinct_id", distinctID);
DatabaseManager.update(context, tr);
}
catch (SQLException e)
{
log.error("caught exception: ", e);
String msg = "problem creating distinct mapping: table=" + table + ",item-id=" + itemID + ",distinct_id=" + distinctID;
throw new BrowseException(msg, e);
}
}
/* (non-Javadoc)
* @see org.dspace.browse.BrowseCreateDAO#updateDistinctMapping(java.lang.String, int, int)
*/
public boolean updateDistinctMapping(String table, int itemID, int distinctID) throws BrowseException
public boolean updateDistinctMappings(String table, int itemID, int[] distinctIDs) throws BrowseException
{
try
{
TableRow tr = DatabaseManager.findByUnique(context, table, "item_id", itemID);
if (tr != null)
// Remove (set to -1) any duplicate distinctIDs
for (int i = 0; i < distinctIDs.length; i++)
{
if (distinctID != tr.getIntColumn("distinct_id"))
{
tr.setColumn("distinct_id", distinctID);
DatabaseManager.update(context, tr);
if (!isFirstOccurrence(distinctIDs, i))
distinctIDs[i] = -1;
}
return true;
// Find all existing mappings for this item
TableRowIterator tri = DatabaseManager.queryTable(context, table, "SELECT * FROM " + table + " WHERE item_id=?", itemID);
if (tri != null)
{
while (tri.hasNext())
{
TableRow tr = tri.next();
// Check the item mappings to see if it contains this mapping
boolean itemIsMapped = false;
int trDistinctID = tr.getIntColumn("distinct_id");
for (int i = 0; i < distinctIDs.length; i++)
{
// Found this mapping
if (distinctIDs[i] == trDistinctID)
{
// Flag it, and remove (-1) from the item mappings
itemIsMapped = true;
distinctIDs[i] = -1;
}
}
// The item is no longer mapped to this community, so remove the database record
if (!itemIsMapped)
DatabaseManager.delete(context, tr);
}
}
// Any remaining mappings need to be added to the database
for (int i = 0; i < distinctIDs.length; i++)
{
if (distinctIDs[i] > -1)
{
TableRow row = DatabaseManager.create(context, table);
row.setColumn("item_id", itemID);
row.setColumn("distinct_id", distinctIDs[i]);
DatabaseManager.update(context, row);
}
}
}
catch (SQLException e)
{
log.error("caught exception: ", e);
String msg = "problem updating distinct mapping: table=" + table + ",item-id=" + itemID + ",distinct_id=" + distinctID;
String msg = "problem updating distinct mappings: table=" + table + ",item-id=" + itemID;
throw new BrowseException(msg, e);
}

View File

@@ -238,50 +238,64 @@ public class BrowseCreateDAOPostgres implements BrowseCreateDAO
}
}
/* (non-Javadoc)
* @see org.dspace.browse.BrowseCreateDAO#createDistinctMapping(java.lang.String, int, int)
*/
public void createDistinctMapping(String table, int itemID, int distinctID)
throws BrowseException
{
try
{
TableRow tr = DatabaseManager.create(context, table);
tr.setColumn("item_id", itemID);
tr.setColumn("distinct_id", distinctID);
DatabaseManager.update(context, tr);
}
catch (SQLException e)
{
log.error("caught exception: ", e);
String msg = "problem creating distinct mapping: table=" + table + ",item-id=" + itemID + ",distinct_id=" + distinctID;
throw new BrowseException(msg, e);
}
}
/* (non-Javadoc)
* @see org.dspace.browse.BrowseCreateDAO#updateDistinctMapping(java.lang.String, int, int)
*/
public boolean updateDistinctMapping(String table, int itemID, int distinctID) throws BrowseException
public boolean updateDistinctMappings(String table, int itemID, int[] distinctIDs) throws BrowseException
{
try
{
TableRow tr = DatabaseManager.findByUnique(context, table, "item_id", itemID);
if (tr != null)
// Remove (set to -1) any duplicate distinctIDs
for (int i = 0; i < distinctIDs.length; i++)
{
if (distinctID != tr.getIntColumn("distinct_id"))
{
tr.setColumn("distinct_id", distinctID);
DatabaseManager.update(context, tr);
if (!isFirstOccurrence(distinctIDs, i))
distinctIDs[i] = -1;
}
return true;
// Find all existing mappings for this item
TableRowIterator tri = DatabaseManager.queryTable(context, table, "SELECT * FROM " + table + " WHERE item_id=?", itemID);
if (tri != null)
{
while (tri.hasNext())
{
TableRow tr = tri.next();
// Check the item mappings to see if it contains this mapping
boolean itemIsMapped = false;
int trDistinctID = tr.getIntColumn("distinct_id");
for (int i = 0; i < distinctIDs.length; i++)
{
// Found this mapping
if (distinctIDs[i] == trDistinctID)
{
// Flag it, and remove (-1) from the item mappings
itemIsMapped = true;
distinctIDs[i] = -1;
}
}
// The item is no longer mapped to this community, so remove the database record
if (!itemIsMapped)
DatabaseManager.delete(context, tr);
}
}
// Any remaining mappings need to be added to the database
for (int i = 0; i < distinctIDs.length; i++)
{
if (distinctIDs[i] > -1)
{
TableRow row = DatabaseManager.create(context, table);
row.setColumn("item_id", itemID);
row.setColumn("distinct_id", distinctIDs[i]);
DatabaseManager.update(context, row);
}
}
}
catch (SQLException e)
{
log.error("caught exception: ", e);
String msg = "problem updating distinct mapping: table=" + table + ",item-id=" + itemID + ",distinct_id=" + distinctID;
String msg = "problem updating distinct mappings: table=" + table + ",item-id=" + itemID;
throw new BrowseException(msg, e);
}

View File

@@ -40,8 +40,10 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import org.apache.commons.cli.CommandLine;
@@ -415,7 +417,7 @@ public class IndexBrowse
if (bis[i].isMetadataIndex())
{
boolean itemMapped = false;
Set<Integer> distIDSet = new HashSet<Integer>();
// now index the new details - but only if it's archived and not withdrawn
if (item.isArchived() && !item.isWithdrawn())
@@ -443,23 +445,31 @@ public class IndexBrowse
{
// get the normalised version of the value
String nVal = OrderFormat.makeSortString(values[x].value, values[x].language, bis[i].getDataType());
int distinctID = dao.getDistinctID(bis[i].getDistinctTableName(), values[x].value, nVal);
// Update the existing mapping, or create a new one if it doesn't exist
if (!dao.updateDistinctMapping(bis[i].getMapTableName(), item.getID(), distinctID))
dao.createDistinctMapping(bis[i].getMapTableName(), item.getID(), distinctID);
itemMapped = true;
distIDSet.add(dao.getDistinctID(bis[i].getDistinctTableName(), values[x].value, nVal));
}
}
}
}
}
// Do we have any mappings?
if (distIDSet.isEmpty())
{
// remove any old mappings
if (!itemMapped)
removeIndex(item.getID(), bis[i].getMapTableName());
}
else
{
// Update the existing mappings
int[] distIDarr = new int[distIDSet.size()];
int didx = 0;
for (Integer distID : distIDSet)
{
distIDarr[didx++] = distID;
}
dao.updateDistinctMappings(bis[i].getMapTableName(), item.getID(), distIDarr);
}
}
}
}
catch (SQLException e)