diff --git a/DataBaseConnectionService.java b/DataBaseConnectionService.java new file mode 100644 index 0000000000000000000000000000000000000000..996b2e79b67ce2e0791b66435c79f8696d8478bf --- /dev/null +++ b/DataBaseConnectionService.java @@ -0,0 +1,79 @@ +package myserver.example; + +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.ProtocolException; +import java.net.URL; +import org.basex.util.Base64; + +public final class DataBaseConnectionService { + + public static String executeQuery(String query) { + StringBuffer response; + try { + // The java URL connection to the server of Thierry. + URL url = new URL("http://localhost:8080/BaseX821/rest/Flugzeuge"); + + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + + conn.setRequestMethod("POST"); + + // User and password. + String user = "admin"; + String pw = "admin"; + + String encoded = Base64.encode(user + ":" + pw); + conn.setRequestProperty("Authorization", "Basic " + encoded); + String q = "<query xmlns=\"http://basex.org/rest\"> " + "<text><![CDATA[ " + query + " ]]></text> " + " </query>"; + conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + conn.setRequestProperty("Content-Length", "" + Integer.toString(query.getBytes().length)); + conn.setRequestProperty("Content-Language", "en-US"); + conn.setUseCaches(false); + conn.setDoInput(true); + conn.setDoOutput(true); + + // Send request + DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); + wr.writeBytes(q); + wr.flush(); + wr.close(); + + // get response + InputStream is = conn.getInputStream(); + BufferedReader rd = new BufferedReader(new InputStreamReader(is, "UTF8")); + String line; + response = new StringBuffer(); + while ((line = rd.readLine()) != null) { + response.append(line); + response.append('\n'); + } + rd.close(); + conn.disconnect(); + return response.toString(); + + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (ProtocolException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return ""; + } + + // debugging.... can be deleted + public static void main(String[] args) throws Exception { + // String XPATHQUERY = "(//plane/name)[position() <= 5]"; + + String XPATHQUERY = "(for $p in /planes/plane return (fn:string($p/name), fn:string($p/stueck), fn:string($p/erstflug), fn:string($p/beschreibung), fn:string($p/bild), fn:string($p/herstellerName), fn:string($p/herstellerBeschreibung)))[position() <= 28]"; + String response = executeQuery(XPATHQUERY); + System.out.println(response); + + } +} \ No newline at end of file diff --git a/DatabaseUpdater.jar b/DatabaseUpdater.jar new file mode 100644 index 0000000000000000000000000000000000000000..10075b27dd8ebd5d41adf77e8bad44058414c93e Binary files /dev/null and b/DatabaseUpdater.jar differ diff --git a/DatabaseUpdater.java b/DatabaseUpdater.java new file mode 100644 index 0000000000000000000000000000000000000000..696f672b19aacd630a2ea1ce231405e8a34b50bd --- /dev/null +++ b/DatabaseUpdater.java @@ -0,0 +1,123 @@ +package updater; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.ProtocolException; +import java.net.URL; +import org.basex.util.Base64; + +public final class DatabaseUpdater { + + public static boolean delete(String filename) { + try { + URL url = new URL("http://localhost:8080/BaseX821/rest/Flugzeuge/" + filename); + + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + + conn.setRequestMethod("DELETE"); + + String user = "admin"; + String pw = "admin"; + + String encoded = Base64.encode(user + ":" + pw); + conn.setRequestProperty("Authorization", "Basic " + encoded); + conn.setUseCaches(false); + conn.setDoInput(true); + conn.setDoOutput(true); + + InputStream is = null; + try { + is = conn.getInputStream(); + } catch (IOException ioe) { + is = conn.getErrorStream(); + } + BufferedReader rd = new BufferedReader(new InputStreamReader(is, "UTF8")); + while (rd.readLine() != null) { + } + rd.close(); + conn.disconnect(); + return conn.getResponseCode() == 200; + + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (ProtocolException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return false; + } + + public static boolean put(String filename) { + try { + URL url = new URL("http://localhost:8080/BaseX821/rest/Flugzeuge/" + filename); + + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + + conn.setRequestMethod("PUT"); + + String user = "admin"; + String pw = "admin"; + File f = new File(filename); + String encoded = Base64.encode(user + ":" + pw); + conn.setRequestProperty("Authorization", "Basic " + encoded); + conn.setRequestProperty("Content-Type", "application/xml"); + conn.setRequestProperty("Content-Length", "" + Long.toString(f.length())); + conn.setRequestProperty("Content-Language", "en-US"); + conn.setUseCaches(false); + conn.setDoInput(true); + conn.setDoOutput(true); + + FileInputStream fis = new FileInputStream(f); + OutputStream os = conn.getOutputStream(); + byte[] buffer = new byte[256]; + int bytesRead = 0; + while ((bytesRead = fis.read(buffer)) != -1) { + os.write(buffer, 0, bytesRead); + } + fis.close(); + os.flush(); + os.close(); + + InputStream is = null; + try { + is = conn.getInputStream(); + } catch (IOException ioe) { + is = conn.getErrorStream(); + } + BufferedReader rd = new BufferedReader(new InputStreamReader(is, "UTF8")); + while (rd.readLine() != null) { + } + rd.close(); + conn.disconnect(); + return conn.getResponseCode() == 201; + + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (ProtocolException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return false; + } + + public static void main(String[] args) throws Exception { + if (args.length != 1) { + throw new IllegalArgumentException("Invalid Parameters!"); + } + // xml_Database_v3.xml + delete(args[0]); + Thread.sleep(2000); + put(args[0]); + } +} \ No newline at end of file diff --git a/RestServer.java b/RestServer.java new file mode 100644 index 0000000000000000000000000000000000000000..423f651d47c6f5242d80293463ca3f735195e744 --- /dev/null +++ b/RestServer.java @@ -0,0 +1,48 @@ +package myserver.example; + +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.MediaType; + +// Plain old Java Object it does not extend as class or implements +// an interface + +// The class registers its methods for the HTTP GET request using the @GET annotation. +// Using the @Produces annotation, it defines that it can deliver several MIME types, +// text, XML and HTML. +// http://q1k.de:8088/ /home/xml/res +// http://q1k.de:8088/xml3/xml_Database_v3.xml + +// The browser requests per default the HTML MIME type. + +// Sets the path to base URL + /any Element +@Path("/") +public class RestServer extends Application { + + // This method is called if XML is request + @GET + @Path("/planes") + @Produces(MediaType.APPLICATION_XML) + public String getName() { + String query = "for $p in /planes\nreturn $p"; + String result = DataBaseConnectionService.executeQuery(query); + return result; + } + + @GET + @Path("/plane/{name}") + @Produces(MediaType.APPLICATION_XML) + public String getStueck(@PathParam("name") String name) { + try { + name = URLDecoder.decode(name, "UTF-8"); + } catch (UnsupportedEncodingException e1) {} + String query = "for $p in /planes/plane\n where $p/name = '"+name+"'\n return $p"; + String result = DataBaseConnectionService.executeQuery(query); + return result; + } +} \ No newline at end of file diff --git a/quiz.css b/quiz.css new file mode 100644 index 0000000000000000000000000000000000000000..13f38702de24780a24ab47f6e0ca7db3f3785817 --- /dev/null +++ b/quiz.css @@ -0,0 +1,31 @@ +.red { + background: #F33 +} + +.green { + background: #3F3 +} + +img { + min-width: 300px; + max-height: 300px; +} + +body { + text-align: center; + background-color: #FAE7B1; +} + +table { + margin-left: auto; + margin-right: auto; +} + +.invis { + display: none; +} + +.trtext { + text-align: left; + max-width: 300px; +} \ No newline at end of file diff --git a/quiz.js b/quiz.js new file mode 100644 index 0000000000000000000000000000000000000000..5d3c8f47f34ff73bc2b9e930e53483b8ca16e353 --- /dev/null +++ b/quiz.js @@ -0,0 +1,89 @@ +$(document).ready(function(){ + $('input[type=radio]').prop('checked', false); +}); + +selection = [0, 0, 0, 0]; + +function swap(number, option) { + if(selection[number - 1] == option) { + return; + } + var conflict = isSelected(option); + if(conflict == 0) { + selection[number - 1] = option; + return; + } + if(selection[number - 1] != 0) { + if(selection[number - 1] > option) { + var tmp = isSelected(option); + for(i = option; i < selection[number - 1]; i++) { + if(tmp == 0){ + break; + } + var tmp2 = isSelected(i + 1); + selection[tmp - 1]++; + tmp = tmp2; + } + } else { + var tmp = isSelected(option); + for(i = option; i > selection[number - 1]; i--) { + if(tmp == 0){ + break; + } + var tmp2 = isSelected(i - 1); + selection[tmp - 1]--; + tmp = tmp2; + } + } + } else { + + } + selection[number - 1] = option; + var list = $('input[type=radio][name=first]'); + for(i = 4; i --> 0;) { + $(list[i]).prop('checked', i + 1 == selection[0]); + } + var list = $('input[type=radio][name=second]'); + for(i = 4; i --> 0;) { + $(list[i]).prop('checked', i + 1 == selection[1]); + } + var list = $('input[type=radio][name=third]'); + for(i = 4; i --> 0;) { + $(list[i]).prop('checked', i + 1 == selection[2]); + } + var list = $('input[type=radio][name=fourth]'); + for(i = 4; i --> 0;) { + $(list[i]).prop('checked', i + 1 == selection[3]); + } +} + +function isSelected(option) { + var conflict = 0; + for(var i = 4; i --> 0;){ + if(selection[i] == option){ + conflict = i + 1; + } + } + return conflict; +} + +function checkResult() { + var correct = true; + for(var i = 4; i --> 0;){ + if(correctList[i] != selection[i]){ + correct = false; + $('.selector')[i].classList.remove('green'); + $('.selector')[i].classList.add('red'); + }else{ + $('.selector')[i].classList.remove('red'); + $('.selector')[i].classList.add('green'); + } + } + if(!correct){ + return; + } + var xl = $('.invis'); + for(var i = xl.length; i --> 0;){ + xl[i].classList.remove('invis'); + } +} \ No newline at end of file diff --git a/quiz.jsp b/quiz.jsp new file mode 100644 index 0000000000000000000000000000000000000000..072494e710b71962688c9fa149ea9e793b4ee605 --- /dev/null +++ b/quiz.jsp @@ -0,0 +1,197 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" + pageEncoding="UTF-8"%> +<%@page import="org.basex.query.value.item.Item"%> +<%@page import="org.basex.query.iter.Iter"%> +<%@page import="org.basex.query.QueryProcessor"%> +<%@page import="org.basex.core.Context"%> +<%@page import="org.basex.core.cmd.XQuery"%> +<%@page import="java.util.Date"%> +<%@page import="java.text.SimpleDateFormat"%> +<%@page import="java.io.BufferedReader"%> +<%@page import="java.io.DataOutputStream"%> +<%@page import="java.io.IOException"%> +<%@page import="java.io.InputStream"%> +<%@page import="java.io.InputStreamReader"%> +<%@page import="java.net.HttpURLConnection"%> +<%@page import="java.net.MalformedURLException"%> +<%@page import="java.net.ProtocolException"%> +<%@page import="java.net.URL"%> +<%@page import="org.basex.util.Base64"%> +<% + String query = "declare namespace math = \"java:java.lang.Math\";\n" + + "(for $p in /planes/plane\n" + + "order by math:random()\n" + + "return (fn:string($p/name), fn:string($p/stueck), fn:string($p/erstflug), fn:string($p/beschreibung), fn:string($p/bild), fn:string($p/herstellerName), fn:string($p/herstellerBeschreibung)))[position() <= 28]"; + String[] name = new String[4]; + int[] stueck = new int[4]; + Date[] dates = new Date[4]; + Date never = new Date(); + String[] abstracts = new String[4]; + String[] imgUrls = new String[4]; + String[] manufactor = new String[4]; + String[] mfabstract = new String[4]; + SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd'Z'"); + SimpleDateFormat sdf2 = new SimpleDateFormat("YYYY"); + + BufferedReader br = null; + try { + // The java URL connection to the server of Thierry. + URL url = new URL("http://localhost:8080/BaseX821/rest/Flugzeuge"); + + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + + conn.setRequestMethod("POST"); + + // User and password. + String user = "admin"; + String pw = "admin"; + + String encoded = Base64.encode(user + ":" + pw); + conn.setRequestProperty("Authorization", "Basic " + encoded); + String q = "<query xmlns=\"http://basex.org/rest\"> " + "<text><![CDATA[ " + query + " ]]></text> " + " </query>"; + conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + conn.setRequestProperty("Content-Length", "" + Integer.toString(query.getBytes().length)); + conn.setRequestProperty("Content-Language", "en-US"); + conn.setUseCaches(false); + conn.setDoInput(true); + conn.setDoOutput(true); + + // Send request + DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); + wr.writeBytes(q); + wr.flush(); + wr.close(); + + // get response + InputStream is = conn.getInputStream(); + br = new BufferedReader(new InputStreamReader(is, "UTF8")); + } catch (MalformedURLException e) { + //e.printStackTrace(); + } catch (ProtocolException e) { + //e.printStackTrace(); + } catch (IOException e) { + //e.printStackTrace(); + } + + for (int i = 0; i < 4; i++) { + name[i] = br.readLine(); + stueck[i] = Integer.parseInt(br.readLine()); + String date = br.readLine(); + if (date.length() == 4) { + dates[i] = sdf2.parse(date); + } else if (date.length() == 11) { + dates[i] = sdf.parse(date); + } else { + dates[i] = never; + } + abstracts[i] = br.readLine(); + imgUrls[i] = br.readLine(); + manufactor[i] = br.readLine(); + mfabstract[i] = br.readLine(); + } + br.close(); + boolean search = Math.random() > 0.5; + int[] correct = new int[4]; + for (int i = 0; i < 4; i++) { + correct[i] = i + 1; + } + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + if(i == j){ + continue; + } + if (search) { + if (dates[j].after(dates[i]) == correct[j] < correct[i]) { + int tmp = correct[j]; + correct[j] = correct[i]; + correct[i] = tmp; + } + } else { + if (stueck[j] < stueck[i] == correct[j] < correct[i]) { + int tmp = correct[j]; + correct[j] = correct[i]; + correct[i] = tmp; + } + } + } + } + String searchFor = search ? "Erstflugsdatum" : "Stückzahl" ; +%> +<!DOCTYPE html> +<html xmlns="http://www.w3.org/1999/xhtml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:foaf="http://xmlns.com/foaf/0.1/"> +<head> +<title>XML Projekt: Flugzeug-Quiz</title> +<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> +<link rel="stylesheet" type="text/css" href="quiz.css" /> +<script src="jquery-2.1.4.min.js"></script> +<script> + correctList = [ +<%=correct[0]%> + , +<%=correct[1]%> + , +<%=correct[2]%> + , +<%=correct[3]%> + ]; +</script> +<script src="quiz.js"></script> +</head> +<body> + Sortiere nach: <%=searchFor%> + <form> + <table border=1> + <tr> + <td class="trtext invis"><%=name[0]%><br /><%=abstracts[0]%><br /><br />Erstflug: <%=dates[0]%><br />Stueckzahl: <%=stueck[0]%><br /><br />Hersteller: <%=manufactor[0]%><br /><%=mfabstract[0]%></td> + <td><img src="<%=imgUrls[0]%>" /></td> + <td><img src="<%=imgUrls[1]%>" /></td> + <td class="trtext invis"><%=name[1]%><br /><%=abstracts[1]%><br />Erstflug: <%=dates[1]%><br />Stueckzahl: <%=stueck[1]%><br /><br />Hersteller: <%=manufactor[1]%><br /><%=mfabstract[1]%></td> + </tr> + <tr> + <td class="invis"></td> + <td class="selector"><input type="radio" name="first" value="1" + onclick="swap(1, 1)" /> <input type="radio" name="first" value="2" + onclick="swap(1, 2)" /> <input type="radio" name="first" value="3" + onclick="swap(1, 3)" /> <input type="radio" name="first" value="4" + onclick="swap(1, 4)" /></td> + <td class="selector"><input type="radio" name="second" + value="1" onclick="swap(2, 1)" /> <input type="radio" + name="second" value="2" onclick="swap(2, 2)" /> <input + type="radio" name="second" value="3" onclick="swap(2, 3)" /> <input + type="radio" name="second" value="4" onclick="swap(2, 4)" /></td> + <td class="invis"></td> + </tr> + <tr> + <td class="invis"></td> + <td class="selector"><input type="radio" name="third" value="1" + onclick="swap(3, 1)" /> <input type="radio" name="third" value="2" + onclick="swap(3, 2)" /> <input type="radio" name="third" value="3" + onclick="swap(3, 3)" /> <input type="radio" name="third" value="4" + onclick="swap(3, 4)" /></td> + <td class="selector"><input type="radio" name="fourth" + value="1" onclick="swap(4, 1)" /> <input type="radio" + name="fourth" value="2" onclick="swap(4, 2)" /> <input + type="radio" name="fourth" value="3" onclick="swap(4, 3)" /> <input + type="radio" name="fourth" value="4" onclick="swap(4, 4)" /></td> + <td class="invis"></td> + </tr> + <tr> + <td class="trtext invis"><%=name[2]%><br /><%=abstracts[2]%><br />Erstflug: <%=dates[2]%><br />Stueckzahl: <%=stueck[2]%><br /><br />Hersteller: <%=manufactor[2]%><br /><%=mfabstract[2]%></td> + <td><img src="<%=imgUrls[2]%>" /></td> + <td><img src="<%=imgUrls[3]%>" /></td> + <td class="trtext invis"><%=name[3]%><br /><%=abstracts[3]%><br />Erstflug: <%=dates[3]%><br />Stueckzahl: <%=stueck[3]%><br /><br />Hersteller: <%=manufactor[3]%><br /><%=mfabstract[3]%></td> + </tr> + </table> + <input type="button" value="Bewerten" onclick="checkResult()" /> + </form> + Made by + <span property="dc:creator"> + <span typeof="foaf:Person" property="foaf:name">Timo Gielser</span>, + <span typeof="foaf:Person" property="foaf:name">Markus Mies</span>, + <span typeof="foaf:Person" property="foaf:name">Julian Petrasch</span>, + <span typeof="foaf:Person" property="foaf:name">Thiery Meurers</span>, + <span typeof="foaf:Person" property="foaf:name">Alexis Iakovenko</span> and + <span typeof="foaf:Person" property="foaf:name">Shiho Onitsuka</span> + </span> +</body> +</html> \ No newline at end of file