Skip to content
Snippets Groups Projects
Commit 28dfca5d authored by Julian Dehne's avatar Julian Dehne
Browse files

feat: added session example for simplification of context storing

parent c1ac569c
No related branches found
No related tags found
No related merge requests found
......@@ -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
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>");
}
}
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();
}
}
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);
}
}
......@@ -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
<%--
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>
<%--
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>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment