Index: lams_tool_forum/src/java/org/lamsfoundation/lams/tool/forum/persistence/ForumUser.java =================================================================== diff -u -r827f3387ff5d20c38597d5947d0b68e54ed2387b -rc5773ea3c45ef20d1b625a99dedb94f849e1a4c1 --- lams_tool_forum/src/java/org/lamsfoundation/lams/tool/forum/persistence/ForumUser.java (.../ForumUser.java) (revision 827f3387ff5d20c38597d5947d0b68e54ed2387b) +++ lams_tool_forum/src/java/org/lamsfoundation/lams/tool/forum/persistence/ForumUser.java (.../ForumUser.java) (revision c5773ea3c45ef20d1b625a99dedb94f849e1a4c1) @@ -57,6 +57,7 @@ public ForumUser() { } + /** Create the user based on the DTO in the session */ public ForumUser(UserDTO user, ForumToolSession session) { this.userId = new Long(user.getUserID().intValue()); this.firstName = user.getFirstName(); @@ -65,6 +66,16 @@ this.session = session; this.sessionFinished = false; } + + /** Create the user based on the details in the JSON call - used for authoring so no session exists. */ + public ForumUser(Long userId, String firstName, String lastName, String loginName) { + this.userId = userId; + this.firstName = firstName; + this.lastName = lastName; + this.loginName = loginName; + this.session = null; + this.sessionFinished = false; + } // ********************************************************** // Function method for ForumUser Index: lams_tool_forum/src/java/org/lamsfoundation/lams/tool/forum/service/ForumService.java =================================================================== diff -u -r3f65546d337cbe98aea623132d1835a770e6d1c8 -rc5773ea3c45ef20d1b625a99dedb94f849e1a4c1 --- lams_tool_forum/src/java/org/lamsfoundation/lams/tool/forum/service/ForumService.java (.../ForumService.java) (revision 3f65546d337cbe98aea623132d1835a770e6d1c8) +++ lams_tool_forum/src/java/org/lamsfoundation/lams/tool/forum/service/ForumService.java (.../ForumService.java) (revision c5773ea3c45ef20d1b625a99dedb94f849e1a4c1) @@ -45,6 +45,7 @@ import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.apache.struts.upload.FormFile; +import org.apache.tomcat.util.json.JSONArray; import org.apache.tomcat.util.json.JSONException; import org.apache.tomcat.util.json.JSONObject; import org.lamsfoundation.lams.contentrepository.AccessDeniedException; @@ -69,6 +70,7 @@ import org.lamsfoundation.lams.notebook.model.NotebookEntry; import org.lamsfoundation.lams.notebook.service.CoreNotebookConstants; import org.lamsfoundation.lams.notebook.service.ICoreNotebookService; +import org.lamsfoundation.lams.rest.ToolRestManager; import org.lamsfoundation.lams.tool.ToolContentImport102Manager; import org.lamsfoundation.lams.tool.ToolContentManager; import org.lamsfoundation.lams.tool.ToolOutput; @@ -110,6 +112,7 @@ import org.lamsfoundation.lams.usermanagement.User; import org.lamsfoundation.lams.usermanagement.dto.UserDTO; import org.lamsfoundation.lams.usermanagement.service.IUserManagementService; +import org.lamsfoundation.lams.util.JsonUtil; import org.lamsfoundation.lams.util.MessageService; import org.lamsfoundation.lams.util.WebUtil; import org.lamsfoundation.lams.util.audit.IAuditService; @@ -122,7 +125,7 @@ * * @version $Revision$ */ -public class ForumService implements IForumService, ToolContentManager, ToolSessionManager, ToolContentImport102Manager { +public class ForumService implements IForumService, ToolContentManager, ToolSessionManager, ToolContentImport102Manager, ToolRestManager { private static final Logger log = Logger.getLogger(ForumService.class); // DAO variables @@ -1588,4 +1591,95 @@ // ****************** REST methods ************************* + /** Used by the Rest calls to create content. + * Mandatory fields in toolContentJSON: title, instructions, topics. + * Topics must contain a JSONArray of JSONObject objects, which have the following mandatory fields: subject, body + * There should be at least one topic object in the Topics array. + */ + @Override + public void createRestToolContent(Integer userID, Long toolContentID, JSONObject toolContentJSON) throws JSONException { + + Forum forum = new Forum(); + Date updateDate = new Date(); + forum.setCreated(updateDate); + forum.setUpdated(updateDate); + + forum.setContentId(toolContentID); + forum.setTitle(toolContentJSON.getString("title")); + forum.setInstructions(toolContentJSON.getString("instructions")); + + forum.setAllowAnonym(JsonUtil.opt(toolContentJSON, "allowAnonym", Boolean.FALSE)); + forum.setAllowEdit(JsonUtil.opt(toolContentJSON, "allowEdit", Boolean.TRUE)); // defaults to true in the default entry in the db + forum.setAllowNewTopic(JsonUtil.opt(toolContentJSON, "allowNewTopic", Boolean.TRUE)); // defaults to true in the default entry in the db + forum.setAllowRateMessages(JsonUtil.opt(toolContentJSON, "allowRateMessages", Boolean.FALSE)); + forum.setAllowRichEditor(JsonUtil.opt(toolContentJSON, "allowRichEditor", Boolean.FALSE)); + forum.setAllowUpload(JsonUtil.opt(toolContentJSON, "allowUpload", Boolean.FALSE)); + forum.setContentInUse(false); + forum.setDefineLater(false); + forum.setLimitedMaxCharacters(JsonUtil.opt(toolContentJSON, "limitedMaxCharacters", Boolean.FALSE)); + forum.setLimitedMinCharacters(JsonUtil.opt(toolContentJSON, "limitedMinCharacters", Boolean.FALSE)); + forum.setLockWhenFinished(JsonUtil.opt(toolContentJSON, "lockWhenFinished", Boolean.FALSE)); + forum.setMaxCharacters(JsonUtil.opt(toolContentJSON, "maxCharacters", 5000)); // defaults to 5000 chars in the default entry in the db. + forum.setMaximumRate(JsonUtil.opt(toolContentJSON, "maximumRate", 0)); + forum.setMaximumReply(JsonUtil.opt(toolContentJSON, "maximumReply", 0)); + forum.setMinCharacters(JsonUtil.opt(toolContentJSON, "minCharacters", 0)); + forum.setMinimumRate(JsonUtil.opt(toolContentJSON, "minimumRate", 0)); + forum.setMinimumReply(JsonUtil.opt(toolContentJSON, "minimumReply", 0)); + forum.setNotifyLearnersOnForumPosting(JsonUtil.opt(toolContentJSON, "notifyLearnersOnForumPosting", Boolean.FALSE)); + forum.setNotifyLearnersOnMarkRelease(JsonUtil.opt(toolContentJSON, "notifyLearnersOnMarkRelease", Boolean.FALSE)); + forum.setNotifyTeachersOnForumPosting(JsonUtil.opt(toolContentJSON, "notifyTeachersOnForumPosting", Boolean.FALSE)); + forum.setReflectInstructions((String) JsonUtil.opt(toolContentJSON, "reflectInstructions", null)); + forum.setReflectOnActivity(JsonUtil.opt(toolContentJSON, "reflectOnActivity", Boolean.FALSE)); + forum.setSubmissionDeadline((Date) JsonUtil.opt(toolContentJSON, "submissionDeadline", null)); + + // *******************************Handle user******************* + // Code taken from AuthoringAction TODO +// String contentFolderID = (String) sessionMap.get(AttributeNames.PARAM_CONTENT_FOLDER_ID); + // check whether it is sysadmin:LDEV-906 +// if (!StringUtils.equals(contentFolderID, "-1")) { + // try to get form system session +// HttpSession ss = SessionManager.getSession(); + // get back login user DTO +// UserDTO user = (UserDTO) ss.getAttribute(AttributeNames.USER); + ForumUser forumUser = getUserByID(userID.longValue()); + if (forumUser == null) { + forumUser = new ForumUser(userID.longValue(), toolContentJSON.getString("firstName"), toolContentJSON.getString("lastName"),toolContentJSON.getString("loginName")); + getForumUserDao().save(forumUser); + } + forum.setCreatedBy(forumUser); + + updateForum(forum); + + // **************************** Handle topic ********************* + JSONArray topics = toolContentJSON.getJSONArray("topics"); + for (int i=0; i