Skip to content

Commit

Permalink
Step two move logic into the service.
Browse files Browse the repository at this point in the history
  • Loading branch information
csev committed Jan 16, 2025
1 parent c716f21 commit 994c690
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1012,76 +1012,13 @@ private Assignment mergeAssignment(final String siteId, final Element element, f
if ( assignmentTitle == null ) return null;
if ( assignmentTitles.contains(assignmentTitle) ) return null;

// Check is there is a content item in this assignment
Map<String, Object> content = null;
Map<String, Object> tool = null;
NodeList nl = element.getElementsByTagName("sakai-lti-content");
if ( nl.getLength() >= 1 ) {
Node toolNode = nl.item(0);
if ( toolNode.getNodeType() == Node.ELEMENT_NODE ) {
Element toolElement = (Element) toolNode;
content = new HashMap();
tool = new HashMap();
SakaiLTIUtil.mergeContent(toolElement, content, tool);
String contentErrors = ltiService.validateContent(content);
if ( contentErrors != null ) {
log.warn("import found invalid sakai-lti-content "+contentErrors);
content = null;
}

String toolErrors = ltiService.validateTool(tool);
if ( toolErrors != null ) {
log.warn("import found invalid sakai-lti-tool "+toolErrors);
tool = null;
}
}
}

if (assignmentFromXml != null) {
assignmentFromXml.setId(null);
assignmentFromXml.setContext(siteId);
assignmentFromXml.setContentId(null);

// Lets find the right tool to assiociate with
// See also lessonbuilder/tool/src/java/org/sakaiproject/lessonbuildertool/service/BltiEntity.java
String launchUrl = content != null ? (String) content.get(LTIService.LTI_LAUNCH) : null;
log.debug("LTI Assignment {}",launchUrl);
if ( content != null && launchUrl != null && tool != null ) {
String toolCheckSum = (String) tool.get(LTIService.SAKAI_TOOL_CHECKSUM);
List<Map<String,Object>> tools = ltiService.getTools(null,null,0,0, siteId);
Map<String, Object> theTool = SakaiLTIUtil.findBestToolMatch(launchUrl, toolCheckSum, tools);
if ( theTool == null ) {
Object result = ltiService.insertTool(tool, siteId);
if ( result instanceof String ) {
log.info("Could not insert tool - "+result);
}
if ( result instanceof Long ) theTool = ltiService.getTool((Long) result, siteId);
}

Map<String, Object> theContent = null;
if ( theTool == null ) {
log.info("No tool to associate to content item - "+launchUrl);
} else {
Long toolId = Foorm.getLongNull(theTool.get(LTIService.LTI_ID));
log.debug("Matched toolId={} for launchUrl={}", toolId, launchUrl);
content.put(LTIService.LTI_TOOL_ID, toolId.intValue());
Object result = ltiService.insertContent(Foorm.convertToProperties(content), siteId);
if ( result instanceof String ) {
log.info("Could not insert content - "+result);
}
if ( result instanceof Long ) {
theContent = ltiService.getContent((Long) result, siteId);
if ( theContent == null) {
log.warn("Could not re-retrieve inserted content item "+launchUrl);
} else {
Long contentKey = Foorm.getLongNull(theContent.get(LTIService.LTI_ID));
log.debug("Created contentKey={} for launchUrl={}", contentKey, launchUrl);
if ( contentKey != null ) assignmentFromXml.setContentId(contentKey.intValue());
}
}
}

}
Long contentKey = ltiService.mergeContentFromImport(element, siteId);
if ( contentKey != null ) assignmentFromXml.setContentId(contentKey.intValue());

if (serverConfigurationService.getBoolean(SAK_PROP_ASSIGNMENT_IMPORT_SUBMISSIONS, false)) {
Set<AssignmentSubmission> submissions = assignmentFromXml.getSubmissions();
Expand Down
10 changes: 9 additions & 1 deletion lti/lti-api/src/java/org/sakaiproject/lti/api/LTIService.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ public interface LTIService extends LTISubstitutionsFilter {

// Checksum for import and export
String SAKAI_TOOL_CHECKSUM = "sakai_tool_checksum";
String ARCHIVE_LTI_CONTENT_TAG = "sakai-lti-content";
String ARCHIVE_LTI_TOOL_TAG = "sakai-lti-tool";

// For Instructors, this model is filtered down dynamically based on
// Tool settings
Expand Down Expand Up @@ -564,11 +566,17 @@ public interface LTIService extends LTISubstitutionsFilter {
boolean toolDeployed(Long toolKey, String siteId);

/**
* Include an LTI ContentItem in a Sakai Archive
* Include an LTI content item and its tool in a Sakai Archive
* @param siteId
*/
Element archiveContentByKey(Document doc, Long contentKey, String siteId);

/**
* Import a content item and link it to an existing or new tool
* @param siteId
*/
Long mergeContentFromImport(Element element, String siteId);

/**
* Copy an LTI Content Item from an old site into a new site
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,6 @@ public class SakaiLTIUtil {
"urn:lti:instrole:ims/lis/Administrator=http://purl.imsglobal.org/vocab/lis/v2/institution/person#Administrator;"
;

public static final String ARCHIVE_LTI_CONTENT_TAG = "sakai-lti-content";
public static final String ARCHIVE_LTI_TOOL_TAG = "sakai-lti-tool";

public static boolean rosterEnabled() {
String allowRoster = ServerConfigurationService.getString(LTI_ROSTER_ENABLED, LTI_ROSTER_ENABLED_DEFAULT);
return "true".equals(allowRoster);
Expand Down Expand Up @@ -3586,7 +3583,7 @@ public static String getToolTitle(Map<String, Object> tool, Map<String, Object>
}

public static Element archiveTool(Document doc, Map<String, Object> tool) {
Element retval = Foorm.archiveThing(doc, ARCHIVE_LTI_TOOL_TAG, LTIService.TOOL_MODEL, tool);
Element retval = Foorm.archiveThing(doc, LTIService.ARCHIVE_LTI_TOOL_TAG, LTIService.TOOL_MODEL, tool);
String checksum = computeToolCheckSum(tool);
if ( checksum != null ) {
Element newElement = doc.createElement(LTIService.SAKAI_TOOL_CHECKSUM);
Expand All @@ -3597,7 +3594,7 @@ public static Element archiveTool(Document doc, Map<String, Object> tool) {
}

public static Element archiveContent(Document doc, Map<String, Object> content, Map<String, Object> tool) {
Element retval = Foorm.archiveThing(doc, ARCHIVE_LTI_CONTENT_TAG, LTIService.CONTENT_MODEL, content);
Element retval = Foorm.archiveThing(doc, LTIService.ARCHIVE_LTI_CONTENT_TAG, LTIService.CONTENT_MODEL, content);

if ( tool != null ) {
Element toolElement = archiveTool(doc, tool);
Expand All @@ -3613,7 +3610,7 @@ public static void mergeTool(Element element, Map<String, Object> tool) {
public static void mergeContent(Element element, Map<String, Object> content, Map<String, Object> tool) {
Foorm.mergeThing(element, LTIService.CONTENT_MODEL, content);
if ( tool != null ) {
NodeList nl = element.getElementsByTagName(ARCHIVE_LTI_TOOL_TAG);
NodeList nl = element.getElementsByTagName(LTIService.ARCHIVE_LTI_TOOL_TAG);
if ( nl.getLength() >= 1 ) {
Node toolNode = nl.item(0);
if ( toolNode.getNodeType() == Node.ELEMENT_NODE ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;

import org.json.simple.JSONObject;

Expand Down Expand Up @@ -1065,6 +1067,81 @@ public Element archiveContentByKey(Document doc, Long contentKey, String siteId)
return retval;
}

@Override
public Long mergeContentFromImport(Element element, String siteId) {

Map<String, Object> content = null;
Map<String, Object> tool = null;
NodeList nl = element.getElementsByTagName(LTIService.ARCHIVE_LTI_CONTENT_TAG);
if ( nl.getLength() >= 1 ) {
Node toolNode = nl.item(0);
if ( toolNode.getNodeType() == Node.ELEMENT_NODE ) {
Element toolElement = (Element) toolNode;
content = new HashMap();
tool = new HashMap();
SakaiLTIUtil.mergeContent(toolElement, content, tool);
String contentErrors = this.validateContent(content);
if ( contentErrors != null ) {
log.warn("import found invalid content tag "+contentErrors);
return null;
}

String toolErrors = this.validateTool(tool);
if ( toolErrors != null ) {
log.warn("import found invalid tool tag "+toolErrors);
return null;
}
}
}

// Lets find the right tool to assiociate with
// See also lessonbuilder/tool/src/java/org/sakaiproject/lessonbuildertool/service/BltiEntity.java
String launchUrl = content != null ? (String) content.get(LTIService.LTI_LAUNCH) : null;
if ( launchUrl == null ) {
log.warn("lti content import could not find launch url");
return null;
}

log.debug("LTI Import launchUrl {}",launchUrl);
String toolCheckSum = (String) tool.get(LTIService.SAKAI_TOOL_CHECKSUM);
List<Map<String,Object>> tools = this.getTools(null,null,0,0, siteId);
Map<String, Object> theTool = SakaiLTIUtil.findBestToolMatch(launchUrl, toolCheckSum, tools);
if ( theTool == null ) {
Object result = this.insertTool(tool, siteId);
if ( ! (result instanceof Long) ) {
log.info("Could not insert tool - "+result);
return null;
}
theTool = this.getTool((Long) result, siteId);
}

Map<String, Object> theContent = null;
if ( theTool == null ) {
log.info("No tool to associate to content item - "+launchUrl);
return null;
} else {
Long toolId = Foorm.getLongNull(theTool.get(LTIService.LTI_ID));
log.debug("Matched toolId={} for launchUrl={}", toolId, launchUrl);
content.put(LTIService.LTI_TOOL_ID, toolId.intValue());
Object result = this.insertContent(Foorm.convertToProperties(content), siteId);
if ( ! (result instanceof Long) ) {
log.info("Could not insert content - "+result);
return null;
}

theContent = this.getContent((Long) result, siteId);
if ( theContent == null) {
log.warn("Could not re-retrieve inserted content item "+launchUrl);
return null;
} else {
Long contentKey = Foorm.getLongNull(theContent.get(LTIService.LTI_ID));
log.debug("Created contentKey={} for launchUrl={}", contentKey, launchUrl);
return contentKey;
}
}

}

@Override
public Object copyLTIContent(Long contentKey, String siteId, String oldSiteId)
{
Expand Down

0 comments on commit 994c690

Please sign in to comment.