Index: lams_tool_survey/src/java/org/lamsfoundation/lams/tool/survey/model/SurveyUser.java
===================================================================
RCS file: /usr/local/cvsroot/lams_tool_survey/src/java/org/lamsfoundation/lams/tool/survey/model/SurveyUser.java,v
diff -u -r1.6 -r1.7
--- lams_tool_survey/src/java/org/lamsfoundation/lams/tool/survey/model/SurveyUser.java 28 Oct 2014 23:23:27 -0000 1.6
+++ lams_tool_survey/src/java/org/lamsfoundation/lams/tool/survey/model/SurveyUser.java 7 Jun 2015 06:57:59 -0000 1.7
@@ -73,7 +73,18 @@
this.survey = content;
this.responseFinalized = false;
}
+
+ public SurveyUser(Long userId, String firstName, String lastName, String login, Survey content) {
+ this.userId = userId;
+ this.firstName = firstName;
+ this.lastName = lastName;
+ this.loginName = login;
+ this.session = null;
+ this.survey = content;
+ this.responseFinalized = false;
+ }
+
/**
* Clone method from java.lang.Object
*/
Index: lams_tool_survey/src/java/org/lamsfoundation/lams/tool/survey/service/SurveyServiceImpl.java
===================================================================
RCS file: /usr/local/cvsroot/lams_tool_survey/src/java/org/lamsfoundation/lams/tool/survey/service/SurveyServiceImpl.java,v
diff -u -r1.40 -r1.41
--- lams_tool_survey/src/java/org/lamsfoundation/lams/tool/survey/service/SurveyServiceImpl.java 14 Apr 2015 20:59:55 -0000 1.40
+++ lams_tool_survey/src/java/org/lamsfoundation/lams/tool/survey/service/SurveyServiceImpl.java 7 Jun 2015 06:57:59 -0000 1.41
@@ -42,14 +42,20 @@
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
+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.events.IEventNotificationService;
import org.lamsfoundation.lams.learning.service.ILearnerService;
+import org.lamsfoundation.lams.learningdesign.TextSearchConditionComparator;
import org.lamsfoundation.lams.learningdesign.service.ExportToolContentException;
import org.lamsfoundation.lams.learningdesign.service.IExportToolContentService;
import org.lamsfoundation.lams.learningdesign.service.ImportToolContentException;
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.RestTags;
+import org.lamsfoundation.lams.rest.ToolRestManager;
import org.lamsfoundation.lams.tool.ToolContentImport102Manager;
import org.lamsfoundation.lams.tool.ToolContentManager;
import org.lamsfoundation.lams.tool.ToolOutput;
@@ -83,6 +89,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.wddx.WDDXProcessor;
@@ -92,7 +99,7 @@
* @author Dapeng.Ni
*/
public class SurveyServiceImpl implements ISurveyService, ToolContentManager, ToolSessionManager,
- ToolContentImport102Manager {
+ ToolContentImport102Manager, ToolRestManager {
private static Logger log = Logger.getLogger(SurveyServiceImpl.class.getName());
// DAO
@@ -696,13 +703,8 @@
SurveyUser user = surveyUserDao
.getUserByUserIDAndContentID(new Long(newUserUid.longValue()), toolContentId);
if (user == null) {
- user = new SurveyUser();
UserDTO sysUser = ((User) userManagementService.findById(User.class, newUserUid)).getUserDTO();
- user.setFirstName(sysUser.getFirstName());
- user.setLastName(sysUser.getLastName());
- user.setLoginName(sysUser.getLogin());
- user.setUserId(new Long(newUserUid.longValue()));
- user.setSurvey(toolContentObj);
+ user = new SurveyUser(sysUser, toolContentObj);
}
toolContentObj.setCreatedBy(user);
@@ -1124,4 +1126,81 @@
public void setSurveyOutputFactory(SurveyOutputFactory surveyOutputFactory) {
this.surveyOutputFactory = surveyOutputFactory;
}
+
+ // ****************** REST methods *************************
+
+ /**
+ * Used by the Rest calls to create content.
+ *
+ * Mandatory fields in toolContentJSON: title, instructions, questions. Optional fields are lockWhenFinished
+ * (default true), showOnePage (default true), notifyTeachersOnAnswerSumbit (default false), showOtherUsersAnswers
+ * (default false), reflectOnActivity, reflectInstructions, submissionDeadline
+ *
+ * Questions must contain a JSONArray of JSONObject objects, which have the following mandatory fields:
+ * questionText, type (1=one answer,2=multiple answers,3=free text entry) and answers. Answers is a JSONArray of
+ * strings, which are the answer text. A question may also have the optional fields: allowOtherTextEntry (default
+ * false), required (default true)
+ *
+ * There should be at least one question object in the Questions array and at least one option in the Options array.
+ */
+ @Override
+ public void createRestToolContent(Integer userID, Long toolContentID, JSONObject toolContentJSON)
+ throws JSONException {
+
+ Survey survey = new Survey();
+ Date updateDate = new Date();
+ survey.setCreated(updateDate);
+ survey.setUpdated(updateDate);
+
+ survey.setContentId(toolContentID);
+ survey.setTitle(toolContentJSON.getString(RestTags.TITLE));
+ survey.setInstructions(toolContentJSON.getString(RestTags.INSTRUCTIONS));
+
+ survey.setContentInUse(false);
+ survey.setDefineLater(false);
+ survey.setLockWhenFinished(JsonUtil.opt(toolContentJSON, RestTags.LOCK_WHEN_FINISHED, Boolean.TRUE));
+ survey.setReflectInstructions((String) JsonUtil.opt(toolContentJSON, RestTags.REFLECT_INSTRUCTIONS, null));
+ survey.setReflectOnActivity(JsonUtil.opt(toolContentJSON, RestTags.REFLECT_ON_ACTIVITY, Boolean.FALSE));
+ survey.setSubmissionDeadline((Date) JsonUtil.opt(toolContentJSON, RestTags.SUBMISSION_DEADLINE, null));
+ survey.setNotifyTeachersOnAnswerSumbit(JsonUtil.opt(toolContentJSON, "notifyTeachersOnAnswerSumbit", Boolean.FALSE));
+ survey.setShowOnePage(JsonUtil.opt(toolContentJSON, "showOnePage", Boolean.TRUE));
+ survey.setShowOtherUsersAnswers(JsonUtil.opt(toolContentJSON, "showOtherUsersAnswers", Boolean.FALSE));
+
+ SurveyUser surveyUser = new SurveyUser(userID.longValue(), toolContentJSON.getString("firstName"),
+ toolContentJSON.getString("lastName"), toolContentJSON.getString("loginName"), survey);
+ survey.setCreatedBy(surveyUser);
+
+ // **************************** Handle Survey Questions *********************
+
+ JSONArray questions = toolContentJSON.getJSONArray(RestTags.QUESTIONS);
+ for (int i = 0; i < questions.length(); i++) {
+ JSONObject questionData = (JSONObject) questions.get(i);
+ SurveyQuestion newQuestion = new SurveyQuestion();
+ newQuestion.setCreateBy(surveyUser);
+ newQuestion.setCreateDate(updateDate);
+ newQuestion.setDescription(questionData.getString(RestTags.QUESTION_TEXT));
+ newQuestion.setType((short) questionData.getInt("type"));
+ newQuestion.setAppendText(JsonUtil.opt(questionData, "allowOtherTextEntry", Boolean.FALSE));
+ Boolean required = JsonUtil.opt(questionData, "required", Boolean.TRUE);
+ newQuestion.setOptional(!required);
+ newQuestion.setSequenceId(i + 1); // sequence number starts at 1
+
+ Set newOptions = new HashSet();
+ JSONArray options = questionData.getJSONArray(RestTags.ANSWERS);
+ for (int j = 0; j < options.length(); j++) {
+ SurveyOption newOption = new SurveyOption();
+ newOption.setDescription(options.getString(j));
+ newOption.setSequenceId(j); // sequence number starts at 0
+ newOptions.add(newOption);
+ }
+ newQuestion.setOptions(newOptions);
+
+ survey.getQuestions().add(newQuestion);
+ }
+
+ saveOrUpdateSurvey(survey);
+ // *******************************
+ // TODO - investigate conditions
+ // survey.setConditions(conditions);
+ }
}
\ No newline at end of file