Skip to content

Commit

Permalink
Support transversalMap per Earle's review
Browse files Browse the repository at this point in the history
  • Loading branch information
csev committed Feb 4, 2025
1 parent 183bacc commit 7dfad52
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Stack;
Expand Down Expand Up @@ -1480,6 +1481,8 @@ protected String getSummaryFromHeader(Message item, MessageHeader header) {
*/
public Map<String, String> transferCopyEntities(String fromContext, String toContext, List<String> resourceIds, List<String> options) {

Map<String, String> transversalMap = new HashMap<>();

// get the channel associated with this site
String oChannelRef = channelReference(fromContext, SiteService.MAIN_CONTAINER);
AnnouncementChannel oChannel = null;
Expand Down Expand Up @@ -1555,7 +1558,7 @@ public Map<String, String> transferCopyEntities(String fromContext, String toCon
// the "to" message
nMessage = (AnnouncementMessageEdit) nChannel.addMessage();
String newBody = oMessage.getBody();
newBody = ltiService.fixLtiLaunchUrls(newBody, fromContext, toContext);
newBody = ltiService.fixLtiLaunchUrls(newBody, fromContext, toContext, transversalMap);
newBody = linkMigrationHelper.migrateOneLink(fromContext, toContext, newBody);
nMessage.setBody(newBody);
// message header
Expand Down Expand Up @@ -1674,8 +1677,8 @@ public Map<String, String> transferCopyEntities(String fromContext, String toCon
{
log.warn(".importResources(): exception in handling {} : {}", serviceName(), any);
}
return null;

return transversalMap;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4254,7 +4254,7 @@ public Map<String, String> transferCopyEntities(String fromContext, String toCon
nAssignment.setTitle(oAssignment.getTitle());
// replace all occurrence of old context with new context inside instruction text
if(StringUtils.isNotBlank(oAssignment.getInstructions())){
String newInstructions = ltiService.fixLtiLaunchUrls(oAssignment.getInstructions(), fromContext, toContext);
String newInstructions = ltiService.fixLtiLaunchUrls(oAssignment.getInstructions(), fromContext, toContext, transversalMap);
nAssignment.setInstructions(newInstructions.replaceAll(fromContext, toContext));
}
nAssignment.setTypeOfGrade(oAssignment.getTypeOfGrade());
Expand Down Expand Up @@ -4616,7 +4616,7 @@ public Map<String, String> transferCopyEntities(String fromContext, String toCon
}
transversalMap.putAll(transferCopyEntities(fromContext, toContext, ids, transferOptions));
} catch (Exception e) {
log.info("End removing Assignmentt data {}", e.getMessage());
log.info("End removing Assignment data {}", e.getMessage());
}

return transversalMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1918,7 +1918,7 @@ public Map<String, String> transferCopyEntities(String fromContext, String toCon
continue;

String description = oEvent.getDescriptionFormatted();
description = ltiService.fixLtiLaunchUrls(description, fromContext, toContext);
description = ltiService.fixLtiLaunchUrls(description, fromContext, toContext, transversalMap);
oEvent.setDescriptionFormatted(description);

CalendarEvent e = nCalendar.addEvent(oEvent.getRange(), oEvent.getDisplayName(), oEvent.getDescriptionFormatted(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2627,7 +2627,7 @@ public Map<String, String> transferCopyEntities(String fromContext, String toCon

newBean.id = fromBean.id;
newBean.title = fromBean.title;
newBean.message = ltiService.fixLtiLaunchUrls(fromBean.message, fromContext, toContext);
newBean.message = ltiService.fixLtiLaunchUrls(fromBean.message, fromContext, toContext, traversalMap);
newBean.siteId = toContext;
newBean.draft = true;
newBean.type = fromBean.type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.anyString;

import static org.mockito.ArgumentMatchers.any;
import java.io.IOException;
import java.util.Properties;
import java.util.Map;
import javax.annotation.Resource;
import javax.sql.DataSource;

Expand Down Expand Up @@ -308,7 +309,7 @@ public GradingService gradingService() {
@Bean(name = "org.sakaiproject.lti.api.LTIService")
public LTIService ltiService() {
LTIService ltiService = mock(LTIService.class);
when(ltiService.fixLtiLaunchUrls(anyString(), anyString(), anyString()))
when(ltiService.fixLtiLaunchUrls(anyString(), anyString(), anyString(), any(Map.class)))
.thenAnswer(invocation -> invocation.getArgument(0));
return ltiService;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ public interface LTIService extends LTISubstitutionsFilter {
* @param toContext The destination context
* @return The text with updated LTI launch URLs
*/
String fixLtiLaunchUrls(String text, String fromContext, String toContext);
String fixLtiLaunchUrls(String text, String fromContext, String toContext, Map<String, String> transversalMap);

/**
* Fix LTI launch URLs when copying content between contexts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1267,18 +1267,19 @@ public Long getId(Map<String, Object> thing) {
@Override
public String fixLtiLaunchUrls(String text, String toContext, Map<Long, Map<String, Object>> ltiContentItems) {
String fromContext = null;
return fixLtiLaunchUrls(text, fromContext, toContext, ltiContentItems);
Map<String, String> transversalMap = null;
return fixLtiLaunchUrls(text, fromContext, toContext, ltiContentItems, transversalMap);
}

@Override
public String fixLtiLaunchUrls(String text, String fromContext, String toContext) {
public String fixLtiLaunchUrls(String text, String fromContext, String toContext, Map<String, String> transversalMap) {
Map<Long, Map<String, Object>> ltiContentItems = null;
return fixLtiLaunchUrls(text, fromContext, toContext, ltiContentItems);
return fixLtiLaunchUrls(text, fromContext, toContext, ltiContentItems, transversalMap);
}

// http://localhost:8080/access/lti/site/7d529bf7-b856-4400-9da1-ba8670ed1489/content:1
// http://localhost:8080/access/lti/site/7d529bf7-b856-4400-9da1-ba8670ed1489/content:42
protected String fixLtiLaunchUrls(String text, String fromContext, String toContext, Map<Long, Map<String, Object>> ltiContentItems) {
protected String fixLtiLaunchUrls(String text, String fromContext, String toContext, Map<Long, Map<String, Object>> ltiContentItems, Map<String, String> transversalMap) {
if (StringUtils.isBlank(text)) return text;
List<String> urls = SakaiLTIUtil.extractLtiLaunchUrls(text);
for (String url : urls) {
Expand All @@ -1287,6 +1288,12 @@ protected String fixLtiLaunchUrls(String text, String fromContext, String toCont
String linkSiteId = pieces[0];
String linkContextId = pieces[1];

if ( transversalMap != null && transversalMap.containsKey(url) ) {
log.debug("Found transversal map entry for {} -> {}", url, transversalMap.get(url));
text = text.replace(url, transversalMap.get(url));
return text;
}

// We need to load up the content item from the old context which should be successful
Long contentKey = Long.parseLong(linkContextId);
Map<String, Object> content = this.getContent(contentKey, linkSiteId);
Expand All @@ -1312,6 +1319,7 @@ protected String fixLtiLaunchUrls(String text, String fromContext, String toCont
// Upgrade the access prefix from legacy blti to modern lti
String newUrl = baseUrl.replace(LTIService.LAUNCH_PREFIX_LEGACY, LTIService.LAUNCH_PREFIX) + toContext + "/content:" + newContentId;
text = text.replace(url, newUrl);
transversalMap.put(url, newUrl);
log.debug("Inserted content item {} in site {} newUrl {}", newContentId, toContext, newUrl);
} else {
log.error("Could not insert content item {} in site {}",contentKey,toContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ public Map<String, String> transferCopyEntities(String fromContext, String toCon
}
if (fromTopic.getExtendedDescription() != null && fromTopic.getExtendedDescription().length() > 0) {
String extendedDescription = fromTopic.getExtendedDescription();
extendedDescription = ltiService.fixLtiLaunchUrls(extendedDescription, fromContext, toContext);
extendedDescription = ltiService.fixLtiLaunchUrls(extendedDescription, fromContext, toContext, transversalMap);
newTopic.setExtendedDescription(extendedDescription);
}
newTopic.setLocked(fromTopic.getLocked());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.Stack;
import java.util.UUID;
Expand All @@ -50,9 +51,7 @@
import org.sakaiproject.entity.api.Entity;
import org.sakaiproject.entity.api.EntityManager;
import org.sakaiproject.entity.api.EntityTransferrer;
import org.sakaiproject.entity.api.HttpAccess;
import org.sakaiproject.entity.api.Reference;
import org.sakaiproject.entity.api.ResourceProperties;
import org.sakaiproject.genericdao.api.search.Order;
import org.sakaiproject.genericdao.api.search.Restriction;
import org.sakaiproject.genericdao.api.search.Search;
Expand Down Expand Up @@ -573,7 +572,7 @@ public Map<String, String> transferCopyEntities(String fromContext, String toCon

@Override
public Map<String, String> transferCopyEntities(String fromContext, String toContext, List<String> resourceIds, List<String> transferOptions) {

Map<String, String> transversalMap = new HashMap<>();
try {
for (Poll fromPoll : findAllPolls(fromContext)) {
Poll fromPollV = getPollWithVotes(fromPoll.getPollId());
Expand All @@ -589,7 +588,7 @@ public Map<String, String> transferCopyEntities(String fromContext, String toCon
toPoll.setDisplayResult(fromPollV.getDisplayResult());
toPoll.setLimitVoting(fromPollV.getLimitVoting());
String details = fromPollV.getDetails();
details = ltiService.fixLtiLaunchUrls(details, fromContext, toContext);
details = ltiService.fixLtiLaunchUrls(details, fromContext, toContext, transversalMap);
toPoll.setDetails(details);

//Guardamos toPoll para que se puedan ir añandiéndole las opciones y los votos
Expand All @@ -606,7 +605,7 @@ public Map<String, String> transferCopyEntities(String fromContext, String toCon
toOption.setOptionOrder(fromOption.getOptionOrder());

String text = fromOption.getText();
text = ltiService.fixLtiLaunchUrls(text, fromContext, toContext);
text = ltiService.fixLtiLaunchUrls(text, fromContext, toContext, transversalMap);
toOption.setText(text);

saveOption(toOption);
Expand All @@ -628,7 +627,7 @@ public Map<String, String> transferCopyEntities(String fromContext, String toCon
log.error("Failed to save transfer polls: {}", e.toString());
}

return null;
return transversalMap;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ else if (fromSyllabusItem.getRedirectURL() !=null) {
.intValue() + 1);

String assetStr = sd.getAsset();
assetStr = ltiService.fixLtiLaunchUrls(assetStr, fromContext, toContext);
assetStr = ltiService.fixLtiLaunchUrls(assetStr, fromContext, toContext, transversalMap);
sd.setAsset(assetStr);

SyllabusData newToSyData = syllabusManager
Expand Down

0 comments on commit 7dfad52

Please sign in to comment.