Skip to content

Commit

Permalink
fix conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
tomdesair authored and dylan committed May 27, 2016
1 parent c1b3c6a commit b191896
Show file tree
Hide file tree
Showing 17 changed files with 310 additions and 107 deletions.
Expand Up @@ -113,9 +113,9 @@ public void deleteByDsoAndAction(Context context, DSpaceObject dso, int actionId

@Override
public void deleteByDsoAndType(Context context, DSpaceObject dso, String type) throws SQLException {
String queryString = "delete from ResourcePolicy where dSpaceObject= :dso AND rptype = :rptype";
String queryString = "delete from ResourcePolicy where dSpaceObject.id = :dsoId AND rptype = :rptype";
Query query = createQuery(context, queryString);
query.setParameter("dso", dso);
query.setParameter("dsoId", dso.getID());
query.setString("rptype", type);
query.executeUpdate();
}
Expand Down
Expand Up @@ -8,6 +8,8 @@
package org.dspace.content;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.dspace.authorize.ResourcePolicy;
import org.dspace.handle.Handle;
import org.hibernate.annotations.GenericGenerator;
Expand Down Expand Up @@ -44,7 +46,7 @@ public abstract class DSpaceObject implements Serializable
// Order by is here to ensure that the oldest handle is retrieved first,
// multiple handles are assigned to the latest version of an item the original handle will have the lowest identifier
// This handle is the prefered handle.
@OrderBy("handle_id ASC")
@OrderBy("id ASC")
private List<Handle> handles = new ArrayList<>();

@OneToMany(fetch = FetchType.LAZY, mappedBy = "dSpaceObject", cascade={CascadeType.PERSIST}, orphanRemoval = false)
Expand Down Expand Up @@ -192,4 +194,5 @@ public void clearModified() {
protected void setModified() {
this.modified = true;
}

}
Expand Up @@ -205,31 +205,16 @@ public List<MetadataValue> getMetadata(T dso, String schema, String element, Str

@Override
public void addMetadata(Context context, T dso, String schema, String element, String qualifier, String lang, List<String> values) throws SQLException {
String fieldKey = metadataAuthorityService.makeFieldKey(schema, element, qualifier);
if (metadataAuthorityService.isAuthorityControlled(fieldKey))
{
List<String> authorities = new ArrayList<String>();
List<Integer> confidences = new ArrayList<Integer>();
for (int i = 0; i < values.size(); ++i)
{
if(dso instanceof Item)
{
getAuthoritiesAndConfidences(fieldKey, ((Item) dso).getOwningCollection(), values, authorities, confidences, i);
}else{
getAuthoritiesAndConfidences(fieldKey, null, values, authorities, confidences, i);
}
}
addMetadata(context, dso, schema, element, qualifier, lang, values, authorities, confidences);
}
else
{
addMetadata(context, dso, schema, element, qualifier, lang, values, null, null);
MetadataField metadataField = metadataFieldService.findByElement(context, schema, element, qualifier);
if (metadataField == null) {
throw new SQLException("bad_dublin_core schema=" + schema + "." + element + "." + qualifier);
}

addMetadata(context, dso, metadataField, lang, values);
}

@Override
public void addMetadata(Context context, T dso, String schema, String element, String qualifier, String lang, List<String> values, List<String> authorities, List<Integer> confidences) throws SQLException {

// We will not verify that they are valid entries in the registry
// until update() is called.
MetadataField metadataField = metadataFieldService.findByElement(context, schema, element, qualifier);
Expand Down Expand Up @@ -321,27 +306,23 @@ public void addMetadata(Context context, T dso, MetadataField metadataField, Str

@Override
public void addMetadata(Context context, T dso, MetadataField metadataField, String language, List<String> values) throws SQLException {
String fieldKey = metadataAuthorityService.makeFieldKey(metadataField.getMetadataSchema().getName(), metadataField.getElement(), metadataField.getQualifier());
if (metadataAuthorityService.isAuthorityControlled(fieldKey))
{
List<String> authorities = new ArrayList<String>();
List<Integer> confidences = new ArrayList<Integer>();
for (int i = 0; i < values.size(); ++i)
{
if(dso instanceof Item)
{
getAuthoritiesAndConfidences(fieldKey, ((Item) dso).getOwningCollection(), values, authorities, confidences, i);
}else{
getAuthoritiesAndConfidences(fieldKey, null, values, authorities, confidences, i);
if(metadataField != null) {
String fieldKey = metadataAuthorityService.makeFieldKey(metadataField.getMetadataSchema().getName(), metadataField.getElement(), metadataField.getQualifier());
if (metadataAuthorityService.isAuthorityControlled(fieldKey)) {
List<String> authorities = new ArrayList<String>();
List<Integer> confidences = new ArrayList<Integer>();
for (int i = 0; i < values.size(); ++i) {
if (dso instanceof Item) {
getAuthoritiesAndConfidences(fieldKey, ((Item) dso).getOwningCollection(), values, authorities, confidences, i);
} else {
getAuthoritiesAndConfidences(fieldKey, null, values, authorities, confidences, i);
}
}
addMetadata(context, dso, metadataField, language, values, authorities, confidences);
} else {
addMetadata(context, dso, metadataField, language, values, null, null);
}
addMetadata(context, dso, metadataField, language, values, authorities, confidences);
}
else
{
addMetadata(context, dso, metadataField, language, values, null, null);
}

}

@Override
Expand Down
Expand Up @@ -26,7 +26,6 @@
import org.dspace.eperson.Group;
import org.dspace.event.Event;
import org.dspace.harvest.HarvestedItem;
import org.dspace.harvest.factory.HarvestServiceFactory;
import org.dspace.harvest.service.HarvestedItemService;
import org.dspace.identifier.IdentifierException;
import org.dspace.identifier.service.IdentifierService;
Expand Down Expand Up @@ -622,7 +621,7 @@ protected void rawDelete(Context context, Item item) throws AuthorizeException,
// Remove any Handle
handleService.unbindHandle(context, item);

// remove version attached to the item
// remove version attached to the item
removeVersion(context, item);

// Finally remove item row
Expand Down
Expand Up @@ -201,7 +201,7 @@ public Iterator<Item> findByMetadataQuery(Context context, List<List<MetadataFie

@Override
public Iterator<Item> findByAuthorityValue(Context context, MetadataField metadataField, String authority, boolean inArchive) throws SQLException {
Query query = createQuery(context, "SELECT item FROM Item as item join item.metadata metadatavalue WHERE item.inArchive=:in_archive AND metadatavalue.metadataField = :metadata_field AND metadatavalue.authority = :authority");
Query query = createQuery(context, "SELECT item FROM Item as item join item.metadata metadatavalue WHERE item.inArchive=:in_archive AND metadatavalue.metadataField = :metadata_field AND metadatavalueq = :authority");
query.setParameter("in_archive", inArchive);
query.setParameter("metadata_field", metadataField);
query.setParameter("authority", authority);
Expand Down
67 changes: 56 additions & 11 deletions dspace-api/src/main/java/org/dspace/core/Context.java
Expand Up @@ -7,9 +7,6 @@
*/
package org.dspace.core;

import java.sql.SQLException;
import java.util.*;

import org.apache.log4j.Logger;
import org.dspace.eperson.EPerson;
import org.dspace.eperson.Group;
Expand All @@ -22,6 +19,9 @@
import org.dspace.utils.DSpace;
import org.springframework.util.CollectionUtils;

import java.sql.SQLException;
import java.util.*;

/**
* Class representing the context of a particular DSpace operation. This stores
* information such as the current authenticated user and the database
Expand Down Expand Up @@ -89,9 +89,13 @@ protected Context(EventService eventService, DBConnection dbConnection) {
init();
}


/**
* Construct a new context object with default options. A database connection is opened.
* No user is authenticated.
*
* @exception SQLException
* if there was an error obtaining a database connection
*/
public Context()
{
Expand Down Expand Up @@ -154,6 +158,10 @@ DBConnection getDBConnection()
return dbConnection;
}

public void enableBatchMode(boolean batchModeEnabled) throws SQLException {
dbConnection.setOptimizedForBatchProcessing(batchModeEnabled);
}


public DatabaseConfigVO getDBConfig() throws SQLException
{
Expand Down Expand Up @@ -346,30 +354,53 @@ public void complete() throws SQLException
if(!isValid())
log.info("complete() was called on a closed Context object. No changes to commit.");

// FIXME: Might be good not to do a commit() if nothing has actually
// been written using this connection
try
{
// As long as we have a valid, writeable database connection,
// commit any changes made as part of the transaction
if (isValid() && !isReadOnly())
{
dispatchEvents();
}
commit();
}
finally
{
if(dbConnection != null)
{
//Commit our changes
dbConnection.commit();
// Free the DB connection
dbConnection.closeDBConnection();
dbConnection = null;
}
}
}

public void commit() throws SQLException
{
// If Context is no longer open/valid, just note that it has already been closed
if(!isValid()) {
log.info("commit() was called on a closed Context object. No changes to commit.");
}

// Our DB Connection (Hibernate) will decide if an actual commit is required or not
try
{
// As long as we have a valid, writeable database connection,
// commit any changes made as part of the transaction
if (isValid() && !isReadOnly())
{
dispatchEvents();
}

} finally {
if(log.isDebugEnabled()) {
log.debug("Cache size on commit is " + getCacheSize());
}

if(dbConnection != null)
{
//Commit our changes
dbConnection.commit();
}
}
}


public void dispatchEvents()
{
Expand Down Expand Up @@ -594,6 +625,20 @@ public void shutDownDatabase() throws SQLException {
}

public void clearCache() throws SQLException {
if(log.isDebugEnabled()) {
log.debug("Cache size before clear cache is " + getCacheSize());
}

UUID epersonId = getCurrentUser().getID();

this.getDBConnection().clearCache();

if(epersonId != null) {
setCurrentUser(EPersonServiceFactory.getInstance().getEPersonService().find(this, epersonId));
}
}

public long getCacheSize() throws SQLException {
return this.getDBConnection().getCacheSize();
}
}
4 changes: 4 additions & 0 deletions dspace-api/src/main/java/org/dspace/core/DBConnection.java
Expand Up @@ -41,4 +41,8 @@ public interface DBConnection<T> {
public DatabaseConfigVO getDatabaseConfig() throws SQLException;

public void clearCache() throws SQLException;

public void setOptimizedForBatchProcessing(boolean batchOptimized) throws SQLException;

public long getCacheSize() throws SQLException;
}
Expand Up @@ -8,6 +8,7 @@
package org.dspace.core;

import org.dspace.storage.rdbms.DatabaseConfigVO;
import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
Expand All @@ -32,10 +33,13 @@ public class HibernateDBConnection implements DBConnection<Session> {
@Qualifier("sessionFactory")
private SessionFactory sessionFactory;

private boolean batchModeEnabled = false;

@Override
public Session getSession() throws SQLException {
if(!isTransActionAlive()){
sessionFactory.getCurrentSession().beginTransaction();
configureBatchMode();
}
return sessionFactory.getCurrentSession();
}
Expand Down Expand Up @@ -74,6 +78,7 @@ public void closeDBConnection() throws SQLException {
public void commit() throws SQLException {
if(isTransActionAlive() && !getTransaction().wasRolledBack())
{
getSession().flush();
getTransaction().commit();
}
}
Expand Down Expand Up @@ -110,6 +115,26 @@ public DatabaseConfigVO getDatabaseConfig() throws SQLException {

@Override
public void clearCache() throws SQLException {
this.getSession().clear();
getSession().flush();
getSession().clear();
}

@Override
public long getCacheSize() throws SQLException {
return getSession().getStatistics().getEntityCount();
}

@Override
public void setOptimizedForBatchProcessing(final boolean batchOptimized) throws SQLException {
this.batchModeEnabled = batchOptimized;
configureBatchMode();
}

private void configureBatchMode() throws SQLException {
if(batchModeEnabled) {
getSession().setFlushMode(FlushMode.ALWAYS);
} else {
getSession().setFlushMode(FlushMode.AUTO);
}
}
}
Expand Up @@ -97,7 +97,7 @@ public Group findByName(final Context context, final String name) throws SQLExce
query.setParameter("name", name);
query.setCacheable(true);

return uniqueResult(query);
return singleResult(query);
}

@Override
Expand All @@ -123,7 +123,7 @@ public Group findByNameAndEPerson(Context context, String groupName, EPerson ePe
query.setParameter("eperson_id", ePerson.getID());
query.setCacheable(true);

return uniqueResult(query);
return singleResult(query);
}
}

Expand Down
24 changes: 24 additions & 0 deletions dspace-api/src/main/java/org/dspace/handle/Handle.java
Expand Up @@ -7,6 +7,8 @@
*/
package org.dspace.handle;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.dspace.content.DSpaceObject;
import org.dspace.core.Context;

Expand Down Expand Up @@ -78,4 +80,26 @@ public void setResourceTypeId(Integer resourceTypeId) {
public Integer getResourceTypeId() {
return resourceTypeId;
}

public boolean equals(final Object o) {
if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

Handle handle1 = (Handle) o;

return new EqualsBuilder()
.append(id, handle1.id)
.append(handle, handle1.handle)
.append(resourceTypeId, handle1.resourceTypeId)
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(id)
.append(handle)
.append(resourceTypeId)
.toHashCode();
}
}

0 comments on commit b191896

Please sign in to comment.