/* * RDFmanager.java * * Version: $Revision: 0.1 $ * * Date: $Date: 2008/02/13 11:29:28 $ * */ package nl.leidenuniv.dspace.rdf; import java.sql.SQLException; //import org.dspace.authorize.AuthorizeException; //import org.dspace.authorize.AuthorizeManager; //import org.dspace.content.DSpaceObject; import org.dspace.content.DCValue; import org.dspace.core.Context; //import org.dspace.core.LogManager; import org.dspace.storage.rdbms.DatabaseManager; import org.dspace.storage.rdbms.TableRow; import org.dspace.storage.rdbms.TableRowIterator; //import org.dspace.core.Constants; import nl.leidenuniv.dspace.rdf.RDFpredicate; import nl.leidenuniv.dspace.rdf.RDFvalue; import nl.leidenuniv.dspace.rdf.RDFresource; //import org.dspace.content.Bitstream; //import org.dspace.content.Bundle; //import org.dspace.content.Collection; //import org.dspace.content.Community; import org.dspace.content.Item; //import org.dspace.eperson.EPerson; //import org.dspace.eperson.Group; //import java.util.ArrayList; //import java.util.List; /** * Manager for Resource Description Framework triples * * @author Lucas van Schaik * @version $Revision: 0.1 $ */ public class RDFmanager { public static boolean addTriple(Context context, Item item, String subject_type, String subject_value, String predicateString, String object_type, String object_value) throws SQLException { if (subject_value == null || object_value == null) { return false; } Object subject = findResource(context, item, subject_type, subject_value); Object object = findResource(context, item, object_type, object_value); if (object == null || subject == null) { return false; } RDFpredicate predicate = RDFpredicate.find(context,predicateString); if (predicate == null) { predicate = new RDFpredicate(context,predicateString); } RDFtriple triple = RDFtriple.find(context,subject,predicate,null); if (triple != null) { if (object != null) { Object obj = triple.getObject(); if ( ! object.equals(obj)) { triple.setObject(object); triple.update(); } } else { // komt hier nooit triple.delete(); return false; } } else { if (object!= null) { triple = new RDFtriple(context,subject,predicate,object); } } return true; } private static Object findResource(Context context, Item item, String type, String value) { Object object = null; if (type.startsWith("dc:")) { String[] parts = type.split("[:.]"); String element = parts[1]; String qualifier = null; if (parts.length > 2) { qualifier = parts[2]; } DCValue[] dcs = item.getDC(element,qualifier,Item.ANY); for (int i=0; i 0) { object = value; } } else if (type.equals("item")) { object = item; } return object; } public static void main(String[] argv) { if (argv.length != 5) { System.err.println("usage: RDFmanager subject_type subject_value predicate_string object_type object_value\n example: RDFmanager dc:contributor.author \"Piet, J.E.\" hasDAI string \"123459657\"\n"); return; } String subject_type = argv[0]; String subject_value = argv[1]; String predicateString = argv[2]; String object_type = argv[3]; String object_value = argv[4]; try { Context context = new Context(); boolean done = false; if (subject_type.startsWith("dc:")) { String[] parts = subject_type.split("[:.]"); String element = parts[1]; String sql = "SELECT v.item_id as itemid FROM dcvalue v, dctyperegistry t WHERE " + "v.dc_type_id = t.dc_type_id and " + "v.text_value = '"+subject_value+"' and " + "t.element = '"+element+"'"; if (parts.length > 2) { if (!parts[2].equals("*")) { sql += " and t.qualifier = '"+parts[2]+"'"; } } else { sql += " and t.qualifier is null"; } TableRowIterator tri = null; try { tri = DatabaseManager.query(context,sql); while (tri.hasNext()) { TableRow r = (TableRow) tri.next(); int itemid = r.getIntColumn("itemid"); Item item = Item.find(context,itemid); if (item.isArchived()) { System.out.println("Adding triple to item "+item.getHandle()+" with subject "+subject_type+" "+subject_value+" "+predicateString+" "+object_type+" "+object_value); done = RDFmanager.addTriple(context, item, subject_type, subject_value, predicateString, object_type, object_value); if (!done) { System.err.println("FAILED to add triple to item "+item.getHandle()+" with subject "+subject_type+" "+subject_value+" "+predicateString+" "+object_type+" "+object_value); } } } } finally { if (tri != null) { tri.close(); } } } else { System.err.println("Only subject_type dc:... implemented... bailing....\n"); } context.complete(); } catch (Exception e) { System.out.print("EXCEPTION: "+e); e.printStackTrace(); } } }