From 8f8637789e03b46656ebba95d3c7b7ca81532c3e Mon Sep 17 00:00:00 2001 From: Azmi TOUIL <42934070+AzmiTouil@users.noreply.github.com> Date: Tue, 17 Dec 2024 16:54:42 +0100 Subject: [PATCH] fix: Add contribution link to the achievements exported file - MEED-7835 - Meeds-io/meeds#2601 (#1794) This PR will add contribution link to the achievements exported file. --- .../gamification/plugin/EventPlugin.java | 15 ++++++++ .../service/impl/RealizationServiceImpl.java | 36 ++++++++++++++++--- .../io/meeds/gamification/utils/Utils.java | 16 +++++++++ .../locale/addon/Gamification_en.properties | 1 + .../service/RealizationServiceTest.java | 11 +++--- 5 files changed, 70 insertions(+), 9 deletions(-) diff --git a/services/src/main/java/io/meeds/gamification/plugin/EventPlugin.java b/services/src/main/java/io/meeds/gamification/plugin/EventPlugin.java index 9bbd236b5f..3681f34679 100644 --- a/services/src/main/java/io/meeds/gamification/plugin/EventPlugin.java +++ b/services/src/main/java/io/meeds/gamification/plugin/EventPlugin.java @@ -18,12 +18,15 @@ */ package io.meeds.gamification.plugin; +import io.meeds.gamification.model.RealizationDTO; import io.meeds.gamification.service.EventService; import org.exoplatform.container.component.BaseComponentPlugin; +import org.exoplatform.social.core.service.LinkProvider; import java.util.List; import java.util.Map; +import static io.meeds.gamification.utils.Utils.getBaseUrl; import static io.meeds.gamification.utils.Utils.stringToMap; /** @@ -47,6 +50,18 @@ public abstract class EventPlugin extends BaseComponentPlugin { */ public abstract boolean isValidEvent(Map eventProperties, String triggerDetails); + /** + * get contribution link + * + * @return the contribution link + */ + public String getLink(RealizationDTO realizationDTO) { + if (realizationDTO.getObjectId() != null && (realizationDTO.getObjectId().startsWith("http://") || realizationDTO.getObjectId().startsWith("https://"))) { + return realizationDTO.getObjectId(); + } + return getBaseUrl() + LinkProvider.getRedirectUri("activity?id=" + realizationDTO.getObjectId()); + } + /** * get points ration using event properties and properties coming from an * external trigger diff --git a/services/src/main/java/io/meeds/gamification/service/impl/RealizationServiceImpl.java b/services/src/main/java/io/meeds/gamification/service/impl/RealizationServiceImpl.java index dae17b3119..21a6fd8d1c 100644 --- a/services/src/main/java/io/meeds/gamification/service/impl/RealizationServiceImpl.java +++ b/services/src/main/java/io/meeds/gamification/service/impl/RealizationServiceImpl.java @@ -29,14 +29,15 @@ import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.SystemUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.poi.ss.usermodel.CreationHelper; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.common.usermodel.HyperlinkType; +import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.exoplatform.commons.api.notification.NotificationContext; import org.exoplatform.commons.api.notification.model.PluginKey; import org.exoplatform.commons.notification.impl.NotificationContextImpl; +import org.exoplatform.commons.notification.template.TemplateUtils; +import org.exoplatform.social.core.service.LinkProvider; import org.picocontainer.Startable; import org.exoplatform.commons.api.persistence.ExoTransactional; @@ -68,7 +69,7 @@ public class RealizationServiceImpl implements RealizationService, Startable { // File header private static final String[] COLUMNS = new String[] { "date", "grantee", "actionType", "programLabel", - "actionLabel", "points", "status" }; + "actionLabel", "contribution", "points", "status" }; private static final String SHEETNAME = "Achivements Report"; @@ -850,14 +851,16 @@ private void appendRealizationRow(Sheet sheet, int rowIndex, CreationHelper help ruleService.findRuleByTitle(realization.getActionTitle()); String eventTitle = rule == null || rule.getEvent() == null ? null : rule.getEvent().getTitle(); + String eventTrigger = rule == null || rule.getEvent() == null ? null : rule.getEvent().getTrigger(); String actionLabel = realization.getActionTitle() != null ? realization.getActionTitle() : eventTitle; String programTitle = escapeIllegalCharacterInMessage(realization.getProgramLabel()); int cellIndex = 0; row.createCell(cellIndex++).setCellValue(helper.createRichTextString(realization.getCreatedDate())); - row.createCell(cellIndex++).setCellValue(Utils.getUserFullName(realization.getEarnerId())); + row.createCell(cellIndex++).setCellValue(getUserFullName(realization.getEarnerId())); row.createCell(cellIndex++).setCellValue(rule != null ? rule.getType().name() : "-"); row.createCell(cellIndex++).setCellValue(programTitle); row.createCell(cellIndex++).setCellValue(actionLabel); + appendContributionCells(row, cellIndex++, helper, realization, eventTrigger); row.createCell(cellIndex++).setCellValue(realization.getActionScore()); row.createCell(cellIndex).setCellValue(realization.getStatus()); } catch (Exception e) { @@ -865,6 +868,29 @@ private void appendRealizationRow(Sheet sheet, int rowIndex, CreationHelper help } } + private void appendContributionCells(Row row, int cellIndex, CreationHelper helper, RealizationDTO realization, String eventTrigger) { + Cell hashCell = row.createCell(cellIndex); + String contributionURL = null; + if (realization.getType() == EntityType.MANUAL) { + Long activityId = realization.getActivityId(); + if (activityId != null && activityId > 0) { + contributionURL = Utils.getBaseUrl() + LinkProvider.getRedirectUri("activity?id=" + activityId); + } + hashCell.setCellValue(TemplateUtils.cleanHtmlTags(realization.getComment())); + } else { + EventPlugin eventPlugin = eventService.getEventPlugin(eventTrigger); + if (eventPlugin != null) { + contributionURL = eventPlugin.getLink(realization); + } + hashCell.setCellValue(contributionURL != null ? contributionURL : ""); + } + if (contributionURL != null && (contributionURL.startsWith("http://") || contributionURL.startsWith("https://"))) { + Hyperlink link = helper.createHyperlink(HyperlinkType.URL); + link.setAddress(contributionURL); + hashCell.setHyperlink(link); + } + } + private File createTempFile(String fileName) throws IOException { SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd_HH-mm-ss"); fileName += formatter.format(new Date()); diff --git a/services/src/main/java/io/meeds/gamification/utils/Utils.java b/services/src/main/java/io/meeds/gamification/utils/Utils.java index 37348dbce9..5fee70cc96 100644 --- a/services/src/main/java/io/meeds/gamification/utils/Utils.java +++ b/services/src/main/java/io/meeds/gamification/utils/Utils.java @@ -25,6 +25,7 @@ import java.util.Map; import java.util.TimeZone; +import jakarta.servlet.http.HttpServletRequest; import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.StringEscapeUtils; @@ -35,6 +36,7 @@ import org.exoplatform.services.listener.ListenerService; import org.exoplatform.services.log.ExoLogger; import org.exoplatform.services.log.Log; +import org.exoplatform.services.rest.impl.EnvironmentContext; import org.exoplatform.services.security.Authenticator; import org.exoplatform.services.security.ConversationState; import org.exoplatform.services.security.IdentityConstants; @@ -503,6 +505,20 @@ public static String getBaseURLProgramRest() { + BASE_URL_PROGRAMS_REST_API; } + public static HttpServletRequest getCurrentServletRequest() { + EnvironmentContext environmentContext = EnvironmentContext.getCurrent(); + return (HttpServletRequest) environmentContext.get(HttpServletRequest.class); + } + + public static String getBaseUrl() { + HttpServletRequest currentServletRequest = getCurrentServletRequest(); + String scheme = currentServletRequest.getScheme(); + String serverName = currentServletRequest.getServerName(); + int serverPort = currentServletRequest.getServerPort(); + boolean isDefaultPort = (scheme.equals("http") && serverPort == 80) || (scheme.equals("https") && serverPort == 443); + return isDefaultPort ? scheme + "://" + serverName : scheme + "://" + serverName + ":" + serverPort; + } + public static void broadcastEvent(ListenerService listenerService, String eventName, Object source, Object data) { try { listenerService.broadcast(eventName, source, data); diff --git a/services/src/main/resources/locale/addon/Gamification_en.properties b/services/src/main/resources/locale/addon/Gamification_en.properties index 9cd494e473..f66b37fef1 100644 --- a/services/src/main/resources/locale/addon/Gamification_en.properties +++ b/services/src/main/resources/locale/addon/Gamification_en.properties @@ -317,6 +317,7 @@ realization.label.grantee=Contributor realization.label.reviewer=Reviewer realization.label.actionId=Action ID realization.label.actionLabel=Action +realization.label.contribution=Contribution realization.label.actionType=Type realization.label.program=Program ID realization.label.programLabel=Program diff --git a/services/src/test/java/io/meeds/gamification/service/RealizationServiceTest.java b/services/src/test/java/io/meeds/gamification/service/RealizationServiceTest.java index 1cd1c8c6bb..db2952a6bb 100644 --- a/services/src/test/java/io/meeds/gamification/service/RealizationServiceTest.java +++ b/services/src/test/java/io/meeds/gamification/service/RealizationServiceTest.java @@ -824,43 +824,46 @@ public void testExportRealizations() throws Exception { // NOSONAR assertEquals(3, sheet.getLastRowNum()); Row header = sheet.getRow(0); assertNotNull(header); - assertEquals(7, header.getLastCellNum()); + assertEquals(8, header.getLastCellNum()); assertTrue(StringUtils.isNotBlank(header.getCell(header.getFirstCellNum()).getStringCellValue())); assertTrue(StringUtils.isNotBlank(header.getCell(header.getLastCellNum() - 1).getStringCellValue())); Row row1 = sheet.getRow(1); assertNotNull(row1); - assertEquals(7, row1.getLastCellNum()); + assertEquals(8, row1.getLastCellNum()); int cellIndex = 0; assertEquals(realization1.getCreatedDate(), row1.getCell(cellIndex++).getStringCellValue()); assertEquals(Utils.getUserFullName(realization1.getEarnerId()), row1.getCell(cellIndex++).getStringCellValue()); assertEquals(rule.getType().name(), row1.getCell(cellIndex++).getStringCellValue()); assertEquals(realization1.getProgramLabel(), row1.getCell(cellIndex++).getStringCellValue()); assertEquals(realization1.getActionTitle(), row1.getCell(cellIndex++).getStringCellValue()); + assertEquals("", row1.getCell(cellIndex++).getStringCellValue()); assertEquals(realization1.getActionScore(), row1.getCell(cellIndex++).getNumericCellValue(), 0d); assertEquals(realization1.getStatus(), row1.getCell(cellIndex).getStringCellValue()); Row row2 = sheet.getRow(2); assertNotNull(row2); - assertEquals(7, row2.getLastCellNum()); + assertEquals(8, row2.getLastCellNum()); cellIndex = 0; assertEquals(realization2.getCreatedDate(), row2.getCell(cellIndex++).getStringCellValue()); assertEquals(Utils.getUserFullName(realization2.getEarnerId()), row2.getCell(cellIndex++).getStringCellValue()); assertEquals(rule.getType().name(), row2.getCell(cellIndex++).getStringCellValue()); assertEquals(realization2.getProgramLabel(), row2.getCell(cellIndex++).getStringCellValue()); assertEquals(realization2.getActionTitle(), row2.getCell(cellIndex++).getStringCellValue()); + assertEquals("", row2.getCell(cellIndex++).getStringCellValue()); assertEquals(realization2.getActionScore(), row2.getCell(cellIndex++).getNumericCellValue(), 0d); assertEquals(realization2.getStatus(), row2.getCell(cellIndex).getStringCellValue()); Row row3 = sheet.getRow(3); assertNotNull(row3); - assertEquals(7, row3.getLastCellNum()); + assertEquals(8, row3.getLastCellNum()); cellIndex = 0; assertEquals(realization3.getCreatedDate(), row3.getCell(cellIndex++).getStringCellValue()); assertEquals(Utils.getUserFullName(realization3.getEarnerId()), row3.getCell(cellIndex++).getStringCellValue()); assertEquals(EntityType.MANUAL.name(), row3.getCell(cellIndex++).getStringCellValue()); assertEquals(realization3.getProgramLabel(), row3.getCell(cellIndex++).getStringCellValue()); assertEquals(realization3.getActionTitle(), row3.getCell(cellIndex++).getStringCellValue()); + assertEquals("", row3.getCell(cellIndex++).getStringCellValue()); assertEquals(realization3.getActionScore(), row3.getCell(cellIndex++).getNumericCellValue(), 0d); assertEquals(realization3.getStatus(), row3.getCell(cellIndex).getStringCellValue()); }