Skip to content

Commit

Permalink
User Metadata is correctly imported - it is not updated but added eve…
Browse files Browse the repository at this point in the history
…ry time with a transaction_id.
  • Loading branch information
milanmajchrak committed Sep 26, 2023
1 parent fb252cf commit c5a2df1
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
@Table(name = "user_registration")
public class ClarinUserRegistration implements ReloadableEntity<Integer> {

public static final String ANONYMOUS_USER_REGISTRATION = "anonymous";

private static Logger log = org.apache.logging.log4j.LogManager.getLogger(ClarinUserRegistration.class);

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public List<ClarinUserRegistration> findByEPersonUUID(Context context, UUID eper
return clarinUserRegistrationDAO.findByEPersonUUID(context, epersonUUID);
}

@Override
public List<ClarinUserRegistration> findByEmail(Context context, String email) throws SQLException {
return clarinUserRegistrationDAO.findByEmail(context, email);
}

@Override
public void delete(Context context, ClarinUserRegistration clarinUserRegistration)
throws SQLException, AuthorizeException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@
public interface ClarinUserRegistrationDAO extends GenericDAO<ClarinUserRegistration> {

List<ClarinUserRegistration> findByEPersonUUID(Context context, UUID epersonUUID) throws SQLException;

List<ClarinUserRegistration> findByEmail(Context context, String email) throws SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,15 @@ public List<ClarinUserRegistration> findByEPersonUUID(Context context, UUID eper

return list(query);
}

@Override
public List<ClarinUserRegistration> findByEmail(Context context, String email) throws SQLException {
Query query = createQuery(context, "SELECT cur FROM ClarinUserRegistration as cur " +
"WHERE cur.email = :email");

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

return list(query);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ ClarinUserRegistration create(Context context,
ClarinUserRegistration find(Context context, int valueId) throws SQLException;
List<ClarinUserRegistration> findAll(Context context) throws SQLException, AuthorizeException;
List<ClarinUserRegistration> findByEPersonUUID(Context context, UUID epersonUUID) throws SQLException;

List<ClarinUserRegistration> findByEmail(Context context, String email) throws SQLException;
void delete(Context context, ClarinUserRegistration clarinUserRegistration) throws SQLException, AuthorizeException;
void update(Context context, ClarinUserRegistration clarinUserRegistration) throws SQLException, AuthorizeException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static org.dspace.app.rest.utils.ContextUtil.obtainContext;
import static org.dspace.content.clarin.ClarinLicense.SEND_TOKEN;
import static org.dspace.content.clarin.ClarinUserRegistration.ANONYMOUS_USER_REGISTRATION;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.io.IOException;
Expand Down Expand Up @@ -223,43 +224,43 @@ public List<ClarinUserMetadata> processSignedInUser(Context context, EPerson cur
}

List<ClarinUserMetadata> currentClarinUserMetadataList = clarinUserRegistration.getUserMetadata();
List<ClarinUserMetadata> newClarinUserMetadataList;
List<ClarinUserMetadata> newClarinUserMetadataList = new ArrayList<>(currentClarinUserMetadataList);
// If exists ClarinResourceUserAllowance - Clrua record in the table, create a new clrua with current
// resource mapping, user metadata, user registration
if (CollectionUtils.isEmpty(currentClarinUserMetadataList)) {
// The current user doesn't fill in any user metadata, create a new UserMetadata objects
newClarinUserMetadataList = this.createUserMetadataFromRequest(context,
clarinUserMetadataRestList);
} else {
// The current user does fill in any user metadata, update actual UserMetadata objects and create
// the new ones is some are missing.
// if (CollectionUtils.isEmpty(currentClarinUserMetadataList)) {
// // The current user doesn't fill in any user metadata, create a new UserMetadata objects
// newClarinUserMetadataList = this.createUserMetadataFromRequest(context,
// clarinUserMetadataRestList);
// } else {
// The current user has signed some user metadata, update actual UserMetadata objects and create
// the new one.
// Compare the old metadata value with the new one and if the value is changed or missing, create/update
// the metadata value.
newClarinUserMetadataList = new ArrayList<>();
for (ClarinUserMetadataRest clarinUserMetadataRest : clarinUserMetadataRestList) {
boolean shouldCreate = true;
for (ClarinUserMetadata clarinUserMetadata: currentClarinUserMetadataList) {
if (StringUtils.equals(clarinUserMetadataRest.getMetadataKey(),
clarinUserMetadata.getMetadataKey())) {
shouldCreate = false;
// Set metadata value
clarinUserMetadata.setMetadataValue(clarinUserMetadataRest.getMetadataValue());
// Update the user metadata record
clarinUserMetadataService.update(context, clarinUserMetadata);
// Add userMetadata to the list of the new user metadata
newClarinUserMetadataList.add(clarinUserMetadata);
}
}
if (shouldCreate) {
ClarinUserMetadata clarinUserMetadata = this.clarinUserMetadataService.create(context);
clarinUserMetadata.setMetadataKey(clarinUserMetadataRest.getMetadataKey());
clarinUserMetadata.setMetadataValue(clarinUserMetadataRest.getMetadataValue());
clarinUserMetadata.setEperson(clarinUserRegistration);
clarinUserMetadataService.update(context, clarinUserMetadata);
// Add userMetadata to the list of the new user metadata
newClarinUserMetadataList.add(clarinUserMetadata);
}
}
// newClarinUserMetadataList = new ArrayList<>();
for (ClarinUserMetadataRest clarinUserMetadataRest : clarinUserMetadataRestList) {
// boolean shouldCreate = true;
// for (ClarinUserMetadata clarinUserMetadata: currentClarinUserMetadataList) {
// if (StringUtils.equals(clarinUserMetadataRest.getMetadataKey(),
// clarinUserMetadata.getMetadataKey())) {
// shouldCreate = false;
// // Set metadata value
// clarinUserMetadata.setMetadataValue(clarinUserMetadataRest.getMetadataValue());
// // Update the user metadata record
// clarinUserMetadataService.update(context, clarinUserMetadata);
// // Add userMetadata to the list of the new user metadata
// newClarinUserMetadataList.add(clarinUserMetadata);
// }
// }
// if (shouldCreate) {
ClarinUserMetadata clarinUserMetadata = this.clarinUserMetadataService.create(context);
clarinUserMetadata.setMetadataKey(clarinUserMetadataRest.getMetadataKey());
clarinUserMetadata.setMetadataValue(clarinUserMetadataRest.getMetadataValue());
clarinUserMetadata.setEperson(clarinUserRegistration);
clarinUserMetadataService.update(context, clarinUserMetadata);
// Add userMetadata to the list of the new user metadata
newClarinUserMetadataList.add(clarinUserMetadata);
// }
// }
}

// Process clrua with the new clarin user metadata
Expand Down Expand Up @@ -289,6 +290,7 @@ private ClarinLicenseResourceUserAllowance createClrua(Context context,
clrua.setCreatedOn(Calendar.getInstance().getTime());
// Generate token to download the bitstream. The token is sent by the response or by the e-mail.
clrua.setToken(downloadToken);

if (Objects.nonNull(clarinUserRegistration)) {
clrua.setUserRegistration(clarinUserRegistration);
}
Expand All @@ -306,12 +308,25 @@ public List<ClarinUserMetadata> processNonSignedInUser(Context context,
List<ClarinUserMetadata> clarinUserMetadataList = this.createUserMetadataFromRequest(context,
clarinUserMetadataRestList);

// Get anonymous user registration
ClarinUserRegistration clarinUserRegistration = null;
List<ClarinUserRegistration> clarinUserRegistrationList = clarinUserRegistrationService
.findByEmail(context, ANONYMOUS_USER_REGISTRATION);
for (ClarinUserRegistration fetchedClarinuserRegistration : clarinUserRegistrationList) {
if (!StringUtils.equals(fetchedClarinuserRegistration.getOrganization(), ANONYMOUS_USER_REGISTRATION)) {
continue;
}
clarinUserRegistration = fetchedClarinuserRegistration;
break;
}

// Create ClarinResourceUserAllowance record to generate token.
ClarinLicenseResourceUserAllowance clrua = this.createClrua(context, clarinLicenseResourceMapping,
clarinUserMetadataList, downloadToken, null);
clarinUserMetadataList, downloadToken, clarinUserRegistration);
// Add Clarin License Resource Allowance to the user metadata records
for (ClarinUserMetadata clarinUserMetadata : clarinUserMetadataList) {
clarinUserMetadata.setTransaction(clrua);
clarinUserMetadata.setEperson(clarinUserRegistration);
clarinUserMetadataService.update(context, clarinUserMetadata);
}
return clarinUserMetadataList;
Expand Down

0 comments on commit c5a2df1

Please sign in to comment.