/* * RDFpredicate.java * * Version: $Revision: 0.1 $ * * Date: $Date: 2006/09/27 16:14:28 $ * */ 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.storage.rdbms.DatabaseManager; import org.dspace.storage.rdbms.TableRow; import org.dspace.storage.rdbms.TableRowIterator; import java.util.ArrayList; import java.util.List; /** * Class representing a Resource Description Framework predicate * * @author Lucas van Schaik * @version $Revision: 0.1 $ */ public class RDFpredicate { /** log4j logger */ private static Logger log = Logger.getLogger(RDFpredicate.class); /** Our context */ private Context myContext; /** The row in the table representing this eperson */ private TableRow myRow; /** RDFpredicate.ANY represents ANY RDFpredicate when searching **/ public final static String ANY = null; RDFpredicate(Context context, TableRow row) { myContext = context; myRow = row; myContext.cache(this, this.getID()); } public RDFpredicate(Context context, String predicate) throws SQLException { myContext = context; myRow = DatabaseManager.create(context, "RDFpredicate"); this.setURI(predicate); context.cache(this, getID()); this.update(); } public int getID() { return myRow.getIntColumn("rdf_predicate_id"); } public String getURI() { return myRow.getStringColumn("URI"); } public void setURI(String aString) { myRow.setColumn("URI", aString); } public static RDFpredicate find(Context context, int id) throws SQLException { RDFpredicate fromCache = (RDFpredicate) context.fromCache(RDFpredicate.class, id); if (fromCache != null) { return fromCache; } TableRow row = DatabaseManager.find(context, "RDFpredicate", id); if (row == null) { if (log.isDebugEnabled()) { log.debug(LogManager.getHeader(context, "find_RDFpredicate", "not_found,rdf_predicate=" + id)); } return null; } else { if (log.isDebugEnabled()) { log.debug(LogManager.getHeader(context, "find_RDFpredicate", "rdf_predicate=" + id)); } return new RDFpredicate(context,row); } } public static RDFpredicate find(Context context, String anURI) throws SQLException { TableRow row = DatabaseManager.findByUnique(context, "RDFpredicate", "URI", anURI); if (row == null) { return null; } else { // First check the cache RDFpredicate fromCache = (RDFpredicate) context.fromCache(RDFpredicate.class, row.getIntColumn("rdf_predicate_id")); if (fromCache != null) { return fromCache; } else { return new RDFpredicate(context, row); } } } public static RDFpredicate[] findAll(Context context, String partOfURI) throws SQLException { String query = "SELECT * from RDFpredicate"; if (partOfURI != null && partOfURI.length() > 0) { query += " WHERE URI like '%"+partOfURI+"%'"; } TableRowIterator tri = DatabaseManager.query(context,"RDFpredicate",query); List predicates = new ArrayList(); while (tri.hasNext()) { TableRow r = (TableRow) tri.next(); RDFpredicate fromCache = (RDFpredicate) context.fromCache( RDFpredicate.class, r.getIntColumn("rdf_predicate_id")); if (fromCache != null) { predicates.add(fromCache); } else { predicates.add(new RDFpredicate(context, r)); } } tri.close(); return (RDFpredicate[])predicates.toArray(new RDFpredicate[predicates.size()]); } public static RDFpredicate[] 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_predicate", "rdf_predicate_id=" + getID())); } }