[CST-12108] fix community feedback

This commit is contained in:
Mykhaylo Boychuk
2024-02-20 13:25:56 +01:00
parent a2ee986f7f
commit 7079654f8b
7 changed files with 130 additions and 20 deletions

View File

@@ -12,8 +12,6 @@ import static org.dspace.correctiontype.WithdrawnCorrectionType.WITHDRAWAL_REINS
import java.sql.SQLException;
import java.util.Date;
import java.util.Objects;
import java.util.UUID;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
@@ -67,12 +65,12 @@ public class ReinstateCorrectionType implements CorrectionType, InitializingBean
}
private boolean currentUserIsMemberOfwithdrawalReinstateGroup(Context context) throws SQLException {
String withdrawalReinstateGroupUUID = configurationService.getProperty(WITHDRAWAL_REINSTATE_GROUP);
if (StringUtils.isBlank(withdrawalReinstateGroupUUID)) {
String groupName = configurationService.getProperty(WITHDRAWAL_REINSTATE_GROUP);
if (StringUtils.isBlank(groupName)) {
return false;
}
Group withdrawalReinstateGroup = groupService.find(context, UUID.fromString(withdrawalReinstateGroupUUID));
return Objects.nonNull(withdrawalReinstateGroup) && groupService.isMember(context, withdrawalReinstateGroup);
Group withdrawalReinstateGroup = groupService.findByName(context, groupName);
return withdrawalReinstateGroup != null && groupService.isMember(context, withdrawalReinstateGroup);
}
@Override

View File

@@ -12,8 +12,6 @@ import static org.dspace.core.Constants.READ;
import java.sql.SQLException;
import java.util.Date;
import java.util.Objects;
import java.util.UUID;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
@@ -40,7 +38,7 @@ import org.springframework.beans.factory.annotation.Autowired;
*/
public class WithdrawnCorrectionType implements CorrectionType, InitializingBean {
public static final String WITHDRAWAL_REINSTATE_GROUP = "withdrawal.reinstate.group";
public static final String WITHDRAWAL_REINSTATE_GROUP = "qaevents.withdraw-reinstate.group";
private String id;
private String topic;
@@ -75,12 +73,12 @@ public class WithdrawnCorrectionType implements CorrectionType, InitializingBean
}
private boolean currentUserIsMemberOfwithdrawalReinstateGroup(Context context) throws SQLException {
String withdrawalReinstateGroupUUID = configurationService.getProperty(WITHDRAWAL_REINSTATE_GROUP);
if (StringUtils.isBlank(withdrawalReinstateGroupUUID)) {
String groupName = configurationService.getProperty(WITHDRAWAL_REINSTATE_GROUP);
if (StringUtils.isBlank(groupName)) {
return false;
}
Group withdrawalReinstateGroup = groupService.find(context, UUID.fromString(withdrawalReinstateGroupUUID));
return Objects.nonNull(withdrawalReinstateGroup) && groupService.isMember(context, withdrawalReinstateGroup);
Group withdrawalReinstateGroup = groupService.findByName(context, groupName);
return withdrawalReinstateGroup != null && groupService.isMember(context, withdrawalReinstateGroup);
}
@Override

View File

@@ -23,6 +23,7 @@ import java.util.UUID;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import org.apache.commons.lang3.ArrayUtils;
@@ -70,6 +71,8 @@ public class QAEventServiceImpl implements QAEventService {
private static final Logger log = LoggerFactory.getLogger(QAEventServiceImpl.class);
public static final String QAEVENTS_SOURCES = "qaevents.sources";
@Autowired(required = true)
protected ConfigurationService configurationService;
@@ -328,11 +331,12 @@ public class QAEventServiceImpl implements QAEventService {
*/
public void sentEmailToAdminAboutNewRequest(QAEvent qaEvent) {
try {
String uiUrl = configurationService.getProperty("dspace.ui.url");
Email email = Email.getEmail(I18nUtil.getEmailFilename(Locale.getDefault(), "qaevent_admin_notification"));
email.addRecipient(configurationService.getProperty("qaevent.mail.notification"));
email.addRecipient(configurationService.getProperty("qaevents.mail.notification"));
email.addArgument(qaEvent.getTopic());
email.addArgument(qaEvent.getTarget());
email.addArgument(qaEvent.getMessage());
email.addArgument(uiUrl + "/items/" + qaEvent.getTarget());
email.addArgument(parsJson(qaEvent.getMessage()));
email.send();
} catch (Exception e) {
log.warn("Error during sending email of Withdrawn/Reinstate request for item with uuid:"
@@ -340,6 +344,17 @@ public class QAEventServiceImpl implements QAEventService {
}
}
private String parsJson(String jsonString) {
try {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonString);
return jsonNode.get("reason").asText();
} catch (Exception e) {
log.warn("Unable to parse the JSON:" + jsonString);
return jsonString;
}
}
@Override
public QAEvent findEventByEventId(Context context, String eventId) {
SolrQuery solrQuery = new SolrQuery("*:*");
@@ -595,7 +610,7 @@ public class QAEventServiceImpl implements QAEventService {
}
private String[] getSupportedSources() {
return configurationService.getArrayProperty("qaevent.sources", new String[] { QAEvent.OPENAIRE_SOURCE });
return configurationService.getArrayProperty(QAEVENTS_SOURCES, new String[] { QAEvent.OPENAIRE_SOURCE });
}
@Override

View File

@@ -174,6 +174,10 @@ public class QAEventRestRepository extends DSpaceRestRepository<QAEventRest, Str
throw new UnprocessableEntityException("The given correction type in the request is not valid!");
}
if (!correctionType.isAllowed(context, targetItem)) {
throw new UnprocessableEntityException("This item cannot be processed by this correction type!");
}
ObjectMapper mapper = new ObjectMapper();
CorrectionTypeMessageDTO reason = null;
try {

View File

@@ -13,6 +13,7 @@ import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasNoJsonPath;
import static org.dspace.app.rest.matcher.QAEventMatcher.matchQAEventEntry;
import static org.dspace.content.QAEvent.DSPACE_USERS_SOURCE;
import static org.dspace.content.QAEvent.OPENAIRE_SOURCE;
import static org.dspace.correctiontype.WithdrawnCorrectionType.WITHDRAWAL_REINSTATE_GROUP;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasSize;
@@ -41,7 +42,9 @@ import org.dspace.app.rest.model.patch.ReplaceOperation;
import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
import org.dspace.builder.CollectionBuilder;
import org.dspace.builder.CommunityBuilder;
import org.dspace.builder.EPersonBuilder;
import org.dspace.builder.EntityTypeBuilder;
import org.dspace.builder.GroupBuilder;
import org.dspace.builder.ItemBuilder;
import org.dspace.builder.QAEventBuilder;
import org.dspace.builder.RelationshipTypeBuilder;
@@ -50,9 +53,12 @@ import org.dspace.content.EntityType;
import org.dspace.content.Item;
import org.dspace.content.QAEvent;
import org.dspace.content.QAEventProcessed;
import org.dspace.eperson.EPerson;
import org.dspace.eperson.Group;
import org.dspace.qaevent.QANotifyPatterns;
import org.dspace.qaevent.dao.QAEventsDAO;
import org.dspace.qaevent.service.dto.CorrectionTypeMessageDTO;
import org.dspace.services.ConfigurationService;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -67,6 +73,8 @@ public class QAEventRestRepositoryIT extends AbstractControllerIntegrationTest {
@Autowired
private QAEventsDAO qaEventsDao;
@Autowired
private ConfigurationService configurationService;
@Test
public void findAllNotImplementedTest() throws Exception {
@@ -808,7 +816,7 @@ public class QAEventRestRepositoryIT extends AbstractControllerIntegrationTest {
.withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144300\"}")
.build();
QAEvent event2 = QAEventBuilder.createTarget(context, col1, "Science and Freedom")
QAEventBuilder.createTarget(context, col1, "Science and Freedom")
.withTopic("ENRICH/MISSING/PID")
.withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144300\"}")
.build();
@@ -862,6 +870,7 @@ public class QAEventRestRepositoryIT extends AbstractControllerIntegrationTest {
@Test
public void createQAEventByCorrectionTypeWithdrawnRequestTest() throws Exception {
context.turnOffAuthorisationSystem();
configurationService.setProperty(WITHDRAWAL_REINSTATE_GROUP, "Anonymous");
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
@@ -926,6 +935,7 @@ public class QAEventRestRepositoryIT extends AbstractControllerIntegrationTest {
@Test
public void createQAEventByCorrectionTypeReinstateRequestTest() throws Exception {
context.turnOffAuthorisationSystem();
configurationService.setProperty(WITHDRAWAL_REINSTATE_GROUP, "Anonymous");
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
@@ -989,4 +999,86 @@ public class QAEventRestRepositoryIT extends AbstractControllerIntegrationTest {
.andExpect(jsonPath("$.withdrawn", is(false)));
}
@Test
public void createQAEventOnlyUserPresentInWithdrawalReinstateGroupTest() throws Exception {
context.turnOffAuthorisationSystem();
EPerson user1 = EPersonBuilder.createEPerson(context)
.withEmail("eperson-test@mail.com")
.withPassword(password)
.build();
Group withdrawalGroup = GroupBuilder.createGroup(context)
.withName("WithdrawGroup")
.addMember(user1)
.build();
configurationService.setProperty(WITHDRAWAL_REINSTATE_GROUP, withdrawalGroup.getName());
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Collection col = CollectionBuilder.createCollection(context, parentCommunity)
.withName("Collection for Publications")
.withEntityType("Publication")
.build();
Item publication = ItemBuilder.createItem(context, col)
.withTitle("Publication archived item")
.build();
context.restoreAuthSystemState();
String adminToken = getAuthToken(admin.getEmail(), password);
AtomicReference<String> idRef = new AtomicReference<String>();
CorrectionTypeMessageDTO message = new CorrectionTypeMessageDTO("reasone");
String ePersonToken = getAuthToken(eperson.getEmail(), password);
// eperson is not present into the withdraw-reinstate group
// and so cannot make the request
getClient(ePersonToken).perform(post("/api/integration/qualityassuranceevents")
.param("correctionType", "request-withdrawn")
.param("target", publication.getID().toString())
.content(new ObjectMapper().writeValueAsBytes(message))
.contentType(contentType))
.andExpect(status().isUnprocessableEntity());
String user1Token = getAuthToken(user1.getEmail(), password);
// instead user1 is present into the withdraw-reinstate group
getClient(user1Token).perform(post("/api/integration/qualityassuranceevents")
.param("correctionType", "request-withdrawn")
.param("target", publication.getID().toString())
.content(new ObjectMapper().writeValueAsBytes(message))
.contentType(contentType))
.andExpect(status().isCreated())
.andDo(result -> idRef.set(read(result.getResponse().getContentAsString(), "$.id")));
getClient(adminToken).perform(get("/api/integration/qualityassuranceevents/" + idRef.get()))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is(idRef.get())))
.andExpect(jsonPath("$.source", is(DSPACE_USERS_SOURCE)))
.andExpect(jsonPath("$.title", is(publication.getName())))
.andExpect(jsonPath("$.topic", is("REQUEST/WITHDRAWN")))
.andExpect(jsonPath("$.trust", is("1.000")))
.andExpect(jsonPath("$.status", is("PENDING")));
getClient(adminToken).perform(get("/api/core/items/" + publication.getID()))
.andExpect(status().isOk())
.andExpect(jsonPath("$.inArchive", is(true)))
.andExpect(jsonPath("$.withdrawn", is(false)));
List<Operation> acceptOp = new ArrayList<Operation>();
acceptOp.add(new ReplaceOperation("/status", QAEvent.ACCEPTED));
getClient(adminToken).perform(patch("/api/integration/qualityassuranceevents/" + idRef.get())
.content(getPatchContent(acceptOp))
.contentType(MediaType.APPLICATION_JSON_PATCH_JSON))
.andExpect(status().isOk());
getClient(adminToken).perform(get("/api/core/items/" + publication.getID()))
.andExpect(status().isOk())
.andExpect(jsonPath("$.inArchive", is(false)))
.andExpect(jsonPath("$.withdrawn", is(true)));
}
}

View File

@@ -13,6 +13,8 @@ import static org.hamcrest.Matchers.emptyOrNullString;
import static org.hamcrest.Matchers.is;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -49,10 +51,11 @@ public class QAEventMatcher {
public static Matcher<? super Object> matchQAEventEntry(QAEvent event) {
try {
ObjectMapper jsonMapper = new JsonMapper();
DecimalFormat decimalFormat = new DecimalFormat("0.000", new DecimalFormatSymbols(Locale.ENGLISH));
return allOf(hasJsonPath("$.id", is(event.getEventId())),
hasJsonPath("$.originalId", is(event.getOriginalId())),
hasJsonPath("$.title", is(event.getTitle())),
hasJsonPath("$.trust", is(new DecimalFormat("0.000").format(event.getTrust()))),
hasJsonPath("$.trust", is(decimalFormat.format(event.getTrust()))),
hasJsonPath("$.status", Matchers.equalToIgnoringCase(event.getStatus())),
hasJsonPath("$.message",
matchMessage(event.getTopic(), jsonMapper.readValue(event.getMessage(),

View File

@@ -9,5 +9,5 @@
Item Details:
Type of request: ${params[0]}
Relatem to item with uuid: ${params[1]}
Relatem to item: ${params[1]}
Reason: ${params[2]}