Commit 32f5b52f authored by Michael Schimpelsberger's avatar Michael Schimpelsberger
Browse files

- Schnittstelle Student erweitert

- H2 Datenbank eingebunden (aktuell nur über absoluten Pfad möglich)
- TextDaos entfernt
parent 643cea84
......@@ -6,16 +6,11 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
......@@ -29,5 +24,6 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="src/main/webapp/WEB-INF/lib/h2-1.4.197.jar"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
......
eclipse.preferences.version=1
org.eclipse.m2e.wtp.enabledProjectSpecificPrefs=false
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<fixed facet="wst.jsdt.web"/>
<installed facet="java" version="1.8"/>
<installed facet="jst.web" version="2.5"/>
<installed facet="jst.jaxrs" version="2.1"/>
<installed facet="wst.jsdt.web" version="1.0"/>
<installed facet="java" version="1.8"/>
</faceted-project>
DROP TABLE subject CASCADE CONSTRAINTS;
DROP TABLE student CASCADE CONSTRAINTS;
DROP TABLE study CASCADE CONSTRAINTS;
-- Create Tables
CREATE TABLE student (matrnr INTEGER PRIMARY KEY, firstname VARCHAR2(200), lastname VARCHAR2(200));
CREATE TABLE study (studyid INTEGER PRIMARY KEY, name VARCHAR(40));
CREATE TABLE subject (
studyId INTEGER,
subjectid INTEGER,
name VARCHAR2(40),
ects INTEGER,
PRIMARY KEY (studyid, subjectid),
FOREIGN KEY (studyId) REFERENCES study(studyId) ON DELETE CASCADE
);
CREATE TABLE inscription(
matrnr INTEGER,
studyid INTEGER,
ins_date DATE,
FOREIGN KEY (studyid) REFERENCES study(studyid),
FOREIGN KEY (matrnr) REFERENCES student(matrnr),
PRIMARY KEY (matrnr, studyid)
);
-- Insert Data sutdent
INSERT INTO student(matrnr, firstname,lastname) values(1, 'Andres', 'Iniesta');
INSERT INTO student(matrnr, firstname,lastname) values(2, 'Lionel', 'Messi');
INSERT INTO student(matrnr, firstname,lastname) values(3, 'David', 'Silva');
INSERT INTO student(matrnr, firstname,lastname) values(4, 'Luka', 'Modric');
INSERT INTO student(matrnr, firstname,lastname) values(5, 'Marcel', 'Hirscher');
INSERT INTO student(matrnr, firstname,lastname) values(6, 'Henrik', 'Kristoffersen');
INSERT INTO student(matrnr, firstname,lastname) values(7, 'Aksel Lund', 'Svindal');
INSERT INTO student(matrnr, firstname,lastname) values(8, 'Hermann', 'Maier');
-- Insert Data study
INSERT INTO study(studyId, name) values(1, 'Wirtschftsinformatik');
INSERT INTO study(studyId, name) values(2, 'Informatik');
INSERT INTO study(studyId, name) values(3, 'Mathematik');
INSERT INTO study(studyId, name) values(4, 'Physiotherapie');
INSERT INTO study(studyId, name) values(5, 'Gesundheits- und Krankenpflege');
INSERT INTO study(studyId, name) values(6, 'Sportwissenschaften');
-- Insert Data subject
INSERT INTO subject(studyid, subjectid, name, ects) values(1,1,'Communications Engineering',6);
INSERT INTO subject(studyid, subjectid, name, ects) values(1,2,'Grundlagen Softwareentwicklung',6);
INSERT INTO subject(studyid, subjectid, name, ects) values(1,3,'Vertiefung Softwareentwicklung',6);
INSERT INTO subject(studyid, subjectid, name, ects) values(2,1,'Softwareentwicklung 1',6);
INSERT INTO subject(studyid, subjectid, name, ects) values(2,2,'Softwareentwicklung 2',6);
INSERT INTO subject(studyid, subjectid, name, ects) values(2,3,'Elektronik',4);
INSERT INTO subject(studyid, subjectid, name, ects) values(3,1,'Algebra',3);
INSERT INTO subject(studyid, subjectid, name, ects) values(3,2,'Mathematik 1',3);
INSERT INTO subject(studyid, subjectid, name, ects) values(4,1,'Bewegungslehre',6);
INSERT INTO subject(studyid, subjectid, name, ects) values(4,2,'Trainingslehre',2);
INSERT INTO subject(studyid, subjectid, name, ects) values(4,3,'Spezifische Anatomie',2);
INSERT INTO subject(studyid, subjectid, name, ects) values(5,1,'Allgemeine Anatomie',3);
INSERT INTO subject(studyid, subjectid, name, ects) values(5,2,'Allgemeine Physiologie',3);
INSERT INTO subject(studyid, subjectid, name, ects) values(6,1,'Trainingslehre',6);
INSERT INTO subject(studyid, subjectid, name, ects) values(6,2,'Bewegungslehre',3);
INSERT INTO inscription(matrnr, studyid, ins_date) values (1,1,sysdate);
INSERT INTO inscription(matrnr, studyid, ins_date) values (1,2,sysdate);
INSERT INTO inscription(matrnr, studyid, ins_date) values (1,3,sysdate);
INSERT INTO inscription(matrnr, studyid, ins_date) values (2,1,sysdate);
INSERT INTO inscription(matrnr, studyid, ins_date) values (2,2,sysdate);
INSERT INTO inscription(matrnr, studyid, ins_date) values (3,3,sysdate);
\ No newline at end of file
This diff is collapsed.
......@@ -25,6 +25,8 @@
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
......@@ -75,12 +77,16 @@
<artifactId>jaxb-impl</artifactId>
<version>2.2-promoted-b65</version>
</dependency>
</dependencies>
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<jersey.version>2.27</jersey.version>
......
......@@ -4,8 +4,18 @@ import java.util.List;
import java.util.Map;
import university.at.jku.ce.model.Student;
import university.at.jku.ce.model.Study;
public interface StudentDao {
public Student getStudent(long matrNr);
public Map<Long,Student>getAllStudents();
public Student getStudent(int matrNr);
public List<Student>getAllStudents();
public Student addStudent(Student student);
public Student updateStudent(Student student);
public void removeStudent(int matrNr);
public List<Study> getInscriptions(int matrNr);
}
package university.at.jku.ce.dao;
import java.util.List;
import java.util.Map;
import university.at.jku.ce.model.Study;
public interface StudyDao {
public abstract Map<Long,Study> getAllStudies(String name);
public List<Study> getAllStudies(String name);
public abstract Study getStudy(Long id);
public Study getStudy(int studyId);
public abstract Study addStudy(Study study);
public Study addStudy(Study study);
public abstract Study updateStudy(Study study);
public Study updateStudy(Study study);
public abstract void removeStudy(long studyId);
public void removeStudy(int studyId);
}
package university.at.jku.ce.dao;
import java.util.List;
import java.util.Map;
import university.at.jku.ce.model.Subject;
public interface SubjectDao {
public abstract Map<Long,Subject> getAllSubjects(Long studyId);
public List<Subject> getAllSubjects(int studyId);
public abstract Subject getSubject(long studyId, long id);
public Subject getSubject(int studyId,int subjectId);
public abstract Subject addSubject (long studyId, Subject subject);
public Subject addSubject (Subject subject);
public abstract Subject updateSubject (long studyId, Subject subject);
public Subject updateSubject (Subject subject);
public abstract void removeSubject (long studyId, long id);
public void removeSubject (int studyId, int subjectId);
}
package university.at.jku.ce.dao;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import university.at.jku.ce.model.Student;
import university.at.jku.ce.model.Study;
public class TextStudentDao implements StudentDao {
Map <Long,Student> students;
@Override
public Student getStudent(long matrNr) {
getAllStudents();
return students.get(matrNr);
}
@Override
public Map<Long,Student> getAllStudents() {
ArrayList <String> lineList=new ArrayList<String>();
students=new HashMap<Long,Student>();
try {
FileReader fr = new FileReader("C:\\Users\\Michael\\development\\workspace-ce5\\at.jku.ce\\src\\main\\resources\\Students.txt");
BufferedReader br = new BufferedReader(fr);
String line="";
do{
line = br.readLine();
lineList.add(line);
}
while (line != null);
br.close();
} catch (Throwable t) {t.printStackTrace();}
for(String s: lineList) {
if (s!=null) {
int i=0;
Long matrNr;
String firstName;
String lastName;
StringBuilder b=new StringBuilder();
while (s.charAt(i)!='|') {
b.append(s.charAt(i));
i++;
}
matrNr=Long.parseLong(b.toString());
b=new StringBuilder();
i++;
while (s.charAt(i)!='|') {
b.append(s.charAt(i));
i++;
}
firstName=b.toString();
b=new StringBuilder();
i++;
while (i<s.length()) {
b.append(s.charAt(i));
i++;
}
lastName=b.toString();
students.put(matrNr, new Student(matrNr,firstName,lastName));
}
}
return students;
}
}
package university.at.jku.ce.dao;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import university.at.jku.ce.model.Study;
public class TextStudyDao implements StudyDao {
Map <Long,Study> studies;
@Override
public Map<Long,Study> getAllStudies(String name) {
ArrayList <String> lineList=new ArrayList<String>();
studies=new HashMap<Long,Study>();
try {
FileReader fr = new FileReader("C:\\Users\\Michael\\development\\workspace-ce5\\at.jku.ce\\src\\main\\resources\\Studies.txt");
BufferedReader br = new BufferedReader(fr);
String line="";
do{
line = br.readLine();
lineList.add(line);
}
while (line != null);
br.close();
} catch (Throwable t) {t.printStackTrace();}
for(String s: lineList) {
if (s!=null) {
int i=0;
Long studyId;
String studyName;
StringBuilder b=new StringBuilder();
while (s.charAt(i)!='|') {
b.append(s.charAt(i));
i++;
}
studyId=Long.parseLong(b.toString());
b=new StringBuilder();
i++;
while (i<s.length()) {
b.append(s.charAt(i));
i++;
}
studyName=b.toString();
if (name==null||name.equals("")) {
studies.put(studyId, new Study(studyId,studyName));
}
else {
if(studyName.contains(name)) {
studies.put(studyId, new Study(studyId,studyName));
}
}
}
}
return studies;
}
@Override
public Study getStudy(Long id) {
studies=getAllStudies(null);
return studies.get(id);
}
@Override
public Study addStudy(Study study) {
studies=getAllStudies(null);
study.setStudyId(getNextId());
studies.put(study.getStudyId(), study);
write();
return study;
}
@Override
public Study updateStudy(Study study) {
studies=getAllStudies(null);
if (studies.containsKey(study.getStudyId())) {
studies.put(study.getStudyId(), study);
}
write();
return study;
}
@Override
public void removeStudy(long studyId) {
studies=getAllStudies(null);
studies.remove(studyId);
write();
}
void write() {
try {
FileWriter fw = new FileWriter("C:\\Users\\Michael\\development\\workspace-ce5\\at.jku.ce\\src\\main\\resources\\Studies.txt");
BufferedWriter bw = new BufferedWriter(fw);
String n = System.getProperty("line.separator");
ArrayList <String> lineList=new ArrayList<String>();
for (Study s:studies.values()) {
StringBuilder b=new StringBuilder();
b.append(s.getStudyId());
b.append("|");
b.append(s.getName());
lineList.add(b.toString());
}
for (String s:lineList) {bw.write(s); bw.write(n);}
bw.close();
} catch (Throwable t) {t.printStackTrace();}
}
public long getNextId() {
long nextId=0;
for (Study s:studies.values()) {
if (s.getStudyId()>nextId) {
nextId=s.getStudyId();
}
}
return nextId+1;
}
}
package university.at.jku.ce.dao;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import university.at.jku.ce.model.Study;
import university.at.jku.ce.model.Subject;
public class TextSubjectDao implements SubjectDao {
Map <Long,Subject> subjects;
Map <SubjectId,Subject> allSubjects;
class SubjectId{
Long studyId;
Long subjectId;
SubjectId(Long studyId, Long subjectId) {
this.studyId=studyId;
this.subjectId=subjectId;
}
@Override
public boolean equals (Object o) {
if (o instanceof SubjectId) {
SubjectId si=(SubjectId)o;
if (this.studyId.equals(si.studyId)&&this.subjectId.equals(si.subjectId)) return true;
}
return false;
}
@Override
public int hashCode() {
int code=(int) (((studyId+subjectId)*(studyId+subjectId+1))/2+subjectId);
return code;
}
}
@Override
public Map<Long,Subject> getAllSubjects(Long studyId) {
ArrayList <String> lineList=new ArrayList<String>();
allSubjects=new HashMap<SubjectId,Subject>();
subjects=new HashMap<Long,Subject>();
try {
FileReader fr = new FileReader("C:\\Users\\Michael\\development\\workspace-ce5\\at.jku.ce\\src\\main\\resources\\Subjects.txt");
BufferedReader br = new BufferedReader(fr);
String line="";
do{
line = br.readLine();
lineList.add(line);
}
while (line != null);
br.close();
} catch (Throwable t) {t.printStackTrace();}
for(String s: lineList) {
if (s!=null) {
int i=0;
Long textStudyId;
Long id;
String subjectName;
String subjectDesc;
StringBuilder b=new StringBuilder();
while (s.charAt(i)!='|') {
b.append(s.charAt(i));
i++;
}
textStudyId=Long.parseLong(b.toString());
b=new StringBuilder();
i++;
while (s.charAt(i)!='|') {
b.append(s.charAt(i));
i++;
}
id=Long.parseLong(b.toString());
b=new StringBuilder();
i++;
while (s.charAt(i)!='|') {
b.append(s.charAt(i));
i++;
}
subjectName=b.toString();
b=new StringBuilder();
i++;
while (i<s.length()) {
b.append(s.charAt(i));
i++;
}
subjectDesc=b.toString();
Subject subject=new Subject(id,subjectName,subjectDesc);
if(studyId==textStudyId) {
subjects.put(id, subject);
}
allSubjects.put(new SubjectId(textStudyId,id), subject);
}
}
return subjects;
}
@Override
public Subject getSubject(long studyId, long id) {
subjects=getAllSubjects(studyId);
return subjects.get(id);
}
@Override
public Subject addSubject(long studyId, Subject subject) {
subjects=getAllSubjects(studyId);
subject.setId(getNextId());
subjects.put(subject.getId(), subject);
allSubjects.put(new SubjectId(studyId,subject.getId()), subject);
write();
return subject;
}
@Override
public Subject updateSubject(long studyId, Subject subject) {
subjects=getAllSubjects(studyId);
subjects.put(subject.getId(), subject);
allSubjects.put(new SubjectId(studyId,subject.getId()), subject);
write();
return subject;
}
@Override
public void removeSubject(long studyId, long id) {
subjects=getAllSubjects(studyId);
subjects.remove(id);
allSubjects.remove(new SubjectId(studyId, id));
write();
}
void write() {
ArrayList <String> lineList=new ArrayList<String>();
try {
FileWriter fw = new FileWriter("C:\\Users\\Michael\\development\\workspace-ce5\\at.jku.ce\\src\\main\\resources\\Subjects.txt");
BufferedWriter bw = new BufferedWriter(fw);
String n = System.getProperty("line.separator");
for (Entry<SubjectId, Subject> entry:allSubjects.entrySet()) {
StringBuilder b=new StringBuilder();
b.append(entry.getKey().studyId);
b.append("|");
b.append(entry.getValue().getId());
b.append("|");
b.append(entry.getValue().getName());