Skip to content

Commit

Permalink
ufal/be-user_metadata-wrong-import (#440)
Browse files Browse the repository at this point in the history
* User Metadata is correctly imported - it is not updated but added every time with a transaction_id.

* Refactoring and created test

* Refactoring
  • Loading branch information
milanmajchrak authored Sep 28, 2023
1 parent ded4fc4 commit 9caa945
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 39 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 @@ -222,44 +223,18 @@ public List<ClarinUserMetadata> processSignedInUser(Context context, EPerson cur
currentUser.getID() + " is null.");
}

List<ClarinUserMetadata> currentClarinUserMetadataList = clarinUserRegistration.getUserMetadata();
List<ClarinUserMetadata> newClarinUserMetadataList;
// 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.
// 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);
}
}
// Copy current user_metadata records into a list and append it by a new user metadata.
List<ClarinUserMetadata> newClarinUserMetadataList = new ArrayList<>(clarinUserRegistration.getUserMetadata());

// Create user metadata records from request
for (ClarinUserMetadataRest clarinUserMetadataRest : clarinUserMetadataRestList) {
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 @@ -306,12 +281,25 @@ public List<ClarinUserMetadata> processNonSignedInUser(Context context,
List<ClarinUserMetadata> clarinUserMetadataList = this.createUserMetadataFromRequest(context,
clarinUserMetadataRestList);

// Get anonymous user registration - user metadata should not have `user_registration_id = null`
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
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,66 @@ public void importUserMetadataWithoutEpersonTest() throws Exception {
ClarinUserMetadataBuilder.deleteClarinUserMetadata(clarinUserRegistration.getID());
}

// The user metadata shouldn't be updated, but it should be added
@Test
public void importTwoTimesUserMetadataWithEpersonTest() throws Exception {
this.prepareEnvironment("NAME");
context.turnOffAuthorisationSystem();
ClarinUserRegistration clarinUserRegistration = ClarinUserRegistrationBuilder
.createClarinUserRegistration(context).withEPersonID(admin.getID()).build();
context.restoreAuthSystemState();
ObjectMapper mapper = new ObjectMapper();
ClarinUserMetadataRest clarinUserMetadata1 = new ClarinUserMetadataRest();
clarinUserMetadata1.setMetadataKey("NAME");
clarinUserMetadata1.setMetadataValue("Test");

List<ClarinUserMetadataRest> clarinUserMetadataRestList = new ArrayList<>();
clarinUserMetadataRestList.add(clarinUserMetadata1);

String adminToken = getAuthToken(admin.getEmail(), password);

// There should exist record in the UserRegistration table
getClient(adminToken).perform(get("/api/core/clarinuserregistrations")
.contentType(contentType))
.andExpect(status().isOk())
.andExpect(jsonPath("$.page.totalElements", is(1)));

// Manage UserMetadata and get token
getClient(adminToken).perform(post("/api/clarin/import/usermetadata")
.content(mapper.writeValueAsBytes(clarinUserMetadataRestList.toArray()))
.contentType(MediaType.APPLICATION_JSON)
.param("userRegistrationId", clarinUserRegistration.getID().toString())
.param("bitstreamUUID", bitstream.getID().toString())
.param("createdOn", "2012-09-19T10:30:03.741633")
.param("token", "111"))
.andExpect(status().isOk());

getClient(adminToken).perform(post("/api/clarin/import/usermetadata")
.content(mapper.writeValueAsBytes(clarinUserMetadataRestList.toArray()))
.contentType(MediaType.APPLICATION_JSON)
.param("userRegistrationId", clarinUserRegistration.getID().toString())
.param("bitstreamUUID", bitstream.getID().toString())
.param("createdOn", "2012-09-19T10:30:03.741633")
.param("token", "111"))
.andExpect(status().isOk());

List<ClarinUserMetadata> allUserMetadata = clarinUserMetadataService.findAll(context);
// UserMetadata should be created and not updated
assertEquals(2, allUserMetadata.size());

// get first created data and check it
ClarinUserMetadata clarinUserMetadata = allUserMetadata.get(0);
assertEquals(clarinUserMetadata.getMetadataKey(), "NAME");
assertEquals(clarinUserMetadata.getMetadataValue(), "Test");
assertEquals(clarinUserMetadata.getEperson().getPersonID(), admin.getID());
assertEquals(clarinUserMetadata.getTransaction().getCreatedOn().getTime(),
getDateFromString("2012-09-19T10:30:03.741633").getTime());
assertEquals(clarinUserMetadata.getTransaction().getToken(), "111");

//clean all
ClarinUserMetadataBuilder.deleteClarinUserMetadata(clarinUserRegistration.getID());
}

/**
* Create Workspace item with file.
*/
Expand Down

0 comments on commit 9caa945

Please sign in to comment.