Skip to content

Commit

Permalink
fix: Add contribution link to the achievements exported file - MEED-7835
Browse files Browse the repository at this point in the history
 - Meeds-io/meeds#2601 (#1794)

This PR will add contribution link to the achievements exported file.
  • Loading branch information
AzmiTouil authored Dec 17, 2024
1 parent 640ee7e commit 8f86377
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -47,6 +50,18 @@ public abstract class EventPlugin extends BaseComponentPlugin {
*/
public abstract boolean isValidEvent(Map<String, String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";

Expand Down Expand Up @@ -850,21 +851,46 @@ 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) {
LOG.error("Error when computing to XLSX ", e);
}
}

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());
Expand Down
16 changes: 16 additions & 0 deletions services/src/main/java/io/meeds/gamification/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit 8f86377

Please sign in to comment.