diff --git a/GroupKOPS_COMP303_Assignment3_JPA/build/classes/com/faq/app/dao/DatabaseOperations.class b/GroupKOPS_COMP303_Assignment3_JPA/build/classes/com/faq/app/dao/DatabaseOperations.class index a0cadfe..d39d50c 100644 Binary files a/GroupKOPS_COMP303_Assignment3_JPA/build/classes/com/faq/app/dao/DatabaseOperations.class and b/GroupKOPS_COMP303_Assignment3_JPA/build/classes/com/faq/app/dao/DatabaseOperations.class differ diff --git a/GroupKOPS_COMP303_Assignment3_JPA/build/classes/com/faq/app/model/Faq.class b/GroupKOPS_COMP303_Assignment3_JPA/build/classes/com/faq/app/model/Faq.class index d8b488c..9df242c 100644 Binary files a/GroupKOPS_COMP303_Assignment3_JPA/build/classes/com/faq/app/model/Faq.class and b/GroupKOPS_COMP303_Assignment3_JPA/build/classes/com/faq/app/model/Faq.class differ diff --git a/GroupKOPS_COMP303_Assignment3_JPA/src/com/faq/app/dao/DatabaseOperations.java b/GroupKOPS_COMP303_Assignment3_JPA/src/com/faq/app/dao/DatabaseOperations.java index 0212584..fdb06a5 100644 --- a/GroupKOPS_COMP303_Assignment3_JPA/src/com/faq/app/dao/DatabaseOperations.java +++ b/GroupKOPS_COMP303_Assignment3_JPA/src/com/faq/app/dao/DatabaseOperations.java @@ -18,7 +18,30 @@ public class DatabaseOperations { public static List getAllFaqRecords() { TypedQuery query = em.createNamedQuery("Faq.findAll", Faq.class); + return getFaqQueryResults(query); // Query query = em.createQuery("SELECT f FROM Faq f"); + // List faqList = query.getResultList(); + // if (faqList != null && faqList.size() > 0) { + // return faqList; + // } else { + // return null; + // } + } + + public static List getAllFaqRecordsWithTopicNameContaining(String topicName) { + TypedQuery query = em.createNamedQuery("Faq.findByContainingTopicName", Faq.class); + query.setParameter("topicName", topicName); + return getFaqQueryResults(query); + // List faqList = query.getResultList(); + // if (faqList != null && faqList.size() > 0) { + // return faqList; + // } else { + // return null; + // } + } + + // Helper functions + private static List getFaqQueryResults(TypedQuery query) { List faqList = query.getResultList(); if (faqList != null && faqList.size() > 0) { return faqList; diff --git a/GroupKOPS_COMP303_Assignment3_JPA/src/com/faq/app/model/Faq.java b/GroupKOPS_COMP303_Assignment3_JPA/src/com/faq/app/model/Faq.java index 7267067..3a26c11 100644 --- a/GroupKOPS_COMP303_Assignment3_JPA/src/com/faq/app/model/Faq.java +++ b/GroupKOPS_COMP303_Assignment3_JPA/src/com/faq/app/model/Faq.java @@ -6,6 +6,7 @@ import javax.persistence.Entity; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; +import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; /** @@ -13,7 +14,8 @@ * */ @Entity -@NamedQuery(name = "Faq.findAll", query = "SELECT f FROM Faq f") +@NamedQueries({ @NamedQuery(name = "Faq.findAll", query = "SELECT f FROM Faq f"), + @NamedQuery(name = "Faq.findByContainingTopicName", query = "SELECT f FROM Faq f WHERE LOWER(f.topic.topicName) LIKE LOWER(CONCAT('%', :topicName, '%'))") }) public class Faq implements Serializable { private static final long serialVersionUID = 1L; diff --git a/GroupKOPS_COMP303_Assignment3_Web/WebContent/index.jsp b/GroupKOPS_COMP303_Assignment3_Web/WebContent/index.jsp index fd7f467..0362f87 100644 --- a/GroupKOPS_COMP303_Assignment3_Web/WebContent/index.jsp +++ b/GroupKOPS_COMP303_Assignment3_Web/WebContent/index.jsp @@ -56,8 +56,10 @@ - Please enter in the name of a topic title above and - press search to find questions related to the topic. +

Please enter in the name of a topic title above and press + search to find questions related to the topic.

+

If the topic name field is left blank, all questions will be + displayed.

diff --git a/GroupKOPS_COMP303_Assignment3_Web/build/classes/com/faq/app/servlet/Faqs.class b/GroupKOPS_COMP303_Assignment3_Web/build/classes/com/faq/app/servlet/Faqs.class index 44db2a9..d27b567 100644 Binary files a/GroupKOPS_COMP303_Assignment3_Web/build/classes/com/faq/app/servlet/Faqs.class and b/GroupKOPS_COMP303_Assignment3_Web/build/classes/com/faq/app/servlet/Faqs.class differ diff --git a/GroupKOPS_COMP303_Assignment3_Web/src/com/faq/app/servlet/Faqs.java b/GroupKOPS_COMP303_Assignment3_Web/src/com/faq/app/servlet/Faqs.java index 71c4cfa..0134c68 100644 --- a/GroupKOPS_COMP303_Assignment3_Web/src/com/faq/app/servlet/Faqs.java +++ b/GroupKOPS_COMP303_Assignment3_Web/src/com/faq/app/servlet/Faqs.java @@ -26,8 +26,8 @@ public class Faqs extends HttpServlet { */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - // TODO Auto-generated method stub - response.getWriter().append("Served at: ").append(request.getContextPath()); + // should not be able to do get request to /Faqs, redirects to landing page + response.sendRedirect(""); } /** @@ -37,18 +37,20 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - List faqQueryResults = DatabaseOperations.getAllFaqRecords(); - System.out.println("Found faqs: " + faqQueryResults); + // List faqQueryResults = DatabaseOperations.getAllFaqRecords(); + // System.out.println("Found faqs: " + faqQueryResults); // when a user has made a post request to /Faqs, then they have queried the // db...otherwise they simply visited page, no queries executed yet (show diff // view on page) HttpSession session = request.getSession(); + String queriedTopicName = request.getParameter("topicName"); + List faqQueryResults = DatabaseOperations.getAllFaqRecordsWithTopicNameContaining(queriedTopicName); // if (request.getAttribute("userQueriedDb") == null) { request.setAttribute("userQueriedDb", true); // } - request.setAttribute("queriedTopicName", request.getParameter("topicName")); + request.setAttribute("queriedTopicName", queriedTopicName); request.setAttribute("matchingFaqs", faqQueryResults); request.getRequestDispatcher("index.jsp").forward(request, response); }