Commit f21da174 authored by clc4217's avatar clc4217
Browse files

merged version with CSS with version with db

parents e6314519 ca025894
Pipeline #65967 passed with stages
in 4 minutes and 5 seconds
......@@ -17,4 +17,4 @@ deploy:
stage: deploy
script:
- mvn package
- dpl --provider=heroku --app=clc4217-simplewebapp --api-key=f469d3bc-ca9c-4cb0-bf44-b7dabdd23168
- dpl --provider=heroku --app=clc4217-simplewebapp --api-key=$API_KEY
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>demo</groupId>
<artifactId>shared-resources</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>CSS Resources</name>
</project>
\ No newline at end of file
package ic.doc;
import java.util.List;
public class Query {
private String query;
private String name;
private String description;
private String imgURL;
private String wikiURL;
public Query(String query, String description, String imgURL, String wikiURL) {
this.query = query;
public Query(String name, String description, String imgURL, String wikiURL) {
this.name = name;
this.description = description;
if(query != null) {
if(name != null) {
this.description = this.description.concat("\n");
}
this.imgURL = imgURL;
this.wikiURL = wikiURL;
}
public String getQuery() {
return query;
public String getName() {
return name;
}
public String getDescription() {
......
......@@ -13,18 +13,10 @@ public class QueryProcessor {
this.dbReader = dbReader;
}
// this.queriesMap.add(new Query(Arrays.asList("augusta", "ada", "king", "lovelace"),
// "Augusta Ada King, Countess of Lovelace (10 December 1815 - 27 November\n" +
// "November 1852) was an English mathematician and writer, chiefly\n" +
// "known for her work on Charles Babbage's proposed mechanical\n" +
// "general-purpose computer, the Analytical Engine. She was the first to\n" +
// "recognise that the machine had applications beyond pure calculation,\n" +
// "and published the first algorithm intended to be carried out by such\n" +
// "a machine. As a result, she is sometimes regarded as the first to\n" +
// "recognise the full potential of a \"computing machine\" and the first\n" +
// "computer programmer.",
// "https://upload.wikimedia.org/wikipedia/commons/0/0b/Ada_Byron_daguerreotype_by_Antoine_Claudet_1843_or_1850.jpg",
// "https://en.wikipedia.org/wiki/Ada_Lovelace"));
// this.queriesMap.add(new Query(Arrays.asList(""),
// ",
// "",
// ));
public ResultSet process(String query) {
......
......@@ -38,37 +38,51 @@ public class WebServer {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String query = req.getParameter("q");
String type = req.getParameter("type");
if (query == null || type == null) {
new IndexPage().writeTo(resp);
String type = req.getParameter("type"); // || type == null
if (query == null) {
if(type != null) {
String[] queryAndType = type.split(",");
String[] q = queryAndType[0].split("\\+");
renderResultsPage(String.join(" ", q), queryAndType[1], resp);
} else {
new IndexPage().writeTo(resp);
}
} else {
ResultSet queryRes = queryProcessor.process(query);
List<Query> possibilities = new ArrayList<>();
if (queryRes != null) {
try {
while (queryRes.next()) {
String name = queryRes.getString("name");
String description = queryRes.getString("description");
String imgUrl = queryRes.getString("imgUrl");
String wikiUrl = queryRes.getString("wikiUrl");
possibilities.add(new Query(name, description, imgUrl, wikiUrl));
}
}
catch (Exception e) {
System.out.println("Unknown error occurred");
renderResultsPage(query, "html", resp);
}
}
private void renderResultsPage(String query, String type, HttpServletResponse resp) throws IOException {
ResultSet queryRes = queryProcessor.process(query);
List<Query> possibilities = new ArrayList<>();
if (queryRes != null) {
try {
while (queryRes.next()) {
String name = queryRes.getString("name");
String description = queryRes.getString("description");
String imgUrl = queryRes.getString("imgUrl");
String wikiUrl = queryRes.getString("wikiUrl");
possibilities.add(new Query(name, description, imgUrl, wikiUrl));
}
if (possibilities.size() == 1) {
Query answer = possibilities.get(0);
if (type.equals("html")) {
new HTMLResultPage(query, answer).writeTo(resp);
} else {
new DownloadPage(query, answer, type).writeTo(resp);
}
} catch (Exception e) {
System.out.println("Unknown error occurred");
}
}
if (possibilities.size() == 1) {
Query answer = possibilities.get(0);
if(type != null) {
if (type.equals("html")) {
new HTMLResultPage(query, answer).writeTo(resp);
} else {
new ChoicePage(query, possibilities).writeTo(resp);
// may not need a choice page class, just put code here
new DownloadPage(query, answer, type).writeTo(resp);
}
}
} else if (possibilities.size() > 1 ){
new ChoicePage(query, possibilities).writeTo(resp);
}
else {
new HTMLResultPage("", null).writeTo(resp);
}
}
}
......
......@@ -23,7 +23,11 @@ public class ChoicePage implements Page {
// Header
writer.println("<html>");
writer.println("<head><title>" + ambiguousQuery + "</title></head>");
writer.println("<head>");
writer.println("<style>");
writer.println(htmlStyle);
writer.println("</style>");
writer.println("<title>" + ambiguousQuery + "</title></head>");
writer.println("<body>");
// Content - list of hyperlinked choices
......@@ -31,7 +35,7 @@ public class ChoicePage implements Page {
"<p>" + ambiguousQuery + " gave many results. Select which result you would like to view " +
"<br><br>");
while (!possibilities.isEmpty()) {
String nameString = possibilities.get(0).getQuery();
String nameString = possibilities.get(0).getName();
String name = String.join(" ", nameString);
String ref = String.join("+", nameString);
writer.print("<a href=\"/?q=" + ref + "&type=html\">" + name + "</a>" +
......
......@@ -22,7 +22,9 @@ public class HTMLResultPage implements Page {
// Header
writer.println("<html>");
writer.println("<head><title>" + query + "</title></head>");
writer.println("<head>"+
"<style>"+htmlStyle+"</style>"+
"<title>" + query + "</title></head>");
writer.println("<body>");
......@@ -30,13 +32,29 @@ public class HTMLResultPage implements Page {
if (answer == null || answer.getDescription() == null || answer.getDescription().isEmpty()) {
writer.println("<h1>Sorry</h1>");
writer.print("<p>Sorry, we didn't understand <em>" + query + "</em></p>");
writer.println("<p><a href=\"/\">Back to Search Page</a></p>");
} else {
writer.println("<h1>" + query + "</h1>");
writer.println("<p>" + answer.getDescription().replace("\n", "<br>") + "</p>");
writer.println("<p> <img src=\"" + answer.getImgURL() + "\"> </p>");
String name = String.join("+", answer.getName());
writer.println("<header>");
writer.println("<h1>" + answer.getName() + "</h1>");
writer.println("</header>");
writer.println("<p class=\"information\">" + answer.getDescription().replace("\n", "<br>") + "</p>");
writer.println("<img src=\"" + answer.getImgURL() + "\">");
writer.println("<form>");
writer.println("<button type=\"submit\" name=\"type\" value=\""+name+",markdown\"> Markdown </button>");
writer.println("<button type=\"submit\" name=\"type\" value=\""+name+",pdf\"> PDF </button>");
writer.println("</form>");
writer.println("<p><a href=\""+answer.getWikiURL()+"\" target=\"_blank" +
"\">Learn More!</a></p>");
writer.println("<p><a href=\"/\">Back to Search Page</a></p>");
writer.println("<div class=\"credits\">");
writer.println("<a href=\"" + answer.getImgURL() + "\"> Image credits: from wikipedia");
writer.println("</div>");
}
......
......@@ -12,7 +12,11 @@ public class IndexPage implements Page {
// Header
writer.println("<html>");
writer.println("<head><title>Welcome</title></head>");
writer.println("<head>");
writer.println("<style>");
writer.println(htmlStyle);
writer.println("</style>");
writer.println("<title>Welcome</title></head>");
writer.println("<body>");
// Content
......@@ -21,12 +25,6 @@ public class IndexPage implements Page {
"<form>" +
"<input type=\"text\" name=\"q\" />" +
"<br><br>" +
"<input type=\"radio\" name=\"type\" value=\"html\"> HTML" +
"<br>" +
"<input type=\"radio\" name=\"type\" value=\"markdown\"> Download Markdown File" +
"<br>" +
"<input type=\"radio\" name=\"type\" value=\"pdf\"> PDF" +
"<br><br>" +
"<input type=\"submit\">" +
"</form>" +
"</p>");
......
......@@ -4,6 +4,112 @@ import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public interface Page {
String htmlStyle = "body {\n" +
" text-align: center;\n" +
" background-color: rgb(255, 255, 153);\n" +
" /* background-image: url(\"https://ae01.alicdn.com/kf/HTB1nAkRi9YH8KJjSspdq6ARgVXa4/white-damask-gray-grey-Floral-pattern-Background-Vinyl-cloth-High-quality-Computer-print-wall-photo-backdrop.jpg\"); */\n" +
"}\n" +
"\n" +
"header {\n" +
" position: sticky;\n" +
" top: 0;\n" +
" left: 0;\n" +
" right: 0;\n" +
" background-color: #ff9966;\n" +
"}\n" +
"\n" +
"h1 {\n" +
" color: teal;\n" +
" font-family: \"Old English Text MT\", cursive;\n" +
" /* background-color: maroon; */\n" +
" font-size: 100px;\n" +
" width: 100%;\n" +
"\n" +
"}\n" +
"\n" +
".information {\n" +
" text-align: left;\n" +
" width: 50%;\n" +
" left: 0;\n" +
" display: inline-block;\n" +
" font-size: 150%;\n" +
"}\n" +
"\n" +
"p {\n" +
" font-size: 200%;\n" +
" /* display: inline-block; */\n" +
"\n" +
" font-family: cursive;\n" +
" /* background-color: rebeccapurple; */\n" +
"}\n" +
"\n" +
".forms {\n" +
" /* background-color: rgb(204, 255, 204); */\n" +
"}\n" +
"\n" +
".forms input[type=\"text\"] {\n" +
" width: 250px;\n" +
" padding: 10px;\n" +
" font-weight: bold;\n" +
" background-color: yellow;\n" +
" font-family: \"Old English Text MT\", fantasy;\n" +
" font-size: 120%;\n" +
" /* color: blue; */\n" +
"}\n" +
"\n" +
".forms input[type=\"radio\"] {\n" +
" cursor: pointer;\n" +
" position: relative;\n" +
"}\n" +
"\n" +
".forms input[type=\"submit\"] {\n" +
" cursor: pointer;\n" +
" height: 50px;\n" +
" width: 100px;\n" +
" font-weight: bold;\n" +
" font-size: 120%;\n" +
" font-family: \"Old English Text MT\", cursive;\n" +
" background-color: maroon;\n" +
" color: yellow;\n" +
"}\n" +
"\n" +
".forms input[type=\"submit\"]:hover {\n" +
" background-color: blue;\n" +
"}\n" +
"\n" +
".radio-value {\n" +
" font-family: Stencil, cursive;\n" +
" font-size: 150%\n" +
"}\n" +
"\n" +
"img {\n" +
" display: inline-block;\n" +
" border-radius: 50%;\n" +
" /* align: right; */\n" +
" width: 250px;\n" +
" margin-right: 100px;\n" +
" /* padding-right: 40px; */\n" +
"}\n" +
"\n" +
"a:visited {\n" +
" color: red;\n" +
"}\n" +
"\n" +
"a:hover {\n" +
" color: rgb(51, 204, 51);\n" +
"}\n" +
"\n" +
"a:active {\n" +
" color: blue;\n" +
"}\n" +
"\n" +
".credits {\n" +
" position: static;\n" +
" text-align: left;\n" +
" bottom: 0;\n" +
" left: 0;\n" +
" font-size: 20px;\n" +
"}\n";
void writeTo(HttpServletResponse resp) throws IOException;
}
No preview for this file type
No preview for this file type
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment