package fi.otavanopisto.pyramus.json.students; import java.util.Date; import java.util.HashMap; import java.util.Map; import fi.internetix.smvc.SmvcRuntimeException; import fi.internetix.smvc.StatusCode; import fi.internetix.smvc.controllers.JSONRequestContext; import fi.otavanopisto.pyramus.dao.DAOFactory; import fi.otavanopisto.pyramus.dao.students.StudentGroupContactLogEntryCommentDAO; import fi.otavanopisto.pyramus.domainmodel.students.StudentGroupContactLogEntryComment; import fi.otavanopisto.pyramus.framework.JSONRequestController; import fi.otavanopisto.pyramus.framework.UserRole; /** * JSON request controller for editing a contact entry. */ public class EditGroupContactEntryCommentJSONRequestController extends JSONRequestController { /** * Method to process JSON requests. * * In parameters * - commentId - Id to identify the comment being modified * - commentText - Textual message or description about the contact * - commentCreatorName - Name of the person who made the contact * - commentDate - Date of the entry * * Page parameters * - results Map including * * id - Comment id * * entryId - Entry id * * creatorName - Comment creator * * timestamp - Comment date * * text - Comment message * * @param jsonRequestContext JSON request context */ public void process(JSONRequestContext jsonRequestContext) { StudentGroupContactLogEntryCommentDAO entryCommentDAO = DAOFactory.getInstance().getStudentGroupContactLogEntryCommentDAO(); Long commentId = jsonRequestContext.getLong("commentId"); if (commentId == null) throw new SmvcRuntimeException(StatusCode.UNDEFINED, "CommentId was not defined."); try { StudentGroupContactLogEntryComment comment = entryCommentDAO.findById(commentId); String commentText = jsonRequestContext.getString("commentText"); String commentCreatorName = jsonRequestContext.getString("commentCreatorName"); Date commentDate = jsonRequestContext.getDate("commentDate"); entryCommentDAO.update(comment, commentText, commentDate, commentCreatorName); Map<String, Object> info = new HashMap<>(); info.put("id", comment.getId()); info.put("creatorName", comment.getCreatorName()); info.put("timestamp", comment.getCommentDate() != null ? comment.getCommentDate().getTime() : ""); info.put("text", comment.getText()); info.put("entryId", comment.getEntry().getId()); jsonRequestContext.addResponseParameter("results", info); } catch (Exception e) { throw new SmvcRuntimeException(e); } } public UserRole[] getAllowedRoles() { return new UserRole[] { UserRole.MANAGER, UserRole.STUDY_PROGRAMME_LEADER, UserRole.ADMINISTRATOR }; } }