From 28dfca5de4842b4623bdc412954b0c1e76e8330b Mon Sep 17 00:00:00 2001
From: Julian Dehne <julian.dehne@uni-potsdam.de>
Date: Mon, 23 Jul 2018 15:11:38 +0200
Subject: [PATCH] feat: added session example for simplification of context
 storing

---
 .../gf/core/management/project/Project.java   | 15 +++-
 .../gf/core/session/ContextTag.java           | 27 +++++++
 .../unipotsdam/gf/core/session/GFContext.java | 77 +++++++++++++++++++
 .../gf/core/session/SessionTag.java           | 51 ++++++++++++
 .../webapp/core/pages/gemeinsamForschen.tld   | 12 +++
 .../main/webapp/session/session_example_1.jsp | 18 +++++
 .../main/webapp/session/session_example_2.jsp | 17 ++++
 7 files changed, 216 insertions(+), 1 deletion(-)
 create mode 100644 gemeinsamforschen/src/main/java/unipotsdam/gf/core/session/ContextTag.java
 create mode 100644 gemeinsamforschen/src/main/java/unipotsdam/gf/core/session/GFContext.java
 create mode 100644 gemeinsamforschen/src/main/java/unipotsdam/gf/core/session/SessionTag.java
 create mode 100644 gemeinsamforschen/src/main/webapp/session/session_example_1.jsp
 create mode 100644 gemeinsamforschen/src/main/webapp/session/session_example_2.jsp

diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/project/Project.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/project/Project.java
index 06bd24d7..2fb7b840 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/project/Project.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/project/Project.java
@@ -122,5 +122,18 @@ public class Project {
         return timecreated;
     }
 
-
+    @Override
+    public String toString() {
+        final StringBuilder sb = new StringBuilder("Project{");
+        sb.append("id='").append(id).append('\'');
+        sb.append(", password='").append(password).append('\'');
+        sb.append(", active=").append(active);
+        sb.append(", timecreated=").append(timecreated);
+        sb.append(", author='").append(author).append('\'');
+        sb.append(", adminPassword='").append(adminPassword).append('\'');
+        sb.append(", token='").append(token).append('\'');
+        sb.append(", phase='").append(phase).append('\'');
+        sb.append('}');
+        return sb.toString();
+    }
 }
\ No newline at end of file
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/session/ContextTag.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/session/ContextTag.java
new file mode 100644
index 00000000..741d3eb2
--- /dev/null
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/session/ContextTag.java
@@ -0,0 +1,27 @@
+package unipotsdam.gf.core.session;
+
+import unipotsdam.gf.core.management.project.Project;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.jsp.JspWriter;
+import javax.servlet.jsp.PageContext;
+import javax.servlet.jsp.tagext.SimpleTagSupport;
+import java.io.IOException;
+
+public class ContextTag extends SimpleTagSupport {
+    public void doTag() throws IOException {
+        PageContext pageContext = (PageContext) getJspContext();
+        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
+        // sessionID is created with first call and persisted throughout the user's session<
+        JspWriter out = getJspContext().getOut();
+        // lets add some context to the site
+
+        /**
+         * We assume that the project context is added to the session, when a project is selected
+         * in the view, then the project is loaded from db and added via setAttribute like below
+         */
+        GFContext gfContext = (GFContext) request.getSession().getAttribute("gf_context");
+        out.println("<p>project:"+gfContext.getProject().toString()+"</p>");
+
+    }
+}
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/session/GFContext.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/session/GFContext.java
new file mode 100644
index 00000000..bdbac9ae
--- /dev/null
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/session/GFContext.java
@@ -0,0 +1,77 @@
+package unipotsdam.gf.core.session;
+
+import unipotsdam.gf.core.management.project.Project;
+import unipotsdam.gf.core.management.user.User;
+import unipotsdam.gf.core.states.ProjectPhase;
+import unipotsdam.gf.modules.assessment.controller.model.Quiz;
+import unipotsdam.gf.modules.communication.model.chat.ChatRoom;
+
+public class GFContext {
+    Project project;
+    User user;
+    ProjectPhase projectPhase;
+    ChatRoom chatRoom;
+    // could be quizState ....
+    Quiz quiz;
+
+    public GFContext(
+            Project project, User user, ProjectPhase projectPhase, ChatRoom chatRoom, Quiz quiz) {
+        this.project = project;
+        this.user = user;
+        this.projectPhase = projectPhase;
+        this.chatRoom = chatRoom;
+        this.quiz = quiz;
+    }
+
+    public Project getProject() {
+        return project;
+    }
+
+    public void setProject(Project project) {
+        this.project = project;
+    }
+
+    public User getUser() {
+        return user;
+    }
+
+    public void setUser(User user) {
+        this.user = user;
+    }
+
+    public ProjectPhase getProjectPhase() {
+        return projectPhase;
+    }
+
+    public void setProjectPhase(ProjectPhase projectPhase) {
+        this.projectPhase = projectPhase;
+    }
+
+    public ChatRoom getChatRoom() {
+        return chatRoom;
+    }
+
+    public void setChatRoom(ChatRoom chatRoom) {
+        this.chatRoom = chatRoom;
+    }
+
+    public Quiz getQuiz() {
+        return quiz;
+    }
+
+    public void setQuiz(Quiz quiz) {
+        this.quiz = quiz;
+    }
+
+    @Override
+    public String toString() {
+        final StringBuilder sb = new StringBuilder("GFContext{");
+        sb.append("project=").append(project);
+        sb.append(", user=").append(user);
+        sb.append(", projectPhase=").append(projectPhase);
+        sb.append(", chatRoom=").append(chatRoom);
+        sb.append(", quiz=").append(quiz);
+        sb.append('}');
+        return sb.toString();
+    }
+}
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/session/SessionTag.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/session/SessionTag.java
new file mode 100644
index 00000000..eae3f549
--- /dev/null
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/session/SessionTag.java
@@ -0,0 +1,51 @@
+package unipotsdam.gf.core.session;
+
+import uk.co.jemos.podam.api.PodamFactory;
+import uk.co.jemos.podam.api.PodamFactoryImpl;
+import unipotsdam.gf.core.management.ManagementImpl;
+import unipotsdam.gf.core.management.project.Project;
+import unipotsdam.gf.core.management.user.User;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.jsp.JspWriter;
+import javax.servlet.jsp.PageContext;
+import javax.servlet.jsp.tagext.SimpleTagSupport;
+import java.io.IOException;
+
+public class SessionTag extends SimpleTagSupport {
+
+    /**
+     * Utility to creaty dummy data for students
+     */
+    PodamFactory factory = new PodamFactoryImpl();
+
+
+    public void doTag() throws IOException {
+        PageContext pageContext = (PageContext) getJspContext();
+        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
+        // sessionID is created with first call and persisted throughout the user's session<
+        JspWriter out = getJspContext().getOut();
+        out.println("<p id=\"sessionId\"> SessionId:"+request.getSession().getId()+"</p>");
+
+        // lets add some context to the site
+
+        /**
+         * We assume that the project context is added to the session, when a project is selected
+         * in the view, then the project is loaded from db and added via setAttribute like below
+         * this is only done here for the purpose of example
+         */
+
+        // create dummy context
+        String context1 = factory.manufacturePojo(GFContext.class).toString();
+        // set dummy context in sessions
+        request.getSession().setAttribute("gf_context", context1);
+
+        // you can update it
+        Project project = factory.manufacturePojo(Project.class);
+        GFContext context2 = (GFContext) request.getSession().getAttribute("gf_context");
+        context2.setProject(project);
+        // updated context set in session
+        request.getSession().setAttribute("gf_context", context2);
+
+    }
+}
diff --git a/gemeinsamforschen/src/main/webapp/core/pages/gemeinsamForschen.tld b/gemeinsamforschen/src/main/webapp/core/pages/gemeinsamForschen.tld
index 3704c718..63ab26a6 100644
--- a/gemeinsamforschen/src/main/webapp/core/pages/gemeinsamForschen.tld
+++ b/gemeinsamforschen/src/main/webapp/core/pages/gemeinsamForschen.tld
@@ -15,4 +15,16 @@
         <body-content>empty</body-content>
     </tag>
 
+    <tag>
+        <name>session</name>
+        <tag-class>unipotsdam.gf.core.session.SessionTag</tag-class>
+        <body-content>empty</body-content>
+    </tag>
+
+    <tag>
+        <name>context</name>
+        <tag-class>unipotsdam.gf.core.session.ContextTag</tag-class>
+        <body-content>empty</body-content>
+    </tag>
+
 </taglib>
\ No newline at end of file
diff --git a/gemeinsamforschen/src/main/webapp/session/session_example_1.jsp b/gemeinsamforschen/src/main/webapp/session/session_example_1.jsp
new file mode 100644
index 00000000..4f2ddf97
--- /dev/null
+++ b/gemeinsamforschen/src/main/webapp/session/session_example_1.jsp
@@ -0,0 +1,18 @@
+<%--
+  Created by IntelliJ IDEA.
+  User: dehne
+  Date: 23.07.2018
+  Time: 13:51
+  To change this template use File | Settings | File Templates.
+--%>
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+<%@ taglib prefix="gf" uri="../core/pages/gemeinsamForschen.tld" %>
+<html>
+<head>
+    <title>Title</title>
+</head>
+<body>
+    <gf:session></gf:session>
+    <a href="session_example_2.jsp">click mich</a>
+</body>
+</html>
diff --git a/gemeinsamforschen/src/main/webapp/session/session_example_2.jsp b/gemeinsamforschen/src/main/webapp/session/session_example_2.jsp
new file mode 100644
index 00000000..a6e570bc
--- /dev/null
+++ b/gemeinsamforschen/src/main/webapp/session/session_example_2.jsp
@@ -0,0 +1,17 @@
+<%--
+  Created by IntelliJ IDEA.
+  User: dehne
+  Date: 23.07.2018
+  Time: 13:51
+  To change this template use File | Settings | File Templates.
+--%>
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+<%@ taglib prefix="gf" uri="../core/pages/gemeinsamForschen.tld" %>
+<html>
+<head>
+    <title>Big Deal</title>
+</head>
+<body>
+    <gf:context/>
+</body>
+</html>
-- 
GitLab