/* * RDFtriple.java * * Version: $Revision: 0.1 $ * * Date: $Date: 2005/08/25 17:20:28 $ * */ package nl.leidenuniv.dspace.rdf; import java.sql.SQLException; import org.apache.log4j.Logger; import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.AuthorizeManager; import org.dspace.content.DSpaceObject; 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; /** * Class representing a Resource Description Framework triple * * @author Lucas van Schaik * @version $Revision: 0.1 $ */ public class RDFtriple { /** log4j logger */ private static Logger log = Logger.getLogger(RDFtriple.class); /** Our context */ private Context myContext; /** The row in the table representing this rdf triple */ private TableRow myRow; /** RDFtriple.ANY represents ANY type of subject or object when searching **/ public final static String ANY = null; private final static int RDFVALUE = 254; private final static int RDFRESOURCE = 255; RDFtriple(Context context, TableRow row) { myContext = context; myRow = row; myContext.cache(this, this.getID()); } public RDFtriple(Context context, Object subject, RDFpredicate predicate, Object object) throws SQLException { myContext = context; myRow = DatabaseManager.create(context, "RDFtriple"); this.setSubject(subject); this.setPredicate(predicate); this.setObject(object); context.cache(this, getID()); this.update(); } public int getID() { return myRow.getIntColumn("rdf_triple_id"); } public Object getSubject() { int anId = myRow.getIntColumn("subject_id"); int aType = myRow.getIntColumn("subject_type_id"); return retrieveObject(aType,anId); } public void setSubject(Object aSubject) { saveObject(true,aSubject); } private void saveObject(boolean isSubject, Object anObject) { int aType = -1; int anId = -1; if (anObject instanceof DSpaceObject) { aType = ((DSpaceObject)anObject).getType(); anId = ((DSpaceObject)anObject).getID(); } else { try { if (anObject instanceof String) { RDFvalue value = RDFvalue.create(myContext,(String)anObject); aType = RDFVALUE; anId = value.getID(); } else { RDFresource resource = RDFresource.create(myContext,anObject); aType = RDFRESOURCE; anId = resource.getID(); } } catch (SQLException e) { // do nothing, will go wrong eventually } } if (isSubject) { myRow.setColumn("subject_type_id", aType); myRow.setColumn("subject_id", anId); } else { myRow.setColumn("object_type_id", aType); myRow.setColumn("object_id", anId); } } public RDFpredicate getPredicate() { int anId = myRow.getIntColumn("predicate_id"); RDFpredicate aPredicate = null; try { aPredicate = RDFpredicate.find(myContext, anId); } catch (SQLException e) { // do nothing } return aPredicate; } public void setPredicate(RDFpredicate aPredicate) { int anId = aPredicate.getID(); myRow.setColumn("predicate_id", anId); } public Object getObject() { int anId = myRow.getIntColumn("object_id"); int aType = myRow.getIntColumn("object_type_id"); return retrieveObject(aType,anId); } public void setObject(Object aObject) { saveObject(false,aObject); } public static RDFtriple find(Context context, int id) throws SQLException { RDFtriple fromCache = (RDFtriple) context.fromCache(RDFtriple.class, id); if (fromCache != null) { return fromCache; } TableRow row = DatabaseManager.find(context, "RDFtriple", id); if (row == null) { if (log.isDebugEnabled()) { log.debug(LogManager.getHeader(context, "find_RDFtriple", "not_found,rdf_triple_id=" + id)); } return null; } else { if (log.isDebugEnabled()) { log.debug(LogManager.getHeader(context, "find_RDFtriple", "rdf_triple_id=" + id)); } return new RDFtriple(context,row); } } public static RDFtriple find(Context context, DSpaceObject subject, RDFpredicate predicate, Class object) throws SQLException { return find(context,(Object)subject,predicate,(Object)object); } public static RDFtriple find(Context context, Object subject, RDFpredicate predicate, Class object) throws SQLException { return find(context,subject,predicate,(Object)object); } public static RDFtriple find(Context context, DSpaceObject subject, RDFpredicate predicate, DSpaceObject object) throws SQLException { return find(context,(Object)subject,predicate,(Object)object); } public static RDFtriple find(Context context, Class subject, RDFpredicate predicate, DSpaceObject object) throws SQLException { return find(context,(Object)subject,predicate,(Object)object); } public static RDFtriple find(Context context, Class subject, RDFpredicate predicate, Class object) throws SQLException { return find(context,(Object)subject,predicate,(Object)object); } public static RDFtriple find(Context context, Object subject, RDFpredicate predicate, Object object) throws SQLException { RDFtriple[] results = RDFtriple.findAll(context,subject,predicate,object); if (results.length == 0) { return null; } else { return (RDFtriple)results[0]; } } public static RDFtriple[] findAll(Context context, Object subject, RDFpredicate predicate, Object object) throws SQLException { int subjectId = -1; int subjectType = -1; int objectId = -1; int objectType = -1; int predicateId = -1; RDFtriple[] result = {}; if (subject != null) { subjectId = retrieveID(context,subject); subjectType = retrieveType(context,subject); if (subjectType < 0) { return result; } } if (predicate != null) { predicateId = predicate.getID(); } if (object != null) { objectId = retrieveID(context,object); objectType = retrieveType(context,object); if (objectType < 0) { return result; } } return RDFtriple.findAll(context,subjectType,subjectId,predicateId,objectType,objectId); } private static RDFtriple[] findAll(Context context, int subjectType, int subjectId, int predicateId, int objectType, int objectId) throws SQLException { String query = "SELECT * from RDFtriple WHERE " + "predicate_id = predicate_id"; if (subjectType >= 0) { query += " AND subject_type_id = "+subjectType; } if (subjectId > 0) { query += " AND subject_id = "+subjectId; } if (predicateId > 0) { query += " AND predicate_id = "+predicateId; } if (objectType >= 0) { query += " AND object_type_id = "+objectType; } if (objectId > 0) { query += " AND object_id = "+objectId; } TableRowIterator tri = DatabaseManager.query(context,"RDFtriple",query); List triples = new ArrayList(); while (tri.hasNext()) { TableRow r = (TableRow) tri.next(); RDFtriple fromCache = (RDFtriple) context.fromCache( RDFtriple.class, r.getIntColumn("rdf_triple_id")); if (fromCache != null) { triples.add(fromCache); } else { triples.add(new RDFtriple(context, r)); } } tri.close(); return (RDFtriple[])triples.toArray(new RDFtriple[triples.size()]); } private static int retrieveType(Context context, Object anObject) { int aType = -1; if (anObject != null) { if (anObject instanceof DSpaceObject) { aType = ((DSpaceObject)anObject).getType(); } else if (anObject instanceof Class) { Class aClass = (Class)anObject; String aName = aClass.getName().toUpperCase(); aName = aName.replaceAll("^(?:[A-Z0-9]+\\.)+", ""); if (aName.equals("STRING")) { aType = RDFVALUE; } else { aType = Constants.getTypeID(aName); } } else { int anId = retrieveID(context,anObject); if (anId > 0) { // only get the type if the associated RDFvalue or RDFresource exists if (anObject instanceof String) { aType = RDFVALUE; } else { aType = RDFRESOURCE; } } } } return aType; } private static int retrieveID(Context context, Object anObject) { int anId = -1; if (anObject != null) { if (anObject instanceof DSpaceObject) { anId = ((DSpaceObject)anObject).getID(); } else if (anObject instanceof Class) { // anId = -1 still true } else { try { if (anObject instanceof String) { RDFvalue value = RDFvalue.find(context,(String)anObject); if (value != null) { anId = value.getID(); } } else { RDFresource resource = RDFresource.find(context,anObject); if (resource != null) { anId = resource.getID(); } } } catch (SQLException e) { anId = -2; } } } return anId; } private Object retrieveObject(int aType, int anId) { Object anObject = null; try { switch(aType) { case Constants.BITSTREAM: anObject = Bitstream.find(myContext, anId); break; case Constants.BUNDLE: anObject = Bundle.find(myContext, anId); break; case Constants.ITEM: anObject = Item.find(myContext, anId); break; case Constants.COLLECTION: anObject = Collection.find(myContext, anId); break; case Constants.COMMUNITY: anObject = Community.find(myContext, anId); break; case Constants.SITE: // not implemented, but found in constants... break; case Constants.GROUP: anObject = Group.find(myContext, anId); break; case Constants.EPERSON: anObject = EPerson.find(myContext, anId); break; case RDFVALUE: RDFvalue v = RDFvalue.find(myContext, anId); anObject = v.getValue(); break; case RDFRESOURCE: RDFresource r = RDFresource.find(myContext, anId); anObject = r.getResource(); break; } } catch(SQLException e) { // do nothing... } return anObject; } public void update() throws SQLException { // FIXME: Check authorisation DatabaseManager.update(myContext, myRow); } public void delete() throws SQLException { // FIXME: Check authorisation // Remove from cache myContext.removeCached(this, getID()); // Remove ourself DatabaseManager.delete(myContext, myRow); log.info(LogManager.getHeader(myContext, "delete_rdf_triple", "rdf_triple_id=" + getID())); } public static void main(String[] argv) { try { Context context = new Context(); RDFpredicate predicate; context.complete(); } catch (Exception e) { System.out.print("EXCEPTION: "+e); e.printStackTrace(); } } }