/* * RDFresource.java * * Version: $Revision: 0.1 $ * * Date: $Date: 2007/11/23 12:40:00 $ * */ package nl.leidenuniv.dspace.rdf; import java.sql.SQLException; import org.apache.log4j.Logger; import org.dspace.core.Context; import org.dspace.core.LogManager; import org.dspace.content.DCValue; import org.dspace.content.Item; import org.dspace.storage.rdbms.DatabaseManager; import org.dspace.storage.rdbms.TableRow; import org.dspace.storage.rdbms.TableRowIterator; import org.dspace.handle.HandleManager; import org.dspace.core.Utils; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * Class representing a Resource Description Framework resource * * @author Lucas van Schaik * @version $Revision: 0.1 $ */ public class RDFresource { /** log4j logger */ private static Logger log = Logger.getLogger(RDFresource.class); /** Our context */ private Context myContext; /** The row in the table representing this eperson */ private TableRow myRow; RDFresource(Context context, TableRow row) { myContext = context; myRow = row; myContext.cache(this, this.getID()); } RDFresource(Context context, String uri) throws SQLException { myContext = context; myRow = DatabaseManager.create(context, "RDFresource"); this.setURI(uri); context.cache(this, getID()); this.update(); } RDFresource(Context context, Object resource) throws IOException, SQLException { String uri = RDFresource.getURIforObject(context,resource); myContext = context; myRow = DatabaseManager.create(context, "RDFresource"); this.setURI(uri); context.cache(this, getID()); this.update(); } public static RDFresource create(Context context, Object resource) throws IOException, SQLException { RDFresource result = RDFresource.find(context, resource); if (result == null) { result = new RDFresource(context, resource); } return result; } public int getID() { return myRow.getIntColumn("rdf_resource_id"); } public String getURI() { return myRow.getStringColumn("URI"); } public void setURI(String aString) { myRow.setColumn("URI", aString); } public Object getResource() { // dit moet anders... URI 'uitpakken' en herleiden naar object Object result = null; try { result = RDFresource.find(myContext,getURI()); } catch (SQLException e) { // do nothing } return result; } public static RDFresource find(Context context, int id) throws SQLException { RDFresource fromCache = (RDFresource) context.fromCache(RDFresource.class, id); if (fromCache != null) { return fromCache; } TableRow row = DatabaseManager.find(context, "RDFresource", id); if (row == null) { if (log.isDebugEnabled()) { log.debug(LogManager.getHeader(context, "find_RDFresource", "not_found,rdf_resource_id=" + id)); } return null; } else { if (log.isDebugEnabled()) { log.debug(LogManager.getHeader(context, "find_RDFresource", "rdf_resource_id=" + id)); } return new RDFresource(context,row); } } public static RDFresource find(Context context, Object anObject) throws IOException, SQLException { String uri = RDFresource.getURIforObject(context,anObject); return find(context,uri); } public static RDFresource find(Context context, String anURI) throws SQLException { String query = "SELECT * from RDFresource where URI like '"+anURI+"'"; TableRow row = DatabaseManager.querySingle(context, "RDFresource", query); if (row == null) { return null; } else { // First check the cache RDFresource fromCache = (RDFresource) context.fromCache(RDFresource.class, row.getIntColumn("rdf_resource_id")); if (fromCache != null) { return fromCache; } else { return new RDFresource(context, row); } } } public static RDFresource[] findAll(Context context, String partOfURI) throws SQLException { String query = "SELECT * from RDFresource"; if (partOfURI != null && partOfURI.length() > 0) { query += " WHERE URI like '%"+partOfURI+"%'"; } TableRowIterator tri = DatabaseManager.query(context,"RDFresource",query); List resource = new ArrayList(); while (tri.hasNext()) { TableRow r = (TableRow) tri.next(); RDFresource fromCache = (RDFresource) context.fromCache( RDFresource.class, r.getIntColumn("rdf_resource_id")); if (fromCache != null) { resource.add(fromCache); } else { resource.add(new RDFresource(context, r)); } } tri.close(); return (RDFresource[])resource.toArray(new RDFresource[resource.size()]); } public static RDFresource[] findAll(Context context) throws SQLException { return findAll(context, null); } 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_resource", "rdf_resource_id=" + getID())); } private static String getURIforObject(Context c, Object anObject) throws SQLException, IOException { String result = null; // only do dcvalue for now if (anObject instanceof DCValue) { DCValue dcv = (DCValue)anObject; Item item = dcv.item; String dcq = "dc." + dcv.element; if (dcv.qualifier != null) { dcq += "." + dcv.qualifier; } String handle = null; if ((item == null) || (handle = HandleManager.findHandle(c, item)) == null) { throw new IOException("Cannot make URI for DCValue '"+dcv.value+"' ("+dcv.element+(dcv.qualifier==null?"":"."+dcv.qualifier)+") if item does not have handle"); } result = "hdl:"+handle+"#"+dcq+":"+Utils.getMD5(dcv.value+dcv.language); } return result; } }