Commit b6b00a96 authored by Hakim Ouhida's avatar Hakim Ouhida
Browse files

Merge branch 'master' of gitlab.ensimag.fr:bugaudz/web

parents 1c295ad0 b2067cc1
......@@ -7,6 +7,9 @@ package controleur;
import dao.ChoixDAO;
import dao.ParagraphDAO;
import modele.Choix;
import modele.Paragraph;
import java.io.IOException;
import java.io.PrintWriter;
import javax.annotation.Resource;
......@@ -100,11 +103,11 @@ public class AssociateParagraphChoice extends HttpServlet {
HttpServletResponse response,
ParagraphDAO paragraphDAO)
throws ServletException, IOException {
HttpSession session = request.getSession(false);
if (session == null || session.getAttribute("username") == null) {
return;//TODO
}
//TODO verify user is associated to this story
System.out.println("choice_id: " + request.getParameter("choice_id"));
int choiceID = Integer.parseInt(request.getParameter("choice_id"));
int paragraphFollowerID;
......@@ -115,6 +118,17 @@ public class AssociateParagraphChoice extends HttpServlet {
paragraphFollowerID = (Integer) (request.getAttribute("paragraphFollower"));
}
System.out.println(paragraphFollowerID);
Paragraph oldParagraph = new ChoixDAO(ds).getChoiceParagraph(choiceID);
if(oldParagraph!=null && !oldParagraph.isValide() && oldParagraph.getAuteur() == session.getAttribute("username")){
paragraphDAO.deleteParagraph(oldParagraph.getId());
}else if(oldParagraph!=null){
response.setContentType("text/html");
try (PrintWriter out = response.getWriter()){
out.println(-1);
}
return;
}
paragraphDAO.associateParagraphChoice(choiceID, paragraphFollowerID);
}
......
......@@ -110,22 +110,21 @@ public class CreateStory extends HttpServlet {
//Recupération des données
String title = request.getParameter("title");
String textfirstParagraph = request.getParameter("firstParagraph");
int published = request.getParameter("published") != null ? 1 : 0;
int access = request.getParameter("access") != null ? 1 : 0;
String firstChoice = request.getParameter("firstChoice");
try {
//MAJ ParagraphDAO pour premier paragraph
//MAJ ParagraphDAO pour premier paragraph
ParagraphDAO paraDAO = new ParagraphDAO(ds);
int idFirstParagraph = paraDAO.createParagraph(textfirstParagraph, author, false, true);
if (idFirstParagraph == -1) {
//TODO failure
//TODO failure
}
ChoixDAO choixDAO = new ChoixDAO(ds);
choixDAO.addChoice(idFirstParagraph, firstChoice);
paraDAO.validateParagraph(idFirstParagraph);
//MAJ StoryDAO
int idStory = storyDAO.addStory(access, published, idFirstParagraph, title);
int idStory = storyDAO.addStory(access, 0, idFirstParagraph, title); //published = 0 by default
//Association Story-Paragraphe
paraDAO.associateStoryParagraph(idStory, idFirstParagraph);
......
......@@ -60,7 +60,11 @@ public class Login extends HttpServlet {
if (login != null && pass != null && userDAO.isLoginValid(login, pass)) {
HttpSession session = request.getSession();
session.setAttribute("username", login);
response.sendRedirect("index.jsp");
if(request.getParameterMap().containsKey("sourcePage")){
response.sendRedirect(request.getParameter("sourcePage"));
}else {
response.sendRedirect("index.jsp");
}
} else {
errorLogin(request, response, "Invalid Login Credentials");
}
......
......@@ -5,6 +5,7 @@
*/
package controleur;
import dao.ChoixDAO;
import dao.StoryDAO;
import dao.ParagraphDAO;
import modele.Choix;
......@@ -126,6 +127,12 @@ public class LookupParagraph extends HttpServlet {
}
choiceList = newChoiceList;
}
for (Choix choice:choiceList) {
choice.setRequired(new ChoixDAO(ds).getReadBefore(choice.getId()));
if(!allReadbefore(choice.getRequired())){
choice.setPublished(false);
}
}
boolean deletable = paragraphDAO.isDeletable(Integer.parseInt(request.getParameter("paragraph_id")));
request.setAttribute("paragraph",paragraph);
request.setAttribute("choiceList",choiceList);
......@@ -137,5 +144,13 @@ public class LookupParagraph extends HttpServlet {
else System.out.println("mode: " + request.getParameter("mode"));
}
private boolean allReadbefore(List<Integer> required) {
if(required==null){
return true;
}
//TODO return true if all elements in required are in history
return true;
}
}
......@@ -36,6 +36,8 @@ public class LookupStory extends HttpServlet {
@Resource(name = "jdbc/projet-web")
DataSource ds;
//TODO get HTML CODE OUT OF THE SERVELETS
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
......@@ -58,6 +60,15 @@ public class LookupStory extends HttpServlet {
case "1":
actionGetStory(request, response, new StoryDAO(ds));
break;
case "2":
actionGetMyStories(request, response, new StoryDAO(ds));
break;
case "3":
actionGetInviterStories(request,response, new StoryDAO(ds));
break;
case "4":
actionGetAllPublicStories(request, response,new StoryDAO(ds));
default:
throw new IllegalStateException("Unexpected value: " + request.getParameter("code"));
}
......@@ -136,7 +147,12 @@ public class LookupStory extends HttpServlet {
return;//TODO
}
ArrayList<Story> stories = histoireDAO.getUserStories((String) session.getAttribute("username"));
//TODO response
for (Story story : stories) {
PrintWriter out = response.getWriter();
out.println("<div class=\"story_div\">"
+ "<a class=\"story\" href=\"LookupStory?code=1&&story_id=" + story.getId() + "\">" + story.getTitre() + "</a>"
+ "</div>");
}
}
/**
......@@ -155,7 +171,8 @@ public class LookupStory extends HttpServlet {
throws ServletException, IOException {
HttpSession session = request.getSession(true);
if (histoireDAO.isPublished(Integer.parseInt(request.getParameter("story_id")))) {
boolean published = histoireDAO.isPublished(Integer.parseInt(request.getParameter("story_id")));
if (published) {
}else if(session.getAttribute("username") == null){
response.sendRedirect("index.jsp");
......@@ -187,7 +204,8 @@ public class LookupStory extends HttpServlet {
request.setAttribute("history", session.getAttribute("history"));
request.setAttribute("publicStory", publicStory);
request.setAttribute("invitedList",invitedList);
request.setAttribute("published", published);
this.getServletContext().getRequestDispatcher("/story.jsp").include(request, response);
if (session.getAttribute("sotries") == null) session.setAttribute("stories", new HashSet<Integer>());
......@@ -209,7 +227,12 @@ public class LookupStory extends HttpServlet {
throws ServletException, IOException {
ArrayList<Story> stories = histoireDAO.getPublicStories();
//TODO response
PrintWriter out = response.getWriter();
for (Story story : stories) {
out.println("<div class=\"story_div\">"
+ "<a class=\"story\" href=\"LookupStory?code=1&&story_id=" + story.getId() + "\">" + story.getTitre() + "</a>"
+ "</div>");
}
}
/**
......@@ -254,8 +277,13 @@ public class LookupStory extends HttpServlet {
return;//TODO
}
ArrayList<Story> stories = histoireDAO.getInviterStories((String) session.getAttribute("username"));
PrintWriter out = response.getWriter();
for (Story story : stories) {
out.println("<div class=\"story_div\">"
+ "<a class=\"story\" href=\"LookupStory?code=1&&story_id=" + story.getId() + "\">" + story.getTitre() + "</a>"
+ "</div>");
}
//TODO response
}
}
......@@ -8,7 +8,6 @@ package controleur;
import dao.ChoixDAO;
import dao.DAOException;
import dao.ParagraphDAO;
import dao.StoryDAO;
import java.io.IOException;
import java.io.PrintWriter;
......@@ -69,6 +68,9 @@ public class ManageParagraph extends HttpServlet {
case "7":
actionDeleteParagraph(request, response, new ParagraphDAO(ds));
break;
case "8":
actionSetReadBefore(request, response, new ParagraphDAO(ds));
break;
}}catch(Exception e) {
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
......@@ -77,6 +79,7 @@ public class ManageParagraph extends HttpServlet {
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
......@@ -189,16 +192,18 @@ public class ManageParagraph extends HttpServlet {
return; //TODO
}
int id = Integer.parseInt(request.getParameter("paragraph_id"));
try {
if (paragraphDAO.validateParagraph(id)) {
//TODO success
} else {
//TODO failure
if (paragraphDAO.validateParagraph(id)) {
response.setContentType("text/html");
try (PrintWriter out = response.getWriter()){
out.println("1");
}
} else {
response.setContentType("text/html");
try (PrintWriter out = response.getWriter()){
out.println("-1");
}
} catch (DAOException e) {
//TODO
}
}
/**
......@@ -229,6 +234,14 @@ public class ManageParagraph extends HttpServlet {
}
}
private void actionSetReadBefore(HttpServletRequest request, HttpServletResponse response, ParagraphDAO paragraphDAO) {
if (!isLegitim(request, paragraphDAO)) {
return; //TODO
}
int id = Integer.parseInt(request.getParameter("paragraph_id"));
int before_id = Integer.parseInt(request.getParameter("before_id"));
paragraphDAO.setReadBefore(id,before_id);
}
/**
* Deletes the given paragraph. We verify if the author is connected
......
......@@ -8,11 +8,15 @@ package controleur;
import dao.StoryDAO;
import java.io.IOException;
import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import javax.websocket.Session;
/**
*
......@@ -20,6 +24,8 @@ import javax.servlet.http.HttpServletResponse;
*/
@WebServlet(name = "PublishStory", urlPatterns = {"/PublishStory"})
public class PublishStory extends HttpServlet {
@Resource(name = "jdbc/projet-web")
DataSource ds;
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
......@@ -32,18 +38,23 @@ public class PublishStory extends HttpServlet {
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet PublishStory</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet PublishStory at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
try{
actionPublishStory(request,response, new StoryDAO(ds));
}catch (Exception e) {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet PublishStory</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet PublishStory at " + request.getContextPath() + "</h1>");
out.println("<h1>error " + e + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
}
......@@ -90,11 +101,40 @@ public class PublishStory extends HttpServlet {
HttpServletResponse response,
StoryDAO histoireDAO)
throws ServletException, IOException {
//TODO VERIFY RIGHTS
HttpSession session = request.getSession(false);
System.out.println(request.getParameter("story_id"));
int storyId =Integer.parseInt(request.getParameter("story_id"));
if (session==null || session.getAttribute("username")==null){
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println(-1);
out.println("not connected");
}
return;
}
if(!histoireDAO.isOwner((String) session.getAttribute("username"),storyId)){
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println(-1);
out.println("not the owner of the story");
}
return;
}
if(!histoireDAO.publishStory(storyId)) {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println(-1);
out.println("no path to conclusion found");
}
}
try (PrintWriter out = response.getWriter()) {
out.println(1);
}
return;
}
private void actionUnpublishStory(HttpServletRequest request,
HttpServletResponse response,
StoryDAO histoireDAO)
......
......@@ -39,9 +39,9 @@ public class Register extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Utilisateur utilisateur = new Utilisateur(request.getParameter("username"), request.getParameter("mdp"));
Utilisateur utilisateur = new Utilisateur(request.getParameter("login"), request.getParameter("password"));
new UtilisateurDAO(ds).register(utilisateur);
response.sendRedirect("index.jsp");
this.getServletContext().getRequestDispatcher("/Login").forward(request,response);
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
......
......@@ -5,11 +5,16 @@
*/
package dao;
import modele.Choix;
import modele.Paragraph;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Set;
/**
*
......@@ -20,12 +25,12 @@ public class ChoixDAO extends AbstractDAO {
public ChoixDAO(DataSource ds) {
super(ds);
}
/**
* Get id for the choice (next sequence number of id_choix_seq)
*/
private int getNextIdSequence() {
try ( Connection conn = getConn(); PreparedStatement st = conn.prepareStatement("SELECT id_choix_seq.nextval FROM dual")) {
try (Connection conn = getConn(); PreparedStatement st = conn.prepareStatement("SELECT id_choix_seq.nextval FROM dual")) {
ResultSet rs = st.executeQuery();
rs.next();
int idChoice = rs.getInt("nextval");
......@@ -36,20 +41,20 @@ public class ChoixDAO extends AbstractDAO {
}
public int addChoice(int paragraph_id, String choice) {
int choiceID = getNextIdSequence();
try (Connection conn = getConn()) {
PreparedStatement st = conn.prepareStatement("INSERT INTO choix (id_choix, id_paragraphe_a_continuer, " +
"titre, id_paragraphe_suite,publie) VALUES (?, ?, ?, NULL,0)");
st.setInt(1, choiceID);
st.setInt(2, paragraph_id);
st.setString(3, choice);
st.executeQuery();
return choiceID;
} catch (SQLException e) {
throw new DAOException("add choice Database access error :( : " + e.getMessage(), e);
}
int choiceID = getNextIdSequence();
try (Connection conn = getConn()) {
PreparedStatement st = conn.prepareStatement("INSERT INTO choix (id_choix, id_paragraphe_a_continuer, " +
"titre, id_paragraphe_suite,publie) VALUES (?, ?, ?, NULL,0)");
st.setInt(1, choiceID);
st.setInt(2, paragraph_id);
st.setString(3, choice);
st.executeQuery();
return choiceID;
} catch (SQLException e) {
throw new DAOException("add choice Database access error :( : " + e.getMessage(), e);
}
}
public boolean isLigitim(String username, int id) {
try (Connection conn = getConn()) {
PreparedStatement st = conn.prepareStatement("SELECT * FROM paragraphe WHERE id_paragraphe = ? AND auteur = ?");
......@@ -61,9 +66,9 @@ public class ChoixDAO extends AbstractDAO {
throw new DAOException("choix is legitim Database access error :( : " + e.getMessage(), e);
}
}
public boolean canLock(String username, int choiceID) {
try ( Connection conn = getConn()) {
try (Connection conn = getConn()) {
boolean can = true;
PreparedStatement st = conn.prepareStatement("SELECT * FROM choix WHERE id_choix=? AND NOT id_paragraphe_suite=NULL");
st.setInt(1, choiceID);
......@@ -82,4 +87,76 @@ public class ChoixDAO extends AbstractDAO {
}
}
}
public boolean publishChoice(int choiceId, Set<Integer> history) {
boolean publish = false;
Paragraph paragraph= getChoiceParagraph(choiceId);
if(paragraph==null){
return false;
}
history.add(choiceId);
List<Integer> requires= new ParagraphDAO(dataSource).getReadBefore(paragraph.getId());
for (Integer req:requires){
if(!history.contains(req)){
return false;
}
}
if (paragraph.getIsConclusion()) {
publish = true;
publishChoiceDAO(choiceId);
System.out.println(choiceId+"is conclusion");
}
List<Choix> choices = new ParagraphDAO(dataSource).getChoicesParagraph(paragraph.getId());
for(Choix choix: choices){
if(publishChoice(choix.getId(),history)){
publish = true;
publishChoiceDAO(choiceId);
System.out.println(choiceId+"is published");
}
}
return publish;
}
private void publishChoiceDAO(int choiceId) {
try (Connection conn = getConn()) {
PreparedStatement st = conn.prepareStatement(" UPDATE choix SET publie=1 WHERE id_choix=?");
st.setInt(1, choiceId);
st.executeQuery();
} catch (SQLException exception) {
exception.printStackTrace();
}
}
public Paragraph getChoiceParagraph(int choiceId){
try (Connection conn = getConn()) {
PreparedStatement st = conn.prepareStatement("SELECT * FROM choix WHERE id_choix=? AND id_paragraphe_suite IS NOT NULL");
st.setInt(1, choiceId);
ResultSet rs = st.executeQuery();
if (rs.next()) {
Paragraph paragraph = new ParagraphDAO(dataSource).getParagraph(rs.getInt("id_paragraphe_suite"));
return paragraph;
} else {
return null;
}
} catch (SQLException e) {
throw new DAOException("publish choice Database access error :( : " + e.getMessage(), e);
}
}
public List<Integer> getReadBefore(int id) {
Paragraph paragraph = getChoiceParagraph(id);
if(paragraph==null){
return null;
}
List<Integer> readBefore= new ParagraphDAO(dataSource).getReadBefore(paragraph.getId());
if(readBefore.isEmpty()){
return null;
}
return readBefore;
}
}
\ No newline at end of file
......@@ -12,6 +12,7 @@ import javax.sql.DataSource;
import java.sql.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
/**
*
......@@ -319,4 +320,31 @@ public class ParagraphDAO extends AbstractDAO {
}
}
public void setReadBefore(int paragraphId, int beforeParagraphId){
try ( Connection conn = getConn()) {
PreparedStatement st = conn.prepareStatement("INSERT INTO alireavant (id_paragraphe, id_paragraphe_precedent) VALUES (?, ?)");
st.setInt(1, paragraphId);
st.setInt(2, beforeParagraphId);
st.executeQuery();
} catch (SQLException e) {
throw new DAOException(" set read before Database access error :( : " + e.getMessage(), e);
}
}
public List<Integer> getReadBefore(int paragraphId){
try ( Connection conn = getConn(); PreparedStatement st = conn.prepareStatement("SELECT * FROM alireavant WHERE id_paragraphe=?")) {
st.setInt(1, paragraphId);
ResultSet rs = st.executeQuery();
List<Integer> ll = new LinkedList<>();
while (rs.next()) {
ll.add(rs.getInt("id_paragraphe_precedent"));
}
return ll;
} catch (Exception e) {
throw new DAOException(" get read before Database access error :( : " + e.getMessage(), e);
}
}
}
......@@ -8,8 +8,10 @@ package dao;
import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import modele.Choix;
import modele.Story;
import modele.Utilisateur;
......@@ -294,4 +296,34 @@ public class StoryDAO extends AbstractDAO {
throw new DAOException("isOwner Database access error :( : " + e.getMessage(), e);
}
}