Commit 41439c68 authored by Franz Reischl's avatar Franz Reischl 🏖
Browse files

Merge branch 'master' into combination

parents 816f04f9 b2e75f59
package university.at.jku.ce;
import java.util.HashMap;
import java.util.Map;
import university.at.jku.ce.model.Student;
import university.at.jku.ce.model.Study;
import university.at.jku.ce.model.Subject;
public class Database {
private static Map<Long ,Student> students=new HashMap<Long,Student>();
private static Map<Long ,Subject> subjects=new HashMap<Long,Subject>();
private static Map<Long ,Study> studies=new HashMap<Long,Study>();
public static Map<Long, Student> getStudents() {
return students;
}
public static Map<Long, Subject> getSubjects() {
return subjects;
}
public static Map<Long, Study> getStudies() {
return studies;
}
}
package university.at.jku.ce.dao;
import java.util.Map;
import university.at.jku.ce.model.Study;
public interface StudyDao {
public abstract Map<Long,Study> getAllStudies(String name);
public abstract Study getStudy(Long id);
public abstract Study addStudy(Study study);
public abstract Study updateStudy(Study study);
public abstract void removeStudy(long studyId);
}
package university.at.jku.ce.dao;
import java.util.Map;
import university.at.jku.ce.model.Subject;
public interface SubjectDao {
public abstract Map<Long,Subject> getAllSubjects(Long studyId);
public abstract Subject getSubject(long studyId, long id);
public abstract Subject addSubject (long studyId, Subject subject);
public abstract Subject updateSubject (long studyId, Subject subject);
public abstract void removeSubject (long studyId, long id);
}
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());
b.append("|");
b.append(entry.getValue().getInfo());
lineList.add(b.toString());
}
for (String st:lineList) {bw.write(st); bw.write(n);}
bw.close();
} catch (Throwable t) {t.printStackTrace();}
}
public long getNextId() {
long nextId=0;
for (Subject s:subjects.values()) {
if (s.getId()>nextId) {
nextId=s.getId();
}
}
return nextId+1;
}
}
...@@ -18,6 +18,11 @@ public class Student { ...@@ -18,6 +18,11 @@ public class Student {
public Student () {}; public Student () {};
public Student(String firstName, String lastName) {
this.firstName=firstName;
this.lastName=lastName;
}
public Student(int matrNr, String firstName, String lastName) { public Student(int matrNr, String firstName, String lastName) {
this.matrNr=matrNr; this.matrNr=matrNr;
this.firstName=firstName; this.firstName=firstName;
......
...@@ -13,14 +13,15 @@ public class Study { ...@@ -13,14 +13,15 @@ public class Study {
private long studyId; private long studyId;
private String name; private String name;
private Map <Long, Subject> subjects=new HashMap<Long,Subject>();
public Study() {} public Study() {}
public Study(long studyId, String name, Map<Long, Subject> subjects) { public Study(String name) {
this.name=name; this.name=name;
}
public Study (long studyId, String name) {
this.studyId=studyId; this.studyId=studyId;
this.subjects=subjects; this.name=name;
} }
public long getStudyId() { public long getStudyId() {
...@@ -38,13 +39,4 @@ public class Study { ...@@ -38,13 +39,4 @@ public class Study {
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
@JsonbTransient
public Map<Long, Subject> getSubjects() {
return subjects;
}
public void setSubjects(Map<Long, Subject> subjects) {
this.subjects = subjects;
}
} }
...@@ -10,6 +10,11 @@ public class Subject { ...@@ -10,6 +10,11 @@ public class Subject {
public Subject() {}; public Subject() {};
public Subject(String name, String info) {
this.name=name;
this.info=info;
}
public Subject(long id, String name, String info) { public Subject(long id, String name, String info) {
this.id=id; this.id=id;
this.name=name; this.name=name;
...@@ -39,4 +44,9 @@ public class Subject { ...@@ -39,4 +44,9 @@ public class Subject {
public void setId(long id) { public void setId(long id) {
this.id = id; this.id = id;
} }
} }
...@@ -15,87 +15,88 @@ import javax.ws.rs.core.MediaType; ...@@ -15,87 +15,88 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import university.at.jku.ce.model.Student; import university.at.jku.ce.model.Student;
import university.at.jku.ce.service.StudentService; import university.at.jku.ce.service.Service;
import university.at.jku.ce.service.ServiceImpl;
@Path("/students") @Path("/students")
public class StudentResource { public class StudentResource {
StudentService studentService=new StudentService(); Service service=new ServiceImpl();
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getStudents() {
List<Student>list= studentService.getAllStudents();
GenericEntity<List<Student>> entity=new GenericEntity<List<Student>>(list) {};
return Response.ok(entity).build();
}
// @GET // @GET
// @Produces(MediaType.APPLICATION_XML) // @Produces(MediaType.APPLICATION_JSON)
// public List <Student> getStudents() { // public Response getStudents() {
// return studentService.getAllStudents(); // List<Student>list= studentService.getAllStudents();
// GenericEntity<List<Student>> entity=new GenericEntity<List<Student>>(list) {};
// return Response.ok(entity).build();
// } // }
//
@POST //// @GET
@Consumes(MediaType.APPLICATION_JSON) //// @Produces(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_JSON) //// public List <Student> getStudents() {
public Student addStudent(Student student) { //// return studentService.getAllStudents();
return studentService.addStudent(student); //// }
} //
// @POST // @POST
// @Consumes(MediaType.APPLICATION_XML) // @Consumes(MediaType.APPLICATION_JSON)
// @Produces(MediaType.APPLICATION_XML) // @Produces(MediaType.APPLICATION_JSON)
// public Student addMessage(Student student) { // public Student addStudent(Student student) {
// return studentService.addStudent(student); // return studentService.addStudent(student);
// } // }
//
@GET //// @POST
@Path("/{studentMatrNr}") //// @Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_JSON) //// @Produces(MediaType.APPLICATION_XML)
public Student getStudent(@PathParam("studentMatrNr")long matrNr) { //// public Student addMessage(Student student) {
return studentService.getStudent(matrNr); //// return studentService.addStudent(student);
} //// }
//
// @GET // @GET
// @Path("/{studentMatrNr}") // @Path("/{studentMatrNr}")
// @Produces(MediaType.APPLICATION_XML) // @Produces(MediaType.APPLICATION_JSON)
// public Student getStudent(@PathParam("studentMatrNr")long matrNr) { // public Student getStudent(@PathParam("studentMatrNr")long matrNr) {
// return studentService.getStudent(matrNr); // return studentService.getStudent(matrNr);
// } // }
//
@PUT //// @GET
@Path("/{studentMatrNr}") //// @Path("/{studentMatrNr}")
@Consumes(MediaType.APPLICATION_JSON) //// @Produces(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_JSON) //// public Student getStudent(@PathParam("studentMatrNr")long matrNr) {
public Student updateStudent(@PathParam("studentMatrNr")long matrNr,Student student) { //// return studentService.getStudent(matrNr);
student.setMatrNr(matrNr); //if User does not send correct id it is still used correct id from URL //// }
return studentService.updateStudent(student); //
}
// @PUT // @PUT
// @Path("/{studentMatrNr}") // @Path("/{studentMatrNr}")
// @Consumes(MediaType.APPLICATION_XML) // @Consumes(MediaType.APPLICATION_JSON)
// @Produces(MediaType.APPLICATION_XML) // @Produces(MediaType.APPLICATION_JSON)
// public Student updateStudent(@PathParam("studentMatrNr")long matrNr,Student student) { // public Student updateStudent(@PathParam("studentMatrNr")long matrNr,Student student) {
// student.setMatrNr(matrNr); //if User does not send correct id it is still used correct id from URL // student.setMatrNr(matrNr); //if User does not send correct id it is still used correct id from URL
// return studentService.updateStudent(student); // return studentService.updateStudent(student);
// }