From 7cc31c4dbe4e543176f40aa0f5f4fc85cef73322 Mon Sep 17 00:00:00 2001 From: Kevin Ma Date: Thu, 19 Apr 2018 19:42:10 -0400 Subject: [PATCH] Close #1; Add app logic to query db for topic name based on regex and display faqs with matching topic names; Add re-routing of GET on /Faqs to landing page --- .../com/faq/app/dao/DatabaseOperations.class | Bin 1729 -> 2415 bytes .../build/classes/com/faq/app/model/Faq.class | Bin 1704 -> 1898 bytes .../com/faq/app/dao/DatabaseOperations.java | 23 ++++++++++++++++++ .../src/com/faq/app/model/Faq.java | 4 ++- .../WebContent/index.jsp | 6 +++-- .../classes/com/faq/app/servlet/Faqs.class | Bin 2463 -> 2086 bytes .../src/com/faq/app/servlet/Faqs.java | 12 +++++---- 7 files changed, 37 insertions(+), 8 deletions(-) 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 a0cadfe33d2b756b92f4b2eb75d475259b1f5c1b..d39d50c6d4a13b65fe675244239189dba29ba9c2 100644 GIT binary patch delta 773 zcmZuvOK%cU6#fn~2&DtsiPFYEt&gO@#8OS;LPSenws!0Ts`#v#(h-LsD09(NO`5oN zH%k+DF5FdD;=;s@3m2OB6Z`@C2aNGtsI^2l_uljR9%tr5_=g_)^y}Sw0AslII`mCB z8Ui7s;S90_y=;pa>;7`nZfxXjw^p-22-@5Kd}0n8I(-WMg-ui+vV2%2k)C9A>m zgxw%ad9Reg;Nn`vE{ROiTUCdAYtnVx`>Gz4%+nHQSwkLaLYVz_wj=k@ixpfB;1VIy zy&w%mTp_5g^T;MBiR7I3L_Nf4&&cmR_P^ZBL*d;6`WwXclloW-rxeVm-s1EYXB71@*qp9hIXR7Y9+e%r{={HkW1%dZY(M#D7++%uUF;+!`MWr*CN=rEE zDltoqGIi!?WK2_enO*j>&NTZRAn5&LhZ&onz`lZkvNqy}x<%VkuL+J+q;dQQ3m-8( 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 d8b488cda3821999d8020c7277a9c4935d3bd7f5..9df242cd725feb916001ac6d38a3d06b3821119b 100644 GIT binary patch delta 311 zcmZ3%`-+e2)W2Q(7#J8#7~D2;B{DNAOwM7J;mJ%bwq|5tElbQPO`ROTC^osC*}R^S zLDns?P%kYrFU6_SIX|x?F*7eSFFho`AT!x7F*lWwAv)OA$JIGRAx*(8$lq51s2a!& z_iznzRq*i-cMZ}=(<=d~hL9jVnhHLi-Y{8be?Mo(5Dj%zbsYsOutJa$9R;9(rY6JW z?aY%{l^9qUlqdVK*f6S1?q;!N)nZ^}(4KssRebUj7JWv&$s(+_tVTdFW3X5*t3IRI zmM4Z-4KZ2F8QVDV@+4OR=FxFuM8 mK2Y2SC~nK@02FfsifN0AFfcLL0$pJTq{WLF>=_)285{x5&?B?} 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 44db2a9ce8e52725cb9bf148aaa969ce7f7725fe..d27b567f1be390a14592b000a2ef36221511c0e1 100644 GIT binary patch delta 729 zcmYL`O;giQ6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O~31;KbZ?vQYlmGw# 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); }