<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse" name="canvas_updateDiscussionTopicWithTopPosts" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence onError="faultHandlerSeq">
<!--Canvas Properties-->
<property name="canvas.apiUrl" expression="json-eval($.canvasApiUrl)" />
<property name="canvas.accessToken" expression="json-eval($.canvasAccessToken)" />
<property name="canvas.courseId" expression="json-eval($.canvasCourseId)" />
<!--Disqus Properties-->
<property name="disqus.apiUrl" expression="json-eval($.disqusApiUrl)" />
<property name="disqus.apiKey" expression="json-eval($.disqusApiKey)" />
<property name="disqus.topPosts" expression="json-eval($.disqusTopPosts)" />
<property name="disqus.forumId" expression="json-eval($.disqusForumId)" />
<!--Array of ID Map Objects-->
<property name="canvas.disqus.idMap" expression="json-eval($.canvasDisqusIdMap)" />
<property name="responseString" value="" scope="operation" />
<property name="noOfDiscussions" expression="count(//jsonObject/canvasDisqusIdMap)" scope="operation" />
<property name="discussionIndex" expression="0" scope="operation" />
<property name="noOfEntries" expression="0" scope="operation" />
<property name="entryIndex" expression="0" scope="operation" />
<!--If the canvasDisqusIdMap array is empty, send an error message to the user and loop back.-->
<filter xpath="get-property('operation', 'noOfDiscussions') = get-property('operation', 'discussionIndex')">
<!--If no entries are found in canvasDisqusIdMap, respond immediately.-->
<then>
<payloadFactory media-type="json">
<format>{
"Response":{
"activity":"canvas_updateDiscussionTopicsWithTopPosts",
"activityResponse":"canvasDisqusIdMap is empty - No entries to process."
}
}</format>
<args>
<arg expression="get-property('operation','responseString')" />
</args>
</payloadFactory>
<property name="messageType" value="application/json" scope="axis2" />
<respond />
</then>
<else>
<!--FOR EACH DiscussionID Map - Delete Loop: BEGIN-->
<iterate continueParent="false" id="discussions" expression="//jsonObject/canvasDisqusIdMap" sequential="true">
<target>
<sequence>
<!--Increment the discussion count by 1.-->
<property name="discussionIndex" expression="get-property('operation', 'discussionIndex') + 1" scope="operation" />
<!--Store the discussion and thread details.-->
<property name="canvas.discussionId" expression="//canvasDisqusIdMap/canvas_discussionTopicId/text()" />
<property name="disqus.threadId" expression="//canvasDisqusIdMap/disqus_threadId/text()" />
<!--First, check whether there are any featured posts for the thread in Disqus.-->
<disqus.init>
<apiUrl>{$ctx:disqus.apiUrl}</apiUrl>
<apiKey>{$ctx:disqus.apiKey}</apiKey>
</disqus.init>
<disqus.listPosts>
<forumId>{$ctx:disqus.forumId}</forumId>
<limit>{$ctx:disqus.topPosts}</limit>
<popular>true</popular>
<include>unapproved,approved,highlighted</include>
<order>best</order>
<related>thread</related>
<threadId>{$ctx:disqus.threadId}</threadId>
</disqus.listPosts>
<sequence key="removeResponseHeaders" />
<property name="noOfPosts" expression="count(//jsonObject/response)" />
<property name="postIndex" expression="0" />
<!--Process only if there are any posts to update.-->
<filter xpath="get-property('noOfPosts') != get-property('postIndex')">
<then>
<!--List all entries that are added to the discussion topic.-->
<canvas.init>
<apiUrl>{$ctx:canvas.apiUrl}</apiUrl>
<accessToken>{$ctx:canvas.accessToken}</accessToken>
</canvas.init>
<canvas.listEntries>
<topicId>{$ctx:canvas.discussionId}</topicId>
<perPage>100</perPage>
<courseId>{$ctx:canvas.courseId}</courseId>
</canvas.listEntries>
<sequence key="removeResponseHeaders" />
<property name="noOfEntries" expression="count(//jsonArray/jsonElement)" />
<property name="entryIndex" expression="0" />
<!--If there are any entries to be deleted, delete them.-->
<filter xpath="get-property('noOfEntries') != get-property('entryIndex')">
<then>
<!--Increment the no. of entries to be deleted by the appropriate no.-->
<property name="noOfEntries" expression="get-property('operation','noOfEntries') + count(//jsonArray/jsonElement)" scope="operation" />
<!--FOR EACH Entry: BEGIN-->
<iterate continueParent="false" id="entries" expression="//jsonArray/jsonElement" sequential="true">
<target>
<sequence>
<property name="canvas.entryId" expression="//jsonElement/id/text()" />
<!--Delete the entry.-->
<canvas.init>
<apiUrl>{$ctx:canvas.apiUrl}</apiUrl>
<accessToken>{$ctx:canvas.accessToken}</accessToken>
</canvas.init>
<canvas.deleteEntry>
<topicId>{$ctx:canvas.discussionId}</topicId>
<courseId>{$ctx:canvas.courseId}</courseId>
<entryId>{$ctx:canvas.entryId}</entryId>
</canvas.deleteEntry>
<sequence key="removeResponseHeaders" />
<!--Increment the entry count.-->
<property name="entryIndex" expression="get-property('operation','entryIndex') + 1" scope="operation" />
</sequence>
</target>
</iterate>
<!--FOR EACH Entry: END-->
</then>
</filter>
</then>
</filter>
</sequence>
</target>
</iterate>
<!--FOR EACH DiscussionID Map - Delete Loop: END-->
<!--Start adding posts.-->
<filter xpath="(get-property('operation', 'noOfDiscussions') = get-property('operation', 'discussionIndex')) and
(get-property('operation', 'noOfEntries') = get-property('operation', 'entryIndex'))">
<then>
<property name="discussionIndex" expression="0" scope="operation" />
<property name="noOfPosts" expression="0" scope="operation" />
<property name="postIndex" expression="0" scope="operation" />
<!--Set the User Payload for the iteration.-->
<payloadFactory media-type="json">
<format>{
"idmap": $1
}
</format>
<args>
<arg expression="get-property('canvas.disqus.idMap')" />
</args>
</payloadFactory>
<!--FOR EACH DiscussionID Map: BEGIN-->
<iterate continueParent="false" id="discussions" expression="//jsonObject/idmap" sequential="true">
<target>
<sequence>
<!--Increment the discussion count.-->
<property name="discussionIndex" expression="get-property('operation', 'discussionIndex') + 1" scope="operation" />
<!--Store the discussion and thread details.-->
<property name="canvas.discussionId" expression="//idmap/canvas_discussionTopicId/text()" />
<property name="disqus.threadId" expression="//idmap/disqus_threadId/text()" />
<!--Invalidate uri.var.accessToken which was set in the canvas.createDiscussionTopic template to prevent ambiguity.-->
<property name="uri.var.accessToken" value="" action="remove" />
<property name="uri.var.query" value="" action="remove" />
<!--List and check whether there are any featured posts for the thread in Disqus.-->
<disqus.init>
<apiUrl>{$ctx:disqus.apiUrl}</apiUrl>
<apiKey>{$ctx:disqus.apiKey}</apiKey>
</disqus.init>
<disqus.listPosts>
<forumId>{$ctx:disqus.forumId}</forumId>
<limit>{$ctx:disqus.topPosts}</limit>
<popular>true</popular>
<include>unapproved,approved,highlighted</include>
<order>best</order>
<related>thread</related>
<threadId>{$ctx:disqus.threadId}</threadId>
</disqus.listPosts>
<sequence key="removeResponseHeaders" />
<property name="noOfPosts" expression="count(//jsonObject/response)" />
<property name="postIndex" expression="0" />
<!--If the thread doesn't have any posts, send a message to the user. Else, continue with the iteration.-->
<filter xpath="get-property('noOfPosts') = get-property('postIndex')">
<!---->
<then>
<property name="id" expression="fn:concat('canvas_discussionTopicId:', get-property('canvas.discussionId'), ',disqus_threadId:', get-property('disqus.threadId'))" />
<property name="status" value="Empty" />
<property name="message" expression="fn:concat('No posts were found in Disqus for Thread ID: ', get-property('disqus.threadId'))" />
<call-template target="responseHandlerTemplate">
<with-param name="activity" value="disqus_listPopularThreads" />
<with-param name="id" value="{$ctx:id}" />
<with-param name="status" value="{$ctx:status}" />
<with-param name="message" value="{$ctx:message}" />
</call-template>
</then>
<else>
<!--Increment the no. of entries to be deleted.-->
<property name="noOfPosts" expression="get-property('operation','noOfPosts') + count(//jsonObject/response)" scope="operation" />
<iterate continueParent="false" id="entries" expression="//jsonObject/response" sequential="true">
<target>
<sequence>
<property name="disqus.post.id" expression="//response/id/text()" />
<property name="disqus.message" expression="//response/raw_message/text()" />
<property name="disqus.commentor" expression="//response/author/name/text()" />
<property name="disqus.likes" expression="//response/points/text()" />
<property name="messageToUpdate" expression="fn:concat(get-property('disqus.message'), ' - ', get-property('disqus.commentor'), ' [', get-property('disqus.likes'), ' Point(s)]')" />
<!--Build the ID string.-->
<property name="id" expression="fn:concat('canvas_discussionTopicId:', get-property('canvas.discussionId'), ',disqus_threadId:', get-property('disqus.threadId'), ',disqus_postId:', get-property('disqus.post.id'))" />
<!--Add Entry. Message would be null as the API expects multipart/form-data.-->
<canvas.init>
<apiUrl>{$ctx:canvas.apiUrl}</apiUrl>
<accessToken>{$ctx:canvas.accessToken}</accessToken>
</canvas.init>
<canvas.createEntry>
<topicId>{$ctx:canvas.discussionId}</topicId>
<courseId>{$ctx:canvas.courseId}</courseId>
</canvas.createEntry>
<sequence key="removeResponseHeaders" />
<property name="canvas.updatEntryId" expression="json-eval($.id)" />
<!--If creation is unsuccessful, set an error message to be sent to the user. Else, continue with the process.-->
<filter source="boolean(get-property('canvas.updatEntryId'))" regex="false">
<then>
<property name="status" value="Failure" />
<property name="message" expression="json-eval($)" />
</then>
<else>
<!--Update the entry with the message.-->
<canvas.init>
<apiUrl>{$ctx:canvas.apiUrl}</apiUrl>
<accessToken>{$ctx:canvas.accessToken}</accessToken>
</canvas.init>
<canvas.updateEntry>
<topicId>{$ctx:canvas.discussionId}</topicId>
<courseId>{$ctx:canvas.courseId}</courseId>
<message>{$ctx:messageToUpdate}</message>
<entryId>{$ctx:canvas.updatEntryId}</entryId>
</canvas.updateEntry>
<sequence key="removeResponseHeaders" />
<property name="canvas.updatEntryId" expression="json-eval($.id)" />
<!--If updation is unsuccessful, set an error message to be sent to the user. Else, set a success message for the user.-->
<filter source="boolean(get-property('canvas.updatEntryId'))" regex="true">
<!--If the update is successful.-->
<then>
<property name="status" value="Success" />
<property name="message" value="Canvas discussion topic entries successfully synchronized with Disqus thread posts." />
</then>
<!--If the update is unsuccessful.-->
<else>
<property name="apiErrorResponse" expression="json-eval($)" />
<property name="status" value="Failure" />
<property name="message" expression="json-eval($)" />
</else>
</filter>
</else>
</filter>
<!--Call the response handler template.-->
<call-template target="responseHandlerTemplate">
<with-param name="activity" value="canvas_updateDiscussionTopicWithTopPosts" />
<with-param name="id" value="{$ctx:id}" />
<with-param name="status" value="{$ctx:status}" />
<with-param name="message" value="{$ctx:message}" />
</call-template>
<!--Increment the post count.-->
<property name="postIndex" expression="get-property('operation','postIndex') + 1" scope="operation" />
</sequence>
</target>
</iterate>
</else>
</filter>
</sequence>
</target>
</iterate>
<!--FOR EACH DiscussionID Map: END-->
<filter xpath="(get-property('operation', 'noOfDiscussions') = get-property('operation', 'discussionIndex')) and
(get-property('operation', 'noOfPosts') = get-property('operation', 'postIndex'))">
<then>
<loopback />
</then>
</filter>
</then>
</filter>
</else>
</filter>
</inSequence>
<outSequence>
<payloadFactory media-type="json">
<format>{
"Response":{
"activity":"canvas_updateDiscussionTopicsWithTopPosts",
"activityResponse":[$1]
}
}</format>
<args>
<arg expression="get-property('operation','responseString')" />
</args>
</payloadFactory>
<property name="messageType" value="application/json" scope="axis2" />
<send />
</outSequence>
</target>
<description />
</proxy> |