/* * RDFvalue.java * * Version: $Revision: 0.1 $ * * Date: $Date: 2007/11/23 13:46: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.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 value * * @author Lucas van Schaik * @version $Revision: 0.1 $ */ public class RDFvalue { /** log4j logger */ private static Logger log = Logger.getLogger(RDFvalue.class); /** Our context */ private Context myContext; /** The row in the table representing this eperson */ private TableRow myRow; RDFvalue(Context context, TableRow row) { myContext = context; myRow = row; myContext.cache(this, this.getID()); } RDFvalue(Context context, String value) throws SQLException { myContext = context; myRow = DatabaseManager.create(context, "RDFvalue"); this.setValue(value); context.cache(this, getID()); this.update(); } public static RDFvalue create(Context context, String value) throws SQLException { RDFvalue result = RDFvalue.find(context, value); if (result == null) { result = new RDFvalue(context, value); } return result; } public int getID() { return myRow.getIntColumn("rdf_value_id"); } public String getValue() { return myRow.getStringColumn("value"); } public void setValue(String aString) { myRow.setColumn("value", aString); } public static RDFvalue find(Context context, int id) throws SQLException { RDFvalue fromCache = (RDFvalue) context.fromCache(RDFvalue.class, id); if (fromCache != null) { return fromCache; } TableRow row = DatabaseManager.find(context, "RDFvalue", id); if (row == null) { if (log.isDebugEnabled()) { log.debug(LogManager.getHeader(context, "find_RDFvalue", "not_found,rdf_value=" + id)); } return null; } else { if (log.isDebugEnabled()) { log.debug(LogManager.getHeader(context, "find_RDFvalue", "rdf_value=" + id)); } return new RDFvalue(context,row); } } public static RDFvalue find(Context context, String aValue) throws SQLException { TableRow row = DatabaseManager.findByUnique(context, "RDFvalue", "value", aValue); if (row == null) { return null; } else { // First check the cache RDFvalue fromCache = (RDFvalue) context.fromCache(RDFvalue.class, row.getIntColumn("rdf_value_id")); if (fromCache != null) { return fromCache; } else { return new RDFvalue(context, row); } } } public static RDFvalue[] findAll(Context context, String partOfValue) throws SQLException { String query = "SELECT * from RDFvalue"; if (partOfValue != null && partOfValue.length() > 0) { query += " WHERE value like '%"+partOfValue+"%'"; } TableRowIterator tri = DatabaseManager.query(context,"RDFvalue",query); List values = new ArrayList(); while (tri.hasNext()) { TableRow r = (TableRow) tri.next(); RDFvalue fromCache = (RDFvalue) context.fromCache( RDFvalue.class, r.getIntColumn("rdf_value_id")); if (fromCache != null) { values.add(fromCache); } else { values.add(new RDFvalue(context, r)); } } tri.close(); return (RDFvalue[])values.toArray(new RDFvalue[values.size()]); } public static RDFvalue[] 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_value", "rdf_value_id=" + getID())); } }