Skip to content

Commit

Permalink
ufal/license-agreement-wrong-behaviour (#534)
Browse files Browse the repository at this point in the history
* The all transactions are not updated in the user metadata after a new download..

* Return the user metadata depending on user registration, bitstream, mapping and the last translation.

* Fixed checkstyle issues

* Fixed failing integration tests - do not fetch user metadata for testing by ID where there are more tests which creates the user metadata
  • Loading branch information
milanmajchrak authored Feb 15, 2024
1 parent 855c369 commit a2254c2
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;

import org.apache.commons.lang.NullArgumentException;
import org.dspace.authorize.AuthorizeException;
Expand Down Expand Up @@ -76,4 +78,44 @@ public void delete(Context context, ClarinUserMetadata clarinUserMetadata) throw
}
clarinUserMetadataDAO.delete(context, clarinUserMetadata);
}

@Override
public List<ClarinUserMetadata> findByUserRegistrationAndBitstream(Context context, Integer userRegUUID,
UUID bitstreamUUID, boolean lastTransaction)
throws SQLException {
if (lastTransaction) {
return getLastTransactionUserMetadata(clarinUserMetadataDAO.findByUserRegistrationAndBitstream(context,
userRegUUID, bitstreamUUID));
}
return clarinUserMetadataDAO.findByUserRegistrationAndBitstream(context, userRegUUID, bitstreamUUID);
}

private List<ClarinUserMetadata> getLastTransactionUserMetadata(List<ClarinUserMetadata> userMetadataList) {
Integer latestTransactionId = getIdOfLastTransaction(userMetadataList);
if (latestTransactionId == null) {
return userMetadataList;
}

List<ClarinUserMetadata> filteredUserMetadata = null;
// Filter all user metadata by the last transaction
try {
filteredUserMetadata = userMetadataList.stream()
.filter(clarinUserMetadata -> clarinUserMetadata.getTransaction().getID()
.equals(latestTransactionId))
.collect(Collectors.toList());
} catch (Exception e) {
log.error("Error filtering user metadata by the last transaction", e);
}
return filteredUserMetadata;
}

private Integer getIdOfLastTransaction(List<ClarinUserMetadata> userMetadataList) {
// userMetadataList is filtered by the last transaction - first element is the last transaction
try {
return userMetadataList.get(0).getTransaction().getID();
} catch (IndexOutOfBoundsException e) {
log.error("No transaction found for the user metadata");
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
*/
package org.dspace.content.dao.clarin;

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

import org.dspace.content.clarin.ClarinUserMetadata;
import org.dspace.core.Context;
import org.dspace.core.GenericDAO;

public interface ClarinUserMetadataDAO extends GenericDAO<ClarinUserMetadata> {
List<ClarinUserMetadata> findByUserRegistrationAndBitstream(Context context, Integer userRegUUID,
UUID bitstreamUUID) throws SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,38 @@
*/
package org.dspace.content.dao.impl.clarin;

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
import javax.persistence.Query;

import org.dspace.content.clarin.ClarinUserMetadata;
import org.dspace.content.dao.clarin.ClarinUserMetadataDAO;
import org.dspace.core.AbstractHibernateDAO;
import org.dspace.core.Context;

public class ClarinUserMetadataDAOImpl extends AbstractHibernateDAO<ClarinUserMetadata>
implements ClarinUserMetadataDAO {

protected ClarinUserMetadataDAOImpl() {
super();
}

@Override
public List<ClarinUserMetadata> findByUserRegistrationAndBitstream(Context context, Integer userRegUUID,
UUID bitstreamUUID) throws SQLException {
Query query = createQuery(context, "SELECT cum FROM ClarinUserMetadata as cum " +
"JOIN cum.eperson as ur " +
"JOIN cum.transaction as clrua " +
"JOIN clrua.licenseResourceMapping as map " +
"WHERE ur.id = :userRegUUID " +
"AND map.bitstream.id = :bitstreamUUID " +
"ORDER BY clrua.id DESC");

query.setParameter("userRegUUID", userRegUUID);
query.setParameter("bitstreamUUID", bitstreamUUID);
query.setHint("org.hibernate.cacheable", Boolean.TRUE);

return list(query);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

import org.dspace.authorize.AuthorizeException;
import org.dspace.content.clarin.ClarinUserMetadata;
Expand All @@ -22,4 +23,8 @@ public interface ClarinUserMetadataService {
List<ClarinUserMetadata> findAll(Context context) throws SQLException;
void update(Context context, ClarinUserMetadata clarinUserMetadata) throws SQLException;
void delete(Context context, ClarinUserMetadata clarinUserMetadata) throws SQLException, AuthorizeException;

List<ClarinUserMetadata> findByUserRegistrationAndBitstream(Context context, Integer userRegUUID,
UUID bitstreamUUID, boolean lastTransaction)
throws SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ public List<ClarinUserMetadata> processSignedInUser(Context context, EPerson cur
currentUser.getID() + " is null.");
}

// Copy current user_metadata records into a list and append it by a new user metadata.
List<ClarinUserMetadata> newClarinUserMetadataList = new ArrayList<>(clarinUserRegistration.getUserMetadata());
// List of the new user metadata - passed from the request
List<ClarinUserMetadata> clarinUserMetadataList = new ArrayList<>();

// Create user metadata records from request
for (ClarinUserMetadataRest clarinUserMetadataRest : clarinUserMetadataRestList) {
Expand All @@ -249,20 +249,20 @@ public List<ClarinUserMetadata> processSignedInUser(Context context, EPerson cur
clarinUserMetadata.setEperson(clarinUserRegistration);
clarinUserMetadataService.update(context, clarinUserMetadata);
// Add userMetadata to the list of the new user metadata
newClarinUserMetadataList.add(clarinUserMetadata);
clarinUserMetadataList.add(clarinUserMetadata);
}

// Process clrua with the new clarin user metadata
ClarinLicenseResourceUserAllowance clrua =
this.createClrua(context, clarinLicenseResourceMapping, newClarinUserMetadataList, downloadToken,
this.createClrua(context, clarinLicenseResourceMapping, clarinUserMetadataList, downloadToken,
clarinUserRegistration);

// Add Clarin License Resource Allowance to the user metadata records
for (ClarinUserMetadata clarinUserMetadata : newClarinUserMetadataList) {
for (ClarinUserMetadata clarinUserMetadata : clarinUserMetadataList) {
clarinUserMetadata.setTransaction(clrua);
clarinUserMetadataService.update(context, clarinUserMetadata);
}
return newClarinUserMetadataList;
return clarinUserMetadataList;
}

private ClarinLicenseResourceUserAllowance createClrua(Context context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

import org.dspace.app.rest.Parameter;
import org.dspace.app.rest.SearchRestMethod;
import org.dspace.app.rest.model.ClarinUserMetadataRest;
import org.dspace.content.clarin.ClarinUserMetadata;
import org.dspace.content.service.clarin.ClarinUserMetadataService;
Expand Down Expand Up @@ -53,6 +56,19 @@ public Page<ClarinUserMetadataRest> findAll(Context context, Pageable pageable)
}
}

@SearchRestMethod(name = "byUserRegistrationAndBitstream")
public Page<ClarinUserMetadataRest> findByUserRegistrationAndBitstream(
@Parameter(value = "userRegUUID", required = true) Integer userRegId,
@Parameter(value = "bitstreamUUID", required = true) UUID bitstreamUUID,
Pageable pageable) throws SQLException {
Context context = obtainContext();

List<ClarinUserMetadata> clarinUserMetadataList =
clarinUserMetadataService.findByUserRegistrationAndBitstream(context, userRegId, bitstreamUUID, true);

return converter.toRestPage(clarinUserMetadataList, pageable, utils.obtainProjection());
}

@Override
public Class<ClarinUserMetadataRest> getDomainClass() {
return ClarinUserMetadataRest.class;
Expand Down
Loading

0 comments on commit a2254c2

Please sign in to comment.