mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-12 04:23:13 +00:00
DSpace refactored service api
This commit is contained in:
@@ -7,22 +7,14 @@
|
||||
*/
|
||||
package org.dspace.content;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
import org.hibernate.proxy.HibernateProxyHelper;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.dspace.authorize.AuthorizeException;
|
||||
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 javax.persistence.*;
|
||||
|
||||
/**
|
||||
* Database access class representing a Dublin Core metadata value.
|
||||
* It represents a value of a given <code>MetadataField</code> on an Item.
|
||||
* (The Item can have many values of the same field.) It contains element, qualifier, value and language.
|
||||
* (The Item can have many values of the same field.) It contains element, qualifier, value and language.
|
||||
* the field (which names the schema, element, and qualifier), language,
|
||||
* and a value.
|
||||
*
|
||||
@@ -30,134 +22,80 @@ import org.dspace.storage.rdbms.TableRowIterator;
|
||||
* @see org.dspace.content.MetadataSchema
|
||||
* @see org.dspace.content.MetadataField
|
||||
*/
|
||||
@Entity
|
||||
@Table(name="metadatavalue", schema = "public")
|
||||
public class MetadataValue
|
||||
{
|
||||
/** The reference to the metadata field */
|
||||
private int fieldId = 0;
|
||||
@Id
|
||||
@Column(name="metadata_value_id")
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE ,generator="metadatavalue_seq")
|
||||
@SequenceGenerator(name="metadatavalue_seq", sequenceName="metadatavalue_seq", allocationSize = 1)
|
||||
private int valueId;
|
||||
|
||||
/** The primary key for the metadata value */
|
||||
private int valueId = 0;
|
||||
|
||||
/** The reference to the DSpace resource */
|
||||
private int resourceId;
|
||||
|
||||
/** The reference to the DSpace resource type*/
|
||||
private int resourceTypeId;
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "metadata_field_id")
|
||||
private MetadataField metadataField = null;
|
||||
|
||||
/** The value of the field */
|
||||
public String value;
|
||||
// @Column(name = "text_value")
|
||||
// @Lob
|
||||
// @Type(type = "org.hibernate.type.TextType")
|
||||
@Column(name="text_value", columnDefinition = "text")
|
||||
private String value;
|
||||
|
||||
/** The language of the field, may be <code>null</code> */
|
||||
public String language;
|
||||
@Column(name = "text_lang", length = 24)
|
||||
private String language;
|
||||
|
||||
/** The position of the record. */
|
||||
public int place = 1;
|
||||
@Column(name = "place")
|
||||
private int place = 1;
|
||||
|
||||
/** Authority key, if any */
|
||||
public String authority = null;
|
||||
@Column(name = "authority", length = 100)
|
||||
private String authority = null;
|
||||
|
||||
/** Authority confidence value -- see Choices class for values */
|
||||
public int confidence = 0;
|
||||
@Column(name = "confidence")
|
||||
private int confidence = -1;
|
||||
|
||||
/** log4j logger */
|
||||
private static Logger log = Logger.getLogger(MetadataValue.class);
|
||||
@ManyToOne(fetch = FetchType.EAGER, cascade={CascadeType.PERSIST})
|
||||
@JoinColumn(name="dspace_object_id")
|
||||
protected DSpaceObject dSpaceObject;
|
||||
|
||||
/** The row in the table representing this type */
|
||||
private TableRow row;
|
||||
|
||||
/**
|
||||
* Construct the metadata object from the matching database row.
|
||||
*
|
||||
* @param row database row to use for contents
|
||||
*/
|
||||
public MetadataValue(TableRow row)
|
||||
{
|
||||
if (row != null)
|
||||
{
|
||||
fieldId = row.getIntColumn("metadata_field_id");
|
||||
valueId = row.getIntColumn("metadata_value_id");
|
||||
resourceId = row.getIntColumn("resource_id");
|
||||
resourceTypeId = row.getIntColumn("resource_type_id");
|
||||
value = row.getStringColumn("text_value");
|
||||
language = row.getStringColumn("text_lang");
|
||||
place = row.getIntColumn("place");
|
||||
authority = row.getStringColumn("authority");
|
||||
confidence = row.getIntColumn("confidence");
|
||||
this.row = row;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
public MetadataValue()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor to create a value for a given field.
|
||||
*
|
||||
* @param field initial value for field
|
||||
*/
|
||||
public MetadataValue(MetadataField field)
|
||||
{
|
||||
this.fieldId = field.getFieldID();
|
||||
protected MetadataValue() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the field ID the metadata value represents.
|
||||
*
|
||||
* @return metadata field ID
|
||||
* @return metadata value ID
|
||||
*/
|
||||
public int getFieldId()
|
||||
public int getValueId()
|
||||
{
|
||||
return fieldId;
|
||||
return valueId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the field ID that the metadata value represents.
|
||||
* Get the dspaceObject
|
||||
*
|
||||
* @param fieldId new field ID
|
||||
* @return dspaceObject
|
||||
*/
|
||||
public void setFieldId(int fieldId)
|
||||
public DSpaceObject getDSpaceObject()
|
||||
{
|
||||
this.fieldId = fieldId;
|
||||
return dSpaceObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the resource type ID.
|
||||
* Set the dspaceObject ID.
|
||||
*
|
||||
* @return resource type ID
|
||||
* @param dso new dspaceObject ID
|
||||
*/
|
||||
public int getResourceTypeId() {
|
||||
return resourceTypeId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the resource type ID.
|
||||
*
|
||||
* @param resourceTypeId new resource type ID
|
||||
*/
|
||||
public void setResourceTypeId(int resourceTypeId) {
|
||||
this.resourceTypeId = resourceTypeId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the resource id
|
||||
*
|
||||
* @return resource ID
|
||||
*/
|
||||
public int getResourceId() {
|
||||
return resourceId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the resource type ID.
|
||||
*
|
||||
* @param resourceId new resource ID
|
||||
*/
|
||||
public void setResourceId(int resourceId) {
|
||||
this.resourceId = resourceId;
|
||||
public void setDSpaceObject(DSpaceObject dso)
|
||||
{
|
||||
this.dSpaceObject = dso;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -200,14 +138,12 @@ public class MetadataValue
|
||||
this.place = place;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value ID.
|
||||
*
|
||||
* @return value ID
|
||||
*/
|
||||
public int getValueId()
|
||||
{
|
||||
return valueId;
|
||||
public MetadataField getMetadataField() {
|
||||
return metadataField;
|
||||
}
|
||||
|
||||
public void setMetadataField(MetadataField metadataField) {
|
||||
this.metadataField = metadataField;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -270,156 +206,6 @@ public class MetadataValue
|
||||
this.confidence = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new metadata value.
|
||||
*
|
||||
* @param context
|
||||
* DSpace context object
|
||||
* @throws SQLException
|
||||
* @throws AuthorizeException
|
||||
*/
|
||||
public void create(Context context) throws SQLException, AuthorizeException
|
||||
{
|
||||
// Create a table row and update it with the values
|
||||
row = DatabaseManager.row("MetadataValue");
|
||||
row.setColumn("resource_id", resourceId);
|
||||
row.setColumn("resource_type_id", resourceTypeId);
|
||||
row.setColumn("metadata_field_id", fieldId);
|
||||
row.setColumn("text_value", value);
|
||||
row.setColumn("text_lang", language);
|
||||
row.setColumn("place", place);
|
||||
row.setColumn("authority", authority);
|
||||
row.setColumn("confidence", confidence);
|
||||
DatabaseManager.insert(context, row);
|
||||
|
||||
// Remember the new row number
|
||||
this.valueId = row.getIntColumn("metadata_value_id");
|
||||
|
||||
// log.info(LogManager.getHeader(context, "create_metadata_value",
|
||||
// "metadata_value_id=" + valueId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the metadata value from the database.
|
||||
*
|
||||
* @param context dspace context
|
||||
* @param valueId database key id of value
|
||||
* @return recalled metadata value
|
||||
* @throws IOException
|
||||
* @throws SQLException
|
||||
* @throws AuthorizeException
|
||||
*/
|
||||
public static MetadataValue find(Context context, int valueId)
|
||||
throws IOException, SQLException, AuthorizeException
|
||||
{
|
||||
// Grab rows from DB
|
||||
TableRowIterator tri = DatabaseManager.queryTable(context, "MetadataValue",
|
||||
"SELECT * FROM MetadataValue where metadata_value_id= ? ",
|
||||
valueId);
|
||||
|
||||
TableRow row = null;
|
||||
try
|
||||
{
|
||||
if (tri.hasNext())
|
||||
{
|
||||
row = tri.next();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
// close the TableRowIterator to free up resources
|
||||
if (tri != null)
|
||||
{
|
||||
tri.close();
|
||||
}
|
||||
}
|
||||
|
||||
if (row == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return new MetadataValue(row);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the metadata values for a given field from the database.
|
||||
*
|
||||
* @param context dspace context
|
||||
* @param fieldId field whose values to look for
|
||||
* @return a collection of metadata values
|
||||
* @throws IOException
|
||||
* @throws SQLException
|
||||
* @throws AuthorizeException
|
||||
*/
|
||||
public static List<MetadataValue> findByField(Context context, int fieldId)
|
||||
throws IOException, SQLException, AuthorizeException
|
||||
{
|
||||
// Grab rows from DB
|
||||
TableRowIterator tri = DatabaseManager.queryTable(context, "MetadataValue",
|
||||
"SELECT * FROM MetadataValue WHERE metadata_field_id= ? ",
|
||||
fieldId);
|
||||
|
||||
TableRow row = null;
|
||||
List<MetadataValue> ret = new ArrayList<MetadataValue>();
|
||||
try
|
||||
{
|
||||
while (tri.hasNext())
|
||||
{
|
||||
row = tri.next();
|
||||
ret.add(new MetadataValue(row));
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
// close the TableRowIterator to free up resources
|
||||
if (tri != null)
|
||||
{
|
||||
tri.close();
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the metadata value in the database.
|
||||
*
|
||||
* @param context dspace context
|
||||
* @throws SQLException
|
||||
* @throws AuthorizeException
|
||||
*/
|
||||
public void update(Context context) throws SQLException, AuthorizeException
|
||||
{
|
||||
row.setColumn("resource_id", resourceId);
|
||||
row.setColumn("resource_type_id", resourceTypeId);
|
||||
row.setColumn("metadata_field_id", fieldId);
|
||||
row.setColumn("text_value", value);
|
||||
row.setColumn("text_lang", language);
|
||||
row.setColumn("place", place);
|
||||
row.setColumn("authority", authority);
|
||||
row.setColumn("confidence", confidence);
|
||||
DatabaseManager.update(context, row);
|
||||
|
||||
log.info(LogManager.getHeader(context, "update_metadatavalue",
|
||||
"metadata_value_id=" + getValueId()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the metadata field.
|
||||
*
|
||||
* @param context dspace context
|
||||
* @throws SQLException
|
||||
* @throws AuthorizeException
|
||||
*/
|
||||
public void delete(Context context) throws SQLException, AuthorizeException
|
||||
{
|
||||
log.info(LogManager.getHeader(context, "delete_metadata_value",
|
||||
" metadata_value_id=" + getValueId()));
|
||||
DatabaseManager.delete(context, row);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return <code>true</code> if <code>other</code> is the same MetadataValue
|
||||
@@ -438,24 +224,21 @@ public class MetadataValue
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass())
|
||||
Class<?> objClass = HibernateProxyHelper.getClassWithoutInitializingProxy(obj);
|
||||
if (getClass() != objClass)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
final MetadataValue other = (MetadataValue) obj;
|
||||
if (this.fieldId != other.fieldId)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (this.valueId != other.valueId)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (this.resourceId != other.resourceId)
|
||||
if (this.getValueId() != other.getValueId())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (this.resourceTypeId != other.resourceTypeId)
|
||||
if (this.getDSpaceObject().getID() != other.getDSpaceObject().getID())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -466,10 +249,11 @@ public class MetadataValue
|
||||
public int hashCode()
|
||||
{
|
||||
int hash = 7;
|
||||
hash = 47 * hash + this.fieldId;
|
||||
hash = 47 * hash + this.valueId;
|
||||
hash = 47 * hash + this.resourceId;
|
||||
hash = 47 * hash + this.resourceTypeId;
|
||||
hash = 47 * hash + this.getValueId();
|
||||
hash = 47 * hash + this.getDSpaceObject().getID().hashCode();
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user